blob: b3ffe7bfdfaf6c16491b0f3849c16197f77b27ee [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
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100170#define DEBUG_LEXER 0
171
172#if DEBUG_LEXER
173static unsigned lex_indent;
174#define dbg_lex(...) \
175 do { \
176 fprintf(stderr, "%*s", lex_indent, ""); \
177 bb_error_msg(__VA_ARGS__); \
178 } while (0)
179#define dbg_lex_enter(...) \
180 do { \
181 dbg_lex(__VA_ARGS__); \
182 lex_indent++; \
183 } while (0)
184#define dbg_lex_done(...) \
185 do { \
186 lex_indent--; \
187 dbg_lex(__VA_ARGS__); \
188 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100189#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100190# define dbg_lex(...) ((void)0)
191# define dbg_lex_enter(...) ((void)0)
192# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100193#endif
194
Gavin Howard01055ba2018-11-03 11:00:21 -0600195typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100196 BC_STATUS_SUCCESS = 0,
197 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100198 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600199} BcStatus;
200
Gavin Howard01055ba2018-11-03 11:00:21 -0600201#define BC_VEC_INVALID_IDX ((size_t) -1)
202#define BC_VEC_START_CAP (1 << 5)
203
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100204typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600205
206typedef struct BcVec {
207 char *v;
208 size_t len;
209 size_t cap;
210 size_t size;
211 BcVecFree dtor;
212} BcVec;
213
Gavin Howard01055ba2018-11-03 11:00:21 -0600214typedef signed char BcDig;
215
216typedef struct BcNum {
217 BcDig *restrict num;
218 size_t rdx;
219 size_t len;
220 size_t cap;
221 bool neg;
222} BcNum;
223
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100224#define BC_NUM_MIN_BASE ((unsigned long) 2)
225#define BC_NUM_MAX_IBASE ((unsigned long) 16)
226// larger value might speed up BIGNUM calculations a bit:
227#define BC_NUM_DEF_SIZE (16)
228#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600229
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100230#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600231
Gavin Howard01055ba2018-11-03 11:00:21 -0600232typedef enum BcInst {
233
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100234#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600235 BC_INST_INC_PRE,
236 BC_INST_DEC_PRE,
237 BC_INST_INC_POST,
238 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100239#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600240
241 BC_INST_NEG,
242
243 BC_INST_POWER,
244 BC_INST_MULTIPLY,
245 BC_INST_DIVIDE,
246 BC_INST_MODULUS,
247 BC_INST_PLUS,
248 BC_INST_MINUS,
249
250 BC_INST_REL_EQ,
251 BC_INST_REL_LE,
252 BC_INST_REL_GE,
253 BC_INST_REL_NE,
254 BC_INST_REL_LT,
255 BC_INST_REL_GT,
256
257 BC_INST_BOOL_NOT,
258 BC_INST_BOOL_OR,
259 BC_INST_BOOL_AND,
260
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100261#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600262 BC_INST_ASSIGN_POWER,
263 BC_INST_ASSIGN_MULTIPLY,
264 BC_INST_ASSIGN_DIVIDE,
265 BC_INST_ASSIGN_MODULUS,
266 BC_INST_ASSIGN_PLUS,
267 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100268#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600269 BC_INST_ASSIGN,
270
271 BC_INST_NUM,
272 BC_INST_VAR,
273 BC_INST_ARRAY_ELEM,
274 BC_INST_ARRAY,
275
276 BC_INST_SCALE_FUNC,
277 BC_INST_IBASE,
278 BC_INST_SCALE,
279 BC_INST_LAST,
280 BC_INST_LENGTH,
281 BC_INST_READ,
282 BC_INST_OBASE,
283 BC_INST_SQRT,
284
285 BC_INST_PRINT,
286 BC_INST_PRINT_POP,
287 BC_INST_STR,
288 BC_INST_PRINT_STR,
289
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100290#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600291 BC_INST_JUMP,
292 BC_INST_JUMP_ZERO,
293
294 BC_INST_CALL,
295
296 BC_INST_RET,
297 BC_INST_RET0,
298
299 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100300#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600301
302 BC_INST_POP,
303 BC_INST_POP_EXEC,
304
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100305#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600306 BC_INST_MODEXP,
307 BC_INST_DIVMOD,
308
309 BC_INST_EXECUTE,
310 BC_INST_EXEC_COND,
311
312 BC_INST_ASCIIFY,
313 BC_INST_PRINT_STREAM,
314
315 BC_INST_PRINT_STACK,
316 BC_INST_CLEAR_STACK,
317 BC_INST_STACK_LEN,
318 BC_INST_DUPLICATE,
319 BC_INST_SWAP,
320
321 BC_INST_LOAD,
322 BC_INST_PUSH_VAR,
323 BC_INST_PUSH_TO_VAR,
324
325 BC_INST_QUIT,
326 BC_INST_NQUIT,
327
328 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100329#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
331} BcInst;
332
333typedef struct BcId {
334 char *name;
335 size_t idx;
336} BcId;
337
338typedef struct BcFunc {
339 BcVec code;
340 BcVec labels;
341 size_t nparams;
342 BcVec autos;
343} BcFunc;
344
345typedef enum BcResultType {
346
347 BC_RESULT_TEMP,
348
349 BC_RESULT_VAR,
350 BC_RESULT_ARRAY_ELEM,
351 BC_RESULT_ARRAY,
352
353 BC_RESULT_STR,
354
355 BC_RESULT_IBASE,
356 BC_RESULT_SCALE,
357 BC_RESULT_LAST,
358
359 // These are between to calculate ibase, obase, and last from instructions.
360 BC_RESULT_CONSTANT,
361 BC_RESULT_ONE,
362
363 BC_RESULT_OBASE,
364
365} BcResultType;
366
367typedef union BcResultData {
368 BcNum n;
369 BcVec v;
370 BcId id;
371} BcResultData;
372
373typedef struct BcResult {
374 BcResultType t;
375 BcResultData d;
376} BcResult;
377
378typedef struct BcInstPtr {
379 size_t func;
380 size_t idx;
381 size_t len;
382} BcInstPtr;
383
Gavin Howard01055ba2018-11-03 11:00:21 -0600384// BC_LEX_NEG is not used in lexing; it is only for parsing.
385typedef enum BcLexType {
386
387 BC_LEX_EOF,
388 BC_LEX_INVALID,
389
390 BC_LEX_OP_INC,
391 BC_LEX_OP_DEC,
392
393 BC_LEX_NEG,
394
395 BC_LEX_OP_POWER,
396 BC_LEX_OP_MULTIPLY,
397 BC_LEX_OP_DIVIDE,
398 BC_LEX_OP_MODULUS,
399 BC_LEX_OP_PLUS,
400 BC_LEX_OP_MINUS,
401
402 BC_LEX_OP_REL_EQ,
403 BC_LEX_OP_REL_LE,
404 BC_LEX_OP_REL_GE,
405 BC_LEX_OP_REL_NE,
406 BC_LEX_OP_REL_LT,
407 BC_LEX_OP_REL_GT,
408
409 BC_LEX_OP_BOOL_NOT,
410 BC_LEX_OP_BOOL_OR,
411 BC_LEX_OP_BOOL_AND,
412
413 BC_LEX_OP_ASSIGN_POWER,
414 BC_LEX_OP_ASSIGN_MULTIPLY,
415 BC_LEX_OP_ASSIGN_DIVIDE,
416 BC_LEX_OP_ASSIGN_MODULUS,
417 BC_LEX_OP_ASSIGN_PLUS,
418 BC_LEX_OP_ASSIGN_MINUS,
419 BC_LEX_OP_ASSIGN,
420
421 BC_LEX_NLINE,
422 BC_LEX_WHITESPACE,
423
424 BC_LEX_LPAREN,
425 BC_LEX_RPAREN,
426
427 BC_LEX_LBRACKET,
428 BC_LEX_COMMA,
429 BC_LEX_RBRACKET,
430
431 BC_LEX_LBRACE,
432 BC_LEX_SCOLON,
433 BC_LEX_RBRACE,
434
435 BC_LEX_STR,
436 BC_LEX_NAME,
437 BC_LEX_NUMBER,
438
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100439 BC_LEX_KEY_1st_keyword,
440 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600441 BC_LEX_KEY_BREAK,
442 BC_LEX_KEY_CONTINUE,
443 BC_LEX_KEY_DEFINE,
444 BC_LEX_KEY_ELSE,
445 BC_LEX_KEY_FOR,
446 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100447 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
448 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600449 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100450 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600451 BC_LEX_KEY_LENGTH,
452 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100453 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600454 BC_LEX_KEY_PRINT,
455 BC_LEX_KEY_QUIT,
456 BC_LEX_KEY_READ,
457 BC_LEX_KEY_RETURN,
458 BC_LEX_KEY_SCALE,
459 BC_LEX_KEY_SQRT,
460 BC_LEX_KEY_WHILE,
461
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100462#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600463 BC_LEX_EQ_NO_REG,
464 BC_LEX_OP_MODEXP,
465 BC_LEX_OP_DIVMOD,
466
467 BC_LEX_COLON,
468 BC_LEX_ELSE,
469 BC_LEX_EXECUTE,
470 BC_LEX_PRINT_STACK,
471 BC_LEX_CLEAR_STACK,
472 BC_LEX_STACK_LEVEL,
473 BC_LEX_DUPLICATE,
474 BC_LEX_SWAP,
475 BC_LEX_POP,
476
477 BC_LEX_ASCIIFY,
478 BC_LEX_PRINT_STREAM,
479
480 BC_LEX_STORE_IBASE,
481 BC_LEX_STORE_SCALE,
482 BC_LEX_LOAD,
483 BC_LEX_LOAD_POP,
484 BC_LEX_STORE_PUSH,
485 BC_LEX_STORE_OBASE,
486 BC_LEX_PRINT_POP,
487 BC_LEX_NQUIT,
488 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100489#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600490} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100491// must match order of BC_LEX_KEY_foo etc above
492#if ENABLE_BC
493struct BcLexKeyword {
494 char name8[8];
495};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100496#define BC_LEX_KW_ENTRY(a, b) \
497 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100498static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100499 BC_LEX_KW_ENTRY("auto" , 1), // 0
500 BC_LEX_KW_ENTRY("break" , 1), // 1
501 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
502 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100503 BC_LEX_KW_ENTRY("else" , 0), // 4
504 BC_LEX_KW_ENTRY("for" , 1), // 5
505 BC_LEX_KW_ENTRY("halt" , 0), // 6
506 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100507 BC_LEX_KW_ENTRY("if" , 1), // 8
508 BC_LEX_KW_ENTRY("last" , 0), // 9
509 BC_LEX_KW_ENTRY("length" , 1), // 10
510 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100511 BC_LEX_KW_ENTRY("obase" , 1), // 12
512 BC_LEX_KW_ENTRY("print" , 0), // 13
513 BC_LEX_KW_ENTRY("quit" , 1), // 14
514 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100515 BC_LEX_KW_ENTRY("return" , 1), // 16
516 BC_LEX_KW_ENTRY("scale" , 1), // 17
517 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
518 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100519};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520#undef BC_LEX_KW_ENTRY
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100521enum {
522 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100523 | (1 << 0) // 0
524 | (1 << 1) // 1
525 | (0 << 2) // 2
526 | (1 << 3) // 3
527 | (0 << 4) // 4
528 | (1 << 5) // 5
529 | (0 << 6) // 6
530 | (1 << 7) // 7
531 | (1 << 8) // 8
532 | (0 << 9) // 9
533 | (1 << 10) // 10
534 | (0 << 11) // 11
535 | (1 << 12) // 12
536 | (0 << 13) // 13
537 | (1 << 14) // 14
538 | (0 << 15) // 15
539 | (1 << 16) // 16
540 | (1 << 17) // 17
541 | (1 << 18) // 18
542 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100545#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600546
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100547#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
548# define BC_STATUS BcStatus
549#else
550# define BC_STATUS void
551#endif
552
Gavin Howard01055ba2018-11-03 11:00:21 -0600553typedef struct BcLex {
554
555 const char *buf;
556 size_t i;
557 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600558 size_t len;
559 bool newline;
560
561 struct {
562 BcLexType t;
563 BcLexType last;
564 BcVec v;
565 } t;
566
Gavin Howard01055ba2018-11-03 11:00:21 -0600567} BcLex;
568
569#define BC_PARSE_STREND ((char) UCHAR_MAX)
570
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100571#define BC_PARSE_REL (1 << 0)
572#define BC_PARSE_PRINT (1 << 1)
Gavin Howard01055ba2018-11-03 11:00:21 -0600573#define BC_PARSE_NOCALL (1 << 2)
574#define BC_PARSE_NOREAD (1 << 3)
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100575#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600576
577#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
578#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
579
580#define BC_PARSE_FLAG_FUNC_INNER (1 << 0)
581#define BC_PARSE_FUNC_INNER(parse) \
582 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
583
584#define BC_PARSE_FLAG_FUNC (1 << 1)
585#define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
586
587#define BC_PARSE_FLAG_BODY (1 << 2)
588#define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
589
590#define BC_PARSE_FLAG_LOOP (1 << 3)
591#define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
592
593#define BC_PARSE_FLAG_LOOP_INNER (1 << 4)
594#define BC_PARSE_LOOP_INNER(parse) \
595 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
596
597#define BC_PARSE_FLAG_IF (1 << 5)
598#define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
599
600#define BC_PARSE_FLAG_ELSE (1 << 6)
601#define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
602
603#define BC_PARSE_FLAG_IF_END (1 << 7)
604#define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
605
606#define BC_PARSE_CAN_EXEC(parse) \
607 (!(BC_PARSE_TOP_FLAG(parse) & \
608 (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \
609 BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \
610 BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
611
Gavin Howard01055ba2018-11-03 11:00:21 -0600612struct BcParse;
613
614struct BcProgram;
615
Gavin Howard01055ba2018-11-03 11:00:21 -0600616typedef struct BcParse {
617
Gavin Howard01055ba2018-11-03 11:00:21 -0600618 BcLex l;
619
620 BcVec flags;
621
622 BcVec exits;
623 BcVec conds;
624
625 BcVec ops;
626
Gavin Howard01055ba2018-11-03 11:00:21 -0600627 BcFunc *func;
628 size_t fidx;
629
630 size_t nbraces;
631 bool auto_part;
632
633} BcParse;
634
Gavin Howard01055ba2018-11-03 11:00:21 -0600635typedef struct BcProgram {
636
637 size_t len;
638 size_t scale;
639
640 BcNum ib;
641 size_t ib_t;
642 BcNum ob;
643 size_t ob_t;
644
645 BcNum hexb;
646
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100647#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600648 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100649#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600650
651 BcVec results;
652 BcVec stack;
653
654 BcVec fns;
655 BcVec fn_map;
656
657 BcVec vars;
658 BcVec var_map;
659
660 BcVec arrs;
661 BcVec arr_map;
662
663 BcVec strs;
664 BcVec consts;
665
666 const char *file;
667
668 BcNum last;
669 BcNum zero;
670 BcNum one;
671
672 size_t nchars;
673
Gavin Howard01055ba2018-11-03 11:00:21 -0600674} BcProgram;
675
676#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
677
678#define BC_PROG_MAIN (0)
679#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100680#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600681#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100682#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600683
684#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
685#define BC_PROG_NUM(r, n) \
686 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
687
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100688#define BC_FLAG_W (1 << 0)
689#define BC_FLAG_V (1 << 1)
690#define BC_FLAG_S (1 << 2)
691#define BC_FLAG_Q (1 << 3)
692#define BC_FLAG_L (1 << 4)
693#define BC_FLAG_I (1 << 5)
694#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600695
696#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
697#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
698
Denys Vlasenko64074a12018-12-07 15:50:14 +0100699#define BC_MAX_OBASE ((unsigned) 999)
700#define BC_MAX_DIM ((unsigned) INT_MAX)
701#define BC_MAX_SCALE ((unsigned) UINT_MAX)
702#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
703#define BC_MAX_NUM BC_MAX_STRING
704// Unused apart from "limits" message. Just show a "biggish number" there.
705//#define BC_MAX_NAME BC_MAX_STRING
706//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
707//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
708#define BC_MAX_NAME_STR "999999999"
709#define BC_MAX_EXP_STR "999999999"
710#define BC_MAX_VARS_STR "999999999"
711
712#define BC_MAX_OBASE_STR "999"
713
714#if INT_MAX == 2147483647
715# define BC_MAX_DIM_STR "2147483647"
716#elif INT_MAX == 9223372036854775807
717# define BC_MAX_DIM_STR "9223372036854775807"
718#else
719# error Strange INT_MAX
720#endif
721
722#if UINT_MAX == 4294967295
723# define BC_MAX_SCALE_STR "4294967295"
724# define BC_MAX_STRING_STR "4294967294"
725#elif UINT_MAX == 18446744073709551615
726# define BC_MAX_SCALE_STR "18446744073709551615"
727# define BC_MAX_STRING_STR "18446744073709551614"
728#else
729# error Strange UINT_MAX
730#endif
731#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600732
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100733struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100734 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100735 IF_FEATURE_CLEAN_UP(smallint exiting;)
Denys Vlasenko69171dc2018-12-12 00:29:24 +0100736 smallint in_read;
Gavin Howard01055ba2018-11-03 11:00:21 -0600737
738 BcParse prs;
739 BcProgram prog;
740
Denys Vlasenko5318f812018-12-05 17:48:01 +0100741 // For error messages. Can be set to current parsed line,
742 // or [TODO] to current executing line (can be before last parsed one)
743 unsigned err_line;
744
Gavin Howard01055ba2018-11-03 11:00:21 -0600745 BcVec files;
746
747 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100748
749#if ENABLE_FEATURE_EDITING
750 line_input_t *line_input_state;
751#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100752} FIX_ALIASING;
753#define G (*ptr_to_globals)
754#define INIT_G() do { \
755 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
756} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100757#define FREE_G() do { \
758 FREE_PTR_TO_GLOBALS(); \
759} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100760#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
761#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100762#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100763#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100764# define G_interrupt bb_got_signal
765# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100766#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100767# define G_interrupt 0
768# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100769#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100770#if ENABLE_FEATURE_CLEAN_UP
771# define G_exiting G.exiting
772#else
773# define G_exiting 0
774#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100775#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100776#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100777
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100778#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600779
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100780// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600781// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100782enum {
783 BC_PARSE_EXPRS_BITS = 0
784 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
785 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
786 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
787 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
788 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
789 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
790 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
791 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600792};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100793static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
794{
795#if ULONG_MAX > 0xffffffff
796 // 64-bit version (will not work correctly for 32-bit longs!)
797 return BC_PARSE_EXPRS_BITS & (1UL << i);
798#else
799 // 32-bit version
800 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
801 if (i >= 32) {
802 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
803 i &= 31;
804 }
805 return m & (1UL << i);
806#endif
807}
Gavin Howard01055ba2018-11-03 11:00:21 -0600808
809// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100810static const uint8_t bc_parse_ops[] = {
811#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100812 OP(0, false), OP( 0, false ), // inc dec
813 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100814 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100815 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
816 OP(4, true ), OP( 4, true ), // mod + -
817 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
818 OP(1, false), // not
819 OP(7, true ), OP( 7, true ), // or and
820 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
821 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100822#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600823};
Denys Vlasenko65437582018-12-05 19:37:19 +0100824#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
825#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600826
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100827// Byte array of up to 4 BC_LEX's, packed into 32-bit word
828typedef uint32_t BcParseNext;
829
Gavin Howard01055ba2018-11-03 11:00:21 -0600830// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100831enum {
832#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
833#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
834#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
835 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
836 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
837 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
838 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
839 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
840 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
841 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
842#undef BC_PARSE_NEXT4
843#undef BC_PARSE_NEXT2
844#undef BC_PARSE_NEXT1
845};
Gavin Howard01055ba2018-11-03 11:00:21 -0600846#endif // ENABLE_BC
847
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100848#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100849static const //BcLexType - should be this type, but narrower type saves size:
850uint8_t
851dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600852 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
853 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
854 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
855 BC_LEX_STORE_PUSH,
856};
857
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100858static const //BcLexType - should be this type
859uint8_t
860dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600861 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
862 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
863 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
864 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
865 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
866 BC_LEX_INVALID, BC_LEX_INVALID,
867 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
868 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
869 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
870 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
871 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
872 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
873 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
874 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
875 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
876 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
877 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
878 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
879 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
880 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
881 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
882 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
883 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
884 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
885 BC_LEX_INVALID
886};
887
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100888static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
889int8_t
890dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600891 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
892 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
893 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
894 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
895 BC_INST_INVALID, BC_INST_INVALID,
896 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
897 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
898 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
899 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
900 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
901 BC_INST_INVALID, BC_INST_INVALID,
902 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
903 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
904 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
905 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
906 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
907 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
908 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
909 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
910 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
911 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
912 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
913 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
914};
915#endif // ENABLE_DC
916
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100917// In configurations where errors abort instead of propagating error
918// return code up the call chain, functions returning BC_STATUS
919// actually don't return anything, they always succeed and return "void".
920// A macro wrapper is provided, which makes this statement work:
921// s = zbc_func(...)
922// and makes it visible to the compiler that s is always zero,
923// allowing compiler to optimize dead code after the statement.
924//
925// To make code more readable, each such function has a "z"
926// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
927//
928#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100929# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100930# define ERRORFUNC /*nothing*/
931# define ERROR_RETURN(a) a
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100932//moved up: # define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100933# define RETURN_STATUS(v) return (v)
934#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100935# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100936# define ERRORFUNC NORETURN
937# define ERROR_RETURN(a) /*nothing*/
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100938//moved up: # define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100939# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
940#endif
941
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100942#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
943#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
944#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
945//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
946static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
947{
948 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
949}
950//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
951static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
952{
953 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
954}
955
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100956typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
957
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100958typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100959
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100960static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
961 BcNumBinaryOp op, size_t req);
962static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
963static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
964static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
965static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
966static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
967static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
968
969static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
970{
971 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
972 (void) scale;
973 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
974}
975
976static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
977{
978 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
979 (void) scale;
980 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
981}
982
983static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
984{
985 size_t req = BC_NUM_MREQ(a, b, scale);
986 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
987}
988
989static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
990{
991 size_t req = BC_NUM_MREQ(a, b, scale);
992 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
993}
994
995static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
996{
997 size_t req = BC_NUM_MREQ(a, b, scale);
998 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
999}
1000
1001static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1002{
1003 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1004}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +01001005
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001006static const BcNumBinaryOp zbc_program_ops[] = {
1007 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 -06001008};
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001009#if ERRORS_ARE_FATAL
1010# define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS)
1011# define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS)
1012# define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS)
1013# define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS)
1014# define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS)
1015# define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS)
1016#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001017
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001018static void fflush_and_check(void)
1019{
1020 fflush_all();
1021 if (ferror(stdout) || ferror(stderr))
1022 bb_perror_msg_and_die("output error");
1023}
1024
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001025#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001026#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001027do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001028 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001029 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001030 return BC_STATUS_FAILURE; \
1031} while (0)
1032#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001033#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001034#endif
1035
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001036static void quit(void) NORETURN;
1037static void quit(void)
1038{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001039 if (ferror(stdin))
1040 bb_perror_msg_and_die("input error");
1041 fflush_and_check();
1042 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001043}
1044
Denys Vlasenko5318f812018-12-05 17:48:01 +01001045static void bc_verror_msg(const char *fmt, va_list p)
1046{
Denys Vlasenko82269122018-12-14 16:30:56 +01001047 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +01001048 if (G.prog.file) {
1049 sv = applet_name;
1050 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
1051 }
1052 bb_verror_msg(fmt, p, NULL);
1053 if (G.prog.file) {
1054 free((char*)applet_name);
1055 applet_name = sv;
1056 }
1057}
1058
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001059static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001060{
1061 va_list p;
1062
1063 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001064 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001065 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001066
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001067 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001068 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001069 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001070}
1071
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001072#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001073static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001074{
1075 va_list p;
1076
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001077 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001078 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001079 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001080
1081 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001082 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001083 va_end(p);
1084
1085 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001086 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001087 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001088 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001089 exit(1);
1090 return BC_STATUS_FAILURE;
1091}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001092#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001093
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001094// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1095// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001096// function must not have caller-cleaned parameters on stack.
1097// Unfortunately, vararg function API does exactly that on most arches.
1098// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001099static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001100{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001101 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1102}
1103static ERRORFUNC int bc_error_bad_character(char c)
1104{
1105 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1106}
1107static ERRORFUNC int bc_error_bad_expression(void)
1108{
1109 ERROR_RETURN(return) bc_error("bad expression");
1110}
1111static ERRORFUNC int bc_error_bad_token(void)
1112{
1113 ERROR_RETURN(return) bc_error("bad token");
1114}
1115static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1116{
1117 ERROR_RETURN(return) bc_error("stack has too few elements");
1118}
1119static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1120{
1121 ERROR_RETURN(return) bc_error("variable is wrong type");
1122}
1123static ERRORFUNC int bc_error_nested_read_call(void)
1124{
1125 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001126}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001127#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001128static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001129{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001130 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001131}
Denys Vlasenko00646792018-12-05 18:12:27 +01001132static int bc_POSIX_does_not_allow(const char *msg)
1133{
1134 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1135}
1136static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1137{
1138 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1139}
1140static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1141{
1142 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1143}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001144#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001145
Gavin Howard01055ba2018-11-03 11:00:21 -06001146static void bc_vec_grow(BcVec *v, size_t n)
1147{
1148 size_t cap = v->cap * 2;
1149 while (cap < v->len + n) cap *= 2;
1150 v->v = xrealloc(v->v, v->size * cap);
1151 v->cap = cap;
1152}
1153
1154static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1155{
1156 v->size = esize;
1157 v->cap = BC_VEC_START_CAP;
1158 v->len = 0;
1159 v->dtor = dtor;
1160 v->v = xmalloc(esize * BC_VEC_START_CAP);
1161}
1162
Denys Vlasenko7d628012018-12-04 21:46:47 +01001163static void bc_char_vec_init(BcVec *v)
1164{
1165 bc_vec_init(v, sizeof(char), NULL);
1166}
1167
Gavin Howard01055ba2018-11-03 11:00:21 -06001168static void bc_vec_expand(BcVec *v, size_t req)
1169{
1170 if (v->cap < req) {
1171 v->v = xrealloc(v->v, v->size * req);
1172 v->cap = req;
1173 }
1174}
1175
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001176static void bc_vec_pop(BcVec *v)
1177{
1178 v->len--;
1179 if (v->dtor)
1180 v->dtor(v->v + (v->size * v->len));
1181}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001182
Gavin Howard01055ba2018-11-03 11:00:21 -06001183static void bc_vec_npop(BcVec *v, size_t n)
1184{
1185 if (!v->dtor)
1186 v->len -= n;
1187 else {
1188 size_t len = v->len - n;
1189 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1190 }
1191}
1192
Denys Vlasenko7d628012018-12-04 21:46:47 +01001193static void bc_vec_pop_all(BcVec *v)
1194{
1195 bc_vec_npop(v, v->len);
1196}
1197
Gavin Howard01055ba2018-11-03 11:00:21 -06001198static void bc_vec_push(BcVec *v, const void *data)
1199{
1200 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1201 memmove(v->v + (v->size * v->len), data, v->size);
1202 v->len += 1;
1203}
1204
1205static void bc_vec_pushByte(BcVec *v, char data)
1206{
1207 bc_vec_push(v, &data);
1208}
1209
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001210static void bc_vec_pushZeroByte(BcVec *v)
1211{
1212 //bc_vec_pushByte(v, '\0');
1213 // better:
1214 bc_vec_push(v, &const_int_0);
1215}
1216
Gavin Howard01055ba2018-11-03 11:00:21 -06001217static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1218{
1219 if (idx == v->len)
1220 bc_vec_push(v, data);
1221 else {
1222
1223 char *ptr;
1224
1225 if (v->len == v->cap) bc_vec_grow(v, 1);
1226
1227 ptr = v->v + v->size * idx;
1228
1229 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1230 memmove(ptr, data, v->size);
1231 }
1232}
1233
1234static void bc_vec_string(BcVec *v, size_t len, const char *str)
1235{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001236 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001237 bc_vec_expand(v, len + 1);
1238 memcpy(v->v, str, len);
1239 v->len = len;
1240
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001241 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001242}
1243
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001244#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001245static void bc_vec_concat(BcVec *v, const char *str)
1246{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001247 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001248
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001249 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001250
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001251 slen = strlen(str);
1252 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001253
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001254 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001255 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001256
1257 v->len = len;
1258}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001259#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001260
1261static void *bc_vec_item(const BcVec *v, size_t idx)
1262{
1263 return v->v + v->size * idx;
1264}
1265
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001266static char** bc_program_str(size_t idx)
1267{
1268 return bc_vec_item(&G.prog.strs, idx);
1269}
1270
1271static BcFunc* bc_program_func(size_t idx)
1272{
1273 return bc_vec_item(&G.prog.fns, idx);
1274}
1275
Gavin Howard01055ba2018-11-03 11:00:21 -06001276static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1277{
1278 return v->v + v->size * (v->len - idx - 1);
1279}
1280
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001281static void *bc_vec_top(const BcVec *v)
1282{
1283 return v->v + v->size * (v->len - 1);
1284}
1285
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001286static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001287{
1288 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001289 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001290 free(v->v);
1291}
1292
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001293static int bc_id_cmp(const void *e1, const void *e2)
1294{
1295 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1296}
1297
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001298static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001299{
1300 free(((BcId *) id)->name);
1301}
1302
Gavin Howard01055ba2018-11-03 11:00:21 -06001303static size_t bc_map_find(const BcVec *v, const void *ptr)
1304{
1305 size_t low = 0, high = v->len;
1306
1307 while (low < high) {
1308
1309 size_t mid = (low + high) / 2;
1310 BcId *id = bc_vec_item(v, mid);
1311 int result = bc_id_cmp(ptr, id);
1312
1313 if (result == 0)
1314 return mid;
1315 else if (result < 0)
1316 high = mid;
1317 else
1318 low = mid + 1;
1319 }
1320
1321 return low;
1322}
1323
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001324static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001325{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001326 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001327
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001328 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001329 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001330 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1331 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001332 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001333 bc_vec_pushAt(v, ptr, n);
1334 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001335}
1336
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001337#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001338static size_t bc_map_index(const BcVec *v, const void *ptr)
1339{
1340 size_t i = bc_map_find(v, ptr);
1341 if (i >= v->len) return BC_VEC_INVALID_IDX;
1342 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1343}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001344#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001345
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001346static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001347{
1348 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1349 || c > 0x7e
1350 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001351 bc_error_fmt("illegal character 0x%02x", c);
1352 return 1;
1353 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001354 return 0;
1355}
1356
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001357// Note: it _appends_ data from the stdin to vec.
Denys Vlasenko8a892472018-12-12 22:43:58 +01001358static void bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001359{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001360 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001361 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001362
Gavin Howard01055ba2018-11-03 11:00:21 -06001363#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001364 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001365 intr:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001366 G_interrupt = 0;
1367 // GNU bc says "interrupted execution."
1368 // GNU dc says "Interrupt!"
1369 fputs("\ninterrupted execution\n", stderr);
1370 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001371
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001372# if ENABLE_FEATURE_EDITING
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001373 if (G_ttyin) {
1374 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001375# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001376 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1377 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1378 if (n == 0) // ^C
1379 goto intr;
Denys Vlasenkoe755e302018-12-13 22:25:28 +01001380 bc_vec_pushZeroByte(vec);
1381 return;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001382 }
1383 i = 0;
1384 for (;;) {
1385 char c = line_buf[i++];
1386 if (!c) break;
1387 if (bad_input_byte(c)) goto again;
1388 }
1389 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001390# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001391 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001392# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001393#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001394 {
1395 int c;
1396 bool bad_chars = 0;
1397 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001398
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001399 IF_FEATURE_BC_SIGNALS(errno = 0;)
1400 do {
1401 c = fgetc(stdin);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001402#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001403 // Both conditions appear simultaneously, check both just in case
1404 if (errno == EINTR || G_interrupt) {
1405 // ^C was pressed
1406 clearerr(stdin);
1407 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001408 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001409#endif
1410 if (c == EOF) {
1411 if (ferror(stdin))
1412 quit(); // this emits error message
1413 // Note: EOF does not append '\n', therefore:
1414 // printf 'print 123\n' | bc - works
1415 // printf 'print 123' | bc - fails (syntax error)
1416 break;
1417 }
1418 bad_chars |= bad_input_byte(c);
1419 bc_vec_pushByte(vec, (char)c);
1420 } while (c != '\n');
1421 if (bad_chars) {
1422 // Bad chars on this line, ignore entire line
1423 vec->len = len;
1424 goto again;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001425 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001426 bc_vec_pushZeroByte(vec);
1427 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001428}
1429
Denys Vlasenkodf515392018-12-02 19:27:48 +01001430static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001431{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001432 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001433 size_t size = ((size_t) -1);
1434 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001435
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001436 // Never returns NULL (dies on errors)
1437 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001438
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001439 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001440 char c = buf[i];
1441 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1442 || c > 0x7e
1443 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001444 free(buf);
1445 buf = NULL;
1446 break;
1447 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001448 }
1449
Denys Vlasenkodf515392018-12-02 19:27:48 +01001450 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001451}
1452
Gavin Howard01055ba2018-11-03 11:00:21 -06001453static void bc_num_setToZero(BcNum *n, size_t scale)
1454{
1455 n->len = 0;
1456 n->neg = false;
1457 n->rdx = scale;
1458}
1459
1460static void bc_num_zero(BcNum *n)
1461{
1462 bc_num_setToZero(n, 0);
1463}
1464
1465static void bc_num_one(BcNum *n)
1466{
1467 bc_num_setToZero(n, 0);
1468 n->len = 1;
1469 n->num[0] = 1;
1470}
1471
1472static void bc_num_ten(BcNum *n)
1473{
1474 bc_num_setToZero(n, 0);
1475 n->len = 2;
1476 n->num[0] = 0;
1477 n->num[1] = 1;
1478}
1479
Denys Vlasenko3129f702018-12-09 12:04:44 +01001480// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001481static void bc_num_init(BcNum *n, size_t req)
1482{
1483 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001484 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001485 n->num = xmalloc(req);
1486 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001487 n->rdx = 0;
1488 n->len = 0;
1489 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001490}
1491
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001492static void bc_num_init_DEF_SIZE(BcNum *n)
1493{
1494 bc_num_init(n, BC_NUM_DEF_SIZE);
1495}
1496
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001497static void bc_num_expand(BcNum *n, size_t req)
1498{
1499 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1500 if (req > n->cap) {
1501 n->num = xrealloc(n->num, req);
1502 n->cap = req;
1503 }
1504}
1505
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001506static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001507{
1508 free(((BcNum *) num)->num);
1509}
1510
1511static void bc_num_copy(BcNum *d, BcNum *s)
1512{
1513 if (d != s) {
1514 bc_num_expand(d, s->cap);
1515 d->len = s->len;
1516 d->neg = s->neg;
1517 d->rdx = s->rdx;
1518 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1519 }
1520}
1521
Denys Vlasenko29301232018-12-11 15:29:32 +01001522static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001523{
1524 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001525 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001526
Denys Vlasenko29301232018-12-11 15:29:32 +01001527 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001528
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001529 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001530
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001531 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001532
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001533 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001534 pow *= 10;
1535
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001536 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001537 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001538 prev = result;
1539 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001540 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001541 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001542
Denys Vlasenko29301232018-12-11 15:29:32 +01001543 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001544}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001545#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001546# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001547#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001548
1549static void bc_num_ulong2num(BcNum *n, unsigned long val)
1550{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001551 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001552
1553 bc_num_zero(n);
1554
1555 if (val == 0) return;
1556
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001557 if (ULONG_MAX == 0xffffffffUL)
1558 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001559 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001560 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001561 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001562
1563 ptr = n->num;
1564 for (;;) {
1565 n->len++;
1566 *ptr++ = val % 10;
1567 val /= 10;
1568 if (val == 0) break;
1569 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001570}
1571
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001572static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001573 size_t len)
1574{
1575 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001576 for (i = 0; i < len; ++i) {
1577 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001578 a[i + j++] += 10;
1579 a[i + j] -= 1;
1580 }
1581 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001582}
1583
1584static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1585{
1586 size_t i;
1587 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001588 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001589 return BC_NUM_NEG(i + 1, c < 0);
1590}
1591
1592static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1593{
1594 size_t i, min, a_int, b_int, diff;
1595 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001596 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001597 ssize_t cmp;
1598
1599 if (a == b) return 0;
1600 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1601 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001602
1603 if (a->neg != b->neg) // signs of a and b differ
1604 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1605 return (int)b->neg - (int)a->neg;
1606 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001607
1608 a_int = BC_NUM_INT(a);
1609 b_int = BC_NUM_INT(b);
1610 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001611
1612 if (a_int != 0) return (ssize_t) a_int;
1613
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001614 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001615 if (a_max) {
1616 min = b->rdx;
1617 diff = a->rdx - b->rdx;
1618 max_num = a->num + diff;
1619 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001620 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1621 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001622 min = a->rdx;
1623 diff = b->rdx - a->rdx;
1624 max_num = b->num + diff;
1625 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001626 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001627 }
1628
1629 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001630 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001631
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001632 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001633 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001634 }
1635
1636 return 0;
1637}
1638
1639static void bc_num_truncate(BcNum *n, size_t places)
1640{
1641 if (places == 0) return;
1642
1643 n->rdx -= places;
1644
1645 if (n->len != 0) {
1646 n->len -= places;
1647 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1648 }
1649}
1650
1651static void bc_num_extend(BcNum *n, size_t places)
1652{
1653 size_t len = n->len + places;
1654
1655 if (places != 0) {
1656
1657 if (n->cap < len) bc_num_expand(n, len);
1658
1659 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1660 memset(n->num, 0, sizeof(BcDig) * places);
1661
1662 n->len += places;
1663 n->rdx += places;
1664 }
1665}
1666
1667static void bc_num_clean(BcNum *n)
1668{
1669 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1670 if (n->len == 0)
1671 n->neg = false;
1672 else if (n->len < n->rdx)
1673 n->len = n->rdx;
1674}
1675
1676static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1677{
1678 if (n->rdx < scale)
1679 bc_num_extend(n, scale - n->rdx);
1680 else
1681 bc_num_truncate(n, n->rdx - scale);
1682
1683 bc_num_clean(n);
1684 if (n->len != 0) n->neg = !neg1 != !neg2;
1685}
1686
1687static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1688 BcNum *restrict b)
1689{
1690 if (idx < n->len) {
1691
1692 b->len = n->len - idx;
1693 a->len = idx;
1694 a->rdx = b->rdx = 0;
1695
1696 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1697 memcpy(a->num, n->num, idx * sizeof(BcDig));
1698 }
1699 else {
1700 bc_num_zero(b);
1701 bc_num_copy(a, n);
1702 }
1703
1704 bc_num_clean(a);
1705 bc_num_clean(b);
1706}
1707
Denys Vlasenko29301232018-12-11 15:29:32 +01001708static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001709{
Denys Vlasenko29301232018-12-11 15:29:32 +01001710 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001711
1712 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1713 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1714 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001715 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001716 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001717
1718 if (n->rdx >= places)
1719 n->rdx -= places;
1720 else {
1721 bc_num_extend(n, places - n->rdx);
1722 n->rdx = 0;
1723 }
1724
1725 bc_num_clean(n);
1726
Denys Vlasenko29301232018-12-11 15:29:32 +01001727 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001728}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001729#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001730# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001731#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001732
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001733static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001734{
1735 BcNum one;
1736 BcDig num[2];
1737
1738 one.cap = 2;
1739 one.num = num;
1740 bc_num_one(&one);
1741
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001742 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001743}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001744#if ERRORS_ARE_FATAL
1745# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1746#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001747
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001748static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001749{
1750 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1751 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1752 int carry, in;
1753
1754 // Because this function doesn't need to use scale (per the bc spec),
1755 // I am hijacking it to say whether it's doing an add or a subtract.
1756
1757 if (a->len == 0) {
1758 bc_num_copy(c, b);
1759 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001760 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001761 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001763 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001764 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001765 }
1766
1767 c->neg = a->neg;
1768 c->rdx = BC_MAX(a->rdx, b->rdx);
1769 min_rdx = BC_MIN(a->rdx, b->rdx);
1770 c->len = 0;
1771
1772 if (a->rdx > b->rdx) {
1773 diff = a->rdx - b->rdx;
1774 ptr = a->num;
1775 ptr_a = a->num + diff;
1776 ptr_b = b->num;
1777 }
1778 else {
1779 diff = b->rdx - a->rdx;
1780 ptr = b->num;
1781 ptr_a = a->num;
1782 ptr_b = b->num + diff;
1783 }
1784
1785 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1786
1787 ptr_c += diff;
1788 a_int = BC_NUM_INT(a);
1789 b_int = BC_NUM_INT(b);
1790
1791 if (a_int > b_int) {
1792 min_int = b_int;
1793 max = a_int;
1794 ptr = ptr_a;
1795 }
1796 else {
1797 min_int = a_int;
1798 max = b_int;
1799 ptr = ptr_b;
1800 }
1801
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001802 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1804 carry = in / 10;
1805 ptr_c[i] = (BcDig)(in % 10);
1806 }
1807
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001808 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001809 in = ((int) ptr[i]) + carry;
1810 carry = in / 10;
1811 ptr_c[i] = (BcDig)(in % 10);
1812 }
1813
1814 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1815
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001816 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001817}
1818
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001819static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001820{
Gavin Howard01055ba2018-11-03 11:00:21 -06001821 ssize_t cmp;
1822 BcNum *minuend, *subtrahend;
1823 size_t start;
1824 bool aneg, bneg, neg;
1825
1826 // Because this function doesn't need to use scale (per the bc spec),
1827 // I am hijacking it to say whether it's doing an add or a subtract.
1828
1829 if (a->len == 0) {
1830 bc_num_copy(c, b);
1831 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001832 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001833 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001834 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001835 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001836 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 }
1838
1839 aneg = a->neg;
1840 bneg = b->neg;
1841 a->neg = b->neg = false;
1842
1843 cmp = bc_num_cmp(a, b);
1844
1845 a->neg = aneg;
1846 b->neg = bneg;
1847
1848 if (cmp == 0) {
1849 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001850 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001851 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001852 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001853 neg = a->neg;
1854 minuend = a;
1855 subtrahend = b;
1856 }
1857 else {
1858 neg = b->neg;
1859 if (sub) neg = !neg;
1860 minuend = b;
1861 subtrahend = a;
1862 }
1863
1864 bc_num_copy(c, minuend);
1865 c->neg = neg;
1866
1867 if (c->rdx < subtrahend->rdx) {
1868 bc_num_extend(c, subtrahend->rdx - c->rdx);
1869 start = 0;
1870 }
1871 else
1872 start = c->rdx - subtrahend->rdx;
1873
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001874 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875
1876 bc_num_clean(c);
1877
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001878 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001879}
1880
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001881static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001882 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001883#if ERRORS_ARE_FATAL
1884# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1885#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001886{
1887 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001888 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001889 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001890 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001891
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 if (a->len == 0 || b->len == 0) {
1893 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001894 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001896 aone = BC_NUM_ONE(a);
1897 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001899 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001900 }
1901
1902 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1903 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1904 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001905 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001906 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001907
Gavin Howard01055ba2018-11-03 11:00:21 -06001908 bc_num_expand(c, a->len + b->len + 1);
1909
1910 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001911 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001912
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001913 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001914
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001915 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001916 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001917 unsigned in = c->num[i + j];
1918 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1919 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001920 carry = in / 10;
1921 c->num[i + j] = (BcDig)(in % 10);
1922 }
1923
1924 c->num[i + j] += (BcDig) carry;
1925 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001926
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001927#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001928 // a=2^1000000
1929 // a*a <- without check below, this will not be interruptible
1930 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001931#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001932 }
1933
1934 c->len = len;
1935
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001936 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001937 }
1938
1939 bc_num_init(&l1, max);
1940 bc_num_init(&h1, max);
1941 bc_num_init(&l2, max);
1942 bc_num_init(&h2, max);
1943 bc_num_init(&m1, max);
1944 bc_num_init(&m2, max);
1945 bc_num_init(&z0, max);
1946 bc_num_init(&z1, max);
1947 bc_num_init(&z2, max);
1948 bc_num_init(&temp, max + max);
1949
1950 bc_num_split(a, max2, &l1, &h1);
1951 bc_num_split(b, max2, &l2, &h2);
1952
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001953 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001954 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001955 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 if (s) goto err;
1957
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001958 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001960 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001961 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001962 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001963 if (s) goto err;
1964
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001965 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001966 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001967 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 if (s) goto err;
1969
Denys Vlasenko29301232018-12-11 15:29:32 +01001970 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001971 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001972 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001973 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001974 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001975 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001976 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001977
1978err:
1979 bc_num_free(&temp);
1980 bc_num_free(&z2);
1981 bc_num_free(&z1);
1982 bc_num_free(&z0);
1983 bc_num_free(&m2);
1984 bc_num_free(&m1);
1985 bc_num_free(&h2);
1986 bc_num_free(&l2);
1987 bc_num_free(&h1);
1988 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001989 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001990}
1991
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001992static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001993{
1994 BcStatus s;
1995 BcNum cpa, cpb;
1996 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1997
1998 scale = BC_MAX(scale, a->rdx);
1999 scale = BC_MAX(scale, b->rdx);
2000 scale = BC_MIN(a->rdx + b->rdx, scale);
2001 maxrdx = BC_MAX(maxrdx, scale);
2002
2003 bc_num_init(&cpa, a->len);
2004 bc_num_init(&cpb, b->len);
2005
2006 bc_num_copy(&cpa, a);
2007 bc_num_copy(&cpb, b);
2008 cpa.neg = cpb.neg = false;
2009
Denys Vlasenko29301232018-12-11 15:29:32 +01002010 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002011 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002012 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002014 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06002015 if (s) goto err;
2016
2017 maxrdx += scale;
2018 bc_num_expand(c, c->len + maxrdx);
2019
2020 if (c->len < maxrdx) {
2021 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2022 c->len += maxrdx;
2023 }
2024
2025 c->rdx = maxrdx;
2026 bc_num_retireMul(c, scale, a->neg, b->neg);
2027
2028err:
2029 bc_num_free(&cpb);
2030 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002031 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002032}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002033#if ERRORS_ARE_FATAL
2034# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2035#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002036
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002037static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002038{
2039 BcStatus s = BC_STATUS_SUCCESS;
2040 BcDig *n, *p, q;
2041 size_t len, end, i;
2042 BcNum cp;
2043 bool zero = true;
2044
2045 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002046 RETURN_STATUS(bc_error("divide by zero"));
2047 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002048 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002049 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002050 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002051 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002052 bc_num_copy(c, a);
2053 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002054 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002055 }
2056
2057 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2058 bc_num_copy(&cp, a);
2059 len = b->len;
2060
2061 if (len > cp.len) {
2062 bc_num_expand(&cp, len + 2);
2063 bc_num_extend(&cp, len - cp.len);
2064 }
2065
2066 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2067 cp.rdx -= b->rdx;
2068 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2069
2070 if (b->rdx == b->len) {
2071 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2072 len -= i - 1;
2073 }
2074
2075 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2076
2077 // We want an extra zero in front to make things simpler.
2078 cp.num[cp.len++] = 0;
2079 end = cp.len - len;
2080
2081 bc_num_expand(c, cp.len);
2082
2083 bc_num_zero(c);
2084 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2085 c->rdx = cp.rdx;
2086 c->len = cp.len;
2087 p = b->num;
2088
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002089 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002090 n = cp.num + i;
2091 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002092 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002093 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002094#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002095 // a=2^100000
2096 // scale=40000
2097 // 1/a <- without check below, this will not be interruptible
2098 if (G_interrupt) {
2099 s = BC_STATUS_FAILURE;
2100 break;
2101 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002102#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002103 }
2104
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002105 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002106 bc_num_free(&cp);
2107
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002108 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002109}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002110#if ERRORS_ARE_FATAL
2111# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2112#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002113
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002114static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 BcNum *restrict d, size_t scale, size_t ts)
2116{
2117 BcStatus s;
2118 BcNum temp;
2119 bool neg;
2120
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002121 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002122 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002123
2124 if (a->len == 0) {
2125 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002126 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002127 }
2128
2129 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002130 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002131 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002132
2133 if (scale != 0) scale = ts;
2134
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002135 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002136 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002137 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002138 if (s) goto err;
2139
2140 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2141
2142 neg = d->neg;
2143 bc_num_retireMul(d, ts, a->neg, b->neg);
2144 d->neg = neg;
2145
2146err:
2147 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002148 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002149}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002150#if ERRORS_ARE_FATAL
2151# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2152#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002153
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002154static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002155{
2156 BcStatus s;
2157 BcNum c1;
2158 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2159
2160 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002161 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 bc_num_free(&c1);
2163
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002164 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002165}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002166#if ERRORS_ARE_FATAL
2167# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2168#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002169
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002170static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002171{
2172 BcStatus s = BC_STATUS_SUCCESS;
2173 BcNum copy;
2174 unsigned long pow;
2175 size_t i, powrdx, resrdx;
2176 bool neg, zero;
2177
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002178 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002179
2180 if (b->len == 0) {
2181 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002182 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002183 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002184 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002185 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002186 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002187 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002188 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002189 if (!b->neg)
2190 bc_num_copy(c, a);
2191 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002192 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002193 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002194 }
2195
2196 neg = b->neg;
2197 b->neg = false;
2198
Denys Vlasenko29301232018-12-11 15:29:32 +01002199 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002200 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002201
2202 bc_num_init(&copy, a->len);
2203 bc_num_copy(&copy, a);
2204
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002205 if (!neg) {
2206 if (a->rdx > scale)
2207 scale = a->rdx;
2208 if (a->rdx * pow < scale)
2209 scale = a->rdx * pow;
2210 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002211
2212 b->neg = neg;
2213
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002214 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002216 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002217 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002218 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002219 //if (G_interrupt) {
2220 // s = BC_STATUS_FAILURE;
2221 // goto err;
2222 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002223 }
2224
Gavin Howard01055ba2018-11-03 11:00:21 -06002225 bc_num_copy(c, &copy);
2226
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002227 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002228
2229 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002230 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002231 if (s) goto err;
2232
2233 if (pow & 1) {
2234 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002235 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002236 if (s) goto err;
2237 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002238 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002239 //if (G_interrupt) {
2240 // s = BC_STATUS_FAILURE;
2241 // goto err;
2242 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002243 }
2244
2245 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002246 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002247 if (s) goto err;
2248 }
2249
Gavin Howard01055ba2018-11-03 11:00:21 -06002250 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2251
2252 // We can't use bc_num_clean() here.
2253 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2254 if (zero) bc_num_setToZero(c, scale);
2255
2256err:
2257 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002258 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002259}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002260#if ERRORS_ARE_FATAL
2261# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2262#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002263
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002264static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002265 BcNumBinaryOp op, size_t req)
2266{
2267 BcStatus s;
2268 BcNum num2, *ptr_a, *ptr_b;
2269 bool init = false;
2270
2271 if (c == a) {
2272 ptr_a = &num2;
2273 memcpy(ptr_a, c, sizeof(BcNum));
2274 init = true;
2275 }
2276 else
2277 ptr_a = a;
2278
2279 if (c == b) {
2280 ptr_b = &num2;
2281 if (c != a) {
2282 memcpy(ptr_b, c, sizeof(BcNum));
2283 init = true;
2284 }
2285 }
2286 else
2287 ptr_b = b;
2288
2289 if (init)
2290 bc_num_init(c, req);
2291 else
2292 bc_num_expand(c, req);
2293
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002294 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01002295 ERROR_RETURN(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002296
2297 if (init) bc_num_free(&num2);
2298
Denys Vlasenko29301232018-12-11 15:29:32 +01002299 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002300}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002301#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002302# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002303#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002304
Denys Vlasenko9311e012018-12-10 11:54:18 +01002305static bool bc_num_strValid(const char *val, size_t base)
2306{
2307 BcDig b;
2308 bool radix;
2309
2310 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2311 radix = false;
2312 for (;;) {
2313 BcDig c = *val++;
2314 if (c == '\0')
2315 break;
2316 if (c == '.') {
2317 if (radix) return false;
2318 radix = true;
2319 continue;
2320 }
2321 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2322 return false;
2323 }
2324 return true;
2325}
2326
2327// Note: n is already "bc_num_zero()"ed,
2328// leading zeroes in "val" are removed
2329static void bc_num_parseDecimal(BcNum *n, const char *val)
2330{
2331 size_t len, i;
2332 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002333
2334 len = strlen(val);
2335 if (len == 0)
2336 return;
2337
Denys Vlasenko9311e012018-12-10 11:54:18 +01002338 bc_num_expand(n, len);
2339
2340 ptr = strchr(val, '.');
2341
2342 n->rdx = 0;
2343 if (ptr != NULL)
2344 n->rdx = (size_t)((val + len) - (ptr + 1));
2345
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002346 for (i = 0; val[i]; ++i) {
2347 if (val[i] != '0' && val[i] != '.') {
2348 // Not entirely zero value - convert it, and exit
2349 i = len - 1;
2350 for (;;) {
2351 n->num[n->len] = val[i] - '0';
2352 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002353 skip_dot:
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002354 if (i == 0) break;
2355 if (val[--i] == '.') goto skip_dot;
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002356 }
2357 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002358 }
2359 }
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002360 // if for() exits without hitting if(), the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002361}
2362
2363// Note: n is already "bc_num_zero()"ed,
2364// leading zeroes in "val" are removed
2365static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2366{
2367 BcStatus s;
2368 BcNum temp, mult, result;
2369 BcDig c = '\0';
2370 unsigned long v;
2371 size_t i, digits;
2372
2373 for (i = 0; ; ++i) {
2374 if (val[i] == '\0')
2375 return;
2376 if (val[i] != '.' && val[i] != '0')
2377 break;
2378 }
2379
2380 bc_num_init_DEF_SIZE(&temp);
2381 bc_num_init_DEF_SIZE(&mult);
2382
2383 for (;;) {
2384 c = *val++;
2385 if (c == '\0') goto int_err;
2386 if (c == '.') break;
2387
2388 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2389
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002390 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002391 if (s) goto int_err;
2392 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002393 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002394 if (s) goto int_err;
2395 }
2396
2397 bc_num_init(&result, base->len);
2398 //bc_num_zero(&result); - already is
2399 bc_num_one(&mult);
2400
2401 digits = 0;
2402 for (;;) {
2403 c = *val++;
2404 if (c == '\0') break;
2405 digits++;
2406
2407 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2408
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002409 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002410 if (s) goto err;
2411 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002412 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002413 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002414 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002415 if (s) goto err;
2416 }
2417
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002418 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002419 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002420 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002421 if (s) goto err;
2422
2423 if (n->len != 0) {
2424 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2425 } else
2426 bc_num_zero(n);
2427
2428err:
2429 bc_num_free(&result);
2430int_err:
2431 bc_num_free(&mult);
2432 bc_num_free(&temp);
2433}
2434
Denys Vlasenko29301232018-12-11 15:29:32 +01002435static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002436 size_t base_t)
2437{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002438 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002439 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002440
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002441 bc_num_zero(n);
2442 while (*val == '0') val++;
2443
Gavin Howard01055ba2018-11-03 11:00:21 -06002444 if (base_t == 10)
2445 bc_num_parseDecimal(n, val);
2446 else
2447 bc_num_parseBase(n, val, base);
2448
Denys Vlasenko29301232018-12-11 15:29:32 +01002449 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002450}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002451#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002452# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002453#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002454
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002455static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002456{
2457 BcStatus s;
2458 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2459 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2460 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2461
2462 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2463 bc_num_expand(b, req);
2464
2465 if (a->len == 0) {
2466 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002467 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 }
2469 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002470 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002471 else if (BC_NUM_ONE(a)) {
2472 bc_num_one(b);
2473 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002474 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002475 }
2476
2477 scale = BC_MAX(scale, a->rdx) + 1;
2478 len = a->len + scale;
2479
2480 bc_num_init(&num1, len);
2481 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002482 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002483
2484 bc_num_one(&half);
2485 half.num[0] = 5;
2486 half.rdx = 1;
2487
2488 bc_num_init(&f, len);
2489 bc_num_init(&fprime, len);
2490
2491 x0 = &num1;
2492 x1 = &num2;
2493
2494 bc_num_one(x0);
2495 pow = BC_NUM_INT(a);
2496
2497 if (pow) {
2498
2499 if (pow & 1)
2500 x0->num[0] = 2;
2501 else
2502 x0->num[0] = 6;
2503
2504 pow -= 2 - (pow & 1);
2505
2506 bc_num_extend(x0, pow);
2507
2508 // Make sure to move the radix back.
2509 x0->rdx -= pow;
2510 }
2511
2512 x0->rdx = digs = digs1 = 0;
2513 resrdx = scale + 2;
2514 len = BC_NUM_INT(x0) + resrdx - 1;
2515
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002516 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002517
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002518 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002519 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002520 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002521 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002522 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002523 if (s) goto err;
2524
2525 cmp = bc_num_cmp(x1, x0);
2526 digs = x1->len - (unsigned long long) llabs(cmp);
2527
2528 if (cmp == cmp2 && digs == digs1)
2529 times += 1;
2530 else
2531 times = 0;
2532
2533 resrdx += times > 4;
2534
2535 cmp2 = cmp1;
2536 cmp1 = cmp;
2537 digs1 = digs;
2538
2539 temp = x0;
2540 x0 = x1;
2541 x1 = temp;
2542 }
2543
Gavin Howard01055ba2018-11-03 11:00:21 -06002544 bc_num_copy(b, x0);
2545 scale -= 1;
2546 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2547
2548err:
2549 bc_num_free(&fprime);
2550 bc_num_free(&f);
2551 bc_num_free(&half);
2552 bc_num_free(&num2);
2553 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002554 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002555}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002556#if ERRORS_ARE_FATAL
2557# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2558#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002559
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002560static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002561 size_t scale)
2562{
2563 BcStatus s;
2564 BcNum num2, *ptr_a;
2565 bool init = false;
2566 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2567
2568 if (c == a) {
2569 memcpy(&num2, c, sizeof(BcNum));
2570 ptr_a = &num2;
2571 bc_num_init(c, len);
2572 init = true;
2573 }
2574 else {
2575 ptr_a = a;
2576 bc_num_expand(c, len);
2577 }
2578
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002579 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002580
2581 if (init) bc_num_free(&num2);
2582
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002583 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002584}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002585#if ERRORS_ARE_FATAL
2586# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2587#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002588
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002589#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002590static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002591{
2592 BcStatus s;
2593 BcNum base, exp, two, temp;
2594
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002595 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002596 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002597 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002598 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002599 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002600 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002601
2602 bc_num_expand(d, c->len);
2603 bc_num_init(&base, c->len);
2604 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002605 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002606 bc_num_init(&temp, b->len);
2607
2608 bc_num_one(&two);
2609 two.num[0] = 2;
2610 bc_num_one(d);
2611
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002612 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002613 if (s) goto err;
2614 bc_num_copy(&exp, b);
2615
2616 while (exp.len != 0) {
2617
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002618 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002619 if (s) goto err;
2620
2621 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002622 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002623 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002624 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002625 if (s) goto err;
2626 }
2627
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002628 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002629 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002630 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002631 if (s) goto err;
2632 }
2633
2634err:
2635 bc_num_free(&temp);
2636 bc_num_free(&two);
2637 bc_num_free(&exp);
2638 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002639 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002640}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002641#if ERRORS_ARE_FATAL
2642# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2643#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002644#endif // ENABLE_DC
2645
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002646#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002647static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002648{
2649 BcId a;
2650 size_t i;
2651
2652 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002653 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002654 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002655 }
2656
2657 a.idx = var;
2658 a.name = name;
2659
2660 bc_vec_push(&f->autos, &a);
2661
Denys Vlasenko29301232018-12-11 15:29:32 +01002662 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002663}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002664#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002665# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002666#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002667#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002668
2669static void bc_func_init(BcFunc *f)
2670{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002671 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002672 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2673 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2674 f->nparams = 0;
2675}
2676
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002677static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002678{
2679 BcFunc *f = (BcFunc *) func;
2680 bc_vec_free(&f->code);
2681 bc_vec_free(&f->autos);
2682 bc_vec_free(&f->labels);
2683}
2684
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002685static void bc_array_expand(BcVec *a, size_t len);
2686
Gavin Howard01055ba2018-11-03 11:00:21 -06002687static void bc_array_init(BcVec *a, bool nums)
2688{
2689 if (nums)
2690 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2691 else
2692 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2693 bc_array_expand(a, 1);
2694}
2695
Gavin Howard01055ba2018-11-03 11:00:21 -06002696static void bc_array_expand(BcVec *a, size_t len)
2697{
2698 BcResultData data;
2699
2700 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2701 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002702 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002703 bc_vec_push(a, &data.n);
2704 }
2705 }
2706 else {
2707 while (len > a->len) {
2708 bc_array_init(&data.v, true);
2709 bc_vec_push(a, &data.v);
2710 }
2711 }
2712}
2713
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002714static void bc_array_copy(BcVec *d, const BcVec *s)
2715{
2716 size_t i;
2717
2718 bc_vec_pop_all(d);
2719 bc_vec_expand(d, s->cap);
2720 d->len = s->len;
2721
2722 for (i = 0; i < s->len; ++i) {
2723 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2724 bc_num_init(dnum, snum->len);
2725 bc_num_copy(dnum, snum);
2726 }
2727}
2728
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002729static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002730{
2731 free(*((char **) string));
2732}
2733
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002734#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002735static void bc_result_copy(BcResult *d, BcResult *src)
2736{
2737 d->t = src->t;
2738
2739 switch (d->t) {
2740
2741 case BC_RESULT_TEMP:
2742 case BC_RESULT_IBASE:
2743 case BC_RESULT_SCALE:
2744 case BC_RESULT_OBASE:
2745 {
2746 bc_num_init(&d->d.n, src->d.n.len);
2747 bc_num_copy(&d->d.n, &src->d.n);
2748 break;
2749 }
2750
2751 case BC_RESULT_VAR:
2752 case BC_RESULT_ARRAY:
2753 case BC_RESULT_ARRAY_ELEM:
2754 {
2755 d->d.id.name = xstrdup(src->d.id.name);
2756 break;
2757 }
2758
2759 case BC_RESULT_CONSTANT:
2760 case BC_RESULT_LAST:
2761 case BC_RESULT_ONE:
2762 case BC_RESULT_STR:
2763 {
2764 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2765 break;
2766 }
2767 }
2768}
2769#endif // ENABLE_DC
2770
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002771static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002772{
2773 BcResult *r = (BcResult *) result;
2774
2775 switch (r->t) {
2776
2777 case BC_RESULT_TEMP:
2778 case BC_RESULT_IBASE:
2779 case BC_RESULT_SCALE:
2780 case BC_RESULT_OBASE:
2781 {
2782 bc_num_free(&r->d.n);
2783 break;
2784 }
2785
2786 case BC_RESULT_VAR:
2787 case BC_RESULT_ARRAY:
2788 case BC_RESULT_ARRAY_ELEM:
2789 {
2790 free(r->d.id.name);
2791 break;
2792 }
2793
2794 default:
2795 {
2796 // Do nothing.
2797 break;
2798 }
2799 }
2800}
2801
2802static void bc_lex_lineComment(BcLex *l)
2803{
2804 l->t.t = BC_LEX_WHITESPACE;
2805 while (l->i < l->len && l->buf[l->i++] != '\n');
2806 --l->i;
2807}
2808
2809static void bc_lex_whitespace(BcLex *l)
2810{
Gavin Howard01055ba2018-11-03 11:00:21 -06002811 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002812 for (;;) {
2813 char c = l->buf[l->i];
2814 if (c == '\n' || !isspace(c))
2815 break;
2816 l->i++;
2817 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002818}
2819
Denys Vlasenko29301232018-12-11 15:29:32 +01002820static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002821{
2822 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002823 size_t len, bslashes, i, ccnt;
2824 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002825
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002826 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002827 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002828 bslashes = 0;
2829 ccnt = i = 0;
2830 for (;;) {
2831 char c = buf[i];
2832 if (c == '\0')
2833 break;
2834 if (c == '\\' && buf[i + 1] == '\n') {
2835 i += 2;
2836 bslashes++;
2837 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002838 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002839 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2840 if (c != '.') break;
2841 // if '.' was already seen, stop on second one:
2842 if (pt) break;
2843 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002844 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002845 // buf[i] is one of "0-9A-F."
2846 i++;
2847 if (c != '.')
2848 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002849 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002850 //i is buf[i] index of the first not-yet-parsed char
2851 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002852
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002853 //ccnt is the number of chars in the number string, excluding possible
2854 //trailing "." and possible following trailing "\<newline>"(s).
2855 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2856
Denys Vlasenko64074a12018-12-07 15:50:14 +01002857 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2858 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2859 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002860 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002861 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002862
Denys Vlasenko7d628012018-12-04 21:46:47 +01002863 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002864 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002865 bc_vec_push(&l->t.v, &start);
2866
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002867 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002868 // If we have hit a backslash, skip it. We don't have
2869 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002870 if (*buf == '\\') {
2871 buf += 2;
2872 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002873 continue;
2874 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002875 bc_vec_push(&l->t.v, buf);
2876 buf++;
2877 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002878 }
2879
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002880 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002881
Denys Vlasenko29301232018-12-11 15:29:32 +01002882 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002883}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002884#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002885# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002886#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002887
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002888static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002889{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002890 size_t i;
2891 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002892
2893 l->t.t = BC_LEX_NAME;
2894
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002895 i = 0;
2896 buf = l->buf + l->i - 1;
2897 for (;;) {
2898 char c = buf[i];
2899 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2900 i++;
2901 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002902
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002903#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002904 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2905 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2906 if (i > BC_MAX_STRING)
2907 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2908 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002909#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002910 bc_vec_string(&l->t.v, i, buf);
2911
2912 // Increment the index. We minus 1 because it has already been incremented.
2913 l->i += i - 1;
2914
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002915 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002916}
2917
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002918static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002919{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002920 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002921}
2922
2923static void bc_lex_free(BcLex *l)
2924{
2925 bc_vec_free(&l->t.v);
2926}
2927
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002928static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002929{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002930 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002931 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002932}
2933
Denys Vlasenkoe755e302018-12-13 22:25:28 +01002934IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2935IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002936
2937static BC_STATUS zcommon_lex_token(BcLex *l)
2938{
2939 if (IS_BC) {
2940 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2941 }
2942 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2943}
2944
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002945static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002946{
2947 BcStatus s;
2948
2949 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002950 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002951
2952 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002953 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002954 l->t.t = BC_LEX_EOF;
2955
2956 l->newline = (l->i == l->len);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002957 if (l->newline) RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002958
2959 // Loop until failure or we don't have whitespace. This
2960 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002961 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002962 do {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002963 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002964 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2965 l->buf + l->i);
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002966 ERROR_RETURN(s =) zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002967 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002968 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002969
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002970 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002971}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002972#if ERRORS_ARE_FATAL
2973# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS)
2974#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002975
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002976static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002977{
2978 l->buf = text;
2979 l->i = 0;
2980 l->len = strlen(text);
2981 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002982 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002983}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002984#if ERRORS_ARE_FATAL
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002985# define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002986#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002987
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002988#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002989static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002990{
2991 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002992 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002993 const char *buf = l->buf + l->i - 1;
2994
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002995 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2996 const char *keyword8 = bc_lex_kws[i].name8;
2997 unsigned j = 0;
2998 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2999 j++;
3000 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003001 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003002 if (keyword8[j] != '\0')
3003 continue;
3004 match:
3005 // buf starts with keyword bc_lex_kws[i]
3006 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003007 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003008 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003009 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003010 }
3011
3012 // We minus 1 because the index has already been incremented.
3013 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003014 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003015 }
3016
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003017 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003018
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003019 if (l->t.v.len > 2) {
3020 // Prevent this:
3021 // >>> qwe=1
3022 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3023 // '
3024 unsigned len = strchrnul(buf, '\n') - buf;
3025 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3026 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003027
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003028 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003029}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003030#if ERRORS_ARE_FATAL
3031# define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__), BC_STATUS_SUCCESS)
3032#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003033
Denys Vlasenko26819db2018-12-12 16:08:46 +01003034static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003035{
3036 size_t len, nls = 0, i = l->i;
3037 char c;
3038
3039 l->t.t = BC_LEX_STR;
3040
Denys Vlasenko26819db2018-12-12 16:08:46 +01003041 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i])
3042 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003043
3044 if (c == '\0') {
3045 l->i = i;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003046 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003047 }
3048
3049 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003050 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3051 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3052 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003053 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003054 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003055 bc_vec_string(&l->t.v, len, l->buf + l->i);
3056
3057 l->i = i + 1;
3058 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003059 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003060
Denys Vlasenko26819db2018-12-12 16:08:46 +01003061 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003062}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003063#if ERRORS_ARE_FATAL
3064# define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3065#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003066
Denys Vlasenko0a238142018-12-14 16:48:34 +01003067static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003068{
3069 if (l->buf[l->i] == '=') {
3070 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003071 with_and_without >>= 8; // store "with" value
3072 } // else store "without" value
3073 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003074}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003075#define bc_lex_assign(l, with, without) \
3076 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003077
Denys Vlasenko29301232018-12-11 15:29:32 +01003078static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003079{
3080 size_t i, nls = 0;
3081 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003082
3083 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003084 i = ++l->i;
3085 for (;;) {
3086 char c = buf[i];
3087 check_star:
3088 if (c == '*') {
3089 c = buf[++i];
3090 if (c == '/')
3091 break;
3092 goto check_star;
3093 }
3094 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003095 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003096 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003097 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003098 nls += (c == '\n');
3099 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 }
3101
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003102 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003103 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003104 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003105
Denys Vlasenko29301232018-12-11 15:29:32 +01003106 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003107}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003108#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003109# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003110#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003111
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003112static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003113{
3114 BcStatus s = BC_STATUS_SUCCESS;
3115 char c = l->buf[l->i++], c2;
3116
3117 // This is the workhorse of the lexer.
3118 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003119 case '\0':
3120 case '\n':
Gavin Howard01055ba2018-11-03 11:00:21 -06003121 l->newline = true;
3122 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3123 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 case '\t':
3125 case '\v':
3126 case '\f':
3127 case '\r':
3128 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003129 bc_lex_whitespace(l);
3130 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003131 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003134 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003135 ERROR_RETURN(if (s) RETURN_STATUS(s);)
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 '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003139 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003142 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003143 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003147 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3148 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003149 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 c2 = l->buf[l->i];
3151 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003152 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003153 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003154 ++l->i;
3155 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003156 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003157 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003158 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003159 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003160 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003161 case '(':
3162 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3164 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003165 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3167 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003168 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 c2 = l->buf[l->i];
3170 if (c2 == '+') {
3171 ++l->i;
3172 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003173 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3175 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003176 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003177 l->t.t = BC_LEX_COMMA;
3178 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003179 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 c2 = l->buf[l->i];
3181 if (c2 == '-') {
3182 ++l->i;
3183 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003184 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003185 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3186 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003187 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003188 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003189 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003190 else {
3191 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003192 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003193 }
3194 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 c2 = l->buf[l->i];
3197 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003198 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 else
3200 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3201 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 case '0':
3203 case '1':
3204 case '2':
3205 case '3':
3206 case '4':
3207 case '5':
3208 case '6':
3209 case '7':
3210 case '8':
3211 case '9':
3212 case 'A':
3213 case 'B':
3214 case 'C':
3215 case 'D':
3216 case 'E':
3217 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003218 s = zbc_lex_number(l, 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 l->t.t = BC_LEX_SCOLON;
3222 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3225 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3228 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3231 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 case '[':
3233 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003234 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3235 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003236 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003237 if (l->buf[l->i] == '\n') {
3238 l->t.t = BC_LEX_WHITESPACE;
3239 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003240 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003241 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003242 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003243 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003244 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3245 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 case 'a':
3247 case 'b':
3248 case 'c':
3249 case 'd':
3250 case 'e':
3251 case 'f':
3252 case 'g':
3253 case 'h':
3254 case 'i':
3255 case 'j':
3256 case 'k':
3257 case 'l':
3258 case 'm':
3259 case 'n':
3260 case 'o':
3261 case 'p':
3262 case 'q':
3263 case 'r':
3264 case 's':
3265 case 't':
3266 case 'u':
3267 case 'v':
3268 case 'w':
3269 case 'x':
3270 case 'y':
3271 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003272 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 case '{':
3275 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3277 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003281 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003282 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003283 ++l->i;
3284 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003285 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003287 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003292 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003293 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 }
3295
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003296 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003297}
3298#endif // ENABLE_BC
3299
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003300#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003301static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003302{
Gavin Howard01055ba2018-11-03 11:00:21 -06003303 if (isspace(l->buf[l->i - 1])) {
3304 bc_lex_whitespace(l);
3305 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003306 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003307 RETURN_STATUS(bc_error("extended register"));
3308 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003309 }
3310 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003311 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003312 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003313 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003314 l->t.t = BC_LEX_NAME;
3315 }
3316
Denys Vlasenko29301232018-12-11 15:29:32 +01003317 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003318}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003319#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003320# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003321#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003322
Denys Vlasenko29301232018-12-11 15:29:32 +01003323static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003324{
3325 size_t depth = 1, nls = 0, i = l->i;
3326 char c;
3327
3328 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003329 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003330
3331 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3332
3333 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3334 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3335 nls += (c == '\n');
3336
3337 if (depth) bc_vec_push(&l->t.v, &c);
3338 }
3339
3340 if (c == '\0') {
3341 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003342 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003343 }
3344
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003345 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003346 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3347 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3348 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003349 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003350 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003351
3352 l->i = i;
3353 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003354 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003355
Denys Vlasenko29301232018-12-11 15:29:32 +01003356 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003357}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003358#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003359# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003360#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003361
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003362static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003363{
3364 BcStatus s = BC_STATUS_SUCCESS;
3365 char c = l->buf[l->i++], c2;
3366 size_t i;
3367
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003368 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3369 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003370 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003371 }
3372
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003373 if (c >= '%' && c <= '~'
3374 && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID
3375 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003376 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003377 }
3378
3379 // This is the workhorse of the lexer.
3380 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003381 case '\0':
Gavin Howard01055ba2018-11-03 11:00:21 -06003382 l->t.t = BC_LEX_EOF;
3383 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003384 case '\n':
3385 case '\t':
3386 case '\v':
3387 case '\f':
3388 case '\r':
3389 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003390 l->newline = (c == '\n');
3391 bc_lex_whitespace(l);
3392 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003393 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003394 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003395 if (c2 == '=')
3396 l->t.t = BC_LEX_OP_REL_NE;
3397 else if (c2 == '<')
3398 l->t.t = BC_LEX_OP_REL_LE;
3399 else if (c2 == '>')
3400 l->t.t = BC_LEX_OP_REL_GE;
3401 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003402 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003403 ++l->i;
3404 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003405 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003406 bc_lex_lineComment(l);
3407 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003408 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003409 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003410 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003411 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003412 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003413 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003414 case '0':
3415 case '1':
3416 case '2':
3417 case '3':
3418 case '4':
3419 case '5':
3420 case '6':
3421 case '7':
3422 case '8':
3423 case '9':
3424 case 'A':
3425 case 'B':
3426 case 'C':
3427 case 'D':
3428 case 'E':
3429 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003430 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003431 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003432 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003433 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003434 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003435 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003436 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003437 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003438 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003439 }
3440
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003441 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003442}
3443#endif // ENABLE_DC
3444
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003445static void bc_program_addFunc(char *name, size_t *idx);
3446
Gavin Howard01055ba2018-11-03 11:00:21 -06003447static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3448{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003449 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003450 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003451}
3452
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003453static void bc_parse_push(BcParse *p, char i)
3454{
3455 dbg_lex("%s:%d pushing opcode %d", __func__, __LINE__, i);
3456 bc_vec_pushByte(&p->func->code, i);
3457}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003458
Gavin Howard01055ba2018-11-03 11:00:21 -06003459static void bc_parse_pushName(BcParse *p, char *name)
3460{
3461 size_t i = 0, len = strlen(name);
3462
3463 for (; i < len; ++i) bc_parse_push(p, name[i]);
3464 bc_parse_push(p, BC_PARSE_STREND);
3465
3466 free(name);
3467}
3468
3469static void bc_parse_pushIndex(BcParse *p, size_t idx)
3470{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003471 size_t mask;
3472 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003473
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003474 dbg_lex("%s:%d pushing index %d", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003475 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3476 amt = sizeof(idx);
3477 do {
3478 if (idx & mask) break;
3479 mask >>= 8;
3480 amt--;
3481 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003482
3483 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003484
3485 while (idx != 0) {
3486 bc_parse_push(p, (unsigned char)idx);
3487 idx >>= 8;
3488 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003489}
3490
3491static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3492{
3493 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003494 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003495
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003496 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003497
3498 bc_parse_push(p, BC_INST_NUM);
3499 bc_parse_pushIndex(p, idx);
3500
3501 ++(*nexs);
3502 (*prev) = BC_INST_NUM;
3503}
3504
Denys Vlasenkoe755e302018-12-13 22:25:28 +01003505IF_BC(static BC_STATUS zbc_parse_parse(BcParse *p);)
3506IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003507
3508static BC_STATUS zcommon_parse(BcParse *p)
3509{
3510 if (IS_BC) {
3511 IF_BC(RETURN_STATUS(zbc_parse_parse(p));)
3512 }
3513 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3514}
3515
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003516static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003517{
3518 BcStatus s;
3519
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003520 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003521
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003522 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003523 p->l.t.t = BC_LEX_INVALID;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003524 s = BC_STATUS_SUCCESS;
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003525 ERROR_RETURN(s =) zcommon_parse(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003526 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003527 if (!BC_PARSE_CAN_EXEC(p))
Denys Vlasenko26819db2018-12-12 16:08:46 +01003528 RETURN_STATUS(bc_error("file is not executable"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003529 }
3530
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003531 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003532}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003533#if ERRORS_ARE_FATAL
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003534# define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003535#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003536
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003537// Called when parsing or execution detects a failure,
3538// resets execution structures.
3539static void bc_program_reset(void)
3540{
3541 BcFunc *f;
3542 BcInstPtr *ip;
3543
3544 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3545 bc_vec_pop_all(&G.prog.results);
3546
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003547 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003548 ip = bc_vec_top(&G.prog.stack);
3549 ip->idx = f->code.len;
3550}
3551
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003552#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003553 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003554
Denys Vlasenko26819db2018-12-12 16:08:46 +01003555// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003556// resets parsing structures.
3557static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003558{
3559 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003560 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003561 bc_vec_pop_all(&p->func->code);
3562 bc_vec_pop_all(&p->func->autos);
3563 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003564
3565 bc_parse_updateFunc(p, BC_PROG_MAIN);
3566 }
3567
3568 p->l.i = p->l.len;
3569 p->l.t.t = BC_LEX_EOF;
3570 p->auto_part = (p->nbraces = 0);
3571
3572 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003573 bc_vec_pop_all(&p->exits);
3574 bc_vec_pop_all(&p->conds);
3575 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003576
Denys Vlasenkod38af482018-12-04 19:11:02 +01003577 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003578}
3579
3580static void bc_parse_free(BcParse *p)
3581{
3582 bc_vec_free(&p->flags);
3583 bc_vec_free(&p->exits);
3584 bc_vec_free(&p->conds);
3585 bc_vec_free(&p->ops);
3586 bc_lex_free(&p->l);
3587}
3588
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003589static void bc_parse_create(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06003590{
3591 memset(p, 0, sizeof(BcParse));
3592
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003593 bc_lex_init(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003594 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3595 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3596 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003597 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003598 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3599
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003600 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003601 bc_parse_updateFunc(p, func);
3602}
3603
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003604#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003605
3606#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3607#define BC_PARSE_LEAF(p, rparen) \
3608 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3609 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3610
3611// We can calculate the conversion between tokens and exprs by subtracting the
3612// position of the first operator in the lex enum and adding the position of the
3613// first in the expr enum. Note: This only works for binary operators.
3614#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3615
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003616static BC_STATUS zbc_parse_else(BcParse *p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003617static BC_STATUS zbc_parse_stmt(BcParse *p);
3618static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003619static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003620#if ERRORS_ARE_FATAL
3621# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
3622# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
3623# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
3624#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003625
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003626static BC_STATUS zbc_parse_operator(BcParse *p, BcLexType type, size_t start,
Gavin Howard01055ba2018-11-03 11:00:21 -06003627 size_t *nexprs, bool next)
3628{
3629 BcStatus s = BC_STATUS_SUCCESS;
3630 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003631 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3632 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003633
3634 while (p->ops.len > start) {
3635
3636 t = BC_PARSE_TOP_OP(p);
3637 if (t == BC_LEX_LPAREN) break;
3638
Denys Vlasenko65437582018-12-05 19:37:19 +01003639 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003640 if (l >= r && (l != r || !left)) break;
3641
3642 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3643 bc_vec_pop(&p->ops);
3644 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3645 }
3646
3647 bc_vec_push(&p->ops, &type);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003648 if (next) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003649
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003650 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003651}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003652#if ERRORS_ARE_FATAL
3653# define zbc_parse_operator(...) (zbc_parse_operator(__VA_ARGS__), BC_STATUS_SUCCESS)
3654#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003655
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003656static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003657{
3658 BcLexType top;
3659
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003660 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003661 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003662 top = BC_PARSE_TOP_OP(p);
3663
3664 while (top != BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003665 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3666
3667 bc_vec_pop(&p->ops);
3668 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3669
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003670 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003671 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003672 top = BC_PARSE_TOP_OP(p);
3673 }
3674
3675 bc_vec_pop(&p->ops);
3676
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003677 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003678}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003679#if ERRORS_ARE_FATAL
3680# define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__), BC_STATUS_SUCCESS)
3681#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003682
Denys Vlasenko26819db2018-12-12 16:08:46 +01003683static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003684{
3685 BcStatus s;
3686 bool comma = false;
3687 size_t nparams;
3688
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003689 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003690 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003691 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003692
3693 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003694 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003695 s = zbc_parse_expr(p, flags, bc_parse_next_param);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003696 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003697
3698 comma = p->l.t.t == BC_LEX_COMMA;
3699 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003700 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003701 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003702 }
3703 }
3704
Denys Vlasenko26819db2018-12-12 16:08:46 +01003705 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003706 bc_parse_push(p, BC_INST_CALL);
3707 bc_parse_pushIndex(p, nparams);
3708
Denys Vlasenko26819db2018-12-12 16:08:46 +01003709 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003710}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003711#if ERRORS_ARE_FATAL
3712# define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__), BC_STATUS_SUCCESS)
3713#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003714
Denys Vlasenko26819db2018-12-12 16:08:46 +01003715static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003716{
3717 BcStatus s;
3718 BcId entry, *entry_ptr;
3719 size_t idx;
3720
3721 entry.name = name;
3722
Denys Vlasenko26819db2018-12-12 16:08:46 +01003723 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003724 if (s) goto err;
3725
3726 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003727 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003728 goto err;
3729 }
3730
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003731 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003732
3733 if (idx == BC_VEC_INVALID_IDX) {
3734 name = xstrdup(entry.name);
3735 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003736 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003737 free(entry.name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003738 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003739 free(name);
3740
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003741 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003742 bc_parse_pushIndex(p, entry_ptr->idx);
3743
Denys Vlasenko26819db2018-12-12 16:08:46 +01003744 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003745
3746err:
3747 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003748 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003749}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003750#if ERRORS_ARE_FATAL
3751# define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__), BC_STATUS_SUCCESS)
3752#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003753
Denys Vlasenko26819db2018-12-12 16:08:46 +01003754static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003755{
3756 BcStatus s;
3757 char *name;
3758
3759 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003760 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003761 if (s) goto err;
3762
3763 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003764 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003765 if (s) goto err;
3766
3767 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003768 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003769 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003770 goto err;
3771 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003772 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003773 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003774 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003775 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003776 s = zbc_parse_expr(p, flags, bc_parse_next_elem);
Gavin Howard01055ba2018-11-03 11:00:21 -06003777 if (s) goto err;
3778 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003779 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003780 if (s) goto err;
3781 bc_parse_push(p, *type);
3782 bc_parse_pushName(p, name);
3783 }
3784 else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003785 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003786 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003787 goto err;
3788 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003789 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003790 s = zbc_parse_call(p, name, flags);
3791 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003792 *type = BC_INST_VAR;
3793 bc_parse_push(p, BC_INST_VAR);
3794 bc_parse_pushName(p, name);
3795 }
3796
Denys Vlasenko26819db2018-12-12 16:08:46 +01003797 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003798
3799err:
3800 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003801 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003802}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003803#if ERRORS_ARE_FATAL
3804# define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__), BC_STATUS_SUCCESS)
3805#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003806
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003807static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003808{
3809 BcStatus s;
3810
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003811 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003812 if (s) RETURN_STATUS(s);
3813 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003814
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003815 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003816 if (s) RETURN_STATUS(s);
3817 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003818
3819 bc_parse_push(p, BC_INST_READ);
3820
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003821 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003822}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003823#if ERRORS_ARE_FATAL
3824# define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__), BC_STATUS_SUCCESS)
3825#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003826
Denys Vlasenko26819db2018-12-12 16:08:46 +01003827static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003828 BcInst *prev)
3829{
3830 BcStatus s;
3831
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003832 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003833 if (s) RETURN_STATUS(s);
3834 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003835
3836 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3837
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003838 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003839 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003840
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003841 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003842 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003843
Denys Vlasenko26819db2018-12-12 16:08:46 +01003844 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003845
3846 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3847 bc_parse_push(p, *prev);
3848
Denys Vlasenko26819db2018-12-12 16:08:46 +01003849 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003850}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003851#if ERRORS_ARE_FATAL
3852# define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
3853#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003854
Denys Vlasenko26819db2018-12-12 16:08:46 +01003855static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003856{
3857 BcStatus s;
3858
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003859 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003860 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003861
3862 if (p->l.t.t != BC_LEX_LPAREN) {
3863 *type = BC_INST_SCALE;
3864 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003865 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003866 }
3867
3868 *type = BC_INST_SCALE_FUNC;
3869 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3870
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003871 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003872 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003873
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003874 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003875 if (s) RETURN_STATUS(s);
3876 if (p->l.t.t != BC_LEX_RPAREN)
3877 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 bc_parse_push(p, BC_INST_SCALE_FUNC);
3879
Denys Vlasenko26819db2018-12-12 16:08:46 +01003880 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003881}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003882#if ERRORS_ARE_FATAL
3883# define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__), BC_STATUS_SUCCESS)
3884#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003885
Denys Vlasenko26819db2018-12-12 16:08:46 +01003886static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 size_t *nexprs, uint8_t flags)
3888{
3889 BcStatus s;
3890 BcLexType type;
3891 char inst;
3892 BcInst etype = *prev;
3893
3894 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3895 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3896 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3897 {
3898 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3899 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003900 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003901 }
3902 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003903 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3904 *paren_expr = true;
3905
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003906 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003907 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 type = p->l.t.t;
3909
3910 // Because we parse the next part of the expression
3911 // right here, we need to increment this.
3912 *nexprs = *nexprs + 1;
3913
3914 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003915 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01003916 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003917 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003918 case BC_LEX_KEY_IBASE:
3919 case BC_LEX_KEY_LAST:
3920 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06003921 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003922 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003923 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003924 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003925 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003926 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003927 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003928 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003929 else
3930 bc_parse_push(p, BC_INST_SCALE);
3931 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003932 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003933 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003934 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003935 }
3936
3937 if (!s) bc_parse_push(p, inst);
3938 }
3939
Denys Vlasenko26819db2018-12-12 16:08:46 +01003940 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003941}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003942#if ERRORS_ARE_FATAL
3943# define zbc_parse_incdec(...) (zbc_parse_incdec(__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_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06003947 bool rparen, size_t *nexprs)
3948{
3949 BcStatus s;
3950 BcLexType type;
3951 BcInst etype = *prev;
3952
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003953 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003954 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003955
3956 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3957 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3958 BC_LEX_OP_MINUS :
3959 BC_LEX_NEG;
3960 *prev = BC_PARSE_TOKEN_INST(type);
3961
3962 // We can just push onto the op stack because this is the largest
3963 // precedence operator that gets pushed. Inc/dec does not.
3964 if (type != BC_LEX_OP_MINUS)
3965 bc_vec_push(&p->ops, &type);
3966 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003967 s = zbc_parse_operator(p, type, ops_bgn, nexprs, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06003968
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003969 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003970}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003971#if ERRORS_ARE_FATAL
3972# define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__), BC_STATUS_SUCCESS)
3973#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003974
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003975static BC_STATUS zbc_parse_string(BcParse *p, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06003976{
3977 char *str = xstrdup(p->l.t.v.v);
3978
3979 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003980 bc_parse_pushIndex(p, G.prog.strs.len);
3981 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003982 bc_parse_push(p, inst);
3983
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003984 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003985}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003986#if ERRORS_ARE_FATAL
3987# define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3988#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003989
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003990static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003991{
3992 BcStatus s;
3993 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003994 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06003995
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003996 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003997 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003998
3999 type = p->l.t.t;
4000
4001 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004002 RETURN_STATUS(bc_error("bad print statement"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004003
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004004 comma = false;
4005 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004006 if (type == BC_LEX_STR) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004007 s = zbc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004008 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004009 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004010 s = zbc_parse_expr(p, 0, bc_parse_next_print);
4011 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004012 bc_parse_push(p, BC_INST_PRINT_POP);
4013 }
4014
Gavin Howard01055ba2018-11-03 11:00:21 -06004015 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004016 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004017 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004018 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004019 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004020 type = p->l.t.t;
4021 }
4022
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004023 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004024
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004025 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004026}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004027#if ERRORS_ARE_FATAL
4028# define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__), BC_STATUS_SUCCESS)
4029#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004030
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004031static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004032{
4033 BcStatus s;
4034 BcLexType t;
4035 bool paren;
4036
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004037 if (!BC_PARSE_FUNC(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004038
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004039 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004040 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004041
4042 t = p->l.t.t;
4043 paren = t == BC_LEX_LPAREN;
4044
4045 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4046 bc_parse_push(p, BC_INST_RET0);
4047 else {
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004048 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004049 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004050 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004051 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004052 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004053 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004054
4055 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004056 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004057 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06004058 }
4059
4060 bc_parse_push(p, BC_INST_RET);
4061 }
4062
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004063 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004064}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004065#if ERRORS_ARE_FATAL
4066# define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS)
4067#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004068
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004069static BC_STATUS zbc_parse_endBody(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004070{
4071 BcStatus s = BC_STATUS_SUCCESS;
4072
4073 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004074 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004075
4076 if (brace) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004077 if (p->l.t.t != BC_LEX_RBRACE)
4078 RETURN_STATUS(bc_error_bad_token());
4079 if (!p->nbraces)
4080 RETURN_STATUS(bc_error_bad_token());
4081 --p->nbraces;
4082 s = zbc_lex_next(&p->l);
4083 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004084 }
4085
4086 if (BC_PARSE_IF(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004087 uint8_t *flag_ptr;
4088
4089 while (p->l.t.t == BC_LEX_NLINE) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004090 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004091 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004092 }
4093
4094 bc_vec_pop(&p->flags);
4095
4096 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
Denys Vlasenko17df8822018-12-14 23:00:24 +01004097 dbg_lex("%s:%d setting BC_PARSE_FLAG_IF_END bit", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004098 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4099
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004100 if (p->l.t.t == BC_LEX_KEY_ELSE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004101 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004102 }
4103 else if (BC_PARSE_ELSE(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004104 BcInstPtr *ip;
4105 size_t *label;
4106
4107 bc_vec_pop(&p->flags);
4108
4109 ip = bc_vec_top(&p->exits);
4110 label = bc_vec_item(&p->func->labels, ip->idx);
4111 *label = p->func->code.len;
4112
4113 bc_vec_pop(&p->exits);
4114 }
4115 else if (BC_PARSE_FUNC_INNER(p)) {
4116 bc_parse_push(p, BC_INST_RET0);
4117 bc_parse_updateFunc(p, BC_PROG_MAIN);
4118 bc_vec_pop(&p->flags);
4119 }
4120 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004121 BcInstPtr *ip = bc_vec_top(&p->exits);
4122 size_t *label = bc_vec_top(&p->conds);
4123
4124 bc_parse_push(p, BC_INST_JUMP);
4125 bc_parse_pushIndex(p, *label);
4126
4127 label = bc_vec_item(&p->func->labels, ip->idx);
4128 *label = p->func->code.len;
4129
4130 bc_vec_pop(&p->flags);
4131 bc_vec_pop(&p->exits);
4132 bc_vec_pop(&p->conds);
4133 }
4134
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004135 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004136}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004137#if ERRORS_ARE_FATAL
4138# define zbc_parse_endBody(...) (zbc_parse_endBody(__VA_ARGS__), BC_STATUS_SUCCESS)
4139#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004140
4141static void bc_parse_startBody(BcParse *p, uint8_t flags)
4142{
4143 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4144 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4145 flags |= BC_PARSE_FLAG_BODY;
4146 bc_vec_push(&p->flags, &flags);
4147}
4148
4149static void bc_parse_noElse(BcParse *p)
4150{
4151 BcInstPtr *ip;
4152 size_t *label;
4153 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4154
Denys Vlasenko17df8822018-12-14 23:00:24 +01004155 dbg_lex("%s:%d clearing BC_PARSE_FLAG_IF_END bit", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4157
4158 ip = bc_vec_top(&p->exits);
4159 label = bc_vec_item(&p->func->labels, ip->idx);
4160 *label = p->func->code.len;
4161
4162 bc_vec_pop(&p->exits);
4163}
4164
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004165static BC_STATUS zbc_parse_if(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004166{
4167 BcStatus s;
4168 BcInstPtr ip;
4169
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004170 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004171 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004172 if (s) RETURN_STATUS(s);
4173 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004174
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004175 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004176 if (s) RETURN_STATUS(s);
4177 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4178 if (s) RETURN_STATUS(s);
4179 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004180
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004181 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004182 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004183 bc_parse_push(p, BC_INST_JUMP_ZERO);
4184
4185 ip.idx = p->func->labels.len;
4186 ip.func = ip.len = 0;
4187
4188 bc_parse_pushIndex(p, ip.idx);
4189 bc_vec_push(&p->exits, &ip);
4190 bc_vec_push(&p->func->labels, &ip.idx);
4191 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4192
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004193 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004194 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004195}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004196#if ERRORS_ARE_FATAL
4197# define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__), BC_STATUS_SUCCESS)
4198#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004199
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004200#undef zbc_parse_else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004201static BC_STATUS zbc_parse_else(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004202{
4203 BcInstPtr ip;
4204
Denys Vlasenko17df8822018-12-14 23:00:24 +01004205 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004206 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004207
4208 ip.idx = p->func->labels.len;
4209 ip.func = ip.len = 0;
4210
4211 bc_parse_push(p, BC_INST_JUMP);
4212 bc_parse_pushIndex(p, ip.idx);
4213
Denys Vlasenko17df8822018-12-14 23:00:24 +01004214 dbg_lex("%s:%d calling bc_parse_noElse()", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004215 bc_parse_noElse(p);
4216
4217 bc_vec_push(&p->exits, &ip);
4218 bc_vec_push(&p->func->labels, &ip.idx);
4219 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4220
Denys Vlasenko17df8822018-12-14 23:00:24 +01004221 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004222 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004223}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004224#if ERRORS_ARE_FATAL
4225# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
4226#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004227
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004228static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004229{
4230 BcStatus s;
4231 BcInstPtr ip;
4232
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004233 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004234 if (s) RETURN_STATUS(s);
4235 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004236 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004237 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004238
4239 ip.idx = p->func->labels.len;
4240
4241 bc_vec_push(&p->func->labels, &p->func->code.len);
4242 bc_vec_push(&p->conds, &ip.idx);
4243
4244 ip.idx = p->func->labels.len;
4245 ip.func = 1;
4246 ip.len = 0;
4247
4248 bc_vec_push(&p->exits, &ip);
4249 bc_vec_push(&p->func->labels, &ip.idx);
4250
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004251 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4252 if (s) RETURN_STATUS(s);
4253 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004254 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004255 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004256
4257 bc_parse_push(p, BC_INST_JUMP_ZERO);
4258 bc_parse_pushIndex(p, ip.idx);
4259 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4260
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004261 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004262}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004263#if ERRORS_ARE_FATAL
4264# define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__), BC_STATUS_SUCCESS)
4265#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004266
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004267static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004268{
4269 BcStatus s;
4270 BcInstPtr ip;
4271 size_t cond_idx, exit_idx, body_idx, update_idx;
4272
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004273 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004274 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004275 if (s) RETURN_STATUS(s);
4276 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004277 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004278 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004279
4280 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004281 s = zbc_parse_expr(p, 0, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004282 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004283 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004284
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004285 if (s) RETURN_STATUS(s);
4286 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004287 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004288 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004289
4290 cond_idx = p->func->labels.len;
4291 update_idx = cond_idx + 1;
4292 body_idx = update_idx + 1;
4293 exit_idx = body_idx + 1;
4294
4295 bc_vec_push(&p->func->labels, &p->func->code.len);
4296
4297 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004298 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004299 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004300 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004301
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004302 if (s) RETURN_STATUS(s);
4303 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004305 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004306 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004307
4308 bc_parse_push(p, BC_INST_JUMP_ZERO);
4309 bc_parse_pushIndex(p, exit_idx);
4310 bc_parse_push(p, BC_INST_JUMP);
4311 bc_parse_pushIndex(p, body_idx);
4312
4313 ip.idx = p->func->labels.len;
4314
4315 bc_vec_push(&p->conds, &update_idx);
4316 bc_vec_push(&p->func->labels, &p->func->code.len);
4317
4318 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004319 s = zbc_parse_expr(p, 0, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06004320 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004321 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004322
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004323 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004324
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004325 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004326 bc_parse_push(p, BC_INST_JUMP);
4327 bc_parse_pushIndex(p, cond_idx);
4328 bc_vec_push(&p->func->labels, &p->func->code.len);
4329
4330 ip.idx = exit_idx;
4331 ip.func = 1;
4332 ip.len = 0;
4333
4334 bc_vec_push(&p->exits, &ip);
4335 bc_vec_push(&p->func->labels, &ip.idx);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004336 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004337 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004338 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4339
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004340 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004341}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004342#if ERRORS_ARE_FATAL
4343# define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__), BC_STATUS_SUCCESS)
4344#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004345
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004346static BC_STATUS zbc_parse_loopExit(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004347{
4348 BcStatus s;
4349 size_t i;
4350 BcInstPtr *ip;
4351
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004352 if (!BC_PARSE_LOOP(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004353
4354 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004355 if (p->exits.len == 0) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004356
4357 i = p->exits.len - 1;
4358 ip = bc_vec_item(&p->exits, i);
4359
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004360 while (!ip->func && i < p->exits.len)
4361 ip = bc_vec_item(&p->exits, i--);
4362 if (i >= p->exits.len && !ip->func)
4363 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004364
4365 i = ip->idx;
4366 }
4367 else
4368 i = *((size_t *) bc_vec_top(&p->conds));
4369
4370 bc_parse_push(p, BC_INST_JUMP);
4371 bc_parse_pushIndex(p, i);
4372
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004373 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004374 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004375
4376 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004377 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004378
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004379 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004380}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004381#if ERRORS_ARE_FATAL
4382# define zbc_parse_loopExit(...) (zbc_parse_loopExit(__VA_ARGS__), BC_STATUS_SUCCESS)
4383#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004384
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004385static BC_STATUS zbc_parse_func(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004386{
4387 BcStatus s;
4388 bool var, comma = false;
4389 uint8_t flags;
4390 char *name;
4391
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004392 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004393 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004394 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004395 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004396
4397 name = xstrdup(p->l.t.v.v);
4398 bc_parse_addFunc(p, name, &p->fidx);
4399
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004400 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004401 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004402 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004403 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004404 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004405 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004406
4407 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004408 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004409 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004410
4411 ++p->func->nparams;
4412
4413 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004414 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004415 if (s) goto err;
4416
4417 var = p->l.t.t != BC_LEX_LBRACKET;
4418
4419 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004420 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004421 if (s) goto err;
4422
4423 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004424 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004425 goto err;
4426 }
4427
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004428 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004429 if (s) goto err;
4430 }
4431
4432 comma = p->l.t.t == BC_LEX_COMMA;
4433 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004434 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004435 if (s) goto err;
4436 }
4437
Denys Vlasenko29301232018-12-11 15:29:32 +01004438 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004439 if (s) goto err;
4440 }
4441
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004442 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004443
4444 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4445 bc_parse_startBody(p, flags);
4446
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004447 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004448 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004449
4450 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004451 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004452
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004453 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004454
4455err:
4456 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004457 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004458}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004459#if ERRORS_ARE_FATAL
4460# define zbc_parse_func(...) (zbc_parse_func(__VA_ARGS__), BC_STATUS_SUCCESS)
4461#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004462
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004463static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004464{
4465 BcStatus s;
4466 bool comma, var, one;
4467 char *name;
4468
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004469 if (!p->auto_part) RETURN_STATUS(bc_error_bad_token());
4470
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004471 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004472 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004473
4474 p->auto_part = comma = false;
4475 one = p->l.t.t == BC_LEX_NAME;
4476
4477 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004478 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004479 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004480 if (s) goto err;
4481
4482 var = p->l.t.t != BC_LEX_LBRACKET;
4483 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004484 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004485 if (s) goto err;
4486
4487 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004488 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004489 goto err;
4490 }
4491
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004492 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004493 if (s) goto err;
4494 }
4495
4496 comma = p->l.t.t == BC_LEX_COMMA;
4497 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004498 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 if (s) goto err;
4500 }
4501
Denys Vlasenko29301232018-12-11 15:29:32 +01004502 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004503 if (s) goto err;
4504 }
4505
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004506 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4507 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004508
4509 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004510 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004511
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004512 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004513
4514err:
4515 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004516 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004517}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004518#if ERRORS_ARE_FATAL
4519# define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__), BC_STATUS_SUCCESS)
4520#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004521
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004522static BC_STATUS zbc_parse_body(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004523{
4524 BcStatus s = BC_STATUS_SUCCESS;
4525 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4526
Denys Vlasenko17df8822018-12-14 23:00:24 +01004527 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004528 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4529
4530 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004531 dbg_lex("%s:%d BC_PARSE_FLAG_FUNC_INNER", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004532 if (!brace) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004533
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4535
4536 if (!p->auto_part) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004537 s = zbc_parse_auto(p);
4538 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004539 }
4540
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004541 if (p->l.t.t == BC_LEX_NLINE) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004542 }
4543 else {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004544 dbg_lex("%s:%d !BC_PARSE_FLAG_FUNC_INNER", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004545 s = zbc_parse_stmt(p);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004546 if (!s && !brace) s = zbc_parse_endBody(p, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 }
4548
Denys Vlasenko17df8822018-12-14 23:00:24 +01004549 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004550 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004551}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004552#if ERRORS_ARE_FATAL
4553# define zbc_parse_body(...) (zbc_parse_body(__VA_ARGS__), BC_STATUS_SUCCESS)
4554#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004555
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004556#undef zbc_parse_stmt
4557static BC_STATUS zbc_parse_stmt(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004558{
4559 BcStatus s = BC_STATUS_SUCCESS;
4560
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004561 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004563 case BC_LEX_NLINE:
Denys Vlasenko17df8822018-12-14 23:00:24 +01004564 dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004565 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004566
4567 case BC_LEX_KEY_ELSE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 p->auto_part = false;
4569 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004570
4571 case BC_LEX_LBRACE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004572 if (!BC_PARSE_BODY(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 ++p->nbraces;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004574 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004575 if (s) RETURN_STATUS(s);
Denys Vlasenko17df8822018-12-14 23:00:24 +01004576 dbg_lex_done("%s:%d done (returning zbc_parse_body())", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004577 RETURN_STATUS(zbc_parse_body(p, true));
Gavin Howard01055ba2018-11-03 11:00:21 -06004578
4579 case BC_LEX_KEY_AUTO:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004580 RETURN_STATUS(zbc_parse_auto(p));
Gavin Howard01055ba2018-11-03 11:00:21 -06004581
4582 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 p->auto_part = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 if (BC_PARSE_IF_END(p)) {
4585 bc_parse_noElse(p);
Denys Vlasenko17df8822018-12-14 23:00:24 +01004586 dbg_lex_done("%s:%d done (BC_PARSE_IF_END is true)", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004587 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 }
Denys Vlasenko17df8822018-12-14 23:00:24 +01004589 if (BC_PARSE_BODY(p)) {
4590 dbg_lex_done("%s:%d done (returning zbc_parse_body())", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004591 RETURN_STATUS(zbc_parse_body(p, false));
Denys Vlasenko17df8822018-12-14 23:00:24 +01004592 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004594 }
4595
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004596 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004597 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004598 case BC_LEX_OP_INC:
4599 case BC_LEX_OP_DEC:
4600 case BC_LEX_OP_MINUS:
4601 case BC_LEX_OP_BOOL_NOT:
4602 case BC_LEX_LPAREN:
4603 case BC_LEX_NAME:
4604 case BC_LEX_NUMBER:
4605 case BC_LEX_KEY_IBASE:
4606 case BC_LEX_KEY_LAST:
4607 case BC_LEX_KEY_LENGTH:
4608 case BC_LEX_KEY_OBASE:
4609 case BC_LEX_KEY_READ:
4610 case BC_LEX_KEY_SCALE:
4611 case BC_LEX_KEY_SQRT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004612 s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
Gavin Howard01055ba2018-11-03 11:00:21 -06004613 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 case BC_LEX_KEY_ELSE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004615 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 case BC_LEX_SCOLON:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004618 while (!s && p->l.t.t == BC_LEX_SCOLON) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004619 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 case BC_LEX_RBRACE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004621 s = zbc_parse_endBody(p, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004623 case BC_LEX_STR:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004624 s = zbc_parse_string(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004625 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004626 case BC_LEX_KEY_BREAK:
4627 case BC_LEX_KEY_CONTINUE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004628 s = zbc_parse_loopExit(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004629 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004630 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004631 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004632 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004633 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004634 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004635 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004636 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004637 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004638 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004639 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004640 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004641 // "limits" is a compile-time command,
4642 // the output is produced at _parse time_.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004643 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004644 if (s) RETURN_STATUS(s);
Denys Vlasenko64074a12018-12-07 15:50:14 +01004645 printf(
4646 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4647 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4648 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4649 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4650 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4651 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4652 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4653 "Number of vars = "BC_MAX_VARS_STR "\n"
4654 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004655 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004656 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004657 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004658 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004659 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004660 // "quit" is a compile-time command. For example,
4661 // "if (0 == 1) quit" terminates when parsing the statement,
4662 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004663 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004664 case BC_LEX_KEY_RETURN:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004665 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004667 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004668 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004669 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004670 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004671 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004672 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004673 }
4674
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004675 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004676 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004677}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004678#if ERRORS_ARE_FATAL
4679# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
4680#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004681
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01004682static BC_STATUS zbc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004683{
4684 BcStatus s;
4685
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004686 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004687 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004688 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 -06004689 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004690 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004691 if (!BC_PARSE_CAN_EXEC(p))
4692 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004693 s = zbc_parse_func(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004694 } else {
4695 dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004696 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004697 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004698
Denys Vlasenkod38af482018-12-04 19:11:02 +01004699 if (s || G_interrupt) {
4700 bc_parse_reset(p);
4701 s = BC_STATUS_FAILURE;
4702 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004703
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004704 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004705 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004706}
Denys Vlasenko26819db2018-12-12 16:08:46 +01004707#if ERRORS_ARE_FATAL
4708# define zbc_parse_parse(...) (zbc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
4709#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004710
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004711// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004712static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004713{
4714 BcStatus s = BC_STATUS_SUCCESS;
4715 BcInst prev = BC_INST_PRINT;
4716 BcLexType top, t = p->l.t.t;
4717 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004718 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004719 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4720
Denys Vlasenko17df8822018-12-14 23:00:24 +01004721 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004722 paren_first = p->l.t.t == BC_LEX_LPAREN;
4723 nparens = nrelops = 0;
4724 paren_expr = rprn = done = get_token = assign = false;
4725 bin_last = true;
4726
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004727 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004728 switch (t) {
4729
4730 case BC_LEX_OP_INC:
4731 case BC_LEX_OP_DEC:
4732 {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004733 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004734 rprn = get_token = bin_last = false;
4735 break;
4736 }
4737
4738 case BC_LEX_OP_MINUS:
4739 {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004740 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004741 rprn = get_token = false;
4742 bin_last = prev == BC_INST_MINUS;
4743 break;
4744 }
4745
4746 case BC_LEX_OP_ASSIGN_POWER:
4747 case BC_LEX_OP_ASSIGN_MULTIPLY:
4748 case BC_LEX_OP_ASSIGN_DIVIDE:
4749 case BC_LEX_OP_ASSIGN_MODULUS:
4750 case BC_LEX_OP_ASSIGN_PLUS:
4751 case BC_LEX_OP_ASSIGN_MINUS:
4752 case BC_LEX_OP_ASSIGN:
4753 {
4754 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4755 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4756 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4757 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004758 s = bc_error("bad assignment:"
4759 " left side must be scale,"
4760 " ibase, obase, last, var,"
4761 " or array element"
4762 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004763 break;
4764 }
4765 }
4766 // Fallthrough.
4767 case BC_LEX_OP_POWER:
4768 case BC_LEX_OP_MULTIPLY:
4769 case BC_LEX_OP_DIVIDE:
4770 case BC_LEX_OP_MODULUS:
4771 case BC_LEX_OP_PLUS:
4772 case BC_LEX_OP_REL_EQ:
4773 case BC_LEX_OP_REL_LE:
4774 case BC_LEX_OP_REL_GE:
4775 case BC_LEX_OP_REL_NE:
4776 case BC_LEX_OP_REL_LT:
4777 case BC_LEX_OP_REL_GT:
4778 case BC_LEX_OP_BOOL_NOT:
4779 case BC_LEX_OP_BOOL_OR:
4780 case BC_LEX_OP_BOOL_AND:
4781 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004782 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4783 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4784 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004785 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004786 }
4787
4788 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4789 prev = BC_PARSE_TOKEN_INST(t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004790 s = zbc_parse_operator(p, t, ops_bgn, &nexprs, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004791 rprn = get_token = false;
4792 bin_last = t != BC_LEX_OP_BOOL_NOT;
4793
4794 break;
4795 }
4796
4797 case BC_LEX_LPAREN:
4798 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004799 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004800 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004801 ++nparens;
4802 paren_expr = rprn = bin_last = false;
4803 get_token = true;
4804 bc_vec_push(&p->ops, &t);
4805
4806 break;
4807 }
4808
4809 case BC_LEX_RPAREN:
4810 {
4811 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004812 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004813
4814 if (nparens == 0) {
4815 s = BC_STATUS_SUCCESS;
4816 done = true;
4817 get_token = false;
4818 break;
4819 }
Denys Vlasenko17df8822018-12-14 23:00:24 +01004820 else if (!paren_expr) {
4821 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004822 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004823 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004824
4825 --nparens;
4826 paren_expr = rprn = true;
4827 get_token = bin_last = false;
4828
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004829 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004830
4831 break;
4832 }
4833
4834 case BC_LEX_NAME:
4835 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004836 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004837 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004838 paren_expr = true;
4839 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004840 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004841 ++nexprs;
4842
4843 break;
4844 }
4845
4846 case BC_LEX_NUMBER:
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();
Gavin Howard01055ba2018-11-03 11:00:21 -06004850 bc_parse_number(p, &prev, &nexprs);
4851 paren_expr = get_token = true;
4852 rprn = bin_last = false;
4853
4854 break;
4855 }
4856
4857 case BC_LEX_KEY_IBASE:
4858 case BC_LEX_KEY_LAST:
4859 case BC_LEX_KEY_OBASE:
4860 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004861 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004862 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004863 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4864 bc_parse_push(p, (char) prev);
4865
4866 paren_expr = get_token = true;
4867 rprn = bin_last = false;
4868 ++nexprs;
4869
4870 break;
4871 }
4872
4873 case BC_LEX_KEY_LENGTH:
4874 case BC_LEX_KEY_SQRT:
4875 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004876 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004877 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004878 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004879 paren_expr = true;
4880 rprn = get_token = bin_last = false;
4881 ++nexprs;
4882
4883 break;
4884 }
4885
4886 case BC_LEX_KEY_READ:
4887 {
4888 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004889 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004890 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004891 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004892 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004893 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004894
4895 paren_expr = true;
4896 rprn = get_token = bin_last = false;
4897 ++nexprs;
4898 prev = BC_INST_READ;
4899
4900 break;
4901 }
4902
4903 case BC_LEX_KEY_SCALE:
4904 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004905 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004906 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004907 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004908 paren_expr = true;
4909 rprn = get_token = bin_last = false;
4910 ++nexprs;
4911 prev = BC_INST_SCALE;
4912
4913 break;
4914 }
4915
4916 default:
4917 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004918 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004919 break;
4920 }
4921 }
4922
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004923 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004924 }
4925
4926 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004927 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004928
4929 while (p->ops.len > ops_bgn) {
4930
4931 top = BC_PARSE_TOP_OP(p);
4932 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4933
4934 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004935 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004936
4937 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4938
4939 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4940 bc_vec_pop(&p->ops);
4941 }
4942
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004943 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004944 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004945
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004946 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4947 for (;;) {
4948 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004949 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004950 if (next & 0x80) // last element?
4951 break;
4952 next >>= 8;
4953 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004954 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004955 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004956
4957 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004958 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004959 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004960 }
4961 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004962 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004963 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004964 }
4965
4966 if (flags & BC_PARSE_PRINT) {
4967 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4968 bc_parse_push(p, BC_INST_POP);
4969 }
4970
Denys Vlasenko17df8822018-12-14 23:00:24 +01004971 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004972 return s;
4973}
4974
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004975#undef zbc_parse_expr
4976static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004977{
4978 BcStatus s;
4979
4980 s = bc_parse_expr_empty_ok(p, flags, next);
4981 if (s == BC_STATUS_PARSE_EMPTY_EXP)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004982 RETURN_STATUS(bc_error("empty expression"));
4983 RETURN_STATUS(s);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004984}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004985#if ERRORS_ARE_FATAL
4986# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
4987#endif
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004988
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004989static BC_STATUS zbc_parse_expression(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004990{
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004991 RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read));
Gavin Howard01055ba2018-11-03 11:00:21 -06004992}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004993#if ERRORS_ARE_FATAL
4994# define zbc_parse_expression(...) (zbc_parse_expression(__VA_ARGS__), BC_STATUS_SUCCESS)
4995#endif
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004996
Gavin Howard01055ba2018-11-03 11:00:21 -06004997#endif // ENABLE_BC
4998
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004999#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005000
5001#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
5002
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005003static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005004{
5005 BcStatus s;
5006 char *name;
5007
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005008 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005009 if (s) RETURN_STATUS(s);
5010 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005011
5012 name = xstrdup(p->l.t.v.v);
5013 bc_parse_pushName(p, name);
5014
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005015 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005016}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005017#if ERRORS_ARE_FATAL
5018# define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__), BC_STATUS_SUCCESS)
5019#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005020
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005021static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005022{
5023 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005024 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005025
5026 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
5027 name = xstrdup(b);
5028
5029 str = xstrdup(p->l.t.v.v);
5030 bc_parse_push(p, BC_INST_STR);
5031 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005032 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005033 bc_parse_addFunc(p, name, &idx);
5034
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005035 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06005036}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005037#if ERRORS_ARE_FATAL
5038# define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
5039#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005040
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005041static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06005042{
5043 BcStatus s;
5044
5045 bc_parse_push(p, inst);
5046 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005047 s = zdc_parse_register(p);
5048 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005049 }
5050
5051 if (store) {
5052 bc_parse_push(p, BC_INST_SWAP);
5053 bc_parse_push(p, BC_INST_ASSIGN);
5054 bc_parse_push(p, BC_INST_POP);
5055 }
5056
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005057 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06005058}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005059#if ERRORS_ARE_FATAL
5060# define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__), BC_STATUS_SUCCESS)
5061#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005062
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005063static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005064{
5065 BcStatus s;
5066
5067 bc_parse_push(p, inst);
5068 bc_parse_push(p, BC_INST_EXEC_COND);
5069
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005070 s = zdc_parse_register(p);
5071 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005072
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005073 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005074 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005075
5076 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005077 s = zdc_parse_register(p);
5078 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005079 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005080 }
5081 else
5082 bc_parse_push(p, BC_PARSE_STREND);
5083
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005084 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005085}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005086#if ERRORS_ARE_FATAL
5087# define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__), BC_STATUS_SUCCESS)
5088#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005089
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005090static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005091{
5092 BcStatus s = BC_STATUS_SUCCESS;
5093 BcInst prev;
5094 uint8_t inst;
5095 bool assign, get_token = false;
5096
5097 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005098 case BC_LEX_OP_REL_EQ:
5099 case BC_LEX_OP_REL_LE:
5100 case BC_LEX_OP_REL_GE:
5101 case BC_LEX_OP_REL_NE:
5102 case BC_LEX_OP_REL_LT:
5103 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005104 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005105 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005106 case BC_LEX_SCOLON:
5107 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005108 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005109 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005110 case BC_LEX_STR:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005111 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06005112 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005113 case BC_LEX_NEG:
5114 case BC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06005115 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005116 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005117 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005118 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005119 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005120 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005121 bc_parse_number(p, &prev, &p->nbraces);
Gavin Howard01055ba2018-11-03 11:00:21 -06005122 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5123 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005125 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06005126 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005127 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005128 else
5129 bc_parse_push(p, BC_INST_READ);
5130 get_token = true;
5131 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005132 case BC_LEX_OP_ASSIGN:
5133 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06005134 assign = t == BC_LEX_OP_ASSIGN;
5135 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005136 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005137 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005138 case BC_LEX_LOAD:
5139 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06005140 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005141 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005142 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005143 case BC_LEX_STORE_IBASE:
5144 case BC_LEX_STORE_SCALE:
5145 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005146 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005147 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005148 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005149 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005150 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005151 get_token = true;
5152 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005153 }
5154
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005155 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005156
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005157 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005158}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005159#if ERRORS_ARE_FATAL
5160# define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__), BC_STATUS_SUCCESS)
5161#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005162
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005163static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005164{
5165 BcStatus s = BC_STATUS_SUCCESS;
5166 BcInst inst;
5167 BcLexType t;
5168
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005169 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005170
5171 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005172 inst = dc_parse_insts[t];
5173
5174 if (inst != BC_INST_INVALID) {
5175 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005176 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005177 } else
5178 s = zdc_parse_token(p, t, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06005179 }
5180
5181 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5182 bc_parse_push(p, BC_INST_POP_EXEC);
5183
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005184 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005185}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005186#if ERRORS_ARE_FATAL
5187# define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5188#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005189
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01005190static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005191{
5192 BcStatus s;
5193
5194 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005195 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005196 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005197 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005198
Denys Vlasenkod38af482018-12-04 19:11:02 +01005199 if (s || G_interrupt) {
5200 bc_parse_reset(p);
5201 s = BC_STATUS_FAILURE;
5202 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005203
Denys Vlasenko26819db2018-12-12 16:08:46 +01005204 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005205}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005206#if ERRORS_ARE_FATAL
5207# define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
5208#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005209
Gavin Howard01055ba2018-11-03 11:00:21 -06005210#endif // ENABLE_DC
5211
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005212static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005213{
5214 if (IS_BC) {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005215 IF_BC(RETURN_STATUS(zbc_parse_expression(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005216 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005217 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005218 }
5219}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005220#if ERRORS_ARE_FATAL
5221# define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5222#endif
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005223
Denys Vlasenkodf515392018-12-02 19:27:48 +01005224static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005225{
Gavin Howard01055ba2018-11-03 11:00:21 -06005226 BcId e, *ptr;
5227 BcVec *v, *map;
5228 size_t i;
5229 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005230 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005231
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005232 v = var ? &G.prog.vars : &G.prog.arrs;
5233 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005234
5235 e.name = id;
5236 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005237 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005238
5239 if (new) {
5240 bc_array_init(&data.v, var);
5241 bc_vec_push(v, &data.v);
5242 }
5243
5244 ptr = bc_vec_item(map, i);
5245 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005246 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005247}
5248
Denys Vlasenko29301232018-12-11 15:29:32 +01005249static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005250{
Gavin Howard01055ba2018-11-03 11:00:21 -06005251 switch (r->t) {
5252
5253 case BC_RESULT_STR:
5254 case BC_RESULT_TEMP:
5255 case BC_RESULT_IBASE:
5256 case BC_RESULT_SCALE:
5257 case BC_RESULT_OBASE:
5258 {
5259 *num = &r->d.n;
5260 break;
5261 }
5262
5263 case BC_RESULT_CONSTANT:
5264 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005265 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005266 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005267 size_t base_t, len = strlen(*str);
5268 BcNum *base;
5269
5270 bc_num_init(&r->d.n, len);
5271
5272 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005273 base = hex ? &G.prog.hexb : &G.prog.ib;
5274 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005275 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005276
5277 if (s) {
5278 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005279 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005280 }
5281
5282 *num = &r->d.n;
5283 r->t = BC_RESULT_TEMP;
5284
5285 break;
5286 }
5287
5288 case BC_RESULT_VAR:
5289 case BC_RESULT_ARRAY:
5290 case BC_RESULT_ARRAY_ELEM:
5291 {
5292 BcVec *v;
5293
Denys Vlasenkodf515392018-12-02 19:27:48 +01005294 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005295
5296 if (r->t == BC_RESULT_ARRAY_ELEM) {
5297 v = bc_vec_top(v);
5298 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5299 *num = bc_vec_item(v, r->d.id.idx);
5300 }
5301 else
5302 *num = bc_vec_top(v);
5303
5304 break;
5305 }
5306
5307 case BC_RESULT_LAST:
5308 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005309 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005310 break;
5311 }
5312
5313 case BC_RESULT_ONE:
5314 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005315 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005316 break;
5317 }
5318 }
5319
Denys Vlasenko29301232018-12-11 15:29:32 +01005320 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005321}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005322#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005323# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005324#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005325
Denys Vlasenko29301232018-12-11 15:29:32 +01005326static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005327 BcResult **r, BcNum **rn, bool assign)
5328{
5329 BcStatus s;
5330 bool hex;
5331 BcResultType lt, rt;
5332
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005333 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005334 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005335
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005336 *r = bc_vec_item_rev(&G.prog.results, 0);
5337 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005338
5339 lt = (*l)->t;
5340 rt = (*r)->t;
5341 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5342
Denys Vlasenko29301232018-12-11 15:29:32 +01005343 s = zbc_program_num(*l, ln, false);
5344 if (s) RETURN_STATUS(s);
5345 s = zbc_program_num(*r, rn, hex);
5346 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005347
5348 // We run this again under these conditions in case any vector has been
5349 // reallocated out from under the BcNums or arrays we had.
5350 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005351 s = zbc_program_num(*l, ln, false);
5352 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005353 }
5354
5355 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005356 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005357 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005358 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005359
Denys Vlasenko29301232018-12-11 15:29:32 +01005360 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005361}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005362#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005363# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005364#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005365
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005366static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005367{
5368 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005369 bc_vec_pop(&G.prog.results);
5370 bc_vec_pop(&G.prog.results);
5371 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005372}
5373
Denys Vlasenko29301232018-12-11 15:29:32 +01005374static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005375{
5376 BcStatus s;
5377
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005378 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005379 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005380 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005381
Denys Vlasenko29301232018-12-11 15:29:32 +01005382 s = zbc_program_num(*r, n, false);
5383 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005384
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005385 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005386 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005387
Denys Vlasenko29301232018-12-11 15:29:32 +01005388 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005389}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005390#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005391# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005392#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005393
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005394static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005395{
5396 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005397 bc_vec_pop(&G.prog.results);
5398 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005399}
5400
Denys Vlasenko259137d2018-12-11 19:42:05 +01005401static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005402{
5403 BcStatus s;
5404 BcResult *opd1, *opd2, res;
5405 BcNum *n1, *n2 = NULL;
5406
Denys Vlasenko29301232018-12-11 15:29:32 +01005407 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005408 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005409 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005410
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005411 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005412 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 -06005413 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005414 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005415
Denys Vlasenko259137d2018-12-11 19:42:05 +01005416 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005417
5418err:
5419 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005420 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005421}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005422#if ERRORS_ARE_FATAL
5423# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5424#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005425
Denys Vlasenko26819db2018-12-12 16:08:46 +01005426static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005427{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005428 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005429 BcStatus s;
5430 BcParse parse;
5431 BcVec buf;
5432 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005433 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005434
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005435 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005436 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005437
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005438 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005439 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005440
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005441 sv_file = G.prog.file;
5442 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005443 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005444
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005445 bc_char_vec_init(&buf);
Denys Vlasenko8a892472018-12-12 22:43:58 +01005446 bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005447
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005448 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005449 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005450
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005451 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005452 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005453 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005454 if (s) goto exec_err;
5455
5456 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005457 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005458 goto exec_err;
5459 }
5460
5461 ip.func = BC_PROG_READ;
5462 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005463 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005464
5465 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005466 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005467
5468 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005469 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005470
5471exec_err:
5472 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005473//io_err:
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005474 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005475 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005476 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005477 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005478}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005479#if ERRORS_ARE_FATAL
5480# define zbc_program_read(...) (zbc_program_read(__VA_ARGS__), BC_STATUS_SUCCESS)
5481#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005482
5483static size_t bc_program_index(char *code, size_t *bgn)
5484{
5485 char amt = code[(*bgn)++], i = 0;
5486 size_t res = 0;
5487
5488 for (; i < amt; ++i, ++(*bgn))
5489 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5490
5491 return res;
5492}
5493
5494static char *bc_program_name(char *code, size_t *bgn)
5495{
5496 size_t i;
5497 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5498
5499 s = xmalloc(ptr - str + 1);
5500 c = code[(*bgn)++];
5501
5502 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5503 s[i] = c;
5504
5505 s[i] = '\0';
5506
5507 return s;
5508}
5509
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005510static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005511{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005512#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005513 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005514 // Example: echo '[]ap' | dc
5515 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005516 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005517 return;
5518 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005519#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005520 while (*str) {
5521 int c = *str++;
5522 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005523 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005524 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005525 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005526 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005527 case 'a':
5528 bb_putchar('\a');
5529 break;
5530 case 'b':
5531 bb_putchar('\b');
5532 break;
5533 case '\\':
5534 case 'e':
5535 bb_putchar('\\');
5536 break;
5537 case 'f':
5538 bb_putchar('\f');
5539 break;
5540 case 'n':
5541 bb_putchar('\n');
5542 G.prog.nchars = SIZE_MAX;
5543 break;
5544 case 'r':
5545 bb_putchar('\r');
5546 break;
5547 case 'q':
5548 bb_putchar('"');
5549 break;
5550 case 't':
5551 bb_putchar('\t');
5552 break;
5553 default:
5554 // Just print the backslash and following character.
5555 bb_putchar('\\');
5556 ++G.prog.nchars;
5557 bb_putchar(c);
5558 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005559 }
5560 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005561 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005562 }
5563}
5564
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005565static void bc_num_printNewline(void)
5566{
5567 if (G.prog.nchars == G.prog.len - 1) {
5568 bb_putchar('\\');
5569 bb_putchar('\n');
5570 G.prog.nchars = 0;
5571 }
5572}
5573
5574#if ENABLE_DC
5575static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5576{
5577 (void) radix;
5578 bb_putchar((char) num);
5579 G.prog.nchars += width;
5580}
5581#endif
5582
5583static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5584{
5585 size_t exp, pow;
5586
5587 bc_num_printNewline();
5588 bb_putchar(radix ? '.' : ' ');
5589 ++G.prog.nchars;
5590
5591 bc_num_printNewline();
5592 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5593 continue;
5594
5595 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5596 size_t dig;
5597 bc_num_printNewline();
5598 dig = num / pow;
5599 num -= dig * pow;
5600 bb_putchar(((char) dig) + '0');
5601 }
5602}
5603
5604static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5605{
5606 if (radix) {
5607 bc_num_printNewline();
5608 bb_putchar('.');
5609 G.prog.nchars += 1;
5610 }
5611
5612 bc_num_printNewline();
5613 bb_putchar(bb_hexdigits_upcase[num]);
5614 G.prog.nchars += width;
5615}
5616
5617static void bc_num_printDecimal(BcNum *n)
5618{
5619 size_t i, rdx = n->rdx - 1;
5620
5621 if (n->neg) bb_putchar('-');
5622 G.prog.nchars += n->neg;
5623
5624 for (i = n->len - 1; i < n->len; --i)
5625 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5626}
5627
5628static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5629{
5630 BcStatus s;
5631 BcVec stack;
5632 BcNum intp, fracp, digit, frac_len;
5633 unsigned long dig, *ptr;
5634 size_t i;
5635 bool radix;
5636
5637 if (n->len == 0) {
5638 print(0, width, false);
5639 RETURN_STATUS(BC_STATUS_SUCCESS);
5640 }
5641
5642 bc_vec_init(&stack, sizeof(long), NULL);
5643 bc_num_init(&intp, n->len);
5644 bc_num_init(&fracp, n->rdx);
5645 bc_num_init(&digit, width);
5646 bc_num_init(&frac_len, BC_NUM_INT(n));
5647 bc_num_copy(&intp, n);
5648 bc_num_one(&frac_len);
5649
5650 bc_num_truncate(&intp, intp.rdx);
5651 s = zbc_num_sub(n, &intp, &fracp, 0);
5652 if (s) goto err;
5653
5654 while (intp.len != 0) {
5655 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5656 if (s) goto err;
5657 s = zbc_num_ulong(&digit, &dig);
5658 if (s) goto err;
5659 bc_vec_push(&stack, &dig);
5660 }
5661
5662 for (i = 0; i < stack.len; ++i) {
5663 ptr = bc_vec_item_rev(&stack, i);
5664 print(*ptr, width, false);
5665 }
5666
5667 if (!n->rdx) goto err;
5668
5669 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5670 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5671 if (s) goto err;
5672 s = zbc_num_ulong(&fracp, &dig);
5673 if (s) goto err;
5674 bc_num_ulong2num(&intp, dig);
5675 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5676 if (s) goto err;
5677 print(dig, width, radix);
5678 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5679 if (s) goto err;
5680 }
5681
5682err:
5683 bc_num_free(&frac_len);
5684 bc_num_free(&digit);
5685 bc_num_free(&fracp);
5686 bc_num_free(&intp);
5687 bc_vec_free(&stack);
5688 RETURN_STATUS(s);
5689}
5690#if ERRORS_ARE_FATAL
5691# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5692#endif
5693
5694static BC_STATUS zbc_num_printBase(BcNum *n)
5695{
5696 BcStatus s;
5697 size_t width, i;
5698 BcNumDigitOp print;
5699 bool neg = n->neg;
5700
5701 if (neg) {
5702 bb_putchar('-');
5703 G.prog.nchars++;
5704 }
5705
5706 n->neg = false;
5707
5708 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5709 width = 1;
5710 print = bc_num_printHex;
5711 }
5712 else {
5713 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5714 continue;
5715 print = bc_num_printDigits;
5716 }
5717
5718 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5719 n->neg = neg;
5720
5721 RETURN_STATUS(s);
5722}
5723#if ERRORS_ARE_FATAL
5724# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5725#endif
5726
5727#if ENABLE_DC
5728static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5729{
5730 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5731}
5732#if ERRORS_ARE_FATAL
5733# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5734#endif
5735#endif
5736
5737static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5738{
5739 BcStatus s = BC_STATUS_SUCCESS;
5740
5741 bc_num_printNewline();
5742
5743 if (n->len == 0) {
5744 bb_putchar('0');
5745 ++G.prog.nchars;
5746 }
5747 else if (G.prog.ob_t == 10)
5748 bc_num_printDecimal(n);
5749 else
5750 s = zbc_num_printBase(n);
5751
5752 if (newline) {
5753 bb_putchar('\n');
5754 G.prog.nchars = 0;
5755 }
5756
5757 RETURN_STATUS(s);
5758}
5759#if ERRORS_ARE_FATAL
5760# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5761#endif
5762
Denys Vlasenko29301232018-12-11 15:29:32 +01005763static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005764{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005765 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005766 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005767 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005768 bool pop = inst != BC_INST_PRINT;
5769
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005770 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005771 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005772
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005773 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005774 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005775 s = zbc_program_num(r, &num, false);
5776 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005777
5778 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005779 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005780 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005781 }
5782 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005783 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005784
5785 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005786 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005787
5788 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005789 for (;;) {
5790 char c = *str++;
5791 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005792 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005793 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005794 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005795 }
5796 }
5797 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005798 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005799 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005800 }
5801 }
5802
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005803 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005804
Denys Vlasenko29301232018-12-11 15:29:32 +01005805 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005806}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005807#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005808# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005809#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005810
Denys Vlasenko29301232018-12-11 15:29:32 +01005811static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005812{
5813 BcStatus s;
5814 BcResult res, *ptr;
5815 BcNum *num = NULL;
5816
Denys Vlasenko29301232018-12-11 15:29:32 +01005817 s = zbc_program_prep(&ptr, &num);
5818 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005819
5820 bc_num_init(&res.d.n, num->len);
5821 bc_num_copy(&res.d.n, num);
5822 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5823
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005824 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005825
Denys Vlasenko29301232018-12-11 15:29:32 +01005826 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005827}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005828#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005829# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005830#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005831
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005832static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005833{
5834 BcStatus s;
5835 BcResult *opd1, *opd2, res;
5836 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005837 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005838
Denys Vlasenko29301232018-12-11 15:29:32 +01005839 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005840 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005841
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005842 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005843
5844 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005845 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005846 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005847 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005849 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005850 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005851 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005852 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005853 break;
5854 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005855 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005856 break;
5857 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005858 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005859 break;
5860 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005861 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005862 break;
5863 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005864 cond = (cond > 0);
5865 break;
5866 default: // = case BC_INST_REL_NE:
5867 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005868 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005869 }
5870 }
5871
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005872 if (cond) bc_num_one(&res.d.n);
5873 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005874
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005875 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005876
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005877 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005878}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005879#if ERRORS_ARE_FATAL
5880# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5881#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005882
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005883#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005884static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005885 bool push)
5886{
5887 BcNum n2;
5888 BcResult res;
5889
5890 memset(&n2, 0, sizeof(BcNum));
5891 n2.rdx = res.d.id.idx = r->d.id.idx;
5892 res.t = BC_RESULT_STR;
5893
5894 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005895 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005896 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005897 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005898 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005899 }
5900
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005901 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005903 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005904 bc_vec_push(v, &n2);
5905
Denys Vlasenko29301232018-12-11 15:29:32 +01005906 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005907}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005908#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005909# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005910#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005911#endif // ENABLE_DC
5912
Denys Vlasenko29301232018-12-11 15:29:32 +01005913static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005914{
5915 BcStatus s;
5916 BcResult *ptr, r;
5917 BcVec *v;
5918 BcNum *n;
5919
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005920 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005921 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005922
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005923 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005924 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005925 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005926 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005927
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005928#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005929 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005930 RETURN_STATUS(bc_error_variable_is_wrong_type());
5931 if (ptr->t == BC_RESULT_STR)
5932 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005933#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005934
Denys Vlasenko29301232018-12-11 15:29:32 +01005935 s = zbc_program_num(ptr, &n, false);
5936 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005937
5938 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005939 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005940
5941 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005942 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 bc_num_copy(&r.d.n, n);
5944 }
5945 else {
5946 bc_array_init(&r.d.v, true);
5947 bc_array_copy(&r.d.v, (BcVec *) n);
5948 }
5949
5950 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005951 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005952
Denys Vlasenko29301232018-12-11 15:29:32 +01005953 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005954}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005955#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005956# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005957#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005958
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005959static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005960{
5961 BcStatus s;
5962 BcResult *left, *right, res;
5963 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5965
Denys Vlasenko29301232018-12-11 15:29:32 +01005966 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005967 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005968
5969 ib = left->t == BC_RESULT_IBASE;
5970 sc = left->t == BC_RESULT_SCALE;
5971
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005972#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005973
5974 if (right->t == BC_RESULT_STR) {
5975
5976 BcVec *v;
5977
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005978 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005979 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005980 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005982 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005983 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005984#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005985
5986 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005987 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005988 " left side must be scale,"
5989 " ibase, obase, last, var,"
5990 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005991 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06005992
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005993#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005994 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005995 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005996
5997 if (assign)
5998 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005999 else {
6000 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01006001 ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006002 }
6003 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006004#else
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006006#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
6008 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006009 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006010 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
6011 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
6012 NULL, //can't happen //BC_RESULT_LAST
6013 NULL, //can't happen //BC_RESULT_CONSTANT
6014 NULL, //can't happen //BC_RESULT_ONE
6015 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006016 };
Gavin Howard01055ba2018-11-03 11:00:21 -06006017 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01006018 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06006019
Denys Vlasenko29301232018-12-11 15:29:32 +01006020 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006021 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006022 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06006023 if (sc) {
6024 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006025 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 }
6027 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006028 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006029 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06006030 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006031 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006032 }
6033
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006034 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006035 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006036 if (!sc)
6037 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006038
6039 *ptr = (size_t) val;
6040 s = BC_STATUS_SUCCESS;
6041 }
6042
6043 bc_num_init(&res.d.n, l->len);
6044 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006045 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006046
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006047 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006048}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006049#if ERRORS_ARE_FATAL
6050# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
6051#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006052
Denys Vlasenko416ce762018-12-02 20:57:17 +01006053#if !ENABLE_DC
6054#define bc_program_pushVar(code, bgn, pop, copy) \
6055 bc_program_pushVar(code, bgn)
6056// for bc, 'pop' and 'copy' are always false
6057#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006058static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 bool pop, bool copy)
6060{
Gavin Howard01055ba2018-11-03 11:00:21 -06006061 BcResult r;
6062 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006063
6064 r.t = BC_RESULT_VAR;
6065 r.d.id.name = name;
6066
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006067#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006068 {
6069 BcVec *v = bc_program_search(name, true);
6070 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenko416ce762018-12-02 20:57:17 +01006072 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006073
Denys Vlasenko416ce762018-12-02 20:57:17 +01006074 if (!BC_PROG_STACK(v, 2 - copy)) {
6075 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006076 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006077 }
6078
Gavin Howard01055ba2018-11-03 11:00:21 -06006079 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006080 name = NULL;
6081
6082 if (!BC_PROG_STR(num)) {
6083
6084 r.t = BC_RESULT_TEMP;
6085
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006086 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006087 bc_num_copy(&r.d.n, num);
6088 }
6089 else {
6090 r.t = BC_RESULT_STR;
6091 r.d.id.idx = num->rdx;
6092 }
6093
6094 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006095 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 }
6097#endif // ENABLE_DC
6098
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006099 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006100
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006101 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006102}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006103#if ERRORS_ARE_FATAL
6104# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6105#else
6106# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6107#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006108
Denys Vlasenko29301232018-12-11 15:29:32 +01006109static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006110 char inst)
6111{
6112 BcStatus s = BC_STATUS_SUCCESS;
6113 BcResult r;
6114 BcNum *num;
6115
6116 r.d.id.name = bc_program_name(code, bgn);
6117
6118 if (inst == BC_INST_ARRAY) {
6119 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006120 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006121 }
6122 else {
6123
6124 BcResult *operand;
6125 unsigned long temp;
6126
Denys Vlasenko29301232018-12-11 15:29:32 +01006127 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006129 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006130 if (s) goto err;
6131
6132 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006133 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006134 goto err;
6135 }
6136
6137 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006138 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006139 }
6140
6141err:
6142 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006143 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006144}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006145#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006146# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006147#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006149#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006150static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006151{
6152 BcStatus s;
6153 BcResult *ptr, res, copy;
6154 BcNum *num = NULL;
6155 char inst2 = inst;
6156
Denys Vlasenko29301232018-12-11 15:29:32 +01006157 s = zbc_program_prep(&ptr, &num);
6158 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006159
6160 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6161 copy.t = BC_RESULT_TEMP;
6162 bc_num_init(&copy.d.n, num->len);
6163 bc_num_copy(&copy.d.n, num);
6164 }
6165
6166 res.t = BC_RESULT_ONE;
6167 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6168 BC_INST_ASSIGN_PLUS :
6169 BC_INST_ASSIGN_MINUS;
6170
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006171 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006172 s = zbc_program_assign(inst);
6173 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174
6175 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006176 bc_vec_pop(&G.prog.results);
6177 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006178 }
6179
Denys Vlasenko29301232018-12-11 15:29:32 +01006180 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006182#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006183# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006184#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006185
Denys Vlasenko29301232018-12-11 15:29:32 +01006186static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006187{
Gavin Howard01055ba2018-11-03 11:00:21 -06006188 BcInstPtr ip;
6189 size_t i, nparams = bc_program_index(code, idx);
6190 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 BcId *a;
6192 BcResultData param;
6193 BcResult *arg;
6194
6195 ip.idx = 0;
6196 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006197 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006198
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006199 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006200 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006201 }
6202 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006203 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006204 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006205 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006206
6207 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006208 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006209
6210 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006211 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006212
6213 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006214 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006215
Denys Vlasenko29301232018-12-11 15:29:32 +01006216 s = zbc_program_copyToVar(a->name, a->idx);
6217 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006218 }
6219
6220 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006221 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006222
6223 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006224 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006225
6226 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006227 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006228 bc_vec_push(v, &param.n);
6229 }
6230 else {
6231 bc_array_init(&param.v, true);
6232 bc_vec_push(v, &param.v);
6233 }
6234 }
6235
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006236 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006237
Denys Vlasenko29301232018-12-11 15:29:32 +01006238 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006239}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006240#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006241# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006242#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006243
Denys Vlasenko29301232018-12-11 15:29:32 +01006244static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006245{
Gavin Howard01055ba2018-11-03 11:00:21 -06006246 BcResult res;
6247 BcFunc *f;
6248 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006249 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006250
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006251 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006252 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006253
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006254 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255 res.t = BC_RESULT_TEMP;
6256
6257 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006258 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006259 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006260 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006261
Denys Vlasenko29301232018-12-11 15:29:32 +01006262 s = zbc_program_num(operand, &num, false);
6263 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006264 bc_num_init(&res.d.n, num->len);
6265 bc_num_copy(&res.d.n, num);
6266 }
6267 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006268 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006269 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006270 }
6271
6272 // We need to pop arguments as well, so this takes that into account.
6273 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006274 BcVec *v;
6275 BcId *a = bc_vec_item(&f->autos, i);
6276
Denys Vlasenkodf515392018-12-02 19:27:48 +01006277 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006278 bc_vec_pop(v);
6279 }
6280
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006281 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6282 bc_vec_push(&G.prog.results, &res);
6283 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284
Denys Vlasenko29301232018-12-11 15:29:32 +01006285 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006286}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006287#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006288# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006289#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006290#endif // ENABLE_BC
6291
6292static unsigned long bc_program_scale(BcNum *n)
6293{
6294 return (unsigned long) n->rdx;
6295}
6296
6297static unsigned long bc_program_len(BcNum *n)
6298{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006299 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006301 if (n->rdx != len) return len;
6302 for (;;) {
6303 if (len == 0) break;
6304 len--;
6305 if (n->num[len] != 0) break;
6306 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006307 return len;
6308}
6309
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006310static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006311{
6312 BcStatus s;
6313 BcResult *opnd;
6314 BcNum *num = NULL;
6315 BcResult res;
6316 bool len = inst == BC_INST_LENGTH;
6317
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006318 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006319 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006320 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006321
Denys Vlasenko29301232018-12-11 15:29:32 +01006322 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006323 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006325#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006326 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006327 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006328#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006329
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006330 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006332 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006333#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006334 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006335 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006337#endif
6338#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006339 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006340 char **str;
6341 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6342
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006343 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006344 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006345 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006346#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006347 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006348 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006349 }
6350
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006351 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006352
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006353 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006354}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006355#if ERRORS_ARE_FATAL
6356# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6357#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006358
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006359#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006360static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006361{
6362 BcStatus s;
6363 BcResult *opd1, *opd2, res, res2;
6364 BcNum *n1, *n2 = NULL;
6365
Denys Vlasenko29301232018-12-11 15:29:32 +01006366 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006367 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006368
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006369 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006370 bc_num_init(&res2.d.n, n2->len);
6371
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006372 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006373 if (s) goto err;
6374
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006375 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006376 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006377 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006378
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006379 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006380
6381err:
6382 bc_num_free(&res2.d.n);
6383 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006384 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006385}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006386#if ERRORS_ARE_FATAL
6387# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6388#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006389
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006390static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006391{
6392 BcStatus s;
6393 BcResult *r1, *r2, *r3, res;
6394 BcNum *n1, *n2, *n3;
6395
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006396 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006397 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006398 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006399 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006400
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006401 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006402 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006403 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006404 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006405 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006406
6407 // Make sure that the values have their pointers updated, if necessary.
6408 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6409
6410 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006411 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006412 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006413 }
6414
6415 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006416 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006417 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 }
6419 }
6420
6421 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006422 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006423 if (s) goto err;
6424
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006425 bc_vec_pop(&G.prog.results);
6426 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006427
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006428 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006429
6430err:
6431 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006432 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006433}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006434#if ERRORS_ARE_FATAL
6435# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6436#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006437
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006438static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006439{
Gavin Howard01055ba2018-11-03 11:00:21 -06006440 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006441 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006442
6443 res.t = BC_RESULT_TEMP;
6444
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006445 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006446 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006447 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448}
6449
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006450static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006451{
6452 BcStatus s;
6453 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006454 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006455 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006456 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006457 unsigned long val;
6458
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006459 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006460 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006461 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006462
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006463 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006464 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006465 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006466
6467 if (BC_PROG_NUM(r, num)) {
6468
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006469 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006470 bc_num_copy(&n, num);
6471 bc_num_truncate(&n, n.rdx);
6472
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006473 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006474 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006475 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006476 if (s) goto num_err;
6477
6478 c = (char) val;
6479
6480 bc_num_free(&n);
6481 }
6482 else {
6483 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006484 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006485 c = str2[0];
6486 }
6487
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006488 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006489 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006490 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006491
6492 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006493 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006494
6495 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006496 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006497 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 len = idx;
6499 break;
6500 }
6501 }
6502
6503 free(str);
6504 }
6505 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006506 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006507
6508 res.t = BC_RESULT_STR;
6509 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006510 bc_vec_pop(&G.prog.results);
6511 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006512
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006513 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006514
6515num_err:
6516 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006517 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006518}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006519#if ERRORS_ARE_FATAL
6520# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6521#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006522
Denys Vlasenko29301232018-12-11 15:29:32 +01006523static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006524{
6525 BcStatus s;
6526 BcResult *r;
6527 BcNum *n = NULL;
6528 size_t idx;
6529 char *str;
6530
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006531 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006532 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006533 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006534
Denys Vlasenko29301232018-12-11 15:29:32 +01006535 s = zbc_program_num(r, &n, false);
6536 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006537
6538 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006539 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 else {
6541 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006542 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006543 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 }
6545
Denys Vlasenko29301232018-12-11 15:29:32 +01006546 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006547}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006548#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006549# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006550#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006551
Denys Vlasenko29301232018-12-11 15:29:32 +01006552static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006553{
6554 BcStatus s;
6555 BcResult *opnd;
6556 BcNum *num = NULL;
6557 unsigned long val;
6558
Denys Vlasenko29301232018-12-11 15:29:32 +01006559 s = zbc_program_prep(&opnd, &num);
6560 if (s) RETURN_STATUS(s);
6561 s = zbc_num_ulong(num, &val);
6562 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006563
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006564 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006565
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006566 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006567 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006568 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006569 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006570 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006571
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006572 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006573
Denys Vlasenko29301232018-12-11 15:29:32 +01006574 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006575}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006576#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006577# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006578#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006579
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006580static BC_STATUS zbc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 bool cond)
6582{
6583 BcStatus s = BC_STATUS_SUCCESS;
6584 BcResult *r;
6585 char **str;
6586 BcFunc *f;
6587 BcParse prs;
6588 BcInstPtr ip;
6589 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006590
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006591 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006592 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006593
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006594 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006595
6596 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006597 BcNum *n = n; // for compiler
6598 bool exec;
6599 char *name;
6600 char *then_name = bc_program_name(code, bgn);
6601 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006602
6603 if (code[*bgn] == BC_PARSE_STREND)
6604 (*bgn) += 1;
6605 else
6606 else_name = bc_program_name(code, bgn);
6607
6608 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006609 name = then_name;
6610 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006611 exec = true;
6612 name = else_name;
6613 }
6614
6615 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006616 BcVec *v;
6617 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006618 n = bc_vec_top(v);
6619 }
6620
6621 free(then_name);
6622 free(else_name);
6623
6624 if (!exec) goto exit;
6625 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006626 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006627 goto exit;
6628 }
6629
6630 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006631 } else {
6632 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006634 } else if (r->t == BC_RESULT_VAR) {
6635 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006636 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006637 if (s || !BC_PROG_STR(n)) goto exit;
6638 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006639 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006640 goto exit;
6641 }
6642
6643 fidx = sidx + BC_PROG_REQ_FUNCS;
6644
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006645 str = bc_program_str(sidx);
6646 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006647
6648 if (f->code.len == 0) {
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006649 bc_parse_create(&prs, fidx);
Denys Vlasenkof86e9602018-12-14 17:01:56 +01006650 s = zbc_parse_text_init(&prs, *str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006651 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006652 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 if (s) goto err;
6654
6655 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006656 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006657 goto err;
6658 }
6659
6660 bc_parse_free(&prs);
6661 }
6662
6663 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006664 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006665 ip.func = fidx;
6666
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006667 bc_vec_pop(&G.prog.results);
6668 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006669
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006670 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006671
6672err:
6673 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006674 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006675 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006677 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006678 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006679}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006680#if ERRORS_ARE_FATAL
6681# define zbc_program_execStr(...) (zbc_program_execStr(__VA_ARGS__), BC_STATUS_SUCCESS)
6682#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006683#endif // ENABLE_DC
6684
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006685static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006686{
Gavin Howard01055ba2018-11-03 11:00:21 -06006687 BcResult res;
6688 unsigned long val;
6689
6690 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6691 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006692 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006693 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006694 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006695 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006696 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006697
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006698 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006699 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006700 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006701}
6702
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006703static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006704{
Gavin Howard01055ba2018-11-03 11:00:21 -06006705 BcId entry, *entry_ptr;
6706 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006707 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006708
6709 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006710 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006711
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006712 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6713 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006714
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006715 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006716 *idx = entry_ptr->idx;
6717
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006718 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006719
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006720 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006721
6722 // We need to reset these, so the function can be repopulated.
6723 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006724 bc_vec_pop_all(&func->autos);
6725 bc_vec_pop_all(&func->code);
6726 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006727 }
6728 else {
6729 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006730 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 }
6732}
6733
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006734static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006735{
Gavin Howard01055ba2018-11-03 11:00:21 -06006736 BcResult r, *ptr;
6737 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006738 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006739 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006740 char *code = func->code.v;
6741 bool cond = false;
6742
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006743 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006744 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006745 char inst = code[(ip->idx)++];
6746
6747 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006748#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006750 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006751 if (s) RETURN_STATUS(s);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006752 cond = !bc_num_cmp(num, &G.prog.zero);
6753 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006754 // Fallthrough.
6755 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006756 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006757 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006758 addr = bc_vec_item(&func->labels, idx);
6759 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6760 break;
6761 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006763 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006764 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006765 case BC_INST_INC_PRE:
6766 case BC_INST_DEC_PRE:
6767 case BC_INST_INC_POST:
6768 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006769 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006770 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006771 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006772 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006774 case BC_INST_RET:
6775 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006776 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006777 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006778 case BC_INST_BOOL_OR:
6779 case BC_INST_BOOL_AND:
6780#endif // ENABLE_BC
6781 case BC_INST_REL_EQ:
6782 case BC_INST_REL_LE:
6783 case BC_INST_REL_GE:
6784 case BC_INST_REL_NE:
6785 case BC_INST_REL_LT:
6786 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006787 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006788 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006789 case BC_INST_READ:
Denys Vlasenko26819db2018-12-12 16:08:46 +01006790 s = zbc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006791 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006792 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006793 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006794 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006795 case BC_INST_ARRAY_ELEM:
6796 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006797 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006798 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006799 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006800 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006801 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006802 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006803 case BC_INST_IBASE:
6804 case BC_INST_SCALE:
6805 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006806 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006808 case BC_INST_SCALE_FUNC:
6809 case BC_INST_LENGTH:
6810 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006811 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006812 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 r.t = BC_RESULT_CONSTANT;
6815 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006816 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006817 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006818 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006819 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006820 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006821 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006822 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006823 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006824 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006825 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006826 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006827 case BC_INST_PRINT:
6828 case BC_INST_PRINT_POP:
6829 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006830 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006831 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006833 r.t = BC_RESULT_STR;
6834 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006835 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006836 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006837 case BC_INST_POWER:
6838 case BC_INST_MULTIPLY:
6839 case BC_INST_DIVIDE:
6840 case BC_INST_MODULUS:
6841 case BC_INST_PLUS:
6842 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006843 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006845 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006846 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006847 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006848 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006849 if (!bc_num_cmp(num, &G.prog.zero))
6850 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006851 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006852 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006855 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006857#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 case BC_INST_ASSIGN_POWER:
6859 case BC_INST_ASSIGN_MULTIPLY:
6860 case BC_INST_ASSIGN_DIVIDE:
6861 case BC_INST_ASSIGN_MODULUS:
6862 case BC_INST_ASSIGN_PLUS:
6863 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006864#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006865 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006866 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006868#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006869 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006870 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006871 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006872 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006873 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006875 case BC_INST_EXECUTE:
6876 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006878 s = zbc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006879 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006880 case BC_INST_PRINT_STACK: {
6881 size_t idx;
6882 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006883 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006884 if (s) break;
6885 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006886 break;
6887 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006888 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006889 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006890 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006891 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006892 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006893 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006894 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006895 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006896 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006897 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006898 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006899 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006900 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006901 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006902 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006903 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006904 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006905 ptr = bc_vec_item_rev(&G.prog.results, 0);
6906 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006907 memcpy(&r, ptr, sizeof(BcResult));
6908 memcpy(ptr, ptr2, sizeof(BcResult));
6909 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006910 break;
6911 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006912 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006913 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006914 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006915 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006916 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006917 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006918 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006919 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006920 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006921 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006922 break;
6923 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006924 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006925 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006926 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006927 free(name);
6928 break;
6929 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006930 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006931 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006932 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006933 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006934 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006935 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006936 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006937 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006938#endif // ENABLE_DC
6939 }
6940
Denys Vlasenkod38af482018-12-04 19:11:02 +01006941 if (s || G_interrupt) {
6942 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006943 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006944 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006945
6946 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006947 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006948 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006949 code = func->code.v;
6950 }
6951
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006952 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006953}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006954#if ERRORS_ARE_FATAL
6955# define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
6956#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006957
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006958static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006959{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006960 char *lenv;
6961 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006962
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006963 lenv = getenv(var);
6964 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006965 if (!lenv) return len;
6966
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006967 len = bb_strtou(lenv, NULL, 10) - 1;
6968 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006969 len = BC_NUM_PRINT_WIDTH;
6970
6971 return len;
6972}
6973
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006974static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006975{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006976 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006977
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006978 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
6979 s = zbc_parse_text_init(&G.prs, text);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006980 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006981
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006982 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006983 dbg_lex("%s:%d G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01006984 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006985 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006986 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006987 dbg_lex("%s:%d G.prs.l.t.t:BC_LEX_EOF", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06006988
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006989 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006990 s = zbc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01006991 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01006992 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01006993 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06006994 }
6995
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006996 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006997 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006998}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006999#if ERRORS_ARE_FATAL
7000# define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__), BC_STATUS_SUCCESS)
7001#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007002
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007003static BC_STATUS zbc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007004{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007005 // So far bc/dc have no way to include a file from another file,
7006 // therefore we know G.prog.file == NULL on entry
7007 //const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007008 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007009 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007010 BcFunc *main_func;
7011 BcInstPtr *ip;
7012
Denys Vlasenkodf515392018-12-02 19:27:48 +01007013 data = bc_read_file(file);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007014 if (!data) RETURN_STATUS(bc_error_fmt("file '%s' is not text", file));
Gavin Howard01055ba2018-11-03 11:00:21 -06007015
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007016 //sv_file = G.prog.file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007017 G.prog.file = file;
7018 bc_lex_file(&G.prs.l);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007019 s = zbc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007020 if (s) goto err;
7021
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007022 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007023 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007024
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007025 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007026 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007027
7028err:
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007029 //G.prog.file = sv_file;
7030 G.prog.file = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06007031 free(data);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007032 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007033}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007034#if ERRORS_ARE_FATAL
7035# define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__), BC_STATUS_SUCCESS)
7036#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007037
Denys Vlasenko19f11072018-12-12 22:48:19 +01007038static BC_STATUS zbc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007039{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007040 BcStatus s;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007041 BcVec buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007042 size_t str;
7043 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06007044
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007045 //G.prog.file = NULL; - already is
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007046 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007047
Denys Vlasenko7d628012018-12-04 21:46:47 +01007048 bc_char_vec_init(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007049
7050 // This loop is complex because the vm tries not to send any lines that end
7051 // with a backslash to the parser. The reason for that is because the parser
7052 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7053 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko8a892472018-12-12 22:43:58 +01007054 s = BC_STATUS_SUCCESS;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007055 comment = false;
7056 str = 0;
Denys Vlasenko8a892472018-12-12 22:43:58 +01007057 for (;;) {
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007058 size_t prevlen = buffer.len;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007059 char *string;
Gavin Howard01055ba2018-11-03 11:00:21 -06007060
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007061 bc_read_line(&buffer);
7062 // No more input means EOF
7063 if (buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenko8a892472018-12-12 22:43:58 +01007064 break;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007065
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007066 string = buffer.v + prevlen;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007067 while (*string) {
7068 char c = *string;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007069 if (string == buffer.v || string[-1] != '\\') {
Denys Vlasenkobbcecc42018-12-13 21:17:43 +01007070 if (IS_BC)
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007071 str ^= (c == '"');
Denys Vlasenkobbcecc42018-12-13 21:17:43 +01007072 else {
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007073 if (c == ']')
7074 str -= 1;
7075 else if (c == '[')
7076 str += 1;
Denys Vlasenko766f6722018-12-13 17:23:24 +01007077 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007078 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007079 string++;
7080 if (c == '/' && *string == '*') {
7081 comment = true;
7082 string++;
Gavin Howard01055ba2018-11-03 11:00:21 -06007083 continue;
7084 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007085 if (c == '*' && *string == '/') {
7086 comment = false;
7087 string++;
7088 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007089 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007090 if (str || comment) {
7091 buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007092 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007093 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007094
7095 // Check for backslash+newline.
7096 // we do not check that last char is '\n' -
7097 // if it is not, then it's EOF, and looping back
7098 // to bc_read_line() will detect it:
7099 string -= 2;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007100 if (string >= buffer.v && *string == '\\') {
7101 buffer.len--;
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007102 continue;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01007103 }
Denys Vlasenkob7e61e32018-12-13 18:16:39 +01007104
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007105 s = zbc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007106 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007107 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007108 // Debug config, non-interactive mode:
7109 // return all the way back to main.
7110 // Non-debug builds do not come here, they exit.
7111 break;
7112 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007113 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007114
Denys Vlasenko7d628012018-12-04 21:46:47 +01007115 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007116 }
7117
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007118 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007119 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007120 }
7121 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007122 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007123 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007124
Gavin Howard01055ba2018-11-03 11:00:21 -06007125 bc_vec_free(&buffer);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007126 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007127}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007128#if ERRORS_ARE_FATAL
7129# define zbc_vm_stdin(...) (zbc_vm_stdin(__VA_ARGS__), BC_STATUS_SUCCESS)
7130#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007131
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007132#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01007133static void bc_vm_info(void)
7134{
7135 printf("%s "BB_VER"\n"
7136 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
7137 , applet_name);
7138}
7139
7140static void bc_args(char **argv)
7141{
7142 unsigned opts;
7143 int i;
7144
7145 GETOPT_RESET();
7146#if ENABLE_FEATURE_BC_LONG_OPTIONS
7147 opts = option_mask32 |= getopt32long(argv, "wvsqli",
7148 "warn\0" No_argument "w"
7149 "version\0" No_argument "v"
7150 "standard\0" No_argument "s"
7151 "quiet\0" No_argument "q"
7152 "mathlib\0" No_argument "l"
7153 "interactive\0" No_argument "i"
7154 );
7155#else
7156 opts = option_mask32 |= getopt32(argv, "wvsqli");
7157#endif
7158 if (getenv("POSIXLY_CORRECT"))
7159 option_mask32 |= BC_FLAG_S;
7160
7161 if (opts & BC_FLAG_V) {
7162 bc_vm_info();
7163 exit(0);
7164 }
7165
7166 for (i = optind; argv[i]; ++i)
7167 bc_vec_push(&G.files, argv + i);
7168}
7169
7170static void bc_vm_envArgs(void)
7171{
7172 BcVec v;
7173 char *buf;
7174 char *env_args = getenv("BC_ENV_ARGS");
7175
7176 if (!env_args) return;
7177
7178 G.env_args = xstrdup(env_args);
7179 buf = G.env_args;
7180
7181 bc_vec_init(&v, sizeof(char *), NULL);
7182
7183 while (*(buf = skip_whitespace(buf)) != '\0') {
7184 bc_vec_push(&v, &buf);
7185 buf = skip_non_whitespace(buf);
7186 if (!*buf)
7187 break;
7188 *buf++ = '\0';
7189 }
7190
7191 // NULL terminate, and pass argv[] so that first arg is argv[1]
7192 if (sizeof(int) == sizeof(char*)) {
7193 bc_vec_push(&v, &const_int_0);
7194 } else {
7195 static char *const nullptr = NULL;
7196 bc_vec_push(&v, &nullptr);
7197 }
7198 bc_args(((char **)v.v) - 1);
7199
7200 bc_vec_free(&v);
7201}
7202
7203static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007204 "scale=20"
7205"\n" "define e(x){"
7206"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007207////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7208//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7209//e(-.998896): GNU:.36828580434569428695
7210// above code:.36828580434569428696
7211// actual value:.3682858043456942869594...
7212// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007213"\n" "b=ibase"
7214"\n" "ibase=A"
7215"\n" "if(x<0){"
7216"\n" "n=1"
7217"\n" "x=-x"
7218"\n" "}"
7219"\n" "s=scale"
7220"\n" "r=6+s+0.44*x"
7221"\n" "scale=scale(x)+1"
7222"\n" "while(x>1){"
7223"\n" "d+=1"
7224"\n" "x/=2"
7225"\n" "scale+=1"
7226"\n" "}"
7227"\n" "scale=r"
7228"\n" "r=x+1"
7229"\n" "p=x"
7230"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007231"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007232"\n" "p*=x"
7233"\n" "f*=i"
7234"\n" "v=p/f"
7235"\n" "r+=v"
7236"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007237"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007238"\n" "scale=s"
7239"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007240"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007241"\n" "return(r/1)"
7242"\n" "}"
7243"\n" "define l(x){"
7244"\n" "auto b,s,r,p,a,q,i,v"
7245"\n" "b=ibase"
7246"\n" "ibase=A"
7247"\n" "if(x<=0){"
7248"\n" "r=(1-10^scale)/1"
7249"\n" "ibase=b"
7250"\n" "return(r)"
7251"\n" "}"
7252"\n" "s=scale"
7253"\n" "scale+=6"
7254"\n" "p=2"
7255"\n" "while(x>=2){"
7256"\n" "p*=2"
7257"\n" "x=sqrt(x)"
7258"\n" "}"
7259"\n" "while(x<=0.5){"
7260"\n" "p*=2"
7261"\n" "x=sqrt(x)"
7262"\n" "}"
7263"\n" "r=a=(x-1)/(x+1)"
7264"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007265"\n" "v=1"
7266"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007267"\n" "a*=q"
7268"\n" "v=a/i"
7269"\n" "r+=v"
7270"\n" "}"
7271"\n" "r*=p"
7272"\n" "scale=s"
7273"\n" "ibase=b"
7274"\n" "return(r/1)"
7275"\n" "}"
7276"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007277"\n" "auto b,s,r,a,q,i"
7278"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007279"\n" "b=ibase"
7280"\n" "ibase=A"
7281"\n" "s=scale"
7282"\n" "scale=1.1*s+2"
7283"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007284"\n" "scale=0"
7285"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007286"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007287"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007288"\n" "scale=s+2"
7289"\n" "r=a=x"
7290"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007291"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007292"\n" "a*=q/(i*(i-1))"
7293"\n" "r+=a"
7294"\n" "}"
7295"\n" "scale=s"
7296"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007297"\n" "return(r/1)"
7298"\n" "}"
7299"\n" "define c(x){"
7300"\n" "auto b,s"
7301"\n" "b=ibase"
7302"\n" "ibase=A"
7303"\n" "s=scale"
7304"\n" "scale*=1.2"
7305"\n" "x=s(2*a(1)+x)"
7306"\n" "scale=s"
7307"\n" "ibase=b"
7308"\n" "return(x/1)"
7309"\n" "}"
7310"\n" "define a(x){"
7311"\n" "auto b,s,r,n,a,m,t,f,i,u"
7312"\n" "b=ibase"
7313"\n" "ibase=A"
7314"\n" "n=1"
7315"\n" "if(x<0){"
7316"\n" "n=-1"
7317"\n" "x=-x"
7318"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007319"\n" "if(scale<65){"
7320"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7321"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007322"\n" "}"
7323"\n" "s=scale"
7324"\n" "if(x>.2){"
7325"\n" "scale+=5"
7326"\n" "a=a(.2)"
7327"\n" "}"
7328"\n" "scale=s+3"
7329"\n" "while(x>.2){"
7330"\n" "m+=1"
7331"\n" "x=(x-.2)/(1+.2*x)"
7332"\n" "}"
7333"\n" "r=u=x"
7334"\n" "f=-x*x"
7335"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007336"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007337"\n" "u*=f"
7338"\n" "t=u/i"
7339"\n" "r+=t"
7340"\n" "}"
7341"\n" "scale=s"
7342"\n" "ibase=b"
7343"\n" "return((m*a+r)/n)"
7344"\n" "}"
7345"\n" "define j(n,x){"
7346"\n" "auto b,s,o,a,i,v,f"
7347"\n" "b=ibase"
7348"\n" "ibase=A"
7349"\n" "s=scale"
7350"\n" "scale=0"
7351"\n" "n/=1"
7352"\n" "if(n<0){"
7353"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007354"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007355"\n" "}"
7356"\n" "a=1"
7357"\n" "for(i=2;i<=n;++i)a*=i"
7358"\n" "scale=1.5*s"
7359"\n" "a=(x^n)/2^n/a"
7360"\n" "r=v=1"
7361"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007362"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007363"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007364"\n" "v=v*f/i/(n+i)"
7365"\n" "r+=v"
7366"\n" "}"
7367"\n" "scale=s"
7368"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007369"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007370"\n" "return(a*r/1)"
7371"\n" "}"
7372};
7373#endif // ENABLE_BC
7374
Denys Vlasenko19f11072018-12-12 22:48:19 +01007375static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007376{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007377 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007378 size_t i;
7379
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007380#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007381 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007382 // We know that internal library is not buggy,
7383 // thus error checking is normally disabled.
7384# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007385 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007386 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007387 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007388 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007389#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007390
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007391 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007392 for (i = 0; !s && i < G.files.len; ++i)
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007393 s = zbc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007394 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7395 // Debug config, non-interactive mode:
7396 // return all the way back to main.
7397 // Non-debug builds do not come here, they exit.
Denys Vlasenko19f11072018-12-12 22:48:19 +01007398 RETURN_STATUS(s);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007399 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007400
Denys Vlasenko91cde952018-12-10 20:56:08 +01007401 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko19f11072018-12-12 22:48:19 +01007402 s = zbc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007403
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007404 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007405 s = zbc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007406
Denys Vlasenko19f11072018-12-12 22:48:19 +01007407 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007408}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007409#if ERRORS_ARE_FATAL
7410# define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
7411#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007412
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007413#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007414static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007415{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007416 bc_num_free(&G.prog.ib);
7417 bc_num_free(&G.prog.ob);
7418 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007419# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007420 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007421# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007422 bc_vec_free(&G.prog.fns);
7423 bc_vec_free(&G.prog.fn_map);
7424 bc_vec_free(&G.prog.vars);
7425 bc_vec_free(&G.prog.var_map);
7426 bc_vec_free(&G.prog.arrs);
7427 bc_vec_free(&G.prog.arr_map);
7428 bc_vec_free(&G.prog.strs);
7429 bc_vec_free(&G.prog.consts);
7430 bc_vec_free(&G.prog.results);
7431 bc_vec_free(&G.prog.stack);
7432 bc_num_free(&G.prog.last);
7433 bc_num_free(&G.prog.zero);
7434 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007435}
7436
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007437static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007438{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007439 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007440 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007441 bc_parse_free(&G.prs);
7442 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007443}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007444#endif
7445
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007446static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007447{
7448 size_t idx;
7449 BcInstPtr ip;
7450
Denys Vlasenko82269122018-12-14 16:30:56 +01007451 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007452 memset(&ip, 0, sizeof(BcInstPtr));
7453
Denys Vlasenko82269122018-12-14 16:30:56 +01007454 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007455 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007456 bc_num_ten(&G.prog.ib);
7457 G.prog.ib_t = 10;
7458
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007459 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007460 bc_num_ten(&G.prog.ob);
7461 G.prog.ob_t = 10;
7462
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007463 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007464 bc_num_ten(&G.prog.hexb);
7465 G.prog.hexb.num[0] = 6;
7466
7467#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007468 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007469 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7470#endif
7471
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007472 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007473 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007474
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007475 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007476 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007477
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007478 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007479 bc_num_one(&G.prog.one);
7480
7481 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007482 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007483
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007484 bc_program_addFunc(xstrdup("(main)"), &idx);
7485 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007486
7487 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007488 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007489
7490 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007491 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007492
7493 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7494 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7495 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7496 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7497 bc_vec_push(&G.prog.stack, &ip);
7498}
Gavin Howard01055ba2018-11-03 11:00:21 -06007499
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007500static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007501{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007502#if ENABLE_FEATURE_EDITING
7503 G.line_input_state = new_line_input_t(DO_HISTORY);
7504#endif
7505 G.prog.len = bc_vm_envLen(env_len);
7506
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007507 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007508 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007509 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007510 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007511
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007512//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7513//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007514 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007515#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007516 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007517 // With SA_RESTART, most system calls will restart
7518 // (IOW: they won't fail with EINTR).
7519 // In particular, this means ^C won't cause
7520 // stdout to get into "error state" if SIGINT hits
7521 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007522 //
7523 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007524 // will only be handled after [Enter] since read()
7525 // from stdin is not interrupted by ^C either,
7526 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007527 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007528 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7529
7530 // Without SA_RESTART, this exhibits a bug:
7531 // "while (1) print 1" and try ^C-ing it.
7532 // Intermittently, instead of returning to input line,
7533 // you'll get "output error: Interrupted system call"
7534 // and exit.
7535 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007536#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007537 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007538 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007539 return 0; // "not a tty"
7540}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007541
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007542static BcStatus bc_vm_run(void)
7543{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007544 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007545#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007546 if (G_exiting) // it was actually "halt" or "quit"
7547 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007548 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007549# if ENABLE_FEATURE_EDITING
7550 free_line_input_t(G.line_input_state);
7551# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007552 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007553#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007554 return st;
7555}
7556
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007557#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007558int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007559int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007560{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007561 int is_tty;
7562
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007563 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007564
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007565 is_tty = bc_vm_init("BC_LINE_LENGTH");
7566
7567 bc_args(argv);
7568
7569 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7570 bc_vm_info();
7571
7572 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007573}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007574#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007575
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007576#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007577int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007578int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007579{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007580 int noscript;
7581
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007582 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007583
7584 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7585 // 1 char wider than bc from the same package.
7586 // Both default width, and xC_LINE_LENGTH=N are wider:
7587 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007588 // |1234\ |
7589 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007590 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007591 // |123\ |
7592 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007593 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007594 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007595
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007596 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7597 noscript = BC_FLAG_I;
7598 for (;;) {
7599 int n = getopt(argc, argv, "e:f:x");
7600 if (n <= 0)
7601 break;
7602 switch (n) {
7603 case 'e':
7604 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007605 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007606 if (n) return n;
7607 break;
7608 case 'f':
7609 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007610 n = zbc_vm_file(optarg);
7611 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007612 break;
7613 case 'x':
7614 option_mask32 |= DC_FLAG_X;
7615 break;
7616 default:
7617 bb_show_usage();
7618 }
7619 }
7620 argv += optind;
7621
7622 while (*argv) {
7623 noscript = 0;
7624 bc_vec_push(&G.files, argv++);
7625 }
7626
7627 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7628
7629 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007630}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007631#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007632
7633#endif // not DC_SMALL