blob: 5930caa32f85958af5da03640ca581470fe673f2 [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'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100761#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100762
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100763#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600764
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100765// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600766// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100767enum {
768 BC_PARSE_EXPRS_BITS = 0
769 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
770 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
771 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
772 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
773 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
774 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
775 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
776 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600777};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100778static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
779{
780#if ULONG_MAX > 0xffffffff
781 // 64-bit version (will not work correctly for 32-bit longs!)
782 return BC_PARSE_EXPRS_BITS & (1UL << i);
783#else
784 // 32-bit version
785 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
786 if (i >= 32) {
787 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
788 i &= 31;
789 }
790 return m & (1UL << i);
791#endif
792}
Gavin Howard01055ba2018-11-03 11:00:21 -0600793
794// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100795static const uint8_t bc_parse_ops[] = {
796#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100797 OP(0, false), OP( 0, false ), // inc dec
798 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100799 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100800 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
801 OP(4, true ), OP( 4, true ), // mod + -
802 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
803 OP(1, false), // not
804 OP(7, true ), OP( 7, true ), // or and
805 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
806 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100807#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600808};
Denys Vlasenko65437582018-12-05 19:37:19 +0100809#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
810#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600811
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100812// Byte array of up to 4 BC_LEX's, packed into 32-bit word
813typedef uint32_t BcParseNext;
814
Gavin Howard01055ba2018-11-03 11:00:21 -0600815// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100816enum {
817#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
818#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
819#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
820 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
821 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
822 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
823 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
824 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
825 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
826 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
827#undef BC_PARSE_NEXT4
828#undef BC_PARSE_NEXT2
829#undef BC_PARSE_NEXT1
830};
Gavin Howard01055ba2018-11-03 11:00:21 -0600831#endif // ENABLE_BC
832
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100833#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100834static const //BcLexType - should be this type, but narrower type saves size:
835uint8_t
836dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600837 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
838 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
839 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
840 BC_LEX_STORE_PUSH,
841};
842
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100843static const //BcLexType - should be this type
844uint8_t
845dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600846 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
847 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
848 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
849 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
850 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
851 BC_LEX_INVALID, BC_LEX_INVALID,
852 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
853 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
854 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
855 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
856 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
857 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
858 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
859 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
860 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
861 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
862 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
863 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
864 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
865 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
866 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
867 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
868 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
869 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
870 BC_LEX_INVALID
871};
872
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100873static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
874int8_t
875dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600876 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
877 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
878 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
879 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
880 BC_INST_INVALID, BC_INST_INVALID,
881 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
882 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
883 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
884 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
885 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
886 BC_INST_INVALID, BC_INST_INVALID,
887 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
888 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
889 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
890 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
891 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
892 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
893 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
894 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
895 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
896 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
897 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
898 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
899};
900#endif // ENABLE_DC
901
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100902// In configurations where errors abort instead of propagating error
903// return code up the call chain, functions returning BC_STATUS
904// actually don't return anything, they always succeed and return "void".
905// A macro wrapper is provided, which makes this statement work:
906// s = zbc_func(...)
907// and makes it visible to the compiler that s is always zero,
908// allowing compiler to optimize dead code after the statement.
909//
910// To make code more readable, each such function has a "z"
911// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
912//
913#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100914# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100915# define ERRORFUNC /*nothing*/
916# define ERROR_RETURN(a) a
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100917//moved up: # define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100918# define RETURN_STATUS(v) return (v)
919#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100920# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100921# define ERRORFUNC NORETURN
922# define ERROR_RETURN(a) /*nothing*/
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100923//moved up: # define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100924# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
925#endif
926
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100927#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
928#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
929#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
930//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
931static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
932{
933 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
934}
935//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
936static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
937{
938 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
939}
940
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100941typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
942
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100943typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100944
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100945static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
946 BcNumBinaryOp op, size_t req);
947static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
948static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
949static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
950static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
951static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
952static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
953
954static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
955{
956 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
957 (void) scale;
958 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
959}
960
961static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
962{
963 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
964 (void) scale;
965 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
966}
967
968static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
969{
970 size_t req = BC_NUM_MREQ(a, b, scale);
971 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
972}
973
974static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
975{
976 size_t req = BC_NUM_MREQ(a, b, scale);
977 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
978}
979
980static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
981{
982 size_t req = BC_NUM_MREQ(a, b, scale);
983 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
984}
985
986static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
987{
988 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
989}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100990
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100991static const BcNumBinaryOp zbc_program_ops[] = {
992 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 -0600993};
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100994#if ERRORS_ARE_FATAL
995# define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS)
996# define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS)
997# define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS)
998# define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS)
999# define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS)
1000# define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS)
1001#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001002
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001003static void fflush_and_check(void)
1004{
1005 fflush_all();
1006 if (ferror(stdout) || ferror(stderr))
1007 bb_perror_msg_and_die("output error");
1008}
1009
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001010#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001011#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001012do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001013 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001014 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001015 return BC_STATUS_FAILURE; \
1016} while (0)
1017#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001018#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001019#endif
1020
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001021static void quit(void) NORETURN;
1022static void quit(void)
1023{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001024 if (ferror(stdin))
1025 bb_perror_msg_and_die("input error");
1026 fflush_and_check();
1027 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001028}
1029
Denys Vlasenko5318f812018-12-05 17:48:01 +01001030static void bc_verror_msg(const char *fmt, va_list p)
1031{
1032 const char *sv = sv; /* for compiler */
1033 if (G.prog.file) {
1034 sv = applet_name;
1035 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
1036 }
1037 bb_verror_msg(fmt, p, NULL);
1038 if (G.prog.file) {
1039 free((char*)applet_name);
1040 applet_name = sv;
1041 }
1042}
1043
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001044static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001045{
1046 va_list p;
1047
1048 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001049 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001050 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001051
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001052 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001053 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001054 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001055}
1056
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001057#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001058static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001059{
1060 va_list p;
1061
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001062 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001063 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001064 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001065
1066 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001067 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001068 va_end(p);
1069
1070 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001071 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001072 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001073 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001074 exit(1);
1075 return BC_STATUS_FAILURE;
1076}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001077#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001078
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001079// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1080// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001081// function must not have caller-cleaned parameters on stack.
1082// Unfortunately, vararg function API does exactly that on most arches.
1083// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001084static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001085{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001086 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1087}
1088static ERRORFUNC int bc_error_bad_character(char c)
1089{
1090 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1091}
1092static ERRORFUNC int bc_error_bad_expression(void)
1093{
1094 ERROR_RETURN(return) bc_error("bad expression");
1095}
1096static ERRORFUNC int bc_error_bad_token(void)
1097{
1098 ERROR_RETURN(return) bc_error("bad token");
1099}
1100static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1101{
1102 ERROR_RETURN(return) bc_error("stack has too few elements");
1103}
1104static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1105{
1106 ERROR_RETURN(return) bc_error("variable is wrong type");
1107}
1108static ERRORFUNC int bc_error_nested_read_call(void)
1109{
1110 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001111}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001112#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001113static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001114{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001115 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001116}
Denys Vlasenko00646792018-12-05 18:12:27 +01001117static int bc_POSIX_does_not_allow(const char *msg)
1118{
1119 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1120}
1121static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1122{
1123 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1124}
1125static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1126{
1127 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1128}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001129#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001130
Gavin Howard01055ba2018-11-03 11:00:21 -06001131static void bc_vec_grow(BcVec *v, size_t n)
1132{
1133 size_t cap = v->cap * 2;
1134 while (cap < v->len + n) cap *= 2;
1135 v->v = xrealloc(v->v, v->size * cap);
1136 v->cap = cap;
1137}
1138
1139static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1140{
1141 v->size = esize;
1142 v->cap = BC_VEC_START_CAP;
1143 v->len = 0;
1144 v->dtor = dtor;
1145 v->v = xmalloc(esize * BC_VEC_START_CAP);
1146}
1147
Denys Vlasenko7d628012018-12-04 21:46:47 +01001148static void bc_char_vec_init(BcVec *v)
1149{
1150 bc_vec_init(v, sizeof(char), NULL);
1151}
1152
Gavin Howard01055ba2018-11-03 11:00:21 -06001153static void bc_vec_expand(BcVec *v, size_t req)
1154{
1155 if (v->cap < req) {
1156 v->v = xrealloc(v->v, v->size * req);
1157 v->cap = req;
1158 }
1159}
1160
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001161static void bc_vec_pop(BcVec *v)
1162{
1163 v->len--;
1164 if (v->dtor)
1165 v->dtor(v->v + (v->size * v->len));
1166}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001167
Gavin Howard01055ba2018-11-03 11:00:21 -06001168static void bc_vec_npop(BcVec *v, size_t n)
1169{
1170 if (!v->dtor)
1171 v->len -= n;
1172 else {
1173 size_t len = v->len - n;
1174 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1175 }
1176}
1177
Denys Vlasenko7d628012018-12-04 21:46:47 +01001178static void bc_vec_pop_all(BcVec *v)
1179{
1180 bc_vec_npop(v, v->len);
1181}
1182
Gavin Howard01055ba2018-11-03 11:00:21 -06001183static void bc_vec_push(BcVec *v, const void *data)
1184{
1185 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1186 memmove(v->v + (v->size * v->len), data, v->size);
1187 v->len += 1;
1188}
1189
1190static void bc_vec_pushByte(BcVec *v, char data)
1191{
1192 bc_vec_push(v, &data);
1193}
1194
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001195static void bc_vec_pushZeroByte(BcVec *v)
1196{
1197 //bc_vec_pushByte(v, '\0');
1198 // better:
1199 bc_vec_push(v, &const_int_0);
1200}
1201
Gavin Howard01055ba2018-11-03 11:00:21 -06001202static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1203{
1204 if (idx == v->len)
1205 bc_vec_push(v, data);
1206 else {
1207
1208 char *ptr;
1209
1210 if (v->len == v->cap) bc_vec_grow(v, 1);
1211
1212 ptr = v->v + v->size * idx;
1213
1214 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1215 memmove(ptr, data, v->size);
1216 }
1217}
1218
1219static void bc_vec_string(BcVec *v, size_t len, const char *str)
1220{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001221 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001222 bc_vec_expand(v, len + 1);
1223 memcpy(v->v, str, len);
1224 v->len = len;
1225
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001226 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001227}
1228
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001229#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001230static void bc_vec_concat(BcVec *v, const char *str)
1231{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001232 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001233
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001234 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001235
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001236 slen = strlen(str);
1237 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001238
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001239 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001240 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001241
1242 v->len = len;
1243}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001244#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001245
1246static void *bc_vec_item(const BcVec *v, size_t idx)
1247{
1248 return v->v + v->size * idx;
1249}
1250
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001251static char** bc_program_str(size_t idx)
1252{
1253 return bc_vec_item(&G.prog.strs, idx);
1254}
1255
1256static BcFunc* bc_program_func(size_t idx)
1257{
1258 return bc_vec_item(&G.prog.fns, idx);
1259}
1260
Gavin Howard01055ba2018-11-03 11:00:21 -06001261static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1262{
1263 return v->v + v->size * (v->len - idx - 1);
1264}
1265
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001266static void *bc_vec_top(const BcVec *v)
1267{
1268 return v->v + v->size * (v->len - 1);
1269}
1270
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001271static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001272{
1273 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001274 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001275 free(v->v);
1276}
1277
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001278static int bc_id_cmp(const void *e1, const void *e2)
1279{
1280 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1281}
1282
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001283static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001284{
1285 free(((BcId *) id)->name);
1286}
1287
Gavin Howard01055ba2018-11-03 11:00:21 -06001288static size_t bc_map_find(const BcVec *v, const void *ptr)
1289{
1290 size_t low = 0, high = v->len;
1291
1292 while (low < high) {
1293
1294 size_t mid = (low + high) / 2;
1295 BcId *id = bc_vec_item(v, mid);
1296 int result = bc_id_cmp(ptr, id);
1297
1298 if (result == 0)
1299 return mid;
1300 else if (result < 0)
1301 high = mid;
1302 else
1303 low = mid + 1;
1304 }
1305
1306 return low;
1307}
1308
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001309static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001310{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001311 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001312
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001313 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001314 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001315 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1316 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001317 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001318 bc_vec_pushAt(v, ptr, n);
1319 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001320}
1321
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001322#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001323static size_t bc_map_index(const BcVec *v, const void *ptr)
1324{
1325 size_t i = bc_map_find(v, ptr);
1326 if (i >= v->len) return BC_VEC_INVALID_IDX;
1327 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1328}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001329#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001330
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001331static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001332{
1333 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1334 || c > 0x7e
1335 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001336 bc_error_fmt("illegal character 0x%02x", c);
1337 return 1;
1338 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001339 return 0;
1340}
1341
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001342// Note: it _appends_ data from the stdin to vec.
Denys Vlasenko8a892472018-12-12 22:43:58 +01001343static void bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001344{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001345 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001346 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001347
Gavin Howard01055ba2018-11-03 11:00:21 -06001348#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001349 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001350 intr:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001351 G_interrupt = 0;
1352 // GNU bc says "interrupted execution."
1353 // GNU dc says "Interrupt!"
1354 fputs("\ninterrupted execution\n", stderr);
1355 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001356
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001357# if ENABLE_FEATURE_EDITING
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001358 if (G_ttyin) {
1359 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001360# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001361 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1362 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1363 if (n == 0) // ^C
1364 goto intr;
1365 break;
1366 }
1367 i = 0;
1368 for (;;) {
1369 char c = line_buf[i++];
1370 if (!c) break;
1371 if (bad_input_byte(c)) goto again;
1372 }
1373 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001374# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001375 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001376# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001377#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001378 {
1379 int c;
1380 bool bad_chars = 0;
1381 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001382
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001383 IF_FEATURE_BC_SIGNALS(errno = 0;)
1384 do {
1385 c = fgetc(stdin);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001386#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001387 // Both conditions appear simultaneously, check both just in case
1388 if (errno == EINTR || G_interrupt) {
1389 // ^C was pressed
1390 clearerr(stdin);
1391 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001392 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001393#endif
1394 if (c == EOF) {
1395 if (ferror(stdin))
1396 quit(); // this emits error message
1397 // Note: EOF does not append '\n', therefore:
1398 // printf 'print 123\n' | bc - works
1399 // printf 'print 123' | bc - fails (syntax error)
1400 break;
1401 }
1402 bad_chars |= bad_input_byte(c);
1403 bc_vec_pushByte(vec, (char)c);
1404 } while (c != '\n');
1405 if (bad_chars) {
1406 // Bad chars on this line, ignore entire line
1407 vec->len = len;
1408 goto again;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001409 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001410 bc_vec_pushZeroByte(vec);
1411 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001412}
1413
Denys Vlasenkodf515392018-12-02 19:27:48 +01001414static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001415{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001416 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001417 size_t size = ((size_t) -1);
1418 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001419
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001420 // Never returns NULL (dies on errors)
1421 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001422
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001423 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001424 char c = buf[i];
1425 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1426 || c > 0x7e
1427 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001428 free(buf);
1429 buf = NULL;
1430 break;
1431 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001432 }
1433
Denys Vlasenkodf515392018-12-02 19:27:48 +01001434 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001435}
1436
Gavin Howard01055ba2018-11-03 11:00:21 -06001437static void bc_num_setToZero(BcNum *n, size_t scale)
1438{
1439 n->len = 0;
1440 n->neg = false;
1441 n->rdx = scale;
1442}
1443
1444static void bc_num_zero(BcNum *n)
1445{
1446 bc_num_setToZero(n, 0);
1447}
1448
1449static void bc_num_one(BcNum *n)
1450{
1451 bc_num_setToZero(n, 0);
1452 n->len = 1;
1453 n->num[0] = 1;
1454}
1455
1456static void bc_num_ten(BcNum *n)
1457{
1458 bc_num_setToZero(n, 0);
1459 n->len = 2;
1460 n->num[0] = 0;
1461 n->num[1] = 1;
1462}
1463
Denys Vlasenko3129f702018-12-09 12:04:44 +01001464// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001465static void bc_num_init(BcNum *n, size_t req)
1466{
1467 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001468 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001469 n->num = xmalloc(req);
1470 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001471 n->rdx = 0;
1472 n->len = 0;
1473 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001474}
1475
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001476static void bc_num_init_DEF_SIZE(BcNum *n)
1477{
1478 bc_num_init(n, BC_NUM_DEF_SIZE);
1479}
1480
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001481static void bc_num_expand(BcNum *n, size_t req)
1482{
1483 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1484 if (req > n->cap) {
1485 n->num = xrealloc(n->num, req);
1486 n->cap = req;
1487 }
1488}
1489
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001490static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001491{
1492 free(((BcNum *) num)->num);
1493}
1494
1495static void bc_num_copy(BcNum *d, BcNum *s)
1496{
1497 if (d != s) {
1498 bc_num_expand(d, s->cap);
1499 d->len = s->len;
1500 d->neg = s->neg;
1501 d->rdx = s->rdx;
1502 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1503 }
1504}
1505
Denys Vlasenko29301232018-12-11 15:29:32 +01001506static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001507{
1508 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001509 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001510
Denys Vlasenko29301232018-12-11 15:29:32 +01001511 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001512
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001513 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001514
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001515 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001516
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001517 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001518 pow *= 10;
1519
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001520 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001521 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001522 prev = result;
1523 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001524 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001525 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001526
Denys Vlasenko29301232018-12-11 15:29:32 +01001527 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001528}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001529#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001530# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001531#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001532
1533static void bc_num_ulong2num(BcNum *n, unsigned long val)
1534{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001535 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001536
1537 bc_num_zero(n);
1538
1539 if (val == 0) return;
1540
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001541 if (ULONG_MAX == 0xffffffffUL)
1542 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001543 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001544 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001545 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001546
1547 ptr = n->num;
1548 for (;;) {
1549 n->len++;
1550 *ptr++ = val % 10;
1551 val /= 10;
1552 if (val == 0) break;
1553 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001554}
1555
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001556static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001557 size_t len)
1558{
1559 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001560 for (i = 0; i < len; ++i) {
1561 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001562 a[i + j++] += 10;
1563 a[i + j] -= 1;
1564 }
1565 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001566}
1567
1568static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1569{
1570 size_t i;
1571 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001572 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001573 return BC_NUM_NEG(i + 1, c < 0);
1574}
1575
1576static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1577{
1578 size_t i, min, a_int, b_int, diff;
1579 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001580 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001581 ssize_t cmp;
1582
1583 if (a == b) return 0;
1584 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1585 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001586
1587 if (a->neg != b->neg) // signs of a and b differ
1588 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1589 return (int)b->neg - (int)a->neg;
1590 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001591
1592 a_int = BC_NUM_INT(a);
1593 b_int = BC_NUM_INT(b);
1594 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001595
1596 if (a_int != 0) return (ssize_t) a_int;
1597
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001598 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001599 if (a_max) {
1600 min = b->rdx;
1601 diff = a->rdx - b->rdx;
1602 max_num = a->num + diff;
1603 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001604 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1605 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001606 min = a->rdx;
1607 diff = b->rdx - a->rdx;
1608 max_num = b->num + diff;
1609 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001610 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001611 }
1612
1613 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001614 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001615
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001616 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001617 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001618 }
1619
1620 return 0;
1621}
1622
1623static void bc_num_truncate(BcNum *n, size_t places)
1624{
1625 if (places == 0) return;
1626
1627 n->rdx -= places;
1628
1629 if (n->len != 0) {
1630 n->len -= places;
1631 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1632 }
1633}
1634
1635static void bc_num_extend(BcNum *n, size_t places)
1636{
1637 size_t len = n->len + places;
1638
1639 if (places != 0) {
1640
1641 if (n->cap < len) bc_num_expand(n, len);
1642
1643 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1644 memset(n->num, 0, sizeof(BcDig) * places);
1645
1646 n->len += places;
1647 n->rdx += places;
1648 }
1649}
1650
1651static void bc_num_clean(BcNum *n)
1652{
1653 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1654 if (n->len == 0)
1655 n->neg = false;
1656 else if (n->len < n->rdx)
1657 n->len = n->rdx;
1658}
1659
1660static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1661{
1662 if (n->rdx < scale)
1663 bc_num_extend(n, scale - n->rdx);
1664 else
1665 bc_num_truncate(n, n->rdx - scale);
1666
1667 bc_num_clean(n);
1668 if (n->len != 0) n->neg = !neg1 != !neg2;
1669}
1670
1671static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1672 BcNum *restrict b)
1673{
1674 if (idx < n->len) {
1675
1676 b->len = n->len - idx;
1677 a->len = idx;
1678 a->rdx = b->rdx = 0;
1679
1680 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1681 memcpy(a->num, n->num, idx * sizeof(BcDig));
1682 }
1683 else {
1684 bc_num_zero(b);
1685 bc_num_copy(a, n);
1686 }
1687
1688 bc_num_clean(a);
1689 bc_num_clean(b);
1690}
1691
Denys Vlasenko29301232018-12-11 15:29:32 +01001692static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001693{
Denys Vlasenko29301232018-12-11 15:29:32 +01001694 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001695
1696 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1697 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1698 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001699 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001700 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001701
1702 if (n->rdx >= places)
1703 n->rdx -= places;
1704 else {
1705 bc_num_extend(n, places - n->rdx);
1706 n->rdx = 0;
1707 }
1708
1709 bc_num_clean(n);
1710
Denys Vlasenko29301232018-12-11 15:29:32 +01001711 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001712}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001713#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001714# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001715#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001716
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001717static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001718{
1719 BcNum one;
1720 BcDig num[2];
1721
1722 one.cap = 2;
1723 one.num = num;
1724 bc_num_one(&one);
1725
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001726 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001727}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001728#if ERRORS_ARE_FATAL
1729# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1730#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001731
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001732static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001733{
1734 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1735 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1736 int carry, in;
1737
1738 // Because this function doesn't need to use scale (per the bc spec),
1739 // I am hijacking it to say whether it's doing an add or a subtract.
1740
1741 if (a->len == 0) {
1742 bc_num_copy(c, b);
1743 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001744 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001745 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001746 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001747 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001748 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001749 }
1750
1751 c->neg = a->neg;
1752 c->rdx = BC_MAX(a->rdx, b->rdx);
1753 min_rdx = BC_MIN(a->rdx, b->rdx);
1754 c->len = 0;
1755
1756 if (a->rdx > b->rdx) {
1757 diff = a->rdx - b->rdx;
1758 ptr = a->num;
1759 ptr_a = a->num + diff;
1760 ptr_b = b->num;
1761 }
1762 else {
1763 diff = b->rdx - a->rdx;
1764 ptr = b->num;
1765 ptr_a = a->num;
1766 ptr_b = b->num + diff;
1767 }
1768
1769 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1770
1771 ptr_c += diff;
1772 a_int = BC_NUM_INT(a);
1773 b_int = BC_NUM_INT(b);
1774
1775 if (a_int > b_int) {
1776 min_int = b_int;
1777 max = a_int;
1778 ptr = ptr_a;
1779 }
1780 else {
1781 min_int = a_int;
1782 max = b_int;
1783 ptr = ptr_b;
1784 }
1785
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001786 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001787 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1788 carry = in / 10;
1789 ptr_c[i] = (BcDig)(in % 10);
1790 }
1791
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001792 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001793 in = ((int) ptr[i]) + carry;
1794 carry = in / 10;
1795 ptr_c[i] = (BcDig)(in % 10);
1796 }
1797
1798 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1799
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001800 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001801}
1802
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001803static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001804{
Gavin Howard01055ba2018-11-03 11:00:21 -06001805 ssize_t cmp;
1806 BcNum *minuend, *subtrahend;
1807 size_t start;
1808 bool aneg, bneg, neg;
1809
1810 // Because this function doesn't need to use scale (per the bc spec),
1811 // I am hijacking it to say whether it's doing an add or a subtract.
1812
1813 if (a->len == 0) {
1814 bc_num_copy(c, b);
1815 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001816 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001817 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001818 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001819 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001820 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001821 }
1822
1823 aneg = a->neg;
1824 bneg = b->neg;
1825 a->neg = b->neg = false;
1826
1827 cmp = bc_num_cmp(a, b);
1828
1829 a->neg = aneg;
1830 b->neg = bneg;
1831
1832 if (cmp == 0) {
1833 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001834 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001835 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001836 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 neg = a->neg;
1838 minuend = a;
1839 subtrahend = b;
1840 }
1841 else {
1842 neg = b->neg;
1843 if (sub) neg = !neg;
1844 minuend = b;
1845 subtrahend = a;
1846 }
1847
1848 bc_num_copy(c, minuend);
1849 c->neg = neg;
1850
1851 if (c->rdx < subtrahend->rdx) {
1852 bc_num_extend(c, subtrahend->rdx - c->rdx);
1853 start = 0;
1854 }
1855 else
1856 start = c->rdx - subtrahend->rdx;
1857
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001858 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001859
1860 bc_num_clean(c);
1861
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001862 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001863}
1864
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001865static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001866 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001867#if ERRORS_ARE_FATAL
1868# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1869#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001870{
1871 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001872 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001874 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001875
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 if (a->len == 0 || b->len == 0) {
1877 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001878 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001879 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001880 aone = BC_NUM_ONE(a);
1881 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001882 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001883 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001884 }
1885
1886 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1887 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1888 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001889 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001890 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001891
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 bc_num_expand(c, a->len + b->len + 1);
1893
1894 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001895 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001896
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001897 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001898
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001899 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001900 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001901 unsigned in = c->num[i + j];
1902 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1903 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 carry = in / 10;
1905 c->num[i + j] = (BcDig)(in % 10);
1906 }
1907
1908 c->num[i + j] += (BcDig) carry;
1909 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001910
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001911#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001912 // a=2^1000000
1913 // a*a <- without check below, this will not be interruptible
1914 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001915#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 }
1917
1918 c->len = len;
1919
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001920 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001921 }
1922
1923 bc_num_init(&l1, max);
1924 bc_num_init(&h1, max);
1925 bc_num_init(&l2, max);
1926 bc_num_init(&h2, max);
1927 bc_num_init(&m1, max);
1928 bc_num_init(&m2, max);
1929 bc_num_init(&z0, max);
1930 bc_num_init(&z1, max);
1931 bc_num_init(&z2, max);
1932 bc_num_init(&temp, max + max);
1933
1934 bc_num_split(a, max2, &l1, &h1);
1935 bc_num_split(b, max2, &l2, &h2);
1936
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001937 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001938 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001939 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001940 if (s) goto err;
1941
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001942 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001943 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001944 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001945 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001946 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001947 if (s) goto err;
1948
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001949 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001951 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001952 if (s) goto err;
1953
Denys Vlasenko29301232018-12-11 15:29:32 +01001954 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001955 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001956 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001957 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001958 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001960 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001961
1962err:
1963 bc_num_free(&temp);
1964 bc_num_free(&z2);
1965 bc_num_free(&z1);
1966 bc_num_free(&z0);
1967 bc_num_free(&m2);
1968 bc_num_free(&m1);
1969 bc_num_free(&h2);
1970 bc_num_free(&l2);
1971 bc_num_free(&h1);
1972 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001973 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001974}
1975
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001976static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001977{
1978 BcStatus s;
1979 BcNum cpa, cpb;
1980 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1981
1982 scale = BC_MAX(scale, a->rdx);
1983 scale = BC_MAX(scale, b->rdx);
1984 scale = BC_MIN(a->rdx + b->rdx, scale);
1985 maxrdx = BC_MAX(maxrdx, scale);
1986
1987 bc_num_init(&cpa, a->len);
1988 bc_num_init(&cpb, b->len);
1989
1990 bc_num_copy(&cpa, a);
1991 bc_num_copy(&cpb, b);
1992 cpa.neg = cpb.neg = false;
1993
Denys Vlasenko29301232018-12-11 15:29:32 +01001994 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001995 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001996 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001997 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001998 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001999 if (s) goto err;
2000
2001 maxrdx += scale;
2002 bc_num_expand(c, c->len + maxrdx);
2003
2004 if (c->len < maxrdx) {
2005 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2006 c->len += maxrdx;
2007 }
2008
2009 c->rdx = maxrdx;
2010 bc_num_retireMul(c, scale, a->neg, b->neg);
2011
2012err:
2013 bc_num_free(&cpb);
2014 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002015 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002016}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002017#if ERRORS_ARE_FATAL
2018# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2019#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002020
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002021static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002022{
2023 BcStatus s = BC_STATUS_SUCCESS;
2024 BcDig *n, *p, q;
2025 size_t len, end, i;
2026 BcNum cp;
2027 bool zero = true;
2028
2029 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002030 RETURN_STATUS(bc_error("divide by zero"));
2031 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002032 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002033 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002034 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002035 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002036 bc_num_copy(c, a);
2037 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002038 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002039 }
2040
2041 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2042 bc_num_copy(&cp, a);
2043 len = b->len;
2044
2045 if (len > cp.len) {
2046 bc_num_expand(&cp, len + 2);
2047 bc_num_extend(&cp, len - cp.len);
2048 }
2049
2050 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2051 cp.rdx -= b->rdx;
2052 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2053
2054 if (b->rdx == b->len) {
2055 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2056 len -= i - 1;
2057 }
2058
2059 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2060
2061 // We want an extra zero in front to make things simpler.
2062 cp.num[cp.len++] = 0;
2063 end = cp.len - len;
2064
2065 bc_num_expand(c, cp.len);
2066
2067 bc_num_zero(c);
2068 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2069 c->rdx = cp.rdx;
2070 c->len = cp.len;
2071 p = b->num;
2072
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002073 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002074 n = cp.num + i;
2075 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002076 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002077 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002078#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002079 // a=2^100000
2080 // scale=40000
2081 // 1/a <- without check below, this will not be interruptible
2082 if (G_interrupt) {
2083 s = BC_STATUS_FAILURE;
2084 break;
2085 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002086#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002087 }
2088
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002089 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002090 bc_num_free(&cp);
2091
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002092 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002093}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002094#if ERRORS_ARE_FATAL
2095# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2096#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002097
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002098static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002099 BcNum *restrict d, size_t scale, size_t ts)
2100{
2101 BcStatus s;
2102 BcNum temp;
2103 bool neg;
2104
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002105 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002106 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002107
2108 if (a->len == 0) {
2109 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002110 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002111 }
2112
2113 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002114 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002115 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002116
2117 if (scale != 0) scale = ts;
2118
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002119 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002121 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 if (s) goto err;
2123
2124 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2125
2126 neg = d->neg;
2127 bc_num_retireMul(d, ts, a->neg, b->neg);
2128 d->neg = neg;
2129
2130err:
2131 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002132 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002133}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002134#if ERRORS_ARE_FATAL
2135# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2136#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002137
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002138static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002139{
2140 BcStatus s;
2141 BcNum c1;
2142 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2143
2144 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002145 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 bc_num_free(&c1);
2147
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002148 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002149}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002150#if ERRORS_ARE_FATAL
2151# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2152#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002153
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002154static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002155{
2156 BcStatus s = BC_STATUS_SUCCESS;
2157 BcNum copy;
2158 unsigned long pow;
2159 size_t i, powrdx, resrdx;
2160 bool neg, zero;
2161
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002162 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002163
2164 if (b->len == 0) {
2165 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002166 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002167 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002168 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002169 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002170 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002171 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002172 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002173 if (!b->neg)
2174 bc_num_copy(c, a);
2175 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002176 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002177 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002178 }
2179
2180 neg = b->neg;
2181 b->neg = false;
2182
Denys Vlasenko29301232018-12-11 15:29:32 +01002183 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002184 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002185
2186 bc_num_init(&copy, a->len);
2187 bc_num_copy(&copy, a);
2188
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002189 if (!neg) {
2190 if (a->rdx > scale)
2191 scale = a->rdx;
2192 if (a->rdx * pow < scale)
2193 scale = a->rdx * pow;
2194 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002195
2196 b->neg = neg;
2197
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002198 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002199 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002200 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002201 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002202 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002203 //if (G_interrupt) {
2204 // s = BC_STATUS_FAILURE;
2205 // goto err;
2206 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002207 }
2208
Gavin Howard01055ba2018-11-03 11:00:21 -06002209 bc_num_copy(c, &copy);
2210
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002211 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002212
2213 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002214 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 if (s) goto err;
2216
2217 if (pow & 1) {
2218 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002219 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002220 if (s) goto err;
2221 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002222 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002223 //if (G_interrupt) {
2224 // s = BC_STATUS_FAILURE;
2225 // goto err;
2226 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002227 }
2228
2229 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002230 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002231 if (s) goto err;
2232 }
2233
Gavin Howard01055ba2018-11-03 11:00:21 -06002234 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2235
2236 // We can't use bc_num_clean() here.
2237 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2238 if (zero) bc_num_setToZero(c, scale);
2239
2240err:
2241 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002242 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002243}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002244#if ERRORS_ARE_FATAL
2245# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2246#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002247
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002248static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002249 BcNumBinaryOp op, size_t req)
2250{
2251 BcStatus s;
2252 BcNum num2, *ptr_a, *ptr_b;
2253 bool init = false;
2254
2255 if (c == a) {
2256 ptr_a = &num2;
2257 memcpy(ptr_a, c, sizeof(BcNum));
2258 init = true;
2259 }
2260 else
2261 ptr_a = a;
2262
2263 if (c == b) {
2264 ptr_b = &num2;
2265 if (c != a) {
2266 memcpy(ptr_b, c, sizeof(BcNum));
2267 init = true;
2268 }
2269 }
2270 else
2271 ptr_b = b;
2272
2273 if (init)
2274 bc_num_init(c, req);
2275 else
2276 bc_num_expand(c, req);
2277
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002278 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01002279 ERROR_RETURN(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002280
2281 if (init) bc_num_free(&num2);
2282
Denys Vlasenko29301232018-12-11 15:29:32 +01002283 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002284}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002285#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002286# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002287#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002288
Denys Vlasenko9311e012018-12-10 11:54:18 +01002289static bool bc_num_strValid(const char *val, size_t base)
2290{
2291 BcDig b;
2292 bool radix;
2293
2294 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2295 radix = false;
2296 for (;;) {
2297 BcDig c = *val++;
2298 if (c == '\0')
2299 break;
2300 if (c == '.') {
2301 if (radix) return false;
2302 radix = true;
2303 continue;
2304 }
2305 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2306 return false;
2307 }
2308 return true;
2309}
2310
2311// Note: n is already "bc_num_zero()"ed,
2312// leading zeroes in "val" are removed
2313static void bc_num_parseDecimal(BcNum *n, const char *val)
2314{
2315 size_t len, i;
2316 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002317
2318 len = strlen(val);
2319 if (len == 0)
2320 return;
2321
Denys Vlasenko9311e012018-12-10 11:54:18 +01002322 bc_num_expand(n, len);
2323
2324 ptr = strchr(val, '.');
2325
2326 n->rdx = 0;
2327 if (ptr != NULL)
2328 n->rdx = (size_t)((val + len) - (ptr + 1));
2329
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002330 for (i = 0; val[i]; ++i) {
2331 if (val[i] != '0' && val[i] != '.') {
2332 // Not entirely zero value - convert it, and exit
2333 i = len - 1;
2334 for (;;) {
2335 n->num[n->len] = val[i] - '0';
2336 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002337 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002338 if ((ssize_t)--i == (ssize_t)-1) break;
2339 if (val[i] == '.') goto skip_dot;
2340 }
2341 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002342 }
2343 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002344 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002345}
2346
2347// Note: n is already "bc_num_zero()"ed,
2348// leading zeroes in "val" are removed
2349static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2350{
2351 BcStatus s;
2352 BcNum temp, mult, result;
2353 BcDig c = '\0';
2354 unsigned long v;
2355 size_t i, digits;
2356
2357 for (i = 0; ; ++i) {
2358 if (val[i] == '\0')
2359 return;
2360 if (val[i] != '.' && val[i] != '0')
2361 break;
2362 }
2363
2364 bc_num_init_DEF_SIZE(&temp);
2365 bc_num_init_DEF_SIZE(&mult);
2366
2367 for (;;) {
2368 c = *val++;
2369 if (c == '\0') goto int_err;
2370 if (c == '.') break;
2371
2372 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2373
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002374 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002375 if (s) goto int_err;
2376 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002377 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002378 if (s) goto int_err;
2379 }
2380
2381 bc_num_init(&result, base->len);
2382 //bc_num_zero(&result); - already is
2383 bc_num_one(&mult);
2384
2385 digits = 0;
2386 for (;;) {
2387 c = *val++;
2388 if (c == '\0') break;
2389 digits++;
2390
2391 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2392
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002393 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002394 if (s) goto err;
2395 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002396 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002397 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002398 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002399 if (s) goto err;
2400 }
2401
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002402 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002403 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002404 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002405 if (s) goto err;
2406
2407 if (n->len != 0) {
2408 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2409 } else
2410 bc_num_zero(n);
2411
2412err:
2413 bc_num_free(&result);
2414int_err:
2415 bc_num_free(&mult);
2416 bc_num_free(&temp);
2417}
2418
Denys Vlasenko29301232018-12-11 15:29:32 +01002419static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 size_t base_t)
2421{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002422 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002423 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002424
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002425 bc_num_zero(n);
2426 while (*val == '0') val++;
2427
Gavin Howard01055ba2018-11-03 11:00:21 -06002428 if (base_t == 10)
2429 bc_num_parseDecimal(n, val);
2430 else
2431 bc_num_parseBase(n, val, base);
2432
Denys Vlasenko29301232018-12-11 15:29:32 +01002433 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002434}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002435#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002436# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002437#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002438
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002439static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002440{
2441 BcStatus s;
2442 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2443 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2444 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2445
2446 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2447 bc_num_expand(b, req);
2448
2449 if (a->len == 0) {
2450 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002451 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002452 }
2453 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002454 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002455 else if (BC_NUM_ONE(a)) {
2456 bc_num_one(b);
2457 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002458 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002459 }
2460
2461 scale = BC_MAX(scale, a->rdx) + 1;
2462 len = a->len + scale;
2463
2464 bc_num_init(&num1, len);
2465 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002466 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002467
2468 bc_num_one(&half);
2469 half.num[0] = 5;
2470 half.rdx = 1;
2471
2472 bc_num_init(&f, len);
2473 bc_num_init(&fprime, len);
2474
2475 x0 = &num1;
2476 x1 = &num2;
2477
2478 bc_num_one(x0);
2479 pow = BC_NUM_INT(a);
2480
2481 if (pow) {
2482
2483 if (pow & 1)
2484 x0->num[0] = 2;
2485 else
2486 x0->num[0] = 6;
2487
2488 pow -= 2 - (pow & 1);
2489
2490 bc_num_extend(x0, pow);
2491
2492 // Make sure to move the radix back.
2493 x0->rdx -= pow;
2494 }
2495
2496 x0->rdx = digs = digs1 = 0;
2497 resrdx = scale + 2;
2498 len = BC_NUM_INT(x0) + resrdx - 1;
2499
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002500 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002501
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002502 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002503 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002504 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002505 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002506 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002507 if (s) goto err;
2508
2509 cmp = bc_num_cmp(x1, x0);
2510 digs = x1->len - (unsigned long long) llabs(cmp);
2511
2512 if (cmp == cmp2 && digs == digs1)
2513 times += 1;
2514 else
2515 times = 0;
2516
2517 resrdx += times > 4;
2518
2519 cmp2 = cmp1;
2520 cmp1 = cmp;
2521 digs1 = digs;
2522
2523 temp = x0;
2524 x0 = x1;
2525 x1 = temp;
2526 }
2527
Gavin Howard01055ba2018-11-03 11:00:21 -06002528 bc_num_copy(b, x0);
2529 scale -= 1;
2530 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2531
2532err:
2533 bc_num_free(&fprime);
2534 bc_num_free(&f);
2535 bc_num_free(&half);
2536 bc_num_free(&num2);
2537 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002538 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002539}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002540#if ERRORS_ARE_FATAL
2541# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2542#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002543
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002544static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002545 size_t scale)
2546{
2547 BcStatus s;
2548 BcNum num2, *ptr_a;
2549 bool init = false;
2550 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2551
2552 if (c == a) {
2553 memcpy(&num2, c, sizeof(BcNum));
2554 ptr_a = &num2;
2555 bc_num_init(c, len);
2556 init = true;
2557 }
2558 else {
2559 ptr_a = a;
2560 bc_num_expand(c, len);
2561 }
2562
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002563 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002564
2565 if (init) bc_num_free(&num2);
2566
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002567 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002568}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002569#if ERRORS_ARE_FATAL
2570# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2571#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002572
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002573#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002574static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002575{
2576 BcStatus s;
2577 BcNum base, exp, two, temp;
2578
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002579 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002580 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002581 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002582 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002583 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002584 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002585
2586 bc_num_expand(d, c->len);
2587 bc_num_init(&base, c->len);
2588 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002589 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002590 bc_num_init(&temp, b->len);
2591
2592 bc_num_one(&two);
2593 two.num[0] = 2;
2594 bc_num_one(d);
2595
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002596 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002597 if (s) goto err;
2598 bc_num_copy(&exp, b);
2599
2600 while (exp.len != 0) {
2601
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002602 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002603 if (s) goto err;
2604
2605 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002606 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002607 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002608 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002609 if (s) goto err;
2610 }
2611
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002612 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002613 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002614 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002615 if (s) goto err;
2616 }
2617
2618err:
2619 bc_num_free(&temp);
2620 bc_num_free(&two);
2621 bc_num_free(&exp);
2622 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002623 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002624}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002625#if ERRORS_ARE_FATAL
2626# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2627#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002628#endif // ENABLE_DC
2629
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002630#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002631static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002632{
2633 BcId a;
2634 size_t i;
2635
2636 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002637 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002638 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002639 }
2640
2641 a.idx = var;
2642 a.name = name;
2643
2644 bc_vec_push(&f->autos, &a);
2645
Denys Vlasenko29301232018-12-11 15:29:32 +01002646 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002647}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002648#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002649# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002650#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002651#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002652
2653static void bc_func_init(BcFunc *f)
2654{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002655 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002656 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2657 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2658 f->nparams = 0;
2659}
2660
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002661static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002662{
2663 BcFunc *f = (BcFunc *) func;
2664 bc_vec_free(&f->code);
2665 bc_vec_free(&f->autos);
2666 bc_vec_free(&f->labels);
2667}
2668
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002669static void bc_array_expand(BcVec *a, size_t len);
2670
Gavin Howard01055ba2018-11-03 11:00:21 -06002671static void bc_array_init(BcVec *a, bool nums)
2672{
2673 if (nums)
2674 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2675 else
2676 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2677 bc_array_expand(a, 1);
2678}
2679
Gavin Howard01055ba2018-11-03 11:00:21 -06002680static void bc_array_expand(BcVec *a, size_t len)
2681{
2682 BcResultData data;
2683
2684 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2685 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002686 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002687 bc_vec_push(a, &data.n);
2688 }
2689 }
2690 else {
2691 while (len > a->len) {
2692 bc_array_init(&data.v, true);
2693 bc_vec_push(a, &data.v);
2694 }
2695 }
2696}
2697
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002698static void bc_array_copy(BcVec *d, const BcVec *s)
2699{
2700 size_t i;
2701
2702 bc_vec_pop_all(d);
2703 bc_vec_expand(d, s->cap);
2704 d->len = s->len;
2705
2706 for (i = 0; i < s->len; ++i) {
2707 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2708 bc_num_init(dnum, snum->len);
2709 bc_num_copy(dnum, snum);
2710 }
2711}
2712
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002713static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002714{
2715 free(*((char **) string));
2716}
2717
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002718#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002719static void bc_result_copy(BcResult *d, BcResult *src)
2720{
2721 d->t = src->t;
2722
2723 switch (d->t) {
2724
2725 case BC_RESULT_TEMP:
2726 case BC_RESULT_IBASE:
2727 case BC_RESULT_SCALE:
2728 case BC_RESULT_OBASE:
2729 {
2730 bc_num_init(&d->d.n, src->d.n.len);
2731 bc_num_copy(&d->d.n, &src->d.n);
2732 break;
2733 }
2734
2735 case BC_RESULT_VAR:
2736 case BC_RESULT_ARRAY:
2737 case BC_RESULT_ARRAY_ELEM:
2738 {
2739 d->d.id.name = xstrdup(src->d.id.name);
2740 break;
2741 }
2742
2743 case BC_RESULT_CONSTANT:
2744 case BC_RESULT_LAST:
2745 case BC_RESULT_ONE:
2746 case BC_RESULT_STR:
2747 {
2748 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2749 break;
2750 }
2751 }
2752}
2753#endif // ENABLE_DC
2754
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002755static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002756{
2757 BcResult *r = (BcResult *) result;
2758
2759 switch (r->t) {
2760
2761 case BC_RESULT_TEMP:
2762 case BC_RESULT_IBASE:
2763 case BC_RESULT_SCALE:
2764 case BC_RESULT_OBASE:
2765 {
2766 bc_num_free(&r->d.n);
2767 break;
2768 }
2769
2770 case BC_RESULT_VAR:
2771 case BC_RESULT_ARRAY:
2772 case BC_RESULT_ARRAY_ELEM:
2773 {
2774 free(r->d.id.name);
2775 break;
2776 }
2777
2778 default:
2779 {
2780 // Do nothing.
2781 break;
2782 }
2783 }
2784}
2785
2786static void bc_lex_lineComment(BcLex *l)
2787{
2788 l->t.t = BC_LEX_WHITESPACE;
2789 while (l->i < l->len && l->buf[l->i++] != '\n');
2790 --l->i;
2791}
2792
2793static void bc_lex_whitespace(BcLex *l)
2794{
2795 char c;
2796 l->t.t = BC_LEX_WHITESPACE;
2797 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2798}
2799
Denys Vlasenko29301232018-12-11 15:29:32 +01002800static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002801{
2802 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002803 size_t len, bslashes, i, ccnt;
2804 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002805
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002806 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002807 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002808 bslashes = 0;
2809 ccnt = i = 0;
2810 for (;;) {
2811 char c = buf[i];
2812 if (c == '\0')
2813 break;
2814 if (c == '\\' && buf[i + 1] == '\n') {
2815 i += 2;
2816 bslashes++;
2817 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002818 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002819 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2820 if (c != '.') break;
2821 // if '.' was already seen, stop on second one:
2822 if (pt) break;
2823 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002824 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002825 // buf[i] is one of "0-9A-F."
2826 i++;
2827 if (c != '.')
2828 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002829 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002830 //i is buf[i] index of the first not-yet-parsed char
2831 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002832
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002833 //ccnt is the number of chars in the number string, excluding possible
2834 //trailing "." and possible following trailing "\<newline>"(s).
2835 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2836
Denys Vlasenko64074a12018-12-07 15:50:14 +01002837 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2838 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2839 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002840 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002841 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002842
Denys Vlasenko7d628012018-12-04 21:46:47 +01002843 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002844 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002845 bc_vec_push(&l->t.v, &start);
2846
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002847 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002848 // If we have hit a backslash, skip it. We don't have
2849 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002850 if (*buf == '\\') {
2851 buf += 2;
2852 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002853 continue;
2854 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002855 bc_vec_push(&l->t.v, buf);
2856 buf++;
2857 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002858 }
2859
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002860 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002861
Denys Vlasenko29301232018-12-11 15:29:32 +01002862 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002863}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002864#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002865# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002866#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002867
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002868static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002869{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002870 size_t i;
2871 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002872
2873 l->t.t = BC_LEX_NAME;
2874
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002875 i = 0;
2876 buf = l->buf + l->i - 1;
2877 for (;;) {
2878 char c = buf[i];
2879 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2880 i++;
2881 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002882
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002883#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002884 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2885 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2886 if (i > BC_MAX_STRING)
2887 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2888 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002889#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002890 bc_vec_string(&l->t.v, i, buf);
2891
2892 // Increment the index. We minus 1 because it has already been incremented.
2893 l->i += i - 1;
2894
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002895 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002896}
2897
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002898static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002899{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002900 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002901}
2902
2903static void bc_lex_free(BcLex *l)
2904{
2905 bc_vec_free(&l->t.v);
2906}
2907
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002908static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002909{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002910 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002911 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002912}
2913
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002914static BC_STATUS zbc_lex_token(BcLex *l);
2915static BC_STATUS zdc_lex_token(BcLex *l);
2916
2917static BC_STATUS zcommon_lex_token(BcLex *l)
2918{
2919 if (IS_BC) {
2920 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2921 }
2922 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2923}
2924
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002925static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002926{
2927 BcStatus s;
2928
2929 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002930 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002931
2932 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002933 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002934 l->t.t = BC_LEX_EOF;
2935
2936 l->newline = (l->i == l->len);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002937 if (l->newline) RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002938
2939 // Loop until failure or we don't have whitespace. This
2940 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002941 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002942 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002943//TODO: replace pointer with if(IS_BC)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002944 ERROR_RETURN(s =) zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002945 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2946
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002947 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002948}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002949#if ERRORS_ARE_FATAL
2950# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS)
2951#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002952
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002953static BC_STATUS zbc_lex_text(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002954{
2955 l->buf = text;
2956 l->i = 0;
2957 l->len = strlen(text);
2958 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002959 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002960}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002961#if ERRORS_ARE_FATAL
2962# define zbc_lex_text(...) (zbc_lex_text(__VA_ARGS__), BC_STATUS_SUCCESS)
2963#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002964
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002965#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002966static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002967{
2968 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002969 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002970 const char *buf = l->buf + l->i - 1;
2971
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002972 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2973 const char *keyword8 = bc_lex_kws[i].name8;
2974 unsigned j = 0;
2975 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2976 j++;
2977 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002978 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002979 if (keyword8[j] != '\0')
2980 continue;
2981 match:
2982 // buf starts with keyword bc_lex_kws[i]
2983 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002984 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002985 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002986 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002987 }
2988
2989 // We minus 1 because the index has already been incremented.
2990 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002991 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002992 }
2993
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002994 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002995
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002996 if (l->t.v.len > 2) {
2997 // Prevent this:
2998 // >>> qwe=1
2999 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3000 // '
3001 unsigned len = strchrnul(buf, '\n') - buf;
3002 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3003 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003004
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003005 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003006}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003007#if ERRORS_ARE_FATAL
3008# define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__), BC_STATUS_SUCCESS)
3009#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003010
Denys Vlasenko26819db2018-12-12 16:08:46 +01003011static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003012{
3013 size_t len, nls = 0, i = l->i;
3014 char c;
3015
3016 l->t.t = BC_LEX_STR;
3017
Denys Vlasenko26819db2018-12-12 16:08:46 +01003018 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i])
3019 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003020
3021 if (c == '\0') {
3022 l->i = i;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003023 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003024 }
3025
3026 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003027 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3028 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3029 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003030 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003031 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003032 bc_vec_string(&l->t.v, len, l->buf + l->i);
3033
3034 l->i = i + 1;
3035 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003036 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
Denys Vlasenko26819db2018-12-12 16:08:46 +01003038 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003039}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003040#if ERRORS_ARE_FATAL
3041# define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3042#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003043
3044static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3045{
3046 if (l->buf[l->i] == '=') {
3047 ++l->i;
3048 l->t.t = with;
3049 }
3050 else
3051 l->t.t = without;
3052}
3053
Denys Vlasenko29301232018-12-11 15:29:32 +01003054static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003055{
3056 size_t i, nls = 0;
3057 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003058
3059 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003060 i = ++l->i;
3061 for (;;) {
3062 char c = buf[i];
3063 check_star:
3064 if (c == '*') {
3065 c = buf[++i];
3066 if (c == '/')
3067 break;
3068 goto check_star;
3069 }
3070 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003071 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003072 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003073 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003074 nls += (c == '\n');
3075 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003076 }
3077
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003078 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003079 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003080 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003081
Denys Vlasenko29301232018-12-11 15:29:32 +01003082 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003083}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003084#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003085# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003086#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003087
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003088static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003089{
3090 BcStatus s = BC_STATUS_SUCCESS;
3091 char c = l->buf[l->i++], c2;
3092
3093 // This is the workhorse of the lexer.
3094 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003095 case '\0':
3096 case '\n':
Gavin Howard01055ba2018-11-03 11:00:21 -06003097 l->newline = true;
3098 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3099 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 case '\t':
3101 case '\v':
3102 case '\f':
3103 case '\r':
3104 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003105 bc_lex_whitespace(l);
3106 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003107 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003109 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003110 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003111 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003112 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003113 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003114 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003115 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003118 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003119 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003121 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003122 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003123 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003126 c2 = l->buf[l->i];
3127 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003128 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003129 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003130 ++l->i;
3131 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003132 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003134 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003135 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 case '(':
3138 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3140 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3143 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 c2 = l->buf[l->i];
3146 if (c2 == '+') {
3147 ++l->i;
3148 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003149 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3151 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 l->t.t = BC_LEX_COMMA;
3154 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 c2 = l->buf[l->i];
3157 if (c2 == '-') {
3158 ++l->i;
3159 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003160 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003161 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3162 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003165 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 else {
3167 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003168 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 }
3170 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003171 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 c2 = l->buf[l->i];
3173 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003174 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 else
3176 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3177 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003178 case '0':
3179 case '1':
3180 case '2':
3181 case '3':
3182 case '4':
3183 case '5':
3184 case '6':
3185 case '7':
3186 case '8':
3187 case '9':
3188 case 'A':
3189 case 'B':
3190 case 'C':
3191 case 'D':
3192 case 'E':
3193 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003194 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 l->t.t = BC_LEX_SCOLON;
3198 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3201 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3204 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3207 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 case '[':
3209 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3211 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 if (l->buf[l->i] == '\n') {
3214 l->t.t = BC_LEX_WHITESPACE;
3215 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003216 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003217 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3221 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 case 'a':
3223 case 'b':
3224 case 'c':
3225 case 'd':
3226 case 'e':
3227 case 'f':
3228 case 'g':
3229 case 'h':
3230 case 'i':
3231 case 'j':
3232 case 'k':
3233 case 'l':
3234 case 'm':
3235 case 'n':
3236 case 'o':
3237 case 'p':
3238 case 'q':
3239 case 'r':
3240 case 's':
3241 case 't':
3242 case 'u':
3243 case 'v':
3244 case 'w':
3245 case 'x':
3246 case 'y':
3247 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003248 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 case '{':
3251 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3253 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003256 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003257 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003258 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 ++l->i;
3260 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003261 } else {
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 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003268 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003269 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 }
3271
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003272 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003273}
3274#endif // ENABLE_BC
3275
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003276#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003277static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003278{
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 if (isspace(l->buf[l->i - 1])) {
3280 bc_lex_whitespace(l);
3281 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003282 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003283 RETURN_STATUS(bc_error("extended register"));
3284 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 }
3286 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003287 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003288 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003289 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 l->t.t = BC_LEX_NAME;
3291 }
3292
Denys Vlasenko29301232018-12-11 15:29:32 +01003293 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003294}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003295#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003296# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003297#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003298
Denys Vlasenko29301232018-12-11 15:29:32 +01003299static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003300{
3301 size_t depth = 1, nls = 0, i = l->i;
3302 char c;
3303
3304 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003305 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003306
3307 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3308
3309 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3310 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3311 nls += (c == '\n');
3312
3313 if (depth) bc_vec_push(&l->t.v, &c);
3314 }
3315
3316 if (c == '\0') {
3317 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003318 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003319 }
3320
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003321 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003322 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3323 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3324 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003325 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003326 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003327
3328 l->i = i;
3329 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003330 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003331
Denys Vlasenko29301232018-12-11 15:29:32 +01003332 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003333}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003334#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003335# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003336#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003337
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003338static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003339{
3340 BcStatus s = BC_STATUS_SUCCESS;
3341 char c = l->buf[l->i++], c2;
3342 size_t i;
3343
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003344 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3345 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003346 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003347 }
3348
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003349 if (c >= '%' && c <= '~'
3350 && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID
3351 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003352 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003353 }
3354
3355 // This is the workhorse of the lexer.
3356 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003357 case '\0':
Gavin Howard01055ba2018-11-03 11:00:21 -06003358 l->t.t = BC_LEX_EOF;
3359 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003360 case '\n':
3361 case '\t':
3362 case '\v':
3363 case '\f':
3364 case '\r':
3365 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003366 l->newline = (c == '\n');
3367 bc_lex_whitespace(l);
3368 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003369 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003370 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003371 if (c2 == '=')
3372 l->t.t = BC_LEX_OP_REL_NE;
3373 else if (c2 == '<')
3374 l->t.t = BC_LEX_OP_REL_LE;
3375 else if (c2 == '>')
3376 l->t.t = BC_LEX_OP_REL_GE;
3377 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003378 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003379 ++l->i;
3380 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003381 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003382 bc_lex_lineComment(l);
3383 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003384 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003385 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003386 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003387 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003388 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003389 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003390 case '0':
3391 case '1':
3392 case '2':
3393 case '3':
3394 case '4':
3395 case '5':
3396 case '6':
3397 case '7':
3398 case '8':
3399 case '9':
3400 case 'A':
3401 case 'B':
3402 case 'C':
3403 case 'D':
3404 case 'E':
3405 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003406 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003407 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003408 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003409 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003410 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003411 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003412 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003413 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003414 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003415 }
3416
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003417 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003418}
3419#endif // ENABLE_DC
3420
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003421static void bc_program_addFunc(char *name, size_t *idx);
3422
Gavin Howard01055ba2018-11-03 11:00:21 -06003423static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3424{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003425 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003426 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003427}
3428
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003429#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3430
Gavin Howard01055ba2018-11-03 11:00:21 -06003431static void bc_parse_pushName(BcParse *p, char *name)
3432{
3433 size_t i = 0, len = strlen(name);
3434
3435 for (; i < len; ++i) bc_parse_push(p, name[i]);
3436 bc_parse_push(p, BC_PARSE_STREND);
3437
3438 free(name);
3439}
3440
3441static void bc_parse_pushIndex(BcParse *p, size_t idx)
3442{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003443 size_t mask;
3444 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003445
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003446 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3447 amt = sizeof(idx);
3448 do {
3449 if (idx & mask) break;
3450 mask >>= 8;
3451 amt--;
3452 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003453
3454 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003455
3456 while (idx != 0) {
3457 bc_parse_push(p, (unsigned char)idx);
3458 idx >>= 8;
3459 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003460}
3461
3462static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3463{
3464 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003465 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003466
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003467 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003468
3469 bc_parse_push(p, BC_INST_NUM);
3470 bc_parse_pushIndex(p, idx);
3471
3472 ++(*nexs);
3473 (*prev) = BC_INST_NUM;
3474}
3475
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003476static BC_STATUS zbc_parse_parse(BcParse *p);
3477static BC_STATUS zdc_parse_parse(BcParse *p);
3478
3479static BC_STATUS zcommon_parse(BcParse *p)
3480{
3481 if (IS_BC) {
3482 IF_BC(RETURN_STATUS(zbc_parse_parse(p));)
3483 }
3484 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3485}
3486
Denys Vlasenko26819db2018-12-12 16:08:46 +01003487static BC_STATUS zbc_parse_text(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003488{
3489 BcStatus s;
3490
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003491 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003492
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003493 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003494 p->l.t.t = BC_LEX_INVALID;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003495 s = BC_STATUS_SUCCESS;
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003496 ERROR_RETURN(s =) zcommon_parse(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003497 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003498 if (!BC_PARSE_CAN_EXEC(p))
Denys Vlasenko26819db2018-12-12 16:08:46 +01003499 RETURN_STATUS(bc_error("file is not executable"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003500 }
3501
Denys Vlasenko26819db2018-12-12 16:08:46 +01003502 RETURN_STATUS(zbc_lex_text(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003503}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003504#if ERRORS_ARE_FATAL
3505# define zbc_parse_text(...) (zbc_parse_text(__VA_ARGS__), BC_STATUS_SUCCESS)
3506#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003507
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003508// Called when parsing or execution detects a failure,
3509// resets execution structures.
3510static void bc_program_reset(void)
3511{
3512 BcFunc *f;
3513 BcInstPtr *ip;
3514
3515 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3516 bc_vec_pop_all(&G.prog.results);
3517
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003518 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003519 ip = bc_vec_top(&G.prog.stack);
3520 ip->idx = f->code.len;
3521}
3522
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003523#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003524 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003525
Denys Vlasenko26819db2018-12-12 16:08:46 +01003526// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003527// resets parsing structures.
3528static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003529{
3530 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003531 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003532 bc_vec_pop_all(&p->func->code);
3533 bc_vec_pop_all(&p->func->autos);
3534 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003535
3536 bc_parse_updateFunc(p, BC_PROG_MAIN);
3537 }
3538
3539 p->l.i = p->l.len;
3540 p->l.t.t = BC_LEX_EOF;
3541 p->auto_part = (p->nbraces = 0);
3542
3543 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003544 bc_vec_pop_all(&p->exits);
3545 bc_vec_pop_all(&p->conds);
3546 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003547
Denys Vlasenkod38af482018-12-04 19:11:02 +01003548 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003549}
3550
3551static void bc_parse_free(BcParse *p)
3552{
3553 bc_vec_free(&p->flags);
3554 bc_vec_free(&p->exits);
3555 bc_vec_free(&p->conds);
3556 bc_vec_free(&p->ops);
3557 bc_lex_free(&p->l);
3558}
3559
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003560static void bc_parse_create(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06003561{
3562 memset(p, 0, sizeof(BcParse));
3563
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003564 bc_lex_init(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003565 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3566 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3567 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003568 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003569 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3570
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003571 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003572 bc_parse_updateFunc(p, func);
3573}
3574
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003575#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003576
3577#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3578#define BC_PARSE_LEAF(p, rparen) \
3579 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3580 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3581
3582// We can calculate the conversion between tokens and exprs by subtracting the
3583// position of the first operator in the lex enum and adding the position of the
3584// first in the expr enum. Note: This only works for binary operators.
3585#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3586
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003587static BC_STATUS zbc_parse_else(BcParse *p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003588static BC_STATUS zbc_parse_stmt(BcParse *p);
3589static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003590static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003591#if ERRORS_ARE_FATAL
3592# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
3593# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
3594# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
3595#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003596
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003597static BC_STATUS zbc_parse_operator(BcParse *p, BcLexType type, size_t start,
Gavin Howard01055ba2018-11-03 11:00:21 -06003598 size_t *nexprs, bool next)
3599{
3600 BcStatus s = BC_STATUS_SUCCESS;
3601 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003602 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3603 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003604
3605 while (p->ops.len > start) {
3606
3607 t = BC_PARSE_TOP_OP(p);
3608 if (t == BC_LEX_LPAREN) break;
3609
Denys Vlasenko65437582018-12-05 19:37:19 +01003610 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003611 if (l >= r && (l != r || !left)) break;
3612
3613 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3614 bc_vec_pop(&p->ops);
3615 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3616 }
3617
3618 bc_vec_push(&p->ops, &type);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003619 if (next) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003620
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003621 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003622}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003623#if ERRORS_ARE_FATAL
3624# define zbc_parse_operator(...) (zbc_parse_operator(__VA_ARGS__), BC_STATUS_SUCCESS)
3625#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003626
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003627static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003628{
3629 BcLexType top;
3630
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003631 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003632 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003633 top = BC_PARSE_TOP_OP(p);
3634
3635 while (top != BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003636 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3637
3638 bc_vec_pop(&p->ops);
3639 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3640
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003641 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003642 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003643 top = BC_PARSE_TOP_OP(p);
3644 }
3645
3646 bc_vec_pop(&p->ops);
3647
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003648 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003649}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003650#if ERRORS_ARE_FATAL
3651# define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__), BC_STATUS_SUCCESS)
3652#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003653
Denys Vlasenko26819db2018-12-12 16:08:46 +01003654static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003655{
3656 BcStatus s;
3657 bool comma = false;
3658 size_t nparams;
3659
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003660 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003661 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003662
3663 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003664 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003665 s = zbc_parse_expr(p, flags, bc_parse_next_param);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003666 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003667
3668 comma = p->l.t.t == BC_LEX_COMMA;
3669 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003670 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003671 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003672 }
3673 }
3674
Denys Vlasenko26819db2018-12-12 16:08:46 +01003675 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003676 bc_parse_push(p, BC_INST_CALL);
3677 bc_parse_pushIndex(p, nparams);
3678
Denys Vlasenko26819db2018-12-12 16:08:46 +01003679 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003680}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003681#if ERRORS_ARE_FATAL
3682# define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__), BC_STATUS_SUCCESS)
3683#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003684
Denys Vlasenko26819db2018-12-12 16:08:46 +01003685static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003686{
3687 BcStatus s;
3688 BcId entry, *entry_ptr;
3689 size_t idx;
3690
3691 entry.name = name;
3692
Denys Vlasenko26819db2018-12-12 16:08:46 +01003693 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003694 if (s) goto err;
3695
3696 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003697 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003698 goto err;
3699 }
3700
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
3703 if (idx == BC_VEC_INVALID_IDX) {
3704 name = xstrdup(entry.name);
3705 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003706 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003707 free(entry.name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003708 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003709 free(name);
3710
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003711 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003712 bc_parse_pushIndex(p, entry_ptr->idx);
3713
Denys Vlasenko26819db2018-12-12 16:08:46 +01003714 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003715
3716err:
3717 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003718 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003719}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003720#if ERRORS_ARE_FATAL
3721# define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__), BC_STATUS_SUCCESS)
3722#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003723
Denys Vlasenko26819db2018-12-12 16:08:46 +01003724static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003725{
3726 BcStatus s;
3727 char *name;
3728
3729 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003730 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003731 if (s) goto err;
3732
3733 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003734 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003735 if (s) goto err;
3736
3737 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003738 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003739 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003740 goto err;
3741 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003742 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003743 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003744 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003745 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003746 s = zbc_parse_expr(p, flags, bc_parse_next_elem);
Gavin Howard01055ba2018-11-03 11:00:21 -06003747 if (s) goto err;
3748 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003749 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003750 if (s) goto err;
3751 bc_parse_push(p, *type);
3752 bc_parse_pushName(p, name);
3753 }
3754 else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003755 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003756 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003757 goto err;
3758 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003759 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003760 s = zbc_parse_call(p, name, flags);
3761 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003762 *type = BC_INST_VAR;
3763 bc_parse_push(p, BC_INST_VAR);
3764 bc_parse_pushName(p, name);
3765 }
3766
Denys Vlasenko26819db2018-12-12 16:08:46 +01003767 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003768
3769err:
3770 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003771 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003772}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003773#if ERRORS_ARE_FATAL
3774# define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__), BC_STATUS_SUCCESS)
3775#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003776
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003777static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003778{
3779 BcStatus s;
3780
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003781 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003782 if (s) RETURN_STATUS(s);
3783 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003784
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003785 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003786 if (s) RETURN_STATUS(s);
3787 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003788
3789 bc_parse_push(p, BC_INST_READ);
3790
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003791 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003792}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003793#if ERRORS_ARE_FATAL
3794# define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__), BC_STATUS_SUCCESS)
3795#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003796
Denys Vlasenko26819db2018-12-12 16:08:46 +01003797static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003798 BcInst *prev)
3799{
3800 BcStatus s;
3801
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003802 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003803 if (s) RETURN_STATUS(s);
3804 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003805
3806 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3807
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003808 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003809 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003810
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003811 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003812 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003813
Denys Vlasenko26819db2018-12-12 16:08:46 +01003814 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003815
3816 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3817 bc_parse_push(p, *prev);
3818
Denys Vlasenko26819db2018-12-12 16:08:46 +01003819 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003820}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003821#if ERRORS_ARE_FATAL
3822# define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
3823#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003824
Denys Vlasenko26819db2018-12-12 16:08:46 +01003825static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003826{
3827 BcStatus s;
3828
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003829 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003830 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003831
3832 if (p->l.t.t != BC_LEX_LPAREN) {
3833 *type = BC_INST_SCALE;
3834 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003835 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003836 }
3837
3838 *type = BC_INST_SCALE_FUNC;
3839 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3840
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003841 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003842 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003843
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003844 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003845 if (s) RETURN_STATUS(s);
3846 if (p->l.t.t != BC_LEX_RPAREN)
3847 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003848 bc_parse_push(p, BC_INST_SCALE_FUNC);
3849
Denys Vlasenko26819db2018-12-12 16:08:46 +01003850 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003851}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003852#if ERRORS_ARE_FATAL
3853# define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__), BC_STATUS_SUCCESS)
3854#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003855
Denys Vlasenko26819db2018-12-12 16:08:46 +01003856static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003857 size_t *nexprs, uint8_t flags)
3858{
3859 BcStatus s;
3860 BcLexType type;
3861 char inst;
3862 BcInst etype = *prev;
3863
3864 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3865 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3866 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3867 {
3868 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3869 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003870 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003871 }
3872 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003873 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3874 *paren_expr = true;
3875
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003876 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003877 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 type = p->l.t.t;
3879
3880 // Because we parse the next part of the expression
3881 // right here, we need to increment this.
3882 *nexprs = *nexprs + 1;
3883
3884 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01003886 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 case BC_LEX_KEY_IBASE:
3889 case BC_LEX_KEY_LAST:
3890 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06003891 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003892 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003893 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003895 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003896 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003898 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003899 else
3900 bc_parse_push(p, BC_INST_SCALE);
3901 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003902 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003903 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003904 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 }
3906
3907 if (!s) bc_parse_push(p, inst);
3908 }
3909
Denys Vlasenko26819db2018-12-12 16:08:46 +01003910 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003911}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003912#if ERRORS_ARE_FATAL
3913# define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
3914#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003915
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003916static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06003917 bool rparen, size_t *nexprs)
3918{
3919 BcStatus s;
3920 BcLexType type;
3921 BcInst etype = *prev;
3922
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003923 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003924 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003925
3926 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3927 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3928 BC_LEX_OP_MINUS :
3929 BC_LEX_NEG;
3930 *prev = BC_PARSE_TOKEN_INST(type);
3931
3932 // We can just push onto the op stack because this is the largest
3933 // precedence operator that gets pushed. Inc/dec does not.
3934 if (type != BC_LEX_OP_MINUS)
3935 bc_vec_push(&p->ops, &type);
3936 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003937 s = zbc_parse_operator(p, type, ops_bgn, nexprs, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06003938
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003939 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003940}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003941#if ERRORS_ARE_FATAL
3942# define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__), BC_STATUS_SUCCESS)
3943#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003944
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003945static BC_STATUS zbc_parse_string(BcParse *p, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06003946{
3947 char *str = xstrdup(p->l.t.v.v);
3948
3949 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003950 bc_parse_pushIndex(p, G.prog.strs.len);
3951 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003952 bc_parse_push(p, inst);
3953
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003954 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003955}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003956#if ERRORS_ARE_FATAL
3957# define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3958#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003959
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003960static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003961{
3962 BcStatus s;
3963 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003964 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003966 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003967 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003968
3969 type = p->l.t.t;
3970
3971 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003972 RETURN_STATUS(bc_error("bad print statement"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003973
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003974 comma = false;
3975 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003976 if (type == BC_LEX_STR) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003977 s = zbc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003978 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003979 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003980 s = zbc_parse_expr(p, 0, bc_parse_next_print);
3981 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003982 bc_parse_push(p, BC_INST_PRINT_POP);
3983 }
3984
Gavin Howard01055ba2018-11-03 11:00:21 -06003985 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003986 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003987 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003988 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003989 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003990 type = p->l.t.t;
3991 }
3992
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003993 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003994
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003995 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003996}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003997#if ERRORS_ARE_FATAL
3998# define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__), BC_STATUS_SUCCESS)
3999#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004000
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004001static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004002{
4003 BcStatus s;
4004 BcLexType t;
4005 bool paren;
4006
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004007 if (!BC_PARSE_FUNC(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004008
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004009 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004010 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
4012 t = p->l.t.t;
4013 paren = t == BC_LEX_LPAREN;
4014
4015 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4016 bc_parse_push(p, BC_INST_RET0);
4017 else {
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004018 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004019 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004020 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004021 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004022 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004023 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004024
4025 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004026 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004027 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06004028 }
4029
4030 bc_parse_push(p, BC_INST_RET);
4031 }
4032
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004033 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004034}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004035#if ERRORS_ARE_FATAL
4036# define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS)
4037#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004038
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004039static BC_STATUS zbc_parse_endBody(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004040{
4041 BcStatus s = BC_STATUS_SUCCESS;
4042
4043 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004044 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004045
4046 if (brace) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004047 if (p->l.t.t != BC_LEX_RBRACE)
4048 RETURN_STATUS(bc_error_bad_token());
4049 if (!p->nbraces)
4050 RETURN_STATUS(bc_error_bad_token());
4051 --p->nbraces;
4052 s = zbc_lex_next(&p->l);
4053 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004054 }
4055
4056 if (BC_PARSE_IF(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004057 uint8_t *flag_ptr;
4058
4059 while (p->l.t.t == BC_LEX_NLINE) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004060 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004061 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004062 }
4063
4064 bc_vec_pop(&p->flags);
4065
4066 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4067 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4068
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004069 if (p->l.t.t == BC_LEX_KEY_ELSE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004070 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004071 }
4072 else if (BC_PARSE_ELSE(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004073 BcInstPtr *ip;
4074 size_t *label;
4075
4076 bc_vec_pop(&p->flags);
4077
4078 ip = bc_vec_top(&p->exits);
4079 label = bc_vec_item(&p->func->labels, ip->idx);
4080 *label = p->func->code.len;
4081
4082 bc_vec_pop(&p->exits);
4083 }
4084 else if (BC_PARSE_FUNC_INNER(p)) {
4085 bc_parse_push(p, BC_INST_RET0);
4086 bc_parse_updateFunc(p, BC_PROG_MAIN);
4087 bc_vec_pop(&p->flags);
4088 }
4089 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004090 BcInstPtr *ip = bc_vec_top(&p->exits);
4091 size_t *label = bc_vec_top(&p->conds);
4092
4093 bc_parse_push(p, BC_INST_JUMP);
4094 bc_parse_pushIndex(p, *label);
4095
4096 label = bc_vec_item(&p->func->labels, ip->idx);
4097 *label = p->func->code.len;
4098
4099 bc_vec_pop(&p->flags);
4100 bc_vec_pop(&p->exits);
4101 bc_vec_pop(&p->conds);
4102 }
4103
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004104 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004105}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004106#if ERRORS_ARE_FATAL
4107# define zbc_parse_endBody(...) (zbc_parse_endBody(__VA_ARGS__), BC_STATUS_SUCCESS)
4108#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004109
4110static void bc_parse_startBody(BcParse *p, uint8_t flags)
4111{
4112 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4113 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4114 flags |= BC_PARSE_FLAG_BODY;
4115 bc_vec_push(&p->flags, &flags);
4116}
4117
4118static void bc_parse_noElse(BcParse *p)
4119{
4120 BcInstPtr *ip;
4121 size_t *label;
4122 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4123
4124 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4125
4126 ip = bc_vec_top(&p->exits);
4127 label = bc_vec_item(&p->func->labels, ip->idx);
4128 *label = p->func->code.len;
4129
4130 bc_vec_pop(&p->exits);
4131}
4132
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004133static BC_STATUS zbc_parse_if(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004134{
4135 BcStatus s;
4136 BcInstPtr ip;
4137
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004138 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004139 if (s) RETURN_STATUS(s);
4140 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004141
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004142 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004143 if (s) RETURN_STATUS(s);
4144 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4145 if (s) RETURN_STATUS(s);
4146 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004147
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004148 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004149 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004150 bc_parse_push(p, BC_INST_JUMP_ZERO);
4151
4152 ip.idx = p->func->labels.len;
4153 ip.func = ip.len = 0;
4154
4155 bc_parse_pushIndex(p, ip.idx);
4156 bc_vec_push(&p->exits, &ip);
4157 bc_vec_push(&p->func->labels, &ip.idx);
4158 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4159
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004160 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004161}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004162#if ERRORS_ARE_FATAL
4163# define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__), BC_STATUS_SUCCESS)
4164#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004165
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004166#undef zbc_parse_else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004167static BC_STATUS zbc_parse_else(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004168{
4169 BcInstPtr ip;
4170
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004171 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004172
4173 ip.idx = p->func->labels.len;
4174 ip.func = ip.len = 0;
4175
4176 bc_parse_push(p, BC_INST_JUMP);
4177 bc_parse_pushIndex(p, ip.idx);
4178
4179 bc_parse_noElse(p);
4180
4181 bc_vec_push(&p->exits, &ip);
4182 bc_vec_push(&p->func->labels, &ip.idx);
4183 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4184
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004185 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004186}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004187#if ERRORS_ARE_FATAL
4188# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
4189#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004190
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004191static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004192{
4193 BcStatus s;
4194 BcInstPtr ip;
4195
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004196 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004197 if (s) RETURN_STATUS(s);
4198 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004199 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004200 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004201
4202 ip.idx = p->func->labels.len;
4203
4204 bc_vec_push(&p->func->labels, &p->func->code.len);
4205 bc_vec_push(&p->conds, &ip.idx);
4206
4207 ip.idx = p->func->labels.len;
4208 ip.func = 1;
4209 ip.len = 0;
4210
4211 bc_vec_push(&p->exits, &ip);
4212 bc_vec_push(&p->func->labels, &ip.idx);
4213
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004214 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4215 if (s) RETURN_STATUS(s);
4216 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004217 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004218 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004219
4220 bc_parse_push(p, BC_INST_JUMP_ZERO);
4221 bc_parse_pushIndex(p, ip.idx);
4222 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4223
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004224 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004225}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004226#if ERRORS_ARE_FATAL
4227# define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__), BC_STATUS_SUCCESS)
4228#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004229
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004230static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004231{
4232 BcStatus s;
4233 BcInstPtr ip;
4234 size_t cond_idx, exit_idx, body_idx, update_idx;
4235
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004236 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004237 if (s) RETURN_STATUS(s);
4238 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004239 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004240 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004241
4242 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004243 s = zbc_parse_expr(p, 0, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004244 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004245 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004246
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004247 if (s) RETURN_STATUS(s);
4248 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004249 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004250 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004251
4252 cond_idx = p->func->labels.len;
4253 update_idx = cond_idx + 1;
4254 body_idx = update_idx + 1;
4255 exit_idx = body_idx + 1;
4256
4257 bc_vec_push(&p->func->labels, &p->func->code.len);
4258
4259 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004260 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004261 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004262 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004263
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004264 if (s) RETURN_STATUS(s);
4265 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004266
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004267 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004268 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004269
4270 bc_parse_push(p, BC_INST_JUMP_ZERO);
4271 bc_parse_pushIndex(p, exit_idx);
4272 bc_parse_push(p, BC_INST_JUMP);
4273 bc_parse_pushIndex(p, body_idx);
4274
4275 ip.idx = p->func->labels.len;
4276
4277 bc_vec_push(&p->conds, &update_idx);
4278 bc_vec_push(&p->func->labels, &p->func->code.len);
4279
4280 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004281 s = zbc_parse_expr(p, 0, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06004282 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004283 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004284
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004285 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004286
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004287 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004288 bc_parse_push(p, BC_INST_JUMP);
4289 bc_parse_pushIndex(p, cond_idx);
4290 bc_vec_push(&p->func->labels, &p->func->code.len);
4291
4292 ip.idx = exit_idx;
4293 ip.func = 1;
4294 ip.len = 0;
4295
4296 bc_vec_push(&p->exits, &ip);
4297 bc_vec_push(&p->func->labels, &ip.idx);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004298 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004299 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004300 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4301
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004302 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004303}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004304#if ERRORS_ARE_FATAL
4305# define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__), BC_STATUS_SUCCESS)
4306#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004307
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004308static BC_STATUS zbc_parse_loopExit(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004309{
4310 BcStatus s;
4311 size_t i;
4312 BcInstPtr *ip;
4313
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004314 if (!BC_PARSE_LOOP(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004315
4316 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004317 if (p->exits.len == 0) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004318
4319 i = p->exits.len - 1;
4320 ip = bc_vec_item(&p->exits, i);
4321
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004322 while (!ip->func && i < p->exits.len)
4323 ip = bc_vec_item(&p->exits, i--);
4324 if (i >= p->exits.len && !ip->func)
4325 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004326
4327 i = ip->idx;
4328 }
4329 else
4330 i = *((size_t *) bc_vec_top(&p->conds));
4331
4332 bc_parse_push(p, BC_INST_JUMP);
4333 bc_parse_pushIndex(p, i);
4334
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004335 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004336 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004337
4338 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004339 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004340
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004341 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004342}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004343#if ERRORS_ARE_FATAL
4344# define zbc_parse_loopExit(...) (zbc_parse_loopExit(__VA_ARGS__), BC_STATUS_SUCCESS)
4345#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004346
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004347static BC_STATUS zbc_parse_func(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004348{
4349 BcStatus s;
4350 bool var, comma = false;
4351 uint8_t flags;
4352 char *name;
4353
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004354 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004355 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004356 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004357 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004358
4359 name = xstrdup(p->l.t.v.v);
4360 bc_parse_addFunc(p, name, &p->fidx);
4361
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004362 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004363 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004364 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004365 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004366 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004367 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004368
4369 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004370 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004371 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004372
4373 ++p->func->nparams;
4374
4375 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004376 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004377 if (s) goto err;
4378
4379 var = p->l.t.t != BC_LEX_LBRACKET;
4380
4381 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004382 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004383 if (s) goto err;
4384
4385 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004386 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004387 goto err;
4388 }
4389
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004390 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004391 if (s) goto err;
4392 }
4393
4394 comma = p->l.t.t == BC_LEX_COMMA;
4395 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004396 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004397 if (s) goto err;
4398 }
4399
Denys Vlasenko29301232018-12-11 15:29:32 +01004400 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004401 if (s) goto err;
4402 }
4403
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004404 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004405
4406 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4407 bc_parse_startBody(p, flags);
4408
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004409 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004410 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004411
4412 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004413 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004414
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004415 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004416
4417err:
4418 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004419 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004420}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004421#if ERRORS_ARE_FATAL
4422# define zbc_parse_func(...) (zbc_parse_func(__VA_ARGS__), BC_STATUS_SUCCESS)
4423#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004424
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004425static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004426{
4427 BcStatus s;
4428 bool comma, var, one;
4429 char *name;
4430
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004431 if (!p->auto_part) RETURN_STATUS(bc_error_bad_token());
4432
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004433 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004434 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004435
4436 p->auto_part = comma = false;
4437 one = p->l.t.t == BC_LEX_NAME;
4438
4439 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004440 name = xstrdup(p->l.t.v.v);
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 var = p->l.t.t != BC_LEX_LBRACKET;
4445 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004446 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004447 if (s) goto err;
4448
4449 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004450 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004451 goto err;
4452 }
4453
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004454 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004455 if (s) goto err;
4456 }
4457
4458 comma = p->l.t.t == BC_LEX_COMMA;
4459 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004460 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004461 if (s) goto err;
4462 }
4463
Denys Vlasenko29301232018-12-11 15:29:32 +01004464 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004465 if (s) goto err;
4466 }
4467
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004468 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4469 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004470
4471 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004472 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004473
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004474 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004475
4476err:
4477 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004478 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004479}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004480#if ERRORS_ARE_FATAL
4481# define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__), BC_STATUS_SUCCESS)
4482#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004483
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004484static BC_STATUS zbc_parse_body(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004485{
4486 BcStatus s = BC_STATUS_SUCCESS;
4487 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4488
4489 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4490
4491 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004492 if (!brace) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004493
Gavin Howard01055ba2018-11-03 11:00:21 -06004494 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4495
4496 if (!p->auto_part) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004497 s = zbc_parse_auto(p);
4498 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 }
4500
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004501 if (p->l.t.t == BC_LEX_NLINE) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004502 }
4503 else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004504 s = zbc_parse_stmt(p);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004505 if (!s && !brace) s = zbc_parse_endBody(p, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004506 }
4507
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004508 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004509}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004510#if ERRORS_ARE_FATAL
4511# define zbc_parse_body(...) (zbc_parse_body(__VA_ARGS__), BC_STATUS_SUCCESS)
4512#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004513
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004514#undef zbc_parse_stmt
4515static BC_STATUS zbc_parse_stmt(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004516{
4517 BcStatus s = BC_STATUS_SUCCESS;
4518
4519 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004520 case BC_LEX_NLINE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004521 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004522
4523 case BC_LEX_KEY_ELSE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004524 p->auto_part = false;
4525 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004526
4527 case BC_LEX_LBRACE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004528 if (!BC_PARSE_BODY(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004529 ++p->nbraces;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004530 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004531 if (s) RETURN_STATUS(s);
4532 RETURN_STATUS(zbc_parse_body(p, true));
Gavin Howard01055ba2018-11-03 11:00:21 -06004533
4534 case BC_LEX_KEY_AUTO:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004535 RETURN_STATUS(zbc_parse_auto(p));
Gavin Howard01055ba2018-11-03 11:00:21 -06004536
4537 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06004538 p->auto_part = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004539 if (BC_PARSE_IF_END(p)) {
4540 bc_parse_noElse(p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004541 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004542 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004543 if (BC_PARSE_BODY(p))
4544 RETURN_STATUS(zbc_parse_body(p, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06004545 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004546 }
4547
4548 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 case BC_LEX_OP_INC:
4550 case BC_LEX_OP_DEC:
4551 case BC_LEX_OP_MINUS:
4552 case BC_LEX_OP_BOOL_NOT:
4553 case BC_LEX_LPAREN:
4554 case BC_LEX_NAME:
4555 case BC_LEX_NUMBER:
4556 case BC_LEX_KEY_IBASE:
4557 case BC_LEX_KEY_LAST:
4558 case BC_LEX_KEY_LENGTH:
4559 case BC_LEX_KEY_OBASE:
4560 case BC_LEX_KEY_READ:
4561 case BC_LEX_KEY_SCALE:
4562 case BC_LEX_KEY_SQRT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004563 s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
Gavin Howard01055ba2018-11-03 11:00:21 -06004564 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004565 case BC_LEX_KEY_ELSE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004566 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 case BC_LEX_SCOLON:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004569 while (!s && p->l.t.t == BC_LEX_SCOLON) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 case BC_LEX_RBRACE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004572 s = zbc_parse_endBody(p, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004574 case BC_LEX_STR:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004575 s = zbc_parse_string(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004577 case BC_LEX_KEY_BREAK:
4578 case BC_LEX_KEY_CONTINUE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004579 s = zbc_parse_loopExit(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004582 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004586 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004587 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004589 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004590 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004591 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004592 // "limits" is a compile-time command,
4593 // the output is produced at _parse time_.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004594 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004595 if (s) RETURN_STATUS(s);
Denys Vlasenko64074a12018-12-07 15:50:14 +01004596 printf(
4597 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4598 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4599 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4600 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4601 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4602 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4603 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4604 "Number of vars = "BC_MAX_VARS_STR "\n"
4605 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004606 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004607 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004608 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004610 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004611 // "quit" is a compile-time command. For example,
4612 // "if (0 == 1) quit" terminates when parsing the statement,
4613 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004614 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004615 case BC_LEX_KEY_RETURN:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004616 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004618 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004619 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004622 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004623 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 }
4625
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004626 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004627}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004628#if ERRORS_ARE_FATAL
4629# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
4630#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004631
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01004632static BC_STATUS zbc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004633{
4634 BcStatus s;
4635
4636 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004637 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 -06004638 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004639 if (!BC_PARSE_CAN_EXEC(p))
4640 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004641 s = zbc_parse_func(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004642 } else
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004643 s = zbc_parse_stmt(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004644
Denys Vlasenkod38af482018-12-04 19:11:02 +01004645 if (s || G_interrupt) {
4646 bc_parse_reset(p);
4647 s = BC_STATUS_FAILURE;
4648 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004649
Denys Vlasenko26819db2018-12-12 16:08:46 +01004650 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004651}
Denys Vlasenko26819db2018-12-12 16:08:46 +01004652#if ERRORS_ARE_FATAL
4653# define zbc_parse_parse(...) (zbc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
4654#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004655
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004656// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004657static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004658{
4659 BcStatus s = BC_STATUS_SUCCESS;
4660 BcInst prev = BC_INST_PRINT;
4661 BcLexType top, t = p->l.t.t;
4662 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004663 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004664 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4665
4666 paren_first = p->l.t.t == BC_LEX_LPAREN;
4667 nparens = nrelops = 0;
4668 paren_expr = rprn = done = get_token = assign = false;
4669 bin_last = true;
4670
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004671 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004672 switch (t) {
4673
4674 case BC_LEX_OP_INC:
4675 case BC_LEX_OP_DEC:
4676 {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004677 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004678 rprn = get_token = bin_last = false;
4679 break;
4680 }
4681
4682 case BC_LEX_OP_MINUS:
4683 {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004684 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004685 rprn = get_token = false;
4686 bin_last = prev == BC_INST_MINUS;
4687 break;
4688 }
4689
4690 case BC_LEX_OP_ASSIGN_POWER:
4691 case BC_LEX_OP_ASSIGN_MULTIPLY:
4692 case BC_LEX_OP_ASSIGN_DIVIDE:
4693 case BC_LEX_OP_ASSIGN_MODULUS:
4694 case BC_LEX_OP_ASSIGN_PLUS:
4695 case BC_LEX_OP_ASSIGN_MINUS:
4696 case BC_LEX_OP_ASSIGN:
4697 {
4698 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4699 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4700 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4701 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004702 s = bc_error("bad assignment:"
4703 " left side must be scale,"
4704 " ibase, obase, last, var,"
4705 " or array element"
4706 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004707 break;
4708 }
4709 }
4710 // Fallthrough.
4711 case BC_LEX_OP_POWER:
4712 case BC_LEX_OP_MULTIPLY:
4713 case BC_LEX_OP_DIVIDE:
4714 case BC_LEX_OP_MODULUS:
4715 case BC_LEX_OP_PLUS:
4716 case BC_LEX_OP_REL_EQ:
4717 case BC_LEX_OP_REL_LE:
4718 case BC_LEX_OP_REL_GE:
4719 case BC_LEX_OP_REL_NE:
4720 case BC_LEX_OP_REL_LT:
4721 case BC_LEX_OP_REL_GT:
4722 case BC_LEX_OP_BOOL_NOT:
4723 case BC_LEX_OP_BOOL_OR:
4724 case BC_LEX_OP_BOOL_AND:
4725 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004726 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4727 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4728 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004729 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004730 }
4731
4732 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4733 prev = BC_PARSE_TOKEN_INST(t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004734 s = zbc_parse_operator(p, t, ops_bgn, &nexprs, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004735 rprn = get_token = false;
4736 bin_last = t != BC_LEX_OP_BOOL_NOT;
4737
4738 break;
4739 }
4740
4741 case BC_LEX_LPAREN:
4742 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004743 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004744 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004745 ++nparens;
4746 paren_expr = rprn = bin_last = false;
4747 get_token = true;
4748 bc_vec_push(&p->ops, &t);
4749
4750 break;
4751 }
4752
4753 case BC_LEX_RPAREN:
4754 {
4755 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004756 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004757
4758 if (nparens == 0) {
4759 s = BC_STATUS_SUCCESS;
4760 done = true;
4761 get_token = false;
4762 break;
4763 }
4764 else if (!paren_expr)
4765 return BC_STATUS_PARSE_EMPTY_EXP;
4766
4767 --nparens;
4768 paren_expr = rprn = true;
4769 get_token = bin_last = false;
4770
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004771 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004772
4773 break;
4774 }
4775
4776 case BC_LEX_NAME:
4777 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004778 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004779 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004780 paren_expr = true;
4781 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004782 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 ++nexprs;
4784
4785 break;
4786 }
4787
4788 case BC_LEX_NUMBER:
4789 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004790 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004791 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004792 bc_parse_number(p, &prev, &nexprs);
4793 paren_expr = get_token = true;
4794 rprn = bin_last = false;
4795
4796 break;
4797 }
4798
4799 case BC_LEX_KEY_IBASE:
4800 case BC_LEX_KEY_LAST:
4801 case BC_LEX_KEY_OBASE:
4802 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004803 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004804 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004805 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4806 bc_parse_push(p, (char) prev);
4807
4808 paren_expr = get_token = true;
4809 rprn = bin_last = false;
4810 ++nexprs;
4811
4812 break;
4813 }
4814
4815 case BC_LEX_KEY_LENGTH:
4816 case BC_LEX_KEY_SQRT:
4817 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004818 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004819 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004820 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004821 paren_expr = true;
4822 rprn = get_token = bin_last = false;
4823 ++nexprs;
4824
4825 break;
4826 }
4827
4828 case BC_LEX_KEY_READ:
4829 {
4830 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004831 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004832 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004833 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004834 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004835 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004836
4837 paren_expr = true;
4838 rprn = get_token = bin_last = false;
4839 ++nexprs;
4840 prev = BC_INST_READ;
4841
4842 break;
4843 }
4844
4845 case BC_LEX_KEY_SCALE:
4846 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004847 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004848 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004849 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004850 paren_expr = true;
4851 rprn = get_token = bin_last = false;
4852 ++nexprs;
4853 prev = BC_INST_SCALE;
4854
4855 break;
4856 }
4857
4858 default:
4859 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004860 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004861 break;
4862 }
4863 }
4864
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004865 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004866 }
4867
4868 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004869 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004870
4871 while (p->ops.len > ops_bgn) {
4872
4873 top = BC_PARSE_TOP_OP(p);
4874 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4875
4876 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004877 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004878
4879 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4880
4881 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4882 bc_vec_pop(&p->ops);
4883 }
4884
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004885 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004886 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004887
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004888 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4889 for (;;) {
4890 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004891 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004892 if (next & 0x80) // last element?
4893 break;
4894 next >>= 8;
4895 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004896 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004897 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004898
4899 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004900 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004901 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004902 }
4903 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004904 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004905 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004906 }
4907
4908 if (flags & BC_PARSE_PRINT) {
4909 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4910 bc_parse_push(p, BC_INST_POP);
4911 }
4912
4913 return s;
4914}
4915
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004916#undef zbc_parse_expr
4917static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004918{
4919 BcStatus s;
4920
4921 s = bc_parse_expr_empty_ok(p, flags, next);
4922 if (s == BC_STATUS_PARSE_EMPTY_EXP)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004923 RETURN_STATUS(bc_error("empty expression"));
4924 RETURN_STATUS(s);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004925}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004926#if ERRORS_ARE_FATAL
4927# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
4928#endif
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004929
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004930static BC_STATUS zbc_parse_expression(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004931{
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004932 RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read));
Gavin Howard01055ba2018-11-03 11:00:21 -06004933}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004934#if ERRORS_ARE_FATAL
4935# define zbc_parse_expression(...) (zbc_parse_expression(__VA_ARGS__), BC_STATUS_SUCCESS)
4936#endif
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004937
Gavin Howard01055ba2018-11-03 11:00:21 -06004938#endif // ENABLE_BC
4939
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004940#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004941
4942#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
4943
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004944static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004945{
4946 BcStatus s;
4947 char *name;
4948
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004949 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004950 if (s) RETURN_STATUS(s);
4951 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004952
4953 name = xstrdup(p->l.t.v.v);
4954 bc_parse_pushName(p, name);
4955
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004956 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004957}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004958#if ERRORS_ARE_FATAL
4959# define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__), BC_STATUS_SUCCESS)
4960#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004961
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004962static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004963{
4964 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004965 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004966
4967 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
4968 name = xstrdup(b);
4969
4970 str = xstrdup(p->l.t.v.v);
4971 bc_parse_push(p, BC_INST_STR);
4972 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004973 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004974 bc_parse_addFunc(p, name, &idx);
4975
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004976 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004977}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004978#if ERRORS_ARE_FATAL
4979# define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
4980#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004982static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004983{
4984 BcStatus s;
4985
4986 bc_parse_push(p, inst);
4987 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004988 s = zdc_parse_register(p);
4989 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004990 }
4991
4992 if (store) {
4993 bc_parse_push(p, BC_INST_SWAP);
4994 bc_parse_push(p, BC_INST_ASSIGN);
4995 bc_parse_push(p, BC_INST_POP);
4996 }
4997
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004998 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004999}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005000#if ERRORS_ARE_FATAL
5001# define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__), BC_STATUS_SUCCESS)
5002#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005003
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005004static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005005{
5006 BcStatus s;
5007
5008 bc_parse_push(p, inst);
5009 bc_parse_push(p, BC_INST_EXEC_COND);
5010
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005011 s = zdc_parse_register(p);
5012 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005013
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005014 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005016
5017 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005018 s = zdc_parse_register(p);
5019 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005020 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005021 }
5022 else
5023 bc_parse_push(p, BC_PARSE_STREND);
5024
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005025 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005026}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005027#if ERRORS_ARE_FATAL
5028# define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__), BC_STATUS_SUCCESS)
5029#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005030
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005031static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005032{
5033 BcStatus s = BC_STATUS_SUCCESS;
5034 BcInst prev;
5035 uint8_t inst;
5036 bool assign, get_token = false;
5037
5038 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005039 case BC_LEX_OP_REL_EQ:
5040 case BC_LEX_OP_REL_LE:
5041 case BC_LEX_OP_REL_GE:
5042 case BC_LEX_OP_REL_NE:
5043 case BC_LEX_OP_REL_LT:
5044 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005045 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005046 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005047 case BC_LEX_SCOLON:
5048 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005049 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005050 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005051 case BC_LEX_STR:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005052 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06005053 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005054 case BC_LEX_NEG:
5055 case BC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06005056 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005057 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005058 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005059 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005060 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005061 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005062 bc_parse_number(p, &prev, &p->nbraces);
Gavin Howard01055ba2018-11-03 11:00:21 -06005063 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5064 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005065 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06005067 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005068 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005069 else
5070 bc_parse_push(p, BC_INST_READ);
5071 get_token = true;
5072 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005073 case BC_LEX_OP_ASSIGN:
5074 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06005075 assign = t == BC_LEX_OP_ASSIGN;
5076 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005077 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005079 case BC_LEX_LOAD:
5080 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06005081 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005082 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005083 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 case BC_LEX_STORE_IBASE:
5085 case BC_LEX_STORE_SCALE:
5086 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005087 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005088 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005089 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005090 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005091 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005092 get_token = true;
5093 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005094 }
5095
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005096 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005097
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005098 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005099}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005100#if ERRORS_ARE_FATAL
5101# define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__), BC_STATUS_SUCCESS)
5102#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005103
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005104static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005105{
5106 BcStatus s = BC_STATUS_SUCCESS;
5107 BcInst inst;
5108 BcLexType t;
5109
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005110 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005111
5112 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005113 inst = dc_parse_insts[t];
5114
5115 if (inst != BC_INST_INVALID) {
5116 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005117 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005118 } else
5119 s = zdc_parse_token(p, t, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06005120 }
5121
5122 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5123 bc_parse_push(p, BC_INST_POP_EXEC);
5124
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005125 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005126}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005127#if ERRORS_ARE_FATAL
5128# define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5129#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005130
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01005131static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005132{
5133 BcStatus s;
5134
5135 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005136 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005137 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005138 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005139
Denys Vlasenkod38af482018-12-04 19:11:02 +01005140 if (s || G_interrupt) {
5141 bc_parse_reset(p);
5142 s = BC_STATUS_FAILURE;
5143 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005144
Denys Vlasenko26819db2018-12-12 16:08:46 +01005145 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005146}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005147#if ERRORS_ARE_FATAL
5148# define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
5149#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005150
Gavin Howard01055ba2018-11-03 11:00:21 -06005151#endif // ENABLE_DC
5152
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005153static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005154{
5155 if (IS_BC) {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005156 IF_BC(RETURN_STATUS(zbc_parse_expression(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005157 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005158 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005159 }
5160}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005161#if ERRORS_ARE_FATAL
5162# define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5163#endif
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005164
Denys Vlasenkodf515392018-12-02 19:27:48 +01005165static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005166{
Gavin Howard01055ba2018-11-03 11:00:21 -06005167 BcId e, *ptr;
5168 BcVec *v, *map;
5169 size_t i;
5170 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005171 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005172
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005173 v = var ? &G.prog.vars : &G.prog.arrs;
5174 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005175
5176 e.name = id;
5177 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005178 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005179
5180 if (new) {
5181 bc_array_init(&data.v, var);
5182 bc_vec_push(v, &data.v);
5183 }
5184
5185 ptr = bc_vec_item(map, i);
5186 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005187 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005188}
5189
Denys Vlasenko29301232018-12-11 15:29:32 +01005190static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005191{
Gavin Howard01055ba2018-11-03 11:00:21 -06005192 switch (r->t) {
5193
5194 case BC_RESULT_STR:
5195 case BC_RESULT_TEMP:
5196 case BC_RESULT_IBASE:
5197 case BC_RESULT_SCALE:
5198 case BC_RESULT_OBASE:
5199 {
5200 *num = &r->d.n;
5201 break;
5202 }
5203
5204 case BC_RESULT_CONSTANT:
5205 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005206 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005207 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005208 size_t base_t, len = strlen(*str);
5209 BcNum *base;
5210
5211 bc_num_init(&r->d.n, len);
5212
5213 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005214 base = hex ? &G.prog.hexb : &G.prog.ib;
5215 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005216 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
5218 if (s) {
5219 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005220 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005221 }
5222
5223 *num = &r->d.n;
5224 r->t = BC_RESULT_TEMP;
5225
5226 break;
5227 }
5228
5229 case BC_RESULT_VAR:
5230 case BC_RESULT_ARRAY:
5231 case BC_RESULT_ARRAY_ELEM:
5232 {
5233 BcVec *v;
5234
Denys Vlasenkodf515392018-12-02 19:27:48 +01005235 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005236
5237 if (r->t == BC_RESULT_ARRAY_ELEM) {
5238 v = bc_vec_top(v);
5239 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5240 *num = bc_vec_item(v, r->d.id.idx);
5241 }
5242 else
5243 *num = bc_vec_top(v);
5244
5245 break;
5246 }
5247
5248 case BC_RESULT_LAST:
5249 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005250 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005251 break;
5252 }
5253
5254 case BC_RESULT_ONE:
5255 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005256 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005257 break;
5258 }
5259 }
5260
Denys Vlasenko29301232018-12-11 15:29:32 +01005261 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005262}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005263#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005264# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005265#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005266
Denys Vlasenko29301232018-12-11 15:29:32 +01005267static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005268 BcResult **r, BcNum **rn, bool assign)
5269{
5270 BcStatus s;
5271 bool hex;
5272 BcResultType lt, rt;
5273
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005274 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005275 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005276
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005277 *r = bc_vec_item_rev(&G.prog.results, 0);
5278 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005279
5280 lt = (*l)->t;
5281 rt = (*r)->t;
5282 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5283
Denys Vlasenko29301232018-12-11 15:29:32 +01005284 s = zbc_program_num(*l, ln, false);
5285 if (s) RETURN_STATUS(s);
5286 s = zbc_program_num(*r, rn, hex);
5287 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005288
5289 // We run this again under these conditions in case any vector has been
5290 // reallocated out from under the BcNums or arrays we had.
5291 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005292 s = zbc_program_num(*l, ln, false);
5293 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005294 }
5295
5296 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005297 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005298 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005299 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005300
Denys Vlasenko29301232018-12-11 15:29:32 +01005301 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005302}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005303#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005304# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005305#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005306
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005307static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005308{
5309 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005310 bc_vec_pop(&G.prog.results);
5311 bc_vec_pop(&G.prog.results);
5312 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005313}
5314
Denys Vlasenko29301232018-12-11 15:29:32 +01005315static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005316{
5317 BcStatus s;
5318
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005319 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005320 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005321 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005322
Denys Vlasenko29301232018-12-11 15:29:32 +01005323 s = zbc_program_num(*r, n, false);
5324 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005325
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005326 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005327 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005328
Denys Vlasenko29301232018-12-11 15:29:32 +01005329 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005330}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005331#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005332# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005333#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005334
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005335static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005336{
5337 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005338 bc_vec_pop(&G.prog.results);
5339 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005340}
5341
Denys Vlasenko259137d2018-12-11 19:42:05 +01005342static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005343{
5344 BcStatus s;
5345 BcResult *opd1, *opd2, res;
5346 BcNum *n1, *n2 = NULL;
5347
Denys Vlasenko29301232018-12-11 15:29:32 +01005348 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005349 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005350 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005351
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005352 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005353 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 -06005354 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005355 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005356
Denys Vlasenko259137d2018-12-11 19:42:05 +01005357 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005358
5359err:
5360 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005361 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005362}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005363#if ERRORS_ARE_FATAL
5364# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5365#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005366
Denys Vlasenko26819db2018-12-12 16:08:46 +01005367static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005368{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005369 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005370 BcStatus s;
5371 BcParse parse;
5372 BcVec buf;
5373 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005374 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005375
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005376 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005377 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005378
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005379 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005380 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005381
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005382 sv_file = G.prog.file;
5383 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005384 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005385
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005386 bc_char_vec_init(&buf);
Denys Vlasenko8a892472018-12-12 22:43:58 +01005387 bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005388
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005389 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005390 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005391
Denys Vlasenko26819db2018-12-12 16:08:46 +01005392 s = zbc_parse_text(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005393 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005394 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005395 if (s) goto exec_err;
5396
5397 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005398 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005399 goto exec_err;
5400 }
5401
5402 ip.func = BC_PROG_READ;
5403 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005404 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005405
5406 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005407 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005408
5409 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005410 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005411
5412exec_err:
5413 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005414//io_err:
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005415 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005416 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005417 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005418 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005419}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005420#if ERRORS_ARE_FATAL
5421# define zbc_program_read(...) (zbc_program_read(__VA_ARGS__), BC_STATUS_SUCCESS)
5422#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005423
5424static size_t bc_program_index(char *code, size_t *bgn)
5425{
5426 char amt = code[(*bgn)++], i = 0;
5427 size_t res = 0;
5428
5429 for (; i < amt; ++i, ++(*bgn))
5430 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5431
5432 return res;
5433}
5434
5435static char *bc_program_name(char *code, size_t *bgn)
5436{
5437 size_t i;
5438 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5439
5440 s = xmalloc(ptr - str + 1);
5441 c = code[(*bgn)++];
5442
5443 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5444 s[i] = c;
5445
5446 s[i] = '\0';
5447
5448 return s;
5449}
5450
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005451static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005452{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005453#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005454 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005455 // Example: echo '[]ap' | dc
5456 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005457 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005458 return;
5459 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005460#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005461 while (*str) {
5462 int c = *str++;
5463 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005464 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005465 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005466 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005467 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005468 case 'a':
5469 bb_putchar('\a');
5470 break;
5471 case 'b':
5472 bb_putchar('\b');
5473 break;
5474 case '\\':
5475 case 'e':
5476 bb_putchar('\\');
5477 break;
5478 case 'f':
5479 bb_putchar('\f');
5480 break;
5481 case 'n':
5482 bb_putchar('\n');
5483 G.prog.nchars = SIZE_MAX;
5484 break;
5485 case 'r':
5486 bb_putchar('\r');
5487 break;
5488 case 'q':
5489 bb_putchar('"');
5490 break;
5491 case 't':
5492 bb_putchar('\t');
5493 break;
5494 default:
5495 // Just print the backslash and following character.
5496 bb_putchar('\\');
5497 ++G.prog.nchars;
5498 bb_putchar(c);
5499 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005500 }
5501 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005502 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005503 }
5504}
5505
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005506static void bc_num_printNewline(void)
5507{
5508 if (G.prog.nchars == G.prog.len - 1) {
5509 bb_putchar('\\');
5510 bb_putchar('\n');
5511 G.prog.nchars = 0;
5512 }
5513}
5514
5515#if ENABLE_DC
5516static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5517{
5518 (void) radix;
5519 bb_putchar((char) num);
5520 G.prog.nchars += width;
5521}
5522#endif
5523
5524static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5525{
5526 size_t exp, pow;
5527
5528 bc_num_printNewline();
5529 bb_putchar(radix ? '.' : ' ');
5530 ++G.prog.nchars;
5531
5532 bc_num_printNewline();
5533 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5534 continue;
5535
5536 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5537 size_t dig;
5538 bc_num_printNewline();
5539 dig = num / pow;
5540 num -= dig * pow;
5541 bb_putchar(((char) dig) + '0');
5542 }
5543}
5544
5545static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5546{
5547 if (radix) {
5548 bc_num_printNewline();
5549 bb_putchar('.');
5550 G.prog.nchars += 1;
5551 }
5552
5553 bc_num_printNewline();
5554 bb_putchar(bb_hexdigits_upcase[num]);
5555 G.prog.nchars += width;
5556}
5557
5558static void bc_num_printDecimal(BcNum *n)
5559{
5560 size_t i, rdx = n->rdx - 1;
5561
5562 if (n->neg) bb_putchar('-');
5563 G.prog.nchars += n->neg;
5564
5565 for (i = n->len - 1; i < n->len; --i)
5566 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5567}
5568
5569static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5570{
5571 BcStatus s;
5572 BcVec stack;
5573 BcNum intp, fracp, digit, frac_len;
5574 unsigned long dig, *ptr;
5575 size_t i;
5576 bool radix;
5577
5578 if (n->len == 0) {
5579 print(0, width, false);
5580 RETURN_STATUS(BC_STATUS_SUCCESS);
5581 }
5582
5583 bc_vec_init(&stack, sizeof(long), NULL);
5584 bc_num_init(&intp, n->len);
5585 bc_num_init(&fracp, n->rdx);
5586 bc_num_init(&digit, width);
5587 bc_num_init(&frac_len, BC_NUM_INT(n));
5588 bc_num_copy(&intp, n);
5589 bc_num_one(&frac_len);
5590
5591 bc_num_truncate(&intp, intp.rdx);
5592 s = zbc_num_sub(n, &intp, &fracp, 0);
5593 if (s) goto err;
5594
5595 while (intp.len != 0) {
5596 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5597 if (s) goto err;
5598 s = zbc_num_ulong(&digit, &dig);
5599 if (s) goto err;
5600 bc_vec_push(&stack, &dig);
5601 }
5602
5603 for (i = 0; i < stack.len; ++i) {
5604 ptr = bc_vec_item_rev(&stack, i);
5605 print(*ptr, width, false);
5606 }
5607
5608 if (!n->rdx) goto err;
5609
5610 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5611 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5612 if (s) goto err;
5613 s = zbc_num_ulong(&fracp, &dig);
5614 if (s) goto err;
5615 bc_num_ulong2num(&intp, dig);
5616 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5617 if (s) goto err;
5618 print(dig, width, radix);
5619 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5620 if (s) goto err;
5621 }
5622
5623err:
5624 bc_num_free(&frac_len);
5625 bc_num_free(&digit);
5626 bc_num_free(&fracp);
5627 bc_num_free(&intp);
5628 bc_vec_free(&stack);
5629 RETURN_STATUS(s);
5630}
5631#if ERRORS_ARE_FATAL
5632# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5633#endif
5634
5635static BC_STATUS zbc_num_printBase(BcNum *n)
5636{
5637 BcStatus s;
5638 size_t width, i;
5639 BcNumDigitOp print;
5640 bool neg = n->neg;
5641
5642 if (neg) {
5643 bb_putchar('-');
5644 G.prog.nchars++;
5645 }
5646
5647 n->neg = false;
5648
5649 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5650 width = 1;
5651 print = bc_num_printHex;
5652 }
5653 else {
5654 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5655 continue;
5656 print = bc_num_printDigits;
5657 }
5658
5659 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5660 n->neg = neg;
5661
5662 RETURN_STATUS(s);
5663}
5664#if ERRORS_ARE_FATAL
5665# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5666#endif
5667
5668#if ENABLE_DC
5669static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5670{
5671 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5672}
5673#if ERRORS_ARE_FATAL
5674# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5675#endif
5676#endif
5677
5678static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5679{
5680 BcStatus s = BC_STATUS_SUCCESS;
5681
5682 bc_num_printNewline();
5683
5684 if (n->len == 0) {
5685 bb_putchar('0');
5686 ++G.prog.nchars;
5687 }
5688 else if (G.prog.ob_t == 10)
5689 bc_num_printDecimal(n);
5690 else
5691 s = zbc_num_printBase(n);
5692
5693 if (newline) {
5694 bb_putchar('\n');
5695 G.prog.nchars = 0;
5696 }
5697
5698 RETURN_STATUS(s);
5699}
5700#if ERRORS_ARE_FATAL
5701# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5702#endif
5703
Denys Vlasenko29301232018-12-11 15:29:32 +01005704static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005705{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005706 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005707 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005708 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005709 bool pop = inst != BC_INST_PRINT;
5710
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005711 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005712 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005713
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005714 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005715 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005716 s = zbc_program_num(r, &num, false);
5717 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005718
5719 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005720 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005721 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005722 }
5723 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005724 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005725
5726 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005727 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005728
5729 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005730 for (;;) {
5731 char c = *str++;
5732 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005733 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005734 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005735 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005736 }
5737 }
5738 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005739 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005740 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005741 }
5742 }
5743
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005744 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005745
Denys Vlasenko29301232018-12-11 15:29:32 +01005746 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005747}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005748#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005749# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005750#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005751
Denys Vlasenko29301232018-12-11 15:29:32 +01005752static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005753{
5754 BcStatus s;
5755 BcResult res, *ptr;
5756 BcNum *num = NULL;
5757
Denys Vlasenko29301232018-12-11 15:29:32 +01005758 s = zbc_program_prep(&ptr, &num);
5759 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005760
5761 bc_num_init(&res.d.n, num->len);
5762 bc_num_copy(&res.d.n, num);
5763 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5764
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005765 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005766
Denys Vlasenko29301232018-12-11 15:29:32 +01005767 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005768}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005769#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005770# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005771#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005772
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005773static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005774{
5775 BcStatus s;
5776 BcResult *opd1, *opd2, res;
5777 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005778 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005779
Denys Vlasenko29301232018-12-11 15:29:32 +01005780 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005781 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005782
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005783 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005784
5785 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005786 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005787 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005788 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005789 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005790 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005791 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005792 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005793 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005794 break;
5795 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005796 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005797 break;
5798 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005799 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005800 break;
5801 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005802 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005803 break;
5804 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005805 cond = (cond > 0);
5806 break;
5807 default: // = case BC_INST_REL_NE:
5808 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005809 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005810 }
5811 }
5812
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005813 if (cond) bc_num_one(&res.d.n);
5814 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005815
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005816 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005817
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005818 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005819}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005820#if ERRORS_ARE_FATAL
5821# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5822#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005823
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005824#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005825static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005826 bool push)
5827{
5828 BcNum n2;
5829 BcResult res;
5830
5831 memset(&n2, 0, sizeof(BcNum));
5832 n2.rdx = res.d.id.idx = r->d.id.idx;
5833 res.t = BC_RESULT_STR;
5834
5835 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005836 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005837 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005838 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005839 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005840 }
5841
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005842 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005843
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005844 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005845 bc_vec_push(v, &n2);
5846
Denys Vlasenko29301232018-12-11 15:29:32 +01005847 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005849#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005850# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005851#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005852#endif // ENABLE_DC
5853
Denys Vlasenko29301232018-12-11 15:29:32 +01005854static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005855{
5856 BcStatus s;
5857 BcResult *ptr, r;
5858 BcVec *v;
5859 BcNum *n;
5860
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005861 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005862 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005863
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005864 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005865 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005866 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005867 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005868
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005869#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005870 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005871 RETURN_STATUS(bc_error_variable_is_wrong_type());
5872 if (ptr->t == BC_RESULT_STR)
5873 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005874#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005875
Denys Vlasenko29301232018-12-11 15:29:32 +01005876 s = zbc_program_num(ptr, &n, false);
5877 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005878
5879 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005880 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005881
5882 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005883 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884 bc_num_copy(&r.d.n, n);
5885 }
5886 else {
5887 bc_array_init(&r.d.v, true);
5888 bc_array_copy(&r.d.v, (BcVec *) n);
5889 }
5890
5891 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005892 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005893
Denys Vlasenko29301232018-12-11 15:29:32 +01005894 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005895}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005896#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005897# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005898#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005899
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005900static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005901{
5902 BcStatus s;
5903 BcResult *left, *right, res;
5904 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005905 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5906
Denys Vlasenko29301232018-12-11 15:29:32 +01005907 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005908 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005909
5910 ib = left->t == BC_RESULT_IBASE;
5911 sc = left->t == BC_RESULT_SCALE;
5912
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005913#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005914
5915 if (right->t == BC_RESULT_STR) {
5916
5917 BcVec *v;
5918
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005919 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005920 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005921 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005922
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005923 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005925#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005926
5927 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005928 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005929 " left side must be scale,"
5930 " ibase, obase, last, var,"
5931 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005932 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06005933
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005934#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005935 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005936 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005937
5938 if (assign)
5939 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005940 else {
5941 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005942 ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005943 }
5944 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005945#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005946 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005947#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005948
5949 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005950 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005951 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5952 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5953 NULL, //can't happen //BC_RESULT_LAST
5954 NULL, //can't happen //BC_RESULT_CONSTANT
5955 NULL, //can't happen //BC_RESULT_ONE
5956 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005957 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005958 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005959 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005960
Denys Vlasenko29301232018-12-11 15:29:32 +01005961 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005962 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005963 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 if (sc) {
5965 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005966 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005967 }
5968 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005969 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005970 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005971 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005972 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005973 }
5974
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005975 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005976 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005977 if (!sc)
5978 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005979
5980 *ptr = (size_t) val;
5981 s = BC_STATUS_SUCCESS;
5982 }
5983
5984 bc_num_init(&res.d.n, l->len);
5985 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005986 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005987
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005988 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005989}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005990#if ERRORS_ARE_FATAL
5991# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
5992#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005993
Denys Vlasenko416ce762018-12-02 20:57:17 +01005994#if !ENABLE_DC
5995#define bc_program_pushVar(code, bgn, pop, copy) \
5996 bc_program_pushVar(code, bgn)
5997// for bc, 'pop' and 'copy' are always false
5998#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005999static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006000 bool pop, bool copy)
6001{
Gavin Howard01055ba2018-11-03 11:00:21 -06006002 BcResult r;
6003 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006004
6005 r.t = BC_RESULT_VAR;
6006 r.d.id.name = name;
6007
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006008#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006009 {
6010 BcVec *v = bc_program_search(name, true);
6011 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006012
Denys Vlasenko416ce762018-12-02 20:57:17 +01006013 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006014
Denys Vlasenko416ce762018-12-02 20:57:17 +01006015 if (!BC_PROG_STACK(v, 2 - copy)) {
6016 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006017 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006018 }
6019
Gavin Howard01055ba2018-11-03 11:00:21 -06006020 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006021 name = NULL;
6022
6023 if (!BC_PROG_STR(num)) {
6024
6025 r.t = BC_RESULT_TEMP;
6026
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006027 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006028 bc_num_copy(&r.d.n, num);
6029 }
6030 else {
6031 r.t = BC_RESULT_STR;
6032 r.d.id.idx = num->rdx;
6033 }
6034
6035 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006037 }
6038#endif // ENABLE_DC
6039
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006040 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006041
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006042 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006043}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006044#if ERRORS_ARE_FATAL
6045# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6046#else
6047# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6048#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006049
Denys Vlasenko29301232018-12-11 15:29:32 +01006050static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006051 char inst)
6052{
6053 BcStatus s = BC_STATUS_SUCCESS;
6054 BcResult r;
6055 BcNum *num;
6056
6057 r.d.id.name = bc_program_name(code, bgn);
6058
6059 if (inst == BC_INST_ARRAY) {
6060 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006061 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006062 }
6063 else {
6064
6065 BcResult *operand;
6066 unsigned long temp;
6067
Denys Vlasenko29301232018-12-11 15:29:32 +01006068 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006069 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006070 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006071 if (s) goto err;
6072
6073 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006074 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006075 goto err;
6076 }
6077
6078 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006079 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006080 }
6081
6082err:
6083 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006084 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006085}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006086#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006087# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006088#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006089
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006090#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006091static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006092{
6093 BcStatus s;
6094 BcResult *ptr, res, copy;
6095 BcNum *num = NULL;
6096 char inst2 = inst;
6097
Denys Vlasenko29301232018-12-11 15:29:32 +01006098 s = zbc_program_prep(&ptr, &num);
6099 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006100
6101 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6102 copy.t = BC_RESULT_TEMP;
6103 bc_num_init(&copy.d.n, num->len);
6104 bc_num_copy(&copy.d.n, num);
6105 }
6106
6107 res.t = BC_RESULT_ONE;
6108 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6109 BC_INST_ASSIGN_PLUS :
6110 BC_INST_ASSIGN_MINUS;
6111
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006112 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006113 s = zbc_program_assign(inst);
6114 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006115
6116 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006117 bc_vec_pop(&G.prog.results);
6118 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006119 }
6120
Denys Vlasenko29301232018-12-11 15:29:32 +01006121 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006123#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006124# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006125#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006126
Denys Vlasenko29301232018-12-11 15:29:32 +01006127static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006128{
Gavin Howard01055ba2018-11-03 11:00:21 -06006129 BcInstPtr ip;
6130 size_t i, nparams = bc_program_index(code, idx);
6131 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 BcId *a;
6133 BcResultData param;
6134 BcResult *arg;
6135
6136 ip.idx = 0;
6137 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006138 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006139
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006140 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006141 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006142 }
6143 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006144 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006145 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006146 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006147
6148 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006149 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
6151 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006152 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006153
6154 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006155 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006156
Denys Vlasenko29301232018-12-11 15:29:32 +01006157 s = zbc_program_copyToVar(a->name, a->idx);
6158 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006159 }
6160
6161 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006162 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006163
6164 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006165 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006166
6167 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006168 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006169 bc_vec_push(v, &param.n);
6170 }
6171 else {
6172 bc_array_init(&param.v, true);
6173 bc_vec_push(v, &param.v);
6174 }
6175 }
6176
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006177 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006178
Denys Vlasenko29301232018-12-11 15:29:32 +01006179 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006180}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006181#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006182# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006183#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006184
Denys Vlasenko29301232018-12-11 15:29:32 +01006185static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006186{
Gavin Howard01055ba2018-11-03 11:00:21 -06006187 BcResult res;
6188 BcFunc *f;
6189 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006190 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006191
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006192 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006193 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006194
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006195 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006196 res.t = BC_RESULT_TEMP;
6197
6198 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006199 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006200 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006201 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202
Denys Vlasenko29301232018-12-11 15:29:32 +01006203 s = zbc_program_num(operand, &num, false);
6204 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006205 bc_num_init(&res.d.n, num->len);
6206 bc_num_copy(&res.d.n, num);
6207 }
6208 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006209 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006210 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006211 }
6212
6213 // We need to pop arguments as well, so this takes that into account.
6214 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006215 BcVec *v;
6216 BcId *a = bc_vec_item(&f->autos, i);
6217
Denys Vlasenkodf515392018-12-02 19:27:48 +01006218 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006219 bc_vec_pop(v);
6220 }
6221
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006222 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6223 bc_vec_push(&G.prog.results, &res);
6224 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006225
Denys Vlasenko29301232018-12-11 15:29:32 +01006226 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006227}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006228#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006229# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006230#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006231#endif // ENABLE_BC
6232
6233static unsigned long bc_program_scale(BcNum *n)
6234{
6235 return (unsigned long) n->rdx;
6236}
6237
6238static unsigned long bc_program_len(BcNum *n)
6239{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006240 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006241
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006242 if (n->rdx != len) return len;
6243 for (;;) {
6244 if (len == 0) break;
6245 len--;
6246 if (n->num[len] != 0) break;
6247 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006248 return len;
6249}
6250
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006251static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006252{
6253 BcStatus s;
6254 BcResult *opnd;
6255 BcNum *num = NULL;
6256 BcResult res;
6257 bool len = inst == BC_INST_LENGTH;
6258
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006259 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006260 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006261 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006262
Denys Vlasenko29301232018-12-11 15:29:32 +01006263 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006264 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006266#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006267 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006268 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006269#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006270
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006271 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006272
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006273 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006274#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006275 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006276 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006278#endif
6279#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006280 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006281 char **str;
6282 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6283
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006284 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006285 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006286 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006287#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006288 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006289 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006290 }
6291
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006292 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006293
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006294 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006295}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006296#if ERRORS_ARE_FATAL
6297# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6298#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006299
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006300#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006301static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006302{
6303 BcStatus s;
6304 BcResult *opd1, *opd2, res, res2;
6305 BcNum *n1, *n2 = NULL;
6306
Denys Vlasenko29301232018-12-11 15:29:32 +01006307 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006308 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006309
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006310 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006311 bc_num_init(&res2.d.n, n2->len);
6312
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006313 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006314 if (s) goto err;
6315
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006316 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006317 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006318 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006319
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006320 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006321
6322err:
6323 bc_num_free(&res2.d.n);
6324 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006325 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006326}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006327#if ERRORS_ARE_FATAL
6328# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6329#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006330
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006331static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006332{
6333 BcStatus s;
6334 BcResult *r1, *r2, *r3, res;
6335 BcNum *n1, *n2, *n3;
6336
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006337 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006338 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006339 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006340 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006342 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006343 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006344 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006345 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006346 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006347
6348 // Make sure that the values have their pointers updated, if necessary.
6349 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6350
6351 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006352 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006353 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006354 }
6355
6356 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006357 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006358 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006359 }
6360 }
6361
6362 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006363 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006364 if (s) goto err;
6365
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006366 bc_vec_pop(&G.prog.results);
6367 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006368
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006369 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006370
6371err:
6372 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006373 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006374}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006375#if ERRORS_ARE_FATAL
6376# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6377#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006378
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006379static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006380{
Gavin Howard01055ba2018-11-03 11:00:21 -06006381 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006382 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006383
6384 res.t = BC_RESULT_TEMP;
6385
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006386 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006387 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006388 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006389}
6390
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006391static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006392{
6393 BcStatus s;
6394 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006395 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006396 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006397 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006398 unsigned long val;
6399
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006400 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006401 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006402 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006404 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006405 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006406 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006407
6408 if (BC_PROG_NUM(r, num)) {
6409
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006410 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006411 bc_num_copy(&n, num);
6412 bc_num_truncate(&n, n.rdx);
6413
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006414 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006415 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006416 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006417 if (s) goto num_err;
6418
6419 c = (char) val;
6420
6421 bc_num_free(&n);
6422 }
6423 else {
6424 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006425 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 c = str2[0];
6427 }
6428
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006429 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006430 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006431 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006432
6433 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006434 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006435
6436 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006437 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006438 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006439 len = idx;
6440 break;
6441 }
6442 }
6443
6444 free(str);
6445 }
6446 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006447 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448
6449 res.t = BC_RESULT_STR;
6450 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006451 bc_vec_pop(&G.prog.results);
6452 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006453
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006454 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006455
6456num_err:
6457 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006458 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006459}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006460#if ERRORS_ARE_FATAL
6461# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6462#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006463
Denys Vlasenko29301232018-12-11 15:29:32 +01006464static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006465{
6466 BcStatus s;
6467 BcResult *r;
6468 BcNum *n = NULL;
6469 size_t idx;
6470 char *str;
6471
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006472 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006473 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006474 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006475
Denys Vlasenko29301232018-12-11 15:29:32 +01006476 s = zbc_program_num(r, &n, false);
6477 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006478
6479 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006480 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481 else {
6482 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006483 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006484 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006485 }
6486
Denys Vlasenko29301232018-12-11 15:29:32 +01006487 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006488}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006489#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006490# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006491#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006492
Denys Vlasenko29301232018-12-11 15:29:32 +01006493static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006494{
6495 BcStatus s;
6496 BcResult *opnd;
6497 BcNum *num = NULL;
6498 unsigned long val;
6499
Denys Vlasenko29301232018-12-11 15:29:32 +01006500 s = zbc_program_prep(&opnd, &num);
6501 if (s) RETURN_STATUS(s);
6502 s = zbc_num_ulong(num, &val);
6503 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006504
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006505 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006506
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006507 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006508 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006509 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006510 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006511 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006512
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006513 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006514
Denys Vlasenko29301232018-12-11 15:29:32 +01006515 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006516}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006517#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006518# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006519#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006520
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006521static BC_STATUS zbc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006522 bool cond)
6523{
6524 BcStatus s = BC_STATUS_SUCCESS;
6525 BcResult *r;
6526 char **str;
6527 BcFunc *f;
6528 BcParse prs;
6529 BcInstPtr ip;
6530 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006531
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006532 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006533 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006534
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536
6537 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006538 BcNum *n = n; // for compiler
6539 bool exec;
6540 char *name;
6541 char *then_name = bc_program_name(code, bgn);
6542 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006543
6544 if (code[*bgn] == BC_PARSE_STREND)
6545 (*bgn) += 1;
6546 else
6547 else_name = bc_program_name(code, bgn);
6548
6549 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006550 name = then_name;
6551 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006552 exec = true;
6553 name = else_name;
6554 }
6555
6556 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006557 BcVec *v;
6558 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 n = bc_vec_top(v);
6560 }
6561
6562 free(then_name);
6563 free(else_name);
6564
6565 if (!exec) goto exit;
6566 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006567 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006568 goto exit;
6569 }
6570
6571 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006572 } else {
6573 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006575 } else if (r->t == BC_RESULT_VAR) {
6576 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006577 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 if (s || !BC_PROG_STR(n)) goto exit;
6579 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006580 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 goto exit;
6582 }
6583
6584 fidx = sidx + BC_PROG_REQ_FUNCS;
6585
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006586 str = bc_program_str(sidx);
6587 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006588
6589 if (f->code.len == 0) {
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006590 bc_parse_create(&prs, fidx);
Denys Vlasenko26819db2018-12-12 16:08:46 +01006591 s = zbc_parse_text(&prs, *str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006592 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006593 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006594 if (s) goto err;
6595
6596 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006597 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006598 goto err;
6599 }
6600
6601 bc_parse_free(&prs);
6602 }
6603
6604 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006605 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006606 ip.func = fidx;
6607
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006608 bc_vec_pop(&G.prog.results);
6609 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006610
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006611 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006612
6613err:
6614 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006615 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006616 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006617exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006618 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006619 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006620}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006621#if ERRORS_ARE_FATAL
6622# define zbc_program_execStr(...) (zbc_program_execStr(__VA_ARGS__), BC_STATUS_SUCCESS)
6623#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006624#endif // ENABLE_DC
6625
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006626static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006627{
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 BcResult res;
6629 unsigned long val;
6630
6631 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6632 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006633 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006634 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006635 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006636 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006637 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006638
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006639 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006640 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006641 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006642}
6643
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006644static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006645{
Gavin Howard01055ba2018-11-03 11:00:21 -06006646 BcId entry, *entry_ptr;
6647 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006648 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006649
6650 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006651 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006652
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006653 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6654 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006655
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006656 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006657 *idx = entry_ptr->idx;
6658
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006659 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006660
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006661 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006662
6663 // We need to reset these, so the function can be repopulated.
6664 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006665 bc_vec_pop_all(&func->autos);
6666 bc_vec_pop_all(&func->code);
6667 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006668 }
6669 else {
6670 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006671 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 }
6673}
6674
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006675static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006676{
Gavin Howard01055ba2018-11-03 11:00:21 -06006677 BcResult r, *ptr;
6678 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006679 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006680 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006681 char *code = func->code.v;
6682 bool cond = false;
6683
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006684 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006685 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006686 char inst = code[(ip->idx)++];
6687
6688 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006689#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006690 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006691 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006692 if (s) RETURN_STATUS(s);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006693 cond = !bc_num_cmp(num, &G.prog.zero);
6694 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006695 // Fallthrough.
6696 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006697 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006698 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006699 addr = bc_vec_item(&func->labels, idx);
6700 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6701 break;
6702 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006703 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006704 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006705 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006706 case BC_INST_INC_PRE:
6707 case BC_INST_DEC_PRE:
6708 case BC_INST_INC_POST:
6709 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006710 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006711 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006712 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006713 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006714 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006715 case BC_INST_RET:
6716 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006717 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006718 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006719 case BC_INST_BOOL_OR:
6720 case BC_INST_BOOL_AND:
6721#endif // ENABLE_BC
6722 case BC_INST_REL_EQ:
6723 case BC_INST_REL_LE:
6724 case BC_INST_REL_GE:
6725 case BC_INST_REL_NE:
6726 case BC_INST_REL_LT:
6727 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006728 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006729 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 case BC_INST_READ:
Denys Vlasenko26819db2018-12-12 16:08:46 +01006731 s = zbc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006732 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006733 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006734 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006735 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006736 case BC_INST_ARRAY_ELEM:
6737 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006738 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006739 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006740 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006741 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006742 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006743 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006744 case BC_INST_IBASE:
6745 case BC_INST_SCALE:
6746 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006747 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006748 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 case BC_INST_SCALE_FUNC:
6750 case BC_INST_LENGTH:
6751 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006752 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006753 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006754 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006755 r.t = BC_RESULT_CONSTANT;
6756 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006757 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006758 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006759 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006760 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006761 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006763 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006764 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006765 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006766 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006768 case BC_INST_PRINT:
6769 case BC_INST_PRINT_POP:
6770 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006771 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006772 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006774 r.t = BC_RESULT_STR;
6775 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006776 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006777 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006778 case BC_INST_POWER:
6779 case BC_INST_MULTIPLY:
6780 case BC_INST_DIVIDE:
6781 case BC_INST_MODULUS:
6782 case BC_INST_PLUS:
6783 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006784 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006785 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006786 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006787 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006788 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006789 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006790 if (!bc_num_cmp(num, &G.prog.zero))
6791 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006792 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006793 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006794 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006795 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006796 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006797 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006798#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006799 case BC_INST_ASSIGN_POWER:
6800 case BC_INST_ASSIGN_MULTIPLY:
6801 case BC_INST_ASSIGN_DIVIDE:
6802 case BC_INST_ASSIGN_MODULUS:
6803 case BC_INST_ASSIGN_PLUS:
6804 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006805#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006806 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006807 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006808 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006809#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006810 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006811 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006812 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006814 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006815 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006816 case BC_INST_EXECUTE:
6817 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006818 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006819 s = zbc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006820 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006821 case BC_INST_PRINT_STACK: {
6822 size_t idx;
6823 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006824 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006825 if (s) break;
6826 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006827 break;
6828 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006829 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006830 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006831 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006833 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006834 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006835 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006836 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006837 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006838 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006839 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006840 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006841 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006842 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006843 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006844 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006845 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006846 ptr = bc_vec_item_rev(&G.prog.results, 0);
6847 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 memcpy(&r, ptr, sizeof(BcResult));
6849 memcpy(ptr, ptr2, sizeof(BcResult));
6850 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006851 break;
6852 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006854 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006855 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006857 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006859 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006860 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006861 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006862 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 break;
6864 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006865 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006866 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006867 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006868 free(name);
6869 break;
6870 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006871 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006872 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006873 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006874 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006875 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006876 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006877 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006878 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006879#endif // ENABLE_DC
6880 }
6881
Denys Vlasenkod38af482018-12-04 19:11:02 +01006882 if (s || G_interrupt) {
6883 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006884 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006885 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006886
6887 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006888 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006889 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006890 code = func->code.v;
6891 }
6892
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006893 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006894}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006895#if ERRORS_ARE_FATAL
6896# define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
6897#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006898
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006899#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006900static void bc_vm_info(void)
6901{
6902 printf("%s "BB_VER"\n"
6903 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01006904 , applet_name);
6905}
6906
6907static void bc_args(char **argv)
6908{
6909 unsigned opts;
6910 int i;
6911
6912 GETOPT_RESET();
6913#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006914 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01006915 "warn\0" No_argument "w"
6916 "version\0" No_argument "v"
6917 "standard\0" No_argument "s"
6918 "quiet\0" No_argument "q"
6919 "mathlib\0" No_argument "l"
6920 "interactive\0" No_argument "i"
6921 );
6922#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006923 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01006924#endif
6925 if (getenv("POSIXLY_CORRECT"))
6926 option_mask32 |= BC_FLAG_S;
6927
Denys Vlasenko54214c32018-12-06 09:07:06 +01006928 if (opts & BC_FLAG_V) {
6929 bc_vm_info();
6930 exit(0);
6931 }
6932
6933 for (i = optind; argv[i]; ++i)
6934 bc_vec_push(&G.files, argv + i);
6935}
6936
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006937static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006938{
Gavin Howard01055ba2018-11-03 11:00:21 -06006939 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006940 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01006941 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06006942
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006943 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06006944
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006945 G.env_args = xstrdup(env_args);
6946 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06006947
6948 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006949
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006950 while (*(buf = skip_whitespace(buf)) != '\0') {
6951 bc_vec_push(&v, &buf);
6952 buf = skip_non_whitespace(buf);
6953 if (!*buf)
6954 break;
6955 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06006956 }
6957
Denys Vlasenko54214c32018-12-06 09:07:06 +01006958 // NULL terminate, and pass argv[] so that first arg is argv[1]
6959 if (sizeof(int) == sizeof(char*)) {
6960 bc_vec_push(&v, &const_int_0);
6961 } else {
6962 static char *const nullptr = NULL;
6963 bc_vec_push(&v, &nullptr);
6964 }
6965 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006966
6967 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006968}
6969#endif // ENABLE_BC
6970
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006971static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006972{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006973 char *lenv;
6974 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006975
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006976 lenv = getenv(var);
6977 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006978 if (!lenv) return len;
6979
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006980 len = bb_strtou(lenv, NULL, 10) - 1;
6981 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006982 len = BC_NUM_PRINT_WIDTH;
6983
6984 return len;
6985}
6986
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006987static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006988{
Denys Vlasenko26819db2018-12-12 16:08:46 +01006989 BcStatus s = zbc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06006990
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006991 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006992
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006993 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01006994 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006995 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006996 }
6997
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006998 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006999 s = zbc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007000 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007001 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007002 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007003 }
7004
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007005 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007006}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007007#if ERRORS_ARE_FATAL
7008# define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__), BC_STATUS_SUCCESS)
7009#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007010
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007011static BC_STATUS zbc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007012{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007013 // So far bc/dc have no way to include a file from another file,
7014 // therefore we know G.prog.file == NULL on entry
7015 //const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007016 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007017 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007018 BcFunc *main_func;
7019 BcInstPtr *ip;
7020
Denys Vlasenkodf515392018-12-02 19:27:48 +01007021 data = bc_read_file(file);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007022 if (!data) RETURN_STATUS(bc_error_fmt("file '%s' is not text", file));
Gavin Howard01055ba2018-11-03 11:00:21 -06007023
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007024 //sv_file = G.prog.file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007025 G.prog.file = file;
7026 bc_lex_file(&G.prs.l);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007027 s = zbc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007028 if (s) goto err;
7029
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007030 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007031 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007032
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007033 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007034 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007035
7036err:
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007037 //G.prog.file = sv_file;
7038 G.prog.file = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06007039 free(data);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007040 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007041}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007042#if ERRORS_ARE_FATAL
7043# define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__), BC_STATUS_SUCCESS)
7044#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007045
Denys Vlasenko19f11072018-12-12 22:48:19 +01007046static BC_STATUS zbc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007047{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007048 BcStatus s;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007049 BcVec buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007050 size_t str;
7051 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06007052
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007053 //G.prog.file = NULL; - already is
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007054 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007055
Denys Vlasenko7d628012018-12-04 21:46:47 +01007056 bc_char_vec_init(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007057
7058 // This loop is complex because the vm tries not to send any lines that end
7059 // with a backslash to the parser. The reason for that is because the parser
7060 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7061 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko8a892472018-12-12 22:43:58 +01007062 s = BC_STATUS_SUCCESS;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007063 comment = false;
7064 str = 0;
Denys Vlasenko8a892472018-12-12 22:43:58 +01007065 for (;;) {
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007066 size_t prevlen = buffer.len;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007067 char *string;
Gavin Howard01055ba2018-11-03 11:00:21 -06007068
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007069 bc_read_line(&buffer);
7070 // No more input means EOF
7071 if (buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenko8a892472018-12-12 22:43:58 +01007072 break;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007073
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007074 string = buffer.v + prevlen;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007075 while (*string) {
7076 char c = *string;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007077 if (string == buffer.v || string[-1] != '\\') {
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007078 // checking applet type is cheaper than accessing sbgn/send
7079 if (IS_BC) // bc: sbgn = send = '"'
7080 str ^= (c == '"');
7081 else { // dc: sbgn = '[', send = ']'
7082 if (c == ']')
7083 str -= 1;
7084 else if (c == '[')
7085 str += 1;
Denys Vlasenko766f6722018-12-13 17:23:24 +01007086 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007087 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007088 string++;
7089 if (c == '/' && *string == '*') {
7090 comment = true;
7091 string++;
Gavin Howard01055ba2018-11-03 11:00:21 -06007092 continue;
7093 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007094 if (c == '*' && *string == '/') {
7095 comment = false;
7096 string++;
7097 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007098 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007099 if (str || comment) {
7100 buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007101 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007102 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007103
7104 // Check for backslash+newline.
7105 // we do not check that last char is '\n' -
7106 // if it is not, then it's EOF, and looping back
7107 // to bc_read_line() will detect it:
7108 string -= 2;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007109 if (string >= buffer.v && *string == '\\') {
7110 buffer.len--;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007111 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007112 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007113
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007114 s = zbc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007115 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007116 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007117 // Debug config, non-interactive mode:
7118 // return all the way back to main.
7119 // Non-debug builds do not come here, they exit.
7120 break;
7121 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007122 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007123
Denys Vlasenko7d628012018-12-04 21:46:47 +01007124 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007125 }
7126
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007127 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007128 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007129 }
7130 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007131 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007132 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007133
Gavin Howard01055ba2018-11-03 11:00:21 -06007134 bc_vec_free(&buffer);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007135 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007136}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007137#if ERRORS_ARE_FATAL
7138# define zbc_vm_stdin(...) (zbc_vm_stdin(__VA_ARGS__), BC_STATUS_SUCCESS)
7139#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007140
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007141#if ENABLE_BC
7142static const char bc_lib[] = {
7143 "scale=20"
7144"\n" "define e(x){"
7145"\n" "auto b,s,n,r,d,i,p,f,v"
7146"\n" "b=ibase"
7147"\n" "ibase=A"
7148"\n" "if(x<0){"
7149"\n" "n=1"
7150"\n" "x=-x"
7151"\n" "}"
7152"\n" "s=scale"
7153"\n" "r=6+s+0.44*x"
7154"\n" "scale=scale(x)+1"
7155"\n" "while(x>1){"
7156"\n" "d+=1"
7157"\n" "x/=2"
7158"\n" "scale+=1"
7159"\n" "}"
7160"\n" "scale=r"
7161"\n" "r=x+1"
7162"\n" "p=x"
7163"\n" "f=v=1"
7164"\n" "for(i=2;v!=0;++i){"
7165"\n" "p*=x"
7166"\n" "f*=i"
7167"\n" "v=p/f"
7168"\n" "r+=v"
7169"\n" "}"
7170"\n" "while((d--)!=0)r*=r"
7171"\n" "scale=s"
7172"\n" "ibase=b"
7173"\n" "if(n!=0)return(1/r)"
7174"\n" "return(r/1)"
7175"\n" "}"
7176"\n" "define l(x){"
7177"\n" "auto b,s,r,p,a,q,i,v"
7178"\n" "b=ibase"
7179"\n" "ibase=A"
7180"\n" "if(x<=0){"
7181"\n" "r=(1-10^scale)/1"
7182"\n" "ibase=b"
7183"\n" "return(r)"
7184"\n" "}"
7185"\n" "s=scale"
7186"\n" "scale+=6"
7187"\n" "p=2"
7188"\n" "while(x>=2){"
7189"\n" "p*=2"
7190"\n" "x=sqrt(x)"
7191"\n" "}"
7192"\n" "while(x<=0.5){"
7193"\n" "p*=2"
7194"\n" "x=sqrt(x)"
7195"\n" "}"
7196"\n" "r=a=(x-1)/(x+1)"
7197"\n" "q=a*a"
7198"\n" "v=1"
7199"\n" "for(i=3;v!=0;i+=2){"
7200"\n" "a*=q"
7201"\n" "v=a/i"
7202"\n" "r+=v"
7203"\n" "}"
7204"\n" "r*=p"
7205"\n" "scale=s"
7206"\n" "ibase=b"
7207"\n" "return(r/1)"
7208"\n" "}"
7209"\n" "define s(x){"
7210"\n" "auto b,s,r,n,a,q,i"
7211"\n" "b=ibase"
7212"\n" "ibase=A"
7213"\n" "s=scale"
7214"\n" "scale=1.1*s+2"
7215"\n" "a=a(1)"
7216"\n" "if(x<0){"
7217"\n" "n=1"
7218"\n" "x=-x"
7219"\n" "}"
7220"\n" "scale=0"
7221"\n" "q=(x/a+2)/4"
7222"\n" "x=x-4*q*a"
7223"\n" "if(q%2!=0)x=-x"
7224"\n" "scale=s+2"
7225"\n" "r=a=x"
7226"\n" "q=-x*x"
7227"\n" "for(i=3;a!=0;i+=2){"
7228"\n" "a*=q/(i*(i-1))"
7229"\n" "r+=a"
7230"\n" "}"
7231"\n" "scale=s"
7232"\n" "ibase=b"
7233"\n" "if(n!=0)return(-r/1)"
7234"\n" "return(r/1)"
7235"\n" "}"
7236"\n" "define c(x){"
7237"\n" "auto b,s"
7238"\n" "b=ibase"
7239"\n" "ibase=A"
7240"\n" "s=scale"
7241"\n" "scale*=1.2"
7242"\n" "x=s(2*a(1)+x)"
7243"\n" "scale=s"
7244"\n" "ibase=b"
7245"\n" "return(x/1)"
7246"\n" "}"
7247"\n" "define a(x){"
7248"\n" "auto b,s,r,n,a,m,t,f,i,u"
7249"\n" "b=ibase"
7250"\n" "ibase=A"
7251"\n" "n=1"
7252"\n" "if(x<0){"
7253"\n" "n=-1"
7254"\n" "x=-x"
7255"\n" "}"
7256"\n" "if(x==1){"
7257"\n" "if(scale<65){"
7258"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7259"\n" "}"
7260"\n" "}"
7261"\n" "if(x==.2){"
7262"\n" "if(scale<65){"
7263"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7264"\n" "}"
7265"\n" "}"
7266"\n" "s=scale"
7267"\n" "if(x>.2){"
7268"\n" "scale+=5"
7269"\n" "a=a(.2)"
7270"\n" "}"
7271"\n" "scale=s+3"
7272"\n" "while(x>.2){"
7273"\n" "m+=1"
7274"\n" "x=(x-.2)/(1+.2*x)"
7275"\n" "}"
7276"\n" "r=u=x"
7277"\n" "f=-x*x"
7278"\n" "t=1"
7279"\n" "for(i=3;t!=0;i+=2){"
7280"\n" "u*=f"
7281"\n" "t=u/i"
7282"\n" "r+=t"
7283"\n" "}"
7284"\n" "scale=s"
7285"\n" "ibase=b"
7286"\n" "return((m*a+r)/n)"
7287"\n" "}"
7288"\n" "define j(n,x){"
7289"\n" "auto b,s,o,a,i,v,f"
7290"\n" "b=ibase"
7291"\n" "ibase=A"
7292"\n" "s=scale"
7293"\n" "scale=0"
7294"\n" "n/=1"
7295"\n" "if(n<0){"
7296"\n" "n=-n"
7297"\n" "if(n%2==1)o=1"
7298"\n" "}"
7299"\n" "a=1"
7300"\n" "for(i=2;i<=n;++i)a*=i"
7301"\n" "scale=1.5*s"
7302"\n" "a=(x^n)/2^n/a"
7303"\n" "r=v=1"
7304"\n" "f=-x*x/4"
7305"\n" "scale=scale+length(a)-scale(a)"
7306"\n" "for(i=1;v!=0;++i){"
7307"\n" "v=v*f/i/(n+i)"
7308"\n" "r+=v"
7309"\n" "}"
7310"\n" "scale=s"
7311"\n" "ibase=b"
7312"\n" "if(o!=0)a=-a"
7313"\n" "return(a*r/1)"
7314"\n" "}"
7315};
7316#endif // ENABLE_BC
7317
Denys Vlasenko19f11072018-12-12 22:48:19 +01007318static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007319{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007320 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007321 size_t i;
7322
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007323#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007324 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007325
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007326 // We know that internal library is not buggy,
7327 // thus error checking is normally disabled.
7328# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007329 bc_lex_file(&G.prs.l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01007330 s = zbc_parse_text(&G.prs, bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007331 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007332
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007333 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01007334 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007335 if (DEBUG_LIB && s) RETURN_STATUS(s);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007336 }
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007337 s = zbc_program_exec();
Denys Vlasenko19f11072018-12-12 22:48:19 +01007338 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007339 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007341
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007342 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007343 for (i = 0; !s && i < G.files.len; ++i)
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007344 s = zbc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007345 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7346 // Debug config, non-interactive mode:
7347 // return all the way back to main.
7348 // Non-debug builds do not come here, they exit.
Denys Vlasenko19f11072018-12-12 22:48:19 +01007349 RETURN_STATUS(s);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007350 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007351
Denys Vlasenko91cde952018-12-10 20:56:08 +01007352 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko19f11072018-12-12 22:48:19 +01007353 s = zbc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007354
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007355 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007356 s = zbc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007357
Denys Vlasenko19f11072018-12-12 22:48:19 +01007358 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007359}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007360#if ERRORS_ARE_FATAL
7361# define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
7362#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007363
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007364#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007365static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007366{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007367 bc_num_free(&G.prog.ib);
7368 bc_num_free(&G.prog.ob);
7369 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007370# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007371 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007372# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007373 bc_vec_free(&G.prog.fns);
7374 bc_vec_free(&G.prog.fn_map);
7375 bc_vec_free(&G.prog.vars);
7376 bc_vec_free(&G.prog.var_map);
7377 bc_vec_free(&G.prog.arrs);
7378 bc_vec_free(&G.prog.arr_map);
7379 bc_vec_free(&G.prog.strs);
7380 bc_vec_free(&G.prog.consts);
7381 bc_vec_free(&G.prog.results);
7382 bc_vec_free(&G.prog.stack);
7383 bc_num_free(&G.prog.last);
7384 bc_num_free(&G.prog.zero);
7385 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007386}
7387
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007388static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007389{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007390 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007391 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007392 bc_parse_free(&G.prs);
7393 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007394}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007395#endif
7396
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007397static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007398{
7399 size_t idx;
7400 BcInstPtr ip;
7401
7402 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7403 memset(&ip, 0, sizeof(BcInstPtr));
7404
7405 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007406 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007407 bc_num_ten(&G.prog.ib);
7408 G.prog.ib_t = 10;
7409
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007410 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007411 bc_num_ten(&G.prog.ob);
7412 G.prog.ob_t = 10;
7413
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007414 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007415 bc_num_ten(&G.prog.hexb);
7416 G.prog.hexb.num[0] = 6;
7417
7418#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007419 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007420 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7421#endif
7422
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007423 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007424 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007425
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007426 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007427 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007428
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007429 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007430 bc_num_one(&G.prog.one);
7431
7432 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007433 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007434
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007435 bc_program_addFunc(xstrdup("(main)"), &idx);
7436 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007437
7438 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007439 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007440
7441 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007442 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007443
7444 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7445 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7446 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7447 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7448 bc_vec_push(&G.prog.stack, &ip);
7449}
Gavin Howard01055ba2018-11-03 11:00:21 -06007450
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007451static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007452{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007453#if ENABLE_FEATURE_EDITING
7454 G.line_input_state = new_line_input_t(DO_HISTORY);
7455#endif
7456 G.prog.len = bc_vm_envLen(env_len);
7457
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007458 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007459 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007460 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007461 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007462 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007463
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007464//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7465//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007466 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007467#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007468 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007469 // With SA_RESTART, most system calls will restart
7470 // (IOW: they won't fail with EINTR).
7471 // In particular, this means ^C won't cause
7472 // stdout to get into "error state" if SIGINT hits
7473 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007474 //
7475 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007476 // will only be handled after [Enter] since read()
7477 // from stdin is not interrupted by ^C either,
7478 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007479 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007480 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7481
7482 // Without SA_RESTART, this exhibits a bug:
7483 // "while (1) print 1" and try ^C-ing it.
7484 // Intermittently, instead of returning to input line,
7485 // you'll get "output error: Interrupted system call"
7486 // and exit.
7487 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007488#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007489 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007490 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007491 return 0; // "not a tty"
7492}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007493
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007494static BcStatus bc_vm_run(void)
7495{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007496 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007497#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007498 if (G_exiting) // it was actually "halt" or "quit"
7499 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007500 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007501# if ENABLE_FEATURE_EDITING
7502 free_line_input_t(G.line_input_state);
7503# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007504 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007505#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007506 return st;
7507}
7508
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007509#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007510int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007511int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007512{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007513 int is_tty;
7514
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007515 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007516 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007517
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007518 is_tty = bc_vm_init("BC_LINE_LENGTH");
7519
7520 bc_args(argv);
7521
7522 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7523 bc_vm_info();
7524
7525 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007526}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007527#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007528
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007529#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007530int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007531int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007532{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007533 int noscript;
7534
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007535 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007536 G.sbgn = '[';
7537 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007538 /*
7539 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7540 * 1 char wider than bc from the same package.
7541 * Both default width, and xC_LINE_LENGTH=N are wider:
7542 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7543 * 1234\
7544 * 56
7545 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7546 * 123\
7547 * 456
7548 * Do the same, or it's a bug?
7549 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007550 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007551
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007552 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7553 noscript = BC_FLAG_I;
7554 for (;;) {
7555 int n = getopt(argc, argv, "e:f:x");
7556 if (n <= 0)
7557 break;
7558 switch (n) {
7559 case 'e':
7560 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007561 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007562 if (n) return n;
7563 break;
7564 case 'f':
7565 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007566 n = zbc_vm_file(optarg);
7567 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007568 break;
7569 case 'x':
7570 option_mask32 |= DC_FLAG_X;
7571 break;
7572 default:
7573 bb_show_usage();
7574 }
7575 }
7576 argv += optind;
7577
7578 while (*argv) {
7579 noscript = 0;
7580 bc_vec_push(&G.files, argv++);
7581 }
7582
7583 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7584
7585 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007586}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007587#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007588
7589#endif // not DC_SMALL