blob: dd2d7f22c09da42ed86573f00465a1f4e1d6beaa [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
721 BcParse prs;
722 BcProgram prog;
723
Denys Vlasenko5318f812018-12-05 17:48:01 +0100724 // For error messages. Can be set to current parsed line,
725 // or [TODO] to current executing line (can be before last parsed one)
726 unsigned err_line;
727
Gavin Howard01055ba2018-11-03 11:00:21 -0600728 BcVec files;
729
730 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100731
732#if ENABLE_FEATURE_EDITING
733 line_input_t *line_input_state;
734#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100735} FIX_ALIASING;
736#define G (*ptr_to_globals)
737#define INIT_G() do { \
738 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
739} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100740#define FREE_G() do { \
741 FREE_PTR_TO_GLOBALS(); \
742} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100743#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
744#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100745#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100746#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100747# define G_interrupt bb_got_signal
748# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100749#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100750# define G_interrupt 0
751# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100752#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100753#if ENABLE_FEATURE_CLEAN_UP
754# define G_exiting G.exiting
755#else
756# define G_exiting 0
757#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100758#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100759#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100760
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100761#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600762
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100763// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600764// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100765enum {
766 BC_PARSE_EXPRS_BITS = 0
767 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
768 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
769 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
770 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
771 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
772 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
773 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
774 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600775};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100776static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
777{
778#if ULONG_MAX > 0xffffffff
779 // 64-bit version (will not work correctly for 32-bit longs!)
780 return BC_PARSE_EXPRS_BITS & (1UL << i);
781#else
782 // 32-bit version
783 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
784 if (i >= 32) {
785 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
786 i &= 31;
787 }
788 return m & (1UL << i);
789#endif
790}
Gavin Howard01055ba2018-11-03 11:00:21 -0600791
792// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100793static const uint8_t bc_parse_ops[] = {
794#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100795 OP(0, false), OP( 0, false ), // inc dec
796 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100797 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100798 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
799 OP(4, true ), OP( 4, true ), // mod + -
800 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
801 OP(1, false), // not
802 OP(7, true ), OP( 7, true ), // or and
803 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
804 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100805#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600806};
Denys Vlasenko65437582018-12-05 19:37:19 +0100807#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
808#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600809
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100810// Byte array of up to 4 BC_LEX's, packed into 32-bit word
811typedef uint32_t BcParseNext;
812
Gavin Howard01055ba2018-11-03 11:00:21 -0600813// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100814enum {
815#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
816#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
817#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
818 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
819 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
820 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
821 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
822 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
823 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
824 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
825#undef BC_PARSE_NEXT4
826#undef BC_PARSE_NEXT2
827#undef BC_PARSE_NEXT1
828};
Gavin Howard01055ba2018-11-03 11:00:21 -0600829#endif // ENABLE_BC
830
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100831#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100832static const //BcLexType - should be this type, but narrower type saves size:
833uint8_t
834dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600835 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
836 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
837 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
838 BC_LEX_STORE_PUSH,
839};
840
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100841static const //BcLexType - should be this type
842uint8_t
843dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600844 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
845 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
846 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
847 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
848 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
849 BC_LEX_INVALID, BC_LEX_INVALID,
850 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
851 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
852 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
853 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
854 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
855 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
856 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
857 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
858 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
859 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
860 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
861 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
862 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
863 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
864 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
865 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
866 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
867 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
868 BC_LEX_INVALID
869};
870
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100871static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
872int8_t
873dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600874 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
875 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
876 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
877 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
878 BC_INST_INVALID, BC_INST_INVALID,
879 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
880 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
881 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
882 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
883 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
884 BC_INST_INVALID, BC_INST_INVALID,
885 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
886 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
887 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
888 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
889 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
890 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
891 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
892 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
893 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
894 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
895 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
896 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
897};
898#endif // ENABLE_DC
899
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100900// In configurations where errors abort instead of propagating error
901// return code up the call chain, functions returning BC_STATUS
902// actually don't return anything, they always succeed and return "void".
903// A macro wrapper is provided, which makes this statement work:
904// s = zbc_func(...)
905// and makes it visible to the compiler that s is always zero,
906// allowing compiler to optimize dead code after the statement.
907//
908// To make code more readable, each such function has a "z"
909// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
910//
911#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100912# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100913# define ERRORFUNC /*nothing*/
914# define ERROR_RETURN(a) a
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100915//moved up: # define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100916# define RETURN_STATUS(v) return (v)
917#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100918# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100919# define ERRORFUNC NORETURN
920# define ERROR_RETURN(a) /*nothing*/
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100921//moved up: # define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100922# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
923#endif
924
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100925#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
926#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
927#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
928//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
929static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
930{
931 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
932}
933//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
934static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
935{
936 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
937}
938
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100939typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
940
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100941typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100942
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100943static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
944 BcNumBinaryOp op, size_t req);
945static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
946static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
947static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
948static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
949static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
950static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
951
952static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
953{
954 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
955 (void) scale;
956 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
957}
958
959static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
960{
961 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
962 (void) scale;
963 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
964}
965
966static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
967{
968 size_t req = BC_NUM_MREQ(a, b, scale);
969 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
970}
971
972static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
973{
974 size_t req = BC_NUM_MREQ(a, b, scale);
975 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
976}
977
978static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
979{
980 size_t req = BC_NUM_MREQ(a, b, scale);
981 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
982}
983
984static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
985{
986 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
987}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100988
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100989static const BcNumBinaryOp zbc_program_ops[] = {
990 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 -0600991};
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100992#if ERRORS_ARE_FATAL
993# define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS)
994# define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS)
995# define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS)
996# define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS)
997# define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS)
998# define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS)
999#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001000
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001001static void fflush_and_check(void)
1002{
1003 fflush_all();
1004 if (ferror(stdout) || ferror(stderr))
1005 bb_perror_msg_and_die("output error");
1006}
1007
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001008#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001009#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001010do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001011 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001012 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001013 return BC_STATUS_FAILURE; \
1014} while (0)
1015#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001016#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001017#endif
1018
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001019static void quit(void) NORETURN;
1020static void quit(void)
1021{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001022 if (ferror(stdin))
1023 bb_perror_msg_and_die("input error");
1024 fflush_and_check();
1025 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001026}
1027
Denys Vlasenko5318f812018-12-05 17:48:01 +01001028static void bc_verror_msg(const char *fmt, va_list p)
1029{
1030 const char *sv = sv; /* for compiler */
1031 if (G.prog.file) {
1032 sv = applet_name;
1033 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
1034 }
1035 bb_verror_msg(fmt, p, NULL);
1036 if (G.prog.file) {
1037 free((char*)applet_name);
1038 applet_name = sv;
1039 }
1040}
1041
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001042static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001043{
1044 va_list p;
1045
1046 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001047 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001048 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001049
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001050 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001051 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001052 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001053}
1054
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001055#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001056static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001057{
1058 va_list p;
1059
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001060 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001061 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001062 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001063
1064 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001065 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001066 va_end(p);
1067
1068 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001069 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001070 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001071 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001072 exit(1);
1073 return BC_STATUS_FAILURE;
1074}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001075#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001076
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001077// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1078// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001079// function must not have caller-cleaned parameters on stack.
1080// Unfortunately, vararg function API does exactly that on most arches.
1081// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001082static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001083{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001084 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1085}
1086static ERRORFUNC int bc_error_bad_character(char c)
1087{
1088 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1089}
1090static ERRORFUNC int bc_error_bad_expression(void)
1091{
1092 ERROR_RETURN(return) bc_error("bad expression");
1093}
1094static ERRORFUNC int bc_error_bad_token(void)
1095{
1096 ERROR_RETURN(return) bc_error("bad token");
1097}
1098static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1099{
1100 ERROR_RETURN(return) bc_error("stack has too few elements");
1101}
1102static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1103{
1104 ERROR_RETURN(return) bc_error("variable is wrong type");
1105}
1106static ERRORFUNC int bc_error_nested_read_call(void)
1107{
1108 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001109}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001110#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001111static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001112{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001113 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001114}
Denys Vlasenko00646792018-12-05 18:12:27 +01001115static int bc_POSIX_does_not_allow(const char *msg)
1116{
1117 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1118}
1119static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1120{
1121 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1122}
1123static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1124{
1125 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1126}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001127#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001128
Gavin Howard01055ba2018-11-03 11:00:21 -06001129static void bc_vec_grow(BcVec *v, size_t n)
1130{
1131 size_t cap = v->cap * 2;
1132 while (cap < v->len + n) cap *= 2;
1133 v->v = xrealloc(v->v, v->size * cap);
1134 v->cap = cap;
1135}
1136
1137static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1138{
1139 v->size = esize;
1140 v->cap = BC_VEC_START_CAP;
1141 v->len = 0;
1142 v->dtor = dtor;
1143 v->v = xmalloc(esize * BC_VEC_START_CAP);
1144}
1145
Denys Vlasenko7d628012018-12-04 21:46:47 +01001146static void bc_char_vec_init(BcVec *v)
1147{
1148 bc_vec_init(v, sizeof(char), NULL);
1149}
1150
Gavin Howard01055ba2018-11-03 11:00:21 -06001151static void bc_vec_expand(BcVec *v, size_t req)
1152{
1153 if (v->cap < req) {
1154 v->v = xrealloc(v->v, v->size * req);
1155 v->cap = req;
1156 }
1157}
1158
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001159static void bc_vec_pop(BcVec *v)
1160{
1161 v->len--;
1162 if (v->dtor)
1163 v->dtor(v->v + (v->size * v->len));
1164}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001165
Gavin Howard01055ba2018-11-03 11:00:21 -06001166static void bc_vec_npop(BcVec *v, size_t n)
1167{
1168 if (!v->dtor)
1169 v->len -= n;
1170 else {
1171 size_t len = v->len - n;
1172 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1173 }
1174}
1175
Denys Vlasenko7d628012018-12-04 21:46:47 +01001176static void bc_vec_pop_all(BcVec *v)
1177{
1178 bc_vec_npop(v, v->len);
1179}
1180
Gavin Howard01055ba2018-11-03 11:00:21 -06001181static void bc_vec_push(BcVec *v, const void *data)
1182{
1183 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1184 memmove(v->v + (v->size * v->len), data, v->size);
1185 v->len += 1;
1186}
1187
1188static void bc_vec_pushByte(BcVec *v, char data)
1189{
1190 bc_vec_push(v, &data);
1191}
1192
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001193static void bc_vec_pushZeroByte(BcVec *v)
1194{
1195 //bc_vec_pushByte(v, '\0');
1196 // better:
1197 bc_vec_push(v, &const_int_0);
1198}
1199
Gavin Howard01055ba2018-11-03 11:00:21 -06001200static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1201{
1202 if (idx == v->len)
1203 bc_vec_push(v, data);
1204 else {
1205
1206 char *ptr;
1207
1208 if (v->len == v->cap) bc_vec_grow(v, 1);
1209
1210 ptr = v->v + v->size * idx;
1211
1212 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1213 memmove(ptr, data, v->size);
1214 }
1215}
1216
1217static void bc_vec_string(BcVec *v, size_t len, const char *str)
1218{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001219 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001220 bc_vec_expand(v, len + 1);
1221 memcpy(v->v, str, len);
1222 v->len = len;
1223
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001224 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001225}
1226
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001227#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001228static void bc_vec_concat(BcVec *v, const char *str)
1229{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001230 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001231
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001232 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001233
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001234 slen = strlen(str);
1235 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001236
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001237 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001238 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001239
1240 v->len = len;
1241}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001242#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001243
1244static void *bc_vec_item(const BcVec *v, size_t idx)
1245{
1246 return v->v + v->size * idx;
1247}
1248
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001249static char** bc_program_str(size_t idx)
1250{
1251 return bc_vec_item(&G.prog.strs, idx);
1252}
1253
1254static BcFunc* bc_program_func(size_t idx)
1255{
1256 return bc_vec_item(&G.prog.fns, idx);
1257}
1258
Gavin Howard01055ba2018-11-03 11:00:21 -06001259static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1260{
1261 return v->v + v->size * (v->len - idx - 1);
1262}
1263
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001264static void *bc_vec_top(const BcVec *v)
1265{
1266 return v->v + v->size * (v->len - 1);
1267}
1268
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001269static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001270{
1271 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001272 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001273 free(v->v);
1274}
1275
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001276static int bc_id_cmp(const void *e1, const void *e2)
1277{
1278 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1279}
1280
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001281static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001282{
1283 free(((BcId *) id)->name);
1284}
1285
Gavin Howard01055ba2018-11-03 11:00:21 -06001286static size_t bc_map_find(const BcVec *v, const void *ptr)
1287{
1288 size_t low = 0, high = v->len;
1289
1290 while (low < high) {
1291
1292 size_t mid = (low + high) / 2;
1293 BcId *id = bc_vec_item(v, mid);
1294 int result = bc_id_cmp(ptr, id);
1295
1296 if (result == 0)
1297 return mid;
1298 else if (result < 0)
1299 high = mid;
1300 else
1301 low = mid + 1;
1302 }
1303
1304 return low;
1305}
1306
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001307static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001308{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001309 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001310
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001311 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001312 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001313 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1314 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001315 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001316 bc_vec_pushAt(v, ptr, n);
1317 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001318}
1319
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001320#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001321static size_t bc_map_index(const BcVec *v, const void *ptr)
1322{
1323 size_t i = bc_map_find(v, ptr);
1324 if (i >= v->len) return BC_VEC_INVALID_IDX;
1325 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1326}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001327#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001328
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001329static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001330{
1331 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1332 || c > 0x7e
1333 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001334 bc_error_fmt("illegal character 0x%02x", c);
1335 return 1;
1336 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001337 return 0;
1338}
1339
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001340// Note: it _appends_ data from the stdin to vec.
Denys Vlasenko8a892472018-12-12 22:43:58 +01001341static void bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001342{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001343 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001344 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001345
Gavin Howard01055ba2018-11-03 11:00:21 -06001346#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001347 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001348 intr:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001349 G_interrupt = 0;
1350 // GNU bc says "interrupted execution."
1351 // GNU dc says "Interrupt!"
1352 fputs("\ninterrupted execution\n", stderr);
1353 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001354
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001355# if ENABLE_FEATURE_EDITING
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001356 if (G_ttyin) {
1357 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001358# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001359 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1360 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1361 if (n == 0) // ^C
1362 goto intr;
Denys Vlasenkoe755e302018-12-13 22:25:28 +01001363 bc_vec_pushZeroByte(vec);
1364 return;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001365 }
1366 i = 0;
1367 for (;;) {
1368 char c = line_buf[i++];
1369 if (!c) break;
1370 if (bad_input_byte(c)) goto again;
1371 }
1372 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001373# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001374 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001375# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001376#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001377 {
1378 int c;
1379 bool bad_chars = 0;
1380 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001381
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001382 IF_FEATURE_BC_SIGNALS(errno = 0;)
1383 do {
1384 c = fgetc(stdin);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001385#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001386 // Both conditions appear simultaneously, check both just in case
1387 if (errno == EINTR || G_interrupt) {
1388 // ^C was pressed
1389 clearerr(stdin);
1390 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001391 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001392#endif
1393 if (c == EOF) {
1394 if (ferror(stdin))
1395 quit(); // this emits error message
1396 // Note: EOF does not append '\n', therefore:
1397 // printf 'print 123\n' | bc - works
1398 // printf 'print 123' | bc - fails (syntax error)
1399 break;
1400 }
1401 bad_chars |= bad_input_byte(c);
1402 bc_vec_pushByte(vec, (char)c);
1403 } while (c != '\n');
1404 if (bad_chars) {
1405 // Bad chars on this line, ignore entire line
1406 vec->len = len;
1407 goto again;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001408 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001409 bc_vec_pushZeroByte(vec);
1410 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001411}
1412
Denys Vlasenkodf515392018-12-02 19:27:48 +01001413static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001414{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001415 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001416 size_t size = ((size_t) -1);
1417 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001418
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001419 // Never returns NULL (dies on errors)
1420 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001421
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001422 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001423 char c = buf[i];
1424 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1425 || c > 0x7e
1426 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001427 free(buf);
1428 buf = NULL;
1429 break;
1430 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001431 }
1432
Denys Vlasenkodf515392018-12-02 19:27:48 +01001433 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001434}
1435
Gavin Howard01055ba2018-11-03 11:00:21 -06001436static void bc_num_setToZero(BcNum *n, size_t scale)
1437{
1438 n->len = 0;
1439 n->neg = false;
1440 n->rdx = scale;
1441}
1442
1443static void bc_num_zero(BcNum *n)
1444{
1445 bc_num_setToZero(n, 0);
1446}
1447
1448static void bc_num_one(BcNum *n)
1449{
1450 bc_num_setToZero(n, 0);
1451 n->len = 1;
1452 n->num[0] = 1;
1453}
1454
1455static void bc_num_ten(BcNum *n)
1456{
1457 bc_num_setToZero(n, 0);
1458 n->len = 2;
1459 n->num[0] = 0;
1460 n->num[1] = 1;
1461}
1462
Denys Vlasenko3129f702018-12-09 12:04:44 +01001463// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001464static void bc_num_init(BcNum *n, size_t req)
1465{
1466 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001467 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001468 n->num = xmalloc(req);
1469 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001470 n->rdx = 0;
1471 n->len = 0;
1472 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001473}
1474
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001475static void bc_num_init_DEF_SIZE(BcNum *n)
1476{
1477 bc_num_init(n, BC_NUM_DEF_SIZE);
1478}
1479
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001480static void bc_num_expand(BcNum *n, size_t req)
1481{
1482 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1483 if (req > n->cap) {
1484 n->num = xrealloc(n->num, req);
1485 n->cap = req;
1486 }
1487}
1488
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001489static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001490{
1491 free(((BcNum *) num)->num);
1492}
1493
1494static void bc_num_copy(BcNum *d, BcNum *s)
1495{
1496 if (d != s) {
1497 bc_num_expand(d, s->cap);
1498 d->len = s->len;
1499 d->neg = s->neg;
1500 d->rdx = s->rdx;
1501 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1502 }
1503}
1504
Denys Vlasenko29301232018-12-11 15:29:32 +01001505static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001506{
1507 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001508 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001509
Denys Vlasenko29301232018-12-11 15:29:32 +01001510 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001511
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001512 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001513
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001514 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001515
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001516 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001517 pow *= 10;
1518
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001519 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001520 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001521 prev = result;
1522 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001523 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001524 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001525
Denys Vlasenko29301232018-12-11 15:29:32 +01001526 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001527}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001528#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001529# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001530#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001531
1532static void bc_num_ulong2num(BcNum *n, unsigned long val)
1533{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001534 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001535
1536 bc_num_zero(n);
1537
1538 if (val == 0) return;
1539
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001540 if (ULONG_MAX == 0xffffffffUL)
1541 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001542 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001543 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001544 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001545
1546 ptr = n->num;
1547 for (;;) {
1548 n->len++;
1549 *ptr++ = val % 10;
1550 val /= 10;
1551 if (val == 0) break;
1552 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001553}
1554
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001555static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001556 size_t len)
1557{
1558 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001559 for (i = 0; i < len; ++i) {
1560 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001561 a[i + j++] += 10;
1562 a[i + j] -= 1;
1563 }
1564 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001565}
1566
1567static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1568{
1569 size_t i;
1570 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001571 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001572 return BC_NUM_NEG(i + 1, c < 0);
1573}
1574
1575static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1576{
1577 size_t i, min, a_int, b_int, diff;
1578 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001579 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001580 ssize_t cmp;
1581
1582 if (a == b) return 0;
1583 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1584 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001585
1586 if (a->neg != b->neg) // signs of a and b differ
1587 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1588 return (int)b->neg - (int)a->neg;
1589 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001590
1591 a_int = BC_NUM_INT(a);
1592 b_int = BC_NUM_INT(b);
1593 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001594
1595 if (a_int != 0) return (ssize_t) a_int;
1596
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001597 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001598 if (a_max) {
1599 min = b->rdx;
1600 diff = a->rdx - b->rdx;
1601 max_num = a->num + diff;
1602 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001603 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1604 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001605 min = a->rdx;
1606 diff = b->rdx - a->rdx;
1607 max_num = b->num + diff;
1608 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001609 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001610 }
1611
1612 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001613 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001614
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001615 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001616 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001617 }
1618
1619 return 0;
1620}
1621
1622static void bc_num_truncate(BcNum *n, size_t places)
1623{
1624 if (places == 0) return;
1625
1626 n->rdx -= places;
1627
1628 if (n->len != 0) {
1629 n->len -= places;
1630 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1631 }
1632}
1633
1634static void bc_num_extend(BcNum *n, size_t places)
1635{
1636 size_t len = n->len + places;
1637
1638 if (places != 0) {
1639
1640 if (n->cap < len) bc_num_expand(n, len);
1641
1642 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1643 memset(n->num, 0, sizeof(BcDig) * places);
1644
1645 n->len += places;
1646 n->rdx += places;
1647 }
1648}
1649
1650static void bc_num_clean(BcNum *n)
1651{
1652 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1653 if (n->len == 0)
1654 n->neg = false;
1655 else if (n->len < n->rdx)
1656 n->len = n->rdx;
1657}
1658
1659static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1660{
1661 if (n->rdx < scale)
1662 bc_num_extend(n, scale - n->rdx);
1663 else
1664 bc_num_truncate(n, n->rdx - scale);
1665
1666 bc_num_clean(n);
1667 if (n->len != 0) n->neg = !neg1 != !neg2;
1668}
1669
1670static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1671 BcNum *restrict b)
1672{
1673 if (idx < n->len) {
1674
1675 b->len = n->len - idx;
1676 a->len = idx;
1677 a->rdx = b->rdx = 0;
1678
1679 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1680 memcpy(a->num, n->num, idx * sizeof(BcDig));
1681 }
1682 else {
1683 bc_num_zero(b);
1684 bc_num_copy(a, n);
1685 }
1686
1687 bc_num_clean(a);
1688 bc_num_clean(b);
1689}
1690
Denys Vlasenko29301232018-12-11 15:29:32 +01001691static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001692{
Denys Vlasenko29301232018-12-11 15:29:32 +01001693 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001694
1695 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1696 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1697 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001698 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001699 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001700
1701 if (n->rdx >= places)
1702 n->rdx -= places;
1703 else {
1704 bc_num_extend(n, places - n->rdx);
1705 n->rdx = 0;
1706 }
1707
1708 bc_num_clean(n);
1709
Denys Vlasenko29301232018-12-11 15:29:32 +01001710 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001711}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001712#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001713# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001714#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001715
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001716static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001717{
1718 BcNum one;
1719 BcDig num[2];
1720
1721 one.cap = 2;
1722 one.num = num;
1723 bc_num_one(&one);
1724
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001725 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001726}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001727#if ERRORS_ARE_FATAL
1728# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1729#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001730
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001731static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001732{
1733 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1734 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1735 int carry, in;
1736
1737 // Because this function doesn't need to use scale (per the bc spec),
1738 // I am hijacking it to say whether it's doing an add or a subtract.
1739
1740 if (a->len == 0) {
1741 bc_num_copy(c, b);
1742 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001743 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001744 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001745 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001746 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001747 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001748 }
1749
1750 c->neg = a->neg;
1751 c->rdx = BC_MAX(a->rdx, b->rdx);
1752 min_rdx = BC_MIN(a->rdx, b->rdx);
1753 c->len = 0;
1754
1755 if (a->rdx > b->rdx) {
1756 diff = a->rdx - b->rdx;
1757 ptr = a->num;
1758 ptr_a = a->num + diff;
1759 ptr_b = b->num;
1760 }
1761 else {
1762 diff = b->rdx - a->rdx;
1763 ptr = b->num;
1764 ptr_a = a->num;
1765 ptr_b = b->num + diff;
1766 }
1767
1768 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1769
1770 ptr_c += diff;
1771 a_int = BC_NUM_INT(a);
1772 b_int = BC_NUM_INT(b);
1773
1774 if (a_int > b_int) {
1775 min_int = b_int;
1776 max = a_int;
1777 ptr = ptr_a;
1778 }
1779 else {
1780 min_int = a_int;
1781 max = b_int;
1782 ptr = ptr_b;
1783 }
1784
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001785 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001786 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1787 carry = in / 10;
1788 ptr_c[i] = (BcDig)(in % 10);
1789 }
1790
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001791 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001792 in = ((int) ptr[i]) + carry;
1793 carry = in / 10;
1794 ptr_c[i] = (BcDig)(in % 10);
1795 }
1796
1797 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1798
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001799 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001800}
1801
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001802static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001803{
Gavin Howard01055ba2018-11-03 11:00:21 -06001804 ssize_t cmp;
1805 BcNum *minuend, *subtrahend;
1806 size_t start;
1807 bool aneg, bneg, neg;
1808
1809 // Because this function doesn't need to use scale (per the bc spec),
1810 // I am hijacking it to say whether it's doing an add or a subtract.
1811
1812 if (a->len == 0) {
1813 bc_num_copy(c, b);
1814 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001815 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001817 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001818 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001819 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001820 }
1821
1822 aneg = a->neg;
1823 bneg = b->neg;
1824 a->neg = b->neg = false;
1825
1826 cmp = bc_num_cmp(a, b);
1827
1828 a->neg = aneg;
1829 b->neg = bneg;
1830
1831 if (cmp == 0) {
1832 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001833 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001835 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001836 neg = a->neg;
1837 minuend = a;
1838 subtrahend = b;
1839 }
1840 else {
1841 neg = b->neg;
1842 if (sub) neg = !neg;
1843 minuend = b;
1844 subtrahend = a;
1845 }
1846
1847 bc_num_copy(c, minuend);
1848 c->neg = neg;
1849
1850 if (c->rdx < subtrahend->rdx) {
1851 bc_num_extend(c, subtrahend->rdx - c->rdx);
1852 start = 0;
1853 }
1854 else
1855 start = c->rdx - subtrahend->rdx;
1856
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001857 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001858
1859 bc_num_clean(c);
1860
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001861 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001862}
1863
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001864static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001865 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001866#if ERRORS_ARE_FATAL
1867# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1868#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001869{
1870 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001871 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001872 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001873 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001874
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 if (a->len == 0 || b->len == 0) {
1876 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001877 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001879 aone = BC_NUM_ONE(a);
1880 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001882 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001883 }
1884
1885 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1886 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1887 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001888 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001889 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001890
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 bc_num_expand(c, a->len + b->len + 1);
1892
1893 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001894 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001895
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001896 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001897
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001898 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001899 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001900 unsigned in = c->num[i + j];
1901 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1902 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001903 carry = in / 10;
1904 c->num[i + j] = (BcDig)(in % 10);
1905 }
1906
1907 c->num[i + j] += (BcDig) carry;
1908 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001909
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001910#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001911 // a=2^1000000
1912 // a*a <- without check below, this will not be interruptible
1913 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001914#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001915 }
1916
1917 c->len = len;
1918
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001919 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001920 }
1921
1922 bc_num_init(&l1, max);
1923 bc_num_init(&h1, max);
1924 bc_num_init(&l2, max);
1925 bc_num_init(&h2, max);
1926 bc_num_init(&m1, max);
1927 bc_num_init(&m2, max);
1928 bc_num_init(&z0, max);
1929 bc_num_init(&z1, max);
1930 bc_num_init(&z2, max);
1931 bc_num_init(&temp, max + max);
1932
1933 bc_num_split(a, max2, &l1, &h1);
1934 bc_num_split(b, max2, &l2, &h2);
1935
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001936 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001937 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001938 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001939 if (s) goto err;
1940
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001941 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001942 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001943 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001944 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001945 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001946 if (s) goto err;
1947
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001948 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001950 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 if (s) goto err;
1952
Denys Vlasenko29301232018-12-11 15:29:32 +01001953 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001954 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001955 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001957 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001958 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001959 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001960
1961err:
1962 bc_num_free(&temp);
1963 bc_num_free(&z2);
1964 bc_num_free(&z1);
1965 bc_num_free(&z0);
1966 bc_num_free(&m2);
1967 bc_num_free(&m1);
1968 bc_num_free(&h2);
1969 bc_num_free(&l2);
1970 bc_num_free(&h1);
1971 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001972 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001973}
1974
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001975static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001976{
1977 BcStatus s;
1978 BcNum cpa, cpb;
1979 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1980
1981 scale = BC_MAX(scale, a->rdx);
1982 scale = BC_MAX(scale, b->rdx);
1983 scale = BC_MIN(a->rdx + b->rdx, scale);
1984 maxrdx = BC_MAX(maxrdx, scale);
1985
1986 bc_num_init(&cpa, a->len);
1987 bc_num_init(&cpb, b->len);
1988
1989 bc_num_copy(&cpa, a);
1990 bc_num_copy(&cpb, b);
1991 cpa.neg = cpb.neg = false;
1992
Denys Vlasenko29301232018-12-11 15:29:32 +01001993 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001994 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001995 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001996 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001997 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001998 if (s) goto err;
1999
2000 maxrdx += scale;
2001 bc_num_expand(c, c->len + maxrdx);
2002
2003 if (c->len < maxrdx) {
2004 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2005 c->len += maxrdx;
2006 }
2007
2008 c->rdx = maxrdx;
2009 bc_num_retireMul(c, scale, a->neg, b->neg);
2010
2011err:
2012 bc_num_free(&cpb);
2013 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002014 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002015}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002016#if ERRORS_ARE_FATAL
2017# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2018#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002019
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002020static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002021{
2022 BcStatus s = BC_STATUS_SUCCESS;
2023 BcDig *n, *p, q;
2024 size_t len, end, i;
2025 BcNum cp;
2026 bool zero = true;
2027
2028 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002029 RETURN_STATUS(bc_error("divide by zero"));
2030 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002031 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002032 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002033 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002034 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002035 bc_num_copy(c, a);
2036 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002037 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002038 }
2039
2040 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2041 bc_num_copy(&cp, a);
2042 len = b->len;
2043
2044 if (len > cp.len) {
2045 bc_num_expand(&cp, len + 2);
2046 bc_num_extend(&cp, len - cp.len);
2047 }
2048
2049 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2050 cp.rdx -= b->rdx;
2051 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2052
2053 if (b->rdx == b->len) {
2054 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2055 len -= i - 1;
2056 }
2057
2058 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2059
2060 // We want an extra zero in front to make things simpler.
2061 cp.num[cp.len++] = 0;
2062 end = cp.len - len;
2063
2064 bc_num_expand(c, cp.len);
2065
2066 bc_num_zero(c);
2067 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2068 c->rdx = cp.rdx;
2069 c->len = cp.len;
2070 p = b->num;
2071
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002072 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002073 n = cp.num + i;
2074 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002075 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002076 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002077#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002078 // a=2^100000
2079 // scale=40000
2080 // 1/a <- without check below, this will not be interruptible
2081 if (G_interrupt) {
2082 s = BC_STATUS_FAILURE;
2083 break;
2084 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002085#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002086 }
2087
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002088 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002089 bc_num_free(&cp);
2090
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002091 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002092}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093#if ERRORS_ARE_FATAL
2094# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2095#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002096
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002098 BcNum *restrict d, size_t scale, size_t ts)
2099{
2100 BcStatus s;
2101 BcNum temp;
2102 bool neg;
2103
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002104 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002105 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002106
2107 if (a->len == 0) {
2108 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002109 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002110 }
2111
2112 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002113 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002114 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002115
2116 if (scale != 0) scale = ts;
2117
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002118 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002119 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002120 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 if (s) goto err;
2122
2123 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2124
2125 neg = d->neg;
2126 bc_num_retireMul(d, ts, a->neg, b->neg);
2127 d->neg = neg;
2128
2129err:
2130 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002131 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002132}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002133#if ERRORS_ARE_FATAL
2134# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2135#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002136
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002137static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002138{
2139 BcStatus s;
2140 BcNum c1;
2141 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2142
2143 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002145 bc_num_free(&c1);
2146
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002147 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002148}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002149#if ERRORS_ARE_FATAL
2150# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2151#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002152
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002153static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002154{
2155 BcStatus s = BC_STATUS_SUCCESS;
2156 BcNum copy;
2157 unsigned long pow;
2158 size_t i, powrdx, resrdx;
2159 bool neg, zero;
2160
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002161 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002162
2163 if (b->len == 0) {
2164 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002165 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002167 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002168 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002169 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002170 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002171 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002172 if (!b->neg)
2173 bc_num_copy(c, a);
2174 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002175 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002176 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002177 }
2178
2179 neg = b->neg;
2180 b->neg = false;
2181
Denys Vlasenko29301232018-12-11 15:29:32 +01002182 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002183 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002184
2185 bc_num_init(&copy, a->len);
2186 bc_num_copy(&copy, a);
2187
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002188 if (!neg) {
2189 if (a->rdx > scale)
2190 scale = a->rdx;
2191 if (a->rdx * pow < scale)
2192 scale = a->rdx * pow;
2193 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002194
2195 b->neg = neg;
2196
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002197 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002198 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002199 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002200 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002201 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002202 //if (G_interrupt) {
2203 // s = BC_STATUS_FAILURE;
2204 // goto err;
2205 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002206 }
2207
Gavin Howard01055ba2018-11-03 11:00:21 -06002208 bc_num_copy(c, &copy);
2209
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002210 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002211
2212 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002213 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002214 if (s) goto err;
2215
2216 if (pow & 1) {
2217 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002218 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002219 if (s) goto err;
2220 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002221 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002222 //if (G_interrupt) {
2223 // s = BC_STATUS_FAILURE;
2224 // goto err;
2225 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002226 }
2227
2228 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002229 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002230 if (s) goto err;
2231 }
2232
Gavin Howard01055ba2018-11-03 11:00:21 -06002233 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2234
2235 // We can't use bc_num_clean() here.
2236 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2237 if (zero) bc_num_setToZero(c, scale);
2238
2239err:
2240 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002241 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002242}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002243#if ERRORS_ARE_FATAL
2244# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2245#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002246
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002247static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002248 BcNumBinaryOp op, size_t req)
2249{
2250 BcStatus s;
2251 BcNum num2, *ptr_a, *ptr_b;
2252 bool init = false;
2253
2254 if (c == a) {
2255 ptr_a = &num2;
2256 memcpy(ptr_a, c, sizeof(BcNum));
2257 init = true;
2258 }
2259 else
2260 ptr_a = a;
2261
2262 if (c == b) {
2263 ptr_b = &num2;
2264 if (c != a) {
2265 memcpy(ptr_b, c, sizeof(BcNum));
2266 init = true;
2267 }
2268 }
2269 else
2270 ptr_b = b;
2271
2272 if (init)
2273 bc_num_init(c, req);
2274 else
2275 bc_num_expand(c, req);
2276
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002277 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01002278 ERROR_RETURN(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002279
2280 if (init) bc_num_free(&num2);
2281
Denys Vlasenko29301232018-12-11 15:29:32 +01002282 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002283}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002284#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002285# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002286#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002287
Denys Vlasenko9311e012018-12-10 11:54:18 +01002288static bool bc_num_strValid(const char *val, size_t base)
2289{
2290 BcDig b;
2291 bool radix;
2292
2293 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2294 radix = false;
2295 for (;;) {
2296 BcDig c = *val++;
2297 if (c == '\0')
2298 break;
2299 if (c == '.') {
2300 if (radix) return false;
2301 radix = true;
2302 continue;
2303 }
2304 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2305 return false;
2306 }
2307 return true;
2308}
2309
2310// Note: n is already "bc_num_zero()"ed,
2311// leading zeroes in "val" are removed
2312static void bc_num_parseDecimal(BcNum *n, const char *val)
2313{
2314 size_t len, i;
2315 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002316
2317 len = strlen(val);
2318 if (len == 0)
2319 return;
2320
Denys Vlasenko9311e012018-12-10 11:54:18 +01002321 bc_num_expand(n, len);
2322
2323 ptr = strchr(val, '.');
2324
2325 n->rdx = 0;
2326 if (ptr != NULL)
2327 n->rdx = (size_t)((val + len) - (ptr + 1));
2328
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002329 for (i = 0; val[i]; ++i) {
2330 if (val[i] != '0' && val[i] != '.') {
2331 // Not entirely zero value - convert it, and exit
2332 i = len - 1;
2333 for (;;) {
2334 n->num[n->len] = val[i] - '0';
2335 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002336 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002337 if ((ssize_t)--i == (ssize_t)-1) break;
2338 if (val[i] == '.') goto skip_dot;
2339 }
2340 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002341 }
2342 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002343 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002344}
2345
2346// Note: n is already "bc_num_zero()"ed,
2347// leading zeroes in "val" are removed
2348static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2349{
2350 BcStatus s;
2351 BcNum temp, mult, result;
2352 BcDig c = '\0';
2353 unsigned long v;
2354 size_t i, digits;
2355
2356 for (i = 0; ; ++i) {
2357 if (val[i] == '\0')
2358 return;
2359 if (val[i] != '.' && val[i] != '0')
2360 break;
2361 }
2362
2363 bc_num_init_DEF_SIZE(&temp);
2364 bc_num_init_DEF_SIZE(&mult);
2365
2366 for (;;) {
2367 c = *val++;
2368 if (c == '\0') goto int_err;
2369 if (c == '.') break;
2370
2371 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2372
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002373 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002374 if (s) goto int_err;
2375 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002376 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002377 if (s) goto int_err;
2378 }
2379
2380 bc_num_init(&result, base->len);
2381 //bc_num_zero(&result); - already is
2382 bc_num_one(&mult);
2383
2384 digits = 0;
2385 for (;;) {
2386 c = *val++;
2387 if (c == '\0') break;
2388 digits++;
2389
2390 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2391
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002392 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002393 if (s) goto err;
2394 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002395 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002396 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002397 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002398 if (s) goto err;
2399 }
2400
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002401 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002402 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002403 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002404 if (s) goto err;
2405
2406 if (n->len != 0) {
2407 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2408 } else
2409 bc_num_zero(n);
2410
2411err:
2412 bc_num_free(&result);
2413int_err:
2414 bc_num_free(&mult);
2415 bc_num_free(&temp);
2416}
2417
Denys Vlasenko29301232018-12-11 15:29:32 +01002418static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002419 size_t base_t)
2420{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002421 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002422 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002423
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002424 bc_num_zero(n);
2425 while (*val == '0') val++;
2426
Gavin Howard01055ba2018-11-03 11:00:21 -06002427 if (base_t == 10)
2428 bc_num_parseDecimal(n, val);
2429 else
2430 bc_num_parseBase(n, val, base);
2431
Denys Vlasenko29301232018-12-11 15:29:32 +01002432 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002433}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002434#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002435# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002436#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002437
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002438static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002439{
2440 BcStatus s;
2441 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2442 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2443 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2444
2445 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2446 bc_num_expand(b, req);
2447
2448 if (a->len == 0) {
2449 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002450 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002451 }
2452 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002453 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002454 else if (BC_NUM_ONE(a)) {
2455 bc_num_one(b);
2456 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002457 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002458 }
2459
2460 scale = BC_MAX(scale, a->rdx) + 1;
2461 len = a->len + scale;
2462
2463 bc_num_init(&num1, len);
2464 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002465 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002466
2467 bc_num_one(&half);
2468 half.num[0] = 5;
2469 half.rdx = 1;
2470
2471 bc_num_init(&f, len);
2472 bc_num_init(&fprime, len);
2473
2474 x0 = &num1;
2475 x1 = &num2;
2476
2477 bc_num_one(x0);
2478 pow = BC_NUM_INT(a);
2479
2480 if (pow) {
2481
2482 if (pow & 1)
2483 x0->num[0] = 2;
2484 else
2485 x0->num[0] = 6;
2486
2487 pow -= 2 - (pow & 1);
2488
2489 bc_num_extend(x0, pow);
2490
2491 // Make sure to move the radix back.
2492 x0->rdx -= pow;
2493 }
2494
2495 x0->rdx = digs = digs1 = 0;
2496 resrdx = scale + 2;
2497 len = BC_NUM_INT(x0) + resrdx - 1;
2498
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002499 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002500
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002501 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002502 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002503 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002504 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002505 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002506 if (s) goto err;
2507
2508 cmp = bc_num_cmp(x1, x0);
2509 digs = x1->len - (unsigned long long) llabs(cmp);
2510
2511 if (cmp == cmp2 && digs == digs1)
2512 times += 1;
2513 else
2514 times = 0;
2515
2516 resrdx += times > 4;
2517
2518 cmp2 = cmp1;
2519 cmp1 = cmp;
2520 digs1 = digs;
2521
2522 temp = x0;
2523 x0 = x1;
2524 x1 = temp;
2525 }
2526
Gavin Howard01055ba2018-11-03 11:00:21 -06002527 bc_num_copy(b, x0);
2528 scale -= 1;
2529 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2530
2531err:
2532 bc_num_free(&fprime);
2533 bc_num_free(&f);
2534 bc_num_free(&half);
2535 bc_num_free(&num2);
2536 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002537 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002538}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002539#if ERRORS_ARE_FATAL
2540# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2541#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002542
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002543static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002544 size_t scale)
2545{
2546 BcStatus s;
2547 BcNum num2, *ptr_a;
2548 bool init = false;
2549 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2550
2551 if (c == a) {
2552 memcpy(&num2, c, sizeof(BcNum));
2553 ptr_a = &num2;
2554 bc_num_init(c, len);
2555 init = true;
2556 }
2557 else {
2558 ptr_a = a;
2559 bc_num_expand(c, len);
2560 }
2561
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002562 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002563
2564 if (init) bc_num_free(&num2);
2565
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002566 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002567}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002568#if ERRORS_ARE_FATAL
2569# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2570#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002571
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002572#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002573static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002574{
2575 BcStatus s;
2576 BcNum base, exp, two, temp;
2577
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002578 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002579 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002580 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002581 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002582 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002583 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002584
2585 bc_num_expand(d, c->len);
2586 bc_num_init(&base, c->len);
2587 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002588 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002589 bc_num_init(&temp, b->len);
2590
2591 bc_num_one(&two);
2592 two.num[0] = 2;
2593 bc_num_one(d);
2594
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002595 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002596 if (s) goto err;
2597 bc_num_copy(&exp, b);
2598
2599 while (exp.len != 0) {
2600
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002601 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002602 if (s) goto err;
2603
2604 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002605 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002606 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002607 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002608 if (s) goto err;
2609 }
2610
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002611 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002612 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002613 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002614 if (s) goto err;
2615 }
2616
2617err:
2618 bc_num_free(&temp);
2619 bc_num_free(&two);
2620 bc_num_free(&exp);
2621 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002622 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002623}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002624#if ERRORS_ARE_FATAL
2625# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2626#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002627#endif // ENABLE_DC
2628
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002629#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002630static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002631{
2632 BcId a;
2633 size_t i;
2634
2635 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002636 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002637 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002638 }
2639
2640 a.idx = var;
2641 a.name = name;
2642
2643 bc_vec_push(&f->autos, &a);
2644
Denys Vlasenko29301232018-12-11 15:29:32 +01002645 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002646}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002647#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002648# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002649#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002650#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002651
2652static void bc_func_init(BcFunc *f)
2653{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002654 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002655 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2656 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2657 f->nparams = 0;
2658}
2659
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002660static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002661{
2662 BcFunc *f = (BcFunc *) func;
2663 bc_vec_free(&f->code);
2664 bc_vec_free(&f->autos);
2665 bc_vec_free(&f->labels);
2666}
2667
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002668static void bc_array_expand(BcVec *a, size_t len);
2669
Gavin Howard01055ba2018-11-03 11:00:21 -06002670static void bc_array_init(BcVec *a, bool nums)
2671{
2672 if (nums)
2673 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2674 else
2675 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2676 bc_array_expand(a, 1);
2677}
2678
Gavin Howard01055ba2018-11-03 11:00:21 -06002679static void bc_array_expand(BcVec *a, size_t len)
2680{
2681 BcResultData data;
2682
2683 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2684 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002685 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002686 bc_vec_push(a, &data.n);
2687 }
2688 }
2689 else {
2690 while (len > a->len) {
2691 bc_array_init(&data.v, true);
2692 bc_vec_push(a, &data.v);
2693 }
2694 }
2695}
2696
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002697static void bc_array_copy(BcVec *d, const BcVec *s)
2698{
2699 size_t i;
2700
2701 bc_vec_pop_all(d);
2702 bc_vec_expand(d, s->cap);
2703 d->len = s->len;
2704
2705 for (i = 0; i < s->len; ++i) {
2706 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2707 bc_num_init(dnum, snum->len);
2708 bc_num_copy(dnum, snum);
2709 }
2710}
2711
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002712static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002713{
2714 free(*((char **) string));
2715}
2716
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002717#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002718static void bc_result_copy(BcResult *d, BcResult *src)
2719{
2720 d->t = src->t;
2721
2722 switch (d->t) {
2723
2724 case BC_RESULT_TEMP:
2725 case BC_RESULT_IBASE:
2726 case BC_RESULT_SCALE:
2727 case BC_RESULT_OBASE:
2728 {
2729 bc_num_init(&d->d.n, src->d.n.len);
2730 bc_num_copy(&d->d.n, &src->d.n);
2731 break;
2732 }
2733
2734 case BC_RESULT_VAR:
2735 case BC_RESULT_ARRAY:
2736 case BC_RESULT_ARRAY_ELEM:
2737 {
2738 d->d.id.name = xstrdup(src->d.id.name);
2739 break;
2740 }
2741
2742 case BC_RESULT_CONSTANT:
2743 case BC_RESULT_LAST:
2744 case BC_RESULT_ONE:
2745 case BC_RESULT_STR:
2746 {
2747 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2748 break;
2749 }
2750 }
2751}
2752#endif // ENABLE_DC
2753
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002754static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002755{
2756 BcResult *r = (BcResult *) result;
2757
2758 switch (r->t) {
2759
2760 case BC_RESULT_TEMP:
2761 case BC_RESULT_IBASE:
2762 case BC_RESULT_SCALE:
2763 case BC_RESULT_OBASE:
2764 {
2765 bc_num_free(&r->d.n);
2766 break;
2767 }
2768
2769 case BC_RESULT_VAR:
2770 case BC_RESULT_ARRAY:
2771 case BC_RESULT_ARRAY_ELEM:
2772 {
2773 free(r->d.id.name);
2774 break;
2775 }
2776
2777 default:
2778 {
2779 // Do nothing.
2780 break;
2781 }
2782 }
2783}
2784
2785static void bc_lex_lineComment(BcLex *l)
2786{
2787 l->t.t = BC_LEX_WHITESPACE;
2788 while (l->i < l->len && l->buf[l->i++] != '\n');
2789 --l->i;
2790}
2791
2792static void bc_lex_whitespace(BcLex *l)
2793{
Gavin Howard01055ba2018-11-03 11:00:21 -06002794 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002795 for (;;) {
2796 char c = l->buf[l->i];
2797 if (c == '\n' || !isspace(c))
2798 break;
2799 l->i++;
2800 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002801}
2802
Denys Vlasenko29301232018-12-11 15:29:32 +01002803static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002804{
2805 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002806 size_t len, bslashes, i, ccnt;
2807 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002808
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002809 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002810 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002811 bslashes = 0;
2812 ccnt = i = 0;
2813 for (;;) {
2814 char c = buf[i];
2815 if (c == '\0')
2816 break;
2817 if (c == '\\' && buf[i + 1] == '\n') {
2818 i += 2;
2819 bslashes++;
2820 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002821 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002822 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2823 if (c != '.') break;
2824 // if '.' was already seen, stop on second one:
2825 if (pt) break;
2826 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002827 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002828 // buf[i] is one of "0-9A-F."
2829 i++;
2830 if (c != '.')
2831 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002832 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002833 //i is buf[i] index of the first not-yet-parsed char
2834 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002835
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002836 //ccnt is the number of chars in the number string, excluding possible
2837 //trailing "." and possible following trailing "\<newline>"(s).
2838 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2839
Denys Vlasenko64074a12018-12-07 15:50:14 +01002840 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2841 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2842 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002843 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002844 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002845
Denys Vlasenko7d628012018-12-04 21:46:47 +01002846 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002847 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002848 bc_vec_push(&l->t.v, &start);
2849
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002850 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002851 // If we have hit a backslash, skip it. We don't have
2852 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002853 if (*buf == '\\') {
2854 buf += 2;
2855 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002856 continue;
2857 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002858 bc_vec_push(&l->t.v, buf);
2859 buf++;
2860 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002861 }
2862
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002863 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002864
Denys Vlasenko29301232018-12-11 15:29:32 +01002865 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002866}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002867#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002868# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002869#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002870
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002871static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002872{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002873 size_t i;
2874 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002875
2876 l->t.t = BC_LEX_NAME;
2877
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002878 i = 0;
2879 buf = l->buf + l->i - 1;
2880 for (;;) {
2881 char c = buf[i];
2882 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2883 i++;
2884 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002885
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002886#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002887 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2888 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2889 if (i > BC_MAX_STRING)
2890 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2891 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002892#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002893 bc_vec_string(&l->t.v, i, buf);
2894
2895 // Increment the index. We minus 1 because it has already been incremented.
2896 l->i += i - 1;
2897
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002898 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002899}
2900
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002901static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002902{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002903 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002904}
2905
2906static void bc_lex_free(BcLex *l)
2907{
2908 bc_vec_free(&l->t.v);
2909}
2910
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002911static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002912{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002913 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002914 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002915}
2916
Denys Vlasenkoe755e302018-12-13 22:25:28 +01002917IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2918IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002919
2920static BC_STATUS zcommon_lex_token(BcLex *l)
2921{
2922 if (IS_BC) {
2923 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2924 }
2925 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2926}
2927
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002928static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002929{
2930 BcStatus s;
2931
2932 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002933 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002934
2935 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002936 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002937 l->t.t = BC_LEX_EOF;
2938
2939 l->newline = (l->i == l->len);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002940 if (l->newline) RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002941
2942 // Loop until failure or we don't have whitespace. This
2943 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002944 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002945 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002946//TODO: replace pointer with if(IS_BC)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002947 ERROR_RETURN(s =) zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002948 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2949
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002950 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002951}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002952#if ERRORS_ARE_FATAL
2953# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS)
2954#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002955
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002956static BC_STATUS zbc_lex_text(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002957{
2958 l->buf = text;
2959 l->i = 0;
2960 l->len = strlen(text);
2961 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002962 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002963}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002964#if ERRORS_ARE_FATAL
2965# define zbc_lex_text(...) (zbc_lex_text(__VA_ARGS__), BC_STATUS_SUCCESS)
2966#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002967
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002968#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002969static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002970{
2971 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002972 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002973 const char *buf = l->buf + l->i - 1;
2974
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002975 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2976 const char *keyword8 = bc_lex_kws[i].name8;
2977 unsigned j = 0;
2978 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2979 j++;
2980 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002981 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002982 if (keyword8[j] != '\0')
2983 continue;
2984 match:
2985 // buf starts with keyword bc_lex_kws[i]
2986 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002987 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002988 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002989 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002990 }
2991
2992 // We minus 1 because the index has already been incremented.
2993 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002994 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002995 }
2996
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002997 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002998
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002999 if (l->t.v.len > 2) {
3000 // Prevent this:
3001 // >>> qwe=1
3002 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3003 // '
3004 unsigned len = strchrnul(buf, '\n') - buf;
3005 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3006 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003007
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003008 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003009}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003010#if ERRORS_ARE_FATAL
3011# define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__), BC_STATUS_SUCCESS)
3012#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003013
Denys Vlasenko26819db2018-12-12 16:08:46 +01003014static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003015{
3016 size_t len, nls = 0, i = l->i;
3017 char c;
3018
3019 l->t.t = BC_LEX_STR;
3020
Denys Vlasenko26819db2018-12-12 16:08:46 +01003021 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i])
3022 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003023
3024 if (c == '\0') {
3025 l->i = i;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003026 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003027 }
3028
3029 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003030 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3031 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3032 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003033 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003034 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003035 bc_vec_string(&l->t.v, len, l->buf + l->i);
3036
3037 l->i = i + 1;
3038 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003039 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003040
Denys Vlasenko26819db2018-12-12 16:08:46 +01003041 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003042}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003043#if ERRORS_ARE_FATAL
3044# define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3045#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003046
3047static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3048{
3049 if (l->buf[l->i] == '=') {
3050 ++l->i;
3051 l->t.t = with;
3052 }
3053 else
3054 l->t.t = without;
3055}
3056
Denys Vlasenko29301232018-12-11 15:29:32 +01003057static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003058{
3059 size_t i, nls = 0;
3060 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003061
3062 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003063 i = ++l->i;
3064 for (;;) {
3065 char c = buf[i];
3066 check_star:
3067 if (c == '*') {
3068 c = buf[++i];
3069 if (c == '/')
3070 break;
3071 goto check_star;
3072 }
3073 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003074 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003075 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003076 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003077 nls += (c == '\n');
3078 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003079 }
3080
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003081 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003082 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003083 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003084
Denys Vlasenko29301232018-12-11 15:29:32 +01003085 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003086}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003087#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003088# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003089#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003091static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003092{
3093 BcStatus s = BC_STATUS_SUCCESS;
3094 char c = l->buf[l->i++], c2;
3095
3096 // This is the workhorse of the lexer.
3097 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003098 case '\0':
3099 case '\n':
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 l->newline = true;
3101 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3102 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003103 case '\t':
3104 case '\v':
3105 case '\f':
3106 case '\r':
3107 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 bc_lex_whitespace(l);
3109 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003110 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003111 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003112 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003113 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003114 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003115 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003118 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003119 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003121 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003122 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003123 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003126 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3127 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003129 c2 = l->buf[l->i];
3130 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003131 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003132 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 ++l->i;
3134 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003135 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003137 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003138 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 case '(':
3141 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3143 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3146 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003147 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 c2 = l->buf[l->i];
3149 if (c2 == '+') {
3150 ++l->i;
3151 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003152 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3154 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 l->t.t = BC_LEX_COMMA;
3157 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003158 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003159 c2 = l->buf[l->i];
3160 if (c2 == '-') {
3161 ++l->i;
3162 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003163 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3165 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003168 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 else {
3170 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003171 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 }
3173 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 c2 = l->buf[l->i];
3176 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003177 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003178 else
3179 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3180 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003181 case '0':
3182 case '1':
3183 case '2':
3184 case '3':
3185 case '4':
3186 case '5':
3187 case '6':
3188 case '7':
3189 case '8':
3190 case '9':
3191 case 'A':
3192 case 'B':
3193 case 'C':
3194 case 'D':
3195 case 'E':
3196 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003197 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 l->t.t = BC_LEX_SCOLON;
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_LE, BC_LEX_OP_REL_LT);
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_EQ, BC_LEX_OP_ASSIGN);
3207 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003209 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3210 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003211 case '[':
3212 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3214 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003215 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 if (l->buf[l->i] == '\n') {
3217 l->t.t = BC_LEX_WHITESPACE;
3218 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003219 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003220 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3224 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003225 case 'a':
3226 case 'b':
3227 case 'c':
3228 case 'd':
3229 case 'e':
3230 case 'f':
3231 case 'g':
3232 case 'h':
3233 case 'i':
3234 case 'j':
3235 case 'k':
3236 case 'l':
3237 case 'm':
3238 case 'n':
3239 case 'o':
3240 case 'p':
3241 case 'q':
3242 case 'r':
3243 case 's':
3244 case 't':
3245 case 'u':
3246 case 'v':
3247 case 'w':
3248 case 'x':
3249 case 'y':
3250 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003251 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003253 case '{':
3254 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3256 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003257 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003260 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003261 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003262 ++l->i;
3263 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003264 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003266 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003268 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003269 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003271 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003272 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 }
3274
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003275 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003276}
3277#endif // ENABLE_BC
3278
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003279#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003280static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003281{
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 if (isspace(l->buf[l->i - 1])) {
3283 bc_lex_whitespace(l);
3284 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003285 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003286 RETURN_STATUS(bc_error("extended register"));
3287 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 }
3289 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003290 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003291 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003292 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003293 l->t.t = BC_LEX_NAME;
3294 }
3295
Denys Vlasenko29301232018-12-11 15:29:32 +01003296 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003297}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003298#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003299# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003300#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003301
Denys Vlasenko29301232018-12-11 15:29:32 +01003302static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003303{
3304 size_t depth = 1, nls = 0, i = l->i;
3305 char c;
3306
3307 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003308 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003309
3310 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3311
3312 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3313 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3314 nls += (c == '\n');
3315
3316 if (depth) bc_vec_push(&l->t.v, &c);
3317 }
3318
3319 if (c == '\0') {
3320 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003321 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003322 }
3323
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003324 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003325 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3326 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3327 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003328 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003329 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003330
3331 l->i = i;
3332 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003333 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003334
Denys Vlasenko29301232018-12-11 15:29:32 +01003335 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003336}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003337#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003338# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003339#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003340
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003341static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003342{
3343 BcStatus s = BC_STATUS_SUCCESS;
3344 char c = l->buf[l->i++], c2;
3345 size_t i;
3346
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003347 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3348 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003349 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003350 }
3351
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003352 if (c >= '%' && c <= '~'
3353 && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID
3354 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003355 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356 }
3357
3358 // This is the workhorse of the lexer.
3359 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003360 case '\0':
Gavin Howard01055ba2018-11-03 11:00:21 -06003361 l->t.t = BC_LEX_EOF;
3362 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003363 case '\n':
3364 case '\t':
3365 case '\v':
3366 case '\f':
3367 case '\r':
3368 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003369 l->newline = (c == '\n');
3370 bc_lex_whitespace(l);
3371 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003372 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003373 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003374 if (c2 == '=')
3375 l->t.t = BC_LEX_OP_REL_NE;
3376 else if (c2 == '<')
3377 l->t.t = BC_LEX_OP_REL_LE;
3378 else if (c2 == '>')
3379 l->t.t = BC_LEX_OP_REL_GE;
3380 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003381 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003382 ++l->i;
3383 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003384 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003385 bc_lex_lineComment(l);
3386 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003387 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003388 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003389 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003390 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003391 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003392 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003393 case '0':
3394 case '1':
3395 case '2':
3396 case '3':
3397 case '4':
3398 case '5':
3399 case '6':
3400 case '7':
3401 case '8':
3402 case '9':
3403 case 'A':
3404 case 'B':
3405 case 'C':
3406 case 'D':
3407 case 'E':
3408 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003409 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003410 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003411 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003412 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003413 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003414 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003415 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003416 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003417 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003418 }
3419
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003420 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003421}
3422#endif // ENABLE_DC
3423
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003424static void bc_program_addFunc(char *name, size_t *idx);
3425
Gavin Howard01055ba2018-11-03 11:00:21 -06003426static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3427{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003428 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003429 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003430}
3431
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003432#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3433
Gavin Howard01055ba2018-11-03 11:00:21 -06003434static void bc_parse_pushName(BcParse *p, char *name)
3435{
3436 size_t i = 0, len = strlen(name);
3437
3438 for (; i < len; ++i) bc_parse_push(p, name[i]);
3439 bc_parse_push(p, BC_PARSE_STREND);
3440
3441 free(name);
3442}
3443
3444static void bc_parse_pushIndex(BcParse *p, size_t idx)
3445{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003446 size_t mask;
3447 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003448
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003449 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3450 amt = sizeof(idx);
3451 do {
3452 if (idx & mask) break;
3453 mask >>= 8;
3454 amt--;
3455 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003456
3457 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003458
3459 while (idx != 0) {
3460 bc_parse_push(p, (unsigned char)idx);
3461 idx >>= 8;
3462 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003463}
3464
3465static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3466{
3467 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003468 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003469
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003470 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003471
3472 bc_parse_push(p, BC_INST_NUM);
3473 bc_parse_pushIndex(p, idx);
3474
3475 ++(*nexs);
3476 (*prev) = BC_INST_NUM;
3477}
3478
Denys Vlasenkoe755e302018-12-13 22:25:28 +01003479IF_BC(static BC_STATUS zbc_parse_parse(BcParse *p);)
3480IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003481
3482static BC_STATUS zcommon_parse(BcParse *p)
3483{
3484 if (IS_BC) {
3485 IF_BC(RETURN_STATUS(zbc_parse_parse(p));)
3486 }
3487 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3488}
3489
Denys Vlasenko26819db2018-12-12 16:08:46 +01003490static BC_STATUS zbc_parse_text(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003491{
3492 BcStatus s;
3493
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003494 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003495
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003496 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003497 p->l.t.t = BC_LEX_INVALID;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003498 s = BC_STATUS_SUCCESS;
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003499 ERROR_RETURN(s =) zcommon_parse(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003500 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003501 if (!BC_PARSE_CAN_EXEC(p))
Denys Vlasenko26819db2018-12-12 16:08:46 +01003502 RETURN_STATUS(bc_error("file is not executable"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003503 }
3504
Denys Vlasenko26819db2018-12-12 16:08:46 +01003505 RETURN_STATUS(zbc_lex_text(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003506}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003507#if ERRORS_ARE_FATAL
3508# define zbc_parse_text(...) (zbc_parse_text(__VA_ARGS__), BC_STATUS_SUCCESS)
3509#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003510
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003511// Called when parsing or execution detects a failure,
3512// resets execution structures.
3513static void bc_program_reset(void)
3514{
3515 BcFunc *f;
3516 BcInstPtr *ip;
3517
3518 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3519 bc_vec_pop_all(&G.prog.results);
3520
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003521 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003522 ip = bc_vec_top(&G.prog.stack);
3523 ip->idx = f->code.len;
3524}
3525
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003526#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003527 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003528
Denys Vlasenko26819db2018-12-12 16:08:46 +01003529// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003530// resets parsing structures.
3531static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003532{
3533 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003534 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003535 bc_vec_pop_all(&p->func->code);
3536 bc_vec_pop_all(&p->func->autos);
3537 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003538
3539 bc_parse_updateFunc(p, BC_PROG_MAIN);
3540 }
3541
3542 p->l.i = p->l.len;
3543 p->l.t.t = BC_LEX_EOF;
3544 p->auto_part = (p->nbraces = 0);
3545
3546 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003547 bc_vec_pop_all(&p->exits);
3548 bc_vec_pop_all(&p->conds);
3549 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003550
Denys Vlasenkod38af482018-12-04 19:11:02 +01003551 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003552}
3553
3554static void bc_parse_free(BcParse *p)
3555{
3556 bc_vec_free(&p->flags);
3557 bc_vec_free(&p->exits);
3558 bc_vec_free(&p->conds);
3559 bc_vec_free(&p->ops);
3560 bc_lex_free(&p->l);
3561}
3562
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003563static void bc_parse_create(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06003564{
3565 memset(p, 0, sizeof(BcParse));
3566
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003567 bc_lex_init(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003568 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3569 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3570 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003571 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003572 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3573
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003574 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003575 bc_parse_updateFunc(p, func);
3576}
3577
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003578#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003579
3580#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3581#define BC_PARSE_LEAF(p, rparen) \
3582 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3583 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3584
3585// We can calculate the conversion between tokens and exprs by subtracting the
3586// position of the first operator in the lex enum and adding the position of the
3587// first in the expr enum. Note: This only works for binary operators.
3588#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3589
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003590static BC_STATUS zbc_parse_else(BcParse *p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003591static BC_STATUS zbc_parse_stmt(BcParse *p);
3592static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003593static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003594#if ERRORS_ARE_FATAL
3595# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
3596# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
3597# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
3598#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003599
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003600static BC_STATUS zbc_parse_operator(BcParse *p, BcLexType type, size_t start,
Gavin Howard01055ba2018-11-03 11:00:21 -06003601 size_t *nexprs, bool next)
3602{
3603 BcStatus s = BC_STATUS_SUCCESS;
3604 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003605 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3606 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003607
3608 while (p->ops.len > start) {
3609
3610 t = BC_PARSE_TOP_OP(p);
3611 if (t == BC_LEX_LPAREN) break;
3612
Denys Vlasenko65437582018-12-05 19:37:19 +01003613 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003614 if (l >= r && (l != r || !left)) break;
3615
3616 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3617 bc_vec_pop(&p->ops);
3618 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3619 }
3620
3621 bc_vec_push(&p->ops, &type);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003622 if (next) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003623
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003624 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003625}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003626#if ERRORS_ARE_FATAL
3627# define zbc_parse_operator(...) (zbc_parse_operator(__VA_ARGS__), BC_STATUS_SUCCESS)
3628#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003629
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003630static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003631{
3632 BcLexType top;
3633
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003634 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003635 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003636 top = BC_PARSE_TOP_OP(p);
3637
3638 while (top != BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003639 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3640
3641 bc_vec_pop(&p->ops);
3642 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3643
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003644 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003645 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003646 top = BC_PARSE_TOP_OP(p);
3647 }
3648
3649 bc_vec_pop(&p->ops);
3650
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003651 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003652}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003653#if ERRORS_ARE_FATAL
3654# define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__), BC_STATUS_SUCCESS)
3655#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003656
Denys Vlasenko26819db2018-12-12 16:08:46 +01003657static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003658{
3659 BcStatus s;
3660 bool comma = false;
3661 size_t nparams;
3662
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003663 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003664 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003665
3666 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003667 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003668 s = zbc_parse_expr(p, flags, bc_parse_next_param);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003669 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003670
3671 comma = p->l.t.t == BC_LEX_COMMA;
3672 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003673 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003674 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003675 }
3676 }
3677
Denys Vlasenko26819db2018-12-12 16:08:46 +01003678 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003679 bc_parse_push(p, BC_INST_CALL);
3680 bc_parse_pushIndex(p, nparams);
3681
Denys Vlasenko26819db2018-12-12 16:08:46 +01003682 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003683}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003684#if ERRORS_ARE_FATAL
3685# define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__), BC_STATUS_SUCCESS)
3686#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003687
Denys Vlasenko26819db2018-12-12 16:08:46 +01003688static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003689{
3690 BcStatus s;
3691 BcId entry, *entry_ptr;
3692 size_t idx;
3693
3694 entry.name = name;
3695
Denys Vlasenko26819db2018-12-12 16:08:46 +01003696 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003697 if (s) goto err;
3698
3699 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003700 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003701 goto err;
3702 }
3703
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003704 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003705
3706 if (idx == BC_VEC_INVALID_IDX) {
3707 name = xstrdup(entry.name);
3708 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003709 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003710 free(entry.name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003711 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003712 free(name);
3713
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003714 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003715 bc_parse_pushIndex(p, entry_ptr->idx);
3716
Denys Vlasenko26819db2018-12-12 16:08:46 +01003717 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003718
3719err:
3720 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003721 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003722}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003723#if ERRORS_ARE_FATAL
3724# define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__), BC_STATUS_SUCCESS)
3725#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003726
Denys Vlasenko26819db2018-12-12 16:08:46 +01003727static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003728{
3729 BcStatus s;
3730 char *name;
3731
3732 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003733 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003734 if (s) goto err;
3735
3736 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003737 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003738 if (s) goto err;
3739
3740 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003741 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003742 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003743 goto err;
3744 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003745 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003746 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003747 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003748 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003749 s = zbc_parse_expr(p, flags, bc_parse_next_elem);
Gavin Howard01055ba2018-11-03 11:00:21 -06003750 if (s) goto err;
3751 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003752 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003753 if (s) goto err;
3754 bc_parse_push(p, *type);
3755 bc_parse_pushName(p, name);
3756 }
3757 else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003758 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003759 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 goto err;
3761 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003762 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003763 s = zbc_parse_call(p, name, flags);
3764 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003765 *type = BC_INST_VAR;
3766 bc_parse_push(p, BC_INST_VAR);
3767 bc_parse_pushName(p, name);
3768 }
3769
Denys Vlasenko26819db2018-12-12 16:08:46 +01003770 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003771
3772err:
3773 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003774 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003775}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003776#if ERRORS_ARE_FATAL
3777# define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__), BC_STATUS_SUCCESS)
3778#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003779
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003780static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003781{
3782 BcStatus s;
3783
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003784 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003785 if (s) RETURN_STATUS(s);
3786 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003787
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003788 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003789 if (s) RETURN_STATUS(s);
3790 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003791
3792 bc_parse_push(p, BC_INST_READ);
3793
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003794 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003795}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003796#if ERRORS_ARE_FATAL
3797# define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__), BC_STATUS_SUCCESS)
3798#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003799
Denys Vlasenko26819db2018-12-12 16:08:46 +01003800static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003801 BcInst *prev)
3802{
3803 BcStatus s;
3804
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003805 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003806 if (s) RETURN_STATUS(s);
3807 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003808
3809 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3810
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003811 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003812 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003813
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003814 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003815 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003816
Denys Vlasenko26819db2018-12-12 16:08:46 +01003817 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003818
3819 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3820 bc_parse_push(p, *prev);
3821
Denys Vlasenko26819db2018-12-12 16:08:46 +01003822 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003823}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003824#if ERRORS_ARE_FATAL
3825# define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
3826#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003827
Denys Vlasenko26819db2018-12-12 16:08:46 +01003828static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003829{
3830 BcStatus s;
3831
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003832 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003833 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003834
3835 if (p->l.t.t != BC_LEX_LPAREN) {
3836 *type = BC_INST_SCALE;
3837 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003838 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003839 }
3840
3841 *type = BC_INST_SCALE_FUNC;
3842 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3843
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003844 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003845 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003846
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003847 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003848 if (s) RETURN_STATUS(s);
3849 if (p->l.t.t != BC_LEX_RPAREN)
3850 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003851 bc_parse_push(p, BC_INST_SCALE_FUNC);
3852
Denys Vlasenko26819db2018-12-12 16:08:46 +01003853 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003854}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003855#if ERRORS_ARE_FATAL
3856# define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__), BC_STATUS_SUCCESS)
3857#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003858
Denys Vlasenko26819db2018-12-12 16:08:46 +01003859static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003860 size_t *nexprs, uint8_t flags)
3861{
3862 BcStatus s;
3863 BcLexType type;
3864 char inst;
3865 BcInst etype = *prev;
3866
3867 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3868 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3869 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3870 {
3871 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3872 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003873 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003874 }
3875 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003876 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3877 *paren_expr = true;
3878
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003879 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003880 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003881 type = p->l.t.t;
3882
3883 // Because we parse the next part of the expression
3884 // right here, we need to increment this.
3885 *nexprs = *nexprs + 1;
3886
3887 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01003889 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003890 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003891 case BC_LEX_KEY_IBASE:
3892 case BC_LEX_KEY_LAST:
3893 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003895 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003896 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003898 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003899 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003900 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003901 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003902 else
3903 bc_parse_push(p, BC_INST_SCALE);
3904 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003906 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 }
3909
3910 if (!s) bc_parse_push(p, inst);
3911 }
3912
Denys Vlasenko26819db2018-12-12 16:08:46 +01003913 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003914}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003915#if ERRORS_ARE_FATAL
3916# define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
3917#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003918
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003919static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06003920 bool rparen, size_t *nexprs)
3921{
3922 BcStatus s;
3923 BcLexType type;
3924 BcInst etype = *prev;
3925
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003926 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003927 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003928
3929 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3930 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3931 BC_LEX_OP_MINUS :
3932 BC_LEX_NEG;
3933 *prev = BC_PARSE_TOKEN_INST(type);
3934
3935 // We can just push onto the op stack because this is the largest
3936 // precedence operator that gets pushed. Inc/dec does not.
3937 if (type != BC_LEX_OP_MINUS)
3938 bc_vec_push(&p->ops, &type);
3939 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003940 s = zbc_parse_operator(p, type, ops_bgn, nexprs, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06003941
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003942 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003943}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003944#if ERRORS_ARE_FATAL
3945# define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__), BC_STATUS_SUCCESS)
3946#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003947
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003948static BC_STATUS zbc_parse_string(BcParse *p, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06003949{
3950 char *str = xstrdup(p->l.t.v.v);
3951
3952 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003953 bc_parse_pushIndex(p, G.prog.strs.len);
3954 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003955 bc_parse_push(p, inst);
3956
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003957 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003958}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003959#if ERRORS_ARE_FATAL
3960# define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3961#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003962
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003963static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003964{
3965 BcStatus s;
3966 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003967 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06003968
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003969 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003970 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003971
3972 type = p->l.t.t;
3973
3974 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003975 RETURN_STATUS(bc_error("bad print statement"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003976
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003977 comma = false;
3978 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003979 if (type == BC_LEX_STR) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003980 s = zbc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003981 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003982 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003983 s = zbc_parse_expr(p, 0, bc_parse_next_print);
3984 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003985 bc_parse_push(p, BC_INST_PRINT_POP);
3986 }
3987
Gavin Howard01055ba2018-11-03 11:00:21 -06003988 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003989 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003990 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003991 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003992 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003993 type = p->l.t.t;
3994 }
3995
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003996 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003997
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003998 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003999}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004000#if ERRORS_ARE_FATAL
4001# define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__), BC_STATUS_SUCCESS)
4002#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004003
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004004static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004005{
4006 BcStatus s;
4007 BcLexType t;
4008 bool paren;
4009
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004010 if (!BC_PARSE_FUNC(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004012 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004013 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004014
4015 t = p->l.t.t;
4016 paren = t == BC_LEX_LPAREN;
4017
4018 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4019 bc_parse_push(p, BC_INST_RET0);
4020 else {
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004021 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004022 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004024 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004025 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004026 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004027
4028 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004029 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004030 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06004031 }
4032
4033 bc_parse_push(p, BC_INST_RET);
4034 }
4035
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004036 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004037}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004038#if ERRORS_ARE_FATAL
4039# define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS)
4040#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004041
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004042static BC_STATUS zbc_parse_endBody(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004043{
4044 BcStatus s = BC_STATUS_SUCCESS;
4045
4046 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004047 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004048
4049 if (brace) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004050 if (p->l.t.t != BC_LEX_RBRACE)
4051 RETURN_STATUS(bc_error_bad_token());
4052 if (!p->nbraces)
4053 RETURN_STATUS(bc_error_bad_token());
4054 --p->nbraces;
4055 s = zbc_lex_next(&p->l);
4056 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004057 }
4058
4059 if (BC_PARSE_IF(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004060 uint8_t *flag_ptr;
4061
4062 while (p->l.t.t == BC_LEX_NLINE) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004063 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004064 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004065 }
4066
4067 bc_vec_pop(&p->flags);
4068
4069 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4070 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4071
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004072 if (p->l.t.t == BC_LEX_KEY_ELSE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004073 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004074 }
4075 else if (BC_PARSE_ELSE(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004076 BcInstPtr *ip;
4077 size_t *label;
4078
4079 bc_vec_pop(&p->flags);
4080
4081 ip = bc_vec_top(&p->exits);
4082 label = bc_vec_item(&p->func->labels, ip->idx);
4083 *label = p->func->code.len;
4084
4085 bc_vec_pop(&p->exits);
4086 }
4087 else if (BC_PARSE_FUNC_INNER(p)) {
4088 bc_parse_push(p, BC_INST_RET0);
4089 bc_parse_updateFunc(p, BC_PROG_MAIN);
4090 bc_vec_pop(&p->flags);
4091 }
4092 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004093 BcInstPtr *ip = bc_vec_top(&p->exits);
4094 size_t *label = bc_vec_top(&p->conds);
4095
4096 bc_parse_push(p, BC_INST_JUMP);
4097 bc_parse_pushIndex(p, *label);
4098
4099 label = bc_vec_item(&p->func->labels, ip->idx);
4100 *label = p->func->code.len;
4101
4102 bc_vec_pop(&p->flags);
4103 bc_vec_pop(&p->exits);
4104 bc_vec_pop(&p->conds);
4105 }
4106
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004107 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004108}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004109#if ERRORS_ARE_FATAL
4110# define zbc_parse_endBody(...) (zbc_parse_endBody(__VA_ARGS__), BC_STATUS_SUCCESS)
4111#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004112
4113static void bc_parse_startBody(BcParse *p, uint8_t flags)
4114{
4115 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4116 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4117 flags |= BC_PARSE_FLAG_BODY;
4118 bc_vec_push(&p->flags, &flags);
4119}
4120
4121static void bc_parse_noElse(BcParse *p)
4122{
4123 BcInstPtr *ip;
4124 size_t *label;
4125 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4126
4127 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4128
4129 ip = bc_vec_top(&p->exits);
4130 label = bc_vec_item(&p->func->labels, ip->idx);
4131 *label = p->func->code.len;
4132
4133 bc_vec_pop(&p->exits);
4134}
4135
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004136static BC_STATUS zbc_parse_if(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004137{
4138 BcStatus s;
4139 BcInstPtr ip;
4140
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004141 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004142 if (s) RETURN_STATUS(s);
4143 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004144
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004145 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004146 if (s) RETURN_STATUS(s);
4147 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4148 if (s) RETURN_STATUS(s);
4149 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004150
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004151 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004152 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004153 bc_parse_push(p, BC_INST_JUMP_ZERO);
4154
4155 ip.idx = p->func->labels.len;
4156 ip.func = ip.len = 0;
4157
4158 bc_parse_pushIndex(p, ip.idx);
4159 bc_vec_push(&p->exits, &ip);
4160 bc_vec_push(&p->func->labels, &ip.idx);
4161 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4162
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004163 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004164}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004165#if ERRORS_ARE_FATAL
4166# define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__), BC_STATUS_SUCCESS)
4167#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004168
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004169#undef zbc_parse_else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004170static BC_STATUS zbc_parse_else(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004171{
4172 BcInstPtr ip;
4173
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004174 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004175
4176 ip.idx = p->func->labels.len;
4177 ip.func = ip.len = 0;
4178
4179 bc_parse_push(p, BC_INST_JUMP);
4180 bc_parse_pushIndex(p, ip.idx);
4181
4182 bc_parse_noElse(p);
4183
4184 bc_vec_push(&p->exits, &ip);
4185 bc_vec_push(&p->func->labels, &ip.idx);
4186 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4187
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004188 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004189}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004190#if ERRORS_ARE_FATAL
4191# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
4192#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004193
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004194static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004195{
4196 BcStatus s;
4197 BcInstPtr ip;
4198
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);
4201 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004202 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004203 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004204
4205 ip.idx = p->func->labels.len;
4206
4207 bc_vec_push(&p->func->labels, &p->func->code.len);
4208 bc_vec_push(&p->conds, &ip.idx);
4209
4210 ip.idx = p->func->labels.len;
4211 ip.func = 1;
4212 ip.len = 0;
4213
4214 bc_vec_push(&p->exits, &ip);
4215 bc_vec_push(&p->func->labels, &ip.idx);
4216
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004217 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4218 if (s) RETURN_STATUS(s);
4219 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004220 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004221 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004222
4223 bc_parse_push(p, BC_INST_JUMP_ZERO);
4224 bc_parse_pushIndex(p, ip.idx);
4225 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4226
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004227 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004228}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004229#if ERRORS_ARE_FATAL
4230# define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__), BC_STATUS_SUCCESS)
4231#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004232
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004233static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004234{
4235 BcStatus s;
4236 BcInstPtr ip;
4237 size_t cond_idx, exit_idx, body_idx, update_idx;
4238
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);
4241 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004242 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004243 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004244
4245 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004246 s = zbc_parse_expr(p, 0, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004247 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004248 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004249
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004250 if (s) RETURN_STATUS(s);
4251 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004252 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004253 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004254
4255 cond_idx = p->func->labels.len;
4256 update_idx = cond_idx + 1;
4257 body_idx = update_idx + 1;
4258 exit_idx = body_idx + 1;
4259
4260 bc_vec_push(&p->func->labels, &p->func->code.len);
4261
4262 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004263 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004264 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004265 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004266
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004267 if (s) RETURN_STATUS(s);
4268 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004269
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004270 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004271 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
4273 bc_parse_push(p, BC_INST_JUMP_ZERO);
4274 bc_parse_pushIndex(p, exit_idx);
4275 bc_parse_push(p, BC_INST_JUMP);
4276 bc_parse_pushIndex(p, body_idx);
4277
4278 ip.idx = p->func->labels.len;
4279
4280 bc_vec_push(&p->conds, &update_idx);
4281 bc_vec_push(&p->func->labels, &p->func->code.len);
4282
4283 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004284 s = zbc_parse_expr(p, 0, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06004285 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004286 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004287
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004288 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004289
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004290 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004291 bc_parse_push(p, BC_INST_JUMP);
4292 bc_parse_pushIndex(p, cond_idx);
4293 bc_vec_push(&p->func->labels, &p->func->code.len);
4294
4295 ip.idx = exit_idx;
4296 ip.func = 1;
4297 ip.len = 0;
4298
4299 bc_vec_push(&p->exits, &ip);
4300 bc_vec_push(&p->func->labels, &ip.idx);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004301 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004302 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004303 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4304
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004305 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004306}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004307#if ERRORS_ARE_FATAL
4308# define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__), BC_STATUS_SUCCESS)
4309#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004311static BC_STATUS zbc_parse_loopExit(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004312{
4313 BcStatus s;
4314 size_t i;
4315 BcInstPtr *ip;
4316
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004317 if (!BC_PARSE_LOOP(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004318
4319 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004320 if (p->exits.len == 0) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004321
4322 i = p->exits.len - 1;
4323 ip = bc_vec_item(&p->exits, i);
4324
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004325 while (!ip->func && i < p->exits.len)
4326 ip = bc_vec_item(&p->exits, i--);
4327 if (i >= p->exits.len && !ip->func)
4328 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004329
4330 i = ip->idx;
4331 }
4332 else
4333 i = *((size_t *) bc_vec_top(&p->conds));
4334
4335 bc_parse_push(p, BC_INST_JUMP);
4336 bc_parse_pushIndex(p, i);
4337
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004338 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004339 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004340
4341 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004342 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004343
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004344 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004345}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004346#if ERRORS_ARE_FATAL
4347# define zbc_parse_loopExit(...) (zbc_parse_loopExit(__VA_ARGS__), BC_STATUS_SUCCESS)
4348#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004349
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004350static BC_STATUS zbc_parse_func(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004351{
4352 BcStatus s;
4353 bool var, comma = false;
4354 uint8_t flags;
4355 char *name;
4356
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004357 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004358 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004359 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004360 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004361
4362 name = xstrdup(p->l.t.v.v);
4363 bc_parse_addFunc(p, name, &p->fidx);
4364
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004365 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004366 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004367 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004368 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004369 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004370 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004371
4372 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004373 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004374 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004375
4376 ++p->func->nparams;
4377
4378 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004379 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004380 if (s) goto err;
4381
4382 var = p->l.t.t != BC_LEX_LBRACKET;
4383
4384 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004385 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004386 if (s) goto err;
4387
4388 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004389 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004390 goto err;
4391 }
4392
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004393 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004394 if (s) goto err;
4395 }
4396
4397 comma = p->l.t.t == BC_LEX_COMMA;
4398 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004399 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004400 if (s) goto err;
4401 }
4402
Denys Vlasenko29301232018-12-11 15:29:32 +01004403 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004404 if (s) goto err;
4405 }
4406
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004407 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004408
4409 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4410 bc_parse_startBody(p, flags);
4411
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004412 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004413 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004414
4415 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004416 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004417
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004418 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004419
4420err:
4421 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004422 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004423}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004424#if ERRORS_ARE_FATAL
4425# define zbc_parse_func(...) (zbc_parse_func(__VA_ARGS__), BC_STATUS_SUCCESS)
4426#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004427
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004428static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004429{
4430 BcStatus s;
4431 bool comma, var, one;
4432 char *name;
4433
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004434 if (!p->auto_part) RETURN_STATUS(bc_error_bad_token());
4435
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004436 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004437 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004438
4439 p->auto_part = comma = false;
4440 one = p->l.t.t == BC_LEX_NAME;
4441
4442 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004443 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004444 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004445 if (s) goto err;
4446
4447 var = p->l.t.t != BC_LEX_LBRACKET;
4448 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004449 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004450 if (s) goto err;
4451
4452 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004453 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 goto err;
4455 }
4456
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004457 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004458 if (s) goto err;
4459 }
4460
4461 comma = p->l.t.t == BC_LEX_COMMA;
4462 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004463 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004464 if (s) goto err;
4465 }
4466
Denys Vlasenko29301232018-12-11 15:29:32 +01004467 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004468 if (s) goto err;
4469 }
4470
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004471 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4472 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004473
4474 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004475 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004476
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004477 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004478
4479err:
4480 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004481 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004482}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004483#if ERRORS_ARE_FATAL
4484# define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__), BC_STATUS_SUCCESS)
4485#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004486
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004487static BC_STATUS zbc_parse_body(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004488{
4489 BcStatus s = BC_STATUS_SUCCESS;
4490 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4491
4492 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4493
4494 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004495 if (!brace) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004496
Gavin Howard01055ba2018-11-03 11:00:21 -06004497 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4498
4499 if (!p->auto_part) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004500 s = zbc_parse_auto(p);
4501 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004502 }
4503
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004504 if (p->l.t.t == BC_LEX_NLINE) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004505 }
4506 else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004507 s = zbc_parse_stmt(p);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004508 if (!s && !brace) s = zbc_parse_endBody(p, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004509 }
4510
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004511 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004512}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004513#if ERRORS_ARE_FATAL
4514# define zbc_parse_body(...) (zbc_parse_body(__VA_ARGS__), BC_STATUS_SUCCESS)
4515#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004516
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004517#undef zbc_parse_stmt
4518static BC_STATUS zbc_parse_stmt(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004519{
4520 BcStatus s = BC_STATUS_SUCCESS;
4521
4522 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004523 case BC_LEX_NLINE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004524 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004525
4526 case BC_LEX_KEY_ELSE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004527 p->auto_part = false;
4528 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004529
4530 case BC_LEX_LBRACE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004531 if (!BC_PARSE_BODY(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004532 ++p->nbraces;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004533 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004534 if (s) RETURN_STATUS(s);
4535 RETURN_STATUS(zbc_parse_body(p, true));
Gavin Howard01055ba2018-11-03 11:00:21 -06004536
4537 case BC_LEX_KEY_AUTO:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004538 RETURN_STATUS(zbc_parse_auto(p));
Gavin Howard01055ba2018-11-03 11:00:21 -06004539
4540 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 p->auto_part = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004542 if (BC_PARSE_IF_END(p)) {
4543 bc_parse_noElse(p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004544 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004545 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004546 if (BC_PARSE_BODY(p))
4547 RETURN_STATUS(zbc_parse_body(p, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06004548 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 }
4550
4551 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004552 case BC_LEX_OP_INC:
4553 case BC_LEX_OP_DEC:
4554 case BC_LEX_OP_MINUS:
4555 case BC_LEX_OP_BOOL_NOT:
4556 case BC_LEX_LPAREN:
4557 case BC_LEX_NAME:
4558 case BC_LEX_NUMBER:
4559 case BC_LEX_KEY_IBASE:
4560 case BC_LEX_KEY_LAST:
4561 case BC_LEX_KEY_LENGTH:
4562 case BC_LEX_KEY_OBASE:
4563 case BC_LEX_KEY_READ:
4564 case BC_LEX_KEY_SCALE:
4565 case BC_LEX_KEY_SQRT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004566 s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 case BC_LEX_KEY_ELSE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004569 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 case BC_LEX_SCOLON:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004572 while (!s && p->l.t.t == BC_LEX_SCOLON) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004574 case BC_LEX_RBRACE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004575 s = zbc_parse_endBody(p, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004577 case BC_LEX_STR:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004578 s = zbc_parse_string(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 case BC_LEX_KEY_BREAK:
4581 case BC_LEX_KEY_CONTINUE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004582 s = zbc_parse_loopExit(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004585 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004587 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004589 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004590 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004591 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004592 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004594 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004595 // "limits" is a compile-time command,
4596 // the output is produced at _parse time_.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004597 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004598 if (s) RETURN_STATUS(s);
Denys Vlasenko64074a12018-12-07 15:50:14 +01004599 printf(
4600 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4601 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4602 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4603 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4604 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4605 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4606 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4607 "Number of vars = "BC_MAX_VARS_STR "\n"
4608 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004610 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004611 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004613 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004614 // "quit" is a compile-time command. For example,
4615 // "if (0 == 1) quit" terminates when parsing the statement,
4616 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004617 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004618 case BC_LEX_KEY_RETURN:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004619 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004622 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004623 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004625 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004626 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004627 }
4628
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004629 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004630}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004631#if ERRORS_ARE_FATAL
4632# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
4633#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004634
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01004635static BC_STATUS zbc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004636{
4637 BcStatus s;
4638
4639 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004640 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 -06004641 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004642 if (!BC_PARSE_CAN_EXEC(p))
4643 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004644 s = zbc_parse_func(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004645 } else
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004646 s = zbc_parse_stmt(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004647
Denys Vlasenkod38af482018-12-04 19:11:02 +01004648 if (s || G_interrupt) {
4649 bc_parse_reset(p);
4650 s = BC_STATUS_FAILURE;
4651 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004652
Denys Vlasenko26819db2018-12-12 16:08:46 +01004653 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004654}
Denys Vlasenko26819db2018-12-12 16:08:46 +01004655#if ERRORS_ARE_FATAL
4656# define zbc_parse_parse(...) (zbc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
4657#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004658
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004659// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004660static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004661{
4662 BcStatus s = BC_STATUS_SUCCESS;
4663 BcInst prev = BC_INST_PRINT;
4664 BcLexType top, t = p->l.t.t;
4665 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004666 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004667 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4668
4669 paren_first = p->l.t.t == BC_LEX_LPAREN;
4670 nparens = nrelops = 0;
4671 paren_expr = rprn = done = get_token = assign = false;
4672 bin_last = true;
4673
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004674 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004675 switch (t) {
4676
4677 case BC_LEX_OP_INC:
4678 case BC_LEX_OP_DEC:
4679 {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004680 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004681 rprn = get_token = bin_last = false;
4682 break;
4683 }
4684
4685 case BC_LEX_OP_MINUS:
4686 {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004687 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004688 rprn = get_token = false;
4689 bin_last = prev == BC_INST_MINUS;
4690 break;
4691 }
4692
4693 case BC_LEX_OP_ASSIGN_POWER:
4694 case BC_LEX_OP_ASSIGN_MULTIPLY:
4695 case BC_LEX_OP_ASSIGN_DIVIDE:
4696 case BC_LEX_OP_ASSIGN_MODULUS:
4697 case BC_LEX_OP_ASSIGN_PLUS:
4698 case BC_LEX_OP_ASSIGN_MINUS:
4699 case BC_LEX_OP_ASSIGN:
4700 {
4701 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4702 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4703 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4704 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004705 s = bc_error("bad assignment:"
4706 " left side must be scale,"
4707 " ibase, obase, last, var,"
4708 " or array element"
4709 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004710 break;
4711 }
4712 }
4713 // Fallthrough.
4714 case BC_LEX_OP_POWER:
4715 case BC_LEX_OP_MULTIPLY:
4716 case BC_LEX_OP_DIVIDE:
4717 case BC_LEX_OP_MODULUS:
4718 case BC_LEX_OP_PLUS:
4719 case BC_LEX_OP_REL_EQ:
4720 case BC_LEX_OP_REL_LE:
4721 case BC_LEX_OP_REL_GE:
4722 case BC_LEX_OP_REL_NE:
4723 case BC_LEX_OP_REL_LT:
4724 case BC_LEX_OP_REL_GT:
4725 case BC_LEX_OP_BOOL_NOT:
4726 case BC_LEX_OP_BOOL_OR:
4727 case BC_LEX_OP_BOOL_AND:
4728 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004729 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4730 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4731 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004732 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004733 }
4734
4735 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4736 prev = BC_PARSE_TOKEN_INST(t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004737 s = zbc_parse_operator(p, t, ops_bgn, &nexprs, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004738 rprn = get_token = false;
4739 bin_last = t != BC_LEX_OP_BOOL_NOT;
4740
4741 break;
4742 }
4743
4744 case BC_LEX_LPAREN:
4745 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004746 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004747 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004748 ++nparens;
4749 paren_expr = rprn = bin_last = false;
4750 get_token = true;
4751 bc_vec_push(&p->ops, &t);
4752
4753 break;
4754 }
4755
4756 case BC_LEX_RPAREN:
4757 {
4758 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004759 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004760
4761 if (nparens == 0) {
4762 s = BC_STATUS_SUCCESS;
4763 done = true;
4764 get_token = false;
4765 break;
4766 }
4767 else if (!paren_expr)
4768 return BC_STATUS_PARSE_EMPTY_EXP;
4769
4770 --nparens;
4771 paren_expr = rprn = true;
4772 get_token = bin_last = false;
4773
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004774 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004775
4776 break;
4777 }
4778
4779 case BC_LEX_NAME:
4780 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004781 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004782 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 paren_expr = true;
4784 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004785 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004786 ++nexprs;
4787
4788 break;
4789 }
4790
4791 case BC_LEX_NUMBER:
4792 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004793 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004794 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004795 bc_parse_number(p, &prev, &nexprs);
4796 paren_expr = get_token = true;
4797 rprn = bin_last = false;
4798
4799 break;
4800 }
4801
4802 case BC_LEX_KEY_IBASE:
4803 case BC_LEX_KEY_LAST:
4804 case BC_LEX_KEY_OBASE:
4805 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004806 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004807 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004808 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4809 bc_parse_push(p, (char) prev);
4810
4811 paren_expr = get_token = true;
4812 rprn = bin_last = false;
4813 ++nexprs;
4814
4815 break;
4816 }
4817
4818 case BC_LEX_KEY_LENGTH:
4819 case BC_LEX_KEY_SQRT:
4820 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004821 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004822 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004823 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004824 paren_expr = true;
4825 rprn = get_token = bin_last = false;
4826 ++nexprs;
4827
4828 break;
4829 }
4830
4831 case BC_LEX_KEY_READ:
4832 {
4833 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004834 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004835 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004836 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004837 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004838 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004839
4840 paren_expr = true;
4841 rprn = get_token = bin_last = false;
4842 ++nexprs;
4843 prev = BC_INST_READ;
4844
4845 break;
4846 }
4847
4848 case BC_LEX_KEY_SCALE:
4849 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004850 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004851 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004852 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004853 paren_expr = true;
4854 rprn = get_token = bin_last = false;
4855 ++nexprs;
4856 prev = BC_INST_SCALE;
4857
4858 break;
4859 }
4860
4861 default:
4862 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004863 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004864 break;
4865 }
4866 }
4867
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004868 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004869 }
4870
4871 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004872 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004873
4874 while (p->ops.len > ops_bgn) {
4875
4876 top = BC_PARSE_TOP_OP(p);
4877 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4878
4879 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004880 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004881
4882 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4883
4884 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4885 bc_vec_pop(&p->ops);
4886 }
4887
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004888 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004889 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004890
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004891 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4892 for (;;) {
4893 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004894 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004895 if (next & 0x80) // last element?
4896 break;
4897 next >>= 8;
4898 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004899 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004900 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004901
4902 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004903 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004904 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004905 }
4906 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004907 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004908 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004909 }
4910
4911 if (flags & BC_PARSE_PRINT) {
4912 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4913 bc_parse_push(p, BC_INST_POP);
4914 }
4915
4916 return s;
4917}
4918
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004919#undef zbc_parse_expr
4920static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004921{
4922 BcStatus s;
4923
4924 s = bc_parse_expr_empty_ok(p, flags, next);
4925 if (s == BC_STATUS_PARSE_EMPTY_EXP)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004926 RETURN_STATUS(bc_error("empty expression"));
4927 RETURN_STATUS(s);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004928}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004929#if ERRORS_ARE_FATAL
4930# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
4931#endif
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004932
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004933static BC_STATUS zbc_parse_expression(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004934{
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004935 RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read));
Gavin Howard01055ba2018-11-03 11:00:21 -06004936}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004937#if ERRORS_ARE_FATAL
4938# define zbc_parse_expression(...) (zbc_parse_expression(__VA_ARGS__), BC_STATUS_SUCCESS)
4939#endif
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004940
Gavin Howard01055ba2018-11-03 11:00:21 -06004941#endif // ENABLE_BC
4942
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004943#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004944
4945#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
4946
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004947static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004948{
4949 BcStatus s;
4950 char *name;
4951
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004952 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004953 if (s) RETURN_STATUS(s);
4954 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004955
4956 name = xstrdup(p->l.t.v.v);
4957 bc_parse_pushName(p, name);
4958
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004959 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004960}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004961#if ERRORS_ARE_FATAL
4962# define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__), BC_STATUS_SUCCESS)
4963#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004964
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004965static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004966{
4967 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004968 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004969
4970 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
4971 name = xstrdup(b);
4972
4973 str = xstrdup(p->l.t.v.v);
4974 bc_parse_push(p, BC_INST_STR);
4975 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004976 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004977 bc_parse_addFunc(p, name, &idx);
4978
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004979 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004980}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004981#if ERRORS_ARE_FATAL
4982# define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
4983#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004984
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004985static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004986{
4987 BcStatus s;
4988
4989 bc_parse_push(p, inst);
4990 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004991 s = zdc_parse_register(p);
4992 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004993 }
4994
4995 if (store) {
4996 bc_parse_push(p, BC_INST_SWAP);
4997 bc_parse_push(p, BC_INST_ASSIGN);
4998 bc_parse_push(p, BC_INST_POP);
4999 }
5000
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005001 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06005002}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005003#if ERRORS_ARE_FATAL
5004# define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__), BC_STATUS_SUCCESS)
5005#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005006
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005007static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005008{
5009 BcStatus s;
5010
5011 bc_parse_push(p, inst);
5012 bc_parse_push(p, BC_INST_EXEC_COND);
5013
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005014 s = zdc_parse_register(p);
5015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005016
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005017 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005018 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005019
5020 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005021 s = zdc_parse_register(p);
5022 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005023 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005024 }
5025 else
5026 bc_parse_push(p, BC_PARSE_STREND);
5027
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005028 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005029}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005030#if ERRORS_ARE_FATAL
5031# define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__), BC_STATUS_SUCCESS)
5032#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005033
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005034static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005035{
5036 BcStatus s = BC_STATUS_SUCCESS;
5037 BcInst prev;
5038 uint8_t inst;
5039 bool assign, get_token = false;
5040
5041 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005042 case BC_LEX_OP_REL_EQ:
5043 case BC_LEX_OP_REL_LE:
5044 case BC_LEX_OP_REL_GE:
5045 case BC_LEX_OP_REL_NE:
5046 case BC_LEX_OP_REL_LT:
5047 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005048 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005049 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005050 case BC_LEX_SCOLON:
5051 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005052 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005053 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005054 case BC_LEX_STR:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005055 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06005056 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 case BC_LEX_NEG:
5058 case BC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06005059 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005060 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005061 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005062 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005063 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005064 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005065 bc_parse_number(p, &prev, &p->nbraces);
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5067 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005068 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005069 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06005070 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005071 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005072 else
5073 bc_parse_push(p, BC_INST_READ);
5074 get_token = true;
5075 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 case BC_LEX_OP_ASSIGN:
5077 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 assign = t == BC_LEX_OP_ASSIGN;
5079 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005080 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005081 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 case BC_LEX_LOAD:
5083 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005085 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005086 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005087 case BC_LEX_STORE_IBASE:
5088 case BC_LEX_STORE_SCALE:
5089 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005090 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005091 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005092 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005093 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005094 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005095 get_token = true;
5096 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005097 }
5098
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005099 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005100
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005101 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005102}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005103#if ERRORS_ARE_FATAL
5104# define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__), BC_STATUS_SUCCESS)
5105#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005106
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005107static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005108{
5109 BcStatus s = BC_STATUS_SUCCESS;
5110 BcInst inst;
5111 BcLexType t;
5112
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005113 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005114
5115 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005116 inst = dc_parse_insts[t];
5117
5118 if (inst != BC_INST_INVALID) {
5119 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005120 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005121 } else
5122 s = zdc_parse_token(p, t, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06005123 }
5124
5125 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5126 bc_parse_push(p, BC_INST_POP_EXEC);
5127
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005128 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005129}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005130#if ERRORS_ARE_FATAL
5131# define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5132#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005133
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01005134static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005135{
5136 BcStatus s;
5137
5138 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005139 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005140 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005141 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005142
Denys Vlasenkod38af482018-12-04 19:11:02 +01005143 if (s || G_interrupt) {
5144 bc_parse_reset(p);
5145 s = BC_STATUS_FAILURE;
5146 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005147
Denys Vlasenko26819db2018-12-12 16:08:46 +01005148 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005149}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005150#if ERRORS_ARE_FATAL
5151# define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
5152#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005153
Gavin Howard01055ba2018-11-03 11:00:21 -06005154#endif // ENABLE_DC
5155
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005156static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005157{
5158 if (IS_BC) {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005159 IF_BC(RETURN_STATUS(zbc_parse_expression(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005160 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005161 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005162 }
5163}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005164#if ERRORS_ARE_FATAL
5165# define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5166#endif
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005167
Denys Vlasenkodf515392018-12-02 19:27:48 +01005168static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005169{
Gavin Howard01055ba2018-11-03 11:00:21 -06005170 BcId e, *ptr;
5171 BcVec *v, *map;
5172 size_t i;
5173 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005174 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005175
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005176 v = var ? &G.prog.vars : &G.prog.arrs;
5177 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005178
5179 e.name = id;
5180 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005181 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005182
5183 if (new) {
5184 bc_array_init(&data.v, var);
5185 bc_vec_push(v, &data.v);
5186 }
5187
5188 ptr = bc_vec_item(map, i);
5189 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005190 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005191}
5192
Denys Vlasenko29301232018-12-11 15:29:32 +01005193static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005194{
Gavin Howard01055ba2018-11-03 11:00:21 -06005195 switch (r->t) {
5196
5197 case BC_RESULT_STR:
5198 case BC_RESULT_TEMP:
5199 case BC_RESULT_IBASE:
5200 case BC_RESULT_SCALE:
5201 case BC_RESULT_OBASE:
5202 {
5203 *num = &r->d.n;
5204 break;
5205 }
5206
5207 case BC_RESULT_CONSTANT:
5208 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005209 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005210 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005211 size_t base_t, len = strlen(*str);
5212 BcNum *base;
5213
5214 bc_num_init(&r->d.n, len);
5215
5216 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005217 base = hex ? &G.prog.hexb : &G.prog.ib;
5218 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005219 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005220
5221 if (s) {
5222 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005223 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005224 }
5225
5226 *num = &r->d.n;
5227 r->t = BC_RESULT_TEMP;
5228
5229 break;
5230 }
5231
5232 case BC_RESULT_VAR:
5233 case BC_RESULT_ARRAY:
5234 case BC_RESULT_ARRAY_ELEM:
5235 {
5236 BcVec *v;
5237
Denys Vlasenkodf515392018-12-02 19:27:48 +01005238 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005239
5240 if (r->t == BC_RESULT_ARRAY_ELEM) {
5241 v = bc_vec_top(v);
5242 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5243 *num = bc_vec_item(v, r->d.id.idx);
5244 }
5245 else
5246 *num = bc_vec_top(v);
5247
5248 break;
5249 }
5250
5251 case BC_RESULT_LAST:
5252 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005253 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005254 break;
5255 }
5256
5257 case BC_RESULT_ONE:
5258 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005259 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005260 break;
5261 }
5262 }
5263
Denys Vlasenko29301232018-12-11 15:29:32 +01005264 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005265}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005266#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005267# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005268#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005269
Denys Vlasenko29301232018-12-11 15:29:32 +01005270static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005271 BcResult **r, BcNum **rn, bool assign)
5272{
5273 BcStatus s;
5274 bool hex;
5275 BcResultType lt, rt;
5276
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005277 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005278 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005279
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005280 *r = bc_vec_item_rev(&G.prog.results, 0);
5281 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005282
5283 lt = (*l)->t;
5284 rt = (*r)->t;
5285 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5286
Denys Vlasenko29301232018-12-11 15:29:32 +01005287 s = zbc_program_num(*l, ln, false);
5288 if (s) RETURN_STATUS(s);
5289 s = zbc_program_num(*r, rn, hex);
5290 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005291
5292 // We run this again under these conditions in case any vector has been
5293 // reallocated out from under the BcNums or arrays we had.
5294 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005295 s = zbc_program_num(*l, ln, false);
5296 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005297 }
5298
5299 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005300 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005301 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005302 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005303
Denys Vlasenko29301232018-12-11 15:29:32 +01005304 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005305}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005306#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005307# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005308#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005309
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005310static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005311{
5312 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005313 bc_vec_pop(&G.prog.results);
5314 bc_vec_pop(&G.prog.results);
5315 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005316}
5317
Denys Vlasenko29301232018-12-11 15:29:32 +01005318static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005319{
5320 BcStatus s;
5321
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005322 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005323 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005324 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005325
Denys Vlasenko29301232018-12-11 15:29:32 +01005326 s = zbc_program_num(*r, n, false);
5327 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005328
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005329 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005330 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005331
Denys Vlasenko29301232018-12-11 15:29:32 +01005332 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005333}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005334#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005335# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005336#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005337
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005338static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005339{
5340 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005341 bc_vec_pop(&G.prog.results);
5342 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005343}
5344
Denys Vlasenko259137d2018-12-11 19:42:05 +01005345static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005346{
5347 BcStatus s;
5348 BcResult *opd1, *opd2, res;
5349 BcNum *n1, *n2 = NULL;
5350
Denys Vlasenko29301232018-12-11 15:29:32 +01005351 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005352 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005353 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005354
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005355 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005356 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 -06005357 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005358 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005359
Denys Vlasenko259137d2018-12-11 19:42:05 +01005360 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005361
5362err:
5363 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005364 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005365}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005366#if ERRORS_ARE_FATAL
5367# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5368#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005369
Denys Vlasenko26819db2018-12-12 16:08:46 +01005370static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005371{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005372 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005373 BcStatus s;
5374 BcParse parse;
5375 BcVec buf;
5376 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005377 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005378
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005379 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005380 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005381
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005382 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005383 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005384
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005385 sv_file = G.prog.file;
5386 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005387 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005388
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005389 bc_char_vec_init(&buf);
Denys Vlasenko8a892472018-12-12 22:43:58 +01005390 bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005391
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005392 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005393 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005394
Denys Vlasenko26819db2018-12-12 16:08:46 +01005395 s = zbc_parse_text(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005396 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005397 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005398 if (s) goto exec_err;
5399
5400 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005401 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005402 goto exec_err;
5403 }
5404
5405 ip.func = BC_PROG_READ;
5406 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005407 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005408
5409 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005410 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005411
5412 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005413 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005414
5415exec_err:
5416 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005417//io_err:
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005418 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005419 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005420 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005421 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005422}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005423#if ERRORS_ARE_FATAL
5424# define zbc_program_read(...) (zbc_program_read(__VA_ARGS__), BC_STATUS_SUCCESS)
5425#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005426
5427static size_t bc_program_index(char *code, size_t *bgn)
5428{
5429 char amt = code[(*bgn)++], i = 0;
5430 size_t res = 0;
5431
5432 for (; i < amt; ++i, ++(*bgn))
5433 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5434
5435 return res;
5436}
5437
5438static char *bc_program_name(char *code, size_t *bgn)
5439{
5440 size_t i;
5441 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5442
5443 s = xmalloc(ptr - str + 1);
5444 c = code[(*bgn)++];
5445
5446 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5447 s[i] = c;
5448
5449 s[i] = '\0';
5450
5451 return s;
5452}
5453
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005454static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005455{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005456#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005457 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005458 // Example: echo '[]ap' | dc
5459 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005460 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005461 return;
5462 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005463#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005464 while (*str) {
5465 int c = *str++;
5466 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005467 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005468 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005469 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005470 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005471 case 'a':
5472 bb_putchar('\a');
5473 break;
5474 case 'b':
5475 bb_putchar('\b');
5476 break;
5477 case '\\':
5478 case 'e':
5479 bb_putchar('\\');
5480 break;
5481 case 'f':
5482 bb_putchar('\f');
5483 break;
5484 case 'n':
5485 bb_putchar('\n');
5486 G.prog.nchars = SIZE_MAX;
5487 break;
5488 case 'r':
5489 bb_putchar('\r');
5490 break;
5491 case 'q':
5492 bb_putchar('"');
5493 break;
5494 case 't':
5495 bb_putchar('\t');
5496 break;
5497 default:
5498 // Just print the backslash and following character.
5499 bb_putchar('\\');
5500 ++G.prog.nchars;
5501 bb_putchar(c);
5502 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005503 }
5504 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005505 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005506 }
5507}
5508
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005509static void bc_num_printNewline(void)
5510{
5511 if (G.prog.nchars == G.prog.len - 1) {
5512 bb_putchar('\\');
5513 bb_putchar('\n');
5514 G.prog.nchars = 0;
5515 }
5516}
5517
5518#if ENABLE_DC
5519static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5520{
5521 (void) radix;
5522 bb_putchar((char) num);
5523 G.prog.nchars += width;
5524}
5525#endif
5526
5527static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5528{
5529 size_t exp, pow;
5530
5531 bc_num_printNewline();
5532 bb_putchar(radix ? '.' : ' ');
5533 ++G.prog.nchars;
5534
5535 bc_num_printNewline();
5536 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5537 continue;
5538
5539 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5540 size_t dig;
5541 bc_num_printNewline();
5542 dig = num / pow;
5543 num -= dig * pow;
5544 bb_putchar(((char) dig) + '0');
5545 }
5546}
5547
5548static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5549{
5550 if (radix) {
5551 bc_num_printNewline();
5552 bb_putchar('.');
5553 G.prog.nchars += 1;
5554 }
5555
5556 bc_num_printNewline();
5557 bb_putchar(bb_hexdigits_upcase[num]);
5558 G.prog.nchars += width;
5559}
5560
5561static void bc_num_printDecimal(BcNum *n)
5562{
5563 size_t i, rdx = n->rdx - 1;
5564
5565 if (n->neg) bb_putchar('-');
5566 G.prog.nchars += n->neg;
5567
5568 for (i = n->len - 1; i < n->len; --i)
5569 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5570}
5571
5572static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5573{
5574 BcStatus s;
5575 BcVec stack;
5576 BcNum intp, fracp, digit, frac_len;
5577 unsigned long dig, *ptr;
5578 size_t i;
5579 bool radix;
5580
5581 if (n->len == 0) {
5582 print(0, width, false);
5583 RETURN_STATUS(BC_STATUS_SUCCESS);
5584 }
5585
5586 bc_vec_init(&stack, sizeof(long), NULL);
5587 bc_num_init(&intp, n->len);
5588 bc_num_init(&fracp, n->rdx);
5589 bc_num_init(&digit, width);
5590 bc_num_init(&frac_len, BC_NUM_INT(n));
5591 bc_num_copy(&intp, n);
5592 bc_num_one(&frac_len);
5593
5594 bc_num_truncate(&intp, intp.rdx);
5595 s = zbc_num_sub(n, &intp, &fracp, 0);
5596 if (s) goto err;
5597
5598 while (intp.len != 0) {
5599 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5600 if (s) goto err;
5601 s = zbc_num_ulong(&digit, &dig);
5602 if (s) goto err;
5603 bc_vec_push(&stack, &dig);
5604 }
5605
5606 for (i = 0; i < stack.len; ++i) {
5607 ptr = bc_vec_item_rev(&stack, i);
5608 print(*ptr, width, false);
5609 }
5610
5611 if (!n->rdx) goto err;
5612
5613 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5614 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5615 if (s) goto err;
5616 s = zbc_num_ulong(&fracp, &dig);
5617 if (s) goto err;
5618 bc_num_ulong2num(&intp, dig);
5619 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5620 if (s) goto err;
5621 print(dig, width, radix);
5622 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5623 if (s) goto err;
5624 }
5625
5626err:
5627 bc_num_free(&frac_len);
5628 bc_num_free(&digit);
5629 bc_num_free(&fracp);
5630 bc_num_free(&intp);
5631 bc_vec_free(&stack);
5632 RETURN_STATUS(s);
5633}
5634#if ERRORS_ARE_FATAL
5635# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5636#endif
5637
5638static BC_STATUS zbc_num_printBase(BcNum *n)
5639{
5640 BcStatus s;
5641 size_t width, i;
5642 BcNumDigitOp print;
5643 bool neg = n->neg;
5644
5645 if (neg) {
5646 bb_putchar('-');
5647 G.prog.nchars++;
5648 }
5649
5650 n->neg = false;
5651
5652 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5653 width = 1;
5654 print = bc_num_printHex;
5655 }
5656 else {
5657 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5658 continue;
5659 print = bc_num_printDigits;
5660 }
5661
5662 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5663 n->neg = neg;
5664
5665 RETURN_STATUS(s);
5666}
5667#if ERRORS_ARE_FATAL
5668# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5669#endif
5670
5671#if ENABLE_DC
5672static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5673{
5674 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5675}
5676#if ERRORS_ARE_FATAL
5677# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5678#endif
5679#endif
5680
5681static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5682{
5683 BcStatus s = BC_STATUS_SUCCESS;
5684
5685 bc_num_printNewline();
5686
5687 if (n->len == 0) {
5688 bb_putchar('0');
5689 ++G.prog.nchars;
5690 }
5691 else if (G.prog.ob_t == 10)
5692 bc_num_printDecimal(n);
5693 else
5694 s = zbc_num_printBase(n);
5695
5696 if (newline) {
5697 bb_putchar('\n');
5698 G.prog.nchars = 0;
5699 }
5700
5701 RETURN_STATUS(s);
5702}
5703#if ERRORS_ARE_FATAL
5704# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5705#endif
5706
Denys Vlasenko29301232018-12-11 15:29:32 +01005707static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005708{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005709 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005710 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005711 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005712 bool pop = inst != BC_INST_PRINT;
5713
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005714 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005715 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005716
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005717 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005718 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005719 s = zbc_program_num(r, &num, false);
5720 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005721
5722 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005723 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005724 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005725 }
5726 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005727 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005728
5729 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005730 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005731
5732 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005733 for (;;) {
5734 char c = *str++;
5735 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005736 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005737 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005738 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005739 }
5740 }
5741 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005742 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005743 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005744 }
5745 }
5746
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005747 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005748
Denys Vlasenko29301232018-12-11 15:29:32 +01005749 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005750}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005751#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005752# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005753#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005754
Denys Vlasenko29301232018-12-11 15:29:32 +01005755static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005756{
5757 BcStatus s;
5758 BcResult res, *ptr;
5759 BcNum *num = NULL;
5760
Denys Vlasenko29301232018-12-11 15:29:32 +01005761 s = zbc_program_prep(&ptr, &num);
5762 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005763
5764 bc_num_init(&res.d.n, num->len);
5765 bc_num_copy(&res.d.n, num);
5766 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5767
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005768 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005769
Denys Vlasenko29301232018-12-11 15:29:32 +01005770 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005771}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005772#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005773# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005774#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005775
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005776static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005777{
5778 BcStatus s;
5779 BcResult *opd1, *opd2, res;
5780 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005781 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005782
Denys Vlasenko29301232018-12-11 15:29:32 +01005783 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005784 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005785
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005786 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005787
5788 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005789 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005791 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005792 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005793 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005794 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005795 case BC_INST_REL_EQ:
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_LE:
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_GE:
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_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005805 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005806 break;
5807 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005808 cond = (cond > 0);
5809 break;
5810 default: // = case BC_INST_REL_NE:
5811 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005812 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005813 }
5814 }
5815
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005816 if (cond) bc_num_one(&res.d.n);
5817 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005818
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005819 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005820
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005821 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005822}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005823#if ERRORS_ARE_FATAL
5824# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5825#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005826
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005827#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005828static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005829 bool push)
5830{
5831 BcNum n2;
5832 BcResult res;
5833
5834 memset(&n2, 0, sizeof(BcNum));
5835 n2.rdx = res.d.id.idx = r->d.id.idx;
5836 res.t = BC_RESULT_STR;
5837
5838 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005839 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005840 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005842 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005843 }
5844
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005845 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005846
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005847 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848 bc_vec_push(v, &n2);
5849
Denys Vlasenko29301232018-12-11 15:29:32 +01005850 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005851}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005852#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005853# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005854#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005855#endif // ENABLE_DC
5856
Denys Vlasenko29301232018-12-11 15:29:32 +01005857static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005858{
5859 BcStatus s;
5860 BcResult *ptr, r;
5861 BcVec *v;
5862 BcNum *n;
5863
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005864 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005865 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005866
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005867 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005868 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005869 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005870 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005871
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005872#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005873 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005874 RETURN_STATUS(bc_error_variable_is_wrong_type());
5875 if (ptr->t == BC_RESULT_STR)
5876 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005877#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005878
Denys Vlasenko29301232018-12-11 15:29:32 +01005879 s = zbc_program_num(ptr, &n, false);
5880 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005881
5882 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005883 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884
5885 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005886 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005887 bc_num_copy(&r.d.n, n);
5888 }
5889 else {
5890 bc_array_init(&r.d.v, true);
5891 bc_array_copy(&r.d.v, (BcVec *) n);
5892 }
5893
5894 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005895 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005896
Denys Vlasenko29301232018-12-11 15:29:32 +01005897 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005898}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005899#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005900# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005901#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005902
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005903static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005904{
5905 BcStatus s;
5906 BcResult *left, *right, res;
5907 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005908 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5909
Denys Vlasenko29301232018-12-11 15:29:32 +01005910 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005911 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
5913 ib = left->t == BC_RESULT_IBASE;
5914 sc = left->t == BC_RESULT_SCALE;
5915
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005916#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005917
5918 if (right->t == BC_RESULT_STR) {
5919
5920 BcVec *v;
5921
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005922 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005923 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005924 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005925
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005926 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005927 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005928#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005929
5930 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005931 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005932 " left side must be scale,"
5933 " ibase, obase, last, var,"
5934 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005935 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06005936
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005937#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005938 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005939 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005940
5941 if (assign)
5942 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005943 else {
5944 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005945 ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005946 }
5947 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005948#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005949 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005950#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005951
5952 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005953 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005954 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5955 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5956 NULL, //can't happen //BC_RESULT_LAST
5957 NULL, //can't happen //BC_RESULT_CONSTANT
5958 NULL, //can't happen //BC_RESULT_ONE
5959 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005960 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005961 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005962 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005963
Denys Vlasenko29301232018-12-11 15:29:32 +01005964 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005965 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005966 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005967 if (sc) {
5968 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005969 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005970 }
5971 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005972 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005973 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005974 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005975 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005976 }
5977
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005978 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005979 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005980 if (!sc)
5981 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
5983 *ptr = (size_t) val;
5984 s = BC_STATUS_SUCCESS;
5985 }
5986
5987 bc_num_init(&res.d.n, l->len);
5988 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005989 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005990
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005991 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005992}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005993#if ERRORS_ARE_FATAL
5994# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
5995#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005996
Denys Vlasenko416ce762018-12-02 20:57:17 +01005997#if !ENABLE_DC
5998#define bc_program_pushVar(code, bgn, pop, copy) \
5999 bc_program_pushVar(code, bgn)
6000// for bc, 'pop' and 'copy' are always false
6001#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006002static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006003 bool pop, bool copy)
6004{
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 BcResult r;
6006 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
6008 r.t = BC_RESULT_VAR;
6009 r.d.id.name = name;
6010
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006011#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006012 {
6013 BcVec *v = bc_program_search(name, true);
6014 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko416ce762018-12-02 20:57:17 +01006016 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006017
Denys Vlasenko416ce762018-12-02 20:57:17 +01006018 if (!BC_PROG_STACK(v, 2 - copy)) {
6019 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006020 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006021 }
6022
Gavin Howard01055ba2018-11-03 11:00:21 -06006023 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006024 name = NULL;
6025
6026 if (!BC_PROG_STR(num)) {
6027
6028 r.t = BC_RESULT_TEMP;
6029
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006030 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006031 bc_num_copy(&r.d.n, num);
6032 }
6033 else {
6034 r.t = BC_RESULT_STR;
6035 r.d.id.idx = num->rdx;
6036 }
6037
6038 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006039 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006040 }
6041#endif // ENABLE_DC
6042
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006043 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006045 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006046}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006047#if ERRORS_ARE_FATAL
6048# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6049#else
6050# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6051#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006052
Denys Vlasenko29301232018-12-11 15:29:32 +01006053static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006054 char inst)
6055{
6056 BcStatus s = BC_STATUS_SUCCESS;
6057 BcResult r;
6058 BcNum *num;
6059
6060 r.d.id.name = bc_program_name(code, bgn);
6061
6062 if (inst == BC_INST_ARRAY) {
6063 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006064 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006065 }
6066 else {
6067
6068 BcResult *operand;
6069 unsigned long temp;
6070
Denys Vlasenko29301232018-12-11 15:29:32 +01006071 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006072 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006073 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006074 if (s) goto err;
6075
6076 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006077 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006078 goto err;
6079 }
6080
6081 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006082 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006083 }
6084
6085err:
6086 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006087 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006088}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006089#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006090# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006091#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006092
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006093#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006094static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006095{
6096 BcStatus s;
6097 BcResult *ptr, res, copy;
6098 BcNum *num = NULL;
6099 char inst2 = inst;
6100
Denys Vlasenko29301232018-12-11 15:29:32 +01006101 s = zbc_program_prep(&ptr, &num);
6102 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006103
6104 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6105 copy.t = BC_RESULT_TEMP;
6106 bc_num_init(&copy.d.n, num->len);
6107 bc_num_copy(&copy.d.n, num);
6108 }
6109
6110 res.t = BC_RESULT_ONE;
6111 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6112 BC_INST_ASSIGN_PLUS :
6113 BC_INST_ASSIGN_MINUS;
6114
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006115 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006116 s = zbc_program_assign(inst);
6117 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006118
6119 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006120 bc_vec_pop(&G.prog.results);
6121 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122 }
6123
Denys Vlasenko29301232018-12-11 15:29:32 +01006124 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006126#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006127# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006128#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006129
Denys Vlasenko29301232018-12-11 15:29:32 +01006130static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006131{
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 BcInstPtr ip;
6133 size_t i, nparams = bc_program_index(code, idx);
6134 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006135 BcId *a;
6136 BcResultData param;
6137 BcResult *arg;
6138
6139 ip.idx = 0;
6140 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006141 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006142
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006143 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006144 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006145 }
6146 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006147 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006148 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006149 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
6151 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006152 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006153
6154 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006155 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006156
6157 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006158 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006159
Denys Vlasenko29301232018-12-11 15:29:32 +01006160 s = zbc_program_copyToVar(a->name, a->idx);
6161 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006162 }
6163
6164 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006165 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006166
6167 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006168 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006169
6170 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006171 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006172 bc_vec_push(v, &param.n);
6173 }
6174 else {
6175 bc_array_init(&param.v, true);
6176 bc_vec_push(v, &param.v);
6177 }
6178 }
6179
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006180 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181
Denys Vlasenko29301232018-12-11 15:29:32 +01006182 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006183}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006184#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006185# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006186#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006187
Denys Vlasenko29301232018-12-11 15:29:32 +01006188static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006189{
Gavin Howard01055ba2018-11-03 11:00:21 -06006190 BcResult res;
6191 BcFunc *f;
6192 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006193 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006194
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006195 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006196 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006197
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006198 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006199 res.t = BC_RESULT_TEMP;
6200
6201 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006202 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006204 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006205
Denys Vlasenko29301232018-12-11 15:29:32 +01006206 s = zbc_program_num(operand, &num, false);
6207 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006208 bc_num_init(&res.d.n, num->len);
6209 bc_num_copy(&res.d.n, num);
6210 }
6211 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006212 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006213 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006214 }
6215
6216 // We need to pop arguments as well, so this takes that into account.
6217 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006218 BcVec *v;
6219 BcId *a = bc_vec_item(&f->autos, i);
6220
Denys Vlasenkodf515392018-12-02 19:27:48 +01006221 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 bc_vec_pop(v);
6223 }
6224
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006225 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6226 bc_vec_push(&G.prog.results, &res);
6227 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006228
Denys Vlasenko29301232018-12-11 15:29:32 +01006229 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006230}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006231#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006232# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006233#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006234#endif // ENABLE_BC
6235
6236static unsigned long bc_program_scale(BcNum *n)
6237{
6238 return (unsigned long) n->rdx;
6239}
6240
6241static unsigned long bc_program_len(BcNum *n)
6242{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006243 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006244
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006245 if (n->rdx != len) return len;
6246 for (;;) {
6247 if (len == 0) break;
6248 len--;
6249 if (n->num[len] != 0) break;
6250 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006251 return len;
6252}
6253
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006254static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006255{
6256 BcStatus s;
6257 BcResult *opnd;
6258 BcNum *num = NULL;
6259 BcResult res;
6260 bool len = inst == BC_INST_LENGTH;
6261
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006262 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006263 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006264 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265
Denys Vlasenko29301232018-12-11 15:29:32 +01006266 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006267 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006268
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006269#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006270 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006271 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006272#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006273
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006274 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006275
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006276 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006277#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006278 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006279 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006280 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006281#endif
6282#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006283 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006284 char **str;
6285 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6286
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006287 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006288 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006290#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006291 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006292 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006293 }
6294
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006295 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006296
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006297 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006298}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006299#if ERRORS_ARE_FATAL
6300# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6301#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006302
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006303#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006304static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006305{
6306 BcStatus s;
6307 BcResult *opd1, *opd2, res, res2;
6308 BcNum *n1, *n2 = NULL;
6309
Denys Vlasenko29301232018-12-11 15:29:32 +01006310 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006311 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006312
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006313 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006314 bc_num_init(&res2.d.n, n2->len);
6315
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006316 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006317 if (s) goto err;
6318
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006319 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006320 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006321 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006322
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006323 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324
6325err:
6326 bc_num_free(&res2.d.n);
6327 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006328 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006329}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006330#if ERRORS_ARE_FATAL
6331# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6332#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006334static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006335{
6336 BcStatus s;
6337 BcResult *r1, *r2, *r3, res;
6338 BcNum *n1, *n2, *n3;
6339
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006340 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006341 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006342 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006343 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006344
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006345 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006346 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006347 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006348 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006349 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006350
6351 // Make sure that the values have their pointers updated, if necessary.
6352 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6353
6354 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006355 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006356 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006357 }
6358
6359 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006360 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006361 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 }
6363 }
6364
6365 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006366 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006367 if (s) goto err;
6368
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006369 bc_vec_pop(&G.prog.results);
6370 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006371
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006372 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006373
6374err:
6375 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006376 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006377}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006378#if ERRORS_ARE_FATAL
6379# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6380#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006381
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006382static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006383{
Gavin Howard01055ba2018-11-03 11:00:21 -06006384 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006385 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006386
6387 res.t = BC_RESULT_TEMP;
6388
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006389 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006390 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006391 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006392}
6393
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006394static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006395{
6396 BcStatus s;
6397 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006398 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006400 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006401 unsigned long val;
6402
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006403 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006404 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006406
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006407 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006408 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006409 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006410
6411 if (BC_PROG_NUM(r, num)) {
6412
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006413 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006414 bc_num_copy(&n, num);
6415 bc_num_truncate(&n, n.rdx);
6416
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006417 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006419 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006420 if (s) goto num_err;
6421
6422 c = (char) val;
6423
6424 bc_num_free(&n);
6425 }
6426 else {
6427 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006428 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006429 c = str2[0];
6430 }
6431
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006432 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006433 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006434 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006435
6436 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006437 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006438
6439 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006440 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006441 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006442 len = idx;
6443 break;
6444 }
6445 }
6446
6447 free(str);
6448 }
6449 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006450 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006451
6452 res.t = BC_RESULT_STR;
6453 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006454 bc_vec_pop(&G.prog.results);
6455 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006456
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006457 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006458
6459num_err:
6460 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006461 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006462}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006463#if ERRORS_ARE_FATAL
6464# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6465#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006466
Denys Vlasenko29301232018-12-11 15:29:32 +01006467static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006468{
6469 BcStatus s;
6470 BcResult *r;
6471 BcNum *n = NULL;
6472 size_t idx;
6473 char *str;
6474
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006475 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006476 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006477 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006478
Denys Vlasenko29301232018-12-11 15:29:32 +01006479 s = zbc_program_num(r, &n, false);
6480 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481
6482 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006483 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006484 else {
6485 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006486 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006487 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006488 }
6489
Denys Vlasenko29301232018-12-11 15:29:32 +01006490 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006491}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006492#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006493# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006494#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006495
Denys Vlasenko29301232018-12-11 15:29:32 +01006496static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006497{
6498 BcStatus s;
6499 BcResult *opnd;
6500 BcNum *num = NULL;
6501 unsigned long val;
6502
Denys Vlasenko29301232018-12-11 15:29:32 +01006503 s = zbc_program_prep(&opnd, &num);
6504 if (s) RETURN_STATUS(s);
6505 s = zbc_num_ulong(num, &val);
6506 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006507
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006508 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006509
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006510 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006511 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006512 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006513 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006514 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006515
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006516 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517
Denys Vlasenko29301232018-12-11 15:29:32 +01006518 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006519}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006520#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006521# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006522#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006523
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006524static BC_STATUS zbc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006525 bool cond)
6526{
6527 BcStatus s = BC_STATUS_SUCCESS;
6528 BcResult *r;
6529 char **str;
6530 BcFunc *f;
6531 BcParse prs;
6532 BcInstPtr ip;
6533 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006534
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006535 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006536 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006537
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006538 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006539
6540 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006541 BcNum *n = n; // for compiler
6542 bool exec;
6543 char *name;
6544 char *then_name = bc_program_name(code, bgn);
6545 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006546
6547 if (code[*bgn] == BC_PARSE_STREND)
6548 (*bgn) += 1;
6549 else
6550 else_name = bc_program_name(code, bgn);
6551
6552 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006553 name = then_name;
6554 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006555 exec = true;
6556 name = else_name;
6557 }
6558
6559 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006560 BcVec *v;
6561 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006562 n = bc_vec_top(v);
6563 }
6564
6565 free(then_name);
6566 free(else_name);
6567
6568 if (!exec) goto exit;
6569 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006570 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006571 goto exit;
6572 }
6573
6574 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006575 } else {
6576 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006577 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006578 } else if (r->t == BC_RESULT_VAR) {
6579 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006580 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 if (s || !BC_PROG_STR(n)) goto exit;
6582 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006583 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006584 goto exit;
6585 }
6586
6587 fidx = sidx + BC_PROG_REQ_FUNCS;
6588
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006589 str = bc_program_str(sidx);
6590 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006591
6592 if (f->code.len == 0) {
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006593 bc_parse_create(&prs, fidx);
Denys Vlasenko26819db2018-12-12 16:08:46 +01006594 s = zbc_parse_text(&prs, *str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006596 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 if (s) goto err;
6598
6599 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006600 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006601 goto err;
6602 }
6603
6604 bc_parse_free(&prs);
6605 }
6606
6607 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006608 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006609 ip.func = fidx;
6610
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006611 bc_vec_pop(&G.prog.results);
6612 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006613
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006614 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006615
6616err:
6617 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006618 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006619 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006620exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006621 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006622 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006623}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006624#if ERRORS_ARE_FATAL
6625# define zbc_program_execStr(...) (zbc_program_execStr(__VA_ARGS__), BC_STATUS_SUCCESS)
6626#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006627#endif // ENABLE_DC
6628
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006629static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006630{
Gavin Howard01055ba2018-11-03 11:00:21 -06006631 BcResult res;
6632 unsigned long val;
6633
6634 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6635 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006636 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006637 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006638 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006639 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006640 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006641
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006642 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006643 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006644 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006645}
6646
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006647static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006648{
Gavin Howard01055ba2018-11-03 11:00:21 -06006649 BcId entry, *entry_ptr;
6650 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006651 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006652
6653 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006654 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006655
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006656 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6657 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006658
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006659 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006660 *idx = entry_ptr->idx;
6661
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006662 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006663
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006664 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006665
6666 // We need to reset these, so the function can be repopulated.
6667 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006668 bc_vec_pop_all(&func->autos);
6669 bc_vec_pop_all(&func->code);
6670 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006671 }
6672 else {
6673 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006674 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006675 }
6676}
6677
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006678static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006679{
Gavin Howard01055ba2018-11-03 11:00:21 -06006680 BcResult r, *ptr;
6681 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006682 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006683 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006684 char *code = func->code.v;
6685 bool cond = false;
6686
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006687 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006688 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006689 char inst = code[(ip->idx)++];
6690
6691 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006692#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006693 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006694 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006695 if (s) RETURN_STATUS(s);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006696 cond = !bc_num_cmp(num, &G.prog.zero);
6697 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006698 // Fallthrough.
6699 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006701 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006702 addr = bc_vec_item(&func->labels, idx);
6703 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6704 break;
6705 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006706 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006707 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006708 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 case BC_INST_INC_PRE:
6710 case BC_INST_DEC_PRE:
6711 case BC_INST_INC_POST:
6712 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006713 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006714 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006715 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006716 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006717 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006718 case BC_INST_RET:
6719 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006720 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006721 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006722 case BC_INST_BOOL_OR:
6723 case BC_INST_BOOL_AND:
6724#endif // ENABLE_BC
6725 case BC_INST_REL_EQ:
6726 case BC_INST_REL_LE:
6727 case BC_INST_REL_GE:
6728 case BC_INST_REL_NE:
6729 case BC_INST_REL_LT:
6730 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006731 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006732 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006733 case BC_INST_READ:
Denys Vlasenko26819db2018-12-12 16:08:46 +01006734 s = zbc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006735 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006736 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006737 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006738 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006739 case BC_INST_ARRAY_ELEM:
6740 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006741 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006742 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006743 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006744 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006745 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006746 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006747 case BC_INST_IBASE:
6748 case BC_INST_SCALE:
6749 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006750 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006751 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006752 case BC_INST_SCALE_FUNC:
6753 case BC_INST_LENGTH:
6754 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006755 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006756 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006757 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006758 r.t = BC_RESULT_CONSTANT;
6759 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006760 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006761 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006763 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006764 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006765 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006766 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006768 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006769 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006770 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006771 case BC_INST_PRINT:
6772 case BC_INST_PRINT_POP:
6773 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006774 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006775 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006776 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006777 r.t = BC_RESULT_STR;
6778 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006779 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006780 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006781 case BC_INST_POWER:
6782 case BC_INST_MULTIPLY:
6783 case BC_INST_DIVIDE:
6784 case BC_INST_MODULUS:
6785 case BC_INST_PLUS:
6786 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006787 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006788 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006789 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006790 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006791 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006792 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006793 if (!bc_num_cmp(num, &G.prog.zero))
6794 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006795 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006796 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006797 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006798 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006799 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006800 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006801#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006802 case BC_INST_ASSIGN_POWER:
6803 case BC_INST_ASSIGN_MULTIPLY:
6804 case BC_INST_ASSIGN_DIVIDE:
6805 case BC_INST_ASSIGN_MODULUS:
6806 case BC_INST_ASSIGN_PLUS:
6807 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006808#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006809 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006810 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006811 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006812#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006814 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006815 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006816 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006817 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006818 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006819 case BC_INST_EXECUTE:
6820 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006821 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006822 s = zbc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006823 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006824 case BC_INST_PRINT_STACK: {
6825 size_t idx;
6826 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006827 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006828 if (s) break;
6829 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006830 break;
6831 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006833 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006834 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006835 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006836 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006837 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006838 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006839 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006840 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006841 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006842 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006843 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006845 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006846 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006847 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006848 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006849 ptr = bc_vec_item_rev(&G.prog.results, 0);
6850 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006851 memcpy(&r, ptr, sizeof(BcResult));
6852 memcpy(ptr, ptr2, sizeof(BcResult));
6853 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 break;
6855 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006857 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006859 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006860 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006861 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006862 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006863 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006864 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006865 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006866 break;
6867 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006868 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006869 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006870 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006871 free(name);
6872 break;
6873 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006875 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006876 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006877 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006878 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006879 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006880 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006881 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006882#endif // ENABLE_DC
6883 }
6884
Denys Vlasenkod38af482018-12-04 19:11:02 +01006885 if (s || G_interrupt) {
6886 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006887 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006888 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006889
6890 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006891 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006892 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006893 code = func->code.v;
6894 }
6895
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006896 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006897}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006898#if ERRORS_ARE_FATAL
6899# define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
6900#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006901
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006902static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006903{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006904 char *lenv;
6905 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006906
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006907 lenv = getenv(var);
6908 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006909 if (!lenv) return len;
6910
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006911 len = bb_strtou(lenv, NULL, 10) - 1;
6912 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006913 len = BC_NUM_PRINT_WIDTH;
6914
6915 return len;
6916}
6917
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006918static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006919{
Denys Vlasenko26819db2018-12-12 16:08:46 +01006920 BcStatus s = zbc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06006921
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006922 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006923
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006924 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01006925 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006926 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006927 }
6928
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006929 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006930 s = zbc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01006931 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01006932 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01006933 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06006934 }
6935
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006936 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006937}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006938#if ERRORS_ARE_FATAL
6939# define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__), BC_STATUS_SUCCESS)
6940#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006941
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006942static BC_STATUS zbc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06006943{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006944 // So far bc/dc have no way to include a file from another file,
6945 // therefore we know G.prog.file == NULL on entry
6946 //const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06006947 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006948 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006949 BcFunc *main_func;
6950 BcInstPtr *ip;
6951
Denys Vlasenkodf515392018-12-02 19:27:48 +01006952 data = bc_read_file(file);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006953 if (!data) RETURN_STATUS(bc_error_fmt("file '%s' is not text", file));
Gavin Howard01055ba2018-11-03 11:00:21 -06006954
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006955 //sv_file = G.prog.file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006956 G.prog.file = file;
6957 bc_lex_file(&G.prs.l);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006958 s = zbc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06006959 if (s) goto err;
6960
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006961 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006962 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006963
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006964 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006965 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06006966
6967err:
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006968 //G.prog.file = sv_file;
6969 G.prog.file = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006970 free(data);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006971 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006972}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006973#if ERRORS_ARE_FATAL
6974# define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__), BC_STATUS_SUCCESS)
6975#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006976
Denys Vlasenko19f11072018-12-12 22:48:19 +01006977static BC_STATUS zbc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006978{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01006979 BcStatus s;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01006980 BcVec buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01006981 size_t str;
6982 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06006983
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006984 //G.prog.file = NULL; - already is
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006985 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006986
Denys Vlasenko7d628012018-12-04 21:46:47 +01006987 bc_char_vec_init(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06006988
6989 // This loop is complex because the vm tries not to send any lines that end
6990 // with a backslash to the parser. The reason for that is because the parser
6991 // treats a backslash+newline combo as whitespace, per the bc spec. In that
6992 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko8a892472018-12-12 22:43:58 +01006993 s = BC_STATUS_SUCCESS;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01006994 comment = false;
6995 str = 0;
Denys Vlasenko8a892472018-12-12 22:43:58 +01006996 for (;;) {
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01006997 size_t prevlen = buffer.len;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01006998 char *string;
Gavin Howard01055ba2018-11-03 11:00:21 -06006999
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007000 bc_read_line(&buffer);
7001 // No more input means EOF
7002 if (buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenko8a892472018-12-12 22:43:58 +01007003 break;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007004
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007005 string = buffer.v + prevlen;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007006 while (*string) {
7007 char c = *string;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007008 if (string == buffer.v || string[-1] != '\\') {
Denys Vlasenkobbcecc42018-12-13 21:17:43 +01007009 if (IS_BC)
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007010 str ^= (c == '"');
Denys Vlasenkobbcecc42018-12-13 21:17:43 +01007011 else {
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007012 if (c == ']')
7013 str -= 1;
7014 else if (c == '[')
7015 str += 1;
Denys Vlasenko766f6722018-12-13 17:23:24 +01007016 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007017 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007018 string++;
7019 if (c == '/' && *string == '*') {
7020 comment = true;
7021 string++;
Gavin Howard01055ba2018-11-03 11:00:21 -06007022 continue;
7023 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007024 if (c == '*' && *string == '/') {
7025 comment = false;
7026 string++;
7027 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007028 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007029 if (str || comment) {
7030 buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007031 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007032 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007033
7034 // Check for backslash+newline.
7035 // we do not check that last char is '\n' -
7036 // if it is not, then it's EOF, and looping back
7037 // to bc_read_line() will detect it:
7038 string -= 2;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007039 if (string >= buffer.v && *string == '\\') {
7040 buffer.len--;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007041 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007042 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007043
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007044 s = zbc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007045 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007046 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007047 // Debug config, non-interactive mode:
7048 // return all the way back to main.
7049 // Non-debug builds do not come here, they exit.
7050 break;
7051 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007052 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007053
Denys Vlasenko7d628012018-12-04 21:46:47 +01007054 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007055 }
7056
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007057 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007058 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007059 }
7060 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007061 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007062 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007063
Gavin Howard01055ba2018-11-03 11:00:21 -06007064 bc_vec_free(&buffer);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007065 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007066}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007067#if ERRORS_ARE_FATAL
7068# define zbc_vm_stdin(...) (zbc_vm_stdin(__VA_ARGS__), BC_STATUS_SUCCESS)
7069#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007070
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007071#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01007072static void bc_vm_info(void)
7073{
7074 printf("%s "BB_VER"\n"
7075 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
7076 , applet_name);
7077}
7078
7079static void bc_args(char **argv)
7080{
7081 unsigned opts;
7082 int i;
7083
7084 GETOPT_RESET();
7085#if ENABLE_FEATURE_BC_LONG_OPTIONS
7086 opts = option_mask32 |= getopt32long(argv, "wvsqli",
7087 "warn\0" No_argument "w"
7088 "version\0" No_argument "v"
7089 "standard\0" No_argument "s"
7090 "quiet\0" No_argument "q"
7091 "mathlib\0" No_argument "l"
7092 "interactive\0" No_argument "i"
7093 );
7094#else
7095 opts = option_mask32 |= getopt32(argv, "wvsqli");
7096#endif
7097 if (getenv("POSIXLY_CORRECT"))
7098 option_mask32 |= BC_FLAG_S;
7099
7100 if (opts & BC_FLAG_V) {
7101 bc_vm_info();
7102 exit(0);
7103 }
7104
7105 for (i = optind; argv[i]; ++i)
7106 bc_vec_push(&G.files, argv + i);
7107}
7108
7109static void bc_vm_envArgs(void)
7110{
7111 BcVec v;
7112 char *buf;
7113 char *env_args = getenv("BC_ENV_ARGS");
7114
7115 if (!env_args) return;
7116
7117 G.env_args = xstrdup(env_args);
7118 buf = G.env_args;
7119
7120 bc_vec_init(&v, sizeof(char *), NULL);
7121
7122 while (*(buf = skip_whitespace(buf)) != '\0') {
7123 bc_vec_push(&v, &buf);
7124 buf = skip_non_whitespace(buf);
7125 if (!*buf)
7126 break;
7127 *buf++ = '\0';
7128 }
7129
7130 // NULL terminate, and pass argv[] so that first arg is argv[1]
7131 if (sizeof(int) == sizeof(char*)) {
7132 bc_vec_push(&v, &const_int_0);
7133 } else {
7134 static char *const nullptr = NULL;
7135 bc_vec_push(&v, &nullptr);
7136 }
7137 bc_args(((char **)v.v) - 1);
7138
7139 bc_vec_free(&v);
7140}
7141
7142static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007143 "scale=20"
7144"\n" "define e(x){"
7145"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007146////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7147//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7148//e(-.998896): GNU:.36828580434569428695
7149// above code:.36828580434569428696
7150// actual value:.3682858043456942869594...
7151// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007152"\n" "b=ibase"
7153"\n" "ibase=A"
7154"\n" "if(x<0){"
7155"\n" "n=1"
7156"\n" "x=-x"
7157"\n" "}"
7158"\n" "s=scale"
7159"\n" "r=6+s+0.44*x"
7160"\n" "scale=scale(x)+1"
7161"\n" "while(x>1){"
7162"\n" "d+=1"
7163"\n" "x/=2"
7164"\n" "scale+=1"
7165"\n" "}"
7166"\n" "scale=r"
7167"\n" "r=x+1"
7168"\n" "p=x"
7169"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007170"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007171"\n" "p*=x"
7172"\n" "f*=i"
7173"\n" "v=p/f"
7174"\n" "r+=v"
7175"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007176"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007177"\n" "scale=s"
7178"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007179"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007180"\n" "return(r/1)"
7181"\n" "}"
7182"\n" "define l(x){"
7183"\n" "auto b,s,r,p,a,q,i,v"
7184"\n" "b=ibase"
7185"\n" "ibase=A"
7186"\n" "if(x<=0){"
7187"\n" "r=(1-10^scale)/1"
7188"\n" "ibase=b"
7189"\n" "return(r)"
7190"\n" "}"
7191"\n" "s=scale"
7192"\n" "scale+=6"
7193"\n" "p=2"
7194"\n" "while(x>=2){"
7195"\n" "p*=2"
7196"\n" "x=sqrt(x)"
7197"\n" "}"
7198"\n" "while(x<=0.5){"
7199"\n" "p*=2"
7200"\n" "x=sqrt(x)"
7201"\n" "}"
7202"\n" "r=a=(x-1)/(x+1)"
7203"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007204"\n" "v=1"
7205"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007206"\n" "a*=q"
7207"\n" "v=a/i"
7208"\n" "r+=v"
7209"\n" "}"
7210"\n" "r*=p"
7211"\n" "scale=s"
7212"\n" "ibase=b"
7213"\n" "return(r/1)"
7214"\n" "}"
7215"\n" "define s(x){"
7216"\n" "auto b,s,r,n,a,q,i"
7217"\n" "b=ibase"
7218"\n" "ibase=A"
7219"\n" "s=scale"
7220"\n" "scale=1.1*s+2"
7221"\n" "a=a(1)"
7222"\n" "if(x<0){"
7223"\n" "n=1"
7224"\n" "x=-x"
7225"\n" "}"
7226"\n" "scale=0"
7227"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007228"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007229"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007230"\n" "scale=s+2"
7231"\n" "r=a=x"
7232"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007233"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007234"\n" "a*=q/(i*(i-1))"
7235"\n" "r+=a"
7236"\n" "}"
7237"\n" "scale=s"
7238"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007239"\n" "if(n)return(-r/1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007240"\n" "return(r/1)"
7241"\n" "}"
7242"\n" "define c(x){"
7243"\n" "auto b,s"
7244"\n" "b=ibase"
7245"\n" "ibase=A"
7246"\n" "s=scale"
7247"\n" "scale*=1.2"
7248"\n" "x=s(2*a(1)+x)"
7249"\n" "scale=s"
7250"\n" "ibase=b"
7251"\n" "return(x/1)"
7252"\n" "}"
7253"\n" "define a(x){"
7254"\n" "auto b,s,r,n,a,m,t,f,i,u"
7255"\n" "b=ibase"
7256"\n" "ibase=A"
7257"\n" "n=1"
7258"\n" "if(x<0){"
7259"\n" "n=-1"
7260"\n" "x=-x"
7261"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007262"\n" "if(scale<65){"
7263"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7264"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007265"\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"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007279"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007280"\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"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007297"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007298"\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"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007305"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007306"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007307"\n" "v=v*f/i/(n+i)"
7308"\n" "r+=v"
7309"\n" "}"
7310"\n" "scale=s"
7311"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007312"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007313"\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 Vlasenko5f263f42018-12-13 22:49:59 +01007459 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007460 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007461 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007462
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007463//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7464//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007465 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007466#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007467 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007468 // With SA_RESTART, most system calls will restart
7469 // (IOW: they won't fail with EINTR).
7470 // In particular, this means ^C won't cause
7471 // stdout to get into "error state" if SIGINT hits
7472 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007473 //
7474 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007475 // will only be handled after [Enter] since read()
7476 // from stdin is not interrupted by ^C either,
7477 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007478 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007479 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7480
7481 // Without SA_RESTART, this exhibits a bug:
7482 // "while (1) print 1" and try ^C-ing it.
7483 // Intermittently, instead of returning to input line,
7484 // you'll get "output error: Interrupted system call"
7485 // and exit.
7486 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007487#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007488 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007489 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007490 return 0; // "not a tty"
7491}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007492
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007493static BcStatus bc_vm_run(void)
7494{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007495 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007496#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007497 if (G_exiting) // it was actually "halt" or "quit"
7498 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007499 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007500# if ENABLE_FEATURE_EDITING
7501 free_line_input_t(G.line_input_state);
7502# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007503 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007504#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007505 return st;
7506}
7507
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007508#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007509int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007510int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007511{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007512 int is_tty;
7513
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007514 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007515
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007516 is_tty = bc_vm_init("BC_LINE_LENGTH");
7517
7518 bc_args(argv);
7519
7520 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7521 bc_vm_info();
7522
7523 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007524}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007525#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007526
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007527#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007528int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007529int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007530{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007531 int noscript;
7532
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007533 INIT_G();
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007534 /*
7535 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7536 * 1 char wider than bc from the same package.
7537 * Both default width, and xC_LINE_LENGTH=N are wider:
7538 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7539 * 1234\
7540 * 56
7541 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7542 * 123\
7543 * 456
7544 * Do the same, or it's a bug?
7545 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007546 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007547
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007548 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7549 noscript = BC_FLAG_I;
7550 for (;;) {
7551 int n = getopt(argc, argv, "e:f:x");
7552 if (n <= 0)
7553 break;
7554 switch (n) {
7555 case 'e':
7556 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007557 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007558 if (n) return n;
7559 break;
7560 case 'f':
7561 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007562 n = zbc_vm_file(optarg);
7563 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007564 break;
7565 case 'x':
7566 option_mask32 |= DC_FLAG_X;
7567 break;
7568 default:
7569 bb_show_usage();
7570 }
7571 }
7572 argv += optind;
7573
7574 while (*argv) {
7575 noscript = 0;
7576 bc_vec_push(&G.files, argv++);
7577 }
7578
7579 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7580
7581 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007582}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007583#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007584
7585#endif // not DC_SMALL