blob: 214ea44abbbed2b34a923a75bfa40bdada79e792 [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
Denys Vlasenko99b37622018-12-15 20:06:59 +0100171#define DEBUG_EXEC 0
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100172
173#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100174static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100175#define dbg_lex(...) \
176 do { \
177 fprintf(stderr, "%*s", lex_indent, ""); \
178 bb_error_msg(__VA_ARGS__); \
179 } while (0)
180#define dbg_lex_enter(...) \
181 do { \
182 dbg_lex(__VA_ARGS__); \
183 lex_indent++; \
184 } while (0)
185#define dbg_lex_done(...) \
186 do { \
187 lex_indent--; \
188 dbg_lex(__VA_ARGS__); \
189 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100190#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100191# define dbg_lex(...) ((void)0)
192# define dbg_lex_enter(...) ((void)0)
193# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100194#endif
195
Denys Vlasenko99b37622018-12-15 20:06:59 +0100196#if DEBUG_EXEC
197# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
198#else
199# define dbg_exec(...) ((void)0)
200#endif
201
Gavin Howard01055ba2018-11-03 11:00:21 -0600202typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100203 BC_STATUS_SUCCESS = 0,
204 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100205 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600206} BcStatus;
207
Gavin Howard01055ba2018-11-03 11:00:21 -0600208#define BC_VEC_INVALID_IDX ((size_t) -1)
209#define BC_VEC_START_CAP (1 << 5)
210
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100211typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600212
213typedef struct BcVec {
214 char *v;
215 size_t len;
216 size_t cap;
217 size_t size;
218 BcVecFree dtor;
219} BcVec;
220
Gavin Howard01055ba2018-11-03 11:00:21 -0600221typedef signed char BcDig;
222
223typedef struct BcNum {
224 BcDig *restrict num;
225 size_t rdx;
226 size_t len;
227 size_t cap;
228 bool neg;
229} BcNum;
230
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100231#define BC_NUM_MIN_BASE ((unsigned long) 2)
232#define BC_NUM_MAX_IBASE ((unsigned long) 16)
233// larger value might speed up BIGNUM calculations a bit:
234#define BC_NUM_DEF_SIZE (16)
235#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600236
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100237#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600238
Gavin Howard01055ba2018-11-03 11:00:21 -0600239typedef enum BcInst {
240
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100241#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600242 BC_INST_INC_PRE,
243 BC_INST_DEC_PRE,
244 BC_INST_INC_POST,
245 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100246#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600247
248 BC_INST_NEG,
249
250 BC_INST_POWER,
251 BC_INST_MULTIPLY,
252 BC_INST_DIVIDE,
253 BC_INST_MODULUS,
254 BC_INST_PLUS,
255 BC_INST_MINUS,
256
257 BC_INST_REL_EQ,
258 BC_INST_REL_LE,
259 BC_INST_REL_GE,
260 BC_INST_REL_NE,
261 BC_INST_REL_LT,
262 BC_INST_REL_GT,
263
264 BC_INST_BOOL_NOT,
265 BC_INST_BOOL_OR,
266 BC_INST_BOOL_AND,
267
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100268#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600269 BC_INST_ASSIGN_POWER,
270 BC_INST_ASSIGN_MULTIPLY,
271 BC_INST_ASSIGN_DIVIDE,
272 BC_INST_ASSIGN_MODULUS,
273 BC_INST_ASSIGN_PLUS,
274 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100275#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600276 BC_INST_ASSIGN,
277
278 BC_INST_NUM,
279 BC_INST_VAR,
280 BC_INST_ARRAY_ELEM,
281 BC_INST_ARRAY,
282
283 BC_INST_SCALE_FUNC,
284 BC_INST_IBASE,
285 BC_INST_SCALE,
286 BC_INST_LAST,
287 BC_INST_LENGTH,
288 BC_INST_READ,
289 BC_INST_OBASE,
290 BC_INST_SQRT,
291
292 BC_INST_PRINT,
293 BC_INST_PRINT_POP,
294 BC_INST_STR,
295 BC_INST_PRINT_STR,
296
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100297#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600298 BC_INST_JUMP,
299 BC_INST_JUMP_ZERO,
300
301 BC_INST_CALL,
302
303 BC_INST_RET,
304 BC_INST_RET0,
305
306 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100307#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600308
309 BC_INST_POP,
310 BC_INST_POP_EXEC,
311
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100312#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600313 BC_INST_MODEXP,
314 BC_INST_DIVMOD,
315
316 BC_INST_EXECUTE,
317 BC_INST_EXEC_COND,
318
319 BC_INST_ASCIIFY,
320 BC_INST_PRINT_STREAM,
321
322 BC_INST_PRINT_STACK,
323 BC_INST_CLEAR_STACK,
324 BC_INST_STACK_LEN,
325 BC_INST_DUPLICATE,
326 BC_INST_SWAP,
327
328 BC_INST_LOAD,
329 BC_INST_PUSH_VAR,
330 BC_INST_PUSH_TO_VAR,
331
332 BC_INST_QUIT,
333 BC_INST_NQUIT,
334
335 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100336#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600337
338} BcInst;
339
340typedef struct BcId {
341 char *name;
342 size_t idx;
343} BcId;
344
345typedef struct BcFunc {
346 BcVec code;
347 BcVec labels;
348 size_t nparams;
349 BcVec autos;
350} BcFunc;
351
352typedef enum BcResultType {
353
354 BC_RESULT_TEMP,
355
356 BC_RESULT_VAR,
357 BC_RESULT_ARRAY_ELEM,
358 BC_RESULT_ARRAY,
359
360 BC_RESULT_STR,
361
362 BC_RESULT_IBASE,
363 BC_RESULT_SCALE,
364 BC_RESULT_LAST,
365
366 // These are between to calculate ibase, obase, and last from instructions.
367 BC_RESULT_CONSTANT,
368 BC_RESULT_ONE,
369
370 BC_RESULT_OBASE,
371
372} BcResultType;
373
374typedef union BcResultData {
375 BcNum n;
376 BcVec v;
377 BcId id;
378} BcResultData;
379
380typedef struct BcResult {
381 BcResultType t;
382 BcResultData d;
383} BcResult;
384
385typedef struct BcInstPtr {
386 size_t func;
387 size_t idx;
388 size_t len;
389} BcInstPtr;
390
Gavin Howard01055ba2018-11-03 11:00:21 -0600391// BC_LEX_NEG is not used in lexing; it is only for parsing.
392typedef enum BcLexType {
393
394 BC_LEX_EOF,
395 BC_LEX_INVALID,
396
397 BC_LEX_OP_INC,
398 BC_LEX_OP_DEC,
399
400 BC_LEX_NEG,
401
402 BC_LEX_OP_POWER,
403 BC_LEX_OP_MULTIPLY,
404 BC_LEX_OP_DIVIDE,
405 BC_LEX_OP_MODULUS,
406 BC_LEX_OP_PLUS,
407 BC_LEX_OP_MINUS,
408
409 BC_LEX_OP_REL_EQ,
410 BC_LEX_OP_REL_LE,
411 BC_LEX_OP_REL_GE,
412 BC_LEX_OP_REL_NE,
413 BC_LEX_OP_REL_LT,
414 BC_LEX_OP_REL_GT,
415
416 BC_LEX_OP_BOOL_NOT,
417 BC_LEX_OP_BOOL_OR,
418 BC_LEX_OP_BOOL_AND,
419
420 BC_LEX_OP_ASSIGN_POWER,
421 BC_LEX_OP_ASSIGN_MULTIPLY,
422 BC_LEX_OP_ASSIGN_DIVIDE,
423 BC_LEX_OP_ASSIGN_MODULUS,
424 BC_LEX_OP_ASSIGN_PLUS,
425 BC_LEX_OP_ASSIGN_MINUS,
426 BC_LEX_OP_ASSIGN,
427
428 BC_LEX_NLINE,
429 BC_LEX_WHITESPACE,
430
431 BC_LEX_LPAREN,
432 BC_LEX_RPAREN,
433
434 BC_LEX_LBRACKET,
435 BC_LEX_COMMA,
436 BC_LEX_RBRACKET,
437
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100438 BC_LEX_LBRACE, // '{' is 0x7B, '}' is 0x7D,
Gavin Howard01055ba2018-11-03 11:00:21 -0600439 BC_LEX_SCOLON,
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100440 BC_LEX_RBRACE, // should be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600441
442 BC_LEX_STR,
443 BC_LEX_NAME,
444 BC_LEX_NUMBER,
445
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100446 BC_LEX_KEY_1st_keyword,
447 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600448 BC_LEX_KEY_BREAK,
449 BC_LEX_KEY_CONTINUE,
450 BC_LEX_KEY_DEFINE,
451 BC_LEX_KEY_ELSE,
452 BC_LEX_KEY_FOR,
453 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100454 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
455 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600456 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100457 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600458 BC_LEX_KEY_LENGTH,
459 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100460 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600461 BC_LEX_KEY_PRINT,
462 BC_LEX_KEY_QUIT,
463 BC_LEX_KEY_READ,
464 BC_LEX_KEY_RETURN,
465 BC_LEX_KEY_SCALE,
466 BC_LEX_KEY_SQRT,
467 BC_LEX_KEY_WHILE,
468
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100469#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600470 BC_LEX_EQ_NO_REG,
471 BC_LEX_OP_MODEXP,
472 BC_LEX_OP_DIVMOD,
473
474 BC_LEX_COLON,
475 BC_LEX_ELSE,
476 BC_LEX_EXECUTE,
477 BC_LEX_PRINT_STACK,
478 BC_LEX_CLEAR_STACK,
479 BC_LEX_STACK_LEVEL,
480 BC_LEX_DUPLICATE,
481 BC_LEX_SWAP,
482 BC_LEX_POP,
483
484 BC_LEX_ASCIIFY,
485 BC_LEX_PRINT_STREAM,
486
487 BC_LEX_STORE_IBASE,
488 BC_LEX_STORE_SCALE,
489 BC_LEX_LOAD,
490 BC_LEX_LOAD_POP,
491 BC_LEX_STORE_PUSH,
492 BC_LEX_STORE_OBASE,
493 BC_LEX_PRINT_POP,
494 BC_LEX_NQUIT,
495 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100496#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600497} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100498// must match order of BC_LEX_KEY_foo etc above
499#if ENABLE_BC
500struct BcLexKeyword {
501 char name8[8];
502};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100503#define BC_LEX_KW_ENTRY(a, b) \
504 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100505static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100506 BC_LEX_KW_ENTRY("auto" , 1), // 0
507 BC_LEX_KW_ENTRY("break" , 1), // 1
508 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
509 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100510 BC_LEX_KW_ENTRY("else" , 0), // 4
511 BC_LEX_KW_ENTRY("for" , 1), // 5
512 BC_LEX_KW_ENTRY("halt" , 0), // 6
513 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100514 BC_LEX_KW_ENTRY("if" , 1), // 8
515 BC_LEX_KW_ENTRY("last" , 0), // 9
516 BC_LEX_KW_ENTRY("length" , 1), // 10
517 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100518 BC_LEX_KW_ENTRY("obase" , 1), // 12
519 BC_LEX_KW_ENTRY("print" , 0), // 13
520 BC_LEX_KW_ENTRY("quit" , 1), // 14
521 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100522 BC_LEX_KW_ENTRY("return" , 1), // 16
523 BC_LEX_KW_ENTRY("scale" , 1), // 17
524 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
525 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100526};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100528#define STRING_if (bc_lex_kws[8].name8)
529#define STRING_else (bc_lex_kws[4].name8)
530#define STRING_while (bc_lex_kws[19].name8)
531#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100532enum {
533 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100534 | (1 << 0) // 0
535 | (1 << 1) // 1
536 | (0 << 2) // 2
537 | (1 << 3) // 3
538 | (0 << 4) // 4
539 | (1 << 5) // 5
540 | (0 << 6) // 6
541 | (1 << 7) // 7
542 | (1 << 8) // 8
543 | (0 << 9) // 9
544 | (1 << 10) // 10
545 | (0 << 11) // 11
546 | (1 << 12) // 12
547 | (0 << 13) // 13
548 | (1 << 14) // 14
549 | (0 << 15) // 15
550 | (1 << 16) // 16
551 | (1 << 17) // 17
552 | (1 << 18) // 18
553 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100554};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100555#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100556#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600557
Gavin Howard01055ba2018-11-03 11:00:21 -0600558typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600559 const char *buf;
560 size_t i;
561 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600562 size_t len;
563 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600564 struct {
565 BcLexType t;
566 BcLexType last;
567 BcVec v;
568 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600569} BcLex;
570
Denys Vlasenko0154d782018-12-14 23:32:51 +0100571#define BC_PARSE_STREND ((char) UCHAR_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -0600572
Denys Vlasenko0154d782018-12-14 23:32:51 +0100573#define BC_PARSE_REL (1 << 0)
574#define BC_PARSE_PRINT (1 << 1)
575#define BC_PARSE_NOCALL (1 << 2)
576#define BC_PARSE_NOREAD (1 << 3)
577#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600578
Gavin Howard01055ba2018-11-03 11:00:21 -0600579typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600580 BcLex l;
581
Gavin Howard01055ba2018-11-03 11:00:21 -0600582 BcVec exits;
583 BcVec conds;
584
585 BcVec ops;
586
Gavin Howard01055ba2018-11-03 11:00:21 -0600587 BcFunc *func;
588 size_t fidx;
589
Denys Vlasenkoe9519e42018-12-16 17:06:07 +0100590 size_t in_funcdef;
Gavin Howard01055ba2018-11-03 11:00:21 -0600591} BcParse;
592
Gavin Howard01055ba2018-11-03 11:00:21 -0600593typedef struct BcProgram {
594
595 size_t len;
596 size_t scale;
597
598 BcNum ib;
599 size_t ib_t;
600 BcNum ob;
601 size_t ob_t;
602
603 BcNum hexb;
604
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100605#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600606 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100607#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600608
609 BcVec results;
610 BcVec stack;
611
612 BcVec fns;
613 BcVec fn_map;
614
615 BcVec vars;
616 BcVec var_map;
617
618 BcVec arrs;
619 BcVec arr_map;
620
621 BcVec strs;
622 BcVec consts;
623
624 const char *file;
625
626 BcNum last;
627 BcNum zero;
628 BcNum one;
629
630 size_t nchars;
631
Gavin Howard01055ba2018-11-03 11:00:21 -0600632} BcProgram;
633
634#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
635
636#define BC_PROG_MAIN (0)
637#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100638#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600639#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100640#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600641
642#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
643#define BC_PROG_NUM(r, n) \
644 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
645
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100646#define BC_FLAG_W (1 << 0)
647#define BC_FLAG_V (1 << 1)
648#define BC_FLAG_S (1 << 2)
649#define BC_FLAG_Q (1 << 3)
650#define BC_FLAG_L (1 << 4)
651#define BC_FLAG_I (1 << 5)
652#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600653
654#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
655#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
656
Denys Vlasenko64074a12018-12-07 15:50:14 +0100657#define BC_MAX_OBASE ((unsigned) 999)
658#define BC_MAX_DIM ((unsigned) INT_MAX)
659#define BC_MAX_SCALE ((unsigned) UINT_MAX)
660#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
661#define BC_MAX_NUM BC_MAX_STRING
662// Unused apart from "limits" message. Just show a "biggish number" there.
663//#define BC_MAX_NAME BC_MAX_STRING
664//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
665//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
666#define BC_MAX_NAME_STR "999999999"
667#define BC_MAX_EXP_STR "999999999"
668#define BC_MAX_VARS_STR "999999999"
669
670#define BC_MAX_OBASE_STR "999"
671
672#if INT_MAX == 2147483647
673# define BC_MAX_DIM_STR "2147483647"
674#elif INT_MAX == 9223372036854775807
675# define BC_MAX_DIM_STR "9223372036854775807"
676#else
677# error Strange INT_MAX
678#endif
679
680#if UINT_MAX == 4294967295
681# define BC_MAX_SCALE_STR "4294967295"
682# define BC_MAX_STRING_STR "4294967294"
683#elif UINT_MAX == 18446744073709551615
684# define BC_MAX_SCALE_STR "18446744073709551615"
685# define BC_MAX_STRING_STR "18446744073709551614"
686#else
687# error Strange UINT_MAX
688#endif
689#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600690
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100691struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100692 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100693 IF_FEATURE_CLEAN_UP(smallint exiting;)
Denys Vlasenko69171dc2018-12-12 00:29:24 +0100694 smallint in_read;
Gavin Howard01055ba2018-11-03 11:00:21 -0600695
696 BcParse prs;
697 BcProgram prog;
698
Denys Vlasenko5318f812018-12-05 17:48:01 +0100699 // For error messages. Can be set to current parsed line,
700 // or [TODO] to current executing line (can be before last parsed one)
701 unsigned err_line;
702
Gavin Howard01055ba2018-11-03 11:00:21 -0600703 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100704 BcVec input_buffer;
705 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600706
707 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100708
709#if ENABLE_FEATURE_EDITING
710 line_input_t *line_input_state;
711#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100712} FIX_ALIASING;
713#define G (*ptr_to_globals)
714#define INIT_G() do { \
715 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
716} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100717#define FREE_G() do { \
718 FREE_PTR_TO_GLOBALS(); \
719} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100720#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
721#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100722#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100723#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100724# define G_interrupt bb_got_signal
725# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100726#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100727# define G_interrupt 0
728# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100729#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100730#if ENABLE_FEATURE_CLEAN_UP
731# define G_exiting G.exiting
732#else
733# define G_exiting 0
734#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100735#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100736#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100737
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100738#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600739
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100740// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600741// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100742enum {
743 BC_PARSE_EXPRS_BITS = 0
744 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
745 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
746 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
747 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
748 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
749 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
750 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
751 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600752};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100753static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
754{
755#if ULONG_MAX > 0xffffffff
756 // 64-bit version (will not work correctly for 32-bit longs!)
757 return BC_PARSE_EXPRS_BITS & (1UL << i);
758#else
759 // 32-bit version
760 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
761 if (i >= 32) {
762 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
763 i &= 31;
764 }
765 return m & (1UL << i);
766#endif
767}
Gavin Howard01055ba2018-11-03 11:00:21 -0600768
769// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100770static const uint8_t bc_parse_ops[] = {
771#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100772 OP(0, false), OP( 0, false ), // inc dec
773 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100774 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100775 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
776 OP(4, true ), OP( 4, true ), // mod + -
777 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
778 OP(1, false), // not
779 OP(7, true ), OP( 7, true ), // or and
780 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
781 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100782#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600783};
Denys Vlasenko65437582018-12-05 19:37:19 +0100784#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
785#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600786
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100787// Byte array of up to 4 BC_LEX's, packed into 32-bit word
788typedef uint32_t BcParseNext;
789
Gavin Howard01055ba2018-11-03 11:00:21 -0600790// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100791enum {
792#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
793#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
794#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
795 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
796 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
797 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
798 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
799 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
800 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
801 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
802#undef BC_PARSE_NEXT4
803#undef BC_PARSE_NEXT2
804#undef BC_PARSE_NEXT1
805};
Gavin Howard01055ba2018-11-03 11:00:21 -0600806#endif // ENABLE_BC
807
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100808#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100809static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
810int8_t
811dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600812 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
813 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
814 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
815 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
816 BC_INST_INVALID, BC_INST_INVALID,
817 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
818 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
819 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
820 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
821 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
822 BC_INST_INVALID, BC_INST_INVALID,
823 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
824 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
825 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
826 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
827 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
828 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
829 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
830 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
831 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
832 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
833 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
834 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
835};
836#endif // ENABLE_DC
837
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100838// In configurations where errors abort instead of propagating error
839// return code up the call chain, functions returning BC_STATUS
840// actually don't return anything, they always succeed and return "void".
841// A macro wrapper is provided, which makes this statement work:
842// s = zbc_func(...)
843// and makes it visible to the compiler that s is always zero,
844// allowing compiler to optimize dead code after the statement.
845//
846// To make code more readable, each such function has a "z"
847// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
848//
849#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100850# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100851# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100852# define IF_ERROR_RETURN_POSSIBLE(a) a
853# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100854# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100855# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100856#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100857# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100858# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100859# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
860# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100861# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100862# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100863#endif
864
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100865#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
866#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
867#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
868//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
869static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
870{
871 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
872}
873//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
874static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
875{
876 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
877}
878
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100879typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
880
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100881typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100882
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100883static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
884 BcNumBinaryOp op, size_t req);
885static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
886static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
887static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
888static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
889static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
890static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
891
892static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
893{
894 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
895 (void) scale;
896 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
897}
898
899static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
900{
901 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
902 (void) scale;
903 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
904}
905
906static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
907{
908 size_t req = BC_NUM_MREQ(a, b, scale);
909 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
910}
911
912static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
913{
914 size_t req = BC_NUM_MREQ(a, b, scale);
915 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
916}
917
918static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
919{
920 size_t req = BC_NUM_MREQ(a, b, scale);
921 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
922}
923
924static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
925{
926 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
927}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100928
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100929static const BcNumBinaryOp zbc_program_ops[] = {
930 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 -0600931};
Denys Vlasenkoec603182018-12-17 10:34:02 +0100932#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
933#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
934#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
935#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
936#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
937#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -0600938
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100939static void fflush_and_check(void)
940{
941 fflush_all();
942 if (ferror(stdout) || ferror(stderr))
943 bb_perror_msg_and_die("output error");
944}
945
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100946#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100947#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100948do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100949 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100950 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100951 return BC_STATUS_FAILURE; \
952} while (0)
953#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100954#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100955#endif
956
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100957static void quit(void) NORETURN;
958static void quit(void)
959{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100960 if (ferror(stdin))
961 bb_perror_msg_and_die("input error");
962 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100963 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100964 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100965}
966
Denys Vlasenko5318f812018-12-05 17:48:01 +0100967static void bc_verror_msg(const char *fmt, va_list p)
968{
Denys Vlasenko82269122018-12-14 16:30:56 +0100969 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100970 if (G.prog.file) {
971 sv = applet_name;
972 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
973 }
974 bb_verror_msg(fmt, p, NULL);
975 if (G.prog.file) {
976 free((char*)applet_name);
977 applet_name = sv;
978 }
979}
980
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100981static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100982{
983 va_list p;
984
985 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100986 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100987 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100988
Denys Vlasenkoec603182018-12-17 10:34:02 +0100989 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
990 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
991 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100992}
993
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100994#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100995static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100996{
997 va_list p;
998
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100999 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001000 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001001 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001002
1003 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001004 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001005 va_end(p);
1006
1007 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001008 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001009 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenkoec603182018-12-17 10:34:02 +01001010 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
1011 return BC_STATUS_FAILURE;
1012 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001013}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001014#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001015
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001016// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1017// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001018// function must not have caller-cleaned parameters on stack.
1019// Unfortunately, vararg function API does exactly that on most arches.
1020// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001021static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001022{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001023 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001024}
1025static ERRORFUNC int bc_error_bad_character(char c)
1026{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001027 if (!c)
1028 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001029 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001030}
1031static ERRORFUNC int bc_error_bad_expression(void)
1032{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001033 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001034}
1035static ERRORFUNC int bc_error_bad_token(void)
1036{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001037 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001038}
1039static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1040{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001041 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001042}
1043static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1044{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001045 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001046}
1047static ERRORFUNC int bc_error_nested_read_call(void)
1048{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001049 IF_ERROR_RETURN_POSSIBLE(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001050}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001051#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001052static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001053{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001054 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001055}
Denys Vlasenko00646792018-12-05 18:12:27 +01001056static int bc_POSIX_does_not_allow(const char *msg)
1057{
1058 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1059}
1060static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1061{
1062 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1063}
1064static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1065{
1066 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1067}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001068#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001069
Gavin Howard01055ba2018-11-03 11:00:21 -06001070static void bc_vec_grow(BcVec *v, size_t n)
1071{
1072 size_t cap = v->cap * 2;
1073 while (cap < v->len + n) cap *= 2;
1074 v->v = xrealloc(v->v, v->size * cap);
1075 v->cap = cap;
1076}
1077
1078static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1079{
1080 v->size = esize;
1081 v->cap = BC_VEC_START_CAP;
1082 v->len = 0;
1083 v->dtor = dtor;
1084 v->v = xmalloc(esize * BC_VEC_START_CAP);
1085}
1086
Denys Vlasenko7d628012018-12-04 21:46:47 +01001087static void bc_char_vec_init(BcVec *v)
1088{
1089 bc_vec_init(v, sizeof(char), NULL);
1090}
1091
Gavin Howard01055ba2018-11-03 11:00:21 -06001092static void bc_vec_expand(BcVec *v, size_t req)
1093{
1094 if (v->cap < req) {
1095 v->v = xrealloc(v->v, v->size * req);
1096 v->cap = req;
1097 }
1098}
1099
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001100static void bc_vec_pop(BcVec *v)
1101{
1102 v->len--;
1103 if (v->dtor)
1104 v->dtor(v->v + (v->size * v->len));
1105}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001106
Gavin Howard01055ba2018-11-03 11:00:21 -06001107static void bc_vec_npop(BcVec *v, size_t n)
1108{
1109 if (!v->dtor)
1110 v->len -= n;
1111 else {
1112 size_t len = v->len - n;
1113 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1114 }
1115}
1116
Denys Vlasenko7d628012018-12-04 21:46:47 +01001117static void bc_vec_pop_all(BcVec *v)
1118{
1119 bc_vec_npop(v, v->len);
1120}
1121
Gavin Howard01055ba2018-11-03 11:00:21 -06001122static void bc_vec_push(BcVec *v, const void *data)
1123{
1124 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1125 memmove(v->v + (v->size * v->len), data, v->size);
1126 v->len += 1;
1127}
1128
1129static void bc_vec_pushByte(BcVec *v, char data)
1130{
1131 bc_vec_push(v, &data);
1132}
1133
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001134static void bc_vec_pushZeroByte(BcVec *v)
1135{
1136 //bc_vec_pushByte(v, '\0');
1137 // better:
1138 bc_vec_push(v, &const_int_0);
1139}
1140
Gavin Howard01055ba2018-11-03 11:00:21 -06001141static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1142{
1143 if (idx == v->len)
1144 bc_vec_push(v, data);
1145 else {
1146
1147 char *ptr;
1148
1149 if (v->len == v->cap) bc_vec_grow(v, 1);
1150
1151 ptr = v->v + v->size * idx;
1152
1153 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1154 memmove(ptr, data, v->size);
1155 }
1156}
1157
1158static void bc_vec_string(BcVec *v, size_t len, const char *str)
1159{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001160 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001161 bc_vec_expand(v, len + 1);
1162 memcpy(v->v, str, len);
1163 v->len = len;
1164
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001165 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001166}
1167
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001168#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001169static void bc_vec_concat(BcVec *v, const char *str)
1170{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001171 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001172
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001173 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001174
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001175 slen = strlen(str);
1176 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001177
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001178 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001179 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001180
1181 v->len = len;
1182}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001183#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001184
1185static void *bc_vec_item(const BcVec *v, size_t idx)
1186{
1187 return v->v + v->size * idx;
1188}
1189
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001190static char** bc_program_str(size_t idx)
1191{
1192 return bc_vec_item(&G.prog.strs, idx);
1193}
1194
1195static BcFunc* bc_program_func(size_t idx)
1196{
1197 return bc_vec_item(&G.prog.fns, idx);
1198}
1199
Gavin Howard01055ba2018-11-03 11:00:21 -06001200static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1201{
1202 return v->v + v->size * (v->len - idx - 1);
1203}
1204
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001205static void *bc_vec_top(const BcVec *v)
1206{
1207 return v->v + v->size * (v->len - 1);
1208}
1209
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001210static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001211{
1212 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001213 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001214 free(v->v);
1215}
1216
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001217static int bc_id_cmp(const void *e1, const void *e2)
1218{
1219 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1220}
1221
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001222static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001223{
1224 free(((BcId *) id)->name);
1225}
1226
Gavin Howard01055ba2018-11-03 11:00:21 -06001227static size_t bc_map_find(const BcVec *v, const void *ptr)
1228{
1229 size_t low = 0, high = v->len;
1230
1231 while (low < high) {
1232
1233 size_t mid = (low + high) / 2;
1234 BcId *id = bc_vec_item(v, mid);
1235 int result = bc_id_cmp(ptr, id);
1236
1237 if (result == 0)
1238 return mid;
1239 else if (result < 0)
1240 high = mid;
1241 else
1242 low = mid + 1;
1243 }
1244
1245 return low;
1246}
1247
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001248static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001249{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001250 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001251
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001252 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001253 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001254 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1255 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001256 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001257 bc_vec_pushAt(v, ptr, n);
1258 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001259}
1260
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001261#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001262static size_t bc_map_index(const BcVec *v, const void *ptr)
1263{
1264 size_t i = bc_map_find(v, ptr);
1265 if (i >= v->len) return BC_VEC_INVALID_IDX;
1266 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1267}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001268#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001269
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001270static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001271{
1272 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1273 || c > 0x7e
1274 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001275 bc_error_fmt("illegal character 0x%02x", c);
1276 return 1;
1277 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001278 return 0;
1279}
1280
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001281// Note: it _appends_ data from fp to vec.
1282static void bc_read_line(BcVec *vec, FILE *fp)
Gavin Howard01055ba2018-11-03 11:00:21 -06001283{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001284 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001285 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001286
Gavin Howard01055ba2018-11-03 11:00:21 -06001287#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001288 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001289 intr:
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001290 if (fp != stdin) {
1291 // ^C while running a script (bc SCRIPT): die.
1292 // We do not return to interactive prompt:
1293 // user might be running us from a shell,
1294 // and SCRIPT might be intended to terminate
1295 // (e.g. contain a "halt" stmt).
1296 // ^C dropping user into a bc prompt instead of
1297 // the shell would be unexpected.
1298 xfunc_die();
1299 }
1300 // ^C while interactive input
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001301 G_interrupt = 0;
1302 // GNU bc says "interrupted execution."
1303 // GNU dc says "Interrupt!"
1304 fputs("\ninterrupted execution\n", stderr);
1305 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001306
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001307# if ENABLE_FEATURE_EDITING
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001308 if (G_ttyin && fp == stdin) {
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001309 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001310# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001311 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1312 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1313 if (n == 0) // ^C
1314 goto intr;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001315 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01001316 return;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001317 }
1318 i = 0;
1319 for (;;) {
1320 char c = line_buf[i++];
1321 if (!c) break;
1322 if (bad_input_byte(c)) goto again;
1323 }
1324 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001325# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001326 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001327# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001328#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001329 {
1330 int c;
1331 bool bad_chars = 0;
1332 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001333
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001334 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001335#if ENABLE_FEATURE_BC_SIGNALS
1336 if (G_interrupt) {
1337 // ^C was pressed: ignore entire line, get another one
1338 vec->len = len;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001339 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001340 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001341#endif
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001342 do c = fgetc(fp); while (c == '\0');
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001343 if (c == EOF) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001344 if (ferror(fp))
1345 bb_perror_msg_and_die("input error");
1346 // Note: EOF does not append '\n'
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001347 break;
1348 }
1349 bad_chars |= bad_input_byte(c);
1350 bc_vec_pushByte(vec, (char)c);
1351 } while (c != '\n');
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001352
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001353 if (bad_chars) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001354 // Bad chars on this line
1355 if (!G.prog.file) { // stdin
1356 // ignore entire line, get another one
1357 vec->len = len;
1358 goto again;
1359 }
1360 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
Denys Vlasenkoed849352018-12-06 10:26:13 +01001361 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001362 bc_vec_pushZeroByte(vec);
1363 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001364}
1365
Gavin Howard01055ba2018-11-03 11:00:21 -06001366static void bc_num_setToZero(BcNum *n, size_t scale)
1367{
1368 n->len = 0;
1369 n->neg = false;
1370 n->rdx = scale;
1371}
1372
1373static void bc_num_zero(BcNum *n)
1374{
1375 bc_num_setToZero(n, 0);
1376}
1377
1378static void bc_num_one(BcNum *n)
1379{
1380 bc_num_setToZero(n, 0);
1381 n->len = 1;
1382 n->num[0] = 1;
1383}
1384
1385static void bc_num_ten(BcNum *n)
1386{
1387 bc_num_setToZero(n, 0);
1388 n->len = 2;
1389 n->num[0] = 0;
1390 n->num[1] = 1;
1391}
1392
Denys Vlasenko3129f702018-12-09 12:04:44 +01001393// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001394static void bc_num_init(BcNum *n, size_t req)
1395{
1396 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001397 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001398 n->num = xmalloc(req);
1399 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001400 n->rdx = 0;
1401 n->len = 0;
1402 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001403}
1404
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001405static void bc_num_init_DEF_SIZE(BcNum *n)
1406{
1407 bc_num_init(n, BC_NUM_DEF_SIZE);
1408}
1409
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001410static void bc_num_expand(BcNum *n, size_t req)
1411{
1412 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1413 if (req > n->cap) {
1414 n->num = xrealloc(n->num, req);
1415 n->cap = req;
1416 }
1417}
1418
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001419static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001420{
1421 free(((BcNum *) num)->num);
1422}
1423
1424static void bc_num_copy(BcNum *d, BcNum *s)
1425{
1426 if (d != s) {
1427 bc_num_expand(d, s->cap);
1428 d->len = s->len;
1429 d->neg = s->neg;
1430 d->rdx = s->rdx;
1431 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1432 }
1433}
1434
Denys Vlasenko29301232018-12-11 15:29:32 +01001435static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001436{
1437 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001438 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001439
Denys Vlasenko29301232018-12-11 15:29:32 +01001440 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001441
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001442 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001443
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001444 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001445
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001446 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001447 pow *= 10;
1448
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001449 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001450 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001451 prev = result;
1452 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001453 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001454 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001455
Denys Vlasenko29301232018-12-11 15:29:32 +01001456 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001457}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001458#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001459
1460static void bc_num_ulong2num(BcNum *n, unsigned long val)
1461{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001462 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001463
1464 bc_num_zero(n);
1465
1466 if (val == 0) return;
1467
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001468 if (ULONG_MAX == 0xffffffffUL)
1469 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001470 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001471 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001472 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001473
1474 ptr = n->num;
1475 for (;;) {
1476 n->len++;
1477 *ptr++ = val % 10;
1478 val /= 10;
1479 if (val == 0) break;
1480 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001481}
1482
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001483static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001484 size_t len)
1485{
1486 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001487 for (i = 0; i < len; ++i) {
1488 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001489 a[i + j++] += 10;
1490 a[i + j] -= 1;
1491 }
1492 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001493}
1494
1495static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1496{
1497 size_t i;
1498 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001499 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001500 return BC_NUM_NEG(i + 1, c < 0);
1501}
1502
1503static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1504{
1505 size_t i, min, a_int, b_int, diff;
1506 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001507 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001508 ssize_t cmp;
1509
1510 if (a == b) return 0;
1511 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1512 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001513
1514 if (a->neg != b->neg) // signs of a and b differ
1515 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1516 return (int)b->neg - (int)a->neg;
1517 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001518
1519 a_int = BC_NUM_INT(a);
1520 b_int = BC_NUM_INT(b);
1521 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001522
1523 if (a_int != 0) return (ssize_t) a_int;
1524
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001525 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001526 if (a_max) {
1527 min = b->rdx;
1528 diff = a->rdx - b->rdx;
1529 max_num = a->num + diff;
1530 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001531 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1532 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001533 min = a->rdx;
1534 diff = b->rdx - a->rdx;
1535 max_num = b->num + diff;
1536 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001537 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001538 }
1539
1540 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001541 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001542
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001543 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001544 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001545 }
1546
1547 return 0;
1548}
1549
1550static void bc_num_truncate(BcNum *n, size_t places)
1551{
1552 if (places == 0) return;
1553
1554 n->rdx -= places;
1555
1556 if (n->len != 0) {
1557 n->len -= places;
1558 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1559 }
1560}
1561
1562static void bc_num_extend(BcNum *n, size_t places)
1563{
1564 size_t len = n->len + places;
1565
1566 if (places != 0) {
1567
1568 if (n->cap < len) bc_num_expand(n, len);
1569
1570 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1571 memset(n->num, 0, sizeof(BcDig) * places);
1572
1573 n->len += places;
1574 n->rdx += places;
1575 }
1576}
1577
1578static void bc_num_clean(BcNum *n)
1579{
1580 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1581 if (n->len == 0)
1582 n->neg = false;
1583 else if (n->len < n->rdx)
1584 n->len = n->rdx;
1585}
1586
1587static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1588{
1589 if (n->rdx < scale)
1590 bc_num_extend(n, scale - n->rdx);
1591 else
1592 bc_num_truncate(n, n->rdx - scale);
1593
1594 bc_num_clean(n);
1595 if (n->len != 0) n->neg = !neg1 != !neg2;
1596}
1597
1598static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1599 BcNum *restrict b)
1600{
1601 if (idx < n->len) {
1602
1603 b->len = n->len - idx;
1604 a->len = idx;
1605 a->rdx = b->rdx = 0;
1606
1607 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1608 memcpy(a->num, n->num, idx * sizeof(BcDig));
1609 }
1610 else {
1611 bc_num_zero(b);
1612 bc_num_copy(a, n);
1613 }
1614
1615 bc_num_clean(a);
1616 bc_num_clean(b);
1617}
1618
Denys Vlasenko29301232018-12-11 15:29:32 +01001619static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001620{
Denys Vlasenko29301232018-12-11 15:29:32 +01001621 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001622
1623 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1624 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1625 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001626 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001627 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001628
1629 if (n->rdx >= places)
1630 n->rdx -= places;
1631 else {
1632 bc_num_extend(n, places - n->rdx);
1633 n->rdx = 0;
1634 }
1635
1636 bc_num_clean(n);
1637
Denys Vlasenko29301232018-12-11 15:29:32 +01001638 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001639}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001640#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001641
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001642static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001643{
1644 BcNum one;
1645 BcDig num[2];
1646
1647 one.cap = 2;
1648 one.num = num;
1649 bc_num_one(&one);
1650
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001651 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001652}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001653#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001654
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001655static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001656{
1657 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1658 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1659 int carry, in;
1660
1661 // Because this function doesn't need to use scale (per the bc spec),
1662 // I am hijacking it to say whether it's doing an add or a subtract.
1663
1664 if (a->len == 0) {
1665 bc_num_copy(c, b);
1666 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001667 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001668 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001669 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001670 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001671 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001672 }
1673
1674 c->neg = a->neg;
1675 c->rdx = BC_MAX(a->rdx, b->rdx);
1676 min_rdx = BC_MIN(a->rdx, b->rdx);
1677 c->len = 0;
1678
1679 if (a->rdx > b->rdx) {
1680 diff = a->rdx - b->rdx;
1681 ptr = a->num;
1682 ptr_a = a->num + diff;
1683 ptr_b = b->num;
1684 }
1685 else {
1686 diff = b->rdx - a->rdx;
1687 ptr = b->num;
1688 ptr_a = a->num;
1689 ptr_b = b->num + diff;
1690 }
1691
1692 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1693
1694 ptr_c += diff;
1695 a_int = BC_NUM_INT(a);
1696 b_int = BC_NUM_INT(b);
1697
1698 if (a_int > b_int) {
1699 min_int = b_int;
1700 max = a_int;
1701 ptr = ptr_a;
1702 }
1703 else {
1704 min_int = a_int;
1705 max = b_int;
1706 ptr = ptr_b;
1707 }
1708
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001709 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001710 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1711 carry = in / 10;
1712 ptr_c[i] = (BcDig)(in % 10);
1713 }
1714
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001715 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001716 in = ((int) ptr[i]) + carry;
1717 carry = in / 10;
1718 ptr_c[i] = (BcDig)(in % 10);
1719 }
1720
1721 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1722
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001723 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001724}
1725
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001726static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001727{
Gavin Howard01055ba2018-11-03 11:00:21 -06001728 ssize_t cmp;
1729 BcNum *minuend, *subtrahend;
1730 size_t start;
1731 bool aneg, bneg, neg;
1732
1733 // Because this function doesn't need to use scale (per the bc spec),
1734 // I am hijacking it to say whether it's doing an add or a subtract.
1735
1736 if (a->len == 0) {
1737 bc_num_copy(c, b);
1738 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001739 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001740 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001741 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001742 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001743 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001744 }
1745
1746 aneg = a->neg;
1747 bneg = b->neg;
1748 a->neg = b->neg = false;
1749
1750 cmp = bc_num_cmp(a, b);
1751
1752 a->neg = aneg;
1753 b->neg = bneg;
1754
1755 if (cmp == 0) {
1756 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001757 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001758 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001759 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001760 neg = a->neg;
1761 minuend = a;
1762 subtrahend = b;
1763 }
1764 else {
1765 neg = b->neg;
1766 if (sub) neg = !neg;
1767 minuend = b;
1768 subtrahend = a;
1769 }
1770
1771 bc_num_copy(c, minuend);
1772 c->neg = neg;
1773
1774 if (c->rdx < subtrahend->rdx) {
1775 bc_num_extend(c, subtrahend->rdx - c->rdx);
1776 start = 0;
1777 }
1778 else
1779 start = c->rdx - subtrahend->rdx;
1780
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001781 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001782
1783 bc_num_clean(c);
1784
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001785 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001786}
1787
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001788static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001789 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001790#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001791{
1792 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001793 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001795 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001796
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 if (a->len == 0 || b->len == 0) {
1798 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001799 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001801 aone = BC_NUM_ONE(a);
1802 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001804 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001805 }
1806
1807 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1808 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1809 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001810 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001811 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001812
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 bc_num_expand(c, a->len + b->len + 1);
1814
1815 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001816 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001817
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001818 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001819
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001820 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001821 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001822 unsigned in = c->num[i + j];
1823 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1824 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001825 carry = in / 10;
1826 c->num[i + j] = (BcDig)(in % 10);
1827 }
1828
1829 c->num[i + j] += (BcDig) carry;
1830 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001831
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001832#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001833 // a=2^1000000
1834 // a*a <- without check below, this will not be interruptible
1835 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001836#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 }
1838
1839 c->len = len;
1840
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 }
1843
1844 bc_num_init(&l1, max);
1845 bc_num_init(&h1, max);
1846 bc_num_init(&l2, max);
1847 bc_num_init(&h2, max);
1848 bc_num_init(&m1, max);
1849 bc_num_init(&m2, max);
1850 bc_num_init(&z0, max);
1851 bc_num_init(&z1, max);
1852 bc_num_init(&z2, max);
1853 bc_num_init(&temp, max + max);
1854
1855 bc_num_split(a, max2, &l1, &h1);
1856 bc_num_split(b, max2, &l2, &h2);
1857
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001858 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001859 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001860 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001861 if (s) goto err;
1862
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001863 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001864 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001865 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001866 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001867 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001868 if (s) goto err;
1869
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001870 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001871 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001872 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 if (s) goto err;
1874
Denys Vlasenko29301232018-12-11 15:29:32 +01001875 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001877 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001879 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001880 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001881 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001882
1883err:
1884 bc_num_free(&temp);
1885 bc_num_free(&z2);
1886 bc_num_free(&z1);
1887 bc_num_free(&z0);
1888 bc_num_free(&m2);
1889 bc_num_free(&m1);
1890 bc_num_free(&h2);
1891 bc_num_free(&l2);
1892 bc_num_free(&h1);
1893 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001894 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895}
1896
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001897static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001898{
1899 BcStatus s;
1900 BcNum cpa, cpb;
1901 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1902
1903 scale = BC_MAX(scale, a->rdx);
1904 scale = BC_MAX(scale, b->rdx);
1905 scale = BC_MIN(a->rdx + b->rdx, scale);
1906 maxrdx = BC_MAX(maxrdx, scale);
1907
1908 bc_num_init(&cpa, a->len);
1909 bc_num_init(&cpb, b->len);
1910
1911 bc_num_copy(&cpa, a);
1912 bc_num_copy(&cpb, b);
1913 cpa.neg = cpb.neg = false;
1914
Denys Vlasenko29301232018-12-11 15:29:32 +01001915 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001917 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001918 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001919 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001920 if (s) goto err;
1921
1922 maxrdx += scale;
1923 bc_num_expand(c, c->len + maxrdx);
1924
1925 if (c->len < maxrdx) {
1926 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1927 c->len += maxrdx;
1928 }
1929
1930 c->rdx = maxrdx;
1931 bc_num_retireMul(c, scale, a->neg, b->neg);
1932
1933err:
1934 bc_num_free(&cpb);
1935 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001936 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001937}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001938#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001939
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001940static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001941{
1942 BcStatus s = BC_STATUS_SUCCESS;
1943 BcDig *n, *p, q;
1944 size_t len, end, i;
1945 BcNum cp;
1946 bool zero = true;
1947
1948 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001949 RETURN_STATUS(bc_error("divide by zero"));
1950 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001952 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001953 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001954 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001955 bc_num_copy(c, a);
1956 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001957 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001958 }
1959
1960 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1961 bc_num_copy(&cp, a);
1962 len = b->len;
1963
1964 if (len > cp.len) {
1965 bc_num_expand(&cp, len + 2);
1966 bc_num_extend(&cp, len - cp.len);
1967 }
1968
1969 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1970 cp.rdx -= b->rdx;
1971 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1972
1973 if (b->rdx == b->len) {
1974 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
1975 len -= i - 1;
1976 }
1977
1978 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1979
1980 // We want an extra zero in front to make things simpler.
1981 cp.num[cp.len++] = 0;
1982 end = cp.len - len;
1983
1984 bc_num_expand(c, cp.len);
1985
1986 bc_num_zero(c);
1987 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1988 c->rdx = cp.rdx;
1989 c->len = cp.len;
1990 p = b->num;
1991
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001992 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001993 n = cp.num + i;
1994 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001995 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001996 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001997#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01001998 // a=2^100000
1999 // scale=40000
2000 // 1/a <- without check below, this will not be interruptible
2001 if (G_interrupt) {
2002 s = BC_STATUS_FAILURE;
2003 break;
2004 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002005#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002006 }
2007
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002008 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002009 bc_num_free(&cp);
2010
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002011 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002012}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002013#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002014
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002015static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002016 BcNum *restrict d, size_t scale, size_t ts)
2017{
2018 BcStatus s;
2019 BcNum temp;
2020 bool neg;
2021
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002022 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002023 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002024
2025 if (a->len == 0) {
2026 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002027 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002028 }
2029
2030 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002031 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002032 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002033
2034 if (scale != 0) scale = ts;
2035
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002036 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002037 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002038 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002039 if (s) goto err;
2040
2041 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2042
2043 neg = d->neg;
2044 bc_num_retireMul(d, ts, a->neg, b->neg);
2045 d->neg = neg;
2046
2047err:
2048 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002049 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002050}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002051#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002052
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002053static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002054{
2055 BcStatus s;
2056 BcNum c1;
2057 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2058
2059 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002060 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002061 bc_num_free(&c1);
2062
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002063 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002064}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002065#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002066
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002067static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002068{
2069 BcStatus s = BC_STATUS_SUCCESS;
2070 BcNum copy;
2071 unsigned long pow;
2072 size_t i, powrdx, resrdx;
2073 bool neg, zero;
2074
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002075 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002076
2077 if (b->len == 0) {
2078 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002079 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002080 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002081 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002082 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002083 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002084 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002085 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002086 if (!b->neg)
2087 bc_num_copy(c, a);
2088 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002089 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002090 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002091 }
2092
2093 neg = b->neg;
2094 b->neg = false;
2095
Denys Vlasenko29301232018-12-11 15:29:32 +01002096 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002098
2099 bc_num_init(&copy, a->len);
2100 bc_num_copy(&copy, a);
2101
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002102 if (!neg) {
2103 if (a->rdx > scale)
2104 scale = a->rdx;
2105 if (a->rdx * pow < scale)
2106 scale = a->rdx * pow;
2107 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002108
2109 b->neg = neg;
2110
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002111 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002112 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002113 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002114 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002115 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002116 //if (G_interrupt) {
2117 // s = BC_STATUS_FAILURE;
2118 // goto err;
2119 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 }
2121
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 bc_num_copy(c, &copy);
2123
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002124 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002125
2126 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002127 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 if (s) goto err;
2129
2130 if (pow & 1) {
2131 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002132 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002133 if (s) goto err;
2134 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002135 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002136 //if (G_interrupt) {
2137 // s = BC_STATUS_FAILURE;
2138 // goto err;
2139 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002140 }
2141
2142 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002143 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002144 if (s) goto err;
2145 }
2146
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2148
2149 // We can't use bc_num_clean() here.
2150 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2151 if (zero) bc_num_setToZero(c, scale);
2152
2153err:
2154 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002155 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002156}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002157#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002158
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002159static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002160 BcNumBinaryOp op, size_t req)
2161{
2162 BcStatus s;
2163 BcNum num2, *ptr_a, *ptr_b;
2164 bool init = false;
2165
2166 if (c == a) {
2167 ptr_a = &num2;
2168 memcpy(ptr_a, c, sizeof(BcNum));
2169 init = true;
2170 }
2171 else
2172 ptr_a = a;
2173
2174 if (c == b) {
2175 ptr_b = &num2;
2176 if (c != a) {
2177 memcpy(ptr_b, c, sizeof(BcNum));
2178 init = true;
2179 }
2180 }
2181 else
2182 ptr_b = b;
2183
2184 if (init)
2185 bc_num_init(c, req);
2186 else
2187 bc_num_expand(c, req);
2188
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002189 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01002190 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002191
2192 if (init) bc_num_free(&num2);
2193
Denys Vlasenko29301232018-12-11 15:29:32 +01002194 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002195}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002196#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002197
Denys Vlasenko9311e012018-12-10 11:54:18 +01002198static bool bc_num_strValid(const char *val, size_t base)
2199{
2200 BcDig b;
2201 bool radix;
2202
2203 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2204 radix = false;
2205 for (;;) {
2206 BcDig c = *val++;
2207 if (c == '\0')
2208 break;
2209 if (c == '.') {
2210 if (radix) return false;
2211 radix = true;
2212 continue;
2213 }
2214 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2215 return false;
2216 }
2217 return true;
2218}
2219
2220// Note: n is already "bc_num_zero()"ed,
2221// leading zeroes in "val" are removed
2222static void bc_num_parseDecimal(BcNum *n, const char *val)
2223{
2224 size_t len, i;
2225 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002226
2227 len = strlen(val);
2228 if (len == 0)
2229 return;
2230
Denys Vlasenko9311e012018-12-10 11:54:18 +01002231 bc_num_expand(n, len);
2232
2233 ptr = strchr(val, '.');
2234
2235 n->rdx = 0;
2236 if (ptr != NULL)
2237 n->rdx = (size_t)((val + len) - (ptr + 1));
2238
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002239 for (i = 0; val[i]; ++i) {
2240 if (val[i] != '0' && val[i] != '.') {
2241 // Not entirely zero value - convert it, and exit
2242 i = len - 1;
2243 for (;;) {
2244 n->num[n->len] = val[i] - '0';
2245 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002246 skip_dot:
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002247 if (i == 0) break;
2248 if (val[--i] == '.') goto skip_dot;
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002249 }
2250 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002251 }
2252 }
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002253 // if for() exits without hitting if(), the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002254}
2255
2256// Note: n is already "bc_num_zero()"ed,
2257// leading zeroes in "val" are removed
2258static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2259{
2260 BcStatus s;
2261 BcNum temp, mult, result;
2262 BcDig c = '\0';
2263 unsigned long v;
2264 size_t i, digits;
2265
2266 for (i = 0; ; ++i) {
2267 if (val[i] == '\0')
2268 return;
2269 if (val[i] != '.' && val[i] != '0')
2270 break;
2271 }
2272
2273 bc_num_init_DEF_SIZE(&temp);
2274 bc_num_init_DEF_SIZE(&mult);
2275
2276 for (;;) {
2277 c = *val++;
2278 if (c == '\0') goto int_err;
2279 if (c == '.') break;
2280
2281 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2282
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002283 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002284 if (s) goto int_err;
2285 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002286 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002287 if (s) goto int_err;
2288 }
2289
2290 bc_num_init(&result, base->len);
2291 //bc_num_zero(&result); - already is
2292 bc_num_one(&mult);
2293
2294 digits = 0;
2295 for (;;) {
2296 c = *val++;
2297 if (c == '\0') break;
2298 digits++;
2299
2300 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2301
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002302 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002303 if (s) goto err;
2304 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002305 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002306 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002307 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002308 if (s) goto err;
2309 }
2310
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002311 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002312 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002313 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002314 if (s) goto err;
2315
2316 if (n->len != 0) {
2317 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2318 } else
2319 bc_num_zero(n);
2320
2321err:
2322 bc_num_free(&result);
2323int_err:
2324 bc_num_free(&mult);
2325 bc_num_free(&temp);
2326}
2327
Denys Vlasenko29301232018-12-11 15:29:32 +01002328static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002329 size_t base_t)
2330{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002331 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002332 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002333
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002334 bc_num_zero(n);
2335 while (*val == '0') val++;
2336
Gavin Howard01055ba2018-11-03 11:00:21 -06002337 if (base_t == 10)
2338 bc_num_parseDecimal(n, val);
2339 else
2340 bc_num_parseBase(n, val, base);
2341
Denys Vlasenko29301232018-12-11 15:29:32 +01002342 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002343}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002344#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002345
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002346static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002347{
2348 BcStatus s;
2349 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2350 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2351 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2352
2353 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2354 bc_num_expand(b, req);
2355
2356 if (a->len == 0) {
2357 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002358 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359 }
2360 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002361 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002362 else if (BC_NUM_ONE(a)) {
2363 bc_num_one(b);
2364 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002365 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002366 }
2367
2368 scale = BC_MAX(scale, a->rdx) + 1;
2369 len = a->len + scale;
2370
2371 bc_num_init(&num1, len);
2372 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002373 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002374
2375 bc_num_one(&half);
2376 half.num[0] = 5;
2377 half.rdx = 1;
2378
2379 bc_num_init(&f, len);
2380 bc_num_init(&fprime, len);
2381
2382 x0 = &num1;
2383 x1 = &num2;
2384
2385 bc_num_one(x0);
2386 pow = BC_NUM_INT(a);
2387
2388 if (pow) {
2389
2390 if (pow & 1)
2391 x0->num[0] = 2;
2392 else
2393 x0->num[0] = 6;
2394
2395 pow -= 2 - (pow & 1);
2396
2397 bc_num_extend(x0, pow);
2398
2399 // Make sure to move the radix back.
2400 x0->rdx -= pow;
2401 }
2402
2403 x0->rdx = digs = digs1 = 0;
2404 resrdx = scale + 2;
2405 len = BC_NUM_INT(x0) + resrdx - 1;
2406
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002407 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002408
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002409 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002410 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002411 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002412 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002413 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002414 if (s) goto err;
2415
2416 cmp = bc_num_cmp(x1, x0);
2417 digs = x1->len - (unsigned long long) llabs(cmp);
2418
2419 if (cmp == cmp2 && digs == digs1)
2420 times += 1;
2421 else
2422 times = 0;
2423
2424 resrdx += times > 4;
2425
2426 cmp2 = cmp1;
2427 cmp1 = cmp;
2428 digs1 = digs;
2429
2430 temp = x0;
2431 x0 = x1;
2432 x1 = temp;
2433 }
2434
Gavin Howard01055ba2018-11-03 11:00:21 -06002435 bc_num_copy(b, x0);
2436 scale -= 1;
2437 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2438
2439err:
2440 bc_num_free(&fprime);
2441 bc_num_free(&f);
2442 bc_num_free(&half);
2443 bc_num_free(&num2);
2444 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002445 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002446}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002447#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002448
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002449static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002450 size_t scale)
2451{
2452 BcStatus s;
2453 BcNum num2, *ptr_a;
2454 bool init = false;
2455 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2456
2457 if (c == a) {
2458 memcpy(&num2, c, sizeof(BcNum));
2459 ptr_a = &num2;
2460 bc_num_init(c, len);
2461 init = true;
2462 }
2463 else {
2464 ptr_a = a;
2465 bc_num_expand(c, len);
2466 }
2467
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002468 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002469
2470 if (init) bc_num_free(&num2);
2471
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002472 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002473}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002474#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002475
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002476#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002477static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002478{
2479 BcStatus s;
2480 BcNum base, exp, two, temp;
2481
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002482 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002483 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002484 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002485 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002486 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002487 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002488
2489 bc_num_expand(d, c->len);
2490 bc_num_init(&base, c->len);
2491 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002492 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002493 bc_num_init(&temp, b->len);
2494
2495 bc_num_one(&two);
2496 two.num[0] = 2;
2497 bc_num_one(d);
2498
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002499 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002500 if (s) goto err;
2501 bc_num_copy(&exp, b);
2502
2503 while (exp.len != 0) {
2504
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002505 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002506 if (s) goto err;
2507
2508 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002509 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002510 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002511 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002512 if (s) goto err;
2513 }
2514
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002515 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002516 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002517 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002518 if (s) goto err;
2519 }
2520
2521err:
2522 bc_num_free(&temp);
2523 bc_num_free(&two);
2524 bc_num_free(&exp);
2525 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002526 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002527}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002528#define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002529#endif // ENABLE_DC
2530
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002531#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002532static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002533{
2534 BcId a;
2535 size_t i;
2536
2537 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002538 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002539 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002540 }
2541
2542 a.idx = var;
2543 a.name = name;
2544
2545 bc_vec_push(&f->autos, &a);
2546
Denys Vlasenko29301232018-12-11 15:29:32 +01002547 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002548}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002549#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002550#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002551
2552static void bc_func_init(BcFunc *f)
2553{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002554 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002555 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2556 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2557 f->nparams = 0;
2558}
2559
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002560static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002561{
2562 BcFunc *f = (BcFunc *) func;
2563 bc_vec_free(&f->code);
2564 bc_vec_free(&f->autos);
2565 bc_vec_free(&f->labels);
2566}
2567
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002568static void bc_array_expand(BcVec *a, size_t len);
2569
Gavin Howard01055ba2018-11-03 11:00:21 -06002570static void bc_array_init(BcVec *a, bool nums)
2571{
2572 if (nums)
2573 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2574 else
2575 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2576 bc_array_expand(a, 1);
2577}
2578
Gavin Howard01055ba2018-11-03 11:00:21 -06002579static void bc_array_expand(BcVec *a, size_t len)
2580{
2581 BcResultData data;
2582
2583 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2584 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002585 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002586 bc_vec_push(a, &data.n);
2587 }
2588 }
2589 else {
2590 while (len > a->len) {
2591 bc_array_init(&data.v, true);
2592 bc_vec_push(a, &data.v);
2593 }
2594 }
2595}
2596
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002597static void bc_array_copy(BcVec *d, const BcVec *s)
2598{
2599 size_t i;
2600
2601 bc_vec_pop_all(d);
2602 bc_vec_expand(d, s->cap);
2603 d->len = s->len;
2604
2605 for (i = 0; i < s->len; ++i) {
2606 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2607 bc_num_init(dnum, snum->len);
2608 bc_num_copy(dnum, snum);
2609 }
2610}
2611
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002612static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002613{
2614 free(*((char **) string));
2615}
2616
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002617#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002618static void bc_result_copy(BcResult *d, BcResult *src)
2619{
2620 d->t = src->t;
2621
2622 switch (d->t) {
2623
2624 case BC_RESULT_TEMP:
2625 case BC_RESULT_IBASE:
2626 case BC_RESULT_SCALE:
2627 case BC_RESULT_OBASE:
2628 {
2629 bc_num_init(&d->d.n, src->d.n.len);
2630 bc_num_copy(&d->d.n, &src->d.n);
2631 break;
2632 }
2633
2634 case BC_RESULT_VAR:
2635 case BC_RESULT_ARRAY:
2636 case BC_RESULT_ARRAY_ELEM:
2637 {
2638 d->d.id.name = xstrdup(src->d.id.name);
2639 break;
2640 }
2641
2642 case BC_RESULT_CONSTANT:
2643 case BC_RESULT_LAST:
2644 case BC_RESULT_ONE:
2645 case BC_RESULT_STR:
2646 {
2647 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2648 break;
2649 }
2650 }
2651}
2652#endif // ENABLE_DC
2653
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002654static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002655{
2656 BcResult *r = (BcResult *) result;
2657
2658 switch (r->t) {
2659
2660 case BC_RESULT_TEMP:
2661 case BC_RESULT_IBASE:
2662 case BC_RESULT_SCALE:
2663 case BC_RESULT_OBASE:
2664 {
2665 bc_num_free(&r->d.n);
2666 break;
2667 }
2668
2669 case BC_RESULT_VAR:
2670 case BC_RESULT_ARRAY:
2671 case BC_RESULT_ARRAY_ELEM:
2672 {
2673 free(r->d.id.name);
2674 break;
2675 }
2676
2677 default:
2678 {
2679 // Do nothing.
2680 break;
2681 }
2682 }
2683}
2684
2685static void bc_lex_lineComment(BcLex *l)
2686{
2687 l->t.t = BC_LEX_WHITESPACE;
2688 while (l->i < l->len && l->buf[l->i++] != '\n');
2689 --l->i;
2690}
2691
2692static void bc_lex_whitespace(BcLex *l)
2693{
Gavin Howard01055ba2018-11-03 11:00:21 -06002694 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002695 for (;;) {
2696 char c = l->buf[l->i];
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002697 if (c == '\n') // this is BC_LEX_NLINE, not BC_LEX_WHITESPACE
2698 break;
2699 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002700 break;
2701 l->i++;
2702 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002703}
2704
Denys Vlasenko29301232018-12-11 15:29:32 +01002705static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002706{
2707 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002708 size_t len, bslashes, i, ccnt;
2709 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002710
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002711 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002712 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002713 bslashes = 0;
2714 ccnt = i = 0;
2715 for (;;) {
2716 char c = buf[i];
2717 if (c == '\0')
2718 break;
2719 if (c == '\\' && buf[i + 1] == '\n') {
2720 i += 2;
2721 bslashes++;
2722 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002723 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002724 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2725 if (c != '.') break;
2726 // if '.' was already seen, stop on second one:
2727 if (pt) break;
2728 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002729 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002730 // buf[i] is one of "0-9A-F."
2731 i++;
2732 if (c != '.')
2733 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002734 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002735 //i is buf[i] index of the first not-yet-parsed char
2736 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002737
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002738 //ccnt is the number of chars in the number string, excluding possible
2739 //trailing "." and possible following trailing "\<newline>"(s).
2740 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2741
Denys Vlasenko64074a12018-12-07 15:50:14 +01002742 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2743 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2744 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002745 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002746 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002747
Denys Vlasenko7d628012018-12-04 21:46:47 +01002748 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002749 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002750 bc_vec_push(&l->t.v, &start);
2751
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002752 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002753 // If we have hit a backslash, skip it. We don't have
2754 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002755 if (*buf == '\\') {
2756 buf += 2;
2757 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002758 continue;
2759 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002760 bc_vec_push(&l->t.v, buf);
2761 buf++;
2762 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002763 }
2764
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002765 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002766
Denys Vlasenko29301232018-12-11 15:29:32 +01002767 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002768}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002769#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002770
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002771static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002772{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002773 size_t i;
2774 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002775
2776 l->t.t = BC_LEX_NAME;
2777
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002778 i = 0;
2779 buf = l->buf + l->i - 1;
2780 for (;;) {
2781 char c = buf[i];
2782 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2783 i++;
2784 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002785
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002786#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002787 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2788 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2789 if (i > BC_MAX_STRING)
2790 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2791 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002792#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002793 bc_vec_string(&l->t.v, i, buf);
2794
2795 // Increment the index. We minus 1 because it has already been incremented.
2796 l->i += i - 1;
2797
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002798 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002799}
2800
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002801static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002802{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002803 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002804}
2805
2806static void bc_lex_free(BcLex *l)
2807{
2808 bc_vec_free(&l->t.v);
2809}
2810
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002811static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002812{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002813 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002814 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002815}
2816
Denys Vlasenkoe755e302018-12-13 22:25:28 +01002817IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2818IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002819
2820static BC_STATUS zcommon_lex_token(BcLex *l)
2821{
2822 if (IS_BC) {
2823 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2824 }
2825 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2826}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002827#define zcommon_lex_token(...) (zcommon_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002828
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002829static bool bc_lex_more_input(BcLex *l)
2830{
2831 size_t str;
2832 bool comment;
2833
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002834 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002835
2836 // This loop is complex because the vm tries not to send any lines that end
2837 // with a backslash to the parser. The reason for that is because the parser
2838 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2839 // case, and for strings and comments, the parser will expect more stuff.
2840 comment = false;
2841 str = 0;
2842 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002843 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002844 char *string;
2845
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002846 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002847 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002848 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002849 break;
2850
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002851 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002852 while (*string) {
2853 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002854 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002855 if (IS_BC)
2856 str ^= (c == '"');
2857 else {
2858 if (c == ']')
2859 str -= 1;
2860 else if (c == '[')
2861 str += 1;
2862 }
2863 }
2864 string++;
2865 if (c == '/' && *string == '*') {
2866 comment = true;
2867 string++;
2868 continue;
2869 }
2870 if (c == '*' && *string == '/') {
2871 comment = false;
2872 string++;
2873 }
2874 }
2875 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002876 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002877 continue;
2878 }
2879
2880 // Check for backslash+newline.
2881 // we do not check that last char is '\n' -
2882 // if it is not, then it's EOF, and looping back
2883 // to bc_read_line() will detect it:
2884 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002885 if (string >= G.input_buffer.v && *string == '\\') {
2886 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002887 continue;
2888 }
2889
2890 break;
2891 }
2892
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002893 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002894 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002895// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2896 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002897
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002898 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002899}
2900
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002901static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002902{
2903 BcStatus s;
2904
2905 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002906 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002907
2908 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002909 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002910 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002911
2912 // Loop until failure or we don't have whitespace. This
2913 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002914 // Comments are also BC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002915 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002916 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002917 l->t.t = BC_LEX_EOF;
2918 if (l->i == l->len) {
2919 if (!G.input_fp)
2920 RETURN_STATUS(BC_STATUS_SUCCESS);
2921 if (!bc_lex_more_input(l)) {
2922 G.input_fp = NULL;
2923 RETURN_STATUS(BC_STATUS_SUCCESS);
2924 }
2925 // here it's guaranteed that l->i is below l->len
2926 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002927 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002928 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2929 l->buf + l->i);
Denys Vlasenkoec603182018-12-17 10:34:02 +01002930 s = zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002931 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002932 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002933
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002934 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002935}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002936#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002937
Denys Vlasenko202dd192018-12-16 17:30:35 +01002938static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
2939{
2940 if (l->t.t == BC_LEX_NLINE)
2941 RETURN_STATUS(zbc_lex_next(l));
2942 RETURN_STATUS(BC_STATUS_SUCCESS);
2943}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002944#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002945
2946static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
2947{
2948 BcStatus s;
2949 s = zbc_lex_next(l);
2950 if (s) RETURN_STATUS(s);
2951 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
2952 s = zbc_lex_skip_if_at_NLINE(l);
2953 RETURN_STATUS(s);
2954}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002955#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002956
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002957static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002958{
2959 l->buf = text;
2960 l->i = 0;
2961 l->len = strlen(text);
2962 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002963 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002964}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002965#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002966
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002967#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002968static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002969{
2970 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002971 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002972 const char *buf = l->buf + l->i - 1;
2973
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002974 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2975 const char *keyword8 = bc_lex_kws[i].name8;
2976 unsigned j = 0;
2977 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2978 j++;
2979 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002980 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002981 if (keyword8[j] != '\0')
2982 continue;
2983 match:
2984 // buf starts with keyword bc_lex_kws[i]
2985 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002986 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002987 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01002988 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002989 }
2990
2991 // We minus 1 because the index has already been incremented.
2992 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002993 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002994 }
2995
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002996 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002997
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002998 if (l->t.v.len > 2) {
2999 // Prevent this:
3000 // >>> qwe=1
3001 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3002 // '
3003 unsigned len = strchrnul(buf, '\n') - buf;
3004 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3005 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003006
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003007 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003008}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003009#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003010
Denys Vlasenko26819db2018-12-12 16:08:46 +01003011static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003012{
3013 size_t len, nls = 0, i = l->i;
3014 char c;
3015
3016 l->t.t = BC_LEX_STR;
3017
Denys Vlasenko26819db2018-12-12 16:08:46 +01003018 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i])
3019 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003020
3021 if (c == '\0') {
3022 l->i = i;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003023 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003024 }
3025
3026 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003027 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3028 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3029 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003030 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003031 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003032 bc_vec_string(&l->t.v, len, l->buf + l->i);
3033
3034 l->i = i + 1;
3035 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003036 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
Denys Vlasenko26819db2018-12-12 16:08:46 +01003038 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003039}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003040#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003041
Denys Vlasenko0a238142018-12-14 16:48:34 +01003042static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003043{
3044 if (l->buf[l->i] == '=') {
3045 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003046 with_and_without >>= 8; // store "with" value
3047 } // else store "without" value
3048 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003049}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003050#define bc_lex_assign(l, with, without) \
3051 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003052
Denys Vlasenko29301232018-12-11 15:29:32 +01003053static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003054{
3055 size_t i, nls = 0;
3056 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003057
3058 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003059 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003060 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003061 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003062 check_star:
3063 if (c == '*') {
3064 c = buf[++i];
3065 if (c == '/')
3066 break;
3067 goto check_star;
3068 }
3069 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003070 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003071 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003072 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003073 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003074 }
3075
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003076 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003077 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003078 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003079
Denys Vlasenko29301232018-12-11 15:29:32 +01003080 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003081}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003082#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003083
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003084static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003085{
3086 BcStatus s = BC_STATUS_SUCCESS;
3087 char c = l->buf[l->i++], c2;
3088
3089 // This is the workhorse of the lexer.
3090 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003091// case '\0': // probably never reached
3092// l->i--;
3093// l->t.t = BC_LEX_EOF;
3094// l->newline = true;
3095// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003096 case '\n':
3097 l->t.t = BC_LEX_NLINE;
3098 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003099 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 case '\t':
3101 case '\v':
3102 case '\f':
3103 case '\r':
3104 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003105 bc_lex_whitespace(l);
3106 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003107 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003109 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003110 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003111 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003112 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003113 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003114 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003115 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003118 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003119 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003121 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003122 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003123 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003126 c2 = l->buf[l->i];
3127 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003128 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003129 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003130 ++l->i;
3131 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003132 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003134 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003135 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 case '(':
3138 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3140 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3143 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 c2 = l->buf[l->i];
3146 if (c2 == '+') {
3147 ++l->i;
3148 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003149 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3151 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 l->t.t = BC_LEX_COMMA;
3154 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 c2 = l->buf[l->i];
3157 if (c2 == '-') {
3158 ++l->i;
3159 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003160 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003161 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3162 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003165 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 else {
3167 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003168 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 }
3170 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003171 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 c2 = l->buf[l->i];
3173 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003174 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 else
3176 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3177 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003178 case '0':
3179 case '1':
3180 case '2':
3181 case '3':
3182 case '4':
3183 case '5':
3184 case '6':
3185 case '7':
3186 case '8':
3187 case '9':
3188 case 'A':
3189 case 'B':
3190 case 'C':
3191 case 'D':
3192 case 'E':
3193 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003194 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 l->t.t = BC_LEX_SCOLON;
3198 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3201 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3204 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3207 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 case '[':
3209 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3211 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 if (l->buf[l->i] == '\n') {
3214 l->t.t = BC_LEX_WHITESPACE;
3215 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003216 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003217 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3221 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 case 'a':
3223 case 'b':
3224 case 'c':
3225 case 'd':
3226 case 'e':
3227 case 'f':
3228 case 'g':
3229 case 'h':
3230 case 'i':
3231 case 'j':
3232 case 'k':
3233 case 'l':
3234 case 'm':
3235 case 'n':
3236 case 'o':
3237 case 'p':
3238 case 'q':
3239 case 'r':
3240 case 's':
3241 case 't':
3242 case 'u':
3243 case 'v':
3244 case 'w':
3245 case 'x':
3246 case 'y':
3247 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003248 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 case '{':
3251 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3253 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003256 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003257 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003258 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 ++l->i;
3260 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003261 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003262 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003263 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003264 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003268 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003269 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 }
3271
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003272 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003273}
3274#endif // ENABLE_BC
3275
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003276#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003277static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003278{
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 if (isspace(l->buf[l->i - 1])) {
3280 bc_lex_whitespace(l);
3281 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003282 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003283 RETURN_STATUS(bc_error("extended register"));
3284 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 }
3286 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003287 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003288 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003289 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 l->t.t = BC_LEX_NAME;
3291 }
3292
Denys Vlasenko29301232018-12-11 15:29:32 +01003293 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003294}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003295#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003296
Denys Vlasenko29301232018-12-11 15:29:32 +01003297static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003298{
3299 size_t depth = 1, nls = 0, i = l->i;
3300 char c;
3301
3302 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003303 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003304
3305 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3306
3307 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3308 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3309 nls += (c == '\n');
3310
3311 if (depth) bc_vec_push(&l->t.v, &c);
3312 }
3313
3314 if (c == '\0') {
3315 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003316 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003317 }
3318
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003319 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003320 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3321 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3322 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003323 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003324 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003325
3326 l->i = i;
3327 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003328 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003329
Denys Vlasenko29301232018-12-11 15:29:32 +01003330 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003331}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003332#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003333
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003334static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003335{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003336 static const //BcLexType - should be this type, but narrower type saves size:
3337 uint8_t
3338 dc_lex_regs[] = {
3339 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
3340 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
3341 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
3342 BC_LEX_STORE_PUSH,
3343 };
3344 static const //BcLexType - should be this type
3345 uint8_t
3346 dc_lex_tokens[] = {
3347 /* %&'( */
3348 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
3349 /* )*+, */
3350 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
3351 /* -./ */
3352 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
3353 /* 0123456789 */
3354 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3355 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3356 BC_LEX_INVALID, BC_LEX_INVALID,
3357 /* :;<=>?@ */
3358 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
3359 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
3360 /* ABCDEFGH */
3361 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3362 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
3363 /* IJKLMNOP */
3364 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
3365 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
3366 /* QRSTUVWXY */
3367 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
3368 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
3369 /* Z[\] */
3370 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3371 /* ^_` */
3372 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
3373 /* abcdefgh */
3374 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
3375 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
3376 /* ijklmnop */
3377 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
3378 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
3379 /* qrstuvwx */
3380 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
3381 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
3382 /* yz */
3383 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
3384 /* {|}~ */
3385 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
3386 };
3387
Gavin Howard01055ba2018-11-03 11:00:21 -06003388 BcStatus s = BC_STATUS_SUCCESS;
3389 char c = l->buf[l->i++], c2;
3390 size_t i;
3391
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003392 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3393 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003394 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003395 }
3396
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003397 if (c >= '%' && c <= '~'
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003398 && (l->t.t = dc_lex_tokens[c - '%']) != BC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003399 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003400 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003401 }
3402
3403 // This is the workhorse of the lexer.
3404 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003405// case '\0': // probably never reached
3406// l->t.t = BC_LEX_EOF;
3407// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003408 case '\n':
3409 case '\t':
3410 case '\v':
3411 case '\f':
3412 case '\r':
3413 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003414 l->newline = (c == '\n');
3415 bc_lex_whitespace(l);
3416 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003417 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003418 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003419 if (c2 == '=')
3420 l->t.t = BC_LEX_OP_REL_NE;
3421 else if (c2 == '<')
3422 l->t.t = BC_LEX_OP_REL_LE;
3423 else if (c2 == '>')
3424 l->t.t = BC_LEX_OP_REL_GE;
3425 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003426 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003427 ++l->i;
3428 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003429 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003430 bc_lex_lineComment(l);
3431 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003432 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003433 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003434 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003435 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003436 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003437 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003438 case '0':
3439 case '1':
3440 case '2':
3441 case '3':
3442 case '4':
3443 case '5':
3444 case '6':
3445 case '7':
3446 case '8':
3447 case '9':
3448 case 'A':
3449 case 'B':
3450 case 'C':
3451 case 'D':
3452 case 'E':
3453 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003454 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003455 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003456 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003457 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003458 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003459 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003460 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003461 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003462 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003463 }
3464
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003465 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003466}
3467#endif // ENABLE_DC
3468
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003469static void bc_program_addFunc(char *name, size_t *idx);
3470
Gavin Howard01055ba2018-11-03 11:00:21 -06003471static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3472{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003473 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003474 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003475}
3476
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003477static void bc_parse_push(BcParse *p, char i)
3478{
3479 dbg_lex("%s:%d pushing opcode %d", __func__, __LINE__, i);
3480 bc_vec_pushByte(&p->func->code, i);
3481}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003482
Gavin Howard01055ba2018-11-03 11:00:21 -06003483static void bc_parse_pushName(BcParse *p, char *name)
3484{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003485 while (*name)
3486 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003488}
3489
3490static void bc_parse_pushIndex(BcParse *p, size_t idx)
3491{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003492 size_t mask;
3493 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003494
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003495 dbg_lex("%s:%d pushing index %d", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003496 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3497 amt = sizeof(idx);
3498 do {
3499 if (idx & mask) break;
3500 mask >>= 8;
3501 amt--;
3502 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003503
3504 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003505
3506 while (idx != 0) {
3507 bc_parse_push(p, (unsigned char)idx);
3508 idx >>= 8;
3509 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003510}
3511
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003512static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3513{
3514 bc_parse_push(p, BC_INST_JUMP);
3515 bc_parse_pushIndex(p, idx);
3516}
3517
3518static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3519{
3520 bc_parse_push(p, BC_INST_JUMP_ZERO);
3521 bc_parse_pushIndex(p, idx);
3522}
3523
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003524static void bc_parse_number(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003525{
3526 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003527 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003528
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003529 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003530
3531 bc_parse_push(p, BC_INST_NUM);
3532 bc_parse_pushIndex(p, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003533}
3534
Denys Vlasenko99b37622018-12-15 20:06:59 +01003535IF_BC(static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p);)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01003536IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003537
3538static BC_STATUS zcommon_parse(BcParse *p)
3539{
3540 if (IS_BC) {
Denys Vlasenko99b37622018-12-15 20:06:59 +01003541 IF_BC(RETURN_STATUS(zbc_parse_stmt_or_funcdef(p));)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003542 }
3543 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3544}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003545#define zcommon_parse(...) (zcommon_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003546
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003547static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003548{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003549 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003550
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003551 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003552}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003553#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003554
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003555// Called when parsing or execution detects a failure,
3556// resets execution structures.
3557static void bc_program_reset(void)
3558{
3559 BcFunc *f;
3560 BcInstPtr *ip;
3561
3562 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3563 bc_vec_pop_all(&G.prog.results);
3564
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003565 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003566 ip = bc_vec_top(&G.prog.stack);
3567 ip->idx = f->code.len;
3568}
3569
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003570#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003571 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003572
Denys Vlasenko26819db2018-12-12 16:08:46 +01003573// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003574// resets parsing structures.
3575static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003576{
3577 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003578 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003579 bc_vec_pop_all(&p->func->code);
3580 bc_vec_pop_all(&p->func->autos);
3581 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003582
3583 bc_parse_updateFunc(p, BC_PROG_MAIN);
3584 }
3585
3586 p->l.i = p->l.len;
3587 p->l.t.t = BC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003588
Denys Vlasenko7d628012018-12-04 21:46:47 +01003589 bc_vec_pop_all(&p->exits);
3590 bc_vec_pop_all(&p->conds);
3591 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003592
Denys Vlasenkod38af482018-12-04 19:11:02 +01003593 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003594}
3595
3596static void bc_parse_free(BcParse *p)
3597{
Gavin Howard01055ba2018-11-03 11:00:21 -06003598 bc_vec_free(&p->exits);
3599 bc_vec_free(&p->conds);
3600 bc_vec_free(&p->ops);
3601 bc_lex_free(&p->l);
3602}
3603
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003604static void bc_parse_create(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06003605{
3606 memset(p, 0, sizeof(BcParse));
3607
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003608 bc_lex_init(&p->l);
Denys Vlasenko266aa002018-12-16 23:24:25 +01003609 bc_vec_init(&p->exits, sizeof(size_t), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003610 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003611 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3612
Gavin Howard01055ba2018-11-03 11:00:21 -06003613 bc_parse_updateFunc(p, func);
3614}
3615
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003616#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003617
3618#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3619#define BC_PARSE_LEAF(p, rparen) \
3620 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3621 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3622
3623// We can calculate the conversion between tokens and exprs by subtracting the
3624// position of the first operator in the lex enum and adding the position of the
3625// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko0154d782018-12-14 23:32:51 +01003626#define BC_TOKEN_2_INST(t) ((char) ((t) - BC_LEX_NEG + BC_INST_NEG))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003627
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003628static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003629static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003630static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003631#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
3632#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003633
3634static BC_STATUS zbc_parse_stmt(BcParse *p)
3635{
3636 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3637}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003638#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003639
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003640static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003641{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003642 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3643 // Same for "else", "while()", "for()".
3644 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3645 if (s) RETURN_STATUS(s);
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003646 if (p->l.t.t == BC_LEX_NLINE)
3647 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003648
3649 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003650}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003651#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003652
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003653static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3654 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003655{
Denys Vlasenko65437582018-12-05 19:37:19 +01003656 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3657 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003658
3659 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003660 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003661 if (t == BC_LEX_LPAREN) break;
3662
Denys Vlasenko65437582018-12-05 19:37:19 +01003663 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003664 if (l >= r && (l != r || !left)) break;
3665
Denys Vlasenko0154d782018-12-14 23:32:51 +01003666 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003667 bc_vec_pop(&p->ops);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003668 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003669 }
3670
3671 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003672}
3673
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003674static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003675{
3676 BcLexType top;
3677
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003678 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003679 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003680 top = BC_PARSE_TOP_OP(p);
3681
3682 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003683 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003684
3685 bc_vec_pop(&p->ops);
3686 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3687
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003688 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003689 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003690 top = BC_PARSE_TOP_OP(p);
3691 }
3692
3693 bc_vec_pop(&p->ops);
3694
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003695 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003696}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003697#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003698
Denys Vlasenko26819db2018-12-12 16:08:46 +01003699static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003700{
3701 BcStatus s;
3702 bool comma = false;
3703 size_t nparams;
3704
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003705 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003706 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003707 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003708
3709 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003710 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003711 s = zbc_parse_expr(p, flags, bc_parse_next_param);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003712 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003713
3714 comma = p->l.t.t == BC_LEX_COMMA;
3715 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003716 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003717 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003718 }
3719 }
3720
Denys Vlasenko26819db2018-12-12 16:08:46 +01003721 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003722 bc_parse_push(p, BC_INST_CALL);
3723 bc_parse_pushIndex(p, nparams);
3724
Denys Vlasenko26819db2018-12-12 16:08:46 +01003725 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003726}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003727#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003728
Denys Vlasenko26819db2018-12-12 16:08:46 +01003729static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003730{
3731 BcStatus s;
3732 BcId entry, *entry_ptr;
3733 size_t idx;
3734
3735 entry.name = name;
3736
Denys Vlasenko26819db2018-12-12 16:08:46 +01003737 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003738 if (s) goto err;
3739
3740 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003741 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003742 goto err;
3743 }
3744
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003745 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003746
3747 if (idx == BC_VEC_INVALID_IDX) {
3748 name = xstrdup(entry.name);
3749 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003750 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003751 free(entry.name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003752 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003753 free(name);
3754
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003755 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003756 bc_parse_pushIndex(p, entry_ptr->idx);
3757
Denys Vlasenko26819db2018-12-12 16:08:46 +01003758 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003759
3760err:
3761 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003762 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003763}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003764#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003765
Denys Vlasenko26819db2018-12-12 16:08:46 +01003766static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003767{
3768 BcStatus s;
3769 char *name;
3770
3771 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003772 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003773 if (s) goto err;
3774
3775 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003776 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003777 if (s) goto err;
3778
3779 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003780 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003781 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003782 goto err;
3783 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003784 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003785 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003786 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003787 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003788 s = zbc_parse_expr(p, flags, bc_parse_next_elem);
Gavin Howard01055ba2018-11-03 11:00:21 -06003789 if (s) goto err;
3790 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003791 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003792 if (s) goto err;
3793 bc_parse_push(p, *type);
3794 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003795 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003796 }
3797 else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003798 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003799 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003800 goto err;
3801 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003802 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003803 s = zbc_parse_call(p, name, flags);
3804 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003805 *type = BC_INST_VAR;
3806 bc_parse_push(p, BC_INST_VAR);
3807 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003808 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 }
3810
Denys Vlasenko26819db2018-12-12 16:08:46 +01003811 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003812
3813err:
3814 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003815 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003816}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003817#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003818
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003819static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003820{
3821 BcStatus s;
3822
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003823 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003824 if (s) RETURN_STATUS(s);
3825 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003826
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003827 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003828 if (s) RETURN_STATUS(s);
3829 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003830
3831 bc_parse_push(p, BC_INST_READ);
3832
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003833 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003834}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003835#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003836
Denys Vlasenko26819db2018-12-12 16:08:46 +01003837static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003838 BcInst *prev)
3839{
3840 BcStatus s;
3841
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003842 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003843 if (s) RETURN_STATUS(s);
3844 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003845
3846 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3847
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003848 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003849 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003850
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003851 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003852 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003853
Denys Vlasenko26819db2018-12-12 16:08:46 +01003854 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003855
3856 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3857 bc_parse_push(p, *prev);
3858
Denys Vlasenko26819db2018-12-12 16:08:46 +01003859 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003860}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003861#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003862
Denys Vlasenko26819db2018-12-12 16:08:46 +01003863static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003864{
3865 BcStatus s;
3866
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003867 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003868 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003869
3870 if (p->l.t.t != BC_LEX_LPAREN) {
3871 *type = BC_INST_SCALE;
3872 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003873 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003874 }
3875
3876 *type = BC_INST_SCALE_FUNC;
3877 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3878
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003879 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003880 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003881
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003882 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003883 if (s) RETURN_STATUS(s);
3884 if (p->l.t.t != BC_LEX_RPAREN)
3885 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003886 bc_parse_push(p, BC_INST_SCALE_FUNC);
3887
Denys Vlasenko26819db2018-12-12 16:08:46 +01003888 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003889}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003890#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003891
Denys Vlasenko26819db2018-12-12 16:08:46 +01003892static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003893 size_t *nexprs, uint8_t flags)
3894{
3895 BcStatus s;
3896 BcLexType type;
3897 char inst;
3898 BcInst etype = *prev;
3899
3900 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3901 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3902 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3903 {
3904 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3905 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003906 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 }
3908 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003909 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3910 *paren_expr = true;
3911
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003912 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003913 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003914 type = p->l.t.t;
3915
3916 // Because we parse the next part of the expression
3917 // right here, we need to increment this.
3918 *nexprs = *nexprs + 1;
3919
3920 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003921 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01003922 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003923 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003924 case BC_LEX_KEY_IBASE:
3925 case BC_LEX_KEY_LAST:
3926 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06003927 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003928 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003929 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003930 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003931 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003932 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003933 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003934 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003935 else
3936 bc_parse_push(p, BC_INST_SCALE);
3937 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003938 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003939 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003940 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003941 }
3942
3943 if (!s) bc_parse_push(p, inst);
3944 }
3945
Denys Vlasenko26819db2018-12-12 16:08:46 +01003946 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003947}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003948#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003949
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003950static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06003951 bool rparen, size_t *nexprs)
3952{
3953 BcStatus s;
3954 BcLexType type;
3955 BcInst etype = *prev;
3956
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003957 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003958 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003959
3960 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3961 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3962 BC_LEX_OP_MINUS :
3963 BC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01003964 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
3966 // We can just push onto the op stack because this is the largest
3967 // precedence operator that gets pushed. Inc/dec does not.
3968 if (type != BC_LEX_OP_MINUS)
3969 bc_vec_push(&p->ops, &type);
3970 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003971 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06003972
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003973 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003974}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003975#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003976
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003977static BC_STATUS zbc_parse_string(BcParse *p, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06003978{
3979 char *str = xstrdup(p->l.t.v.v);
3980
3981 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003982 bc_parse_pushIndex(p, G.prog.strs.len);
3983 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003984 bc_parse_push(p, inst);
3985
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003986 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003987}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003988#define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__) COMMA_SUCCESS)
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;
Gavin Howard01055ba2018-11-03 11:00:21 -06003994
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01003995 for (;;) {
3996 s = zbc_lex_next(&p->l);
3997 if (s) RETURN_STATUS(s);
3998 type = p->l.t.t;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003999 if (type == BC_LEX_STR) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004000 s = zbc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004001 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004002 s = zbc_parse_expr(p, 0, bc_parse_next_print);
Gavin Howard01055ba2018-11-03 11:00:21 -06004003 bc_parse_push(p, BC_INST_PRINT_POP);
4004 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004005 if (s) RETURN_STATUS(s);
4006 if (p->l.t.t != BC_LEX_COMMA)
4007 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004008 }
4009
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004010 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004011}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004012#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004013
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004014static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004015{
4016 BcStatus s;
4017 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004018
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004019 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004020 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004021 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004022
4023 t = p->l.t.t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004024 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4025 bc_parse_push(p, BC_INST_RET0);
4026 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004027 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004028 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004029 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004030 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004031 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004032 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004033 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004034
4035 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004036 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004037 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004038 }
4039
4040 bc_parse_push(p, BC_INST_RET);
4041 }
4042
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004043 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004044 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004045}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004046#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004047
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004048static void rewrite_label_to_current(BcParse *p, size_t idx)
4049{
4050 size_t *label = bc_vec_item(&p->func->labels, idx);
4051 *label = p->func->code.len;
4052}
4053
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004054static BC_STATUS zbc_parse_if(BcParse *p)
4055{
4056 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004057 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004058
4059 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4060 s = zbc_lex_next(&p->l);
4061 if (s) RETURN_STATUS(s);
4062 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4063
4064 s = zbc_lex_next(&p->l);
4065 if (s) RETURN_STATUS(s);
4066 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4067 if (s) RETURN_STATUS(s);
4068
4069 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004070
Denys Vlasenko15850832018-12-16 21:40:54 +01004071 ip_idx = p->func->labels.len;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004072 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenko15850832018-12-16 21:40:54 +01004073 bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004074
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004075 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004076 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004077
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004078 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4079 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004080 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004081
Denys Vlasenko15850832018-12-16 21:40:54 +01004082 ip2_idx = p->func->labels.len;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004083
Denys Vlasenko15850832018-12-16 21:40:54 +01004084 dbg_lex("%s:%d after if() body: BC_INST_JUMP to %d", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004085 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004086
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004087 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %d", __func__, __LINE__, p->func->code.len);
4088 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004089
Denys Vlasenko15850832018-12-16 21:40:54 +01004090 bc_vec_push(&p->func->labels, &ip2_idx);
4091 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004092
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004093 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004094 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004095 }
4096
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004097 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %d", __func__, __LINE__, p->func->code.len);
4098 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004099
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004100 dbg_lex_done("%s:%d done", __func__, __LINE__);
4101 RETURN_STATUS(s);
4102}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004103#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004104
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004105static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004106{
4107 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004108 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004109 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004110
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004111 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004112 if (s) RETURN_STATUS(s);
4113 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004114 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004115 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004116
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004117 cond_idx = p->func->labels.len;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004118 ip_idx = cond_idx + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004119
4120 bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004121 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004122
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004123 bc_vec_push(&p->exits, &ip_idx);
4124 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004125
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004126 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4127 if (s) RETURN_STATUS(s);
4128 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004129
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004130 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004131
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004132 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004133 if (s) RETURN_STATUS(s);
4134
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004135 dbg_lex("%s:%d BC_INST_JUMP to %d", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004136 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004137
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004138 dbg_lex("%s:%d rewriting label-> %d", __func__, __LINE__, p->func->code.len);
4139 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004140
4141 bc_vec_pop(&p->exits);
4142 bc_vec_pop(&p->conds);
4143
4144 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004145}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004146#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004147
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004148static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004149{
4150 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004151 size_t cond_idx, exit_idx, body_idx, update_idx;
4152
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004153 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004154 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004155 if (s) RETURN_STATUS(s);
4156 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004157 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004158 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004159
4160 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004161 s = zbc_parse_expr(p, 0, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004162 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004163 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004164
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004165 if (s) RETURN_STATUS(s);
4166 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004167 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004168 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004169
4170 cond_idx = p->func->labels.len;
4171 update_idx = cond_idx + 1;
4172 body_idx = update_idx + 1;
4173 exit_idx = body_idx + 1;
4174
4175 bc_vec_push(&p->func->labels, &p->func->code.len);
4176
4177 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004178 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004179 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004180 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004181
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004182 if (s) RETURN_STATUS(s);
4183 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004184
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004185 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004186 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004187
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004188 bc_parse_pushJUMP_ZERO(p, exit_idx);
4189 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004190
Gavin Howard01055ba2018-11-03 11:00:21 -06004191 bc_vec_push(&p->conds, &update_idx);
4192 bc_vec_push(&p->func->labels, &p->func->code.len);
4193
4194 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004195 s = zbc_parse_expr(p, 0, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06004196 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004197 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004198
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004199 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004200
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004201 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004202 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004203 bc_vec_push(&p->func->labels, &p->func->code.len);
4204
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004205 bc_vec_push(&p->exits, &exit_idx);
4206 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004207
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004208 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004209 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004210
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004211 dbg_lex("%s:%d BC_INST_JUMP to %d", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004212 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004213
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004214 dbg_lex("%s:%d rewriting label-> %d", __func__, __LINE__, p->func->code.len);
4215 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004216
4217 bc_vec_pop(&p->exits);
4218 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004219
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004220 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004221}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004222#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004223
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004224static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004225{
4226 BcStatus s;
4227 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004228
Gavin Howard01055ba2018-11-03 11:00:21 -06004229 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004230 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4231 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004232 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004233 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004234 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004235 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004236
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004237 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004238
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004239 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004240 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004241
4242 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004243 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004244
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004245 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004246}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004247#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004248
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004249static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004250{
4251 BcStatus s;
4252 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004253 char *name;
4254
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004255 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004256 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004257 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004258 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004259
4260 name = xstrdup(p->l.t.v.v);
4261 bc_parse_addFunc(p, name, &p->fidx);
4262
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004263 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004264 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004265 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004266 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004267 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004268 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004269
4270 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004271 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004272 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004273
4274 ++p->func->nparams;
4275
4276 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004277 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004278 if (s) goto err;
4279
4280 var = p->l.t.t != BC_LEX_LBRACKET;
4281
4282 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004283 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004284 if (s) goto err;
4285
4286 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004287 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004288 goto err;
4289 }
4290
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004291 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004292 if (s) goto err;
4293 }
4294
4295 comma = p->l.t.t == BC_LEX_COMMA;
4296 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004297 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004298 if (s) goto err;
4299 }
4300
Denys Vlasenko29301232018-12-11 15:29:32 +01004301 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004302 if (s) goto err;
4303 }
4304
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004305 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004306
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004307 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004308 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004309
4310 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004311 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004312
Denys Vlasenko202dd192018-12-16 17:30:35 +01004313 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4314 s = zbc_lex_skip_if_at_NLINE(&p->l);
4315 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004316 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4317 if (p->l.t.t != BC_LEX_LBRACE)
4318 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004319
4320 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004321 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004322 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004323 if (s) RETURN_STATUS(s);
4324
4325 bc_parse_push(p, BC_INST_RET0);
4326 bc_parse_updateFunc(p, BC_PROG_MAIN);
4327
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004328 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004329
4330err:
4331 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004332 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004333}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004334#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004335
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004336static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004337{
4338 BcStatus s;
4339 bool comma, var, one;
4340 char *name;
4341
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004342 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004343 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004344 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004345
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004346 comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004347 one = p->l.t.t == BC_LEX_NAME;
4348
4349 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004350 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004351 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004352 if (s) goto err;
4353
4354 var = p->l.t.t != BC_LEX_LBRACKET;
4355 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004356 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004357 if (s) goto err;
4358
4359 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004360 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004361 goto err;
4362 }
4363
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004364 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004365 if (s) goto err;
4366 }
4367
4368 comma = p->l.t.t == BC_LEX_COMMA;
4369 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004370 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004371 if (s) goto err;
4372 }
4373
Denys Vlasenko29301232018-12-11 15:29:32 +01004374 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004375 if (s) goto err;
4376 }
4377
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004378 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4379 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004380
4381 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004382 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004383
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004384 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004385 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004386
4387err:
4388 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004389 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004390 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004391}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004392#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004393
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004394#undef zbc_parse_stmt_possibly_auto
4395static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004396{
4397 BcStatus s = BC_STATUS_SUCCESS;
4398
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004399 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004400
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004401 if (p->l.t.t == BC_LEX_NLINE) {
4402 dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__);
4403 RETURN_STATUS(zbc_lex_next(&p->l));
4404 }
4405 if (p->l.t.t == BC_LEX_SCOLON) {
4406 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4407 RETURN_STATUS(zbc_lex_next(&p->l));
4408 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004409
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004410 if (p->l.t.t == BC_LEX_LBRACE) {
4411 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4412 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004413 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004414 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004415 } while (p->l.t.t == BC_LEX_NLINE);
4416 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4417 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4418 s = zbc_parse_auto(p);
4419 if (s) RETURN_STATUS(s);
4420 }
4421 while (p->l.t.t != BC_LEX_RBRACE) {
4422 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4423 s = zbc_parse_stmt(p);
4424 if (s) RETURN_STATUS(s);
4425 }
4426 s = zbc_lex_next(&p->l);
4427 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4428 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004429 }
4430
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004431 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004432 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004433 case BC_LEX_OP_INC:
4434 case BC_LEX_OP_DEC:
4435 case BC_LEX_OP_MINUS:
4436 case BC_LEX_OP_BOOL_NOT:
4437 case BC_LEX_LPAREN:
4438 case BC_LEX_NAME:
4439 case BC_LEX_NUMBER:
4440 case BC_LEX_KEY_IBASE:
4441 case BC_LEX_KEY_LAST:
4442 case BC_LEX_KEY_LENGTH:
4443 case BC_LEX_KEY_OBASE:
4444 case BC_LEX_KEY_READ:
4445 case BC_LEX_KEY_SCALE:
4446 case BC_LEX_KEY_SQRT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004447 s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
Gavin Howard01055ba2018-11-03 11:00:21 -06004448 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004449 case BC_LEX_STR:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004450 s = zbc_parse_string(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004451 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 case BC_LEX_KEY_BREAK:
4453 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004454 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004455 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004457 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004458 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004459 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004461 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004462 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004463 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004464 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004465 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004466 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004467 // "limits" is a compile-time command,
4468 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004469 printf(
4470 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4471 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4472 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4473 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4474 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4475 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4476 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4477 "Number of vars = "BC_MAX_VARS_STR "\n"
4478 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004479 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004481 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004482 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004483 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004484 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004485 // "quit" is a compile-time command. For example,
4486 // "if (0 == 1) quit" terminates when parsing the statement,
4487 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004488 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004489 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004490 if (!p->in_funcdef)
4491 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004492 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004493 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004494 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004495 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004496 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004497 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004498 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004500 }
4501
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004502 if (s || G_interrupt) {
4503 bc_parse_reset(p);
4504 s = BC_STATUS_FAILURE;
4505 }
4506
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004507 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004508 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004509}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004510#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004511
Denys Vlasenko99b37622018-12-15 20:06:59 +01004512static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004513{
4514 BcStatus s;
4515
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004516 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004517 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004518 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004519 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004520 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004521 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004522 } else {
4523 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 +01004524 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004525 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004526
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004527 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004528 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004529}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004530#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004531
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004532// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004533static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004534{
4535 BcStatus s = BC_STATUS_SUCCESS;
4536 BcInst prev = BC_INST_PRINT;
4537 BcLexType top, t = p->l.t.t;
4538 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004539 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004540 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4541
Denys Vlasenko17df8822018-12-14 23:00:24 +01004542 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 paren_first = p->l.t.t == BC_LEX_LPAREN;
4544 nparens = nrelops = 0;
4545 paren_expr = rprn = done = get_token = assign = false;
4546 bin_last = true;
4547
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004548 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 switch (t) {
4550
4551 case BC_LEX_OP_INC:
4552 case BC_LEX_OP_DEC:
4553 {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004554 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004555 rprn = get_token = bin_last = false;
4556 break;
4557 }
4558
4559 case BC_LEX_OP_MINUS:
4560 {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004561 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 rprn = get_token = false;
4563 bin_last = prev == BC_INST_MINUS;
4564 break;
4565 }
4566
4567 case BC_LEX_OP_ASSIGN_POWER:
4568 case BC_LEX_OP_ASSIGN_MULTIPLY:
4569 case BC_LEX_OP_ASSIGN_DIVIDE:
4570 case BC_LEX_OP_ASSIGN_MODULUS:
4571 case BC_LEX_OP_ASSIGN_PLUS:
4572 case BC_LEX_OP_ASSIGN_MINUS:
4573 case BC_LEX_OP_ASSIGN:
4574 {
4575 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4576 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4577 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4578 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004579 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004580 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004581 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004582 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 break;
4584 }
4585 }
4586 // Fallthrough.
4587 case BC_LEX_OP_POWER:
4588 case BC_LEX_OP_MULTIPLY:
4589 case BC_LEX_OP_DIVIDE:
4590 case BC_LEX_OP_MODULUS:
4591 case BC_LEX_OP_PLUS:
4592 case BC_LEX_OP_REL_EQ:
4593 case BC_LEX_OP_REL_LE:
4594 case BC_LEX_OP_REL_GE:
4595 case BC_LEX_OP_REL_NE:
4596 case BC_LEX_OP_REL_LT:
4597 case BC_LEX_OP_REL_GT:
4598 case BC_LEX_OP_BOOL_NOT:
4599 case BC_LEX_OP_BOOL_OR:
4600 case BC_LEX_OP_BOOL_AND:
4601 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004602 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4603 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4604 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004605 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004606 }
4607
4608 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004609 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004610 bc_parse_operator(p, t, ops_bgn, &nexprs);
4611 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 rprn = get_token = false;
4613 bin_last = t != BC_LEX_OP_BOOL_NOT;
4614
4615 break;
4616 }
4617
4618 case BC_LEX_LPAREN:
4619 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004620 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004621 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 ++nparens;
4623 paren_expr = rprn = bin_last = false;
4624 get_token = true;
4625 bc_vec_push(&p->ops, &t);
4626
4627 break;
4628 }
4629
4630 case BC_LEX_RPAREN:
4631 {
4632 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004633 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004634
4635 if (nparens == 0) {
4636 s = BC_STATUS_SUCCESS;
4637 done = true;
4638 get_token = false;
4639 break;
4640 }
Denys Vlasenko17df8822018-12-14 23:00:24 +01004641 else if (!paren_expr) {
4642 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004643 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004644 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004645
4646 --nparens;
4647 paren_expr = rprn = true;
4648 get_token = bin_last = false;
4649
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004650 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004651
4652 break;
4653 }
4654
4655 case BC_LEX_NAME:
4656 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004657 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004658 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004659 paren_expr = true;
4660 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004661 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004662 ++nexprs;
4663
4664 break;
4665 }
4666
4667 case BC_LEX_NUMBER:
4668 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004669 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004670 return bc_error_bad_expression();
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004671 bc_parse_number(p);
4672 nexprs++;
4673 prev = BC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004674 paren_expr = get_token = true;
4675 rprn = bin_last = false;
4676
4677 break;
4678 }
4679
4680 case BC_LEX_KEY_IBASE:
4681 case BC_LEX_KEY_LAST:
4682 case BC_LEX_KEY_OBASE:
4683 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004684 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004685 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4687 bc_parse_push(p, (char) prev);
4688
4689 paren_expr = get_token = true;
4690 rprn = bin_last = false;
4691 ++nexprs;
4692
4693 break;
4694 }
4695
4696 case BC_LEX_KEY_LENGTH:
4697 case BC_LEX_KEY_SQRT:
4698 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004699 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004700 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004701 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 paren_expr = true;
4703 rprn = get_token = bin_last = false;
4704 ++nexprs;
4705
4706 break;
4707 }
4708
4709 case BC_LEX_KEY_READ:
4710 {
4711 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004712 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004713 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004714 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004715 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004716 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004717
4718 paren_expr = true;
4719 rprn = get_token = bin_last = false;
4720 ++nexprs;
4721 prev = BC_INST_READ;
4722
4723 break;
4724 }
4725
4726 case BC_LEX_KEY_SCALE:
4727 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004728 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004729 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004730 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004731 paren_expr = true;
4732 rprn = get_token = bin_last = false;
4733 ++nexprs;
4734 prev = BC_INST_SCALE;
4735
4736 break;
4737 }
4738
4739 default:
4740 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004741 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004742 break;
4743 }
4744 }
4745
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004746 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004747 }
4748
4749 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004750 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004751
4752 while (p->ops.len > ops_bgn) {
4753
4754 top = BC_PARSE_TOP_OP(p);
4755 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4756
4757 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004758 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004759
Denys Vlasenko0154d782018-12-14 23:32:51 +01004760 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004761
4762 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4763 bc_vec_pop(&p->ops);
4764 }
4765
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004766 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004767 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004768
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004769//TODO: why is this needed at all?
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004770 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4771 for (;;) {
4772 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004773 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004774 if (next & 0x80) // last element?
4775 break;
4776 next >>= 8;
4777 }
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004778 if (t != BC_LEX_KEY_ELSE)
4779 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004780 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004781
4782 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004783 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004784 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004785 }
4786 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004787 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004788 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004789 }
4790
4791 if (flags & BC_PARSE_PRINT) {
4792 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4793 bc_parse_push(p, BC_INST_POP);
4794 }
4795
Denys Vlasenko17df8822018-12-14 23:00:24 +01004796 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004797 return s;
4798}
4799
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004800#undef zbc_parse_expr
4801static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004802{
4803 BcStatus s;
4804
4805 s = bc_parse_expr_empty_ok(p, flags, next);
4806 if (s == BC_STATUS_PARSE_EMPTY_EXP)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004807 RETURN_STATUS(bc_error("empty expression"));
4808 RETURN_STATUS(s);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004809}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004810#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004811
Gavin Howard01055ba2018-11-03 11:00:21 -06004812#endif // ENABLE_BC
4813
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004814#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004815
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004816static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004817{
4818 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004819
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004820 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004821 if (s) RETURN_STATUS(s);
4822 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004823
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004824 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004825
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004826 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004827}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004828#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004829
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004830static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004831{
Denys Vlasenkoe42cc192018-12-17 11:02:26 +01004832 char *str, *name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004833 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004834
Denys Vlasenkoe42cc192018-12-17 11:02:26 +01004835//why pad to 32 zeros??
4836 name = xasprintf("%032lu", (unsigned long)len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004837
4838 str = xstrdup(p->l.t.v.v);
4839 bc_parse_push(p, BC_INST_STR);
4840 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004841 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004842 bc_parse_addFunc(p, name, &idx);
4843
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004844 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004845}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004846#define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004847
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004848static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004849{
4850 BcStatus s;
4851
4852 bc_parse_push(p, inst);
4853 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004854 s = zdc_parse_register(p);
4855 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004856 }
4857
4858 if (store) {
4859 bc_parse_push(p, BC_INST_SWAP);
4860 bc_parse_push(p, BC_INST_ASSIGN);
4861 bc_parse_push(p, BC_INST_POP);
4862 }
4863
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004864 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004865}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004866#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004867
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004868static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004869{
4870 BcStatus s;
4871
4872 bc_parse_push(p, inst);
4873 bc_parse_push(p, BC_INST_EXEC_COND);
4874
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004875 s = zdc_parse_register(p);
4876 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004877
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004878 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004879 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004880
4881 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004882 s = zdc_parse_register(p);
4883 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004884 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004885 }
4886 else
4887 bc_parse_push(p, BC_PARSE_STREND);
4888
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004889 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004890}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004891#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004892
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004893static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004894{
4895 BcStatus s = BC_STATUS_SUCCESS;
4896 BcInst prev;
4897 uint8_t inst;
4898 bool assign, get_token = false;
4899
4900 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004901 case BC_LEX_OP_REL_EQ:
4902 case BC_LEX_OP_REL_LE:
4903 case BC_LEX_OP_REL_GE:
4904 case BC_LEX_OP_REL_NE:
4905 case BC_LEX_OP_REL_LT:
4906 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004907 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004908 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004909 case BC_LEX_SCOLON:
4910 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004911 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004912 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004913 case BC_LEX_STR:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004914 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004915 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004916 case BC_LEX_NEG:
4917 case BC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004918 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004919 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004920 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004921 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004922 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004923 }
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004924 bc_parse_number(p);
4925 prev = BC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004926 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
4927 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004928 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004929 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004931 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004932 else
4933 bc_parse_push(p, BC_INST_READ);
4934 get_token = true;
4935 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004936 case BC_LEX_OP_ASSIGN:
4937 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06004938 assign = t == BC_LEX_OP_ASSIGN;
4939 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004940 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004941 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004942 case BC_LEX_LOAD:
4943 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06004944 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004945 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004946 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 case BC_LEX_STORE_IBASE:
4948 case BC_LEX_STORE_SCALE:
4949 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004950 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004951 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004952 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004953 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004954 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004955 get_token = true;
4956 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004957 }
4958
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004959 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004960
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004961 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004962}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004963#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004964
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004965static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004966{
4967 BcStatus s = BC_STATUS_SUCCESS;
4968 BcInst inst;
4969 BcLexType t;
4970
Gavin Howard01055ba2018-11-03 11:00:21 -06004971 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004972 inst = dc_parse_insts[t];
4973
4974 if (inst != BC_INST_INVALID) {
4975 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004976 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004977 } else
4978 s = zdc_parse_token(p, t, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004979 }
4980
4981 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
4982 bc_parse_push(p, BC_INST_POP_EXEC);
4983
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004984 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004985}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004986#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004987
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01004988static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004989{
4990 BcStatus s;
4991
4992 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004993 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004994 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004995 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004996
Denys Vlasenkod38af482018-12-04 19:11:02 +01004997 if (s || G_interrupt) {
4998 bc_parse_reset(p);
4999 s = BC_STATUS_FAILURE;
5000 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005001
Denys Vlasenko26819db2018-12-12 16:08:46 +01005002 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005003}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005004#define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005005
Gavin Howard01055ba2018-11-03 11:00:21 -06005006#endif // ENABLE_DC
5007
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005008static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005009{
5010 if (IS_BC) {
Denys Vlasenko99b37622018-12-15 20:06:59 +01005011 IF_BC(RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read)));
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005012 } else {
Denys Vlasenko99b37622018-12-15 20:06:59 +01005013 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags)));
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005014 }
5015}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005016#define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005017
Denys Vlasenkodf515392018-12-02 19:27:48 +01005018static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005019{
Gavin Howard01055ba2018-11-03 11:00:21 -06005020 BcId e, *ptr;
5021 BcVec *v, *map;
5022 size_t i;
5023 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005024 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005025
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005026 v = var ? &G.prog.vars : &G.prog.arrs;
5027 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005028
5029 e.name = id;
5030 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005031 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005032
5033 if (new) {
5034 bc_array_init(&data.v, var);
5035 bc_vec_push(v, &data.v);
5036 }
5037
5038 ptr = bc_vec_item(map, i);
5039 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005040 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005041}
5042
Denys Vlasenko29301232018-12-11 15:29:32 +01005043static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005044{
Gavin Howard01055ba2018-11-03 11:00:21 -06005045 switch (r->t) {
5046
5047 case BC_RESULT_STR:
5048 case BC_RESULT_TEMP:
5049 case BC_RESULT_IBASE:
5050 case BC_RESULT_SCALE:
5051 case BC_RESULT_OBASE:
5052 {
5053 *num = &r->d.n;
5054 break;
5055 }
5056
5057 case BC_RESULT_CONSTANT:
5058 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005059 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005060 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005061 size_t base_t, len = strlen(*str);
5062 BcNum *base;
5063
5064 bc_num_init(&r->d.n, len);
5065
5066 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005067 base = hex ? &G.prog.hexb : &G.prog.ib;
5068 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005069 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005070
5071 if (s) {
5072 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005073 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005074 }
5075
5076 *num = &r->d.n;
5077 r->t = BC_RESULT_TEMP;
5078
5079 break;
5080 }
5081
5082 case BC_RESULT_VAR:
5083 case BC_RESULT_ARRAY:
5084 case BC_RESULT_ARRAY_ELEM:
5085 {
5086 BcVec *v;
5087
Denys Vlasenkodf515392018-12-02 19:27:48 +01005088 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005089
5090 if (r->t == BC_RESULT_ARRAY_ELEM) {
5091 v = bc_vec_top(v);
5092 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5093 *num = bc_vec_item(v, r->d.id.idx);
5094 }
5095 else
5096 *num = bc_vec_top(v);
5097
5098 break;
5099 }
5100
5101 case BC_RESULT_LAST:
5102 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005103 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005104 break;
5105 }
5106
5107 case BC_RESULT_ONE:
5108 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005109 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005110 break;
5111 }
5112 }
5113
Denys Vlasenko29301232018-12-11 15:29:32 +01005114 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005115}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005116#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005117
Denys Vlasenko29301232018-12-11 15:29:32 +01005118static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005119 BcResult **r, BcNum **rn, bool assign)
5120{
5121 BcStatus s;
5122 bool hex;
5123 BcResultType lt, rt;
5124
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005125 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005126 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005127
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005128 *r = bc_vec_item_rev(&G.prog.results, 0);
5129 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005130
5131 lt = (*l)->t;
5132 rt = (*r)->t;
5133 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5134
Denys Vlasenko29301232018-12-11 15:29:32 +01005135 s = zbc_program_num(*l, ln, false);
5136 if (s) RETURN_STATUS(s);
5137 s = zbc_program_num(*r, rn, hex);
5138 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005139
5140 // We run this again under these conditions in case any vector has been
5141 // reallocated out from under the BcNums or arrays we had.
5142 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005143 s = zbc_program_num(*l, ln, false);
5144 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005145 }
5146
5147 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005148 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005149 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005150 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005151
Denys Vlasenko29301232018-12-11 15:29:32 +01005152 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005153}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005154#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005155
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005156static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005157{
5158 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005159 bc_vec_pop(&G.prog.results);
5160 bc_vec_pop(&G.prog.results);
5161 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005162}
5163
Denys Vlasenko29301232018-12-11 15:29:32 +01005164static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005165{
5166 BcStatus s;
5167
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005168 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005169 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005170 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005171
Denys Vlasenko29301232018-12-11 15:29:32 +01005172 s = zbc_program_num(*r, n, false);
5173 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005174
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005175 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005176 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005177
Denys Vlasenko29301232018-12-11 15:29:32 +01005178 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005179}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005180#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005181
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005182static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005183{
5184 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005185 bc_vec_pop(&G.prog.results);
5186 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005187}
5188
Denys Vlasenko259137d2018-12-11 19:42:05 +01005189static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005190{
5191 BcStatus s;
5192 BcResult *opd1, *opd2, res;
5193 BcNum *n1, *n2 = NULL;
5194
Denys Vlasenko29301232018-12-11 15:29:32 +01005195 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005196 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005197 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005198
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005199 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005200 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005201 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005202 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005203
Denys Vlasenko259137d2018-12-11 19:42:05 +01005204 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005205
5206err:
5207 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005208 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005209}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005210#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005211
Denys Vlasenko26819db2018-12-12 16:08:46 +01005212static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005213{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005214 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005215 BcStatus s;
5216 BcParse parse;
5217 BcVec buf;
5218 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005219 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005220
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005221 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005222 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005223
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005224 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005225 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005226
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005227 sv_file = G.prog.file;
5228 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005229 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005230
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005231 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005232 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005234 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005235 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005236
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005237 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005238 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005239 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005240 if (s) goto exec_err;
5241
5242 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005243 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005244 goto exec_err;
5245 }
5246
5247 ip.func = BC_PROG_READ;
5248 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005249 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005250
5251 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005252 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005253
5254 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005255 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005256
5257exec_err:
5258 bc_parse_free(&parse);
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005259 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005260 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005261 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005262 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005263}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005264#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005265
5266static size_t bc_program_index(char *code, size_t *bgn)
5267{
5268 char amt = code[(*bgn)++], i = 0;
5269 size_t res = 0;
5270
5271 for (; i < amt; ++i, ++(*bgn))
5272 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5273
5274 return res;
5275}
5276
5277static char *bc_program_name(char *code, size_t *bgn)
5278{
5279 size_t i;
5280 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5281
5282 s = xmalloc(ptr - str + 1);
5283 c = code[(*bgn)++];
5284
5285 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5286 s[i] = c;
5287
5288 s[i] = '\0';
5289
5290 return s;
5291}
5292
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005293static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005294{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005295#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005296 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005297 // Example: echo '[]ap' | dc
5298 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005299 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005300 return;
5301 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005302#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005303 while (*str) {
5304 int c = *str++;
5305 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005306 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005307 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005308 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005309 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005310 case 'a':
5311 bb_putchar('\a');
5312 break;
5313 case 'b':
5314 bb_putchar('\b');
5315 break;
5316 case '\\':
5317 case 'e':
5318 bb_putchar('\\');
5319 break;
5320 case 'f':
5321 bb_putchar('\f');
5322 break;
5323 case 'n':
5324 bb_putchar('\n');
5325 G.prog.nchars = SIZE_MAX;
5326 break;
5327 case 'r':
5328 bb_putchar('\r');
5329 break;
5330 case 'q':
5331 bb_putchar('"');
5332 break;
5333 case 't':
5334 bb_putchar('\t');
5335 break;
5336 default:
5337 // Just print the backslash and following character.
5338 bb_putchar('\\');
5339 ++G.prog.nchars;
5340 bb_putchar(c);
5341 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005342 }
5343 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005344 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005345 }
5346}
5347
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005348static void bc_num_printNewline(void)
5349{
5350 if (G.prog.nchars == G.prog.len - 1) {
5351 bb_putchar('\\');
5352 bb_putchar('\n');
5353 G.prog.nchars = 0;
5354 }
5355}
5356
5357#if ENABLE_DC
5358static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5359{
5360 (void) radix;
5361 bb_putchar((char) num);
5362 G.prog.nchars += width;
5363}
5364#endif
5365
5366static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5367{
5368 size_t exp, pow;
5369
5370 bc_num_printNewline();
5371 bb_putchar(radix ? '.' : ' ');
5372 ++G.prog.nchars;
5373
5374 bc_num_printNewline();
5375 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5376 continue;
5377
5378 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5379 size_t dig;
5380 bc_num_printNewline();
5381 dig = num / pow;
5382 num -= dig * pow;
5383 bb_putchar(((char) dig) + '0');
5384 }
5385}
5386
5387static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5388{
5389 if (radix) {
5390 bc_num_printNewline();
5391 bb_putchar('.');
5392 G.prog.nchars += 1;
5393 }
5394
5395 bc_num_printNewline();
5396 bb_putchar(bb_hexdigits_upcase[num]);
5397 G.prog.nchars += width;
5398}
5399
5400static void bc_num_printDecimal(BcNum *n)
5401{
5402 size_t i, rdx = n->rdx - 1;
5403
5404 if (n->neg) bb_putchar('-');
5405 G.prog.nchars += n->neg;
5406
5407 for (i = n->len - 1; i < n->len; --i)
5408 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5409}
5410
5411static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5412{
5413 BcStatus s;
5414 BcVec stack;
5415 BcNum intp, fracp, digit, frac_len;
5416 unsigned long dig, *ptr;
5417 size_t i;
5418 bool radix;
5419
5420 if (n->len == 0) {
5421 print(0, width, false);
5422 RETURN_STATUS(BC_STATUS_SUCCESS);
5423 }
5424
5425 bc_vec_init(&stack, sizeof(long), NULL);
5426 bc_num_init(&intp, n->len);
5427 bc_num_init(&fracp, n->rdx);
5428 bc_num_init(&digit, width);
5429 bc_num_init(&frac_len, BC_NUM_INT(n));
5430 bc_num_copy(&intp, n);
5431 bc_num_one(&frac_len);
5432
5433 bc_num_truncate(&intp, intp.rdx);
5434 s = zbc_num_sub(n, &intp, &fracp, 0);
5435 if (s) goto err;
5436
5437 while (intp.len != 0) {
5438 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5439 if (s) goto err;
5440 s = zbc_num_ulong(&digit, &dig);
5441 if (s) goto err;
5442 bc_vec_push(&stack, &dig);
5443 }
5444
5445 for (i = 0; i < stack.len; ++i) {
5446 ptr = bc_vec_item_rev(&stack, i);
5447 print(*ptr, width, false);
5448 }
5449
5450 if (!n->rdx) goto err;
5451
5452 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5453 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5454 if (s) goto err;
5455 s = zbc_num_ulong(&fracp, &dig);
5456 if (s) goto err;
5457 bc_num_ulong2num(&intp, dig);
5458 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5459 if (s) goto err;
5460 print(dig, width, radix);
5461 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5462 if (s) goto err;
5463 }
5464
5465err:
5466 bc_num_free(&frac_len);
5467 bc_num_free(&digit);
5468 bc_num_free(&fracp);
5469 bc_num_free(&intp);
5470 bc_vec_free(&stack);
5471 RETURN_STATUS(s);
5472}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005473#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005474
5475static BC_STATUS zbc_num_printBase(BcNum *n)
5476{
5477 BcStatus s;
5478 size_t width, i;
5479 BcNumDigitOp print;
5480 bool neg = n->neg;
5481
5482 if (neg) {
5483 bb_putchar('-');
5484 G.prog.nchars++;
5485 }
5486
5487 n->neg = false;
5488
5489 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5490 width = 1;
5491 print = bc_num_printHex;
5492 }
5493 else {
5494 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5495 continue;
5496 print = bc_num_printDigits;
5497 }
5498
5499 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5500 n->neg = neg;
5501
5502 RETURN_STATUS(s);
5503}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005504#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005505
5506#if ENABLE_DC
5507static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5508{
5509 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5510}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005511#define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005512#endif
5513
5514static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5515{
5516 BcStatus s = BC_STATUS_SUCCESS;
5517
5518 bc_num_printNewline();
5519
5520 if (n->len == 0) {
5521 bb_putchar('0');
5522 ++G.prog.nchars;
5523 }
5524 else if (G.prog.ob_t == 10)
5525 bc_num_printDecimal(n);
5526 else
5527 s = zbc_num_printBase(n);
5528
5529 if (newline) {
5530 bb_putchar('\n');
5531 G.prog.nchars = 0;
5532 }
5533
5534 RETURN_STATUS(s);
5535}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005536#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005537
Denys Vlasenko29301232018-12-11 15:29:32 +01005538static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005539{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005540 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005541 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005542 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005543 bool pop = inst != BC_INST_PRINT;
5544
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005545 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005546 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005547
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005548 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005549 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005550 s = zbc_program_num(r, &num, false);
5551 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005552
5553 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005554 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005555 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005556 }
5557 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005558 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005559
5560 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005561 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005562
5563 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005564 for (;;) {
5565 char c = *str++;
5566 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005567 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005568 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005569 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005570 }
5571 }
5572 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005573 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005574 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005575 }
5576 }
5577
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005578 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005579
Denys Vlasenko29301232018-12-11 15:29:32 +01005580 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005581}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005582#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005583
Denys Vlasenko29301232018-12-11 15:29:32 +01005584static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005585{
5586 BcStatus s;
5587 BcResult res, *ptr;
5588 BcNum *num = NULL;
5589
Denys Vlasenko29301232018-12-11 15:29:32 +01005590 s = zbc_program_prep(&ptr, &num);
5591 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005592
5593 bc_num_init(&res.d.n, num->len);
5594 bc_num_copy(&res.d.n, num);
5595 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5596
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005597 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005598
Denys Vlasenko29301232018-12-11 15:29:32 +01005599 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005600}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005601#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005602
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005603static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005604{
5605 BcStatus s;
5606 BcResult *opd1, *opd2, res;
5607 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005608 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005609
Denys Vlasenko29301232018-12-11 15:29:32 +01005610 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005611 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005612
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005613 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005614
5615 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005616 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005617 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005618 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005619 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005620 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005621 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005622 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005623 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005624 break;
5625 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005626 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005627 break;
5628 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005629 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005630 break;
5631 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005632 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005633 break;
5634 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005635 cond = (cond > 0);
5636 break;
5637 default: // = case BC_INST_REL_NE:
5638 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005639 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005640 }
5641 }
5642
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005643 if (cond) bc_num_one(&res.d.n);
5644 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005645
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005646 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005647
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005648 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005649}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005650#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005651
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005652#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005653static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005654 bool push)
5655{
5656 BcNum n2;
5657 BcResult res;
5658
5659 memset(&n2, 0, sizeof(BcNum));
5660 n2.rdx = res.d.id.idx = r->d.id.idx;
5661 res.t = BC_RESULT_STR;
5662
5663 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005664 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005665 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005666 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005667 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005668 }
5669
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005670 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005671
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005672 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005673 bc_vec_push(v, &n2);
5674
Denys Vlasenko29301232018-12-11 15:29:32 +01005675 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005676}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005677#define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005678#endif // ENABLE_DC
5679
Denys Vlasenko29301232018-12-11 15:29:32 +01005680static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005681{
5682 BcStatus s;
5683 BcResult *ptr, r;
5684 BcVec *v;
5685 BcNum *n;
5686
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005687 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005688 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005689
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005690 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005691 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005692 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005693 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005694
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005695#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005696 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005697 RETURN_STATUS(bc_error_variable_is_wrong_type());
5698 if (ptr->t == BC_RESULT_STR)
5699 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005700#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005701
Denys Vlasenko29301232018-12-11 15:29:32 +01005702 s = zbc_program_num(ptr, &n, false);
5703 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005704
5705 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005706 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005707
5708 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005709 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005710 bc_num_copy(&r.d.n, n);
5711 }
5712 else {
5713 bc_array_init(&r.d.v, true);
5714 bc_array_copy(&r.d.v, (BcVec *) n);
5715 }
5716
5717 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005718 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005719
Denys Vlasenko29301232018-12-11 15:29:32 +01005720 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005721}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005722#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005723
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005724static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005725{
5726 BcStatus s;
5727 BcResult *left, *right, res;
5728 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005729 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5730
Denys Vlasenko29301232018-12-11 15:29:32 +01005731 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005732 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005733
5734 ib = left->t == BC_RESULT_IBASE;
5735 sc = left->t == BC_RESULT_SCALE;
5736
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005737#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005738
5739 if (right->t == BC_RESULT_STR) {
5740
5741 BcVec *v;
5742
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005743 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005744 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005745 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005746
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005747 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005748 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005749#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005750
5751 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005752 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005753 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005754 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005755 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005756
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005757#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005758 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005759 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005760
5761 if (assign)
5762 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005763 else {
5764 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005765 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005766 }
5767 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005768#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005769 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005770#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005771
5772 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005773 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005774 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5775 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5776 NULL, //can't happen //BC_RESULT_LAST
5777 NULL, //can't happen //BC_RESULT_CONSTANT
5778 NULL, //can't happen //BC_RESULT_ONE
5779 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005780 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005781 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005782 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005783
Denys Vlasenko29301232018-12-11 15:29:32 +01005784 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005785 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005786 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005787 if (sc) {
5788 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005789 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 }
5791 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005792 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005793 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005794 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005795 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005796 }
5797
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005798 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005799 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005800 if (!sc)
5801 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005802
5803 *ptr = (size_t) val;
5804 s = BC_STATUS_SUCCESS;
5805 }
5806
5807 bc_num_init(&res.d.n, l->len);
5808 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005809 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005810
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005811 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005812}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005813#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005814
Denys Vlasenko416ce762018-12-02 20:57:17 +01005815#if !ENABLE_DC
5816#define bc_program_pushVar(code, bgn, pop, copy) \
5817 bc_program_pushVar(code, bgn)
5818// for bc, 'pop' and 'copy' are always false
5819#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005820static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005821 bool pop, bool copy)
5822{
Gavin Howard01055ba2018-11-03 11:00:21 -06005823 BcResult r;
5824 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005825
5826 r.t = BC_RESULT_VAR;
5827 r.d.id.name = name;
5828
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005829#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01005830 {
5831 BcVec *v = bc_program_search(name, true);
5832 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005833
Denys Vlasenko416ce762018-12-02 20:57:17 +01005834 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005835
Denys Vlasenko416ce762018-12-02 20:57:17 +01005836 if (!BC_PROG_STACK(v, 2 - copy)) {
5837 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005838 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01005839 }
5840
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005842 name = NULL;
5843
5844 if (!BC_PROG_STR(num)) {
5845
5846 r.t = BC_RESULT_TEMP;
5847
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005848 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005849 bc_num_copy(&r.d.n, num);
5850 }
5851 else {
5852 r.t = BC_RESULT_STR;
5853 r.d.id.idx = num->rdx;
5854 }
5855
5856 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005857 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005858 }
5859#endif // ENABLE_DC
5860
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005861 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005862
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005863 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005864}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005865#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005866
Denys Vlasenko29301232018-12-11 15:29:32 +01005867static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005868 char inst)
5869{
5870 BcStatus s = BC_STATUS_SUCCESS;
5871 BcResult r;
5872 BcNum *num;
5873
5874 r.d.id.name = bc_program_name(code, bgn);
5875
5876 if (inst == BC_INST_ARRAY) {
5877 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005878 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 }
5880 else {
5881
5882 BcResult *operand;
5883 unsigned long temp;
5884
Denys Vlasenko29301232018-12-11 15:29:32 +01005885 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005886 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005887 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005888 if (s) goto err;
5889
5890 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005891 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005892 goto err;
5893 }
5894
5895 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005896 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897 }
5898
5899err:
5900 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005901 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005903#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005904
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005905#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005906static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005907{
5908 BcStatus s;
5909 BcResult *ptr, res, copy;
5910 BcNum *num = NULL;
5911 char inst2 = inst;
5912
Denys Vlasenko29301232018-12-11 15:29:32 +01005913 s = zbc_program_prep(&ptr, &num);
5914 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005915
5916 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5917 copy.t = BC_RESULT_TEMP;
5918 bc_num_init(&copy.d.n, num->len);
5919 bc_num_copy(&copy.d.n, num);
5920 }
5921
5922 res.t = BC_RESULT_ONE;
5923 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5924 BC_INST_ASSIGN_PLUS :
5925 BC_INST_ASSIGN_MINUS;
5926
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005927 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005928 s = zbc_program_assign(inst);
5929 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005930
5931 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005932 bc_vec_pop(&G.prog.results);
5933 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005934 }
5935
Denys Vlasenko29301232018-12-11 15:29:32 +01005936 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005937}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005938#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005939
Denys Vlasenko29301232018-12-11 15:29:32 +01005940static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005941{
Gavin Howard01055ba2018-11-03 11:00:21 -06005942 BcInstPtr ip;
5943 size_t i, nparams = bc_program_index(code, idx);
5944 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005945 BcId *a;
5946 BcResultData param;
5947 BcResult *arg;
5948
5949 ip.idx = 0;
5950 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005951 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005952
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005953 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005954 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005955 }
5956 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005957 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005958 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005959 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005960
5961 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005962 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005963
5964 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005965 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005966
5967 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005968 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005969
Denys Vlasenko29301232018-12-11 15:29:32 +01005970 s = zbc_program_copyToVar(a->name, a->idx);
5971 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005972 }
5973
5974 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005975 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005976
5977 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005978 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005979
5980 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005981 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005982 bc_vec_push(v, &param.n);
5983 }
5984 else {
5985 bc_array_init(&param.v, true);
5986 bc_vec_push(v, &param.v);
5987 }
5988 }
5989
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005990 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005991
Denys Vlasenko29301232018-12-11 15:29:32 +01005992 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005993}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005994#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005995
Denys Vlasenko29301232018-12-11 15:29:32 +01005996static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005997{
Gavin Howard01055ba2018-11-03 11:00:21 -06005998 BcResult res;
5999 BcFunc *f;
6000 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006001 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006002
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006003 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006004 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006005
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006006 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006007 res.t = BC_RESULT_TEMP;
6008
6009 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006010 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006011 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006012 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
Denys Vlasenko29301232018-12-11 15:29:32 +01006014 s = zbc_program_num(operand, &num, false);
6015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006016 bc_num_init(&res.d.n, num->len);
6017 bc_num_copy(&res.d.n, num);
6018 }
6019 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006020 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006021 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006022 }
6023
6024 // We need to pop arguments as well, so this takes that into account.
6025 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 BcVec *v;
6027 BcId *a = bc_vec_item(&f->autos, i);
6028
Denys Vlasenkodf515392018-12-02 19:27:48 +01006029 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006030 bc_vec_pop(v);
6031 }
6032
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006033 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6034 bc_vec_push(&G.prog.results, &res);
6035 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
Denys Vlasenko29301232018-12-11 15:29:32 +01006037 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006038}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006039#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006040#endif // ENABLE_BC
6041
6042static unsigned long bc_program_scale(BcNum *n)
6043{
6044 return (unsigned long) n->rdx;
6045}
6046
6047static unsigned long bc_program_len(BcNum *n)
6048{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006049 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006050
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006051 if (n->rdx != len) return len;
6052 for (;;) {
6053 if (len == 0) break;
6054 len--;
6055 if (n->num[len] != 0) break;
6056 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 return len;
6058}
6059
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006060static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006061{
6062 BcStatus s;
6063 BcResult *opnd;
6064 BcNum *num = NULL;
6065 BcResult res;
6066 bool len = inst == BC_INST_LENGTH;
6067
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006068 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006069 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006070 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenko29301232018-12-11 15:29:32 +01006072 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006073 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006074
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006075#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006076 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006077 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006078#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006079
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006080 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006081
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006082 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006083#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006084 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006085 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006086 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006087#endif
6088#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006090 char **str;
6091 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6092
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006093 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006094 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006095 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006096#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006098 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006099 }
6100
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006101 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006102
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006103 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006104}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006105#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006106
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006107#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006108static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006109{
6110 BcStatus s;
6111 BcResult *opd1, *opd2, res, res2;
6112 BcNum *n1, *n2 = NULL;
6113
Denys Vlasenko29301232018-12-11 15:29:32 +01006114 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006115 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006116
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006117 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006118 bc_num_init(&res2.d.n, n2->len);
6119
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006120 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006121 if (s) goto err;
6122
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006123 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006124 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006125 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006126
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128
6129err:
6130 bc_num_free(&res2.d.n);
6131 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006132 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006133}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006134#define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006135
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006136static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006137{
6138 BcStatus s;
6139 BcResult *r1, *r2, *r3, res;
6140 BcNum *n1, *n2, *n3;
6141
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006142 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006143 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006144 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006145 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006146
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006147 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006148 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006149 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006150 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006151 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006152
6153 // Make sure that the values have their pointers updated, if necessary.
6154 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6155
6156 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006157 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006158 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006159 }
6160
6161 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006162 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006163 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006164 }
6165 }
6166
6167 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006168 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006169 if (s) goto err;
6170
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006171 bc_vec_pop(&G.prog.results);
6172 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006173
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006174 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006175
6176err:
6177 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006178 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006179}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006180#define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006181
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006182static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006183{
Gavin Howard01055ba2018-11-03 11:00:21 -06006184 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006185 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006186
6187 res.t = BC_RESULT_TEMP;
6188
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006189 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006190 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006191 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006192}
6193
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006194static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006195{
6196 BcStatus s;
6197 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006198 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006199 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006200 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006201 unsigned long val;
6202
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006203 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006204 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006205 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006206
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006207 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006208 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006209 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006210
6211 if (BC_PROG_NUM(r, num)) {
6212
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006213 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006214 bc_num_copy(&n, num);
6215 bc_num_truncate(&n, n.rdx);
6216
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006217 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006218 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006219 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006220 if (s) goto num_err;
6221
6222 c = (char) val;
6223
6224 bc_num_free(&n);
6225 }
6226 else {
6227 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006228 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006229 c = str2[0];
6230 }
6231
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006232 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006233 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006234 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006235
6236 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006237 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006238
6239 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006240 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006241 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006242 len = idx;
6243 break;
6244 }
6245 }
6246
6247 free(str);
6248 }
6249 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006250 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006251
6252 res.t = BC_RESULT_STR;
6253 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006254 bc_vec_pop(&G.prog.results);
6255 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006256
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006257 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006258
6259num_err:
6260 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006261 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006262}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006263#define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
Denys Vlasenko29301232018-12-11 15:29:32 +01006265static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006266{
6267 BcStatus s;
6268 BcResult *r;
6269 BcNum *n = NULL;
6270 size_t idx;
6271 char *str;
6272
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006273 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006274 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006275 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006276
Denys Vlasenko29301232018-12-11 15:29:32 +01006277 s = zbc_program_num(r, &n, false);
6278 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279
6280 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006281 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006282 else {
6283 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006284 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006285 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006286 }
6287
Denys Vlasenko29301232018-12-11 15:29:32 +01006288 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006289}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006290#define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006291
Denys Vlasenko29301232018-12-11 15:29:32 +01006292static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006293{
6294 BcStatus s;
6295 BcResult *opnd;
6296 BcNum *num = NULL;
6297 unsigned long val;
6298
Denys Vlasenko29301232018-12-11 15:29:32 +01006299 s = zbc_program_prep(&opnd, &num);
6300 if (s) RETURN_STATUS(s);
6301 s = zbc_num_ulong(num, &val);
6302 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006303
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006304 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006305
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006306 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006307 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006308 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006309 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006310 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006311
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006312 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006313
Denys Vlasenko29301232018-12-11 15:29:32 +01006314 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006315}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006316#define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006317
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006318static BC_STATUS zbc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006319 bool cond)
6320{
6321 BcStatus s = BC_STATUS_SUCCESS;
6322 BcResult *r;
6323 char **str;
6324 BcFunc *f;
6325 BcParse prs;
6326 BcInstPtr ip;
6327 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006328
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006329 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006330 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006332 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
6334 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006335 BcNum *n = n; // for compiler
6336 bool exec;
6337 char *name;
6338 char *then_name = bc_program_name(code, bgn);
6339 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006340
6341 if (code[*bgn] == BC_PARSE_STREND)
6342 (*bgn) += 1;
6343 else
6344 else_name = bc_program_name(code, bgn);
6345
6346 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006347 name = then_name;
6348 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006349 exec = true;
6350 name = else_name;
6351 }
6352
6353 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006354 BcVec *v;
6355 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 n = bc_vec_top(v);
6357 }
6358
6359 free(then_name);
6360 free(else_name);
6361
6362 if (!exec) goto exit;
6363 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006364 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006365 goto exit;
6366 }
6367
6368 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006369 } else {
6370 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006371 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006372 } else if (r->t == BC_RESULT_VAR) {
6373 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006374 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006375 if (s || !BC_PROG_STR(n)) goto exit;
6376 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006377 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006378 goto exit;
6379 }
6380
6381 fidx = sidx + BC_PROG_REQ_FUNCS;
6382
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006383 str = bc_program_str(sidx);
6384 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006385
6386 if (f->code.len == 0) {
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006387 bc_parse_create(&prs, fidx);
Denys Vlasenkof86e9602018-12-14 17:01:56 +01006388 s = zbc_parse_text_init(&prs, *str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006389 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006390 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 if (s) goto err;
6392
6393 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006394 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006395 goto err;
6396 }
6397
6398 bc_parse_free(&prs);
6399 }
6400
6401 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006402 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 ip.func = fidx;
6404
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 bc_vec_pop(&G.prog.results);
6406 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006407
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006408 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006409
6410err:
6411 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006412 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006413 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006414exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006415 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006416 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006417}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006418#define zbc_program_execStr(...) (zbc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006419#endif // ENABLE_DC
6420
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006421static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006422{
Gavin Howard01055ba2018-11-03 11:00:21 -06006423 BcResult res;
6424 unsigned long val;
6425
6426 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6427 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006428 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006429 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006430 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006431 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006432 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006433
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006434 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006435 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006436 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006437}
6438
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006439static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006440{
Gavin Howard01055ba2018-11-03 11:00:21 -06006441 BcId entry, *entry_ptr;
6442 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006443 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006444
6445 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006446 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006447
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006448 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6449 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006450
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006451 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 *idx = entry_ptr->idx;
6453
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006454 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006455
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006456 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
6458 // We need to reset these, so the function can be repopulated.
6459 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006460 bc_vec_pop_all(&func->autos);
6461 bc_vec_pop_all(&func->code);
6462 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006463 }
6464 else {
6465 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006466 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006467 }
6468}
6469
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006470static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006471{
Gavin Howard01055ba2018-11-03 11:00:21 -06006472 BcResult r, *ptr;
6473 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006474 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006475 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006476 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006477
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006478 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006479 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 char inst = code[(ip->idx)++];
6481
Denys Vlasenko99b37622018-12-15 20:06:59 +01006482 dbg_exec("inst:%d", inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006483 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006484#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006485 case BC_INST_JUMP_ZERO: {
6486 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006487 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006488 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006489 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006490 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006491 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006492 if (!zero) {
6493 bc_program_index(code, &ip->idx);
6494 break;
6495 }
6496 // else: fall through
6497 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006498 case BC_INST_JUMP: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006499 size_t idx = bc_program_index(code, &ip->idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006500 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006501 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006502 ip->idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 break;
6504 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006505 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006506 dbg_exec("BC_INST_CALL:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006507 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006508 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006509 case BC_INST_INC_PRE:
6510 case BC_INST_DEC_PRE:
6511 case BC_INST_INC_POST:
6512 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006513 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006514 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006516 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006517 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006518 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006520 case BC_INST_RET:
6521 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006522 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006523 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006524 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006525 case BC_INST_BOOL_OR:
6526 case BC_INST_BOOL_AND:
6527#endif // ENABLE_BC
6528 case BC_INST_REL_EQ:
6529 case BC_INST_REL_LE:
6530 case BC_INST_REL_GE:
6531 case BC_INST_REL_NE:
6532 case BC_INST_REL_LT:
6533 case BC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006534 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006535 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006537 case BC_INST_READ:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006538 dbg_exec("BC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006539 s = zbc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006541 case BC_INST_VAR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006542 dbg_exec("BC_INST_VAR:");
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006543 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006545 case BC_INST_ARRAY_ELEM:
6546 case BC_INST_ARRAY:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006547 dbg_exec("BC_INST_ARRAY[_ELEM]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006548 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006549 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006550 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006551 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006552 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 case BC_INST_IBASE:
6555 case BC_INST_SCALE:
6556 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006557 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006558 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 case BC_INST_SCALE_FUNC:
6560 case BC_INST_LENGTH:
6561 case BC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006562 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006563 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006564 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006565 case BC_INST_NUM:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006566 dbg_exec("BC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006567 r.t = BC_RESULT_CONSTANT;
6568 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006569 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006570 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006571 case BC_INST_POP:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006572 dbg_exec("BC_INST_POP:");
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006573 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006574 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006575 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006576 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006577 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 case BC_INST_POP_EXEC:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006579 dbg_exec("BC_INST_POP_EXEC:");
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006580 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006582 case BC_INST_PRINT:
6583 case BC_INST_PRINT_POP:
6584 case BC_INST_PRINT_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006585 dbg_exec("BC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006586 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006587 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006588 case BC_INST_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006589 dbg_exec("BC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 r.t = BC_RESULT_STR;
6591 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006592 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006594 case BC_INST_POWER:
6595 case BC_INST_MULTIPLY:
6596 case BC_INST_DIVIDE:
6597 case BC_INST_MODULUS:
6598 case BC_INST_PLUS:
6599 case BC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006600 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006601 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006602 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006603 case BC_INST_BOOL_NOT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006604 dbg_exec("BC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006605 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006606 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006607 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006608 if (!bc_num_cmp(num, &G.prog.zero))
6609 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006610 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006611 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006612 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006613 case BC_INST_NEG:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006614 dbg_exec("BC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006615 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006616 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006617#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006618 case BC_INST_ASSIGN_POWER:
6619 case BC_INST_ASSIGN_MULTIPLY:
6620 case BC_INST_ASSIGN_DIVIDE:
6621 case BC_INST_ASSIGN_MODULUS:
6622 case BC_INST_ASSIGN_PLUS:
6623 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006624#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 case BC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006626 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006627 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006629#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006630 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006631 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006632 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006634 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006635 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006636 case BC_INST_EXECUTE:
6637 case BC_INST_EXEC_COND:
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006638 s = zbc_program_execStr(code, &ip->idx, inst == BC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06006639 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006640 case BC_INST_PRINT_STACK: {
6641 size_t idx;
6642 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006643 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006644 if (s) break;
6645 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006646 break;
6647 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006648 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006649 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006650 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006651 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006652 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006654 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006655 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006656 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006657 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006658 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006659 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006660 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006661 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006662 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006663 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006664 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006665 ptr = bc_vec_item_rev(&G.prog.results, 0);
6666 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006667 memcpy(&r, ptr, sizeof(BcResult));
6668 memcpy(ptr, ptr2, sizeof(BcResult));
6669 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006670 break;
6671 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006673 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006674 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006675 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006676 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006677 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006678 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006679 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006680 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006681 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006682 break;
6683 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006684 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006685 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006686 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006687 free(name);
6688 break;
6689 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006690 case BC_INST_QUIT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006691 dbg_exec("BC_INST_NEG:");
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006692 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006693 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006694 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006695 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006697 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006698 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006699#endif // ENABLE_DC
6700 }
6701
Denys Vlasenkod38af482018-12-04 19:11:02 +01006702 if (s || G_interrupt) {
6703 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006704 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006705 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006706
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006707 fflush_and_check();
6708
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006710 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006711 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006712 code = func->code.v;
6713 }
6714
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006715 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006716}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006717#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006718
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006719static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006720{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006721 char *lenv;
6722 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006723
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006724 lenv = getenv(var);
6725 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006726 if (!lenv) return len;
6727
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006728 len = bb_strtou(lenv, NULL, 10) - 1;
6729 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 len = BC_NUM_PRINT_WIDTH;
6731
6732 return len;
6733}
6734
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006735static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006736{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006737 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006738
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006739 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
6740 s = zbc_parse_text_init(&G.prs, text);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006741 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006742
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006743 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006744 dbg_lex("%s:%d G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenkoec603182018-12-17 10:34:02 +01006745 s = zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006746 if (s) RETURN_STATUS(s);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006747 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006748 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006749 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006750 break;
6751 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006752 }
6753
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006754 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006755 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006756}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006757#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006758
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006759static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006760{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006761 // So far bc/dc have no way to include a file from another file,
6762 // therefore we know G.prog.file == NULL on entry
6763 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006764 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006765
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006766 G.prog.file = filename;
6767 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006768 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006769
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006770 do {
6771 s = zbc_vm_process("");
6772 // We do not stop looping on errors here if reading stdin.
6773 // Example: start interactive bc and enter "return".
6774 // It should say "'return' not in a function"
6775 // but should not exit.
6776 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006777 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006778 RETURN_STATUS(s);
6779}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006780#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006781
6782static BC_STATUS zbc_vm_file(const char *file)
6783{
6784 BcStatus s;
6785 FILE *fp;
6786
6787 fp = xfopen_for_read(file);
6788 s = zbc_vm_execute_FILE(fp, file);
6789 fclose(fp);
6790
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006791 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006792}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006793#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006794
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006795#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006796static void bc_vm_info(void)
6797{
6798 printf("%s "BB_VER"\n"
6799 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6800 , applet_name);
6801}
6802
6803static void bc_args(char **argv)
6804{
6805 unsigned opts;
6806 int i;
6807
6808 GETOPT_RESET();
6809#if ENABLE_FEATURE_BC_LONG_OPTIONS
6810 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6811 "warn\0" No_argument "w"
6812 "version\0" No_argument "v"
6813 "standard\0" No_argument "s"
6814 "quiet\0" No_argument "q"
6815 "mathlib\0" No_argument "l"
6816 "interactive\0" No_argument "i"
6817 );
6818#else
6819 opts = option_mask32 |= getopt32(argv, "wvsqli");
6820#endif
6821 if (getenv("POSIXLY_CORRECT"))
6822 option_mask32 |= BC_FLAG_S;
6823
6824 if (opts & BC_FLAG_V) {
6825 bc_vm_info();
6826 exit(0);
6827 }
6828
6829 for (i = optind; argv[i]; ++i)
6830 bc_vec_push(&G.files, argv + i);
6831}
6832
6833static void bc_vm_envArgs(void)
6834{
6835 BcVec v;
6836 char *buf;
6837 char *env_args = getenv("BC_ENV_ARGS");
6838
6839 if (!env_args) return;
6840
6841 G.env_args = xstrdup(env_args);
6842 buf = G.env_args;
6843
6844 bc_vec_init(&v, sizeof(char *), NULL);
6845
6846 while (*(buf = skip_whitespace(buf)) != '\0') {
6847 bc_vec_push(&v, &buf);
6848 buf = skip_non_whitespace(buf);
6849 if (!*buf)
6850 break;
6851 *buf++ = '\0';
6852 }
6853
6854 // NULL terminate, and pass argv[] so that first arg is argv[1]
6855 if (sizeof(int) == sizeof(char*)) {
6856 bc_vec_push(&v, &const_int_0);
6857 } else {
6858 static char *const nullptr = NULL;
6859 bc_vec_push(&v, &nullptr);
6860 }
6861 bc_args(((char **)v.v) - 1);
6862
6863 bc_vec_free(&v);
6864}
6865
6866static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006867 "scale=20"
6868"\n" "define e(x){"
6869"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006870////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6871//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6872//e(-.998896): GNU:.36828580434569428695
6873// above code:.36828580434569428696
6874// actual value:.3682858043456942869594...
6875// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006876"\n" "b=ibase"
6877"\n" "ibase=A"
6878"\n" "if(x<0){"
6879"\n" "n=1"
6880"\n" "x=-x"
6881"\n" "}"
6882"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006883"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006884"\n" "scale=scale(x)+1"
6885"\n" "while(x>1){"
6886"\n" "d+=1"
6887"\n" "x/=2"
6888"\n" "scale+=1"
6889"\n" "}"
6890"\n" "scale=r"
6891"\n" "r=x+1"
6892"\n" "p=x"
6893"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006894"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006895"\n" "p*=x"
6896"\n" "f*=i"
6897"\n" "v=p/f"
6898"\n" "r+=v"
6899"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006900"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006901"\n" "scale=s"
6902"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006903"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006904"\n" "return(r/1)"
6905"\n" "}"
6906"\n" "define l(x){"
6907"\n" "auto b,s,r,p,a,q,i,v"
6908"\n" "b=ibase"
6909"\n" "ibase=A"
6910"\n" "if(x<=0){"
6911"\n" "r=(1-10^scale)/1"
6912"\n" "ibase=b"
6913"\n" "return(r)"
6914"\n" "}"
6915"\n" "s=scale"
6916"\n" "scale+=6"
6917"\n" "p=2"
6918"\n" "while(x>=2){"
6919"\n" "p*=2"
6920"\n" "x=sqrt(x)"
6921"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006922"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006923"\n" "p*=2"
6924"\n" "x=sqrt(x)"
6925"\n" "}"
6926"\n" "r=a=(x-1)/(x+1)"
6927"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006928"\n" "v=1"
6929"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006930"\n" "a*=q"
6931"\n" "v=a/i"
6932"\n" "r+=v"
6933"\n" "}"
6934"\n" "r*=p"
6935"\n" "scale=s"
6936"\n" "ibase=b"
6937"\n" "return(r/1)"
6938"\n" "}"
6939"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006940"\n" "auto b,s,r,a,q,i"
6941"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006942"\n" "b=ibase"
6943"\n" "ibase=A"
6944"\n" "s=scale"
6945"\n" "scale=1.1*s+2"
6946"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006947"\n" "scale=0"
6948"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006949"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006950"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006951"\n" "scale=s+2"
6952"\n" "r=a=x"
6953"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006954"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006955"\n" "a*=q/(i*(i-1))"
6956"\n" "r+=a"
6957"\n" "}"
6958"\n" "scale=s"
6959"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006960"\n" "return(r/1)"
6961"\n" "}"
6962"\n" "define c(x){"
6963"\n" "auto b,s"
6964"\n" "b=ibase"
6965"\n" "ibase=A"
6966"\n" "s=scale"
6967"\n" "scale*=1.2"
6968"\n" "x=s(2*a(1)+x)"
6969"\n" "scale=s"
6970"\n" "ibase=b"
6971"\n" "return(x/1)"
6972"\n" "}"
6973"\n" "define a(x){"
6974"\n" "auto b,s,r,n,a,m,t,f,i,u"
6975"\n" "b=ibase"
6976"\n" "ibase=A"
6977"\n" "n=1"
6978"\n" "if(x<0){"
6979"\n" "n=-1"
6980"\n" "x=-x"
6981"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006982"\n" "if(scale<65){"
6983"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
6984"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006985"\n" "}"
6986"\n" "s=scale"
6987"\n" "if(x>.2){"
6988"\n" "scale+=5"
6989"\n" "a=a(.2)"
6990"\n" "}"
6991"\n" "scale=s+3"
6992"\n" "while(x>.2){"
6993"\n" "m+=1"
6994"\n" "x=(x-.2)/(1+.2*x)"
6995"\n" "}"
6996"\n" "r=u=x"
6997"\n" "f=-x*x"
6998"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006999"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007000"\n" "u*=f"
7001"\n" "t=u/i"
7002"\n" "r+=t"
7003"\n" "}"
7004"\n" "scale=s"
7005"\n" "ibase=b"
7006"\n" "return((m*a+r)/n)"
7007"\n" "}"
7008"\n" "define j(n,x){"
7009"\n" "auto b,s,o,a,i,v,f"
7010"\n" "b=ibase"
7011"\n" "ibase=A"
7012"\n" "s=scale"
7013"\n" "scale=0"
7014"\n" "n/=1"
7015"\n" "if(n<0){"
7016"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007017"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007018"\n" "}"
7019"\n" "a=1"
7020"\n" "for(i=2;i<=n;++i)a*=i"
7021"\n" "scale=1.5*s"
7022"\n" "a=(x^n)/2^n/a"
7023"\n" "r=v=1"
7024"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007025"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007026"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007027"\n" "v=v*f/i/(n+i)"
7028"\n" "r+=v"
7029"\n" "}"
7030"\n" "scale=s"
7031"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007032"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007033"\n" "return(a*r/1)"
7034"\n" "}"
7035};
7036#endif // ENABLE_BC
7037
Denys Vlasenko19f11072018-12-12 22:48:19 +01007038static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007039{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007040 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007041 size_t i;
7042
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007043#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007044 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007045 // We know that internal library is not buggy,
7046 // thus error checking is normally disabled.
7047# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007048 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007049 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007050 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007051 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007052#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007053
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007054 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007055 for (i = 0; !s && i < G.files.len; ++i)
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007056 s = zbc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007057 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7058 // Debug config, non-interactive mode:
7059 // return all the way back to main.
7060 // Non-debug builds do not come here, they exit.
Denys Vlasenko19f11072018-12-12 22:48:19 +01007061 RETURN_STATUS(s);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007062 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007063
Denys Vlasenko91cde952018-12-10 20:56:08 +01007064 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007065 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007066
Denys Vlasenko19f11072018-12-12 22:48:19 +01007067 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007068}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007069#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007070
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007071#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007072static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007073{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007074 bc_num_free(&G.prog.ib);
7075 bc_num_free(&G.prog.ob);
7076 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007077# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007078 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007079# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007080 bc_vec_free(&G.prog.fns);
7081 bc_vec_free(&G.prog.fn_map);
7082 bc_vec_free(&G.prog.vars);
7083 bc_vec_free(&G.prog.var_map);
7084 bc_vec_free(&G.prog.arrs);
7085 bc_vec_free(&G.prog.arr_map);
7086 bc_vec_free(&G.prog.strs);
7087 bc_vec_free(&G.prog.consts);
7088 bc_vec_free(&G.prog.results);
7089 bc_vec_free(&G.prog.stack);
7090 bc_num_free(&G.prog.last);
7091 bc_num_free(&G.prog.zero);
7092 bc_num_free(&G.prog.one);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007093 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007094}
7095
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007096static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007097{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007098 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007099 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007100 bc_parse_free(&G.prs);
7101 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007102}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007103#endif
7104
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007105static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007106{
7107 size_t idx;
7108 BcInstPtr ip;
7109
Denys Vlasenko82269122018-12-14 16:30:56 +01007110 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007111 memset(&ip, 0, sizeof(BcInstPtr));
7112
Denys Vlasenko82269122018-12-14 16:30:56 +01007113 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007114 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007115 bc_num_ten(&G.prog.ib);
7116 G.prog.ib_t = 10;
7117
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007118 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007119 bc_num_ten(&G.prog.ob);
7120 G.prog.ob_t = 10;
7121
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007122 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007123 bc_num_ten(&G.prog.hexb);
7124 G.prog.hexb.num[0] = 6;
7125
7126#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007127 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007128 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7129#endif
7130
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007131 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007132 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007133
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007134 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007135 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007136
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007137 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007138 bc_num_one(&G.prog.one);
7139
7140 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007141 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007142
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007143 bc_program_addFunc(xstrdup("(main)"), &idx);
7144 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007145
7146 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007147 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007148
7149 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007150 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007151
7152 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7153 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7154 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7155 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7156 bc_vec_push(&G.prog.stack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007157
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007158 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007159}
Gavin Howard01055ba2018-11-03 11:00:21 -06007160
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007161static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007162{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007163#if ENABLE_FEATURE_EDITING
7164 G.line_input_state = new_line_input_t(DO_HISTORY);
7165#endif
7166 G.prog.len = bc_vm_envLen(env_len);
7167
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007168 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007169 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007170 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007171 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007172
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007173//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7174//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007175 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007176#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007177 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007178 // With SA_RESTART, most system calls will restart
7179 // (IOW: they won't fail with EINTR).
7180 // In particular, this means ^C won't cause
7181 // stdout to get into "error state" if SIGINT hits
7182 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007183 //
7184 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007185 // will only be handled after [Enter] since read()
7186 // from stdin is not interrupted by ^C either,
7187 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007188 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007189 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7190
7191 // Without SA_RESTART, this exhibits a bug:
7192 // "while (1) print 1" and try ^C-ing it.
7193 // Intermittently, instead of returning to input line,
7194 // you'll get "output error: Interrupted system call"
7195 // and exit.
7196 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007197#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007198 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007199 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007200 return 0; // "not a tty"
7201}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007202
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007203static BcStatus bc_vm_run(void)
7204{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007205 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007206#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007207 if (G_exiting) // it was actually "halt" or "quit"
7208 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007209 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007210# if ENABLE_FEATURE_EDITING
7211 free_line_input_t(G.line_input_state);
7212# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007213 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007214#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007215 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007216 return st;
7217}
7218
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007219#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007220int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007221int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007222{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007223 int is_tty;
7224
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007225 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007226
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007227 is_tty = bc_vm_init("BC_LINE_LENGTH");
7228
7229 bc_args(argv);
7230
7231 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7232 bc_vm_info();
7233
7234 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007235}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007236#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007237
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007238#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007239int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007240int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007241{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007242 int noscript;
7243
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007244 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007245
7246 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7247 // 1 char wider than bc from the same package.
7248 // Both default width, and xC_LINE_LENGTH=N are wider:
7249 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007250 // |1234\ |
7251 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007252 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007253 // |123\ |
7254 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007255 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007256 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007257
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007258 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7259 noscript = BC_FLAG_I;
7260 for (;;) {
7261 int n = getopt(argc, argv, "e:f:x");
7262 if (n <= 0)
7263 break;
7264 switch (n) {
7265 case 'e':
7266 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007267 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007268 if (n) return n;
7269 break;
7270 case 'f':
7271 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007272 n = zbc_vm_file(optarg);
7273 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007274 break;
7275 case 'x':
7276 option_mask32 |= DC_FLAG_X;
7277 break;
7278 default:
7279 bb_show_usage();
7280 }
7281 }
7282 argv += optind;
7283
7284 while (*argv) {
7285 noscript = 0;
7286 bc_vec_push(&G.files, argv++);
7287 }
7288
7289 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7290
7291 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007292}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007293#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007294
7295#endif // not DC_SMALL