blob: 7c4dfbb20c06b06d220f1fe43d94b5435bda097e [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010013//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060014//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010015//config: This bc has five differences to the GNU bc:
16//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config: 2) Arrays are copied before being passed as arguments to
18//config: functions. This behavior is required by the bc spec.
19//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010020//config: the number of elements in the array. This prints "1":
21//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010023//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060024//config: allows POSIX-compliant scripts to work while somewhat
25//config: preserving expected behavior (versus C) and making parsing
26//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config:
29//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: -q --quiet don't print version and copyright
32//config: -s --standard error if any non-POSIX extensions are used
33//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: s(expr) sine in radians
36//config: c(expr) cosine in radians
37//config: a(expr) arctangent, returning radians
38//config: l(expr) natural log
39//config: e(expr) raises e to the power of expr
40//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
42//config:config DC
43//config: bool "dc (38 kb; 49 kb when combined with bc)"
44//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
62//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010065//config: whitespace. Examples:
66//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060067//config: Also note that, like the FreeBSD dc, extended registers are not
68//config: allowed unless the "-x" option is given.
69//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010070//config:config FEATURE_DC_SMALL
71//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
72//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010073//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
78//config: depends on FEATURE_DC_SMALL
79//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010086//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100101//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600102//config: help
103//config: Enable long options for bc and dc.
104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100113//usage: "[-sqliw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100139//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100144//usage: "\np - print top of the stack (without popping)"
145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166#if ENABLE_FEATURE_DC_SMALL
167# include "dc.c"
168#else
169
Gavin Howard01055ba2018-11-03 11:00:21 -0600170typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100171 BC_STATUS_SUCCESS = 0,
172 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100173 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600174} BcStatus;
175
Gavin Howard01055ba2018-11-03 11:00:21 -0600176#define BC_VEC_INVALID_IDX ((size_t) -1)
177#define BC_VEC_START_CAP (1 << 5)
178
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100179typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600180
181typedef struct BcVec {
182 char *v;
183 size_t len;
184 size_t cap;
185 size_t size;
186 BcVecFree dtor;
187} BcVec;
188
Gavin Howard01055ba2018-11-03 11:00:21 -0600189typedef signed char BcDig;
190
191typedef struct BcNum {
192 BcDig *restrict num;
193 size_t rdx;
194 size_t len;
195 size_t cap;
196 bool neg;
197} BcNum;
198
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100199#define BC_NUM_MIN_BASE ((unsigned long) 2)
200#define BC_NUM_MAX_IBASE ((unsigned long) 16)
201// larger value might speed up BIGNUM calculations a bit:
202#define BC_NUM_DEF_SIZE (16)
203#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600204
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100205#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600206
Gavin Howard01055ba2018-11-03 11:00:21 -0600207typedef enum BcInst {
208
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100209#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600210 BC_INST_INC_PRE,
211 BC_INST_DEC_PRE,
212 BC_INST_INC_POST,
213 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100214#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600215
216 BC_INST_NEG,
217
218 BC_INST_POWER,
219 BC_INST_MULTIPLY,
220 BC_INST_DIVIDE,
221 BC_INST_MODULUS,
222 BC_INST_PLUS,
223 BC_INST_MINUS,
224
225 BC_INST_REL_EQ,
226 BC_INST_REL_LE,
227 BC_INST_REL_GE,
228 BC_INST_REL_NE,
229 BC_INST_REL_LT,
230 BC_INST_REL_GT,
231
232 BC_INST_BOOL_NOT,
233 BC_INST_BOOL_OR,
234 BC_INST_BOOL_AND,
235
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100236#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600237 BC_INST_ASSIGN_POWER,
238 BC_INST_ASSIGN_MULTIPLY,
239 BC_INST_ASSIGN_DIVIDE,
240 BC_INST_ASSIGN_MODULUS,
241 BC_INST_ASSIGN_PLUS,
242 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100243#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600244 BC_INST_ASSIGN,
245
246 BC_INST_NUM,
247 BC_INST_VAR,
248 BC_INST_ARRAY_ELEM,
249 BC_INST_ARRAY,
250
251 BC_INST_SCALE_FUNC,
252 BC_INST_IBASE,
253 BC_INST_SCALE,
254 BC_INST_LAST,
255 BC_INST_LENGTH,
256 BC_INST_READ,
257 BC_INST_OBASE,
258 BC_INST_SQRT,
259
260 BC_INST_PRINT,
261 BC_INST_PRINT_POP,
262 BC_INST_STR,
263 BC_INST_PRINT_STR,
264
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100265#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600266 BC_INST_JUMP,
267 BC_INST_JUMP_ZERO,
268
269 BC_INST_CALL,
270
271 BC_INST_RET,
272 BC_INST_RET0,
273
274 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100275#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600276
277 BC_INST_POP,
278 BC_INST_POP_EXEC,
279
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600281 BC_INST_MODEXP,
282 BC_INST_DIVMOD,
283
284 BC_INST_EXECUTE,
285 BC_INST_EXEC_COND,
286
287 BC_INST_ASCIIFY,
288 BC_INST_PRINT_STREAM,
289
290 BC_INST_PRINT_STACK,
291 BC_INST_CLEAR_STACK,
292 BC_INST_STACK_LEN,
293 BC_INST_DUPLICATE,
294 BC_INST_SWAP,
295
296 BC_INST_LOAD,
297 BC_INST_PUSH_VAR,
298 BC_INST_PUSH_TO_VAR,
299
300 BC_INST_QUIT,
301 BC_INST_NQUIT,
302
303 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100304#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600305
306} BcInst;
307
308typedef struct BcId {
309 char *name;
310 size_t idx;
311} BcId;
312
313typedef struct BcFunc {
314 BcVec code;
315 BcVec labels;
316 size_t nparams;
317 BcVec autos;
318} BcFunc;
319
320typedef enum BcResultType {
321
322 BC_RESULT_TEMP,
323
324 BC_RESULT_VAR,
325 BC_RESULT_ARRAY_ELEM,
326 BC_RESULT_ARRAY,
327
328 BC_RESULT_STR,
329
330 BC_RESULT_IBASE,
331 BC_RESULT_SCALE,
332 BC_RESULT_LAST,
333
334 // These are between to calculate ibase, obase, and last from instructions.
335 BC_RESULT_CONSTANT,
336 BC_RESULT_ONE,
337
338 BC_RESULT_OBASE,
339
340} BcResultType;
341
342typedef union BcResultData {
343 BcNum n;
344 BcVec v;
345 BcId id;
346} BcResultData;
347
348typedef struct BcResult {
349 BcResultType t;
350 BcResultData d;
351} BcResult;
352
353typedef struct BcInstPtr {
354 size_t func;
355 size_t idx;
356 size_t len;
357} BcInstPtr;
358
Gavin Howard01055ba2018-11-03 11:00:21 -0600359// BC_LEX_NEG is not used in lexing; it is only for parsing.
360typedef enum BcLexType {
361
362 BC_LEX_EOF,
363 BC_LEX_INVALID,
364
365 BC_LEX_OP_INC,
366 BC_LEX_OP_DEC,
367
368 BC_LEX_NEG,
369
370 BC_LEX_OP_POWER,
371 BC_LEX_OP_MULTIPLY,
372 BC_LEX_OP_DIVIDE,
373 BC_LEX_OP_MODULUS,
374 BC_LEX_OP_PLUS,
375 BC_LEX_OP_MINUS,
376
377 BC_LEX_OP_REL_EQ,
378 BC_LEX_OP_REL_LE,
379 BC_LEX_OP_REL_GE,
380 BC_LEX_OP_REL_NE,
381 BC_LEX_OP_REL_LT,
382 BC_LEX_OP_REL_GT,
383
384 BC_LEX_OP_BOOL_NOT,
385 BC_LEX_OP_BOOL_OR,
386 BC_LEX_OP_BOOL_AND,
387
388 BC_LEX_OP_ASSIGN_POWER,
389 BC_LEX_OP_ASSIGN_MULTIPLY,
390 BC_LEX_OP_ASSIGN_DIVIDE,
391 BC_LEX_OP_ASSIGN_MODULUS,
392 BC_LEX_OP_ASSIGN_PLUS,
393 BC_LEX_OP_ASSIGN_MINUS,
394 BC_LEX_OP_ASSIGN,
395
396 BC_LEX_NLINE,
397 BC_LEX_WHITESPACE,
398
399 BC_LEX_LPAREN,
400 BC_LEX_RPAREN,
401
402 BC_LEX_LBRACKET,
403 BC_LEX_COMMA,
404 BC_LEX_RBRACKET,
405
406 BC_LEX_LBRACE,
407 BC_LEX_SCOLON,
408 BC_LEX_RBRACE,
409
410 BC_LEX_STR,
411 BC_LEX_NAME,
412 BC_LEX_NUMBER,
413
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100414 BC_LEX_KEY_1st_keyword,
415 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600416 BC_LEX_KEY_BREAK,
417 BC_LEX_KEY_CONTINUE,
418 BC_LEX_KEY_DEFINE,
419 BC_LEX_KEY_ELSE,
420 BC_LEX_KEY_FOR,
421 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100422 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
423 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600424 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100425 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600426 BC_LEX_KEY_LENGTH,
427 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100428 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600429 BC_LEX_KEY_PRINT,
430 BC_LEX_KEY_QUIT,
431 BC_LEX_KEY_READ,
432 BC_LEX_KEY_RETURN,
433 BC_LEX_KEY_SCALE,
434 BC_LEX_KEY_SQRT,
435 BC_LEX_KEY_WHILE,
436
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100437#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600438 BC_LEX_EQ_NO_REG,
439 BC_LEX_OP_MODEXP,
440 BC_LEX_OP_DIVMOD,
441
442 BC_LEX_COLON,
443 BC_LEX_ELSE,
444 BC_LEX_EXECUTE,
445 BC_LEX_PRINT_STACK,
446 BC_LEX_CLEAR_STACK,
447 BC_LEX_STACK_LEVEL,
448 BC_LEX_DUPLICATE,
449 BC_LEX_SWAP,
450 BC_LEX_POP,
451
452 BC_LEX_ASCIIFY,
453 BC_LEX_PRINT_STREAM,
454
455 BC_LEX_STORE_IBASE,
456 BC_LEX_STORE_SCALE,
457 BC_LEX_LOAD,
458 BC_LEX_LOAD_POP,
459 BC_LEX_STORE_PUSH,
460 BC_LEX_STORE_OBASE,
461 BC_LEX_PRINT_POP,
462 BC_LEX_NQUIT,
463 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100464#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600465} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100466// must match order of BC_LEX_KEY_foo etc above
467#if ENABLE_BC
468struct BcLexKeyword {
469 char name8[8];
470};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100471#define BC_LEX_KW_ENTRY(a, b) \
472 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100473static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100474 BC_LEX_KW_ENTRY("auto" , 1), // 0
475 BC_LEX_KW_ENTRY("break" , 1), // 1
476 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
477 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100478
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100479 BC_LEX_KW_ENTRY("else" , 0), // 4
480 BC_LEX_KW_ENTRY("for" , 1), // 5
481 BC_LEX_KW_ENTRY("halt" , 0), // 6
482 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100483
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100484 BC_LEX_KW_ENTRY("if" , 1), // 8
485 BC_LEX_KW_ENTRY("last" , 0), // 9
486 BC_LEX_KW_ENTRY("length" , 1), // 10
487 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100488
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100489 BC_LEX_KW_ENTRY("obase" , 1), // 12
490 BC_LEX_KW_ENTRY("print" , 0), // 13
491 BC_LEX_KW_ENTRY("quit" , 1), // 14
492 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100493
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100494 BC_LEX_KW_ENTRY("return" , 1), // 16
495 BC_LEX_KW_ENTRY("scale" , 1), // 17
496 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
497 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100498};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100499#undef BC_LEX_KW_ENTRY
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100500enum {
501 POSIX_KWORD_MASK = 0
502 | (1 << 0)
503 | (1 << 1)
504 | (0 << 2)
505 | (1 << 3)
506 \
507 | (0 << 4)
508 | (1 << 5)
509 | (0 << 6)
510 | (1 << 7)
511 \
512 | (1 << 8)
513 | (0 << 9)
514 | (1 << 10)
515 | (0 << 11)
516 \
517 | (1 << 12)
518 | (0 << 13)
519 | (1 << 14)
520 | (0 << 15)
521 \
522 | (1 << 16)
523 | (1 << 17)
524 | (1 << 18)
525 | (1 << 19)
526};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100528#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600529
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100530#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
531# define BC_STATUS BcStatus
532#else
533# define BC_STATUS void
534#endif
535
Gavin Howard01055ba2018-11-03 11:00:21 -0600536typedef struct BcLex {
537
538 const char *buf;
539 size_t i;
540 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600541 size_t len;
542 bool newline;
543
544 struct {
545 BcLexType t;
546 BcLexType last;
547 BcVec v;
548 } t;
549
Gavin Howard01055ba2018-11-03 11:00:21 -0600550} BcLex;
551
552#define BC_PARSE_STREND ((char) UCHAR_MAX)
553
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100554#define BC_PARSE_REL (1 << 0)
555#define BC_PARSE_PRINT (1 << 1)
Gavin Howard01055ba2018-11-03 11:00:21 -0600556#define BC_PARSE_NOCALL (1 << 2)
557#define BC_PARSE_NOREAD (1 << 3)
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100558#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600559
560#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
561#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
562
563#define BC_PARSE_FLAG_FUNC_INNER (1 << 0)
564#define BC_PARSE_FUNC_INNER(parse) \
565 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
566
567#define BC_PARSE_FLAG_FUNC (1 << 1)
568#define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
569
570#define BC_PARSE_FLAG_BODY (1 << 2)
571#define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
572
573#define BC_PARSE_FLAG_LOOP (1 << 3)
574#define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
575
576#define BC_PARSE_FLAG_LOOP_INNER (1 << 4)
577#define BC_PARSE_LOOP_INNER(parse) \
578 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
579
580#define BC_PARSE_FLAG_IF (1 << 5)
581#define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
582
583#define BC_PARSE_FLAG_ELSE (1 << 6)
584#define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
585
586#define BC_PARSE_FLAG_IF_END (1 << 7)
587#define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
588
589#define BC_PARSE_CAN_EXEC(parse) \
590 (!(BC_PARSE_TOP_FLAG(parse) & \
591 (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \
592 BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \
593 BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
594
Gavin Howard01055ba2018-11-03 11:00:21 -0600595struct BcParse;
596
597struct BcProgram;
598
Gavin Howard01055ba2018-11-03 11:00:21 -0600599typedef struct BcParse {
600
Gavin Howard01055ba2018-11-03 11:00:21 -0600601 BcLex l;
602
603 BcVec flags;
604
605 BcVec exits;
606 BcVec conds;
607
608 BcVec ops;
609
Gavin Howard01055ba2018-11-03 11:00:21 -0600610 BcFunc *func;
611 size_t fidx;
612
613 size_t nbraces;
614 bool auto_part;
615
616} BcParse;
617
Gavin Howard01055ba2018-11-03 11:00:21 -0600618typedef struct BcProgram {
619
620 size_t len;
621 size_t scale;
622
623 BcNum ib;
624 size_t ib_t;
625 BcNum ob;
626 size_t ob_t;
627
628 BcNum hexb;
629
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100630#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600631 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100632#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600633
634 BcVec results;
635 BcVec stack;
636
637 BcVec fns;
638 BcVec fn_map;
639
640 BcVec vars;
641 BcVec var_map;
642
643 BcVec arrs;
644 BcVec arr_map;
645
646 BcVec strs;
647 BcVec consts;
648
649 const char *file;
650
651 BcNum last;
652 BcNum zero;
653 BcNum one;
654
655 size_t nchars;
656
Gavin Howard01055ba2018-11-03 11:00:21 -0600657} BcProgram;
658
659#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
660
661#define BC_PROG_MAIN (0)
662#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100663#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600664#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100665#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600666
667#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
668#define BC_PROG_NUM(r, n) \
669 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
670
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100671#define BC_FLAG_W (1 << 0)
672#define BC_FLAG_V (1 << 1)
673#define BC_FLAG_S (1 << 2)
674#define BC_FLAG_Q (1 << 3)
675#define BC_FLAG_L (1 << 4)
676#define BC_FLAG_I (1 << 5)
677#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600678
679#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
680#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
681
Denys Vlasenko64074a12018-12-07 15:50:14 +0100682#define BC_MAX_OBASE ((unsigned) 999)
683#define BC_MAX_DIM ((unsigned) INT_MAX)
684#define BC_MAX_SCALE ((unsigned) UINT_MAX)
685#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
686#define BC_MAX_NUM BC_MAX_STRING
687// Unused apart from "limits" message. Just show a "biggish number" there.
688//#define BC_MAX_NAME BC_MAX_STRING
689//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
690//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
691#define BC_MAX_NAME_STR "999999999"
692#define BC_MAX_EXP_STR "999999999"
693#define BC_MAX_VARS_STR "999999999"
694
695#define BC_MAX_OBASE_STR "999"
696
697#if INT_MAX == 2147483647
698# define BC_MAX_DIM_STR "2147483647"
699#elif INT_MAX == 9223372036854775807
700# define BC_MAX_DIM_STR "9223372036854775807"
701#else
702# error Strange INT_MAX
703#endif
704
705#if UINT_MAX == 4294967295
706# define BC_MAX_SCALE_STR "4294967295"
707# define BC_MAX_STRING_STR "4294967294"
708#elif UINT_MAX == 18446744073709551615
709# define BC_MAX_SCALE_STR "18446744073709551615"
710# define BC_MAX_STRING_STR "18446744073709551614"
711#else
712# error Strange UINT_MAX
713#endif
714#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600715
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100716struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100717 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100718 IF_FEATURE_CLEAN_UP(smallint exiting;)
Denys Vlasenko69171dc2018-12-12 00:29:24 +0100719 smallint in_read;
Gavin Howard01055ba2018-11-03 11:00:21 -0600720 char sbgn;
721 char send;
Gavin Howard01055ba2018-11-03 11:00:21 -0600722
723 BcParse prs;
724 BcProgram prog;
725
Denys Vlasenko5318f812018-12-05 17:48:01 +0100726 // For error messages. Can be set to current parsed line,
727 // or [TODO] to current executing line (can be before last parsed one)
728 unsigned err_line;
729
Gavin Howard01055ba2018-11-03 11:00:21 -0600730 BcVec files;
731
732 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100733
734#if ENABLE_FEATURE_EDITING
735 line_input_t *line_input_state;
736#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100737} FIX_ALIASING;
738#define G (*ptr_to_globals)
739#define INIT_G() do { \
740 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
741} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100742#define FREE_G() do { \
743 FREE_PTR_TO_GLOBALS(); \
744} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100745#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
746#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100747#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100748#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100749# define G_interrupt bb_got_signal
750# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100751#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100752# define G_interrupt 0
753# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100754#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100755#if ENABLE_FEATURE_CLEAN_UP
756# define G_exiting G.exiting
757#else
758# define G_exiting 0
759#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100760#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100761#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100762
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100763#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600764
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100765// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600766// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100767enum {
768 BC_PARSE_EXPRS_BITS = 0
769 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
770 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
771 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
772 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
773 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
774 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
775 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
776 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600777};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100778static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
779{
780#if ULONG_MAX > 0xffffffff
781 // 64-bit version (will not work correctly for 32-bit longs!)
782 return BC_PARSE_EXPRS_BITS & (1UL << i);
783#else
784 // 32-bit version
785 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
786 if (i >= 32) {
787 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
788 i &= 31;
789 }
790 return m & (1UL << i);
791#endif
792}
Gavin Howard01055ba2018-11-03 11:00:21 -0600793
794// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100795static const uint8_t bc_parse_ops[] = {
796#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100797 OP(0, false), OP( 0, false ), // inc dec
798 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100799 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100800 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
801 OP(4, true ), OP( 4, true ), // mod + -
802 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
803 OP(1, false), // not
804 OP(7, true ), OP( 7, true ), // or and
805 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
806 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100807#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600808};
Denys Vlasenko65437582018-12-05 19:37:19 +0100809#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
810#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600811
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100812// Byte array of up to 4 BC_LEX's, packed into 32-bit word
813typedef uint32_t BcParseNext;
814
Gavin Howard01055ba2018-11-03 11:00:21 -0600815// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100816enum {
817#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
818#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
819#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
820 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
821 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
822 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
823 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
824 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
825 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
826 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
827#undef BC_PARSE_NEXT4
828#undef BC_PARSE_NEXT2
829#undef BC_PARSE_NEXT1
830};
Gavin Howard01055ba2018-11-03 11:00:21 -0600831#endif // ENABLE_BC
832
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100833#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100834static const //BcLexType - should be this type, but narrower type saves size:
835uint8_t
836dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600837 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
838 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
839 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
840 BC_LEX_STORE_PUSH,
841};
842
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100843static const //BcLexType - should be this type
844uint8_t
845dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600846 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
847 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
848 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
849 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
850 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
851 BC_LEX_INVALID, BC_LEX_INVALID,
852 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
853 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
854 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
855 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
856 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
857 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
858 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
859 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
860 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
861 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
862 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
863 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
864 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
865 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
866 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
867 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
868 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
869 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
870 BC_LEX_INVALID
871};
872
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100873static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
874int8_t
875dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600876 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
877 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
878 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
879 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
880 BC_INST_INVALID, BC_INST_INVALID,
881 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
882 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
883 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
884 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
885 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
886 BC_INST_INVALID, BC_INST_INVALID,
887 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
888 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
889 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
890 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
891 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
892 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
893 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
894 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
895 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
896 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
897 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
898 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
899};
900#endif // ENABLE_DC
901
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100902// In configurations where errors abort instead of propagating error
903// return code up the call chain, functions returning BC_STATUS
904// actually don't return anything, they always succeed and return "void".
905// A macro wrapper is provided, which makes this statement work:
906// s = zbc_func(...)
907// and makes it visible to the compiler that s is always zero,
908// allowing compiler to optimize dead code after the statement.
909//
910// To make code more readable, each such function has a "z"
911// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
912//
913#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100914# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100915# define ERRORFUNC /*nothing*/
916# define ERROR_RETURN(a) a
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100917//moved up: # define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100918# define RETURN_STATUS(v) return (v)
919#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100920# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100921# define ERRORFUNC NORETURN
922# define ERROR_RETURN(a) /*nothing*/
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100923//moved up: # define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100924# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
925#endif
926
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100927#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
928#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
929#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
930//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
931static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
932{
933 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
934}
935//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
936static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
937{
938 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
939}
940
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100941typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
942
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100943typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100944
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100945static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
946 BcNumBinaryOp op, size_t req);
947static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
948static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
949static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
950static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
951static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
952static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
953
954static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
955{
956 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
957 (void) scale;
958 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
959}
960
961static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
962{
963 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
964 (void) scale;
965 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
966}
967
968static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
969{
970 size_t req = BC_NUM_MREQ(a, b, scale);
971 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
972}
973
974static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
975{
976 size_t req = BC_NUM_MREQ(a, b, scale);
977 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
978}
979
980static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
981{
982 size_t req = BC_NUM_MREQ(a, b, scale);
983 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
984}
985
986static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
987{
988 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
989}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100990
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100991static const BcNumBinaryOp zbc_program_ops[] = {
992 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
Gavin Howard01055ba2018-11-03 11:00:21 -0600993};
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100994#if ERRORS_ARE_FATAL
995# define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS)
996# define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS)
997# define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS)
998# define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS)
999# define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS)
1000# define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS)
1001#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001002
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001003static void fflush_and_check(void)
1004{
1005 fflush_all();
1006 if (ferror(stdout) || ferror(stderr))
1007 bb_perror_msg_and_die("output error");
1008}
1009
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001010#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001011#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001012do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001013 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001014 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001015 return BC_STATUS_FAILURE; \
1016} while (0)
1017#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001018#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001019#endif
1020
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001021static void quit(void) NORETURN;
1022static void quit(void)
1023{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001024 if (ferror(stdin))
1025 bb_perror_msg_and_die("input error");
1026 fflush_and_check();
1027 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001028}
1029
Denys Vlasenko5318f812018-12-05 17:48:01 +01001030static void bc_verror_msg(const char *fmt, va_list p)
1031{
1032 const char *sv = sv; /* for compiler */
1033 if (G.prog.file) {
1034 sv = applet_name;
1035 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
1036 }
1037 bb_verror_msg(fmt, p, NULL);
1038 if (G.prog.file) {
1039 free((char*)applet_name);
1040 applet_name = sv;
1041 }
1042}
1043
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001044static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001045{
1046 va_list p;
1047
1048 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001049 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001050 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001051
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001052 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001053 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001054 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001055}
1056
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001057#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001058static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001059{
1060 va_list p;
1061
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001062 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001063 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001064 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001065
1066 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001067 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001068 va_end(p);
1069
1070 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001071 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001072 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001073 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001074 exit(1);
1075 return BC_STATUS_FAILURE;
1076}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001077#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001078
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001079// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1080// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001081// function must not have caller-cleaned parameters on stack.
1082// Unfortunately, vararg function API does exactly that on most arches.
1083// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001084static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001085{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001086 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1087}
1088static ERRORFUNC int bc_error_bad_character(char c)
1089{
1090 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1091}
1092static ERRORFUNC int bc_error_bad_expression(void)
1093{
1094 ERROR_RETURN(return) bc_error("bad expression");
1095}
1096static ERRORFUNC int bc_error_bad_token(void)
1097{
1098 ERROR_RETURN(return) bc_error("bad token");
1099}
1100static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1101{
1102 ERROR_RETURN(return) bc_error("stack has too few elements");
1103}
1104static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1105{
1106 ERROR_RETURN(return) bc_error("variable is wrong type");
1107}
1108static ERRORFUNC int bc_error_nested_read_call(void)
1109{
1110 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001111}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001112#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001113static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001114{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001115 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001116}
Denys Vlasenko00646792018-12-05 18:12:27 +01001117static int bc_POSIX_does_not_allow(const char *msg)
1118{
1119 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1120}
1121static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1122{
1123 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1124}
1125static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1126{
1127 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1128}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001129#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001130
Gavin Howard01055ba2018-11-03 11:00:21 -06001131static void bc_vec_grow(BcVec *v, size_t n)
1132{
1133 size_t cap = v->cap * 2;
1134 while (cap < v->len + n) cap *= 2;
1135 v->v = xrealloc(v->v, v->size * cap);
1136 v->cap = cap;
1137}
1138
1139static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1140{
1141 v->size = esize;
1142 v->cap = BC_VEC_START_CAP;
1143 v->len = 0;
1144 v->dtor = dtor;
1145 v->v = xmalloc(esize * BC_VEC_START_CAP);
1146}
1147
Denys Vlasenko7d628012018-12-04 21:46:47 +01001148static void bc_char_vec_init(BcVec *v)
1149{
1150 bc_vec_init(v, sizeof(char), NULL);
1151}
1152
Gavin Howard01055ba2018-11-03 11:00:21 -06001153static void bc_vec_expand(BcVec *v, size_t req)
1154{
1155 if (v->cap < req) {
1156 v->v = xrealloc(v->v, v->size * req);
1157 v->cap = req;
1158 }
1159}
1160
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001161static void bc_vec_pop(BcVec *v)
1162{
1163 v->len--;
1164 if (v->dtor)
1165 v->dtor(v->v + (v->size * v->len));
1166}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001167
Gavin Howard01055ba2018-11-03 11:00:21 -06001168static void bc_vec_npop(BcVec *v, size_t n)
1169{
1170 if (!v->dtor)
1171 v->len -= n;
1172 else {
1173 size_t len = v->len - n;
1174 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1175 }
1176}
1177
Denys Vlasenko7d628012018-12-04 21:46:47 +01001178static void bc_vec_pop_all(BcVec *v)
1179{
1180 bc_vec_npop(v, v->len);
1181}
1182
Gavin Howard01055ba2018-11-03 11:00:21 -06001183static void bc_vec_push(BcVec *v, const void *data)
1184{
1185 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1186 memmove(v->v + (v->size * v->len), data, v->size);
1187 v->len += 1;
1188}
1189
1190static void bc_vec_pushByte(BcVec *v, char data)
1191{
1192 bc_vec_push(v, &data);
1193}
1194
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001195static void bc_vec_pushZeroByte(BcVec *v)
1196{
1197 //bc_vec_pushByte(v, '\0');
1198 // better:
1199 bc_vec_push(v, &const_int_0);
1200}
1201
Gavin Howard01055ba2018-11-03 11:00:21 -06001202static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1203{
1204 if (idx == v->len)
1205 bc_vec_push(v, data);
1206 else {
1207
1208 char *ptr;
1209
1210 if (v->len == v->cap) bc_vec_grow(v, 1);
1211
1212 ptr = v->v + v->size * idx;
1213
1214 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1215 memmove(ptr, data, v->size);
1216 }
1217}
1218
1219static void bc_vec_string(BcVec *v, size_t len, const char *str)
1220{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001221 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001222 bc_vec_expand(v, len + 1);
1223 memcpy(v->v, str, len);
1224 v->len = len;
1225
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001226 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001227}
1228
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001229#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001230static void bc_vec_concat(BcVec *v, const char *str)
1231{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001232 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001233
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001234 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001235
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001236 slen = strlen(str);
1237 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001238
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001239 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001240 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001241
1242 v->len = len;
1243}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001244#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001245
1246static void *bc_vec_item(const BcVec *v, size_t idx)
1247{
1248 return v->v + v->size * idx;
1249}
1250
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001251static char** bc_program_str(size_t idx)
1252{
1253 return bc_vec_item(&G.prog.strs, idx);
1254}
1255
1256static BcFunc* bc_program_func(size_t idx)
1257{
1258 return bc_vec_item(&G.prog.fns, idx);
1259}
1260
Gavin Howard01055ba2018-11-03 11:00:21 -06001261static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1262{
1263 return v->v + v->size * (v->len - idx - 1);
1264}
1265
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001266static void *bc_vec_top(const BcVec *v)
1267{
1268 return v->v + v->size * (v->len - 1);
1269}
1270
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001271static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001272{
1273 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001274 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001275 free(v->v);
1276}
1277
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001278static int bc_id_cmp(const void *e1, const void *e2)
1279{
1280 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1281}
1282
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001283static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001284{
1285 free(((BcId *) id)->name);
1286}
1287
Gavin Howard01055ba2018-11-03 11:00:21 -06001288static size_t bc_map_find(const BcVec *v, const void *ptr)
1289{
1290 size_t low = 0, high = v->len;
1291
1292 while (low < high) {
1293
1294 size_t mid = (low + high) / 2;
1295 BcId *id = bc_vec_item(v, mid);
1296 int result = bc_id_cmp(ptr, id);
1297
1298 if (result == 0)
1299 return mid;
1300 else if (result < 0)
1301 high = mid;
1302 else
1303 low = mid + 1;
1304 }
1305
1306 return low;
1307}
1308
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001309static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001310{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001311 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001312
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001313 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001314 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001315 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1316 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001317 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001318 bc_vec_pushAt(v, ptr, n);
1319 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001320}
1321
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001322#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001323static size_t bc_map_index(const BcVec *v, const void *ptr)
1324{
1325 size_t i = bc_map_find(v, ptr);
1326 if (i >= v->len) return BC_VEC_INVALID_IDX;
1327 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1328}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001329#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001330
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001331static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001332{
1333 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1334 || c > 0x7e
1335 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001336 bc_error_fmt("illegal character 0x%02x", c);
1337 return 1;
1338 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001339 return 0;
1340}
1341
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001342// Note: it _appends_ data from the stdin to vec.
Denys Vlasenko8a892472018-12-12 22:43:58 +01001343static void bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001344{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001345 again:
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001346 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001347
Gavin Howard01055ba2018-11-03 11:00:21 -06001348#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001349 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001350 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001351 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001352 // GNU bc says "interrupted execution."
1353 // GNU dc says "Interrupt!"
1354 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001355 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001356
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001357# if ENABLE_FEATURE_EDITING
1358 if (G_ttyin) {
1359 int n, i;
1360# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001361 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001362 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1363 if (n == 0) // ^C
1364 goto intr;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001365 break;
1366 }
1367 i = 0;
1368 for (;;) {
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001369 char c = line_buf[i++];
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001370 if (!c) break;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001371 if (bad_input_byte(c)) goto again;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001372 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001373 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001374# undef line_buf
1375 } else
1376# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001377#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001378 {
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001379 int c;
1380 bool bad_chars = 0;
1381 size_t len = vec->len;
1382
Denys Vlasenkoed849352018-12-06 10:26:13 +01001383 IF_FEATURE_BC_SIGNALS(errno = 0;)
1384 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001385 c = fgetc(stdin);
1386#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1387 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001388 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001389 // ^C was pressed
1390 clearerr(stdin);
1391 goto intr;
1392 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001393#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001394 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001395 if (ferror(stdin))
1396 quit(); // this emits error message
Denys Vlasenkoed849352018-12-06 10:26:13 +01001397 // Note: EOF does not append '\n', therefore:
1398 // printf 'print 123\n' | bc - works
1399 // printf 'print 123' | bc - fails (syntax error)
1400 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001401 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001402 bad_chars |= bad_input_byte(c);
1403 bc_vec_pushByte(vec, (char)c);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001404 } while (c != '\n');
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001405 if (bad_chars) {
1406 // Bad chars on this line, ignore entire line
1407 vec->len = len;
1408 goto again;
1409 }
Denys Vlasenkoed849352018-12-06 10:26:13 +01001410 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001411
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001412 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001413}
1414
Denys Vlasenkodf515392018-12-02 19:27:48 +01001415static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001416{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001417 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001418 size_t size = ((size_t) -1);
1419 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001420
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001421 // Never returns NULL (dies on errors)
1422 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001423
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001424 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001425 char c = buf[i];
1426 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1427 || c > 0x7e
1428 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001429 free(buf);
1430 buf = NULL;
1431 break;
1432 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001433 }
1434
Denys Vlasenkodf515392018-12-02 19:27:48 +01001435 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001436}
1437
Gavin Howard01055ba2018-11-03 11:00:21 -06001438static void bc_num_setToZero(BcNum *n, size_t scale)
1439{
1440 n->len = 0;
1441 n->neg = false;
1442 n->rdx = scale;
1443}
1444
1445static void bc_num_zero(BcNum *n)
1446{
1447 bc_num_setToZero(n, 0);
1448}
1449
1450static void bc_num_one(BcNum *n)
1451{
1452 bc_num_setToZero(n, 0);
1453 n->len = 1;
1454 n->num[0] = 1;
1455}
1456
1457static void bc_num_ten(BcNum *n)
1458{
1459 bc_num_setToZero(n, 0);
1460 n->len = 2;
1461 n->num[0] = 0;
1462 n->num[1] = 1;
1463}
1464
Denys Vlasenko3129f702018-12-09 12:04:44 +01001465// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001466static void bc_num_init(BcNum *n, size_t req)
1467{
1468 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001469 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001470 n->num = xmalloc(req);
1471 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001472 n->rdx = 0;
1473 n->len = 0;
1474 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001475}
1476
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001477static void bc_num_init_DEF_SIZE(BcNum *n)
1478{
1479 bc_num_init(n, BC_NUM_DEF_SIZE);
1480}
1481
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001482static void bc_num_expand(BcNum *n, size_t req)
1483{
1484 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1485 if (req > n->cap) {
1486 n->num = xrealloc(n->num, req);
1487 n->cap = req;
1488 }
1489}
1490
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001491static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001492{
1493 free(((BcNum *) num)->num);
1494}
1495
1496static void bc_num_copy(BcNum *d, BcNum *s)
1497{
1498 if (d != s) {
1499 bc_num_expand(d, s->cap);
1500 d->len = s->len;
1501 d->neg = s->neg;
1502 d->rdx = s->rdx;
1503 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1504 }
1505}
1506
Denys Vlasenko29301232018-12-11 15:29:32 +01001507static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001508{
1509 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001510 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001511
Denys Vlasenko29301232018-12-11 15:29:32 +01001512 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001513
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001514 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001515
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001516 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001517
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001518 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001519 pow *= 10;
1520
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001521 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001522 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001523 prev = result;
1524 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001525 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001526 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001527
Denys Vlasenko29301232018-12-11 15:29:32 +01001528 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001529}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001530#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001531# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001532#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001533
1534static void bc_num_ulong2num(BcNum *n, unsigned long val)
1535{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001536 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001537
1538 bc_num_zero(n);
1539
1540 if (val == 0) return;
1541
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001542 if (ULONG_MAX == 0xffffffffUL)
1543 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001544 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001545 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001546 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001547
1548 ptr = n->num;
1549 for (;;) {
1550 n->len++;
1551 *ptr++ = val % 10;
1552 val /= 10;
1553 if (val == 0) break;
1554 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001555}
1556
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001557static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001558 size_t len)
1559{
1560 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001561 for (i = 0; i < len; ++i) {
1562 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001563 a[i + j++] += 10;
1564 a[i + j] -= 1;
1565 }
1566 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001567}
1568
1569static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1570{
1571 size_t i;
1572 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001573 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001574 return BC_NUM_NEG(i + 1, c < 0);
1575}
1576
1577static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1578{
1579 size_t i, min, a_int, b_int, diff;
1580 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001581 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001582 ssize_t cmp;
1583
1584 if (a == b) return 0;
1585 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1586 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001587
1588 if (a->neg != b->neg) // signs of a and b differ
1589 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1590 return (int)b->neg - (int)a->neg;
1591 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001592
1593 a_int = BC_NUM_INT(a);
1594 b_int = BC_NUM_INT(b);
1595 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001596
1597 if (a_int != 0) return (ssize_t) a_int;
1598
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001599 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001600 if (a_max) {
1601 min = b->rdx;
1602 diff = a->rdx - b->rdx;
1603 max_num = a->num + diff;
1604 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001605 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1606 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001607 min = a->rdx;
1608 diff = b->rdx - a->rdx;
1609 max_num = b->num + diff;
1610 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001611 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001612 }
1613
1614 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001615 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001616
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001617 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001618 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001619 }
1620
1621 return 0;
1622}
1623
1624static void bc_num_truncate(BcNum *n, size_t places)
1625{
1626 if (places == 0) return;
1627
1628 n->rdx -= places;
1629
1630 if (n->len != 0) {
1631 n->len -= places;
1632 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1633 }
1634}
1635
1636static void bc_num_extend(BcNum *n, size_t places)
1637{
1638 size_t len = n->len + places;
1639
1640 if (places != 0) {
1641
1642 if (n->cap < len) bc_num_expand(n, len);
1643
1644 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1645 memset(n->num, 0, sizeof(BcDig) * places);
1646
1647 n->len += places;
1648 n->rdx += places;
1649 }
1650}
1651
1652static void bc_num_clean(BcNum *n)
1653{
1654 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1655 if (n->len == 0)
1656 n->neg = false;
1657 else if (n->len < n->rdx)
1658 n->len = n->rdx;
1659}
1660
1661static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1662{
1663 if (n->rdx < scale)
1664 bc_num_extend(n, scale - n->rdx);
1665 else
1666 bc_num_truncate(n, n->rdx - scale);
1667
1668 bc_num_clean(n);
1669 if (n->len != 0) n->neg = !neg1 != !neg2;
1670}
1671
1672static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1673 BcNum *restrict b)
1674{
1675 if (idx < n->len) {
1676
1677 b->len = n->len - idx;
1678 a->len = idx;
1679 a->rdx = b->rdx = 0;
1680
1681 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1682 memcpy(a->num, n->num, idx * sizeof(BcDig));
1683 }
1684 else {
1685 bc_num_zero(b);
1686 bc_num_copy(a, n);
1687 }
1688
1689 bc_num_clean(a);
1690 bc_num_clean(b);
1691}
1692
Denys Vlasenko29301232018-12-11 15:29:32 +01001693static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001694{
Denys Vlasenko29301232018-12-11 15:29:32 +01001695 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001696
1697 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1698 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1699 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001700 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001701 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001702
1703 if (n->rdx >= places)
1704 n->rdx -= places;
1705 else {
1706 bc_num_extend(n, places - n->rdx);
1707 n->rdx = 0;
1708 }
1709
1710 bc_num_clean(n);
1711
Denys Vlasenko29301232018-12-11 15:29:32 +01001712 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001713}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001714#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001715# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001716#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001717
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001718static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001719{
1720 BcNum one;
1721 BcDig num[2];
1722
1723 one.cap = 2;
1724 one.num = num;
1725 bc_num_one(&one);
1726
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001727 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001728}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001729#if ERRORS_ARE_FATAL
1730# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1731#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001732
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001733static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001734{
1735 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1736 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1737 int carry, in;
1738
1739 // Because this function doesn't need to use scale (per the bc spec),
1740 // I am hijacking it to say whether it's doing an add or a subtract.
1741
1742 if (a->len == 0) {
1743 bc_num_copy(c, b);
1744 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001745 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001746 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001747 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001748 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001749 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001750 }
1751
1752 c->neg = a->neg;
1753 c->rdx = BC_MAX(a->rdx, b->rdx);
1754 min_rdx = BC_MIN(a->rdx, b->rdx);
1755 c->len = 0;
1756
1757 if (a->rdx > b->rdx) {
1758 diff = a->rdx - b->rdx;
1759 ptr = a->num;
1760 ptr_a = a->num + diff;
1761 ptr_b = b->num;
1762 }
1763 else {
1764 diff = b->rdx - a->rdx;
1765 ptr = b->num;
1766 ptr_a = a->num;
1767 ptr_b = b->num + diff;
1768 }
1769
1770 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1771
1772 ptr_c += diff;
1773 a_int = BC_NUM_INT(a);
1774 b_int = BC_NUM_INT(b);
1775
1776 if (a_int > b_int) {
1777 min_int = b_int;
1778 max = a_int;
1779 ptr = ptr_a;
1780 }
1781 else {
1782 min_int = a_int;
1783 max = b_int;
1784 ptr = ptr_b;
1785 }
1786
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001787 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001788 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1789 carry = in / 10;
1790 ptr_c[i] = (BcDig)(in % 10);
1791 }
1792
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001793 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 in = ((int) ptr[i]) + carry;
1795 carry = in / 10;
1796 ptr_c[i] = (BcDig)(in % 10);
1797 }
1798
1799 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1800
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001801 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001802}
1803
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001804static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001805{
Gavin Howard01055ba2018-11-03 11:00:21 -06001806 ssize_t cmp;
1807 BcNum *minuend, *subtrahend;
1808 size_t start;
1809 bool aneg, bneg, neg;
1810
1811 // Because this function doesn't need to use scale (per the bc spec),
1812 // I am hijacking it to say whether it's doing an add or a subtract.
1813
1814 if (a->len == 0) {
1815 bc_num_copy(c, b);
1816 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001817 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001818 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001819 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001820 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001821 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 }
1823
1824 aneg = a->neg;
1825 bneg = b->neg;
1826 a->neg = b->neg = false;
1827
1828 cmp = bc_num_cmp(a, b);
1829
1830 a->neg = aneg;
1831 b->neg = bneg;
1832
1833 if (cmp == 0) {
1834 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001835 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001836 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001837 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001838 neg = a->neg;
1839 minuend = a;
1840 subtrahend = b;
1841 }
1842 else {
1843 neg = b->neg;
1844 if (sub) neg = !neg;
1845 minuend = b;
1846 subtrahend = a;
1847 }
1848
1849 bc_num_copy(c, minuend);
1850 c->neg = neg;
1851
1852 if (c->rdx < subtrahend->rdx) {
1853 bc_num_extend(c, subtrahend->rdx - c->rdx);
1854 start = 0;
1855 }
1856 else
1857 start = c->rdx - subtrahend->rdx;
1858
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001859 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001860
1861 bc_num_clean(c);
1862
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001863 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001864}
1865
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001866static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001868#if ERRORS_ARE_FATAL
1869# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1870#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001871{
1872 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001873 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001875 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001876
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 if (a->len == 0 || b->len == 0) {
1878 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001879 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001880 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001881 aone = BC_NUM_ONE(a);
1882 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001883 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001884 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001885 }
1886
1887 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1888 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1889 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001890 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001891 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001892
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 bc_num_expand(c, a->len + b->len + 1);
1894
1895 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001896 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001897
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001898 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001899
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001900 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001901 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001902 unsigned in = c->num[i + j];
1903 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1904 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001905 carry = in / 10;
1906 c->num[i + j] = (BcDig)(in % 10);
1907 }
1908
1909 c->num[i + j] += (BcDig) carry;
1910 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001911
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001912#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001913 // a=2^1000000
1914 // a*a <- without check below, this will not be interruptible
1915 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001916#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001917 }
1918
1919 c->len = len;
1920
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001921 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001922 }
1923
1924 bc_num_init(&l1, max);
1925 bc_num_init(&h1, max);
1926 bc_num_init(&l2, max);
1927 bc_num_init(&h2, max);
1928 bc_num_init(&m1, max);
1929 bc_num_init(&m2, max);
1930 bc_num_init(&z0, max);
1931 bc_num_init(&z1, max);
1932 bc_num_init(&z2, max);
1933 bc_num_init(&temp, max + max);
1934
1935 bc_num_split(a, max2, &l1, &h1);
1936 bc_num_split(b, max2, &l2, &h2);
1937
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001938 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001939 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001940 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001941 if (s) goto err;
1942
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001943 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001944 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001945 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001946 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001947 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001948 if (s) goto err;
1949
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001950 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001952 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001953 if (s) goto err;
1954
Denys Vlasenko29301232018-12-11 15:29:32 +01001955 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001957 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001958 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001959 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001960 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001961 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001962
1963err:
1964 bc_num_free(&temp);
1965 bc_num_free(&z2);
1966 bc_num_free(&z1);
1967 bc_num_free(&z0);
1968 bc_num_free(&m2);
1969 bc_num_free(&m1);
1970 bc_num_free(&h2);
1971 bc_num_free(&l2);
1972 bc_num_free(&h1);
1973 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001974 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001975}
1976
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001977static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001978{
1979 BcStatus s;
1980 BcNum cpa, cpb;
1981 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1982
1983 scale = BC_MAX(scale, a->rdx);
1984 scale = BC_MAX(scale, b->rdx);
1985 scale = BC_MIN(a->rdx + b->rdx, scale);
1986 maxrdx = BC_MAX(maxrdx, scale);
1987
1988 bc_num_init(&cpa, a->len);
1989 bc_num_init(&cpb, b->len);
1990
1991 bc_num_copy(&cpa, a);
1992 bc_num_copy(&cpb, b);
1993 cpa.neg = cpb.neg = false;
1994
Denys Vlasenko29301232018-12-11 15:29:32 +01001995 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001996 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001997 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001998 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001999 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06002000 if (s) goto err;
2001
2002 maxrdx += scale;
2003 bc_num_expand(c, c->len + maxrdx);
2004
2005 if (c->len < maxrdx) {
2006 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2007 c->len += maxrdx;
2008 }
2009
2010 c->rdx = maxrdx;
2011 bc_num_retireMul(c, scale, a->neg, b->neg);
2012
2013err:
2014 bc_num_free(&cpb);
2015 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002016 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002017}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002018#if ERRORS_ARE_FATAL
2019# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2020#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002021
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002022static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002023{
2024 BcStatus s = BC_STATUS_SUCCESS;
2025 BcDig *n, *p, q;
2026 size_t len, end, i;
2027 BcNum cp;
2028 bool zero = true;
2029
2030 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002031 RETURN_STATUS(bc_error("divide by zero"));
2032 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002033 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002034 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002035 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002036 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002037 bc_num_copy(c, a);
2038 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002039 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002040 }
2041
2042 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2043 bc_num_copy(&cp, a);
2044 len = b->len;
2045
2046 if (len > cp.len) {
2047 bc_num_expand(&cp, len + 2);
2048 bc_num_extend(&cp, len - cp.len);
2049 }
2050
2051 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2052 cp.rdx -= b->rdx;
2053 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2054
2055 if (b->rdx == b->len) {
2056 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2057 len -= i - 1;
2058 }
2059
2060 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2061
2062 // We want an extra zero in front to make things simpler.
2063 cp.num[cp.len++] = 0;
2064 end = cp.len - len;
2065
2066 bc_num_expand(c, cp.len);
2067
2068 bc_num_zero(c);
2069 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2070 c->rdx = cp.rdx;
2071 c->len = cp.len;
2072 p = b->num;
2073
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002074 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002075 n = cp.num + i;
2076 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002077 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002078 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002079#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002080 // a=2^100000
2081 // scale=40000
2082 // 1/a <- without check below, this will not be interruptible
2083 if (G_interrupt) {
2084 s = BC_STATUS_FAILURE;
2085 break;
2086 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002087#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002088 }
2089
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002090 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002091 bc_num_free(&cp);
2092
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002094}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002095#if ERRORS_ARE_FATAL
2096# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2097#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002098
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002099static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002100 BcNum *restrict d, size_t scale, size_t ts)
2101{
2102 BcStatus s;
2103 BcNum temp;
2104 bool neg;
2105
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002106 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002107 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002108
2109 if (a->len == 0) {
2110 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002111 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002112 }
2113
2114 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002115 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002116 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002117
2118 if (scale != 0) scale = ts;
2119
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002120 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002122 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 if (s) goto err;
2124
2125 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2126
2127 neg = d->neg;
2128 bc_num_retireMul(d, ts, a->neg, b->neg);
2129 d->neg = neg;
2130
2131err:
2132 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002133 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002134}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002135#if ERRORS_ARE_FATAL
2136# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2137#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002138
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002139static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002140{
2141 BcStatus s;
2142 BcNum c1;
2143 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2144
2145 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002146 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 bc_num_free(&c1);
2148
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002149 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002150}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002151#if ERRORS_ARE_FATAL
2152# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2153#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002154
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002155static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002156{
2157 BcStatus s = BC_STATUS_SUCCESS;
2158 BcNum copy;
2159 unsigned long pow;
2160 size_t i, powrdx, resrdx;
2161 bool neg, zero;
2162
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002163 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002164
2165 if (b->len == 0) {
2166 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002167 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002168 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002169 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002170 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002171 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002172 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002173 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002174 if (!b->neg)
2175 bc_num_copy(c, a);
2176 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002177 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002178 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002179 }
2180
2181 neg = b->neg;
2182 b->neg = false;
2183
Denys Vlasenko29301232018-12-11 15:29:32 +01002184 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002185 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002186
2187 bc_num_init(&copy, a->len);
2188 bc_num_copy(&copy, a);
2189
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002190 if (!neg) {
2191 if (a->rdx > scale)
2192 scale = a->rdx;
2193 if (a->rdx * pow < scale)
2194 scale = a->rdx * pow;
2195 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002196
2197 b->neg = neg;
2198
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002199 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002200 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002201 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002202 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002203 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002204 //if (G_interrupt) {
2205 // s = BC_STATUS_FAILURE;
2206 // goto err;
2207 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002208 }
2209
Gavin Howard01055ba2018-11-03 11:00:21 -06002210 bc_num_copy(c, &copy);
2211
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002212 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002213
2214 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002215 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002216 if (s) goto err;
2217
2218 if (pow & 1) {
2219 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002220 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002221 if (s) goto err;
2222 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002223 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002224 //if (G_interrupt) {
2225 // s = BC_STATUS_FAILURE;
2226 // goto err;
2227 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002228 }
2229
2230 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002231 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002232 if (s) goto err;
2233 }
2234
Gavin Howard01055ba2018-11-03 11:00:21 -06002235 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2236
2237 // We can't use bc_num_clean() here.
2238 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2239 if (zero) bc_num_setToZero(c, scale);
2240
2241err:
2242 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002243 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002244}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002245#if ERRORS_ARE_FATAL
2246# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2247#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002248
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002249static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002250 BcNumBinaryOp op, size_t req)
2251{
2252 BcStatus s;
2253 BcNum num2, *ptr_a, *ptr_b;
2254 bool init = false;
2255
2256 if (c == a) {
2257 ptr_a = &num2;
2258 memcpy(ptr_a, c, sizeof(BcNum));
2259 init = true;
2260 }
2261 else
2262 ptr_a = a;
2263
2264 if (c == b) {
2265 ptr_b = &num2;
2266 if (c != a) {
2267 memcpy(ptr_b, c, sizeof(BcNum));
2268 init = true;
2269 }
2270 }
2271 else
2272 ptr_b = b;
2273
2274 if (init)
2275 bc_num_init(c, req);
2276 else
2277 bc_num_expand(c, req);
2278
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002279 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01002280 ERROR_RETURN(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002281
2282 if (init) bc_num_free(&num2);
2283
Denys Vlasenko29301232018-12-11 15:29:32 +01002284 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002285}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002286#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002287# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002288#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002289
Denys Vlasenko9311e012018-12-10 11:54:18 +01002290static bool bc_num_strValid(const char *val, size_t base)
2291{
2292 BcDig b;
2293 bool radix;
2294
2295 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2296 radix = false;
2297 for (;;) {
2298 BcDig c = *val++;
2299 if (c == '\0')
2300 break;
2301 if (c == '.') {
2302 if (radix) return false;
2303 radix = true;
2304 continue;
2305 }
2306 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2307 return false;
2308 }
2309 return true;
2310}
2311
2312// Note: n is already "bc_num_zero()"ed,
2313// leading zeroes in "val" are removed
2314static void bc_num_parseDecimal(BcNum *n, const char *val)
2315{
2316 size_t len, i;
2317 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002318
2319 len = strlen(val);
2320 if (len == 0)
2321 return;
2322
Denys Vlasenko9311e012018-12-10 11:54:18 +01002323 bc_num_expand(n, len);
2324
2325 ptr = strchr(val, '.');
2326
2327 n->rdx = 0;
2328 if (ptr != NULL)
2329 n->rdx = (size_t)((val + len) - (ptr + 1));
2330
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002331 for (i = 0; val[i]; ++i) {
2332 if (val[i] != '0' && val[i] != '.') {
2333 // Not entirely zero value - convert it, and exit
2334 i = len - 1;
2335 for (;;) {
2336 n->num[n->len] = val[i] - '0';
2337 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002338 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002339 if ((ssize_t)--i == (ssize_t)-1) break;
2340 if (val[i] == '.') goto skip_dot;
2341 }
2342 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002343 }
2344 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002345 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002346}
2347
2348// Note: n is already "bc_num_zero()"ed,
2349// leading zeroes in "val" are removed
2350static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2351{
2352 BcStatus s;
2353 BcNum temp, mult, result;
2354 BcDig c = '\0';
2355 unsigned long v;
2356 size_t i, digits;
2357
2358 for (i = 0; ; ++i) {
2359 if (val[i] == '\0')
2360 return;
2361 if (val[i] != '.' && val[i] != '0')
2362 break;
2363 }
2364
2365 bc_num_init_DEF_SIZE(&temp);
2366 bc_num_init_DEF_SIZE(&mult);
2367
2368 for (;;) {
2369 c = *val++;
2370 if (c == '\0') goto int_err;
2371 if (c == '.') break;
2372
2373 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2374
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002375 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002376 if (s) goto int_err;
2377 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002378 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002379 if (s) goto int_err;
2380 }
2381
2382 bc_num_init(&result, base->len);
2383 //bc_num_zero(&result); - already is
2384 bc_num_one(&mult);
2385
2386 digits = 0;
2387 for (;;) {
2388 c = *val++;
2389 if (c == '\0') break;
2390 digits++;
2391
2392 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2393
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002394 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002395 if (s) goto err;
2396 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002397 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002398 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002399 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002400 if (s) goto err;
2401 }
2402
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002403 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002404 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002405 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002406 if (s) goto err;
2407
2408 if (n->len != 0) {
2409 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2410 } else
2411 bc_num_zero(n);
2412
2413err:
2414 bc_num_free(&result);
2415int_err:
2416 bc_num_free(&mult);
2417 bc_num_free(&temp);
2418}
2419
Denys Vlasenko29301232018-12-11 15:29:32 +01002420static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002421 size_t base_t)
2422{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002423 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002424 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002425
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002426 bc_num_zero(n);
2427 while (*val == '0') val++;
2428
Gavin Howard01055ba2018-11-03 11:00:21 -06002429 if (base_t == 10)
2430 bc_num_parseDecimal(n, val);
2431 else
2432 bc_num_parseBase(n, val, base);
2433
Denys Vlasenko29301232018-12-11 15:29:32 +01002434 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002435}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002436#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002437# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002438#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002439
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002440static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002441{
2442 BcStatus s;
2443 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2444 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2445 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2446
2447 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2448 bc_num_expand(b, req);
2449
2450 if (a->len == 0) {
2451 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002452 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002453 }
2454 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002455 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002456 else if (BC_NUM_ONE(a)) {
2457 bc_num_one(b);
2458 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002459 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002460 }
2461
2462 scale = BC_MAX(scale, a->rdx) + 1;
2463 len = a->len + scale;
2464
2465 bc_num_init(&num1, len);
2466 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002467 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002468
2469 bc_num_one(&half);
2470 half.num[0] = 5;
2471 half.rdx = 1;
2472
2473 bc_num_init(&f, len);
2474 bc_num_init(&fprime, len);
2475
2476 x0 = &num1;
2477 x1 = &num2;
2478
2479 bc_num_one(x0);
2480 pow = BC_NUM_INT(a);
2481
2482 if (pow) {
2483
2484 if (pow & 1)
2485 x0->num[0] = 2;
2486 else
2487 x0->num[0] = 6;
2488
2489 pow -= 2 - (pow & 1);
2490
2491 bc_num_extend(x0, pow);
2492
2493 // Make sure to move the radix back.
2494 x0->rdx -= pow;
2495 }
2496
2497 x0->rdx = digs = digs1 = 0;
2498 resrdx = scale + 2;
2499 len = BC_NUM_INT(x0) + resrdx - 1;
2500
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002501 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002502
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002503 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002504 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002505 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002506 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002507 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002508 if (s) goto err;
2509
2510 cmp = bc_num_cmp(x1, x0);
2511 digs = x1->len - (unsigned long long) llabs(cmp);
2512
2513 if (cmp == cmp2 && digs == digs1)
2514 times += 1;
2515 else
2516 times = 0;
2517
2518 resrdx += times > 4;
2519
2520 cmp2 = cmp1;
2521 cmp1 = cmp;
2522 digs1 = digs;
2523
2524 temp = x0;
2525 x0 = x1;
2526 x1 = temp;
2527 }
2528
Gavin Howard01055ba2018-11-03 11:00:21 -06002529 bc_num_copy(b, x0);
2530 scale -= 1;
2531 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2532
2533err:
2534 bc_num_free(&fprime);
2535 bc_num_free(&f);
2536 bc_num_free(&half);
2537 bc_num_free(&num2);
2538 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002539 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002540}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002541#if ERRORS_ARE_FATAL
2542# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2543#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002544
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002545static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002546 size_t scale)
2547{
2548 BcStatus s;
2549 BcNum num2, *ptr_a;
2550 bool init = false;
2551 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2552
2553 if (c == a) {
2554 memcpy(&num2, c, sizeof(BcNum));
2555 ptr_a = &num2;
2556 bc_num_init(c, len);
2557 init = true;
2558 }
2559 else {
2560 ptr_a = a;
2561 bc_num_expand(c, len);
2562 }
2563
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002564 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002565
2566 if (init) bc_num_free(&num2);
2567
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002568 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002569}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002570#if ERRORS_ARE_FATAL
2571# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2572#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002573
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002574#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002575static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002576{
2577 BcStatus s;
2578 BcNum base, exp, two, temp;
2579
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002580 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002581 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002582 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002583 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002584 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002585 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002586
2587 bc_num_expand(d, c->len);
2588 bc_num_init(&base, c->len);
2589 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002590 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002591 bc_num_init(&temp, b->len);
2592
2593 bc_num_one(&two);
2594 two.num[0] = 2;
2595 bc_num_one(d);
2596
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002597 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002598 if (s) goto err;
2599 bc_num_copy(&exp, b);
2600
2601 while (exp.len != 0) {
2602
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002603 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002604 if (s) goto err;
2605
2606 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002607 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002608 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002609 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002610 if (s) goto err;
2611 }
2612
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002613 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002614 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002615 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002616 if (s) goto err;
2617 }
2618
2619err:
2620 bc_num_free(&temp);
2621 bc_num_free(&two);
2622 bc_num_free(&exp);
2623 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002624 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002625}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002626#if ERRORS_ARE_FATAL
2627# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2628#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002629#endif // ENABLE_DC
2630
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002631#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002632static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002633{
2634 BcId a;
2635 size_t i;
2636
2637 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002638 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002639 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002640 }
2641
2642 a.idx = var;
2643 a.name = name;
2644
2645 bc_vec_push(&f->autos, &a);
2646
Denys Vlasenko29301232018-12-11 15:29:32 +01002647 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002648}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002649#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002650# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002651#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002652#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002653
2654static void bc_func_init(BcFunc *f)
2655{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002656 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002657 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2658 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2659 f->nparams = 0;
2660}
2661
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002662static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002663{
2664 BcFunc *f = (BcFunc *) func;
2665 bc_vec_free(&f->code);
2666 bc_vec_free(&f->autos);
2667 bc_vec_free(&f->labels);
2668}
2669
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002670static void bc_array_expand(BcVec *a, size_t len);
2671
Gavin Howard01055ba2018-11-03 11:00:21 -06002672static void bc_array_init(BcVec *a, bool nums)
2673{
2674 if (nums)
2675 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2676 else
2677 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2678 bc_array_expand(a, 1);
2679}
2680
Gavin Howard01055ba2018-11-03 11:00:21 -06002681static void bc_array_expand(BcVec *a, size_t len)
2682{
2683 BcResultData data;
2684
2685 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2686 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002687 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002688 bc_vec_push(a, &data.n);
2689 }
2690 }
2691 else {
2692 while (len > a->len) {
2693 bc_array_init(&data.v, true);
2694 bc_vec_push(a, &data.v);
2695 }
2696 }
2697}
2698
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002699static void bc_array_copy(BcVec *d, const BcVec *s)
2700{
2701 size_t i;
2702
2703 bc_vec_pop_all(d);
2704 bc_vec_expand(d, s->cap);
2705 d->len = s->len;
2706
2707 for (i = 0; i < s->len; ++i) {
2708 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2709 bc_num_init(dnum, snum->len);
2710 bc_num_copy(dnum, snum);
2711 }
2712}
2713
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002714static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002715{
2716 free(*((char **) string));
2717}
2718
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002719#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002720static void bc_result_copy(BcResult *d, BcResult *src)
2721{
2722 d->t = src->t;
2723
2724 switch (d->t) {
2725
2726 case BC_RESULT_TEMP:
2727 case BC_RESULT_IBASE:
2728 case BC_RESULT_SCALE:
2729 case BC_RESULT_OBASE:
2730 {
2731 bc_num_init(&d->d.n, src->d.n.len);
2732 bc_num_copy(&d->d.n, &src->d.n);
2733 break;
2734 }
2735
2736 case BC_RESULT_VAR:
2737 case BC_RESULT_ARRAY:
2738 case BC_RESULT_ARRAY_ELEM:
2739 {
2740 d->d.id.name = xstrdup(src->d.id.name);
2741 break;
2742 }
2743
2744 case BC_RESULT_CONSTANT:
2745 case BC_RESULT_LAST:
2746 case BC_RESULT_ONE:
2747 case BC_RESULT_STR:
2748 {
2749 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2750 break;
2751 }
2752 }
2753}
2754#endif // ENABLE_DC
2755
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002756static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002757{
2758 BcResult *r = (BcResult *) result;
2759
2760 switch (r->t) {
2761
2762 case BC_RESULT_TEMP:
2763 case BC_RESULT_IBASE:
2764 case BC_RESULT_SCALE:
2765 case BC_RESULT_OBASE:
2766 {
2767 bc_num_free(&r->d.n);
2768 break;
2769 }
2770
2771 case BC_RESULT_VAR:
2772 case BC_RESULT_ARRAY:
2773 case BC_RESULT_ARRAY_ELEM:
2774 {
2775 free(r->d.id.name);
2776 break;
2777 }
2778
2779 default:
2780 {
2781 // Do nothing.
2782 break;
2783 }
2784 }
2785}
2786
2787static void bc_lex_lineComment(BcLex *l)
2788{
2789 l->t.t = BC_LEX_WHITESPACE;
2790 while (l->i < l->len && l->buf[l->i++] != '\n');
2791 --l->i;
2792}
2793
2794static void bc_lex_whitespace(BcLex *l)
2795{
2796 char c;
2797 l->t.t = BC_LEX_WHITESPACE;
2798 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2799}
2800
Denys Vlasenko29301232018-12-11 15:29:32 +01002801static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002802{
2803 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002804 size_t len, bslashes, i, ccnt;
2805 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002806
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002807 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002808 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002809 bslashes = 0;
2810 ccnt = i = 0;
2811 for (;;) {
2812 char c = buf[i];
2813 if (c == '\0')
2814 break;
2815 if (c == '\\' && buf[i + 1] == '\n') {
2816 i += 2;
2817 bslashes++;
2818 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002819 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002820 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2821 if (c != '.') break;
2822 // if '.' was already seen, stop on second one:
2823 if (pt) break;
2824 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002825 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002826 // buf[i] is one of "0-9A-F."
2827 i++;
2828 if (c != '.')
2829 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002830 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002831 //i is buf[i] index of the first not-yet-parsed char
2832 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002833
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002834 //ccnt is the number of chars in the number string, excluding possible
2835 //trailing "." and possible following trailing "\<newline>"(s).
2836 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2837
Denys Vlasenko64074a12018-12-07 15:50:14 +01002838 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2839 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2840 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002841 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002842 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002843
Denys Vlasenko7d628012018-12-04 21:46:47 +01002844 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002845 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002846 bc_vec_push(&l->t.v, &start);
2847
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002848 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002849 // If we have hit a backslash, skip it. We don't have
2850 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002851 if (*buf == '\\') {
2852 buf += 2;
2853 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002854 continue;
2855 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002856 bc_vec_push(&l->t.v, buf);
2857 buf++;
2858 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002859 }
2860
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002861 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002862
Denys Vlasenko29301232018-12-11 15:29:32 +01002863 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002864}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002865#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002866# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002867#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002868
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002869static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002870{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002871 size_t i;
2872 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002873
2874 l->t.t = BC_LEX_NAME;
2875
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002876 i = 0;
2877 buf = l->buf + l->i - 1;
2878 for (;;) {
2879 char c = buf[i];
2880 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2881 i++;
2882 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002883
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002884#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002885 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2886 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2887 if (i > BC_MAX_STRING)
2888 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2889 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002890#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002891 bc_vec_string(&l->t.v, i, buf);
2892
2893 // Increment the index. We minus 1 because it has already been incremented.
2894 l->i += i - 1;
2895
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002896 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002897}
2898
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002899static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002900{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002901 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002902}
2903
2904static void bc_lex_free(BcLex *l)
2905{
2906 bc_vec_free(&l->t.v);
2907}
2908
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002909static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002910{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002911 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002912 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002913}
2914
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002915static BC_STATUS zbc_lex_token(BcLex *l);
2916static BC_STATUS zdc_lex_token(BcLex *l);
2917
2918static BC_STATUS zcommon_lex_token(BcLex *l)
2919{
2920 if (IS_BC) {
2921 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2922 }
2923 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2924}
2925
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002926static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002927{
2928 BcStatus s;
2929
2930 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002931 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002932
2933 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002934 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002935 l->t.t = BC_LEX_EOF;
2936
2937 l->newline = (l->i == l->len);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002938 if (l->newline) RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002939
2940 // Loop until failure or we don't have whitespace. This
2941 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002942 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002943 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002944//TODO: replace pointer with if(IS_BC)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002945 ERROR_RETURN(s =) zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002946 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2947
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002948 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002949}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002950#if ERRORS_ARE_FATAL
2951# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS)
2952#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002953
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002954static BC_STATUS zbc_lex_text(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002955{
2956 l->buf = text;
2957 l->i = 0;
2958 l->len = strlen(text);
2959 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002960 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002961}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002962#if ERRORS_ARE_FATAL
2963# define zbc_lex_text(...) (zbc_lex_text(__VA_ARGS__), BC_STATUS_SUCCESS)
2964#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002965
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002966#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002967static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002968{
2969 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002970 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002971 const char *buf = l->buf + l->i - 1;
2972
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002973 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2974 const char *keyword8 = bc_lex_kws[i].name8;
2975 unsigned j = 0;
2976 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2977 j++;
2978 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002979 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002980 if (keyword8[j] != '\0')
2981 continue;
2982 match:
2983 // buf starts with keyword bc_lex_kws[i]
2984 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002985 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002986 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002987 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002988 }
2989
2990 // We minus 1 because the index has already been incremented.
2991 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002992 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002993 }
2994
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002995 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002996
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002997 if (l->t.v.len > 2) {
2998 // Prevent this:
2999 // >>> qwe=1
3000 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3001 // '
3002 unsigned len = strchrnul(buf, '\n') - buf;
3003 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3004 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003005
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003006 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003007}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003008#if ERRORS_ARE_FATAL
3009# define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__), BC_STATUS_SUCCESS)
3010#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003011
Denys Vlasenko26819db2018-12-12 16:08:46 +01003012static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003013{
3014 size_t len, nls = 0, i = l->i;
3015 char c;
3016
3017 l->t.t = BC_LEX_STR;
3018
Denys Vlasenko26819db2018-12-12 16:08:46 +01003019 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i])
3020 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003021
3022 if (c == '\0') {
3023 l->i = i;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003024 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003025 }
3026
3027 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003028 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3029 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3030 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003031 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003032 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003033 bc_vec_string(&l->t.v, len, l->buf + l->i);
3034
3035 l->i = i + 1;
3036 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003037 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003038
Denys Vlasenko26819db2018-12-12 16:08:46 +01003039 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003040}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003041#if ERRORS_ARE_FATAL
3042# define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3043#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003044
3045static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3046{
3047 if (l->buf[l->i] == '=') {
3048 ++l->i;
3049 l->t.t = with;
3050 }
3051 else
3052 l->t.t = without;
3053}
3054
Denys Vlasenko29301232018-12-11 15:29:32 +01003055static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003056{
3057 size_t i, nls = 0;
3058 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003059
3060 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003061 i = ++l->i;
3062 for (;;) {
3063 char c = buf[i];
3064 check_star:
3065 if (c == '*') {
3066 c = buf[++i];
3067 if (c == '/')
3068 break;
3069 goto check_star;
3070 }
3071 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003072 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003073 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003074 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003075 nls += (c == '\n');
3076 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003077 }
3078
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003079 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003080 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003081 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003082
Denys Vlasenko29301232018-12-11 15:29:32 +01003083 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003084}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003085#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003086# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003087#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003088
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003089static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003090{
3091 BcStatus s = BC_STATUS_SUCCESS;
3092 char c = l->buf[l->i++], c2;
3093
3094 // This is the workhorse of the lexer.
3095 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003096 case '\0':
3097 case '\n':
Gavin Howard01055ba2018-11-03 11:00:21 -06003098 l->newline = true;
3099 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3100 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003101 case '\t':
3102 case '\v':
3103 case '\f':
3104 case '\r':
3105 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003106 bc_lex_whitespace(l);
3107 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003109 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003110 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003111 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003112 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003113 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003114 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003115 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003116 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003119 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003120 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003121 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003122 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003123 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3125 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003126 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 c2 = l->buf[l->i];
3128 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003129 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003130 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003131 ++l->i;
3132 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003133 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003134 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003135 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003138 case '(':
3139 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3141 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003143 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3144 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 c2 = l->buf[l->i];
3147 if (c2 == '+') {
3148 ++l->i;
3149 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003150 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003151 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3152 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003154 l->t.t = BC_LEX_COMMA;
3155 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003157 c2 = l->buf[l->i];
3158 if (c2 == '-') {
3159 ++l->i;
3160 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003161 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003162 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3163 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003165 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003166 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 else {
3168 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003169 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003170 }
3171 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003173 c2 = l->buf[l->i];
3174 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003175 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003176 else
3177 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3178 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003179 case '0':
3180 case '1':
3181 case '2':
3182 case '3':
3183 case '4':
3184 case '5':
3185 case '6':
3186 case '7':
3187 case '8':
3188 case '9':
3189 case 'A':
3190 case 'B':
3191 case 'C':
3192 case 'D':
3193 case 'E':
3194 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003195 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 l->t.t = BC_LEX_SCOLON;
3199 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3202 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003204 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3205 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3208 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003209 case '[':
3210 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003211 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3212 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 if (l->buf[l->i] == '\n') {
3215 l->t.t = BC_LEX_WHITESPACE;
3216 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003217 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003218 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3222 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 case 'a':
3224 case 'b':
3225 case 'c':
3226 case 'd':
3227 case 'e':
3228 case 'f':
3229 case 'g':
3230 case 'h':
3231 case 'i':
3232 case 'j':
3233 case 'k':
3234 case 'l':
3235 case 'm':
3236 case 'n':
3237 case 'o':
3238 case 'p':
3239 case 'q':
3240 case 'r':
3241 case 's':
3242 case 't':
3243 case 'u':
3244 case 'v':
3245 case 'w':
3246 case 'x':
3247 case 'y':
3248 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003249 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003251 case '{':
3252 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003253 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3254 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003256 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003257 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003258 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003259 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 ++l->i;
3261 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003262 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003263 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003264 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003268 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003269 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 }
3272
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003273 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003274}
3275#endif // ENABLE_BC
3276
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003277#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003278static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003279{
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 if (isspace(l->buf[l->i - 1])) {
3281 bc_lex_whitespace(l);
3282 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003283 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003284 RETURN_STATUS(bc_error("extended register"));
3285 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 }
3287 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003288 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003289 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003290 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 l->t.t = BC_LEX_NAME;
3292 }
3293
Denys Vlasenko29301232018-12-11 15:29:32 +01003294 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003295}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003296#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003297# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003298#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003299
Denys Vlasenko29301232018-12-11 15:29:32 +01003300static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003301{
3302 size_t depth = 1, nls = 0, i = l->i;
3303 char c;
3304
3305 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003306 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003307
3308 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3309
3310 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3311 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3312 nls += (c == '\n');
3313
3314 if (depth) bc_vec_push(&l->t.v, &c);
3315 }
3316
3317 if (c == '\0') {
3318 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003319 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003320 }
3321
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003322 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003323 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3324 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3325 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003326 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003327 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003328
3329 l->i = i;
3330 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003331 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003332
Denys Vlasenko29301232018-12-11 15:29:32 +01003333 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003334}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003335#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003336# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003337#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003338
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003339static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003340{
3341 BcStatus s = BC_STATUS_SUCCESS;
3342 char c = l->buf[l->i++], c2;
3343 size_t i;
3344
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003345 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3346 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003347 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003348 }
3349
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003350 if (c >= '%' && c <= '~'
3351 && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID
3352 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003353 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003354 }
3355
3356 // This is the workhorse of the lexer.
3357 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003358 case '\0':
Gavin Howard01055ba2018-11-03 11:00:21 -06003359 l->t.t = BC_LEX_EOF;
3360 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003361 case '\n':
3362 case '\t':
3363 case '\v':
3364 case '\f':
3365 case '\r':
3366 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003367 l->newline = (c == '\n');
3368 bc_lex_whitespace(l);
3369 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003370 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003371 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003372 if (c2 == '=')
3373 l->t.t = BC_LEX_OP_REL_NE;
3374 else if (c2 == '<')
3375 l->t.t = BC_LEX_OP_REL_LE;
3376 else if (c2 == '>')
3377 l->t.t = BC_LEX_OP_REL_GE;
3378 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003379 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003380 ++l->i;
3381 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003382 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003383 bc_lex_lineComment(l);
3384 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003385 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003386 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003387 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003388 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003389 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003390 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003391 case '0':
3392 case '1':
3393 case '2':
3394 case '3':
3395 case '4':
3396 case '5':
3397 case '6':
3398 case '7':
3399 case '8':
3400 case '9':
3401 case 'A':
3402 case 'B':
3403 case 'C':
3404 case 'D':
3405 case 'E':
3406 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003407 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003408 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003409 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003410 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003411 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003412 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003413 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003414 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003415 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003416 }
3417
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003418 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003419}
3420#endif // ENABLE_DC
3421
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003422static void bc_program_addFunc(char *name, size_t *idx);
3423
Gavin Howard01055ba2018-11-03 11:00:21 -06003424static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3425{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003426 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003427 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003428}
3429
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003430#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3431
Gavin Howard01055ba2018-11-03 11:00:21 -06003432static void bc_parse_pushName(BcParse *p, char *name)
3433{
3434 size_t i = 0, len = strlen(name);
3435
3436 for (; i < len; ++i) bc_parse_push(p, name[i]);
3437 bc_parse_push(p, BC_PARSE_STREND);
3438
3439 free(name);
3440}
3441
3442static void bc_parse_pushIndex(BcParse *p, size_t idx)
3443{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003444 size_t mask;
3445 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003446
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003447 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3448 amt = sizeof(idx);
3449 do {
3450 if (idx & mask) break;
3451 mask >>= 8;
3452 amt--;
3453 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003454
3455 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003456
3457 while (idx != 0) {
3458 bc_parse_push(p, (unsigned char)idx);
3459 idx >>= 8;
3460 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003461}
3462
3463static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3464{
3465 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003466 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003467
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003468 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003469
3470 bc_parse_push(p, BC_INST_NUM);
3471 bc_parse_pushIndex(p, idx);
3472
3473 ++(*nexs);
3474 (*prev) = BC_INST_NUM;
3475}
3476
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003477static BC_STATUS zbc_parse_parse(BcParse *p);
3478static BC_STATUS zdc_parse_parse(BcParse *p);
3479
3480static BC_STATUS zcommon_parse(BcParse *p)
3481{
3482 if (IS_BC) {
3483 IF_BC(RETURN_STATUS(zbc_parse_parse(p));)
3484 }
3485 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3486}
3487
Denys Vlasenko26819db2018-12-12 16:08:46 +01003488static BC_STATUS zbc_parse_text(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003489{
3490 BcStatus s;
3491
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003492 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003493
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003494 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 p->l.t.t = BC_LEX_INVALID;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003496 s = BC_STATUS_SUCCESS;
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003497 ERROR_RETURN(s =) zcommon_parse(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003498 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003499 if (!BC_PARSE_CAN_EXEC(p))
Denys Vlasenko26819db2018-12-12 16:08:46 +01003500 RETURN_STATUS(bc_error("file is not executable"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003501 }
3502
Denys Vlasenko26819db2018-12-12 16:08:46 +01003503 RETURN_STATUS(zbc_lex_text(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003504}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003505#if ERRORS_ARE_FATAL
3506# define zbc_parse_text(...) (zbc_parse_text(__VA_ARGS__), BC_STATUS_SUCCESS)
3507#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003508
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003509// Called when parsing or execution detects a failure,
3510// resets execution structures.
3511static void bc_program_reset(void)
3512{
3513 BcFunc *f;
3514 BcInstPtr *ip;
3515
3516 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3517 bc_vec_pop_all(&G.prog.results);
3518
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003519 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003520 ip = bc_vec_top(&G.prog.stack);
3521 ip->idx = f->code.len;
3522}
3523
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003524#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003525 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003526
Denys Vlasenko26819db2018-12-12 16:08:46 +01003527// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003528// resets parsing structures.
3529static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003530{
3531 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003532 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003533 bc_vec_pop_all(&p->func->code);
3534 bc_vec_pop_all(&p->func->autos);
3535 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003536
3537 bc_parse_updateFunc(p, BC_PROG_MAIN);
3538 }
3539
3540 p->l.i = p->l.len;
3541 p->l.t.t = BC_LEX_EOF;
3542 p->auto_part = (p->nbraces = 0);
3543
3544 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003545 bc_vec_pop_all(&p->exits);
3546 bc_vec_pop_all(&p->conds);
3547 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003548
Denys Vlasenkod38af482018-12-04 19:11:02 +01003549 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003550}
3551
3552static void bc_parse_free(BcParse *p)
3553{
3554 bc_vec_free(&p->flags);
3555 bc_vec_free(&p->exits);
3556 bc_vec_free(&p->conds);
3557 bc_vec_free(&p->ops);
3558 bc_lex_free(&p->l);
3559}
3560
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003561static void bc_parse_create(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06003562{
3563 memset(p, 0, sizeof(BcParse));
3564
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003565 bc_lex_init(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003566 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3567 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3568 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003569 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003570 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3571
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003572 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003573 bc_parse_updateFunc(p, func);
3574}
3575
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003576#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003577
3578#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3579#define BC_PARSE_LEAF(p, rparen) \
3580 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3581 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3582
3583// We can calculate the conversion between tokens and exprs by subtracting the
3584// position of the first operator in the lex enum and adding the position of the
3585// first in the expr enum. Note: This only works for binary operators.
3586#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3587
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003588static BC_STATUS zbc_parse_else(BcParse *p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003589static BC_STATUS zbc_parse_stmt(BcParse *p);
3590static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003591static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003592#if ERRORS_ARE_FATAL
3593# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
3594# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
3595# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
3596#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003597
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003598static BC_STATUS zbc_parse_operator(BcParse *p, BcLexType type, size_t start,
Gavin Howard01055ba2018-11-03 11:00:21 -06003599 size_t *nexprs, bool next)
3600{
3601 BcStatus s = BC_STATUS_SUCCESS;
3602 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003603 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3604 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003605
3606 while (p->ops.len > start) {
3607
3608 t = BC_PARSE_TOP_OP(p);
3609 if (t == BC_LEX_LPAREN) break;
3610
Denys Vlasenko65437582018-12-05 19:37:19 +01003611 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003612 if (l >= r && (l != r || !left)) break;
3613
3614 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3615 bc_vec_pop(&p->ops);
3616 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3617 }
3618
3619 bc_vec_push(&p->ops, &type);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003620 if (next) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003621
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003622 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003623}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003624#if ERRORS_ARE_FATAL
3625# define zbc_parse_operator(...) (zbc_parse_operator(__VA_ARGS__), BC_STATUS_SUCCESS)
3626#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003627
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003628static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003629{
3630 BcLexType top;
3631
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003632 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003633 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003634 top = BC_PARSE_TOP_OP(p);
3635
3636 while (top != BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003637 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3638
3639 bc_vec_pop(&p->ops);
3640 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3641
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003642 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003643 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003644 top = BC_PARSE_TOP_OP(p);
3645 }
3646
3647 bc_vec_pop(&p->ops);
3648
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003649 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003650}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003651#if ERRORS_ARE_FATAL
3652# define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__), BC_STATUS_SUCCESS)
3653#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003654
Denys Vlasenko26819db2018-12-12 16:08:46 +01003655static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003656{
3657 BcStatus s;
3658 bool comma = false;
3659 size_t nparams;
3660
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003661 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003662 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003663
3664 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003665 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003666 s = zbc_parse_expr(p, flags, bc_parse_next_param);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003667 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003668
3669 comma = p->l.t.t == BC_LEX_COMMA;
3670 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003671 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003672 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003673 }
3674 }
3675
Denys Vlasenko26819db2018-12-12 16:08:46 +01003676 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003677 bc_parse_push(p, BC_INST_CALL);
3678 bc_parse_pushIndex(p, nparams);
3679
Denys Vlasenko26819db2018-12-12 16:08:46 +01003680 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003681}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003682#if ERRORS_ARE_FATAL
3683# define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__), BC_STATUS_SUCCESS)
3684#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003685
Denys Vlasenko26819db2018-12-12 16:08:46 +01003686static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003687{
3688 BcStatus s;
3689 BcId entry, *entry_ptr;
3690 size_t idx;
3691
3692 entry.name = name;
3693
Denys Vlasenko26819db2018-12-12 16:08:46 +01003694 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003695 if (s) goto err;
3696
3697 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003698 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003699 goto err;
3700 }
3701
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003702 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003703
3704 if (idx == BC_VEC_INVALID_IDX) {
3705 name = xstrdup(entry.name);
3706 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003707 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003708 free(entry.name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003709 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003710 free(name);
3711
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003712 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003713 bc_parse_pushIndex(p, entry_ptr->idx);
3714
Denys Vlasenko26819db2018-12-12 16:08:46 +01003715 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003716
3717err:
3718 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003719 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003720}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003721#if ERRORS_ARE_FATAL
3722# define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__), BC_STATUS_SUCCESS)
3723#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003724
Denys Vlasenko26819db2018-12-12 16:08:46 +01003725static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003726{
3727 BcStatus s;
3728 char *name;
3729
3730 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003731 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003732 if (s) goto err;
3733
3734 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003735 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003736 if (s) goto err;
3737
3738 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003739 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003740 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003741 goto err;
3742 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003743 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003744 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003745 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003746 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003747 s = zbc_parse_expr(p, flags, bc_parse_next_elem);
Gavin Howard01055ba2018-11-03 11:00:21 -06003748 if (s) goto err;
3749 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003750 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003751 if (s) goto err;
3752 bc_parse_push(p, *type);
3753 bc_parse_pushName(p, name);
3754 }
3755 else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003756 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003757 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003758 goto err;
3759 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003761 s = zbc_parse_call(p, name, flags);
3762 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 *type = BC_INST_VAR;
3764 bc_parse_push(p, BC_INST_VAR);
3765 bc_parse_pushName(p, name);
3766 }
3767
Denys Vlasenko26819db2018-12-12 16:08:46 +01003768 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003769
3770err:
3771 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003772 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003773}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003774#if ERRORS_ARE_FATAL
3775# define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__), BC_STATUS_SUCCESS)
3776#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003777
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003778static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003779{
3780 BcStatus s;
3781
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003782 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003783 if (s) RETURN_STATUS(s);
3784 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003785
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003786 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003787 if (s) RETURN_STATUS(s);
3788 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003789
3790 bc_parse_push(p, BC_INST_READ);
3791
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003792 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003793}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003794#if ERRORS_ARE_FATAL
3795# define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__), BC_STATUS_SUCCESS)
3796#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003797
Denys Vlasenko26819db2018-12-12 16:08:46 +01003798static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 BcInst *prev)
3800{
3801 BcStatus s;
3802
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003803 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003804 if (s) RETURN_STATUS(s);
3805 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003806
3807 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3808
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003809 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003810 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003811
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003812 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003813 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003814
Denys Vlasenko26819db2018-12-12 16:08:46 +01003815 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003816
3817 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3818 bc_parse_push(p, *prev);
3819
Denys Vlasenko26819db2018-12-12 16:08:46 +01003820 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003821}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003822#if ERRORS_ARE_FATAL
3823# define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
3824#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003825
Denys Vlasenko26819db2018-12-12 16:08:46 +01003826static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003827{
3828 BcStatus s;
3829
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003830 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003831 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003832
3833 if (p->l.t.t != BC_LEX_LPAREN) {
3834 *type = BC_INST_SCALE;
3835 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003836 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003837 }
3838
3839 *type = BC_INST_SCALE_FUNC;
3840 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3841
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003842 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003843 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003844
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003845 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003846 if (s) RETURN_STATUS(s);
3847 if (p->l.t.t != BC_LEX_RPAREN)
3848 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003849 bc_parse_push(p, BC_INST_SCALE_FUNC);
3850
Denys Vlasenko26819db2018-12-12 16:08:46 +01003851 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003852}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003853#if ERRORS_ARE_FATAL
3854# define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__), BC_STATUS_SUCCESS)
3855#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003856
Denys Vlasenko26819db2018-12-12 16:08:46 +01003857static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003858 size_t *nexprs, uint8_t flags)
3859{
3860 BcStatus s;
3861 BcLexType type;
3862 char inst;
3863 BcInst etype = *prev;
3864
3865 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3866 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3867 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3868 {
3869 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3870 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003871 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 }
3873 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003874 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3875 *paren_expr = true;
3876
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003877 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003878 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003879 type = p->l.t.t;
3880
3881 // Because we parse the next part of the expression
3882 // right here, we need to increment this.
3883 *nexprs = *nexprs + 1;
3884
3885 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003886 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01003887 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003889 case BC_LEX_KEY_IBASE:
3890 case BC_LEX_KEY_LAST:
3891 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06003892 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003893 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003895 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003896 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003897 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003898 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003899 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003900 else
3901 bc_parse_push(p, BC_INST_SCALE);
3902 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003903 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003904 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003906 }
3907
3908 if (!s) bc_parse_push(p, inst);
3909 }
3910
Denys Vlasenko26819db2018-12-12 16:08:46 +01003911 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003912}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003913#if ERRORS_ARE_FATAL
3914# define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
3915#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003916
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003917static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06003918 bool rparen, size_t *nexprs)
3919{
3920 BcStatus s;
3921 BcLexType type;
3922 BcInst etype = *prev;
3923
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003924 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003925 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003926
3927 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3928 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3929 BC_LEX_OP_MINUS :
3930 BC_LEX_NEG;
3931 *prev = BC_PARSE_TOKEN_INST(type);
3932
3933 // We can just push onto the op stack because this is the largest
3934 // precedence operator that gets pushed. Inc/dec does not.
3935 if (type != BC_LEX_OP_MINUS)
3936 bc_vec_push(&p->ops, &type);
3937 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003938 s = zbc_parse_operator(p, type, ops_bgn, nexprs, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003940 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003941}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003942#if ERRORS_ARE_FATAL
3943# define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__), BC_STATUS_SUCCESS)
3944#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003945
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003946static BC_STATUS zbc_parse_string(BcParse *p, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06003947{
3948 char *str = xstrdup(p->l.t.v.v);
3949
3950 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003951 bc_parse_pushIndex(p, G.prog.strs.len);
3952 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003953 bc_parse_push(p, inst);
3954
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003955 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003956}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003957#if ERRORS_ARE_FATAL
3958# define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3959#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003960
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003961static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003962{
3963 BcStatus s;
3964 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003965 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06003966
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003967 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003968 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003969
3970 type = p->l.t.t;
3971
3972 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003973 RETURN_STATUS(bc_error("bad print statement"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003974
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003975 comma = false;
3976 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003977 if (type == BC_LEX_STR) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003978 s = zbc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003979 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003980 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003981 s = zbc_parse_expr(p, 0, bc_parse_next_print);
3982 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003983 bc_parse_push(p, BC_INST_PRINT_POP);
3984 }
3985
Gavin Howard01055ba2018-11-03 11:00:21 -06003986 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003987 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003988 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003989 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003990 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003991 type = p->l.t.t;
3992 }
3993
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003994 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003995
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003996 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003997}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003998#if ERRORS_ARE_FATAL
3999# define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__), BC_STATUS_SUCCESS)
4000#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004001
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004002static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004003{
4004 BcStatus s;
4005 BcLexType t;
4006 bool paren;
4007
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004008 if (!BC_PARSE_FUNC(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004009
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004010 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004011 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004012
4013 t = p->l.t.t;
4014 paren = t == BC_LEX_LPAREN;
4015
4016 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4017 bc_parse_push(p, BC_INST_RET0);
4018 else {
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004019 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004020 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004021 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004022 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004024 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004025
4026 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004027 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004028 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06004029 }
4030
4031 bc_parse_push(p, BC_INST_RET);
4032 }
4033
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004034 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004035}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004036#if ERRORS_ARE_FATAL
4037# define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS)
4038#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004039
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004040static BC_STATUS zbc_parse_endBody(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004041{
4042 BcStatus s = BC_STATUS_SUCCESS;
4043
4044 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004045 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004046
4047 if (brace) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004048 if (p->l.t.t != BC_LEX_RBRACE)
4049 RETURN_STATUS(bc_error_bad_token());
4050 if (!p->nbraces)
4051 RETURN_STATUS(bc_error_bad_token());
4052 --p->nbraces;
4053 s = zbc_lex_next(&p->l);
4054 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004055 }
4056
4057 if (BC_PARSE_IF(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004058 uint8_t *flag_ptr;
4059
4060 while (p->l.t.t == BC_LEX_NLINE) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004061 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004062 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 }
4064
4065 bc_vec_pop(&p->flags);
4066
4067 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4068 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4069
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004070 if (p->l.t.t == BC_LEX_KEY_ELSE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004071 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004072 }
4073 else if (BC_PARSE_ELSE(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004074 BcInstPtr *ip;
4075 size_t *label;
4076
4077 bc_vec_pop(&p->flags);
4078
4079 ip = bc_vec_top(&p->exits);
4080 label = bc_vec_item(&p->func->labels, ip->idx);
4081 *label = p->func->code.len;
4082
4083 bc_vec_pop(&p->exits);
4084 }
4085 else if (BC_PARSE_FUNC_INNER(p)) {
4086 bc_parse_push(p, BC_INST_RET0);
4087 bc_parse_updateFunc(p, BC_PROG_MAIN);
4088 bc_vec_pop(&p->flags);
4089 }
4090 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004091 BcInstPtr *ip = bc_vec_top(&p->exits);
4092 size_t *label = bc_vec_top(&p->conds);
4093
4094 bc_parse_push(p, BC_INST_JUMP);
4095 bc_parse_pushIndex(p, *label);
4096
4097 label = bc_vec_item(&p->func->labels, ip->idx);
4098 *label = p->func->code.len;
4099
4100 bc_vec_pop(&p->flags);
4101 bc_vec_pop(&p->exits);
4102 bc_vec_pop(&p->conds);
4103 }
4104
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004105 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004106}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004107#if ERRORS_ARE_FATAL
4108# define zbc_parse_endBody(...) (zbc_parse_endBody(__VA_ARGS__), BC_STATUS_SUCCESS)
4109#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004110
4111static void bc_parse_startBody(BcParse *p, uint8_t flags)
4112{
4113 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4114 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4115 flags |= BC_PARSE_FLAG_BODY;
4116 bc_vec_push(&p->flags, &flags);
4117}
4118
4119static void bc_parse_noElse(BcParse *p)
4120{
4121 BcInstPtr *ip;
4122 size_t *label;
4123 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4124
4125 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4126
4127 ip = bc_vec_top(&p->exits);
4128 label = bc_vec_item(&p->func->labels, ip->idx);
4129 *label = p->func->code.len;
4130
4131 bc_vec_pop(&p->exits);
4132}
4133
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004134static BC_STATUS zbc_parse_if(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004135{
4136 BcStatus s;
4137 BcInstPtr ip;
4138
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004139 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004140 if (s) RETURN_STATUS(s);
4141 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004142
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004143 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004144 if (s) RETURN_STATUS(s);
4145 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4146 if (s) RETURN_STATUS(s);
4147 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004148
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004149 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004150 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004151 bc_parse_push(p, BC_INST_JUMP_ZERO);
4152
4153 ip.idx = p->func->labels.len;
4154 ip.func = ip.len = 0;
4155
4156 bc_parse_pushIndex(p, ip.idx);
4157 bc_vec_push(&p->exits, &ip);
4158 bc_vec_push(&p->func->labels, &ip.idx);
4159 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4160
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004161 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004162}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004163#if ERRORS_ARE_FATAL
4164# define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__), BC_STATUS_SUCCESS)
4165#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004166
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004167#undef zbc_parse_else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004168static BC_STATUS zbc_parse_else(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004169{
4170 BcInstPtr ip;
4171
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004172 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004173
4174 ip.idx = p->func->labels.len;
4175 ip.func = ip.len = 0;
4176
4177 bc_parse_push(p, BC_INST_JUMP);
4178 bc_parse_pushIndex(p, ip.idx);
4179
4180 bc_parse_noElse(p);
4181
4182 bc_vec_push(&p->exits, &ip);
4183 bc_vec_push(&p->func->labels, &ip.idx);
4184 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4185
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004186 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004187}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004188#if ERRORS_ARE_FATAL
4189# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
4190#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004191
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004192static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004193{
4194 BcStatus s;
4195 BcInstPtr ip;
4196
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004197 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004198 if (s) RETURN_STATUS(s);
4199 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004200 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004201 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004202
4203 ip.idx = p->func->labels.len;
4204
4205 bc_vec_push(&p->func->labels, &p->func->code.len);
4206 bc_vec_push(&p->conds, &ip.idx);
4207
4208 ip.idx = p->func->labels.len;
4209 ip.func = 1;
4210 ip.len = 0;
4211
4212 bc_vec_push(&p->exits, &ip);
4213 bc_vec_push(&p->func->labels, &ip.idx);
4214
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004215 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4216 if (s) RETURN_STATUS(s);
4217 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004218 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004219 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004220
4221 bc_parse_push(p, BC_INST_JUMP_ZERO);
4222 bc_parse_pushIndex(p, ip.idx);
4223 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4224
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004225 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004226}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004227#if ERRORS_ARE_FATAL
4228# define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__), BC_STATUS_SUCCESS)
4229#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004230
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004231static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004232{
4233 BcStatus s;
4234 BcInstPtr ip;
4235 size_t cond_idx, exit_idx, body_idx, update_idx;
4236
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004237 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004238 if (s) RETURN_STATUS(s);
4239 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004240 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004241 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004242
4243 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004244 s = zbc_parse_expr(p, 0, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004245 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004246 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004247
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004248 if (s) RETURN_STATUS(s);
4249 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004250 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004251 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004252
4253 cond_idx = p->func->labels.len;
4254 update_idx = cond_idx + 1;
4255 body_idx = update_idx + 1;
4256 exit_idx = body_idx + 1;
4257
4258 bc_vec_push(&p->func->labels, &p->func->code.len);
4259
4260 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004261 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004262 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004263 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004264
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004265 if (s) RETURN_STATUS(s);
4266 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004267
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004268 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004269 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004270
4271 bc_parse_push(p, BC_INST_JUMP_ZERO);
4272 bc_parse_pushIndex(p, exit_idx);
4273 bc_parse_push(p, BC_INST_JUMP);
4274 bc_parse_pushIndex(p, body_idx);
4275
4276 ip.idx = p->func->labels.len;
4277
4278 bc_vec_push(&p->conds, &update_idx);
4279 bc_vec_push(&p->func->labels, &p->func->code.len);
4280
4281 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004282 s = zbc_parse_expr(p, 0, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06004283 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004284 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004285
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004286 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004287
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004288 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004289 bc_parse_push(p, BC_INST_JUMP);
4290 bc_parse_pushIndex(p, cond_idx);
4291 bc_vec_push(&p->func->labels, &p->func->code.len);
4292
4293 ip.idx = exit_idx;
4294 ip.func = 1;
4295 ip.len = 0;
4296
4297 bc_vec_push(&p->exits, &ip);
4298 bc_vec_push(&p->func->labels, &ip.idx);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004299 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004300 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004301 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4302
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004303 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004304}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004305#if ERRORS_ARE_FATAL
4306# define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__), BC_STATUS_SUCCESS)
4307#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004308
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004309static BC_STATUS zbc_parse_loopExit(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004310{
4311 BcStatus s;
4312 size_t i;
4313 BcInstPtr *ip;
4314
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004315 if (!BC_PARSE_LOOP(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004316
4317 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004318 if (p->exits.len == 0) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004319
4320 i = p->exits.len - 1;
4321 ip = bc_vec_item(&p->exits, i);
4322
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004323 while (!ip->func && i < p->exits.len)
4324 ip = bc_vec_item(&p->exits, i--);
4325 if (i >= p->exits.len && !ip->func)
4326 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004327
4328 i = ip->idx;
4329 }
4330 else
4331 i = *((size_t *) bc_vec_top(&p->conds));
4332
4333 bc_parse_push(p, BC_INST_JUMP);
4334 bc_parse_pushIndex(p, i);
4335
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004336 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004337 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004338
4339 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004340 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004341
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004342 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004343}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004344#if ERRORS_ARE_FATAL
4345# define zbc_parse_loopExit(...) (zbc_parse_loopExit(__VA_ARGS__), BC_STATUS_SUCCESS)
4346#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004347
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004348static BC_STATUS zbc_parse_func(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004349{
4350 BcStatus s;
4351 bool var, comma = false;
4352 uint8_t flags;
4353 char *name;
4354
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004355 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004356 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004357 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004358 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004359
4360 name = xstrdup(p->l.t.v.v);
4361 bc_parse_addFunc(p, name, &p->fidx);
4362
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004363 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004364 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004365 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004366 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004367 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004368 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004369
4370 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004371 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004372 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004373
4374 ++p->func->nparams;
4375
4376 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004377 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004378 if (s) goto err;
4379
4380 var = p->l.t.t != BC_LEX_LBRACKET;
4381
4382 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004383 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004384 if (s) goto err;
4385
4386 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004387 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004388 goto err;
4389 }
4390
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004391 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004392 if (s) goto err;
4393 }
4394
4395 comma = p->l.t.t == BC_LEX_COMMA;
4396 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004397 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004398 if (s) goto err;
4399 }
4400
Denys Vlasenko29301232018-12-11 15:29:32 +01004401 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004402 if (s) goto err;
4403 }
4404
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004405 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004406
4407 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4408 bc_parse_startBody(p, flags);
4409
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004410 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004411 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004412
4413 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004414 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004415
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004416 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004417
4418err:
4419 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004420 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004421}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004422#if ERRORS_ARE_FATAL
4423# define zbc_parse_func(...) (zbc_parse_func(__VA_ARGS__), BC_STATUS_SUCCESS)
4424#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004425
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004426static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004427{
4428 BcStatus s;
4429 bool comma, var, one;
4430 char *name;
4431
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004432 if (!p->auto_part) RETURN_STATUS(bc_error_bad_token());
4433
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004434 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004435 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004436
4437 p->auto_part = comma = false;
4438 one = p->l.t.t == BC_LEX_NAME;
4439
4440 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004441 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004442 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004443 if (s) goto err;
4444
4445 var = p->l.t.t != BC_LEX_LBRACKET;
4446 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004447 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004448 if (s) goto err;
4449
4450 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004451 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 goto err;
4453 }
4454
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004455 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 if (s) goto err;
4457 }
4458
4459 comma = p->l.t.t == BC_LEX_COMMA;
4460 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004461 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004462 if (s) goto err;
4463 }
4464
Denys Vlasenko29301232018-12-11 15:29:32 +01004465 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004466 if (s) goto err;
4467 }
4468
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004469 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4470 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004471
4472 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004473 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004474
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004475 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004476
4477err:
4478 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004479 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004480}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004481#if ERRORS_ARE_FATAL
4482# define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__), BC_STATUS_SUCCESS)
4483#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004484
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004485static BC_STATUS zbc_parse_body(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004486{
4487 BcStatus s = BC_STATUS_SUCCESS;
4488 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4489
4490 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4491
4492 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004493 if (!brace) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004494
Gavin Howard01055ba2018-11-03 11:00:21 -06004495 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4496
4497 if (!p->auto_part) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004498 s = zbc_parse_auto(p);
4499 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004500 }
4501
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004502 if (p->l.t.t == BC_LEX_NLINE) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004503 }
4504 else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004505 s = zbc_parse_stmt(p);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004506 if (!s && !brace) s = zbc_parse_endBody(p, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004507 }
4508
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004509 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004510}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004511#if ERRORS_ARE_FATAL
4512# define zbc_parse_body(...) (zbc_parse_body(__VA_ARGS__), BC_STATUS_SUCCESS)
4513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004514
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004515#undef zbc_parse_stmt
4516static BC_STATUS zbc_parse_stmt(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004517{
4518 BcStatus s = BC_STATUS_SUCCESS;
4519
4520 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004521 case BC_LEX_NLINE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004522 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004523
4524 case BC_LEX_KEY_ELSE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004525 p->auto_part = false;
4526 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004527
4528 case BC_LEX_LBRACE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004529 if (!BC_PARSE_BODY(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004530 ++p->nbraces;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004531 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004532 if (s) RETURN_STATUS(s);
4533 RETURN_STATUS(zbc_parse_body(p, true));
Gavin Howard01055ba2018-11-03 11:00:21 -06004534
4535 case BC_LEX_KEY_AUTO:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004536 RETURN_STATUS(zbc_parse_auto(p));
Gavin Howard01055ba2018-11-03 11:00:21 -06004537
4538 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06004539 p->auto_part = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004540 if (BC_PARSE_IF_END(p)) {
4541 bc_parse_noElse(p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004542 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004544 if (BC_PARSE_BODY(p))
4545 RETURN_STATUS(zbc_parse_body(p, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06004546 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 }
4548
4549 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 case BC_LEX_OP_INC:
4551 case BC_LEX_OP_DEC:
4552 case BC_LEX_OP_MINUS:
4553 case BC_LEX_OP_BOOL_NOT:
4554 case BC_LEX_LPAREN:
4555 case BC_LEX_NAME:
4556 case BC_LEX_NUMBER:
4557 case BC_LEX_KEY_IBASE:
4558 case BC_LEX_KEY_LAST:
4559 case BC_LEX_KEY_LENGTH:
4560 case BC_LEX_KEY_OBASE:
4561 case BC_LEX_KEY_READ:
4562 case BC_LEX_KEY_SCALE:
4563 case BC_LEX_KEY_SQRT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004564 s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
Gavin Howard01055ba2018-11-03 11:00:21 -06004565 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004566 case BC_LEX_KEY_ELSE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004567 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004569 case BC_LEX_SCOLON:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004570 while (!s && p->l.t.t == BC_LEX_SCOLON) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004572 case BC_LEX_RBRACE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004573 s = zbc_parse_endBody(p, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004574 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 case BC_LEX_STR:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004576 s = zbc_parse_string(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004577 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 case BC_LEX_KEY_BREAK:
4579 case BC_LEX_KEY_CONTINUE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004580 s = zbc_parse_loopExit(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004583 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004587 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004589 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004590 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004591 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004592 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004593 // "limits" is a compile-time command,
4594 // the output is produced at _parse time_.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004595 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004596 if (s) RETURN_STATUS(s);
Denys Vlasenko64074a12018-12-07 15:50:14 +01004597 printf(
4598 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4599 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4600 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4601 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4602 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4603 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4604 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4605 "Number of vars = "BC_MAX_VARS_STR "\n"
4606 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004607 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004608 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004609 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004610 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004611 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004612 // "quit" is a compile-time command. For example,
4613 // "if (0 == 1) quit" terminates when parsing the statement,
4614 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004615 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 case BC_LEX_KEY_RETURN:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004617 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004618 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004619 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004620 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004623 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004625 }
4626
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004627 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004628}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004629#if ERRORS_ARE_FATAL
4630# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
4631#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004632
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01004633static BC_STATUS zbc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004634{
4635 BcStatus s;
4636
4637 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004638 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 -06004639 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004640 if (!BC_PARSE_CAN_EXEC(p))
4641 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004642 s = zbc_parse_func(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004643 } else
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004644 s = zbc_parse_stmt(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004645
Denys Vlasenkod38af482018-12-04 19:11:02 +01004646 if (s || G_interrupt) {
4647 bc_parse_reset(p);
4648 s = BC_STATUS_FAILURE;
4649 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004650
Denys Vlasenko26819db2018-12-12 16:08:46 +01004651 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004652}
Denys Vlasenko26819db2018-12-12 16:08:46 +01004653#if ERRORS_ARE_FATAL
4654# define zbc_parse_parse(...) (zbc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
4655#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004656
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004657// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004658static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004659{
4660 BcStatus s = BC_STATUS_SUCCESS;
4661 BcInst prev = BC_INST_PRINT;
4662 BcLexType top, t = p->l.t.t;
4663 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004664 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4666
4667 paren_first = p->l.t.t == BC_LEX_LPAREN;
4668 nparens = nrelops = 0;
4669 paren_expr = rprn = done = get_token = assign = false;
4670 bin_last = true;
4671
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004672 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004673 switch (t) {
4674
4675 case BC_LEX_OP_INC:
4676 case BC_LEX_OP_DEC:
4677 {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004678 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004679 rprn = get_token = bin_last = false;
4680 break;
4681 }
4682
4683 case BC_LEX_OP_MINUS:
4684 {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004685 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 rprn = get_token = false;
4687 bin_last = prev == BC_INST_MINUS;
4688 break;
4689 }
4690
4691 case BC_LEX_OP_ASSIGN_POWER:
4692 case BC_LEX_OP_ASSIGN_MULTIPLY:
4693 case BC_LEX_OP_ASSIGN_DIVIDE:
4694 case BC_LEX_OP_ASSIGN_MODULUS:
4695 case BC_LEX_OP_ASSIGN_PLUS:
4696 case BC_LEX_OP_ASSIGN_MINUS:
4697 case BC_LEX_OP_ASSIGN:
4698 {
4699 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4700 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4701 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4702 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004703 s = bc_error("bad assignment:"
4704 " left side must be scale,"
4705 " ibase, obase, last, var,"
4706 " or array element"
4707 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004708 break;
4709 }
4710 }
4711 // Fallthrough.
4712 case BC_LEX_OP_POWER:
4713 case BC_LEX_OP_MULTIPLY:
4714 case BC_LEX_OP_DIVIDE:
4715 case BC_LEX_OP_MODULUS:
4716 case BC_LEX_OP_PLUS:
4717 case BC_LEX_OP_REL_EQ:
4718 case BC_LEX_OP_REL_LE:
4719 case BC_LEX_OP_REL_GE:
4720 case BC_LEX_OP_REL_NE:
4721 case BC_LEX_OP_REL_LT:
4722 case BC_LEX_OP_REL_GT:
4723 case BC_LEX_OP_BOOL_NOT:
4724 case BC_LEX_OP_BOOL_OR:
4725 case BC_LEX_OP_BOOL_AND:
4726 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004727 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4728 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4729 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004730 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004731 }
4732
4733 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4734 prev = BC_PARSE_TOKEN_INST(t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004735 s = zbc_parse_operator(p, t, ops_bgn, &nexprs, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 rprn = get_token = false;
4737 bin_last = t != BC_LEX_OP_BOOL_NOT;
4738
4739 break;
4740 }
4741
4742 case BC_LEX_LPAREN:
4743 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004744 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004745 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004746 ++nparens;
4747 paren_expr = rprn = bin_last = false;
4748 get_token = true;
4749 bc_vec_push(&p->ops, &t);
4750
4751 break;
4752 }
4753
4754 case BC_LEX_RPAREN:
4755 {
4756 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004757 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004758
4759 if (nparens == 0) {
4760 s = BC_STATUS_SUCCESS;
4761 done = true;
4762 get_token = false;
4763 break;
4764 }
4765 else if (!paren_expr)
4766 return BC_STATUS_PARSE_EMPTY_EXP;
4767
4768 --nparens;
4769 paren_expr = rprn = true;
4770 get_token = bin_last = false;
4771
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004772 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004773
4774 break;
4775 }
4776
4777 case BC_LEX_NAME:
4778 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004779 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004780 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004781 paren_expr = true;
4782 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004783 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004784 ++nexprs;
4785
4786 break;
4787 }
4788
4789 case BC_LEX_NUMBER:
4790 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004791 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004792 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004793 bc_parse_number(p, &prev, &nexprs);
4794 paren_expr = get_token = true;
4795 rprn = bin_last = false;
4796
4797 break;
4798 }
4799
4800 case BC_LEX_KEY_IBASE:
4801 case BC_LEX_KEY_LAST:
4802 case BC_LEX_KEY_OBASE:
4803 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004804 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004805 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004806 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4807 bc_parse_push(p, (char) prev);
4808
4809 paren_expr = get_token = true;
4810 rprn = bin_last = false;
4811 ++nexprs;
4812
4813 break;
4814 }
4815
4816 case BC_LEX_KEY_LENGTH:
4817 case BC_LEX_KEY_SQRT:
4818 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004819 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004820 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004821 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004822 paren_expr = true;
4823 rprn = get_token = bin_last = false;
4824 ++nexprs;
4825
4826 break;
4827 }
4828
4829 case BC_LEX_KEY_READ:
4830 {
4831 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004832 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004833 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004834 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004835 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004836 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004837
4838 paren_expr = true;
4839 rprn = get_token = bin_last = false;
4840 ++nexprs;
4841 prev = BC_INST_READ;
4842
4843 break;
4844 }
4845
4846 case BC_LEX_KEY_SCALE:
4847 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004848 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004849 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004850 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004851 paren_expr = true;
4852 rprn = get_token = bin_last = false;
4853 ++nexprs;
4854 prev = BC_INST_SCALE;
4855
4856 break;
4857 }
4858
4859 default:
4860 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004861 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004862 break;
4863 }
4864 }
4865
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004866 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004867 }
4868
4869 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004870 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004871
4872 while (p->ops.len > ops_bgn) {
4873
4874 top = BC_PARSE_TOP_OP(p);
4875 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4876
4877 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004878 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004879
4880 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4881
4882 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4883 bc_vec_pop(&p->ops);
4884 }
4885
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004886 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004887 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004888
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004889 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4890 for (;;) {
4891 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004892 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004893 if (next & 0x80) // last element?
4894 break;
4895 next >>= 8;
4896 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004897 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004898 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004899
4900 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004901 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004902 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004903 }
4904 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004905 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004906 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004907 }
4908
4909 if (flags & BC_PARSE_PRINT) {
4910 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4911 bc_parse_push(p, BC_INST_POP);
4912 }
4913
4914 return s;
4915}
4916
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004917#undef zbc_parse_expr
4918static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004919{
4920 BcStatus s;
4921
4922 s = bc_parse_expr_empty_ok(p, flags, next);
4923 if (s == BC_STATUS_PARSE_EMPTY_EXP)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004924 RETURN_STATUS(bc_error("empty expression"));
4925 RETURN_STATUS(s);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004926}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004927#if ERRORS_ARE_FATAL
4928# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
4929#endif
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004930
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004931static BC_STATUS zbc_parse_expression(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004932{
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004933 RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read));
Gavin Howard01055ba2018-11-03 11:00:21 -06004934}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004935#if ERRORS_ARE_FATAL
4936# define zbc_parse_expression(...) (zbc_parse_expression(__VA_ARGS__), BC_STATUS_SUCCESS)
4937#endif
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004938
Gavin Howard01055ba2018-11-03 11:00:21 -06004939#endif // ENABLE_BC
4940
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004941#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004942
4943#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
4944
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004945static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004946{
4947 BcStatus s;
4948 char *name;
4949
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004950 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004951 if (s) RETURN_STATUS(s);
4952 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004953
4954 name = xstrdup(p->l.t.v.v);
4955 bc_parse_pushName(p, name);
4956
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004957 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004958}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004959#if ERRORS_ARE_FATAL
4960# define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__), BC_STATUS_SUCCESS)
4961#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004962
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004963static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004964{
4965 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004966 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004967
4968 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
4969 name = xstrdup(b);
4970
4971 str = xstrdup(p->l.t.v.v);
4972 bc_parse_push(p, BC_INST_STR);
4973 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004974 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004975 bc_parse_addFunc(p, name, &idx);
4976
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004977 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004978}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004979#if ERRORS_ARE_FATAL
4980# define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
4981#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004982
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004983static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004984{
4985 BcStatus s;
4986
4987 bc_parse_push(p, inst);
4988 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004989 s = zdc_parse_register(p);
4990 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004991 }
4992
4993 if (store) {
4994 bc_parse_push(p, BC_INST_SWAP);
4995 bc_parse_push(p, BC_INST_ASSIGN);
4996 bc_parse_push(p, BC_INST_POP);
4997 }
4998
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004999 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06005000}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005001#if ERRORS_ARE_FATAL
5002# define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__), BC_STATUS_SUCCESS)
5003#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005004
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005005static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005006{
5007 BcStatus s;
5008
5009 bc_parse_push(p, inst);
5010 bc_parse_push(p, BC_INST_EXEC_COND);
5011
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005012 s = zdc_parse_register(p);
5013 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005014
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005015 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005016 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005017
5018 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005019 s = zdc_parse_register(p);
5020 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005021 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005022 }
5023 else
5024 bc_parse_push(p, BC_PARSE_STREND);
5025
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005026 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005027}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005028#if ERRORS_ARE_FATAL
5029# define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__), BC_STATUS_SUCCESS)
5030#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005031
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005032static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005033{
5034 BcStatus s = BC_STATUS_SUCCESS;
5035 BcInst prev;
5036 uint8_t inst;
5037 bool assign, get_token = false;
5038
5039 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005040 case BC_LEX_OP_REL_EQ:
5041 case BC_LEX_OP_REL_LE:
5042 case BC_LEX_OP_REL_GE:
5043 case BC_LEX_OP_REL_NE:
5044 case BC_LEX_OP_REL_LT:
5045 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005046 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005047 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005048 case BC_LEX_SCOLON:
5049 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005050 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005051 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 case BC_LEX_STR:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005053 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06005054 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005055 case BC_LEX_NEG:
5056 case BC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005058 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005059 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005060 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005061 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005062 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005063 bc_parse_number(p, &prev, &p->nbraces);
Gavin Howard01055ba2018-11-03 11:00:21 -06005064 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5065 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005067 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06005068 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005069 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005070 else
5071 bc_parse_push(p, BC_INST_READ);
5072 get_token = true;
5073 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005074 case BC_LEX_OP_ASSIGN:
5075 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 assign = t == BC_LEX_OP_ASSIGN;
5077 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005078 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005079 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005080 case BC_LEX_LOAD:
5081 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005083 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005085 case BC_LEX_STORE_IBASE:
5086 case BC_LEX_STORE_SCALE:
5087 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005088 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005089 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005090 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005091 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005092 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005093 get_token = true;
5094 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005095 }
5096
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005097 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005098
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005100}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005101#if ERRORS_ARE_FATAL
5102# define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__), BC_STATUS_SUCCESS)
5103#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005104
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005105static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005106{
5107 BcStatus s = BC_STATUS_SUCCESS;
5108 BcInst inst;
5109 BcLexType t;
5110
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005111 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005112
5113 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005114 inst = dc_parse_insts[t];
5115
5116 if (inst != BC_INST_INVALID) {
5117 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005118 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005119 } else
5120 s = zdc_parse_token(p, t, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06005121 }
5122
5123 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5124 bc_parse_push(p, BC_INST_POP_EXEC);
5125
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005126 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005127}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005128#if ERRORS_ARE_FATAL
5129# define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5130#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005131
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01005132static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005133{
5134 BcStatus s;
5135
5136 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005137 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005138 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005139 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005140
Denys Vlasenkod38af482018-12-04 19:11:02 +01005141 if (s || G_interrupt) {
5142 bc_parse_reset(p);
5143 s = BC_STATUS_FAILURE;
5144 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005145
Denys Vlasenko26819db2018-12-12 16:08:46 +01005146 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005147}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005148#if ERRORS_ARE_FATAL
5149# define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
5150#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005151
Gavin Howard01055ba2018-11-03 11:00:21 -06005152#endif // ENABLE_DC
5153
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005154static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005155{
5156 if (IS_BC) {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005157 IF_BC(RETURN_STATUS(zbc_parse_expression(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005158 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005159 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005160 }
5161}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005162#if ERRORS_ARE_FATAL
5163# define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5164#endif
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005165
Denys Vlasenkodf515392018-12-02 19:27:48 +01005166static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005167{
Gavin Howard01055ba2018-11-03 11:00:21 -06005168 BcId e, *ptr;
5169 BcVec *v, *map;
5170 size_t i;
5171 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005172 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005173
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005174 v = var ? &G.prog.vars : &G.prog.arrs;
5175 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005176
5177 e.name = id;
5178 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005179 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005180
5181 if (new) {
5182 bc_array_init(&data.v, var);
5183 bc_vec_push(v, &data.v);
5184 }
5185
5186 ptr = bc_vec_item(map, i);
5187 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005188 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005189}
5190
Denys Vlasenko29301232018-12-11 15:29:32 +01005191static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005192{
Gavin Howard01055ba2018-11-03 11:00:21 -06005193 switch (r->t) {
5194
5195 case BC_RESULT_STR:
5196 case BC_RESULT_TEMP:
5197 case BC_RESULT_IBASE:
5198 case BC_RESULT_SCALE:
5199 case BC_RESULT_OBASE:
5200 {
5201 *num = &r->d.n;
5202 break;
5203 }
5204
5205 case BC_RESULT_CONSTANT:
5206 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005207 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005208 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005209 size_t base_t, len = strlen(*str);
5210 BcNum *base;
5211
5212 bc_num_init(&r->d.n, len);
5213
5214 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005215 base = hex ? &G.prog.hexb : &G.prog.ib;
5216 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005217 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005218
5219 if (s) {
5220 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005221 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005222 }
5223
5224 *num = &r->d.n;
5225 r->t = BC_RESULT_TEMP;
5226
5227 break;
5228 }
5229
5230 case BC_RESULT_VAR:
5231 case BC_RESULT_ARRAY:
5232 case BC_RESULT_ARRAY_ELEM:
5233 {
5234 BcVec *v;
5235
Denys Vlasenkodf515392018-12-02 19:27:48 +01005236 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005237
5238 if (r->t == BC_RESULT_ARRAY_ELEM) {
5239 v = bc_vec_top(v);
5240 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5241 *num = bc_vec_item(v, r->d.id.idx);
5242 }
5243 else
5244 *num = bc_vec_top(v);
5245
5246 break;
5247 }
5248
5249 case BC_RESULT_LAST:
5250 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005251 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005252 break;
5253 }
5254
5255 case BC_RESULT_ONE:
5256 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005257 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005258 break;
5259 }
5260 }
5261
Denys Vlasenko29301232018-12-11 15:29:32 +01005262 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005263}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005264#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005265# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005266#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005267
Denys Vlasenko29301232018-12-11 15:29:32 +01005268static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005269 BcResult **r, BcNum **rn, bool assign)
5270{
5271 BcStatus s;
5272 bool hex;
5273 BcResultType lt, rt;
5274
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005275 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005276 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005277
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005278 *r = bc_vec_item_rev(&G.prog.results, 0);
5279 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005280
5281 lt = (*l)->t;
5282 rt = (*r)->t;
5283 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5284
Denys Vlasenko29301232018-12-11 15:29:32 +01005285 s = zbc_program_num(*l, ln, false);
5286 if (s) RETURN_STATUS(s);
5287 s = zbc_program_num(*r, rn, hex);
5288 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005289
5290 // We run this again under these conditions in case any vector has been
5291 // reallocated out from under the BcNums or arrays we had.
5292 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005293 s = zbc_program_num(*l, ln, false);
5294 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005295 }
5296
5297 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005298 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005299 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005300 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005301
Denys Vlasenko29301232018-12-11 15:29:32 +01005302 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005303}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005304#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005305# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005306#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005307
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005308static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005309{
5310 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005311 bc_vec_pop(&G.prog.results);
5312 bc_vec_pop(&G.prog.results);
5313 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005314}
5315
Denys Vlasenko29301232018-12-11 15:29:32 +01005316static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005317{
5318 BcStatus s;
5319
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005320 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005321 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005322 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005323
Denys Vlasenko29301232018-12-11 15:29:32 +01005324 s = zbc_program_num(*r, n, false);
5325 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005326
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005327 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005328 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005329
Denys Vlasenko29301232018-12-11 15:29:32 +01005330 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005331}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005332#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005333# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005334#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005335
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005336static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005337{
5338 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005339 bc_vec_pop(&G.prog.results);
5340 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005341}
5342
Denys Vlasenko259137d2018-12-11 19:42:05 +01005343static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005344{
5345 BcStatus s;
5346 BcResult *opd1, *opd2, res;
5347 BcNum *n1, *n2 = NULL;
5348
Denys Vlasenko29301232018-12-11 15:29:32 +01005349 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005350 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005351 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005352
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005353 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005354 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 -06005355 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005356 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005357
Denys Vlasenko259137d2018-12-11 19:42:05 +01005358 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005359
5360err:
5361 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005362 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005363}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005364#if ERRORS_ARE_FATAL
5365# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5366#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005367
Denys Vlasenko26819db2018-12-12 16:08:46 +01005368static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005369{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005370 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005371 BcStatus s;
5372 BcParse parse;
5373 BcVec buf;
5374 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005375 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005376
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005377 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005378 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005379
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005380 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005381 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005382
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005383 sv_file = G.prog.file;
5384 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005385 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005386
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005387 bc_char_vec_init(&buf);
Denys Vlasenko8a892472018-12-12 22:43:58 +01005388 bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005389
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005390 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005391 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005392
Denys Vlasenko26819db2018-12-12 16:08:46 +01005393 s = zbc_parse_text(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005394 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005395 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005396 if (s) goto exec_err;
5397
5398 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005399 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005400 goto exec_err;
5401 }
5402
5403 ip.func = BC_PROG_READ;
5404 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005405 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005406
5407 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005408 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005409
5410 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005411 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005412
5413exec_err:
5414 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005415//io_err:
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005416 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005417 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005418 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005419 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005420}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005421#if ERRORS_ARE_FATAL
5422# define zbc_program_read(...) (zbc_program_read(__VA_ARGS__), BC_STATUS_SUCCESS)
5423#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005424
5425static size_t bc_program_index(char *code, size_t *bgn)
5426{
5427 char amt = code[(*bgn)++], i = 0;
5428 size_t res = 0;
5429
5430 for (; i < amt; ++i, ++(*bgn))
5431 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5432
5433 return res;
5434}
5435
5436static char *bc_program_name(char *code, size_t *bgn)
5437{
5438 size_t i;
5439 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5440
5441 s = xmalloc(ptr - str + 1);
5442 c = code[(*bgn)++];
5443
5444 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5445 s[i] = c;
5446
5447 s[i] = '\0';
5448
5449 return s;
5450}
5451
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005452static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005453{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005454#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005455 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005456 // Example: echo '[]ap' | dc
5457 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005458 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005459 return;
5460 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005461#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005462 while (*str) {
5463 int c = *str++;
5464 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005465 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005466 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005467 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005468 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005469 case 'a':
5470 bb_putchar('\a');
5471 break;
5472 case 'b':
5473 bb_putchar('\b');
5474 break;
5475 case '\\':
5476 case 'e':
5477 bb_putchar('\\');
5478 break;
5479 case 'f':
5480 bb_putchar('\f');
5481 break;
5482 case 'n':
5483 bb_putchar('\n');
5484 G.prog.nchars = SIZE_MAX;
5485 break;
5486 case 'r':
5487 bb_putchar('\r');
5488 break;
5489 case 'q':
5490 bb_putchar('"');
5491 break;
5492 case 't':
5493 bb_putchar('\t');
5494 break;
5495 default:
5496 // Just print the backslash and following character.
5497 bb_putchar('\\');
5498 ++G.prog.nchars;
5499 bb_putchar(c);
5500 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005501 }
5502 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005503 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005504 }
5505}
5506
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005507static void bc_num_printNewline(void)
5508{
5509 if (G.prog.nchars == G.prog.len - 1) {
5510 bb_putchar('\\');
5511 bb_putchar('\n');
5512 G.prog.nchars = 0;
5513 }
5514}
5515
5516#if ENABLE_DC
5517static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5518{
5519 (void) radix;
5520 bb_putchar((char) num);
5521 G.prog.nchars += width;
5522}
5523#endif
5524
5525static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5526{
5527 size_t exp, pow;
5528
5529 bc_num_printNewline();
5530 bb_putchar(radix ? '.' : ' ');
5531 ++G.prog.nchars;
5532
5533 bc_num_printNewline();
5534 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5535 continue;
5536
5537 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5538 size_t dig;
5539 bc_num_printNewline();
5540 dig = num / pow;
5541 num -= dig * pow;
5542 bb_putchar(((char) dig) + '0');
5543 }
5544}
5545
5546static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5547{
5548 if (radix) {
5549 bc_num_printNewline();
5550 bb_putchar('.');
5551 G.prog.nchars += 1;
5552 }
5553
5554 bc_num_printNewline();
5555 bb_putchar(bb_hexdigits_upcase[num]);
5556 G.prog.nchars += width;
5557}
5558
5559static void bc_num_printDecimal(BcNum *n)
5560{
5561 size_t i, rdx = n->rdx - 1;
5562
5563 if (n->neg) bb_putchar('-');
5564 G.prog.nchars += n->neg;
5565
5566 for (i = n->len - 1; i < n->len; --i)
5567 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5568}
5569
5570static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5571{
5572 BcStatus s;
5573 BcVec stack;
5574 BcNum intp, fracp, digit, frac_len;
5575 unsigned long dig, *ptr;
5576 size_t i;
5577 bool radix;
5578
5579 if (n->len == 0) {
5580 print(0, width, false);
5581 RETURN_STATUS(BC_STATUS_SUCCESS);
5582 }
5583
5584 bc_vec_init(&stack, sizeof(long), NULL);
5585 bc_num_init(&intp, n->len);
5586 bc_num_init(&fracp, n->rdx);
5587 bc_num_init(&digit, width);
5588 bc_num_init(&frac_len, BC_NUM_INT(n));
5589 bc_num_copy(&intp, n);
5590 bc_num_one(&frac_len);
5591
5592 bc_num_truncate(&intp, intp.rdx);
5593 s = zbc_num_sub(n, &intp, &fracp, 0);
5594 if (s) goto err;
5595
5596 while (intp.len != 0) {
5597 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5598 if (s) goto err;
5599 s = zbc_num_ulong(&digit, &dig);
5600 if (s) goto err;
5601 bc_vec_push(&stack, &dig);
5602 }
5603
5604 for (i = 0; i < stack.len; ++i) {
5605 ptr = bc_vec_item_rev(&stack, i);
5606 print(*ptr, width, false);
5607 }
5608
5609 if (!n->rdx) goto err;
5610
5611 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5612 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5613 if (s) goto err;
5614 s = zbc_num_ulong(&fracp, &dig);
5615 if (s) goto err;
5616 bc_num_ulong2num(&intp, dig);
5617 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5618 if (s) goto err;
5619 print(dig, width, radix);
5620 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5621 if (s) goto err;
5622 }
5623
5624err:
5625 bc_num_free(&frac_len);
5626 bc_num_free(&digit);
5627 bc_num_free(&fracp);
5628 bc_num_free(&intp);
5629 bc_vec_free(&stack);
5630 RETURN_STATUS(s);
5631}
5632#if ERRORS_ARE_FATAL
5633# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5634#endif
5635
5636static BC_STATUS zbc_num_printBase(BcNum *n)
5637{
5638 BcStatus s;
5639 size_t width, i;
5640 BcNumDigitOp print;
5641 bool neg = n->neg;
5642
5643 if (neg) {
5644 bb_putchar('-');
5645 G.prog.nchars++;
5646 }
5647
5648 n->neg = false;
5649
5650 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5651 width = 1;
5652 print = bc_num_printHex;
5653 }
5654 else {
5655 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5656 continue;
5657 print = bc_num_printDigits;
5658 }
5659
5660 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5661 n->neg = neg;
5662
5663 RETURN_STATUS(s);
5664}
5665#if ERRORS_ARE_FATAL
5666# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5667#endif
5668
5669#if ENABLE_DC
5670static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5671{
5672 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5673}
5674#if ERRORS_ARE_FATAL
5675# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5676#endif
5677#endif
5678
5679static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5680{
5681 BcStatus s = BC_STATUS_SUCCESS;
5682
5683 bc_num_printNewline();
5684
5685 if (n->len == 0) {
5686 bb_putchar('0');
5687 ++G.prog.nchars;
5688 }
5689 else if (G.prog.ob_t == 10)
5690 bc_num_printDecimal(n);
5691 else
5692 s = zbc_num_printBase(n);
5693
5694 if (newline) {
5695 bb_putchar('\n');
5696 G.prog.nchars = 0;
5697 }
5698
5699 RETURN_STATUS(s);
5700}
5701#if ERRORS_ARE_FATAL
5702# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5703#endif
5704
Denys Vlasenko29301232018-12-11 15:29:32 +01005705static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005706{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005707 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005708 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005709 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005710 bool pop = inst != BC_INST_PRINT;
5711
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005712 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005713 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005714
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005715 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005716 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005717 s = zbc_program_num(r, &num, false);
5718 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005719
5720 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005721 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005722 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005723 }
5724 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005725 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
5727 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005728 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005729
5730 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005731 for (;;) {
5732 char c = *str++;
5733 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005734 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005735 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005736 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005737 }
5738 }
5739 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005740 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005741 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005742 }
5743 }
5744
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005745 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005746
Denys Vlasenko29301232018-12-11 15:29:32 +01005747 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005748}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005749#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005750# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005751#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005752
Denys Vlasenko29301232018-12-11 15:29:32 +01005753static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005754{
5755 BcStatus s;
5756 BcResult res, *ptr;
5757 BcNum *num = NULL;
5758
Denys Vlasenko29301232018-12-11 15:29:32 +01005759 s = zbc_program_prep(&ptr, &num);
5760 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761
5762 bc_num_init(&res.d.n, num->len);
5763 bc_num_copy(&res.d.n, num);
5764 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5765
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005766 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005767
Denys Vlasenko29301232018-12-11 15:29:32 +01005768 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005769}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005770#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005771# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005772#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005773
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005774static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005775{
5776 BcStatus s;
5777 BcResult *opd1, *opd2, res;
5778 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005779 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005780
Denys Vlasenko29301232018-12-11 15:29:32 +01005781 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005782 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005783
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005784 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005785
5786 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005787 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005788 else if (inst == BC_INST_BOOL_OR)
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 {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005791 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005792 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005793 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005794 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005795 break;
5796 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005797 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005798 break;
5799 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005800 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005801 break;
5802 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005803 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005804 break;
5805 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005806 cond = (cond > 0);
5807 break;
5808 default: // = case BC_INST_REL_NE:
5809 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005810 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005811 }
5812 }
5813
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005814 if (cond) bc_num_one(&res.d.n);
5815 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005816
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005817 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005818
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005819 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005820}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005821#if ERRORS_ARE_FATAL
5822# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5823#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005824
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005825#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005826static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005827 bool push)
5828{
5829 BcNum n2;
5830 BcResult res;
5831
5832 memset(&n2, 0, sizeof(BcNum));
5833 n2.rdx = res.d.id.idx = r->d.id.idx;
5834 res.t = BC_RESULT_STR;
5835
5836 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005837 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005838 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005839 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005840 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 }
5842
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005843 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005844
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005845 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005846 bc_vec_push(v, &n2);
5847
Denys Vlasenko29301232018-12-11 15:29:32 +01005848 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005849}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005850#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005851# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005852#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005853#endif // ENABLE_DC
5854
Denys Vlasenko29301232018-12-11 15:29:32 +01005855static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005856{
5857 BcStatus s;
5858 BcResult *ptr, r;
5859 BcVec *v;
5860 BcNum *n;
5861
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005862 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005863 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005864
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005865 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005866 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005867 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005868 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005869
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005870#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005871 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005872 RETURN_STATUS(bc_error_variable_is_wrong_type());
5873 if (ptr->t == BC_RESULT_STR)
5874 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005875#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005876
Denys Vlasenko29301232018-12-11 15:29:32 +01005877 s = zbc_program_num(ptr, &n, false);
5878 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879
5880 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005881 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005882
5883 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005884 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005885 bc_num_copy(&r.d.n, n);
5886 }
5887 else {
5888 bc_array_init(&r.d.v, true);
5889 bc_array_copy(&r.d.v, (BcVec *) n);
5890 }
5891
5892 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005893 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
Denys Vlasenko29301232018-12-11 15:29:32 +01005895 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005896}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005897#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005898# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005899#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005901static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005902{
5903 BcStatus s;
5904 BcResult *left, *right, res;
5905 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005906 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5907
Denys Vlasenko29301232018-12-11 15:29:32 +01005908 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005909 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005910
5911 ib = left->t == BC_RESULT_IBASE;
5912 sc = left->t == BC_RESULT_SCALE;
5913
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005914#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005915
5916 if (right->t == BC_RESULT_STR) {
5917
5918 BcVec *v;
5919
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005920 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005921 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005922 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005923
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005924 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005926#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005927
5928 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005929 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005930 " left side must be scale,"
5931 " ibase, obase, last, var,"
5932 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005933 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06005934
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005935#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005936 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005937 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005938
5939 if (assign)
5940 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005941 else {
5942 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005943 ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005944 }
5945 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005946#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005948#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005949
5950 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005951 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005952 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5953 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5954 NULL, //can't happen //BC_RESULT_LAST
5955 NULL, //can't happen //BC_RESULT_CONSTANT
5956 NULL, //can't happen //BC_RESULT_ONE
5957 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005958 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005959 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005960 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005961
Denys Vlasenko29301232018-12-11 15:29:32 +01005962 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005963 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005964 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005965 if (sc) {
5966 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005967 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005968 }
5969 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005970 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005971 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005972 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005973 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005974 }
5975
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005976 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005977 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005978 if (!sc)
5979 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005980
5981 *ptr = (size_t) val;
5982 s = BC_STATUS_SUCCESS;
5983 }
5984
5985 bc_num_init(&res.d.n, l->len);
5986 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005987 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005988
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005989 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005990}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005991#if ERRORS_ARE_FATAL
5992# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
5993#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005994
Denys Vlasenko416ce762018-12-02 20:57:17 +01005995#if !ENABLE_DC
5996#define bc_program_pushVar(code, bgn, pop, copy) \
5997 bc_program_pushVar(code, bgn)
5998// for bc, 'pop' and 'copy' are always false
5999#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006000static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006001 bool pop, bool copy)
6002{
Gavin Howard01055ba2018-11-03 11:00:21 -06006003 BcResult r;
6004 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006005
6006 r.t = BC_RESULT_VAR;
6007 r.d.id.name = name;
6008
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006009#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006010 {
6011 BcVec *v = bc_program_search(name, true);
6012 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
Denys Vlasenko416ce762018-12-02 20:57:17 +01006014 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko416ce762018-12-02 20:57:17 +01006016 if (!BC_PROG_STACK(v, 2 - copy)) {
6017 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006018 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006019 }
6020
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006022 name = NULL;
6023
6024 if (!BC_PROG_STR(num)) {
6025
6026 r.t = BC_RESULT_TEMP;
6027
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006028 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006029 bc_num_copy(&r.d.n, num);
6030 }
6031 else {
6032 r.t = BC_RESULT_STR;
6033 r.d.id.idx = num->rdx;
6034 }
6035
6036 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006037 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006038 }
6039#endif // ENABLE_DC
6040
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006041 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006042
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006043 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006045#if ERRORS_ARE_FATAL
6046# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6047#else
6048# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6049#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006050
Denys Vlasenko29301232018-12-11 15:29:32 +01006051static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006052 char inst)
6053{
6054 BcStatus s = BC_STATUS_SUCCESS;
6055 BcResult r;
6056 BcNum *num;
6057
6058 r.d.id.name = bc_program_name(code, bgn);
6059
6060 if (inst == BC_INST_ARRAY) {
6061 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006062 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006063 }
6064 else {
6065
6066 BcResult *operand;
6067 unsigned long temp;
6068
Denys Vlasenko29301232018-12-11 15:29:32 +01006069 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006070 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006071 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006072 if (s) goto err;
6073
6074 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006075 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006076 goto err;
6077 }
6078
6079 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006080 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006081 }
6082
6083err:
6084 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006085 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006086}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006087#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006088# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006089#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006090
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006091#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006092static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006093{
6094 BcStatus s;
6095 BcResult *ptr, res, copy;
6096 BcNum *num = NULL;
6097 char inst2 = inst;
6098
Denys Vlasenko29301232018-12-11 15:29:32 +01006099 s = zbc_program_prep(&ptr, &num);
6100 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006101
6102 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6103 copy.t = BC_RESULT_TEMP;
6104 bc_num_init(&copy.d.n, num->len);
6105 bc_num_copy(&copy.d.n, num);
6106 }
6107
6108 res.t = BC_RESULT_ONE;
6109 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6110 BC_INST_ASSIGN_PLUS :
6111 BC_INST_ASSIGN_MINUS;
6112
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006113 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006114 s = zbc_program_assign(inst);
6115 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006116
6117 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006118 bc_vec_pop(&G.prog.results);
6119 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006120 }
6121
Denys Vlasenko29301232018-12-11 15:29:32 +01006122 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006124#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006125# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006126#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006127
Denys Vlasenko29301232018-12-11 15:29:32 +01006128static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006129{
Gavin Howard01055ba2018-11-03 11:00:21 -06006130 BcInstPtr ip;
6131 size_t i, nparams = bc_program_index(code, idx);
6132 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006133 BcId *a;
6134 BcResultData param;
6135 BcResult *arg;
6136
6137 ip.idx = 0;
6138 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006139 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006140
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006141 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006142 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006143 }
6144 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006145 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006146 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006147 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
6149 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006150 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006151
6152 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006153 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006154
6155 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006156 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006157
Denys Vlasenko29301232018-12-11 15:29:32 +01006158 s = zbc_program_copyToVar(a->name, a->idx);
6159 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 }
6161
6162 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006163 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006164
6165 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006166 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006167
6168 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006169 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006170 bc_vec_push(v, &param.n);
6171 }
6172 else {
6173 bc_array_init(&param.v, true);
6174 bc_vec_push(v, &param.v);
6175 }
6176 }
6177
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006178 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006179
Denys Vlasenko29301232018-12-11 15:29:32 +01006180 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006182#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006183# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006184#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006185
Denys Vlasenko29301232018-12-11 15:29:32 +01006186static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006187{
Gavin Howard01055ba2018-11-03 11:00:21 -06006188 BcResult res;
6189 BcFunc *f;
6190 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006191 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006192
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006193 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006194 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006195
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006196 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006197 res.t = BC_RESULT_TEMP;
6198
6199 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006200 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006201 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006202 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203
Denys Vlasenko29301232018-12-11 15:29:32 +01006204 s = zbc_program_num(operand, &num, false);
6205 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006206 bc_num_init(&res.d.n, num->len);
6207 bc_num_copy(&res.d.n, num);
6208 }
6209 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006210 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006211 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006212 }
6213
6214 // We need to pop arguments as well, so this takes that into account.
6215 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006216 BcVec *v;
6217 BcId *a = bc_vec_item(&f->autos, i);
6218
Denys Vlasenkodf515392018-12-02 19:27:48 +01006219 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006220 bc_vec_pop(v);
6221 }
6222
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006223 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6224 bc_vec_push(&G.prog.results, &res);
6225 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006226
Denys Vlasenko29301232018-12-11 15:29:32 +01006227 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006228}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006229#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006230# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006231#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006232#endif // ENABLE_BC
6233
6234static unsigned long bc_program_scale(BcNum *n)
6235{
6236 return (unsigned long) n->rdx;
6237}
6238
6239static unsigned long bc_program_len(BcNum *n)
6240{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006241 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006242
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006243 if (n->rdx != len) return len;
6244 for (;;) {
6245 if (len == 0) break;
6246 len--;
6247 if (n->num[len] != 0) break;
6248 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006249 return len;
6250}
6251
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006252static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006253{
6254 BcStatus s;
6255 BcResult *opnd;
6256 BcNum *num = NULL;
6257 BcResult res;
6258 bool len = inst == BC_INST_LENGTH;
6259
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006260 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006261 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006262 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenko29301232018-12-11 15:29:32 +01006264 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006265 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006266
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006267#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006268 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006269 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006270#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006271
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006272 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006273
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006274 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006275#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006276 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006277 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006278 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006279#endif
6280#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006281 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006282 char **str;
6283 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6284
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006285 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006286 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006287 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006288#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006290 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006291 }
6292
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006293 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006295 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006296}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006297#if ERRORS_ARE_FATAL
6298# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6299#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006301#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006302static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006303{
6304 BcStatus s;
6305 BcResult *opd1, *opd2, res, res2;
6306 BcNum *n1, *n2 = NULL;
6307
Denys Vlasenko29301232018-12-11 15:29:32 +01006308 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006309 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006310
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006311 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006312 bc_num_init(&res2.d.n, n2->len);
6313
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006314 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006315 if (s) goto err;
6316
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006317 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006319 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006320
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006321 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006322
6323err:
6324 bc_num_free(&res2.d.n);
6325 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006326 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006328#if ERRORS_ARE_FATAL
6329# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6330#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006332static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006333{
6334 BcStatus s;
6335 BcResult *r1, *r2, *r3, res;
6336 BcNum *n1, *n2, *n3;
6337
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006338 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006339 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006340 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006341 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006342
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006343 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006344 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006345 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006346 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006347 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006348
6349 // Make sure that the values have their pointers updated, if necessary.
6350 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6351
6352 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006353 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006354 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006355 }
6356
6357 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006358 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006359 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006360 }
6361 }
6362
6363 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006364 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006365 if (s) goto err;
6366
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006367 bc_vec_pop(&G.prog.results);
6368 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006369
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006370 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006371
6372err:
6373 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006374 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006375}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006376#if ERRORS_ARE_FATAL
6377# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6378#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006379
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006380static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006381{
Gavin Howard01055ba2018-11-03 11:00:21 -06006382 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006383 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006384
6385 res.t = BC_RESULT_TEMP;
6386
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006387 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006388 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006389 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006390}
6391
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006392static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006393{
6394 BcStatus s;
6395 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006396 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006398 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 unsigned long val;
6400
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006401 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006402 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006403 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006404
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006405 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006406 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006407 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006408
6409 if (BC_PROG_NUM(r, num)) {
6410
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006411 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006412 bc_num_copy(&n, num);
6413 bc_num_truncate(&n, n.rdx);
6414
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006415 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006416 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006417 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 if (s) goto num_err;
6419
6420 c = (char) val;
6421
6422 bc_num_free(&n);
6423 }
6424 else {
6425 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006426 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006427 c = str2[0];
6428 }
6429
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006430 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006431 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006432 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006433
6434 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006435 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006436
6437 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006438 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006439 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006440 len = idx;
6441 break;
6442 }
6443 }
6444
6445 free(str);
6446 }
6447 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006448 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006449
6450 res.t = BC_RESULT_STR;
6451 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006452 bc_vec_pop(&G.prog.results);
6453 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006454
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006455 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006456
6457num_err:
6458 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006459 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006460}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006461#if ERRORS_ARE_FATAL
6462# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6463#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006464
Denys Vlasenko29301232018-12-11 15:29:32 +01006465static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006466{
6467 BcStatus s;
6468 BcResult *r;
6469 BcNum *n = NULL;
6470 size_t idx;
6471 char *str;
6472
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006473 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006474 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006475 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006476
Denys Vlasenko29301232018-12-11 15:29:32 +01006477 s = zbc_program_num(r, &n, false);
6478 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006479
6480 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006481 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006482 else {
6483 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006484 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006485 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006486 }
6487
Denys Vlasenko29301232018-12-11 15:29:32 +01006488 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006489}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006490#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006491# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006492#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006493
Denys Vlasenko29301232018-12-11 15:29:32 +01006494static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006495{
6496 BcStatus s;
6497 BcResult *opnd;
6498 BcNum *num = NULL;
6499 unsigned long val;
6500
Denys Vlasenko29301232018-12-11 15:29:32 +01006501 s = zbc_program_prep(&opnd, &num);
6502 if (s) RETURN_STATUS(s);
6503 s = zbc_num_ulong(num, &val);
6504 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006505
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006506 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006507
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006508 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006509 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006510 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006511 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006512 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006513
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006514 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515
Denys Vlasenko29301232018-12-11 15:29:32 +01006516 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006518#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006519# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006520#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006521
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006522static BC_STATUS zbc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006523 bool cond)
6524{
6525 BcStatus s = BC_STATUS_SUCCESS;
6526 BcResult *r;
6527 char **str;
6528 BcFunc *f;
6529 BcParse prs;
6530 BcInstPtr ip;
6531 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006532
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006533 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006534 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006535
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006536 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006537
6538 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006539 BcNum *n = n; // for compiler
6540 bool exec;
6541 char *name;
6542 char *then_name = bc_program_name(code, bgn);
6543 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006544
6545 if (code[*bgn] == BC_PARSE_STREND)
6546 (*bgn) += 1;
6547 else
6548 else_name = bc_program_name(code, bgn);
6549
6550 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006551 name = then_name;
6552 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 exec = true;
6554 name = else_name;
6555 }
6556
6557 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006558 BcVec *v;
6559 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006560 n = bc_vec_top(v);
6561 }
6562
6563 free(then_name);
6564 free(else_name);
6565
6566 if (!exec) goto exit;
6567 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006568 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 goto exit;
6570 }
6571
6572 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006573 } else {
6574 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006575 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006576 } else if (r->t == BC_RESULT_VAR) {
6577 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006578 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006579 if (s || !BC_PROG_STR(n)) goto exit;
6580 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006581 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006582 goto exit;
6583 }
6584
6585 fidx = sidx + BC_PROG_REQ_FUNCS;
6586
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006587 str = bc_program_str(sidx);
6588 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006589
6590 if (f->code.len == 0) {
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006591 bc_parse_create(&prs, fidx);
Denys Vlasenko26819db2018-12-12 16:08:46 +01006592 s = zbc_parse_text(&prs, *str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006594 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 if (s) goto err;
6596
6597 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006598 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006599 goto err;
6600 }
6601
6602 bc_parse_free(&prs);
6603 }
6604
6605 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006606 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006607 ip.func = fidx;
6608
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006609 bc_vec_pop(&G.prog.results);
6610 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006611
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006612 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006613
6614err:
6615 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006616 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006617 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006618exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006619 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006620 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006621}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006622#if ERRORS_ARE_FATAL
6623# define zbc_program_execStr(...) (zbc_program_execStr(__VA_ARGS__), BC_STATUS_SUCCESS)
6624#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006625#endif // ENABLE_DC
6626
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006627static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006628{
Gavin Howard01055ba2018-11-03 11:00:21 -06006629 BcResult res;
6630 unsigned long val;
6631
6632 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6633 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006634 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006635 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006636 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006637 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006638 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006639
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006640 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006641 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006642 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006643}
6644
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006645static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006646{
Gavin Howard01055ba2018-11-03 11:00:21 -06006647 BcId entry, *entry_ptr;
6648 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006649 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006650
6651 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006652 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006653
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006654 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6655 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006656
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006657 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006658 *idx = entry_ptr->idx;
6659
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006660 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006661
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006662 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006663
6664 // We need to reset these, so the function can be repopulated.
6665 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006666 bc_vec_pop_all(&func->autos);
6667 bc_vec_pop_all(&func->code);
6668 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006669 }
6670 else {
6671 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006672 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006673 }
6674}
6675
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006676static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006677{
Gavin Howard01055ba2018-11-03 11:00:21 -06006678 BcResult r, *ptr;
6679 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006680 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006681 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006682 char *code = func->code.v;
6683 bool cond = false;
6684
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006685 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006686 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006687 char inst = code[(ip->idx)++];
6688
6689 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006690#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006691 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006692 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006693 if (s) RETURN_STATUS(s);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006694 cond = !bc_num_cmp(num, &G.prog.zero);
6695 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006696 // Fallthrough.
6697 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006698 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006699 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 addr = bc_vec_item(&func->labels, idx);
6701 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6702 break;
6703 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006704 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006705 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006706 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006707 case BC_INST_INC_PRE:
6708 case BC_INST_DEC_PRE:
6709 case BC_INST_INC_POST:
6710 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006711 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006712 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006713 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006714 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006715 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006716 case BC_INST_RET:
6717 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006718 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006719 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006720 case BC_INST_BOOL_OR:
6721 case BC_INST_BOOL_AND:
6722#endif // ENABLE_BC
6723 case BC_INST_REL_EQ:
6724 case BC_INST_REL_LE:
6725 case BC_INST_REL_GE:
6726 case BC_INST_REL_NE:
6727 case BC_INST_REL_LT:
6728 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006729 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 case BC_INST_READ:
Denys Vlasenko26819db2018-12-12 16:08:46 +01006732 s = zbc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006733 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006734 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006735 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006736 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006737 case BC_INST_ARRAY_ELEM:
6738 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006739 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006740 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006741 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006742 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006743 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006744 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006745 case BC_INST_IBASE:
6746 case BC_INST_SCALE:
6747 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006748 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006750 case BC_INST_SCALE_FUNC:
6751 case BC_INST_LENGTH:
6752 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006753 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006754 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006755 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006756 r.t = BC_RESULT_CONSTANT;
6757 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006758 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006759 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006760 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006761 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006762 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006763 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006764 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006765 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006766 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006767 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006768 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 case BC_INST_PRINT:
6770 case BC_INST_PRINT_POP:
6771 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006772 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006774 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006775 r.t = BC_RESULT_STR;
6776 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006777 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006778 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006779 case BC_INST_POWER:
6780 case BC_INST_MULTIPLY:
6781 case BC_INST_DIVIDE:
6782 case BC_INST_MODULUS:
6783 case BC_INST_PLUS:
6784 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006785 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006786 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006787 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006788 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006789 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006790 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006791 if (!bc_num_cmp(num, &G.prog.zero))
6792 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006793 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006794 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006795 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006796 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006797 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006798 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006799#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006800 case BC_INST_ASSIGN_POWER:
6801 case BC_INST_ASSIGN_MULTIPLY:
6802 case BC_INST_ASSIGN_DIVIDE:
6803 case BC_INST_ASSIGN_MODULUS:
6804 case BC_INST_ASSIGN_PLUS:
6805 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006806#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006808 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006809 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006810#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006811 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006812 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006815 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006816 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006817 case BC_INST_EXECUTE:
6818 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006819 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006820 s = zbc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006821 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006822 case BC_INST_PRINT_STACK: {
6823 size_t idx;
6824 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006825 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006826 if (s) break;
6827 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006828 break;
6829 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006830 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006831 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006833 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006834 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006835 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006836 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006837 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006838 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006839 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006840 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006841 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006842 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006843 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006845 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006846 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006847 ptr = bc_vec_item_rev(&G.prog.results, 0);
6848 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006849 memcpy(&r, ptr, sizeof(BcResult));
6850 memcpy(ptr, ptr2, sizeof(BcResult));
6851 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006852 break;
6853 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006855 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006857 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006858 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006859 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006860 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006861 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006862 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006863 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006864 break;
6865 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006866 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006868 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006869 free(name);
6870 break;
6871 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006872 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006873 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006874 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006875 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006876 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006878 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006879 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006880#endif // ENABLE_DC
6881 }
6882
Denys Vlasenkod38af482018-12-04 19:11:02 +01006883 if (s || G_interrupt) {
6884 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006885 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006886 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006887
6888 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006889 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006890 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006891 code = func->code.v;
6892 }
6893
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006894 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006895}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006896#if ERRORS_ARE_FATAL
6897# define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
6898#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006899
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006900#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006901static void bc_vm_info(void)
6902{
6903 printf("%s "BB_VER"\n"
6904 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01006905 , applet_name);
6906}
6907
6908static void bc_args(char **argv)
6909{
6910 unsigned opts;
6911 int i;
6912
6913 GETOPT_RESET();
6914#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006915 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01006916 "warn\0" No_argument "w"
6917 "version\0" No_argument "v"
6918 "standard\0" No_argument "s"
6919 "quiet\0" No_argument "q"
6920 "mathlib\0" No_argument "l"
6921 "interactive\0" No_argument "i"
6922 );
6923#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006924 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01006925#endif
6926 if (getenv("POSIXLY_CORRECT"))
6927 option_mask32 |= BC_FLAG_S;
6928
Denys Vlasenko54214c32018-12-06 09:07:06 +01006929 if (opts & BC_FLAG_V) {
6930 bc_vm_info();
6931 exit(0);
6932 }
6933
6934 for (i = optind; argv[i]; ++i)
6935 bc_vec_push(&G.files, argv + i);
6936}
6937
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006938static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006939{
Gavin Howard01055ba2018-11-03 11:00:21 -06006940 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006941 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01006942 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06006943
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006944 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06006945
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006946 G.env_args = xstrdup(env_args);
6947 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06006948
6949 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006950
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006951 while (*(buf = skip_whitespace(buf)) != '\0') {
6952 bc_vec_push(&v, &buf);
6953 buf = skip_non_whitespace(buf);
6954 if (!*buf)
6955 break;
6956 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06006957 }
6958
Denys Vlasenko54214c32018-12-06 09:07:06 +01006959 // NULL terminate, and pass argv[] so that first arg is argv[1]
6960 if (sizeof(int) == sizeof(char*)) {
6961 bc_vec_push(&v, &const_int_0);
6962 } else {
6963 static char *const nullptr = NULL;
6964 bc_vec_push(&v, &nullptr);
6965 }
6966 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006967
6968 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006969}
6970#endif // ENABLE_BC
6971
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006972static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006973{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006974 char *lenv;
6975 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006976
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006977 lenv = getenv(var);
6978 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006979 if (!lenv) return len;
6980
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006981 len = bb_strtou(lenv, NULL, 10) - 1;
6982 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006983 len = BC_NUM_PRINT_WIDTH;
6984
6985 return len;
6986}
6987
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006988static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006989{
Denys Vlasenko26819db2018-12-12 16:08:46 +01006990 BcStatus s = zbc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06006991
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006992 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006993
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006994 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01006995 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006997 }
6998
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006999 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007000 s = zbc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007001 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007002 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007003 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007004 }
7005
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007006 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007007}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007008#if ERRORS_ARE_FATAL
7009# define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__), BC_STATUS_SUCCESS)
7010#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007011
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007012static BC_STATUS zbc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007013{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007014 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007015 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007016 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007017 BcFunc *main_func;
7018 BcInstPtr *ip;
7019
Denys Vlasenkodf515392018-12-02 19:27:48 +01007020 data = bc_read_file(file);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007021 if (!data) RETURN_STATUS(bc_error_fmt("file '%s' is not text", file));
Gavin Howard01055ba2018-11-03 11:00:21 -06007022
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007023 sv_file = G.prog.file;
7024 G.prog.file = file;
7025 bc_lex_file(&G.prs.l);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007026 s = zbc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007027 if (s) goto err;
7028
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007029 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007030 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007031
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007032 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007033 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007034
7035err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007036 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007037 free(data);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007038 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007039}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007040#if ERRORS_ARE_FATAL
7041# define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__), BC_STATUS_SUCCESS)
7042#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007043
Denys Vlasenko19f11072018-12-12 22:48:19 +01007044static BC_STATUS zbc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007045{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007046 BcStatus s;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007047 BcVec buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007048 size_t str;
7049 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06007050
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007051 G.prog.file = NULL;
7052 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007053
Denys Vlasenko7d628012018-12-04 21:46:47 +01007054 bc_char_vec_init(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007055
7056 // This loop is complex because the vm tries not to send any lines that end
7057 // with a backslash to the parser. The reason for that is because the parser
7058 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7059 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko8a892472018-12-12 22:43:58 +01007060 s = BC_STATUS_SUCCESS;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007061 comment = false;
7062 str = 0;
Denys Vlasenko8a892472018-12-12 22:43:58 +01007063 for (;;) {
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007064 size_t prevlen = buffer.len;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007065 char *string;
Gavin Howard01055ba2018-11-03 11:00:21 -06007066
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007067 bc_read_line(&buffer);
7068 // No more input means EOF
7069 if (buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenko8a892472018-12-12 22:43:58 +01007070 break;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007071
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007072 string = buffer.v + prevlen;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007073 while (*string) {
7074 char c = *string;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007075 if (string == buffer.v || string[-1] != '\\') {
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007076 // checking applet type is cheaper than accessing sbgn/send
7077 if (IS_BC) // bc: sbgn = send = '"'
7078 str ^= (c == '"');
7079 else { // dc: sbgn = '[', send = ']'
7080 if (c == ']')
7081 str -= 1;
7082 else if (c == '[')
7083 str += 1;
Denys Vlasenko766f6722018-12-13 17:23:24 +01007084 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007085 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007086 string++;
7087 if (c == '/' && *string == '*') {
7088 comment = true;
7089 string++;
Gavin Howard01055ba2018-11-03 11:00:21 -06007090 continue;
7091 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007092 if (c == '*' && *string == '/') {
7093 comment = false;
7094 string++;
7095 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007096 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007097 if (str || comment) {
7098 buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007099 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007100 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007101
7102 // Check for backslash+newline.
7103 // we do not check that last char is '\n' -
7104 // if it is not, then it's EOF, and looping back
7105 // to bc_read_line() will detect it:
7106 string -= 2;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007107 if (string >= buffer.v && *string == '\\') {
7108 buffer.len--;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007109 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007110 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007111
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007112 s = zbc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007113 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007114 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007115 // Debug config, non-interactive mode:
7116 // return all the way back to main.
7117 // Non-debug builds do not come here, they exit.
7118 break;
7119 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007120 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007121
Denys Vlasenko7d628012018-12-04 21:46:47 +01007122 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007123 }
7124
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007125 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007126 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007127 }
7128 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007129 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007130 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007131
Gavin Howard01055ba2018-11-03 11:00:21 -06007132 bc_vec_free(&buffer);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007133 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007134}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007135#if ERRORS_ARE_FATAL
7136# define zbc_vm_stdin(...) (zbc_vm_stdin(__VA_ARGS__), BC_STATUS_SUCCESS)
7137#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007138
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007139#if ENABLE_BC
7140static const char bc_lib[] = {
7141 "scale=20"
7142"\n" "define e(x){"
7143"\n" "auto b,s,n,r,d,i,p,f,v"
7144"\n" "b=ibase"
7145"\n" "ibase=A"
7146"\n" "if(x<0){"
7147"\n" "n=1"
7148"\n" "x=-x"
7149"\n" "}"
7150"\n" "s=scale"
7151"\n" "r=6+s+0.44*x"
7152"\n" "scale=scale(x)+1"
7153"\n" "while(x>1){"
7154"\n" "d+=1"
7155"\n" "x/=2"
7156"\n" "scale+=1"
7157"\n" "}"
7158"\n" "scale=r"
7159"\n" "r=x+1"
7160"\n" "p=x"
7161"\n" "f=v=1"
7162"\n" "for(i=2;v!=0;++i){"
7163"\n" "p*=x"
7164"\n" "f*=i"
7165"\n" "v=p/f"
7166"\n" "r+=v"
7167"\n" "}"
7168"\n" "while((d--)!=0)r*=r"
7169"\n" "scale=s"
7170"\n" "ibase=b"
7171"\n" "if(n!=0)return(1/r)"
7172"\n" "return(r/1)"
7173"\n" "}"
7174"\n" "define l(x){"
7175"\n" "auto b,s,r,p,a,q,i,v"
7176"\n" "b=ibase"
7177"\n" "ibase=A"
7178"\n" "if(x<=0){"
7179"\n" "r=(1-10^scale)/1"
7180"\n" "ibase=b"
7181"\n" "return(r)"
7182"\n" "}"
7183"\n" "s=scale"
7184"\n" "scale+=6"
7185"\n" "p=2"
7186"\n" "while(x>=2){"
7187"\n" "p*=2"
7188"\n" "x=sqrt(x)"
7189"\n" "}"
7190"\n" "while(x<=0.5){"
7191"\n" "p*=2"
7192"\n" "x=sqrt(x)"
7193"\n" "}"
7194"\n" "r=a=(x-1)/(x+1)"
7195"\n" "q=a*a"
7196"\n" "v=1"
7197"\n" "for(i=3;v!=0;i+=2){"
7198"\n" "a*=q"
7199"\n" "v=a/i"
7200"\n" "r+=v"
7201"\n" "}"
7202"\n" "r*=p"
7203"\n" "scale=s"
7204"\n" "ibase=b"
7205"\n" "return(r/1)"
7206"\n" "}"
7207"\n" "define s(x){"
7208"\n" "auto b,s,r,n,a,q,i"
7209"\n" "b=ibase"
7210"\n" "ibase=A"
7211"\n" "s=scale"
7212"\n" "scale=1.1*s+2"
7213"\n" "a=a(1)"
7214"\n" "if(x<0){"
7215"\n" "n=1"
7216"\n" "x=-x"
7217"\n" "}"
7218"\n" "scale=0"
7219"\n" "q=(x/a+2)/4"
7220"\n" "x=x-4*q*a"
7221"\n" "if(q%2!=0)x=-x"
7222"\n" "scale=s+2"
7223"\n" "r=a=x"
7224"\n" "q=-x*x"
7225"\n" "for(i=3;a!=0;i+=2){"
7226"\n" "a*=q/(i*(i-1))"
7227"\n" "r+=a"
7228"\n" "}"
7229"\n" "scale=s"
7230"\n" "ibase=b"
7231"\n" "if(n!=0)return(-r/1)"
7232"\n" "return(r/1)"
7233"\n" "}"
7234"\n" "define c(x){"
7235"\n" "auto b,s"
7236"\n" "b=ibase"
7237"\n" "ibase=A"
7238"\n" "s=scale"
7239"\n" "scale*=1.2"
7240"\n" "x=s(2*a(1)+x)"
7241"\n" "scale=s"
7242"\n" "ibase=b"
7243"\n" "return(x/1)"
7244"\n" "}"
7245"\n" "define a(x){"
7246"\n" "auto b,s,r,n,a,m,t,f,i,u"
7247"\n" "b=ibase"
7248"\n" "ibase=A"
7249"\n" "n=1"
7250"\n" "if(x<0){"
7251"\n" "n=-1"
7252"\n" "x=-x"
7253"\n" "}"
7254"\n" "if(x==1){"
7255"\n" "if(scale<65){"
7256"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7257"\n" "}"
7258"\n" "}"
7259"\n" "if(x==.2){"
7260"\n" "if(scale<65){"
7261"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7262"\n" "}"
7263"\n" "}"
7264"\n" "s=scale"
7265"\n" "if(x>.2){"
7266"\n" "scale+=5"
7267"\n" "a=a(.2)"
7268"\n" "}"
7269"\n" "scale=s+3"
7270"\n" "while(x>.2){"
7271"\n" "m+=1"
7272"\n" "x=(x-.2)/(1+.2*x)"
7273"\n" "}"
7274"\n" "r=u=x"
7275"\n" "f=-x*x"
7276"\n" "t=1"
7277"\n" "for(i=3;t!=0;i+=2){"
7278"\n" "u*=f"
7279"\n" "t=u/i"
7280"\n" "r+=t"
7281"\n" "}"
7282"\n" "scale=s"
7283"\n" "ibase=b"
7284"\n" "return((m*a+r)/n)"
7285"\n" "}"
7286"\n" "define j(n,x){"
7287"\n" "auto b,s,o,a,i,v,f"
7288"\n" "b=ibase"
7289"\n" "ibase=A"
7290"\n" "s=scale"
7291"\n" "scale=0"
7292"\n" "n/=1"
7293"\n" "if(n<0){"
7294"\n" "n=-n"
7295"\n" "if(n%2==1)o=1"
7296"\n" "}"
7297"\n" "a=1"
7298"\n" "for(i=2;i<=n;++i)a*=i"
7299"\n" "scale=1.5*s"
7300"\n" "a=(x^n)/2^n/a"
7301"\n" "r=v=1"
7302"\n" "f=-x*x/4"
7303"\n" "scale=scale+length(a)-scale(a)"
7304"\n" "for(i=1;v!=0;++i){"
7305"\n" "v=v*f/i/(n+i)"
7306"\n" "r+=v"
7307"\n" "}"
7308"\n" "scale=s"
7309"\n" "ibase=b"
7310"\n" "if(o!=0)a=-a"
7311"\n" "return(a*r/1)"
7312"\n" "}"
7313};
7314#endif // ENABLE_BC
7315
Denys Vlasenko19f11072018-12-12 22:48:19 +01007316static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007317{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007318 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007319 size_t i;
7320
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007321#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007322 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007323
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007324 // We know that internal library is not buggy,
7325 // thus error checking is normally disabled.
7326# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007327 bc_lex_file(&G.prs.l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01007328 s = zbc_parse_text(&G.prs, bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007329 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007330
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007331 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01007332 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007333 if (DEBUG_LIB && s) RETURN_STATUS(s);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007334 }
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007335 s = zbc_program_exec();
Denys Vlasenko19f11072018-12-12 22:48:19 +01007336 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007337 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007338#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007339
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007340 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007341 for (i = 0; !s && i < G.files.len; ++i)
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007342 s = zbc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007343 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7344 // Debug config, non-interactive mode:
7345 // return all the way back to main.
7346 // Non-debug builds do not come here, they exit.
Denys Vlasenko19f11072018-12-12 22:48:19 +01007347 RETURN_STATUS(s);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007348 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007349
Denys Vlasenko91cde952018-12-10 20:56:08 +01007350 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko19f11072018-12-12 22:48:19 +01007351 s = zbc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007352
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007353 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007354 s = zbc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007355
Denys Vlasenko19f11072018-12-12 22:48:19 +01007356 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007357}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007358#if ERRORS_ARE_FATAL
7359# define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
7360#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007361
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007362#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007363static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007364{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007365 bc_num_free(&G.prog.ib);
7366 bc_num_free(&G.prog.ob);
7367 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007368# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007369 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007370# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007371 bc_vec_free(&G.prog.fns);
7372 bc_vec_free(&G.prog.fn_map);
7373 bc_vec_free(&G.prog.vars);
7374 bc_vec_free(&G.prog.var_map);
7375 bc_vec_free(&G.prog.arrs);
7376 bc_vec_free(&G.prog.arr_map);
7377 bc_vec_free(&G.prog.strs);
7378 bc_vec_free(&G.prog.consts);
7379 bc_vec_free(&G.prog.results);
7380 bc_vec_free(&G.prog.stack);
7381 bc_num_free(&G.prog.last);
7382 bc_num_free(&G.prog.zero);
7383 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007384}
7385
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007386static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007387{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007388 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007389 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007390 bc_parse_free(&G.prs);
7391 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007392}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007393#endif
7394
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007395static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007396{
7397 size_t idx;
7398 BcInstPtr ip;
7399
7400 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7401 memset(&ip, 0, sizeof(BcInstPtr));
7402
7403 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007404 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007405 bc_num_ten(&G.prog.ib);
7406 G.prog.ib_t = 10;
7407
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007408 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007409 bc_num_ten(&G.prog.ob);
7410 G.prog.ob_t = 10;
7411
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007412 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007413 bc_num_ten(&G.prog.hexb);
7414 G.prog.hexb.num[0] = 6;
7415
7416#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007417 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007418 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7419#endif
7420
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007421 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007422 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007423
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007424 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007425 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007426
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007427 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007428 bc_num_one(&G.prog.one);
7429
7430 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007431 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007432
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007433 bc_program_addFunc(xstrdup("(main)"), &idx);
7434 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007435
7436 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007437 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007438
7439 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007440 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007441
7442 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7443 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7444 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7445 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7446 bc_vec_push(&G.prog.stack, &ip);
7447}
Gavin Howard01055ba2018-11-03 11:00:21 -06007448
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007449static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007450{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007451#if ENABLE_FEATURE_EDITING
7452 G.line_input_state = new_line_input_t(DO_HISTORY);
7453#endif
7454 G.prog.len = bc_vm_envLen(env_len);
7455
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007456 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007457 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007458 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007459 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007460 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007461
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007462//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7463//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007464 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007465#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007466 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007467 // With SA_RESTART, most system calls will restart
7468 // (IOW: they won't fail with EINTR).
7469 // In particular, this means ^C won't cause
7470 // stdout to get into "error state" if SIGINT hits
7471 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007472 //
7473 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007474 // will only be handled after [Enter] since read()
7475 // from stdin is not interrupted by ^C either,
7476 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007477 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007478 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7479
7480 // Without SA_RESTART, this exhibits a bug:
7481 // "while (1) print 1" and try ^C-ing it.
7482 // Intermittently, instead of returning to input line,
7483 // you'll get "output error: Interrupted system call"
7484 // and exit.
7485 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007486#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007487 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007488 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007489 return 0; // "not a tty"
7490}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007491
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007492static BcStatus bc_vm_run(void)
7493{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007494 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007495#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007496 if (G_exiting) // it was actually "halt" or "quit"
7497 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007498 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007499# if ENABLE_FEATURE_EDITING
7500 free_line_input_t(G.line_input_state);
7501# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007502 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007503#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007504 return st;
7505}
7506
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007507#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007508int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007509int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007510{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007511 int is_tty;
7512
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007513 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007514 G.sbgn = G.send = '"';
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 Vlasenko6d9146a2018-12-02 15:48:37 +01007534 G.sbgn = '[';
7535 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007536 /*
7537 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7538 * 1 char wider than bc from the same package.
7539 * Both default width, and xC_LINE_LENGTH=N are wider:
7540 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7541 * 1234\
7542 * 56
7543 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7544 * 123\
7545 * 456
7546 * Do the same, or it's a bug?
7547 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007548 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007549
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007550 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7551 noscript = BC_FLAG_I;
7552 for (;;) {
7553 int n = getopt(argc, argv, "e:f:x");
7554 if (n <= 0)
7555 break;
7556 switch (n) {
7557 case 'e':
7558 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007559 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007560 if (n) return n;
7561 break;
7562 case 'f':
7563 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007564 n = zbc_vm_file(optarg);
7565 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007566 break;
7567 case 'x':
7568 option_mask32 |= DC_FLAG_X;
7569 break;
7570 default:
7571 bb_show_usage();
7572 }
7573 }
7574 argv += optind;
7575
7576 while (*argv) {
7577 noscript = 0;
7578 bc_vec_push(&G.files, argv++);
7579 }
7580
7581 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7582
7583 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007584}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007585#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007586
7587#endif // not DC_SMALL