blob: 50e5457b9a24ab5d88835c01a85d7d0e77937c36 [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 Vlasenko09fe0aa2018-12-18 16:32:25 +0100113//usage: "[-sqlw] 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 Vlasenkob80d7aa2018-12-19 12:35:27 +0100170#define DEBUG_LEXER 0
171#define DEBUG_COMPILE 0
172#define DEBUG_EXEC 0
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100173
174#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100175static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100176#define dbg_lex(...) \
177 do { \
178 fprintf(stderr, "%*s", lex_indent, ""); \
179 bb_error_msg(__VA_ARGS__); \
180 } while (0)
181#define dbg_lex_enter(...) \
182 do { \
183 dbg_lex(__VA_ARGS__); \
184 lex_indent++; \
185 } while (0)
186#define dbg_lex_done(...) \
187 do { \
188 lex_indent--; \
189 dbg_lex(__VA_ARGS__); \
190 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100191#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100192# define dbg_lex(...) ((void)0)
193# define dbg_lex_enter(...) ((void)0)
194# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100195#endif
196
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100197#if DEBUG_COMPILE
198# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
199#else
200# define dbg_compile(...) ((void)0)
201#endif
202
Denys Vlasenko99b37622018-12-15 20:06:59 +0100203#if DEBUG_EXEC
204# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
205#else
206# define dbg_exec(...) ((void)0)
207#endif
208
Gavin Howard01055ba2018-11-03 11:00:21 -0600209typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100210 BC_STATUS_SUCCESS = 0,
211 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100212 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600213} BcStatus;
214
Gavin Howard01055ba2018-11-03 11:00:21 -0600215#define BC_VEC_INVALID_IDX ((size_t) -1)
216#define BC_VEC_START_CAP (1 << 5)
217
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100218typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600219
220typedef struct BcVec {
221 char *v;
222 size_t len;
223 size_t cap;
224 size_t size;
225 BcVecFree dtor;
226} BcVec;
227
Gavin Howard01055ba2018-11-03 11:00:21 -0600228typedef signed char BcDig;
229
230typedef struct BcNum {
231 BcDig *restrict num;
232 size_t rdx;
233 size_t len;
234 size_t cap;
235 bool neg;
236} BcNum;
237
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100238#define BC_NUM_MAX_IBASE ((unsigned long) 16)
239// larger value might speed up BIGNUM calculations a bit:
240#define BC_NUM_DEF_SIZE (16)
241#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600242
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100243#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Gavin Howard01055ba2018-11-03 11:00:21 -0600245typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100246#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600247 BC_INST_INC_PRE,
248 BC_INST_DEC_PRE,
249 BC_INST_INC_POST,
250 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100251#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600252
253 BC_INST_NEG,
254
255 BC_INST_POWER,
256 BC_INST_MULTIPLY,
257 BC_INST_DIVIDE,
258 BC_INST_MODULUS,
259 BC_INST_PLUS,
260 BC_INST_MINUS,
261
262 BC_INST_REL_EQ,
263 BC_INST_REL_LE,
264 BC_INST_REL_GE,
265 BC_INST_REL_NE,
266 BC_INST_REL_LT,
267 BC_INST_REL_GT,
268
269 BC_INST_BOOL_NOT,
270 BC_INST_BOOL_OR,
271 BC_INST_BOOL_AND,
272
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100273#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600274 BC_INST_ASSIGN_POWER,
275 BC_INST_ASSIGN_MULTIPLY,
276 BC_INST_ASSIGN_DIVIDE,
277 BC_INST_ASSIGN_MODULUS,
278 BC_INST_ASSIGN_PLUS,
279 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600281 BC_INST_ASSIGN,
282
283 BC_INST_NUM,
284 BC_INST_VAR,
285 BC_INST_ARRAY_ELEM,
286 BC_INST_ARRAY,
287
288 BC_INST_SCALE_FUNC,
289 BC_INST_IBASE,
290 BC_INST_SCALE,
291 BC_INST_LAST,
292 BC_INST_LENGTH,
293 BC_INST_READ,
294 BC_INST_OBASE,
295 BC_INST_SQRT,
296
297 BC_INST_PRINT,
298 BC_INST_PRINT_POP,
299 BC_INST_STR,
300 BC_INST_PRINT_STR,
301
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100302#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600303 BC_INST_JUMP,
304 BC_INST_JUMP_ZERO,
305
306 BC_INST_CALL,
307
308 BC_INST_RET,
309 BC_INST_RET0,
310
311 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100312#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600313
314 BC_INST_POP,
315 BC_INST_POP_EXEC,
316
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100317#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600318 BC_INST_MODEXP,
319 BC_INST_DIVMOD,
320
321 BC_INST_EXECUTE,
322 BC_INST_EXEC_COND,
323
324 BC_INST_ASCIIFY,
325 BC_INST_PRINT_STREAM,
326
327 BC_INST_PRINT_STACK,
328 BC_INST_CLEAR_STACK,
329 BC_INST_STACK_LEN,
330 BC_INST_DUPLICATE,
331 BC_INST_SWAP,
332
333 BC_INST_LOAD,
334 BC_INST_PUSH_VAR,
335 BC_INST_PUSH_TO_VAR,
336
337 BC_INST_QUIT,
338 BC_INST_NQUIT,
339
340 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100341#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600342} BcInst;
343
344typedef struct BcId {
345 char *name;
346 size_t idx;
347} BcId;
348
349typedef struct BcFunc {
350 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100351 IF_BC(BcVec labels;)
352 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100353 IF_BC(BcVec strs;)
354 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100355 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600356} BcFunc;
357
358typedef enum BcResultType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600359 BC_RESULT_TEMP,
360
361 BC_RESULT_VAR,
362 BC_RESULT_ARRAY_ELEM,
363 BC_RESULT_ARRAY,
364
365 BC_RESULT_STR,
366
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100367 //code uses "inst - BC_INST_IBASE + BC_RESULT_IBASE" construct,
368 BC_RESULT_IBASE, // relative order should match for: BC_INST_IBASE
369 BC_RESULT_SCALE, // relative order should match for: BC_INST_SCALE
370 BC_RESULT_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600371 BC_RESULT_CONSTANT,
372 BC_RESULT_ONE,
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100373 BC_RESULT_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600374} BcResultType;
375
376typedef union BcResultData {
377 BcNum n;
378 BcVec v;
379 BcId id;
380} BcResultData;
381
382typedef struct BcResult {
383 BcResultType t;
384 BcResultData d;
385} BcResult;
386
387typedef struct BcInstPtr {
388 size_t func;
389 size_t idx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100390 IF_BC(size_t len;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600391} BcInstPtr;
392
Gavin Howard01055ba2018-11-03 11:00:21 -0600393// BC_LEX_NEG is not used in lexing; it is only for parsing.
394typedef enum BcLexType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600395 BC_LEX_EOF,
396 BC_LEX_INVALID,
397
398 BC_LEX_OP_INC,
399 BC_LEX_OP_DEC,
400
401 BC_LEX_NEG,
402
403 BC_LEX_OP_POWER,
404 BC_LEX_OP_MULTIPLY,
405 BC_LEX_OP_DIVIDE,
406 BC_LEX_OP_MODULUS,
407 BC_LEX_OP_PLUS,
408 BC_LEX_OP_MINUS,
409
410 BC_LEX_OP_REL_EQ,
411 BC_LEX_OP_REL_LE,
412 BC_LEX_OP_REL_GE,
413 BC_LEX_OP_REL_NE,
414 BC_LEX_OP_REL_LT,
415 BC_LEX_OP_REL_GT,
416
417 BC_LEX_OP_BOOL_NOT,
418 BC_LEX_OP_BOOL_OR,
419 BC_LEX_OP_BOOL_AND,
420
421 BC_LEX_OP_ASSIGN_POWER,
422 BC_LEX_OP_ASSIGN_MULTIPLY,
423 BC_LEX_OP_ASSIGN_DIVIDE,
424 BC_LEX_OP_ASSIGN_MODULUS,
425 BC_LEX_OP_ASSIGN_PLUS,
426 BC_LEX_OP_ASSIGN_MINUS,
427 BC_LEX_OP_ASSIGN,
428
429 BC_LEX_NLINE,
430 BC_LEX_WHITESPACE,
431
432 BC_LEX_LPAREN,
433 BC_LEX_RPAREN,
434
435 BC_LEX_LBRACKET,
436 BC_LEX_COMMA,
437 BC_LEX_RBRACKET,
438
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100439 BC_LEX_LBRACE, // '{' is 0x7B, '}' is 0x7D,
Gavin Howard01055ba2018-11-03 11:00:21 -0600440 BC_LEX_SCOLON,
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100441 BC_LEX_RBRACE, // should be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600442
443 BC_LEX_STR,
444 BC_LEX_NAME,
445 BC_LEX_NUMBER,
446
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100447 BC_LEX_KEY_1st_keyword,
448 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600449 BC_LEX_KEY_BREAK,
450 BC_LEX_KEY_CONTINUE,
451 BC_LEX_KEY_DEFINE,
452 BC_LEX_KEY_ELSE,
453 BC_LEX_KEY_FOR,
454 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100455 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
456 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100458 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_LENGTH,
460 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100461 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600462 BC_LEX_KEY_PRINT,
463 BC_LEX_KEY_QUIT,
464 BC_LEX_KEY_READ,
465 BC_LEX_KEY_RETURN,
466 BC_LEX_KEY_SCALE,
467 BC_LEX_KEY_SQRT,
468 BC_LEX_KEY_WHILE,
469
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100470#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600471 BC_LEX_EQ_NO_REG,
472 BC_LEX_OP_MODEXP,
473 BC_LEX_OP_DIVMOD,
474
475 BC_LEX_COLON,
476 BC_LEX_ELSE,
477 BC_LEX_EXECUTE,
478 BC_LEX_PRINT_STACK,
479 BC_LEX_CLEAR_STACK,
480 BC_LEX_STACK_LEVEL,
481 BC_LEX_DUPLICATE,
482 BC_LEX_SWAP,
483 BC_LEX_POP,
484
485 BC_LEX_ASCIIFY,
486 BC_LEX_PRINT_STREAM,
487
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100488 // code uses "t - BC_LEX_STORE_IBASE + BC_INST_IBASE" construct,
489 BC_LEX_STORE_IBASE, // relative order should match for: BC_INST_IBASE
490 BC_LEX_STORE_SCALE, // relative order should match for: BC_INST_SCALE
Gavin Howard01055ba2018-11-03 11:00:21 -0600491 BC_LEX_LOAD,
492 BC_LEX_LOAD_POP,
493 BC_LEX_STORE_PUSH,
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100494 BC_LEX_STORE_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600495 BC_LEX_PRINT_POP,
496 BC_LEX_NQUIT,
497 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100498#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600499} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100500// must match order of BC_LEX_KEY_foo etc above
501#if ENABLE_BC
502struct BcLexKeyword {
503 char name8[8];
504};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100505#define BC_LEX_KW_ENTRY(a, b) \
506 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100507static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100508 BC_LEX_KW_ENTRY("auto" , 1), // 0
509 BC_LEX_KW_ENTRY("break" , 1), // 1
510 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
511 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100512 BC_LEX_KW_ENTRY("else" , 0), // 4
513 BC_LEX_KW_ENTRY("for" , 1), // 5
514 BC_LEX_KW_ENTRY("halt" , 0), // 6
515 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100516 BC_LEX_KW_ENTRY("if" , 1), // 8
517 BC_LEX_KW_ENTRY("last" , 0), // 9
518 BC_LEX_KW_ENTRY("length" , 1), // 10
519 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520 BC_LEX_KW_ENTRY("obase" , 1), // 12
521 BC_LEX_KW_ENTRY("print" , 0), // 13
522 BC_LEX_KW_ENTRY("quit" , 1), // 14
523 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100524 BC_LEX_KW_ENTRY("return" , 1), // 16
525 BC_LEX_KW_ENTRY("scale" , 1), // 17
526 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
527 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100528};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100529#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100530#define STRING_if (bc_lex_kws[8].name8)
531#define STRING_else (bc_lex_kws[4].name8)
532#define STRING_while (bc_lex_kws[19].name8)
533#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100534enum {
535 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100536 | (1 << 0) // 0
537 | (1 << 1) // 1
538 | (0 << 2) // 2
539 | (1 << 3) // 3
540 | (0 << 4) // 4
541 | (1 << 5) // 5
542 | (0 << 6) // 6
543 | (1 << 7) // 7
544 | (1 << 8) // 8
545 | (0 << 9) // 9
546 | (1 << 10) // 10
547 | (0 << 11) // 11
548 | (1 << 12) // 12
549 | (0 << 13) // 13
550 | (1 << 14) // 14
551 | (0 << 15) // 15
552 | (1 << 16) // 16
553 | (1 << 17) // 17
554 | (1 << 18) // 18
555 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100556};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100557#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100558
559// This is a bit array that corresponds to token types. An entry is
560// true if the token is valid in an expression, false otherwise.
561// Used to figure out when expr parsing should stop *without error message*
562// - 0 element indicates this condition. 1 means "this token is to be eaten
563// as part of the expression", token can them still be determined to be invalid
564// by later processing.
565enum {
566#define EXBITS(a,b,c,d,e,f,g,h) \
567 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
568 BC_PARSE_EXPRS_BITS = 0
569 + (EXBITS(0,0,1,1,1,1,1,1) << (0*8)) // 0: eof inval ++ -- - ^ * /
570 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: % + - == <= >= != <
571 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: > ! || && ^= *= /= %=
572 + (EXBITS(1,1,1,0,0,1,1,0) << (3*8)) // 24: += -= = NL WS ( ) [
573 + (EXBITS(0,0,0,0,0,0,1,1) << (4*8)) // 32: , ] { ; } STR NAME NUM
574 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
575 + (EXBITS(0,1,1,1,1,0,0,1) << (6*8)) // 48: if last len limits obase print quit read - bug, why "limits" is allowed?
576 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
577#undef EXBITS
578};
579static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
580{
581#if ULONG_MAX > 0xffffffff
582 // 64-bit version (will not work correctly for 32-bit longs!)
583 return BC_PARSE_EXPRS_BITS & (1UL << i);
584#else
585 // 32-bit version
586 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
587 if (i >= 32) {
588 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
589 i &= 31;
590 }
591 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100592#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100593}
594
595// This is an array of data for operators that correspond to
596// [BC_LEX_OP_INC...BC_LEX_OP_ASSIGN] token types.
597static const uint8_t bc_parse_ops[] = {
598#define OP(p,l) ((int)(l) * 0x10 + (p))
599 OP(0, false), OP( 0, false ), // inc dec
600 OP(1, false), // neg
601 OP(2, false), // pow
602 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
603 OP(4, true ), OP( 4, true ), // + -
604 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
605 OP(1, false), // not
606 OP(7, true ), OP( 7, true ), // or and
607 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
608 OP(5, false), OP( 5, false ), // -= =
609#undef OP
610};
611#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
612#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
613#endif // ENABLE_BC
614
615#if ENABLE_DC
616static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
617int8_t
618dc_parse_insts[] = {
619 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
620 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
621 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
622 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
623 BC_INST_INVALID, BC_INST_INVALID,
624 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
625 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
626 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
627 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
628 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
629 BC_INST_INVALID, BC_INST_INVALID,
630 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
631 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
632 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
633 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
634 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
635 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
636 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
637 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
638 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
639 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
640 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
641 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
642};
643#endif // ENABLE_DC
644
Gavin Howard01055ba2018-11-03 11:00:21 -0600645typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600646 const char *buf;
647 size_t i;
648 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600649 size_t len;
650 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600651 struct {
652 BcLexType t;
653 BcLexType last;
654 BcVec v;
655 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600656} BcLex;
657
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100658#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600659
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100660#define BC_PARSE_REL (1 << 0)
661#define BC_PARSE_PRINT (1 << 1)
662#define BC_PARSE_NOCALL (1 << 2)
663#define BC_PARSE_NOREAD (1 << 3)
664#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600665
Gavin Howard01055ba2018-11-03 11:00:21 -0600666typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600667 BcLex l;
668
Denys Vlasenko503faf92018-12-20 16:24:18 +0100669 IF_BC(BcVec exits;)
670 IF_BC(BcVec conds;)
671 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600672
Gavin Howard01055ba2018-11-03 11:00:21 -0600673 BcFunc *func;
674 size_t fidx;
675
Denys Vlasenko503faf92018-12-20 16:24:18 +0100676 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600677} BcParse;
678
Gavin Howard01055ba2018-11-03 11:00:21 -0600679typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600680 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100681 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600682
Denys Vlasenko503faf92018-12-20 16:24:18 +0100683 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600684 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600685 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600686
Gavin Howard01055ba2018-11-03 11:00:21 -0600687 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100688 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600689
690 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100691 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600692
693 BcVec vars;
694 BcVec var_map;
695
696 BcVec arrs;
697 BcVec arr_map;
698
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100699 IF_DC(BcVec strs;)
700 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600701
702 const char *file;
703
Gavin Howard01055ba2018-11-03 11:00:21 -0600704 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100705 IF_BC(BcNum one;)
706 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600707} BcProgram;
708
Gavin Howard01055ba2018-11-03 11:00:21 -0600709#define BC_PROG_MAIN (0)
710#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100711#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600712#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100713#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600714
715#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
716#define BC_PROG_NUM(r, n) \
717 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
718
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100719#define BC_FLAG_W (1 << 0)
720#define BC_FLAG_V (1 << 1)
721#define BC_FLAG_S (1 << 2)
722#define BC_FLAG_Q (1 << 3)
723#define BC_FLAG_L (1 << 4)
724#define BC_FLAG_I (1 << 5)
725#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600726
727#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
728#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
729
Denys Vlasenko64074a12018-12-07 15:50:14 +0100730#define BC_MAX_OBASE ((unsigned) 999)
731#define BC_MAX_DIM ((unsigned) INT_MAX)
732#define BC_MAX_SCALE ((unsigned) UINT_MAX)
733#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
734#define BC_MAX_NUM BC_MAX_STRING
735// Unused apart from "limits" message. Just show a "biggish number" there.
736//#define BC_MAX_NAME BC_MAX_STRING
737//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
738//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
739#define BC_MAX_NAME_STR "999999999"
740#define BC_MAX_EXP_STR "999999999"
741#define BC_MAX_VARS_STR "999999999"
742
743#define BC_MAX_OBASE_STR "999"
744
745#if INT_MAX == 2147483647
746# define BC_MAX_DIM_STR "2147483647"
747#elif INT_MAX == 9223372036854775807
748# define BC_MAX_DIM_STR "9223372036854775807"
749#else
750# error Strange INT_MAX
751#endif
752
753#if UINT_MAX == 4294967295
754# define BC_MAX_SCALE_STR "4294967295"
755# define BC_MAX_STRING_STR "4294967294"
756#elif UINT_MAX == 18446744073709551615
757# define BC_MAX_SCALE_STR "18446744073709551615"
758# define BC_MAX_STRING_STR "18446744073709551614"
759#else
760# error Strange UINT_MAX
761#endif
762#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600763
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100764struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100765 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100766 IF_FEATURE_CLEAN_UP(smallint exiting;)
Denys Vlasenko69171dc2018-12-12 00:29:24 +0100767 smallint in_read;
Gavin Howard01055ba2018-11-03 11:00:21 -0600768
769 BcParse prs;
770 BcProgram prog;
771
Denys Vlasenko5318f812018-12-05 17:48:01 +0100772 // For error messages. Can be set to current parsed line,
773 // or [TODO] to current executing line (can be before last parsed one)
774 unsigned err_line;
775
Gavin Howard01055ba2018-11-03 11:00:21 -0600776 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100777 BcVec input_buffer;
778 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600779
780 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100781
782#if ENABLE_FEATURE_EDITING
783 line_input_t *line_input_state;
784#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100785} FIX_ALIASING;
786#define G (*ptr_to_globals)
787#define INIT_G() do { \
788 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
789} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100790#define FREE_G() do { \
791 FREE_PTR_TO_GLOBALS(); \
792} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100793#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
794#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100795#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100796#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100797# define G_interrupt bb_got_signal
798# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100799#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100800# define G_interrupt 0
801# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100802#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100803#if ENABLE_FEATURE_CLEAN_UP
804# define G_exiting G.exiting
805#else
806# define G_exiting 0
807#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100808#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100809#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100810
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100811// In configurations where errors abort instead of propagating error
812// return code up the call chain, functions returning BC_STATUS
813// actually don't return anything, they always succeed and return "void".
814// A macro wrapper is provided, which makes this statement work:
815// s = zbc_func(...)
816// and makes it visible to the compiler that s is always zero,
817// allowing compiler to optimize dead code after the statement.
818//
819// To make code more readable, each such function has a "z"
820// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
821//
822#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100823# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100824# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100825# define IF_ERROR_RETURN_POSSIBLE(a) a
826# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100827# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100828# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100829#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100830# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100831# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100832# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
833# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100834# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100835# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100836#endif
837
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +0100838#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
839#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
840
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100841#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
842#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
843#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
844//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
845static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
846{
847 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
848}
849//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
850static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
851{
852 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
853}
854
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100855typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
856
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100857typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100858
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100859static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
860 BcNumBinaryOp op, size_t req);
861static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
862static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
863static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
864static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
865static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
866static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
867
868static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
869{
870 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
871 (void) scale;
872 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
873}
874
875static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
876{
877 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
878 (void) scale;
879 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
880}
881
882static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
883{
884 size_t req = BC_NUM_MREQ(a, b, scale);
885 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
886}
887
888static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
889{
890 size_t req = BC_NUM_MREQ(a, b, scale);
891 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
892}
893
894static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
895{
896 size_t req = BC_NUM_MREQ(a, b, scale);
897 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
898}
899
900static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
901{
902 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
903}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100904
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100905static const BcNumBinaryOp zbc_program_ops[] = {
906 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 -0600907};
Denys Vlasenkoec603182018-12-17 10:34:02 +0100908#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
909#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
910#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
911#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
912#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
913#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -0600914
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100915static void fflush_and_check(void)
916{
917 fflush_all();
918 if (ferror(stdout) || ferror(stderr))
919 bb_perror_msg_and_die("output error");
920}
921
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100922#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100923#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100924do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100925 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100926 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100927 return BC_STATUS_FAILURE; \
928} while (0)
929#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100930#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100931#endif
932
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100933static void quit(void) NORETURN;
934static void quit(void)
935{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100936 if (ferror(stdin))
937 bb_perror_msg_and_die("input error");
938 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100939 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100940 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100941}
942
Denys Vlasenko5318f812018-12-05 17:48:01 +0100943static void bc_verror_msg(const char *fmt, va_list p)
944{
Denys Vlasenko82269122018-12-14 16:30:56 +0100945 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100946 if (G.prog.file) {
947 sv = applet_name;
948 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
949 }
950 bb_verror_msg(fmt, p, NULL);
951 if (G.prog.file) {
952 free((char*)applet_name);
953 applet_name = sv;
954 }
955}
956
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100957static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100958{
959 va_list p;
960
961 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100962 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100963 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100964
Denys Vlasenkoec603182018-12-17 10:34:02 +0100965 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
966 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
967 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100968}
969
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100970#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100971static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100972{
973 va_list p;
974
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100975 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100976 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100977 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100978
979 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100980 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100981 va_end(p);
982
983 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100984 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100985 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenkoec603182018-12-17 10:34:02 +0100986 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
987 return BC_STATUS_FAILURE;
988 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100989}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100990#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100991
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100992// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
993// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100994// function must not have caller-cleaned parameters on stack.
995// Unfortunately, vararg function API does exactly that on most arches.
996// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100997static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100998{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100999 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001000}
1001static ERRORFUNC int bc_error_bad_character(char c)
1002{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001003 if (!c)
1004 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001005 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_bad_expression(void)
1008{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
1011static ERRORFUNC int bc_error_bad_token(void)
1012{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001013 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001014}
1015static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1016{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001017 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001018}
1019static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1020{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001021 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001022}
1023static ERRORFUNC int bc_error_nested_read_call(void)
1024{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001025 IF_ERROR_RETURN_POSSIBLE(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001026}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001027#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001028static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001029{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001030 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001031}
Denys Vlasenko00646792018-12-05 18:12:27 +01001032static int bc_POSIX_does_not_allow(const char *msg)
1033{
1034 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1035}
1036static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1037{
1038 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1039}
1040static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1041{
1042 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1043}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001044#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001045
Gavin Howard01055ba2018-11-03 11:00:21 -06001046static void bc_vec_grow(BcVec *v, size_t n)
1047{
1048 size_t cap = v->cap * 2;
1049 while (cap < v->len + n) cap *= 2;
1050 v->v = xrealloc(v->v, v->size * cap);
1051 v->cap = cap;
1052}
1053
1054static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1055{
1056 v->size = esize;
1057 v->cap = BC_VEC_START_CAP;
1058 v->len = 0;
1059 v->dtor = dtor;
1060 v->v = xmalloc(esize * BC_VEC_START_CAP);
1061}
1062
Denys Vlasenko7d628012018-12-04 21:46:47 +01001063static void bc_char_vec_init(BcVec *v)
1064{
1065 bc_vec_init(v, sizeof(char), NULL);
1066}
1067
Gavin Howard01055ba2018-11-03 11:00:21 -06001068static void bc_vec_expand(BcVec *v, size_t req)
1069{
1070 if (v->cap < req) {
1071 v->v = xrealloc(v->v, v->size * req);
1072 v->cap = req;
1073 }
1074}
1075
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001076static void bc_vec_pop(BcVec *v)
1077{
1078 v->len--;
1079 if (v->dtor)
1080 v->dtor(v->v + (v->size * v->len));
1081}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001082
Gavin Howard01055ba2018-11-03 11:00:21 -06001083static void bc_vec_npop(BcVec *v, size_t n)
1084{
1085 if (!v->dtor)
1086 v->len -= n;
1087 else {
1088 size_t len = v->len - n;
1089 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1090 }
1091}
1092
Denys Vlasenko7d628012018-12-04 21:46:47 +01001093static void bc_vec_pop_all(BcVec *v)
1094{
1095 bc_vec_npop(v, v->len);
1096}
1097
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001098//TODO: return index of pushed data - many callers can use that
Gavin Howard01055ba2018-11-03 11:00:21 -06001099static void bc_vec_push(BcVec *v, const void *data)
1100{
1101 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1102 memmove(v->v + (v->size * v->len), data, v->size);
1103 v->len += 1;
1104}
1105
1106static void bc_vec_pushByte(BcVec *v, char data)
1107{
1108 bc_vec_push(v, &data);
1109}
1110
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001111static void bc_vec_pushZeroByte(BcVec *v)
1112{
1113 //bc_vec_pushByte(v, '\0');
1114 // better:
1115 bc_vec_push(v, &const_int_0);
1116}
1117
Gavin Howard01055ba2018-11-03 11:00:21 -06001118static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1119{
1120 if (idx == v->len)
1121 bc_vec_push(v, data);
1122 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001123 char *ptr;
1124
1125 if (v->len == v->cap) bc_vec_grow(v, 1);
1126
1127 ptr = v->v + v->size * idx;
1128
1129 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1130 memmove(ptr, data, v->size);
1131 }
1132}
1133
1134static void bc_vec_string(BcVec *v, size_t len, const char *str)
1135{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001136 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001137 bc_vec_expand(v, len + 1);
1138 memcpy(v->v, str, len);
1139 v->len = len;
1140
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001141 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001142}
1143
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001144#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001145static void bc_vec_concat(BcVec *v, const char *str)
1146{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001147 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001148
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001149 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001150
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001151 slen = strlen(str);
1152 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001153
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001154 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001155 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001156
1157 v->len = len;
1158}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001159#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001160
1161static void *bc_vec_item(const BcVec *v, size_t idx)
1162{
1163 return v->v + v->size * idx;
1164}
1165
1166static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1167{
1168 return v->v + v->size * (v->len - idx - 1);
1169}
1170
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001171static void *bc_vec_top(const BcVec *v)
1172{
1173 return v->v + v->size * (v->len - 1);
1174}
1175
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001176static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001177{
1178 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001179 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001180 free(v->v);
1181}
1182
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001183static BcFunc* bc_program_func(size_t idx)
1184{
1185 return bc_vec_item(&G.prog.fns, idx);
1186}
1187// BC_PROG_MAIN is zeroth element, so:
1188#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1189
1190#if ENABLE_BC
1191static BcFunc* bc_program_current_func(void)
1192{
1193 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1194 BcFunc *func = bc_program_func(ip->func);
1195 return func;
1196}
1197#endif
1198
1199static char** bc_program_str(size_t idx)
1200{
1201#if ENABLE_BC
1202 if (IS_BC) {
1203 BcFunc *func = bc_program_current_func();
1204 return bc_vec_item(&func->strs, idx);
1205 }
1206#endif
1207 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1208}
1209
1210static char** bc_program_const(size_t idx)
1211{
1212#if ENABLE_BC
1213 if (IS_BC) {
1214 BcFunc *func = bc_program_current_func();
1215 return bc_vec_item(&func->consts, idx);
1216 }
1217#endif
1218 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1219}
1220
1221
1222
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001223static int bc_id_cmp(const void *e1, const void *e2)
1224{
1225 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1226}
1227
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001228static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001229{
1230 free(((BcId *) id)->name);
1231}
1232
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001233static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001234{
1235 size_t low = 0, high = v->len;
1236
1237 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001238 size_t mid = (low + high) / 2;
1239 BcId *id = bc_vec_item(v, mid);
1240 int result = bc_id_cmp(ptr, id);
1241
1242 if (result == 0)
1243 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001244 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001245 high = mid;
1246 else
1247 low = mid + 1;
1248 }
1249
1250 return low;
1251}
1252
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001253static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001254{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001255 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001256
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001257 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001258 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001259 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1260 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001261 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001262 bc_vec_pushAt(v, ptr, n);
1263 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001264}
1265
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001266#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001267static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001268{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001269 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001270 if (i >= v->len) return BC_VEC_INVALID_IDX;
1271 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1272}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001273#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001274
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001275static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001276{
1277 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1278 || c > 0x7e
1279 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001280 bc_error_fmt("illegal character 0x%02x", c);
1281 return 1;
1282 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001283 return 0;
1284}
1285
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001286// Note: it _appends_ data from fp to vec.
1287static void bc_read_line(BcVec *vec, FILE *fp)
Gavin Howard01055ba2018-11-03 11:00:21 -06001288{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001289 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001290 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001291
Gavin Howard01055ba2018-11-03 11:00:21 -06001292#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001293 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001294 intr:
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001295 if (fp != stdin) {
1296 // ^C while running a script (bc SCRIPT): die.
1297 // We do not return to interactive prompt:
1298 // user might be running us from a shell,
1299 // and SCRIPT might be intended to terminate
1300 // (e.g. contain a "halt" stmt).
1301 // ^C dropping user into a bc prompt instead of
1302 // the shell would be unexpected.
1303 xfunc_die();
1304 }
1305 // ^C while interactive input
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001306 G_interrupt = 0;
1307 // GNU bc says "interrupted execution."
1308 // GNU dc says "Interrupt!"
1309 fputs("\ninterrupted execution\n", stderr);
1310 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001311
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001312# if ENABLE_FEATURE_EDITING
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001313 if (G_ttyin && fp == stdin) {
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001314 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001315# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001316 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1317 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1318 if (n == 0) // ^C
1319 goto intr;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001320 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01001321 return;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001322 }
1323 i = 0;
1324 for (;;) {
1325 char c = line_buf[i++];
1326 if (!c) break;
1327 if (bad_input_byte(c)) goto again;
1328 }
1329 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001330# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001331 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001332# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001333#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001334 {
1335 int c;
1336 bool bad_chars = 0;
1337 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001338
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001339 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001340#if ENABLE_FEATURE_BC_SIGNALS
1341 if (G_interrupt) {
1342 // ^C was pressed: ignore entire line, get another one
1343 vec->len = len;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001344 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001345 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001346#endif
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001347 do c = fgetc(fp); while (c == '\0');
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001348 if (c == EOF) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001349 if (ferror(fp))
1350 bb_perror_msg_and_die("input error");
1351 // Note: EOF does not append '\n'
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001352 break;
1353 }
1354 bad_chars |= bad_input_byte(c);
1355 bc_vec_pushByte(vec, (char)c);
1356 } while (c != '\n');
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001357
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001358 if (bad_chars) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001359 // Bad chars on this line
1360 if (!G.prog.file) { // stdin
1361 // ignore entire line, get another one
1362 vec->len = len;
1363 goto again;
1364 }
1365 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
Denys Vlasenkoed849352018-12-06 10:26:13 +01001366 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001367 bc_vec_pushZeroByte(vec);
1368 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001369}
1370
Gavin Howard01055ba2018-11-03 11:00:21 -06001371static void bc_num_setToZero(BcNum *n, size_t scale)
1372{
1373 n->len = 0;
1374 n->neg = false;
1375 n->rdx = scale;
1376}
1377
1378static void bc_num_zero(BcNum *n)
1379{
1380 bc_num_setToZero(n, 0);
1381}
1382
1383static void bc_num_one(BcNum *n)
1384{
1385 bc_num_setToZero(n, 0);
1386 n->len = 1;
1387 n->num[0] = 1;
1388}
1389
Denys Vlasenko3129f702018-12-09 12:04:44 +01001390// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001391static void bc_num_init(BcNum *n, size_t req)
1392{
1393 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001394 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001395 n->num = xmalloc(req);
1396 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001397 n->rdx = 0;
1398 n->len = 0;
1399 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001400}
1401
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001402static void bc_num_init_DEF_SIZE(BcNum *n)
1403{
1404 bc_num_init(n, BC_NUM_DEF_SIZE);
1405}
1406
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001407static void bc_num_expand(BcNum *n, size_t req)
1408{
1409 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1410 if (req > n->cap) {
1411 n->num = xrealloc(n->num, req);
1412 n->cap = req;
1413 }
1414}
1415
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001416static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001417{
1418 free(((BcNum *) num)->num);
1419}
1420
1421static void bc_num_copy(BcNum *d, BcNum *s)
1422{
1423 if (d != s) {
1424 bc_num_expand(d, s->cap);
1425 d->len = s->len;
1426 d->neg = s->neg;
1427 d->rdx = s->rdx;
1428 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1429 }
1430}
1431
Denys Vlasenko29301232018-12-11 15:29:32 +01001432static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001433{
1434 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001435 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001436
Denys Vlasenko29301232018-12-11 15:29:32 +01001437 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001438
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001439 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001440 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001441
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001442 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001443 pow *= 10;
1444
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001445 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001446 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001447 prev = result;
1448 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001449 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001450 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001451
Denys Vlasenko29301232018-12-11 15:29:32 +01001452 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001453}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001454#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001455
1456static void bc_num_ulong2num(BcNum *n, unsigned long val)
1457{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001458 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001459
1460 bc_num_zero(n);
1461
1462 if (val == 0) return;
1463
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001464 if (ULONG_MAX == 0xffffffffUL)
1465 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001466 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001467 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001468 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001469
1470 ptr = n->num;
1471 for (;;) {
1472 n->len++;
1473 *ptr++ = val % 10;
1474 val /= 10;
1475 if (val == 0) break;
1476 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001477}
1478
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001479static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001480{
1481 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001482 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001483 a[i] -= b[i];
1484 for (j = i; a[j] < 0;) {
1485 a[j++] += 10;
1486 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001487 }
1488 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001489}
1490
1491static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1492{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001493 size_t i = len;
1494 for (;;) {
1495 int c;
1496 if (i == 0)
1497 return 0;
1498 i--;
1499 c = a[i] - b[i];
1500 if (c != 0) {
1501 i++;
1502 if (c < 0)
1503 return -i;
1504 return i;
1505 }
1506 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001507}
1508
1509static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1510{
1511 size_t i, min, a_int, b_int, diff;
1512 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001513 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001514 ssize_t cmp;
1515
1516 if (a == b) return 0;
1517 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1518 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001519
1520 if (a->neg != b->neg) // signs of a and b differ
1521 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1522 return (int)b->neg - (int)a->neg;
1523 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001524
1525 a_int = BC_NUM_INT(a);
1526 b_int = BC_NUM_INT(b);
1527 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001528
1529 if (a_int != 0) return (ssize_t) a_int;
1530
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001531 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001532 if (a_max) {
1533 min = b->rdx;
1534 diff = a->rdx - b->rdx;
1535 max_num = a->num + diff;
1536 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001537 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1538 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001539 min = a->rdx;
1540 diff = b->rdx - a->rdx;
1541 max_num = b->num + diff;
1542 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001543 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001544 }
1545
1546 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001547 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001548
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001549 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001550 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001551 }
1552
1553 return 0;
1554}
1555
1556static void bc_num_truncate(BcNum *n, size_t places)
1557{
1558 if (places == 0) return;
1559
1560 n->rdx -= places;
1561
1562 if (n->len != 0) {
1563 n->len -= places;
1564 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1565 }
1566}
1567
1568static void bc_num_extend(BcNum *n, size_t places)
1569{
1570 size_t len = n->len + places;
1571
1572 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001573 if (n->cap < len) bc_num_expand(n, len);
1574
1575 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1576 memset(n->num, 0, sizeof(BcDig) * places);
1577
1578 n->len += places;
1579 n->rdx += places;
1580 }
1581}
1582
1583static void bc_num_clean(BcNum *n)
1584{
1585 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1586 if (n->len == 0)
1587 n->neg = false;
1588 else if (n->len < n->rdx)
1589 n->len = n->rdx;
1590}
1591
1592static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1593{
1594 if (n->rdx < scale)
1595 bc_num_extend(n, scale - n->rdx);
1596 else
1597 bc_num_truncate(n, n->rdx - scale);
1598
1599 bc_num_clean(n);
1600 if (n->len != 0) n->neg = !neg1 != !neg2;
1601}
1602
1603static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1604 BcNum *restrict b)
1605{
1606 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001607 b->len = n->len - idx;
1608 a->len = idx;
1609 a->rdx = b->rdx = 0;
1610
1611 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1612 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001613 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001614 bc_num_zero(b);
1615 bc_num_copy(a, n);
1616 }
1617
1618 bc_num_clean(a);
1619 bc_num_clean(b);
1620}
1621
Denys Vlasenko29301232018-12-11 15:29:32 +01001622static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001623{
Denys Vlasenko29301232018-12-11 15:29:32 +01001624 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001625
1626 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1627 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1628 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001629 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001630 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001631
1632 if (n->rdx >= places)
1633 n->rdx -= places;
1634 else {
1635 bc_num_extend(n, places - n->rdx);
1636 n->rdx = 0;
1637 }
1638
1639 bc_num_clean(n);
1640
Denys Vlasenko29301232018-12-11 15:29:32 +01001641 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001642}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001643#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001644
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001645static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001646{
Denys Vlasenko503faf92018-12-20 16:24:18 +01001647//TODO: nice example of non-allocated BcNum, use in other places as well!
Gavin Howard01055ba2018-11-03 11:00:21 -06001648 BcNum one;
1649 BcDig num[2];
1650
1651 one.cap = 2;
1652 one.num = num;
1653 bc_num_one(&one);
1654
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001655 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001656}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001657#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001658
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001659static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001660{
1661 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1662 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001663 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001664
1665 // Because this function doesn't need to use scale (per the bc spec),
1666 // I am hijacking it to say whether it's doing an add or a subtract.
1667
1668 if (a->len == 0) {
1669 bc_num_copy(c, b);
1670 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001671 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001672 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001673 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001674 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001675 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001676 }
1677
1678 c->neg = a->neg;
1679 c->rdx = BC_MAX(a->rdx, b->rdx);
1680 min_rdx = BC_MIN(a->rdx, b->rdx);
1681 c->len = 0;
1682
1683 if (a->rdx > b->rdx) {
1684 diff = a->rdx - b->rdx;
1685 ptr = a->num;
1686 ptr_a = a->num + diff;
1687 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001688 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001689 diff = b->rdx - a->rdx;
1690 ptr = b->num;
1691 ptr_a = a->num;
1692 ptr_b = b->num + diff;
1693 }
1694
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001695 ptr_c = c->num;
1696 for (i = 0; i < diff; ++i, ++c->len)
1697 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001698
1699 ptr_c += diff;
1700 a_int = BC_NUM_INT(a);
1701 b_int = BC_NUM_INT(b);
1702
1703 if (a_int > b_int) {
1704 min_int = b_int;
1705 max = a_int;
1706 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001707 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001708 min_int = a_int;
1709 max = b_int;
1710 ptr = ptr_b;
1711 }
1712
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001713 carry = 0;
1714 for (i = 0; i < min_rdx + min_int; ++i) {
1715 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001716 carry = in / 10;
1717 ptr_c[i] = (BcDig)(in % 10);
1718 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001719 for (; i < max + min_rdx; ++i) {
1720 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001721 carry = in / 10;
1722 ptr_c[i] = (BcDig)(in % 10);
1723 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001724 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001725
1726 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1727
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001728 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001729}
1730
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001731static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001732{
Gavin Howard01055ba2018-11-03 11:00:21 -06001733 ssize_t cmp;
1734 BcNum *minuend, *subtrahend;
1735 size_t start;
1736 bool aneg, bneg, neg;
1737
1738 // Because this function doesn't need to use scale (per the bc spec),
1739 // I am hijacking it to say whether it's doing an add or a subtract.
1740
1741 if (a->len == 0) {
1742 bc_num_copy(c, b);
1743 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001744 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001745 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001746 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001747 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001748 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001749 }
1750
1751 aneg = a->neg;
1752 bneg = b->neg;
1753 a->neg = b->neg = false;
1754
1755 cmp = bc_num_cmp(a, b);
1756
1757 a->neg = aneg;
1758 b->neg = bneg;
1759
1760 if (cmp == 0) {
1761 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001763 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001764 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001765 neg = a->neg;
1766 minuend = a;
1767 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001768 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001769 neg = b->neg;
1770 if (sub) neg = !neg;
1771 minuend = b;
1772 subtrahend = a;
1773 }
1774
1775 bc_num_copy(c, minuend);
1776 c->neg = neg;
1777
1778 if (c->rdx < subtrahend->rdx) {
1779 bc_num_extend(c, subtrahend->rdx - c->rdx);
1780 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001781 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001782 start = c->rdx - subtrahend->rdx;
1783
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001784 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001785
1786 bc_num_clean(c);
1787
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001788 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001789}
1790
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001791static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001792 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001793#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001794{
1795 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001796 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001798 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001799
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 if (a->len == 0 || b->len == 0) {
1801 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001802 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001804 aone = BC_NUM_ONE(a);
1805 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001806 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001807 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001808 }
1809
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001810 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1811 || a->len < BC_NUM_KARATSUBA_LEN
1812 || b->len < BC_NUM_KARATSUBA_LEN
1813 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001814 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001815
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 bc_num_expand(c, a->len + b->len + 1);
1817
1818 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001819 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001820
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001821 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001822 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001823 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001824 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001825 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001826 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001827 carry = in / 10;
1828 c->num[i + j] = (BcDig)(in % 10);
1829 }
1830
1831 c->num[i + j] += (BcDig) carry;
1832 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001833
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001834#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001835 // a=2^1000000
1836 // a*a <- without check below, this will not be interruptible
1837 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001838#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001839 }
1840
1841 c->len = len;
1842
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001843 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001844 }
1845
1846 bc_num_init(&l1, max);
1847 bc_num_init(&h1, max);
1848 bc_num_init(&l2, max);
1849 bc_num_init(&h2, max);
1850 bc_num_init(&m1, max);
1851 bc_num_init(&m2, max);
1852 bc_num_init(&z0, max);
1853 bc_num_init(&z1, max);
1854 bc_num_init(&z2, max);
1855 bc_num_init(&temp, max + max);
1856
1857 bc_num_split(a, max2, &l1, &h1);
1858 bc_num_split(b, max2, &l2, &h2);
1859
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001860 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001861 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001862 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001863 if (s) goto err;
1864
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001865 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001866 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001867 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001868 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001869 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 if (s) goto err;
1871
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001872 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001874 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 if (s) goto err;
1876
Denys Vlasenko29301232018-12-11 15:29:32 +01001877 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001879 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001880 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001881 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001882 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001883 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001884 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001885 bc_num_free(&temp);
1886 bc_num_free(&z2);
1887 bc_num_free(&z1);
1888 bc_num_free(&z0);
1889 bc_num_free(&m2);
1890 bc_num_free(&m1);
1891 bc_num_free(&h2);
1892 bc_num_free(&l2);
1893 bc_num_free(&h1);
1894 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001895 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001896}
1897
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001898static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001899{
1900 BcStatus s;
1901 BcNum cpa, cpb;
1902 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1903
1904 scale = BC_MAX(scale, a->rdx);
1905 scale = BC_MAX(scale, b->rdx);
1906 scale = BC_MIN(a->rdx + b->rdx, scale);
1907 maxrdx = BC_MAX(maxrdx, scale);
1908
1909 bc_num_init(&cpa, a->len);
1910 bc_num_init(&cpb, b->len);
1911
1912 bc_num_copy(&cpa, a);
1913 bc_num_copy(&cpb, b);
1914 cpa.neg = cpb.neg = false;
1915
Denys Vlasenko29301232018-12-11 15:29:32 +01001916 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001917 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001918 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001919 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001920 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001921 if (s) goto err;
1922
1923 maxrdx += scale;
1924 bc_num_expand(c, c->len + maxrdx);
1925
1926 if (c->len < maxrdx) {
1927 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1928 c->len += maxrdx;
1929 }
1930
1931 c->rdx = maxrdx;
1932 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001933 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001934 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{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001942 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001943 size_t len, end, i;
1944 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001945
1946 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001947 RETURN_STATUS(bc_error("divide by zero"));
1948 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001950 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001952 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001953 bc_num_copy(c, a);
1954 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001955 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 }
1957
1958 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1959 bc_num_copy(&cp, a);
1960 len = b->len;
1961
1962 if (len > cp.len) {
1963 bc_num_expand(&cp, len + 2);
1964 bc_num_extend(&cp, len - cp.len);
1965 }
1966
1967 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1968 cp.rdx -= b->rdx;
1969 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1970
1971 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001972 for (;;) {
1973 if (len == 0) break;
1974 len--;
1975 if (b->num[len] != 0)
1976 break;
1977 }
1978 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001979 }
1980
1981 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1982
1983 // We want an extra zero in front to make things simpler.
1984 cp.num[cp.len++] = 0;
1985 end = cp.len - len;
1986
1987 bc_num_expand(c, cp.len);
1988
1989 bc_num_zero(c);
1990 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1991 c->rdx = cp.rdx;
1992 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001993
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001994 s = BC_STATUS_SUCCESS;
1995 for (i = end - 1; i < end; --i) {
1996 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06001997 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001998 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
1999 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002000 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002001#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002002 // a=2^100000
2003 // scale=40000
2004 // 1/a <- without check below, this will not be interruptible
2005 if (G_interrupt) {
2006 s = BC_STATUS_FAILURE;
2007 break;
2008 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002009#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002010 }
2011
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002012 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 bc_num_free(&cp);
2014
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002015 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002016}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002017#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002018
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002019static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002020 BcNum *restrict d, size_t scale, size_t ts)
2021{
2022 BcStatus s;
2023 BcNum temp;
2024 bool neg;
2025
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002026 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002027 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002028
2029 if (a->len == 0) {
2030 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002031 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002032 }
2033
2034 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002035 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002036 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002037
2038 if (scale != 0) scale = ts;
2039
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002040 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002041 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002042 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002043 if (s) goto err;
2044
2045 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2046
2047 neg = d->neg;
2048 bc_num_retireMul(d, ts, a->neg, b->neg);
2049 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002050 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002051 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002052 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002053}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002054#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002055
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002056static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002057{
2058 BcStatus s;
2059 BcNum c1;
2060 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2061
2062 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002063 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002064 bc_num_free(&c1);
2065
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002066 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002067}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002068#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002069
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002070static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002071{
2072 BcStatus s = BC_STATUS_SUCCESS;
2073 BcNum copy;
2074 unsigned long pow;
2075 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002076 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002077
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002078 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002079
2080 if (b->len == 0) {
2081 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002082 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002083 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002084 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002085 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002086 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002087 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002088 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002089 if (!b->neg)
2090 bc_num_copy(c, a);
2091 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002092 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002094 }
2095
2096 neg = b->neg;
2097 b->neg = false;
2098
Denys Vlasenko29301232018-12-11 15:29:32 +01002099 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002100 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002101
2102 bc_num_init(&copy, a->len);
2103 bc_num_copy(&copy, a);
2104
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002105 if (!neg) {
2106 if (a->rdx > scale)
2107 scale = a->rdx;
2108 if (a->rdx * pow < scale)
2109 scale = a->rdx * pow;
2110 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002111
2112 b->neg = neg;
2113
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002114 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002116 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002118 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002119 //if (G_interrupt) {
2120 // s = BC_STATUS_FAILURE;
2121 // goto err;
2122 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 }
2124
Gavin Howard01055ba2018-11-03 11:00:21 -06002125 bc_num_copy(c, &copy);
2126
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002127 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002129 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002130 if (s) goto err;
2131
2132 if (pow & 1) {
2133 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002134 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002135 if (s) goto err;
2136 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002137 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002138 //if (G_interrupt) {
2139 // s = BC_STATUS_FAILURE;
2140 // goto err;
2141 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002142 }
2143
2144 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002145 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 if (s) goto err;
2147 }
2148
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2150
2151 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002152 for (i = 0; i < c->len; ++i)
2153 if (c->num[i] != 0)
2154 goto skip;
2155 bc_num_setToZero(c, scale);
2156 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002157
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002158 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002160 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002161}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002162#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002163
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002164static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002165 BcNumBinaryOp op, size_t req)
2166{
2167 BcStatus s;
2168 BcNum num2, *ptr_a, *ptr_b;
2169 bool init = false;
2170
2171 if (c == a) {
2172 ptr_a = &num2;
2173 memcpy(ptr_a, c, sizeof(BcNum));
2174 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002175 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002176 ptr_a = a;
2177
2178 if (c == b) {
2179 ptr_b = &num2;
2180 if (c != a) {
2181 memcpy(ptr_b, c, sizeof(BcNum));
2182 init = true;
2183 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002184 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002185 ptr_b = b;
2186
2187 if (init)
2188 bc_num_init(c, req);
2189 else
2190 bc_num_expand(c, req);
2191
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01002193 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002194
2195 if (init) bc_num_free(&num2);
2196
Denys Vlasenko29301232018-12-11 15:29:32 +01002197 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002198}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002199#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002200
Denys Vlasenko9311e012018-12-10 11:54:18 +01002201static bool bc_num_strValid(const char *val, size_t base)
2202{
2203 BcDig b;
2204 bool radix;
2205
2206 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2207 radix = false;
2208 for (;;) {
2209 BcDig c = *val++;
2210 if (c == '\0')
2211 break;
2212 if (c == '.') {
2213 if (radix) return false;
2214 radix = true;
2215 continue;
2216 }
2217 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2218 return false;
2219 }
2220 return true;
2221}
2222
2223// Note: n is already "bc_num_zero()"ed,
2224// leading zeroes in "val" are removed
2225static void bc_num_parseDecimal(BcNum *n, const char *val)
2226{
2227 size_t len, i;
2228 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002229
2230 len = strlen(val);
2231 if (len == 0)
2232 return;
2233
Denys Vlasenko9311e012018-12-10 11:54:18 +01002234 bc_num_expand(n, len);
2235
2236 ptr = strchr(val, '.');
2237
2238 n->rdx = 0;
2239 if (ptr != NULL)
2240 n->rdx = (size_t)((val + len) - (ptr + 1));
2241
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002242 for (i = 0; val[i]; ++i) {
2243 if (val[i] != '0' && val[i] != '.') {
2244 // Not entirely zero value - convert it, and exit
2245 i = len - 1;
2246 for (;;) {
2247 n->num[n->len] = val[i] - '0';
2248 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002249 skip_dot:
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002250 if (i == 0) break;
2251 if (val[--i] == '.') goto skip_dot;
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002252 }
2253 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002254 }
2255 }
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002256 // if for() exits without hitting if(), the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002257}
2258
2259// Note: n is already "bc_num_zero()"ed,
2260// leading zeroes in "val" are removed
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002261static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko9311e012018-12-10 11:54:18 +01002262{
2263 BcStatus s;
2264 BcNum temp, mult, result;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002265 BcNum base;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002266 BcDig c = '\0';
2267 unsigned long v;
2268 size_t i, digits;
2269
2270 for (i = 0; ; ++i) {
2271 if (val[i] == '\0')
2272 return;
2273 if (val[i] != '.' && val[i] != '0')
2274 break;
2275 }
2276
2277 bc_num_init_DEF_SIZE(&temp);
2278 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenkod3401432018-12-18 17:00:35 +01002279//TODO: have BcNumSmall type, with static buffer
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002280 bc_num_init_DEF_SIZE(&base);
2281 bc_num_ulong2num(&base, base_t);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002282
2283 for (;;) {
2284 c = *val++;
2285 if (c == '\0') goto int_err;
2286 if (c == '.') break;
2287
2288 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2289
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002290 s = zbc_num_mul(n, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002291 if (s) goto int_err;
2292 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002293 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002294 if (s) goto int_err;
2295 }
2296
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002297 bc_num_init(&result, base.len);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002298 //bc_num_zero(&result); - already is
2299 bc_num_one(&mult);
2300
2301 digits = 0;
2302 for (;;) {
2303 c = *val++;
2304 if (c == '\0') break;
2305 digits++;
2306
2307 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2308
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002309 s = zbc_num_mul(&result, &base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002310 if (s) goto err;
2311 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002312 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002313 if (s) goto err;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002314 s = zbc_num_mul(&mult, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002315 if (s) goto err;
2316 }
2317
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002318 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002319 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002320 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002321 if (s) goto err;
2322
2323 if (n->len != 0) {
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002324 if (n->rdx < digits)
2325 bc_num_extend(n, digits - n->rdx);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002326 } else
2327 bc_num_zero(n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002328 err:
Denys Vlasenko9311e012018-12-10 11:54:18 +01002329 bc_num_free(&result);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002330 int_err:
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002331 bc_num_free(&base);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002332 bc_num_free(&mult);
2333 bc_num_free(&temp);
2334}
2335
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002336static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
Gavin Howard01055ba2018-11-03 11:00:21 -06002337{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002338 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002339 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002340
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002341 bc_num_zero(n);
2342 while (*val == '0') val++;
2343
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 if (base_t == 10)
2345 bc_num_parseDecimal(n, val);
2346 else
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002347 bc_num_parseBase(n, val, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002348
Denys Vlasenko29301232018-12-11 15:29:32 +01002349 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002350}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002351#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002352
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002353static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002354{
2355 BcStatus s;
2356 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2357 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2358 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2359
2360 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2361 bc_num_expand(b, req);
2362
2363 if (a->len == 0) {
2364 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002365 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002366 }
2367 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002368 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002369 }
2370 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002371 bc_num_one(b);
2372 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002373 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002374 }
2375
2376 scale = BC_MAX(scale, a->rdx) + 1;
2377 len = a->len + scale;
2378
2379 bc_num_init(&num1, len);
2380 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002381 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002382
2383 bc_num_one(&half);
2384 half.num[0] = 5;
2385 half.rdx = 1;
2386
2387 bc_num_init(&f, len);
2388 bc_num_init(&fprime, len);
2389
2390 x0 = &num1;
2391 x1 = &num2;
2392
2393 bc_num_one(x0);
2394 pow = BC_NUM_INT(a);
2395
2396 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002397 if (pow & 1)
2398 x0->num[0] = 2;
2399 else
2400 x0->num[0] = 6;
2401
2402 pow -= 2 - (pow & 1);
2403
2404 bc_num_extend(x0, pow);
2405
2406 // Make sure to move the radix back.
2407 x0->rdx -= pow;
2408 }
2409
2410 x0->rdx = digs = digs1 = 0;
2411 resrdx = scale + 2;
2412 len = BC_NUM_INT(x0) + resrdx - 1;
2413
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002414 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002415 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002416 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002417 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002418 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002419 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 if (s) goto err;
2421
2422 cmp = bc_num_cmp(x1, x0);
2423 digs = x1->len - (unsigned long long) llabs(cmp);
2424
2425 if (cmp == cmp2 && digs == digs1)
2426 times += 1;
2427 else
2428 times = 0;
2429
2430 resrdx += times > 4;
2431
2432 cmp2 = cmp1;
2433 cmp1 = cmp;
2434 digs1 = digs;
2435
2436 temp = x0;
2437 x0 = x1;
2438 x1 = temp;
2439 }
2440
Gavin Howard01055ba2018-11-03 11:00:21 -06002441 bc_num_copy(b, x0);
2442 scale -= 1;
2443 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002444 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002445 bc_num_free(&fprime);
2446 bc_num_free(&f);
2447 bc_num_free(&half);
2448 bc_num_free(&num2);
2449 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002450 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002451}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002452#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002453
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002454static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002455 size_t scale)
2456{
2457 BcStatus s;
2458 BcNum num2, *ptr_a;
2459 bool init = false;
2460 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2461
2462 if (c == a) {
2463 memcpy(&num2, c, sizeof(BcNum));
2464 ptr_a = &num2;
2465 bc_num_init(c, len);
2466 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002467 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 ptr_a = a;
2469 bc_num_expand(c, len);
2470 }
2471
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002472 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002473
2474 if (init) bc_num_free(&num2);
2475
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002476 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002477}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002478#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002479
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002480#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002481static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002482{
2483 BcStatus s;
2484 BcNum base, exp, two, temp;
2485
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002486 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002487 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002488 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002489 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002490 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002491 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002492
2493 bc_num_expand(d, c->len);
2494 bc_num_init(&base, c->len);
2495 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002496 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002497 bc_num_init(&temp, b->len);
2498
2499 bc_num_one(&two);
2500 two.num[0] = 2;
2501 bc_num_one(d);
2502
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002503 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002504 if (s) goto err;
2505 bc_num_copy(&exp, b);
2506
2507 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002508 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002509 if (s) goto err;
2510
2511 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002512 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002513 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002514 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002515 if (s) goto err;
2516 }
2517
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002518 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002519 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002520 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002521 if (s) goto err;
2522 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002523 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002524 bc_num_free(&temp);
2525 bc_num_free(&two);
2526 bc_num_free(&exp);
2527 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002528 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002529}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002530#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002531#endif // ENABLE_DC
2532
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002533#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002534static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002535{
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002536 BcId *autoid;
Gavin Howard01055ba2018-11-03 11:00:21 -06002537 BcId a;
2538 size_t i;
2539
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002540 autoid = (void*)f->autos.v;
2541 for (i = 0; i < f->autos.len; i++, autoid++) {
2542 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002543 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002544 }
2545
2546 a.idx = var;
2547 a.name = name;
2548
2549 bc_vec_push(&f->autos, &a);
2550
Denys Vlasenko29301232018-12-11 15:29:32 +01002551 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002552}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002553#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002554#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002555
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002556static FAST_FUNC void bc_string_free(void *string)
2557{
2558 free(*(char**)string);
2559}
2560
Gavin Howard01055ba2018-11-03 11:00:21 -06002561static void bc_func_init(BcFunc *f)
2562{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002563 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002564 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2565 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002566 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2567 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002568 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002569}
2570
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002571static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002572{
2573 BcFunc *f = (BcFunc *) func;
2574 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002575 IF_BC(bc_vec_free(&f->labels);)
2576 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002577 IF_BC(bc_vec_free(&f->strs);)
2578 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002579}
2580
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002581static void bc_array_expand(BcVec *a, size_t len);
2582
Gavin Howard01055ba2018-11-03 11:00:21 -06002583static void bc_array_init(BcVec *a, bool nums)
2584{
2585 if (nums)
2586 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2587 else
2588 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2589 bc_array_expand(a, 1);
2590}
2591
Gavin Howard01055ba2018-11-03 11:00:21 -06002592static void bc_array_expand(BcVec *a, size_t len)
2593{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002594 if (a->dtor == bc_num_free
2595 // && a->size == sizeof(BcNum) - always true
2596 ) {
2597 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002598 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002599 bc_num_init_DEF_SIZE(&n);
2600 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002601 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002602 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002603 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002604 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002605 bc_array_init(&v, true);
2606 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002607 }
2608 }
2609}
2610
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002611static void bc_array_copy(BcVec *d, const BcVec *s)
2612{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002613 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002614 size_t i;
2615
2616 bc_vec_pop_all(d);
2617 bc_vec_expand(d, s->cap);
2618 d->len = s->len;
2619
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002620 dnum = (void*)d->v;
2621 snum = (void*)s->v;
2622 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002623 bc_num_init(dnum, snum->len);
2624 bc_num_copy(dnum, snum);
2625 }
2626}
2627
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002628#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002629static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002630{
2631 d->t = src->t;
2632
2633 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002634 case BC_RESULT_TEMP:
2635 case BC_RESULT_IBASE:
2636 case BC_RESULT_SCALE:
2637 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002638 bc_num_init(&d->d.n, src->d.n.len);
2639 bc_num_copy(&d->d.n, &src->d.n);
2640 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002641 case BC_RESULT_VAR:
2642 case BC_RESULT_ARRAY:
2643 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002644 d->d.id.name = xstrdup(src->d.id.name);
2645 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002646 case BC_RESULT_CONSTANT:
2647 case BC_RESULT_LAST:
2648 case BC_RESULT_ONE:
2649 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002650 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2651 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002652 }
2653}
2654#endif // ENABLE_DC
2655
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002656static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002657{
2658 BcResult *r = (BcResult *) result;
2659
2660 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002661 case BC_RESULT_TEMP:
2662 case BC_RESULT_IBASE:
2663 case BC_RESULT_SCALE:
2664 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002665 bc_num_free(&r->d.n);
2666 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002667 case BC_RESULT_VAR:
2668 case BC_RESULT_ARRAY:
2669 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002670 free(r->d.id.name);
2671 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002672 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002673 // Do nothing.
2674 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002675 }
2676}
2677
2678static void bc_lex_lineComment(BcLex *l)
2679{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002680 // Try: echo -n '#foo' | bc
2681 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002682 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002683 i = l->i;
2684 while (i < l->len && l->buf[i] != '\n')
2685 i++;
2686 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002687}
2688
2689static void bc_lex_whitespace(BcLex *l)
2690{
Gavin Howard01055ba2018-11-03 11:00:21 -06002691 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002692 for (;;) {
2693 char c = l->buf[l->i];
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002694 if (c == '\n') // this is BC_LEX_NLINE, not BC_LEX_WHITESPACE
2695 break;
2696 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002697 break;
2698 l->i++;
2699 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002700}
2701
Denys Vlasenko29301232018-12-11 15:29:32 +01002702static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002703{
2704 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002705 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002706 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002707
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002708 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002709 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002710 ccnt = i = 0;
2711 for (;;) {
2712 char c = buf[i];
2713 if (c == '\0')
2714 break;
2715 if (c == '\\' && buf[i + 1] == '\n') {
2716 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002717 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002718 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002719 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002720 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2721 if (c != '.') break;
2722 // if '.' was already seen, stop on second one:
2723 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002724 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002725 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002726 // buf[i] is one of "0-9A-F."
2727 i++;
2728 if (c != '.')
2729 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002730 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002731 //ccnt is the number of chars in the number string, excluding possible
2732 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2733 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002734 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002735
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002736 // This might overestimate the size, if there are "\<NL>"'s
2737 // in the number. Subtracting number_of_backslashes*2 correctly
2738 // is not that easy: consider that in the case of "NNN.\<NL>"
2739 // loop above will count "\<NL>" before it realizes it is not
2740 // in fact *inside* the number:
2741 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002742
Denys Vlasenko64074a12018-12-07 15:50:14 +01002743 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2744 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2745 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002746 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002747 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002748
Denys Vlasenko7d628012018-12-04 21:46:47 +01002749 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002750 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002751 bc_vec_push(&l->t.v, &start);
2752
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002753 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002754 // If we have hit a backslash, skip it. We don't have
2755 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002756 if (*buf == '\\') {
2757 buf += 2;
2758 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002759 continue;
2760 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002761 bc_vec_push(&l->t.v, buf);
2762 buf++;
2763 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002764 }
2765
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002766 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002767
Denys Vlasenko29301232018-12-11 15:29:32 +01002768 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002769}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002770#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002771
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002772static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002773{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002774 size_t i;
2775 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002776
2777 l->t.t = BC_LEX_NAME;
2778
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002779 i = 0;
2780 buf = l->buf + l->i - 1;
2781 for (;;) {
2782 char c = buf[i];
2783 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2784 i++;
2785 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002786
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002787#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002788 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2789 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2790 if (i > BC_MAX_STRING)
2791 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2792 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002793#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002794 bc_vec_string(&l->t.v, i, buf);
2795
2796 // Increment the index. We minus 1 because it has already been incremented.
2797 l->i += i - 1;
2798
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002799 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002800}
2801
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002802static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002803{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002804 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002805}
2806
2807static void bc_lex_free(BcLex *l)
2808{
2809 bc_vec_free(&l->t.v);
2810}
2811
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002812static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002813{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002814 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002815 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002816}
2817
Denys Vlasenkoe755e302018-12-13 22:25:28 +01002818IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2819IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002820
2821static BC_STATUS zcommon_lex_token(BcLex *l)
2822{
2823 if (IS_BC) {
2824 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2825 }
2826 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2827}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002828#define zcommon_lex_token(...) (zcommon_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002829
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002830static bool bc_lex_more_input(BcLex *l)
2831{
2832 size_t str;
2833 bool comment;
2834
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002835 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002836
2837 // This loop is complex because the vm tries not to send any lines that end
2838 // with a backslash to the parser. The reason for that is because the parser
2839 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2840 // case, and for strings and comments, the parser will expect more stuff.
2841 comment = false;
2842 str = 0;
2843 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002844 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002845 char *string;
2846
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002847 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002848 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002849 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002850 break;
2851
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002852 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002853 while (*string) {
2854 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002855 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002856 if (IS_BC)
2857 str ^= (c == '"');
2858 else {
2859 if (c == ']')
2860 str -= 1;
2861 else if (c == '[')
2862 str += 1;
2863 }
2864 }
2865 string++;
2866 if (c == '/' && *string == '*') {
2867 comment = true;
2868 string++;
2869 continue;
2870 }
2871 if (c == '*' && *string == '/') {
2872 comment = false;
2873 string++;
2874 }
2875 }
2876 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002877 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002878 continue;
2879 }
2880
2881 // Check for backslash+newline.
2882 // we do not check that last char is '\n' -
2883 // if it is not, then it's EOF, and looping back
2884 // to bc_read_line() will detect it:
2885 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002886 if (string >= G.input_buffer.v && *string == '\\') {
2887 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002888 continue;
2889 }
2890
2891 break;
2892 }
2893
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002894 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002895 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002896// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2897 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002898
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002899 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002900}
2901
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002902static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002903{
2904 BcStatus s;
2905
2906 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002907 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002908
2909 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002910 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002911 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002912
2913 // Loop until failure or we don't have whitespace. This
2914 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002915 // Comments are also BC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002916 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002917 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002918 if (l->i == l->len) {
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002919 l->t.t = BC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002920 if (!G.input_fp)
2921 RETURN_STATUS(BC_STATUS_SUCCESS);
2922 if (!bc_lex_more_input(l)) {
2923 G.input_fp = NULL;
2924 RETURN_STATUS(BC_STATUS_SUCCESS);
2925 }
2926 // here it's guaranteed that l->i is below l->len
2927 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002928 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002929 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2930 l->buf + l->i);
Denys Vlasenkoec603182018-12-17 10:34:02 +01002931 s = zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002932 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002933 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002934
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002935 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002936}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002937#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002938
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002939#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01002940static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
2941{
2942 if (l->t.t == BC_LEX_NLINE)
2943 RETURN_STATUS(zbc_lex_next(l));
2944 RETURN_STATUS(BC_STATUS_SUCCESS);
2945}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002946#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002947
2948static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
2949{
2950 BcStatus s;
2951 s = zbc_lex_next(l);
2952 if (s) RETURN_STATUS(s);
2953 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
2954 s = zbc_lex_skip_if_at_NLINE(l);
2955 RETURN_STATUS(s);
2956}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002957#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002958#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01002959
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002960static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002961{
2962 l->buf = text;
2963 l->i = 0;
2964 l->len = strlen(text);
2965 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002966 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002967}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002968#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002969
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002970#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002971static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002972{
2973 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002974 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002975 const char *buf = l->buf + l->i - 1;
2976
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002977 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2978 const char *keyword8 = bc_lex_kws[i].name8;
2979 unsigned j = 0;
2980 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2981 j++;
2982 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002983 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002984 if (keyword8[j] != '\0')
2985 continue;
2986 match:
2987 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002988 if (isalnum(buf[j]) || buf[j]=='_')
2989 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002990 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002991 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002992 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01002993 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002994 }
2995
2996 // We minus 1 because the index has already been incremented.
2997 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002998 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002999 }
3000
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003001 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003002 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003003
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003004 if (l->t.v.len > 2) {
3005 // Prevent this:
3006 // >>> qwe=1
3007 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3008 // '
3009 unsigned len = strchrnul(buf, '\n') - buf;
3010 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3011 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003012
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003013 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003014}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003015#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003016
Denys Vlasenko26819db2018-12-12 16:08:46 +01003017static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003018{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003019 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003020
3021 l->t.t = BC_LEX_STR;
3022
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003023 nls = 0;
3024 i = l->i;
3025 for (;;) {
3026 char c = l->buf[i];
3027 if (c == '\0') {
3028 l->i = i;
3029 RETURN_STATUS(bc_error("string end could not be found"));
3030 }
3031 if (c == '"')
3032 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003033 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003034 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003035 }
3036
3037 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003038 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3039 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3040 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003041 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003042 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003043 bc_vec_string(&l->t.v, len, l->buf + l->i);
3044
3045 l->i = i + 1;
3046 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003047 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003048
Denys Vlasenko26819db2018-12-12 16:08:46 +01003049 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003050}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003051#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003052
Denys Vlasenko0a238142018-12-14 16:48:34 +01003053static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003054{
3055 if (l->buf[l->i] == '=') {
3056 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003057 with_and_without >>= 8; // store "with" value
3058 } // else store "without" value
3059 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003060}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003061#define bc_lex_assign(l, with, without) \
3062 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003063
Denys Vlasenko29301232018-12-11 15:29:32 +01003064static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003065{
3066 size_t i, nls = 0;
3067 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003068
3069 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003070 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003071 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003072 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003073 check_star:
3074 if (c == '*') {
3075 c = buf[++i];
3076 if (c == '/')
3077 break;
3078 goto check_star;
3079 }
3080 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003081 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003082 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003083 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003084 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003085 }
3086
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003087 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003088 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003089 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
Denys Vlasenko29301232018-12-11 15:29:32 +01003091 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003092}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003093#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003094
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003095static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003096{
3097 BcStatus s = BC_STATUS_SUCCESS;
3098 char c = l->buf[l->i++], c2;
3099
3100 // This is the workhorse of the lexer.
3101 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003102// case '\0': // probably never reached
3103// l->i--;
3104// l->t.t = BC_LEX_EOF;
3105// l->newline = true;
3106// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003107 case '\n':
3108 l->t.t = BC_LEX_NLINE;
3109 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003110 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003111 case '\t':
3112 case '\v':
3113 case '\f':
3114 case '\r':
3115 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 bc_lex_whitespace(l);
3117 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003119 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003121 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003122 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003123 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003126 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003129 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003130 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003131 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003134 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3135 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 c2 = l->buf[l->i];
3138 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003139 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003140 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 ++l->i;
3142 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003143 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003145 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003147 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 case '(':
3149 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3151 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
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_INC;
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_PLUS, BC_LEX_OP_PLUS);
3162 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 l->t.t = BC_LEX_COMMA;
3165 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 c2 = l->buf[l->i];
3168 if (c2 == '-') {
3169 ++l->i;
3170 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003171 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3173 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003176 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003177 else {
3178 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003179 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 }
3181 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003183 c2 = l->buf[l->i];
3184 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003185 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003186 else
3187 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3188 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003189 case '0':
3190 case '1':
3191 case '2':
3192 case '3':
3193 case '4':
3194 case '5':
3195 case '6':
3196 case '7':
3197 case '8':
3198 case '9':
3199 case 'A':
3200 case 'B':
3201 case 'C':
3202 case 'D':
3203 case 'E':
3204 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003205 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 l->t.t = BC_LEX_SCOLON;
3209 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003211 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3212 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3215 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3218 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 case '[':
3220 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3222 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 if (l->buf[l->i] == '\n') {
3225 l->t.t = BC_LEX_WHITESPACE;
3226 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003227 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003228 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3232 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 case 'a':
3234 case 'b':
3235 case 'c':
3236 case 'd':
3237 case 'e':
3238 case 'f':
3239 case 'g':
3240 case 'h':
3241 case 'i':
3242 case 'j':
3243 case 'k':
3244 case 'l':
3245 case 'm':
3246 case 'n':
3247 case 'o':
3248 case 'p':
3249 case 'q':
3250 case 'r':
3251 case 's':
3252 case 't':
3253 case 'u':
3254 case 'v':
3255 case 'w':
3256 case 'x':
3257 case 'y':
3258 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003259 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003261 case '{':
3262 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003263 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3264 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003268 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003269 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 ++l->i;
3271 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003272 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003274 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003275 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003277 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003279 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 }
3282
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003283 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003284}
3285#endif // ENABLE_BC
3286
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003287#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003288static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003289{
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 if (isspace(l->buf[l->i - 1])) {
3291 bc_lex_whitespace(l);
3292 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003293 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003294 RETURN_STATUS(bc_error("extended register"));
3295 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 }
3297 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003298 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003299 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003300 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 l->t.t = BC_LEX_NAME;
3302 }
3303
Denys Vlasenko29301232018-12-11 15:29:32 +01003304 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003305}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003306#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003307
Denys Vlasenko29301232018-12-11 15:29:32 +01003308static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003309{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003310 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003311
3312 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003313 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003314
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003315 nls = 0;
3316 depth = 1;
3317 i = l->i;
3318 for (;;) {
3319 char c = l->buf[i];
3320 if (c == '\0') {
3321 l->i = i;
3322 RETURN_STATUS(bc_error("string end could not be found"));
3323 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003324 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003325 if (i == l->i || l->buf[i - 1] != '\\') {
3326 if (c == '[') depth++;
3327 if (c == ']')
3328 if (--depth == 0)
3329 break;
3330 }
3331 bc_vec_push(&l->t.v, &l->buf[i]);
3332 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003333 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003334 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003335
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003336 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003337 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3338 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3339 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003340 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003341 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003342
3343 l->i = i;
3344 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003345 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003346
Denys Vlasenko29301232018-12-11 15:29:32 +01003347 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003348}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003349#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003350
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003351static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003352{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003353 static const //BcLexType - should be this type, but narrower type saves size:
3354 uint8_t
3355 dc_lex_regs[] = {
3356 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
3357 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
3358 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
3359 BC_LEX_STORE_PUSH,
3360 };
3361 static const //BcLexType - should be this type
3362 uint8_t
3363 dc_lex_tokens[] = {
3364 /* %&'( */
3365 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
3366 /* )*+, */
3367 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
3368 /* -./ */
3369 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
3370 /* 0123456789 */
3371 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3372 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3373 BC_LEX_INVALID, BC_LEX_INVALID,
3374 /* :;<=>?@ */
3375 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
3376 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
3377 /* ABCDEFGH */
3378 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3379 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
3380 /* IJKLMNOP */
3381 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
3382 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
3383 /* QRSTUVWXY */
3384 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
3385 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
3386 /* Z[\] */
3387 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3388 /* ^_` */
3389 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
3390 /* abcdefgh */
3391 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
3392 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
3393 /* ijklmnop */
3394 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
3395 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
3396 /* qrstuvwx */
3397 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
3398 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
3399 /* yz */
3400 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
3401 /* {|}~ */
3402 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
3403 };
3404
Gavin Howard01055ba2018-11-03 11:00:21 -06003405 BcStatus s = BC_STATUS_SUCCESS;
3406 char c = l->buf[l->i++], c2;
3407 size_t i;
3408
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003409 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3410 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003411 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003412 }
3413
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003414 if (c >= '%' && c <= '~'
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003415 && (l->t.t = dc_lex_tokens[c - '%']) != BC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003416 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003417 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003418 }
3419
3420 // This is the workhorse of the lexer.
3421 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003422// case '\0': // probably never reached
3423// l->t.t = BC_LEX_EOF;
3424// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003425 case '\n':
3426 case '\t':
3427 case '\v':
3428 case '\f':
3429 case '\r':
3430 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003431 l->newline = (c == '\n');
3432 bc_lex_whitespace(l);
3433 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003434 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003435 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003436 if (c2 == '=')
3437 l->t.t = BC_LEX_OP_REL_NE;
3438 else if (c2 == '<')
3439 l->t.t = BC_LEX_OP_REL_LE;
3440 else if (c2 == '>')
3441 l->t.t = BC_LEX_OP_REL_GE;
3442 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003443 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003444 ++l->i;
3445 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003446 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003447 bc_lex_lineComment(l);
3448 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003449 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003450 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003451 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003452 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003453 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003454 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003455 case '0':
3456 case '1':
3457 case '2':
3458 case '3':
3459 case '4':
3460 case '5':
3461 case '6':
3462 case '7':
3463 case '8':
3464 case '9':
3465 case 'A':
3466 case 'B':
3467 case 'C':
3468 case 'D':
3469 case 'E':
3470 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003471 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003472 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003473 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003474 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003475 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003476 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003477 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003478 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 }
3481
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003482 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003483}
3484#endif // ENABLE_DC
3485
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003486static void bc_parse_push(BcParse *p, char i)
3487{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003488 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003489 bc_vec_pushByte(&p->func->code, i);
3490}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003491
Gavin Howard01055ba2018-11-03 11:00:21 -06003492static void bc_parse_pushName(BcParse *p, char *name)
3493{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003494 while (*name)
3495 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003496 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003497}
3498
3499static void bc_parse_pushIndex(BcParse *p, size_t idx)
3500{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003501 size_t mask;
3502 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003503
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003504 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003505 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3506 amt = sizeof(idx);
3507 do {
3508 if (idx & mask) break;
3509 mask >>= 8;
3510 amt--;
3511 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003512
3513 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003514
3515 while (idx != 0) {
3516 bc_parse_push(p, (unsigned char)idx);
3517 idx >>= 8;
3518 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003519}
3520
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003521#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003522static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3523{
3524 bc_parse_push(p, BC_INST_JUMP);
3525 bc_parse_pushIndex(p, idx);
3526}
3527
3528static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3529{
3530 bc_parse_push(p, BC_INST_JUMP_ZERO);
3531 bc_parse_pushIndex(p, idx);
3532}
3533
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003534static BC_STATUS bc_parse_pushSTR(BcParse *p)
3535{
3536 char *str = xstrdup(p->l.t.v.v);
3537
3538 bc_parse_push(p, BC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003539 bc_parse_pushIndex(p, p->func->strs.len);
3540 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003541
3542 RETURN_STATUS(zbc_lex_next(&p->l));
3543}
3544#define bc_parse_pushSTR(...) (bc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003545#endif
3546
3547static void bc_parse_pushNUM(BcParse *p)
3548{
3549 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003550#if ENABLE_BC && ENABLE_DC
3551 size_t idx = IS_BC ? p->func->consts.len : G.prog.consts.len;
3552 bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
3553#elif ENABLE_BC
3554 size_t idx = p->func->consts.len;
3555 bc_vec_push(&p->func->consts, &num);
3556#else // DC
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003557 size_t idx = G.prog.consts.len;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003558 bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003559#endif
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003560 bc_parse_push(p, BC_INST_NUM);
3561 bc_parse_pushIndex(p, idx);
3562}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003563
Denys Vlasenko99b37622018-12-15 20:06:59 +01003564IF_BC(static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p);)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01003565IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003566
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003567// "Parse" half of "parse,execute,repeat" main loop
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003568static BC_STATUS zcommon_parse(BcParse *p)
3569{
3570 if (IS_BC) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003571// FIXME: "eating" of stmt delemiters is coded inconsistently
3572// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
3573// which causes bugs such as "print 1 print 2" erroneously accepted,
3574// or "print 1 else 2" detecting parse error only after executing
3575// "print 1" part.
Denys Vlasenko99b37622018-12-15 20:06:59 +01003576 IF_BC(RETURN_STATUS(zbc_parse_stmt_or_funcdef(p));)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003577 }
3578 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3579}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003580#define zcommon_parse(...) (zcommon_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003581
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003582static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003583{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003584 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003585
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003586 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003587}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003588#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003589
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003590// Called when parsing or execution detects a failure,
3591// resets execution structures.
3592static void bc_program_reset(void)
3593{
3594 BcFunc *f;
3595 BcInstPtr *ip;
3596
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003597 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003598 bc_vec_pop_all(&G.prog.results);
3599
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003600 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003601 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003602 ip->idx = f->code.len;
3603}
3604
Denys Vlasenko26819db2018-12-12 16:08:46 +01003605// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003606// resets parsing structures.
3607static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003608{
3609 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003610 bc_vec_pop_all(&p->func->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003611 IF_BC(bc_vec_pop_all(&p->func->labels);)
3612 IF_BC(bc_vec_pop_all(&p->func->autos);)
3613 IF_BC(p->func->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06003614
Denys Vlasenko65e10462018-12-19 15:13:14 +01003615 p->fidx = BC_PROG_MAIN;
3616 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003617 }
3618
3619 p->l.i = p->l.len;
3620 p->l.t.t = BC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003621
Denys Vlasenko503faf92018-12-20 16:24:18 +01003622 IF_BC(bc_vec_pop_all(&p->exits);)
3623 IF_BC(bc_vec_pop_all(&p->conds);)
3624 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003625
Denys Vlasenkod38af482018-12-04 19:11:02 +01003626 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003627}
3628
3629static void bc_parse_free(BcParse *p)
3630{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003631 IF_BC(bc_vec_free(&p->exits);)
3632 IF_BC(bc_vec_free(&p->conds);)
3633 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003634 bc_lex_free(&p->l);
3635}
3636
Denys Vlasenkofa210792018-12-19 19:35:40 +01003637static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003638{
3639 memset(p, 0, sizeof(BcParse));
3640
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003641 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003642 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3643 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3644 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003645
Denys Vlasenkofa210792018-12-19 19:35:40 +01003646 p->fidx = fidx;
3647 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003648}
3649
Denys Vlasenko04715442018-12-21 00:10:26 +01003650static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003651{
Denys Vlasenko04715442018-12-21 00:10:26 +01003652 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003653 BcFunc f;
3654 bc_func_init(&f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003655 //idx = G.prog.fns.len;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003656 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003657 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003658}
3659
3660#if ENABLE_BC
3661
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003662// Note: takes ownership of 'name' (must be malloced)
3663static size_t bc_program_addFunc(char *name)
3664{
3665 size_t idx;
3666 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003667 int inserted;
3668
3669 entry.name = name;
3670 entry.idx = G.prog.fns.len;
3671
3672 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3673 if (!inserted) free(name);
3674
3675 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3676 idx = entry_ptr->idx;
3677
3678 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003679 // There is already a function with this name.
3680 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003681 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003682 bc_func_free(func);
3683 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003684 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003685 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003686 }
3687
3688 return idx;
3689}
3690
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003691#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3692#define BC_PARSE_LEAF(p, rparen) \
3693 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3694 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3695
3696// We can calculate the conversion between tokens and exprs by subtracting the
3697// position of the first operator in the lex enum and adding the position of the
3698// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko0154d782018-12-14 23:32:51 +01003699#define BC_TOKEN_2_INST(t) ((char) ((t) - BC_LEX_NEG + BC_INST_NEG))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003700
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003701static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3702
3703static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3704{
3705 BcStatus s;
3706
3707 s = bc_parse_expr_empty_ok(p, flags);
3708 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3709 RETURN_STATUS(bc_error("empty expression"));
3710 RETURN_STATUS(s);
3711}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003712#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003713
3714static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003715#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003716
3717static BC_STATUS zbc_parse_stmt(BcParse *p)
3718{
3719 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3720}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003721#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003722
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003723static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003724{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003725 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3726 // Same for "else", "while()", "for()".
3727 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3728 if (s) RETURN_STATUS(s);
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003729 if (p->l.t.t == BC_LEX_NLINE)
3730 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003731
3732 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003733}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003734#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003735
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003736static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3737 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003738{
Denys Vlasenko65437582018-12-05 19:37:19 +01003739 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3740 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003741
3742 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003743 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003744 if (t == BC_LEX_LPAREN) break;
3745
Denys Vlasenko65437582018-12-05 19:37:19 +01003746 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003747 if (l >= r && (l != r || !left)) break;
3748
Denys Vlasenko0154d782018-12-14 23:32:51 +01003749 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003750 bc_vec_pop(&p->ops);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003751 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003752 }
3753
3754 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003755}
3756
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003757static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003758{
3759 BcLexType top;
3760
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003761 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003762 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 top = BC_PARSE_TOP_OP(p);
3764
3765 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003766 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003767
3768 bc_vec_pop(&p->ops);
3769 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3770
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003771 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003772 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003773 top = BC_PARSE_TOP_OP(p);
3774 }
3775
3776 bc_vec_pop(&p->ops);
3777
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003778 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003779}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003780#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003781
Denys Vlasenko26819db2018-12-12 16:08:46 +01003782static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003783{
3784 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003785 size_t nparams;
3786
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003787 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003788 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3789
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003790 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003791 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003792
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003793 nparams = 0;
3794 if (p->l.t.t != BC_LEX_RPAREN) {
3795 for (;;) {
3796 s = zbc_parse_expr(p, flags);
3797 if (s) RETURN_STATUS(s);
3798 nparams++;
3799 if (p->l.t.t != BC_LEX_COMMA) {
3800 if (p->l.t.t == BC_LEX_RPAREN)
3801 break;
3802 RETURN_STATUS(bc_error_bad_token());
3803 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003804 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003805 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003806 }
3807 }
3808
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 bc_parse_push(p, BC_INST_CALL);
3810 bc_parse_pushIndex(p, nparams);
3811
Denys Vlasenko26819db2018-12-12 16:08:46 +01003812 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003813}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003814#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003815
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003816// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003817static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003818{
3819 BcStatus s;
3820 BcId entry, *entry_ptr;
3821 size_t idx;
3822
3823 entry.name = name;
3824
Denys Vlasenko26819db2018-12-12 16:08:46 +01003825 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003826 if (s) goto err;
3827
3828 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003829 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003830 goto err;
3831 }
3832
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003833 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003834
3835 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003836 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003837 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003838 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003839 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003840 free(name);
3841
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003842 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 bc_parse_pushIndex(p, entry_ptr->idx);
3844
Denys Vlasenko26819db2018-12-12 16:08:46 +01003845 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003846 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003848 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003849}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003850#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003851
Denys Vlasenko26819db2018-12-12 16:08:46 +01003852static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003853{
3854 BcStatus s;
3855 char *name;
3856
3857 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003858 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003859 if (s) goto err;
3860
3861 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003862 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003863 if (s) goto err;
3864
3865 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003866 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003867 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 goto err;
3869 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003870 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003871 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003873 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003874 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003875 if (s) goto err;
3876 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003877 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 if (s) goto err;
3879 bc_parse_push(p, *type);
3880 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003881 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003882 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003883 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003884 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 goto err;
3886 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003888 s = zbc_parse_call(p, name, flags);
3889 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003890 *type = BC_INST_VAR;
3891 bc_parse_push(p, BC_INST_VAR);
3892 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003893 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 }
3895
Denys Vlasenko26819db2018-12-12 16:08:46 +01003896 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003897 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003898 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003899 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003900}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003901#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003902
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003903static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003904{
3905 BcStatus s;
3906
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003907 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003908 if (s) RETURN_STATUS(s);
3909 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003910
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003911 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003912 if (s) RETURN_STATUS(s);
3913 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003914
3915 bc_parse_push(p, BC_INST_READ);
3916
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003917 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003918}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003919#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003920
Denys Vlasenko26819db2018-12-12 16:08:46 +01003921static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003922 BcInst *prev)
3923{
3924 BcStatus s;
3925
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003926 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003927 if (s) RETURN_STATUS(s);
3928 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003929
3930 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3931
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003932 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003933 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003934
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003935 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003936 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003937
Denys Vlasenko26819db2018-12-12 16:08:46 +01003938 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
3940 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3941 bc_parse_push(p, *prev);
3942
Denys Vlasenko26819db2018-12-12 16:08:46 +01003943 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003944}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003945#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003946
Denys Vlasenko26819db2018-12-12 16:08:46 +01003947static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003948{
3949 BcStatus s;
3950
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003951 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
3954 if (p->l.t.t != BC_LEX_LPAREN) {
3955 *type = BC_INST_SCALE;
3956 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003957 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003958 }
3959
3960 *type = BC_INST_SCALE_FUNC;
3961 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3962
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003963 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003964 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003966 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003967 if (s) RETURN_STATUS(s);
3968 if (p->l.t.t != BC_LEX_RPAREN)
3969 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003970 bc_parse_push(p, BC_INST_SCALE_FUNC);
3971
Denys Vlasenko26819db2018-12-12 16:08:46 +01003972 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003973}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003974#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003975
Denys Vlasenko26819db2018-12-12 16:08:46 +01003976static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 size_t *nexprs, uint8_t flags)
3978{
3979 BcStatus s;
3980 BcLexType type;
3981 char inst;
3982 BcInst etype = *prev;
3983
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003984 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM
3985 || etype == BC_INST_SCALE || etype == BC_INST_LAST
3986 || etype == BC_INST_IBASE || etype == BC_INST_OBASE
3987 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003988 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3989 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003990 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003991 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003992 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3993 *paren_expr = true;
3994
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003995 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003997 type = p->l.t.t;
3998
3999 // Because we parse the next part of the expression
4000 // right here, we need to increment this.
4001 *nexprs = *nexprs + 1;
4002
4003 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004004 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004005 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004007 case BC_LEX_KEY_IBASE:
4008 case BC_LEX_KEY_LAST:
4009 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004010 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004011 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004012 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004013 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004014 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004016 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004017 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004018 else
4019 bc_parse_push(p, BC_INST_SCALE);
4020 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004021 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004022 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004024 }
4025
4026 if (!s) bc_parse_push(p, inst);
4027 }
4028
Denys Vlasenko26819db2018-12-12 16:08:46 +01004029 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004030}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004031#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004032
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004033static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004034 bool rparen, size_t *nexprs)
4035{
4036 BcStatus s;
4037 BcLexType type;
4038 BcInst etype = *prev;
4039
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004040 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004041 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004042
4043 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4044 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4045 BC_LEX_OP_MINUS :
4046 BC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004047 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004048
4049 // We can just push onto the op stack because this is the largest
4050 // precedence operator that gets pushed. Inc/dec does not.
4051 if (type != BC_LEX_OP_MINUS)
4052 bc_vec_push(&p->ops, &type);
4053 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004054 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004056 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004057}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004058#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004059
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004060static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004061{
4062 BcStatus s;
4063 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004064
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004065 for (;;) {
4066 s = zbc_lex_next(&p->l);
4067 if (s) RETURN_STATUS(s);
4068 type = p->l.t.t;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004069 if (type == BC_LEX_STR) {
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004070 s = bc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004071 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004072 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004073 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004074 if (s) RETURN_STATUS(s);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004075 bc_parse_push(p, BC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004076 if (p->l.t.t != BC_LEX_COMMA)
4077 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004078 }
4079
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004080 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004081}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004082#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004083
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004084static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004085{
4086 BcStatus s;
4087 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004088
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004089 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004090 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004091 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004092
4093 t = p->l.t.t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004094 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4095 bc_parse_push(p, BC_INST_RET0);
4096 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004097 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004098 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004099 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004100 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004101 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004102 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004103 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004104
4105 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004106 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004107 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004108 }
4109
4110 bc_parse_push(p, BC_INST_RET);
4111 }
4112
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004113 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004114 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004115}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004116#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004117
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004118static void rewrite_label_to_current(BcParse *p, size_t idx)
4119{
4120 size_t *label = bc_vec_item(&p->func->labels, idx);
4121 *label = p->func->code.len;
4122}
4123
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004124static BC_STATUS zbc_parse_if(BcParse *p)
4125{
4126 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004127 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004128
4129 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4130 s = zbc_lex_next(&p->l);
4131 if (s) RETURN_STATUS(s);
4132 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4133
4134 s = zbc_lex_next(&p->l);
4135 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004136 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004137 if (s) RETURN_STATUS(s);
4138
4139 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004140
Denys Vlasenko15850832018-12-16 21:40:54 +01004141 ip_idx = p->func->labels.len;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004142 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenko15850832018-12-16 21:40:54 +01004143 bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004144
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004145 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004146 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004147
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004148 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4149 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004150 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004151
Denys Vlasenko15850832018-12-16 21:40:54 +01004152 ip2_idx = p->func->labels.len;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004153
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004154 dbg_lex("%s:%d after if() body: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004155 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004156
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004157 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004158 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004159
Denys Vlasenko15850832018-12-16 21:40:54 +01004160 bc_vec_push(&p->func->labels, &ip2_idx);
4161 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004162
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004163 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004164 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004165 }
4166
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004167 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004168 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004169
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004170 dbg_lex_done("%s:%d done", __func__, __LINE__);
4171 RETURN_STATUS(s);
4172}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004173#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004174
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004175static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004176{
4177 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004178 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004179 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004180
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004181 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004182 if (s) RETURN_STATUS(s);
4183 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004184 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004185 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004186
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004187 cond_idx = p->func->labels.len;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004188 ip_idx = cond_idx + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004189
4190 bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004191 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004192
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004193 bc_vec_push(&p->exits, &ip_idx);
4194 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004195
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004196 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004197 if (s) RETURN_STATUS(s);
4198 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004199
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004200 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004201
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004202 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004203 if (s) RETURN_STATUS(s);
4204
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004205 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004206 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004207
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004208 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004209 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004210
4211 bc_vec_pop(&p->exits);
4212 bc_vec_pop(&p->conds);
4213
4214 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004215}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004216#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004217
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004218static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004219{
4220 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004221 size_t cond_idx, exit_idx, body_idx, update_idx;
4222
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004223 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004224 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004225 if (s) RETURN_STATUS(s);
4226 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004227 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004228 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004229
4230 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004231 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004232 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004233 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004234
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004235 if (s) RETURN_STATUS(s);
4236 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004237 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004238 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004239
4240 cond_idx = p->func->labels.len;
4241 update_idx = cond_idx + 1;
4242 body_idx = update_idx + 1;
4243 exit_idx = body_idx + 1;
4244
4245 bc_vec_push(&p->func->labels, &p->func->code.len);
4246
4247 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004248 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004249 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004250 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004251 // This is safe to set because the current token is a semicolon,
4252 // which has no string requirement.
4253 bc_vec_string(&p->l.t.v, 1, "1");
4254 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004255 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004256 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004257
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004258 if (s) RETURN_STATUS(s);
4259 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004260
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004261 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004262 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004263
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004264 bc_parse_pushJUMP_ZERO(p, exit_idx);
4265 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004266
Gavin Howard01055ba2018-11-03 11:00:21 -06004267 bc_vec_push(&p->conds, &update_idx);
4268 bc_vec_push(&p->func->labels, &p->func->code.len);
4269
4270 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004271 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004272 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004273 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004274
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004275 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004276
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004277 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004278 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004279 bc_vec_push(&p->func->labels, &p->func->code.len);
4280
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004281 bc_vec_push(&p->exits, &exit_idx);
4282 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004283
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004284 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004285 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004286
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004287 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004288 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004289
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004290 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004291 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004292
4293 bc_vec_pop(&p->exits);
4294 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004295
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004296 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004297}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004298#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004299
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004300static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004301{
4302 BcStatus s;
4303 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
Gavin Howard01055ba2018-11-03 11:00:21 -06004305 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004306 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4307 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004308 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004309 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004310 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004311 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004312
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004313 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004314
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004315 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004316 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004317
4318 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004319 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004320
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004321 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004322}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004323#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004324
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004325static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004326{
4327 BcStatus s;
4328 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004329 char *name;
4330
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004331 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004332 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004333 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004334 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004335 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004336
4337 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004338 p->fidx = bc_program_addFunc(name);
4339 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004340
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004341 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004342 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004343 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004344 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004345 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004346 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004347
4348 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004349 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004350 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004351
4352 ++p->func->nparams;
4353
4354 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004355 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004356 if (s) goto err;
4357
4358 var = p->l.t.t != BC_LEX_LBRACKET;
4359
4360 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004361 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004362 if (s) goto err;
4363
4364 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004365 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004366 goto err;
4367 }
4368
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004369 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004370 if (s) goto err;
4371 }
4372
4373 comma = p->l.t.t == BC_LEX_COMMA;
4374 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004375 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004376 if (s) goto err;
4377 }
4378
Denys Vlasenko29301232018-12-11 15:29:32 +01004379 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004380 if (s) goto err;
4381 }
4382
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004383 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004384
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004385 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004386 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004387
4388 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004389 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004390
Denys Vlasenko202dd192018-12-16 17:30:35 +01004391 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4392 s = zbc_lex_skip_if_at_NLINE(&p->l);
4393 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004394 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4395 if (p->l.t.t != BC_LEX_LBRACE)
4396 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004397
4398 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004399 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004400 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004401 if (s) RETURN_STATUS(s);
4402
4403 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004404
4405 // Subsequent code generation is into main program
4406 p->fidx = BC_PROG_MAIN;
4407 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004408
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004409 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004410 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004411 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004412 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004413 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004414 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004415}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004416#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004417
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004418static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004419{
4420 BcStatus s;
4421 bool comma, var, one;
4422 char *name;
4423
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004424 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004425 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004426 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004427
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004428 comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004429 one = p->l.t.t == BC_LEX_NAME;
4430
4431 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004432 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004433 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004434 if (s) goto err;
4435
4436 var = p->l.t.t != BC_LEX_LBRACKET;
4437 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004438 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004439 if (s) goto err;
4440
4441 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004442 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004443 goto err;
4444 }
4445
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004446 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004447 if (s) goto err;
4448 }
4449
4450 comma = p->l.t.t == BC_LEX_COMMA;
4451 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004452 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004453 if (s) goto err;
4454 }
4455
Denys Vlasenko29301232018-12-11 15:29:32 +01004456 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004457 if (s) goto err;
4458 }
4459
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004460 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4461 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004462
4463 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004464 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004465
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004466 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004467 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004468 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004469 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004470 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004471 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004472}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004473#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004474
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004475#undef zbc_parse_stmt_possibly_auto
4476static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004477{
4478 BcStatus s = BC_STATUS_SUCCESS;
4479
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004480 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004481
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004482 if (p->l.t.t == BC_LEX_NLINE) {
4483 dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__);
4484 RETURN_STATUS(zbc_lex_next(&p->l));
4485 }
4486 if (p->l.t.t == BC_LEX_SCOLON) {
4487 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4488 RETURN_STATUS(zbc_lex_next(&p->l));
4489 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004490
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004491 if (p->l.t.t == BC_LEX_LBRACE) {
4492 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4493 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004494 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004495 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004496 } while (p->l.t.t == BC_LEX_NLINE);
4497 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4498 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4499 s = zbc_parse_auto(p);
4500 if (s) RETURN_STATUS(s);
4501 }
4502 while (p->l.t.t != BC_LEX_RBRACE) {
4503 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4504 s = zbc_parse_stmt(p);
4505 if (s) RETURN_STATUS(s);
4506 }
4507 s = zbc_lex_next(&p->l);
4508 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4509 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004510 }
4511
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004512 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004513 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004514 case BC_LEX_OP_INC:
4515 case BC_LEX_OP_DEC:
4516 case BC_LEX_OP_MINUS:
4517 case BC_LEX_OP_BOOL_NOT:
4518 case BC_LEX_LPAREN:
4519 case BC_LEX_NAME:
4520 case BC_LEX_NUMBER:
4521 case BC_LEX_KEY_IBASE:
4522 case BC_LEX_KEY_LAST:
4523 case BC_LEX_KEY_LENGTH:
4524 case BC_LEX_KEY_OBASE:
4525 case BC_LEX_KEY_READ:
4526 case BC_LEX_KEY_SCALE:
4527 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004528 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004529 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004530 case BC_LEX_STR:
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004531 s = bc_parse_pushSTR(p);
4532 bc_parse_push(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004533 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 case BC_LEX_KEY_BREAK:
4535 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004536 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004537 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004538 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004539 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004540 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004542 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004543 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004544 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004545 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004546 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004548 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004549 // "limits" is a compile-time command,
4550 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004551 printf(
4552 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4553 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4554 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4555 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4556 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4557 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4558 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4559 "Number of vars = "BC_MAX_VARS_STR "\n"
4560 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004561 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004563 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004564 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004565 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004566 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004567 // "quit" is a compile-time command. For example,
4568 // "if (0 == 1) quit" terminates when parsing the statement,
4569 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004570 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004572 if (!p->in_funcdef)
4573 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004574 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004577 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004580 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 }
4583
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004584 if (s || G_interrupt) {
4585 bc_parse_reset(p);
4586 s = BC_STATUS_FAILURE;
4587 }
4588
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004589 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004590 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004591}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004592#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004593
Denys Vlasenko99b37622018-12-15 20:06:59 +01004594static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004595{
4596 BcStatus s;
4597
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004598 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004599 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004600 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004601 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004602 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004603 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004604 } else {
4605 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 +01004606 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004607 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004608
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004609 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004610 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004611}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004612#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004613
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004614// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004615static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004616{
4617 BcStatus s = BC_STATUS_SUCCESS;
4618 BcInst prev = BC_INST_PRINT;
4619 BcLexType top, t = p->l.t.t;
4620 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004621 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4623
Denys Vlasenko17df8822018-12-14 23:00:24 +01004624 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004625 paren_first = p->l.t.t == BC_LEX_LPAREN;
4626 nparens = nrelops = 0;
4627 paren_expr = rprn = done = get_token = assign = false;
4628 bin_last = true;
4629
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004630 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004631 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004632 case BC_LEX_OP_INC:
4633 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004634 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004635 rprn = get_token = bin_last = false;
4636 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004637 case BC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004638 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004639 rprn = get_token = false;
4640 bin_last = prev == BC_INST_MINUS;
4641 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004642 case BC_LEX_OP_ASSIGN_POWER:
4643 case BC_LEX_OP_ASSIGN_MULTIPLY:
4644 case BC_LEX_OP_ASSIGN_DIVIDE:
4645 case BC_LEX_OP_ASSIGN_MODULUS:
4646 case BC_LEX_OP_ASSIGN_PLUS:
4647 case BC_LEX_OP_ASSIGN_MINUS:
4648 case BC_LEX_OP_ASSIGN:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004649 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM
4650 && prev != BC_INST_SCALE && prev != BC_INST_IBASE
4651 && prev != BC_INST_OBASE && prev != BC_INST_LAST
4652 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004653 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004654 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004655 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004656 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004657 break;
4658 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004659 // Fallthrough.
4660 case BC_LEX_OP_POWER:
4661 case BC_LEX_OP_MULTIPLY:
4662 case BC_LEX_OP_DIVIDE:
4663 case BC_LEX_OP_MODULUS:
4664 case BC_LEX_OP_PLUS:
4665 case BC_LEX_OP_REL_EQ:
4666 case BC_LEX_OP_REL_LE:
4667 case BC_LEX_OP_REL_GE:
4668 case BC_LEX_OP_REL_NE:
4669 case BC_LEX_OP_REL_LT:
4670 case BC_LEX_OP_REL_GT:
4671 case BC_LEX_OP_BOOL_NOT:
4672 case BC_LEX_OP_BOOL_OR:
4673 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004674 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4675 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4676 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004677 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004678 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004679 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004680 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004681 bc_parse_operator(p, t, ops_bgn, &nexprs);
4682 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004683 rprn = get_token = false;
4684 bin_last = t != BC_LEX_OP_BOOL_NOT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004685 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004687 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004688 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004689 ++nparens;
4690 paren_expr = rprn = bin_last = false;
4691 get_token = true;
4692 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004693 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004694 case BC_LEX_RPAREN:
Gavin Howard01055ba2018-11-03 11:00:21 -06004695 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004696 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004697 if (nparens == 0) {
4698 s = BC_STATUS_SUCCESS;
4699 done = true;
4700 get_token = false;
4701 break;
4702 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004703 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004704 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004705 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004706 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004707 --nparens;
4708 paren_expr = rprn = true;
4709 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004710 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004711 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004712 case BC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004713 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004714 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004715 paren_expr = true;
4716 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004717 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004718 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004719 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 case BC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004721 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004722 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004723 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004724 nexprs++;
4725 prev = BC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004726 paren_expr = get_token = true;
4727 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004728 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004729 case BC_LEX_KEY_IBASE:
4730 case BC_LEX_KEY_LAST:
4731 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004732 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004733 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004734 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4735 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 paren_expr = get_token = true;
4737 rprn = bin_last = false;
4738 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004739 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004740 case BC_LEX_KEY_LENGTH:
4741 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004742 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004743 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004744 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004745 paren_expr = true;
4746 rprn = get_token = bin_last = false;
4747 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004748 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004749 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004750 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004751 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004752 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004753 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004754 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004755 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004756 paren_expr = true;
4757 rprn = get_token = bin_last = false;
4758 ++nexprs;
4759 prev = BC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004760 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004761 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004762 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004763 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004764 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004765 paren_expr = true;
4766 rprn = get_token = bin_last = false;
4767 ++nexprs;
4768 prev = BC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004769 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004770 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004771 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004772 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004773 }
4774
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004775 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004776 }
4777
4778 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004779 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004780
4781 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004782 top = BC_PARSE_TOP_OP(p);
4783 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4784
4785 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004786 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004787
Denys Vlasenko0154d782018-12-14 23:32:51 +01004788 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004789
4790 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4791 bc_vec_pop(&p->ops);
4792 }
4793
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004794 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004795 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004796
Gavin Howard01055ba2018-11-03 11:00:21 -06004797 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004798 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004799 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004800 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004801 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004802 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004803 }
4804
4805 if (flags & BC_PARSE_PRINT) {
4806 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4807 bc_parse_push(p, BC_INST_POP);
4808 }
4809
Denys Vlasenko17df8822018-12-14 23:00:24 +01004810 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004811 return s;
4812}
4813
Gavin Howard01055ba2018-11-03 11:00:21 -06004814#endif // ENABLE_BC
4815
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004816#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004817
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004818static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004819{
4820 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004821
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004822 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004823 if (s) RETURN_STATUS(s);
4824 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004825
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004826 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004827
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004828 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004829}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004830#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004831
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004832static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004833{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004834 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004835 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004836
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004837 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004838
4839 str = xstrdup(p->l.t.v.v);
4840 bc_parse_push(p, BC_INST_STR);
4841 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004842 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004843
4844 // Explanation needed here
4845 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004846 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004847
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004848 dbg_lex_done("%s:%d done", __func__, __LINE__);
4849
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004850 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004851}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004852#define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004853
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004854static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004855{
4856 BcStatus s;
4857
4858 bc_parse_push(p, inst);
4859 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004860 s = zdc_parse_register(p);
4861 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004862 }
4863
4864 if (store) {
4865 bc_parse_push(p, BC_INST_SWAP);
4866 bc_parse_push(p, BC_INST_ASSIGN);
4867 bc_parse_push(p, BC_INST_POP);
4868 }
4869
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004870 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004871}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004872#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004873
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004874static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004875{
4876 BcStatus s;
4877
4878 bc_parse_push(p, inst);
4879 bc_parse_push(p, BC_INST_EXEC_COND);
4880
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004881 s = zdc_parse_register(p);
4882 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004883
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004884 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004885 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004886
4887 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004888 s = zdc_parse_register(p);
4889 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004890 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004891 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06004892 bc_parse_push(p, BC_PARSE_STREND);
4893
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004894 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004895}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004896#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004897
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004898static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004899{
4900 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004901 uint8_t inst;
4902 bool assign, get_token = false;
4903
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004904 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004905 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004906 case BC_LEX_OP_REL_EQ:
4907 case BC_LEX_OP_REL_LE:
4908 case BC_LEX_OP_REL_GE:
4909 case BC_LEX_OP_REL_NE:
4910 case BC_LEX_OP_REL_LT:
4911 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004912 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004913 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004914 case BC_LEX_SCOLON:
4915 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004916 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004917 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004918 case BC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004919 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004920 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004921 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004922 case BC_LEX_NEG:
4923 case BC_LEX_NUMBER:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004924 dbg_lex("%s:%d LEX_NEG/NUMBER", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004925 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004926 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004927 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004928 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004929 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 }
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004931 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004932 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
4933 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004934 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004935 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004936 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004937 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004938 else
4939 bc_parse_push(p, BC_INST_READ);
4940 get_token = true;
4941 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004942 case BC_LEX_OP_ASSIGN:
4943 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06004944 assign = t == BC_LEX_OP_ASSIGN;
4945 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004946 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004948 case BC_LEX_LOAD:
4949 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06004950 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004951 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004952 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004953 case BC_LEX_STORE_IBASE:
4954 case BC_LEX_STORE_SCALE:
4955 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004956 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004957 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004958 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004959 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004960 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004961 get_token = true;
4962 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004963 }
4964
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004965 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004966
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004967 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004968 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004969}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004970#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004971
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004972static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004973{
Gavin Howard01055ba2018-11-03 11:00:21 -06004974 BcLexType t;
4975
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004976 dbg_lex_enter("%s:%d entered G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004977 for (;;) {
4978 BcInst inst;
4979 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004980
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004981 t = p->l.t.t;
4982 if (t == BC_LEX_EOF) break;
4983
4984 inst = dc_parse_insts[t];
Gavin Howard01055ba2018-11-03 11:00:21 -06004985 if (inst != BC_INST_INVALID) {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004986 dbg_lex("%s:%d", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004987 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004988 s = zbc_lex_next(&p->l);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004989 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004990 dbg_lex("%s:%d", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004991 s = zdc_parse_token(p, t, flags);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004992 }
4993 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004994 }
4995
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004996 if (flags & BC_PARSE_NOCALL)
Gavin Howard01055ba2018-11-03 11:00:21 -06004997 bc_parse_push(p, BC_INST_POP_EXEC);
4998
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004999 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005000 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005001}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005002#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005003
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01005004static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005005{
5006 BcStatus s;
5007
5008 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005009 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005010 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005011 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005012
Denys Vlasenkod38af482018-12-04 19:11:02 +01005013 if (s || G_interrupt) {
5014 bc_parse_reset(p);
5015 s = BC_STATUS_FAILURE;
5016 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005017
Denys Vlasenko26819db2018-12-12 16:08:46 +01005018 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005019}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005020#define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005021
Gavin Howard01055ba2018-11-03 11:00:21 -06005022#endif // ENABLE_DC
5023
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005024static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005025{
5026 if (IS_BC) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01005027 IF_BC(RETURN_STATUS(zbc_parse_expr(p, flags)));
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005028 } else {
Denys Vlasenko99b37622018-12-15 20:06:59 +01005029 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags)));
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005030 }
5031}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005032#define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005033
Denys Vlasenkodf515392018-12-02 19:27:48 +01005034static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005035{
Gavin Howard01055ba2018-11-03 11:00:21 -06005036 BcId e, *ptr;
5037 BcVec *v, *map;
5038 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005039 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005040
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005041 v = var ? &G.prog.vars : &G.prog.arrs;
5042 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005043
5044 e.name = id;
5045 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005046 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005047
5048 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005049 BcVec v2;
5050 bc_array_init(&v2, var);
5051 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 }
5053
5054 ptr = bc_vec_item(map, i);
5055 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005056 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005057}
5058
Denys Vlasenko29301232018-12-11 15:29:32 +01005059static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005060{
Gavin Howard01055ba2018-11-03 11:00:21 -06005061 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005062 case BC_RESULT_STR:
5063 case BC_RESULT_TEMP:
5064 case BC_RESULT_IBASE:
5065 case BC_RESULT_SCALE:
5066 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005067 *num = &r->d.n;
5068 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005069 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005070 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005071 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005072 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005073 size_t len;
5074
5075 str = *bc_program_const(r->d.id.idx);
5076 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005077
5078 bc_num_init(&r->d.n, len);
5079
5080 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005081 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005082 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005083
5084 if (s) {
5085 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005086 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005087 }
5088
5089 *num = &r->d.n;
5090 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005091 break;
5092 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005093 case BC_RESULT_VAR:
5094 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005095 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005096 BcVec *v;
5097
Denys Vlasenkodf515392018-12-02 19:27:48 +01005098 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005099
5100 if (r->t == BC_RESULT_ARRAY_ELEM) {
5101 v = bc_vec_top(v);
5102 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5103 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005104 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005105 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005106 break;
5107 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005108#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005109 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005110 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005111 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005112 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005113 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005114 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005115#endif
5116 default:
5117 // Testing the theory that dc does not reach LAST/ONE
5118 bb_error_msg_and_die("BUG:%d", r->t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005119 }
5120
Denys Vlasenko29301232018-12-11 15:29:32 +01005121 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005122}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005123#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005124
Denys Vlasenko29301232018-12-11 15:29:32 +01005125static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005126 BcResult **r, BcNum **rn, bool assign)
5127{
5128 BcStatus s;
5129 bool hex;
5130 BcResultType lt, rt;
5131
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005132 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005133 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005134
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005135 *r = bc_vec_item_rev(&G.prog.results, 0);
5136 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005137
5138 lt = (*l)->t;
5139 rt = (*r)->t;
5140 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5141
Denys Vlasenko29301232018-12-11 15:29:32 +01005142 s = zbc_program_num(*l, ln, false);
5143 if (s) RETURN_STATUS(s);
5144 s = zbc_program_num(*r, rn, hex);
5145 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005146
5147 // We run this again under these conditions in case any vector has been
5148 // reallocated out from under the BcNums or arrays we had.
5149 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005150 s = zbc_program_num(*l, ln, false);
5151 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005152 }
5153
5154 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005155 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005156 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005157 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005158
Denys Vlasenko29301232018-12-11 15:29:32 +01005159 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005160}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005161#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005162
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005163static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005164{
5165 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005166 bc_vec_pop(&G.prog.results);
5167 bc_vec_pop(&G.prog.results);
5168 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005169}
5170
Denys Vlasenko29301232018-12-11 15:29:32 +01005171static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005172{
5173 BcStatus s;
5174
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005175 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005176 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005177 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005178
Denys Vlasenko29301232018-12-11 15:29:32 +01005179 s = zbc_program_num(*r, n, false);
5180 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005181
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005182 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005183 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005184
Denys Vlasenko29301232018-12-11 15:29:32 +01005185 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005186}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005187#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005188
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005189static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005190{
5191 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005192 bc_vec_pop(&G.prog.results);
5193 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005194}
5195
Denys Vlasenko259137d2018-12-11 19:42:05 +01005196static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005197{
5198 BcStatus s;
5199 BcResult *opd1, *opd2, res;
5200 BcNum *n1, *n2 = NULL;
5201
Denys Vlasenko29301232018-12-11 15:29:32 +01005202 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005203 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005204 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005205
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005206 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005207 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 -06005208 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005209 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005210
Denys Vlasenko259137d2018-12-11 19:42:05 +01005211 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005212 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005213 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005214 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005215}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005216#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenko26819db2018-12-12 16:08:46 +01005218static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005219{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005220 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005221 BcStatus s;
5222 BcParse parse;
5223 BcVec buf;
5224 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005225 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005226
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005227 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005228 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005229
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005230 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005231 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005232
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005233 sv_file = G.prog.file;
5234 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005235 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005236
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005237 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005238 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005239
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005240 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005241 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005242
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005243 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005244 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005245 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005246 if (s) goto exec_err;
5247
5248 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005249 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005250 goto exec_err;
5251 }
5252
5253 ip.func = BC_PROG_READ;
5254 ip.idx = 0;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005255 IF_BC(ip.len = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005256
5257 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005258 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005259
5260 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005261 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005262 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005263 bc_parse_free(&parse);
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005264 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005265 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005266 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005267 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005268}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005269#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005270
5271static size_t bc_program_index(char *code, size_t *bgn)
5272{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005273 unsigned char *bytes = (void*)(code + *bgn);
5274 unsigned amt;
5275 unsigned i;
5276 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005277
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005278 amt = *bytes++;
5279 *bgn += amt + 1;
5280
5281 amt *= 8;
5282 res = 0;
5283 for (i = 0; i < amt; i += 8)
5284 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005285
5286 return res;
5287}
5288
5289static char *bc_program_name(char *code, size_t *bgn)
5290{
5291 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005292 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005293
Denys Vlasenko694d2982018-12-18 19:17:11 +01005294 code += *bgn;
5295 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005296 i = 0;
5297 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005298 char c = *code++;
5299 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005300 break;
5301 s[i++] = c;
5302 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005303 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005304 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005305
5306 return s;
5307}
5308
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005309static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005310{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005311#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005312 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005313 // Example: echo '[]ap' | dc
5314 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005315 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005316 return;
5317 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005318#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005319 while (*str) {
5320 int c = *str++;
5321 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005322 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005323 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005324 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005325 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005326 case 'a':
5327 bb_putchar('\a');
5328 break;
5329 case 'b':
5330 bb_putchar('\b');
5331 break;
5332 case '\\':
5333 case 'e':
5334 bb_putchar('\\');
5335 break;
5336 case 'f':
5337 bb_putchar('\f');
5338 break;
5339 case 'n':
5340 bb_putchar('\n');
5341 G.prog.nchars = SIZE_MAX;
5342 break;
5343 case 'r':
5344 bb_putchar('\r');
5345 break;
5346 case 'q':
5347 bb_putchar('"');
5348 break;
5349 case 't':
5350 bb_putchar('\t');
5351 break;
5352 default:
5353 // Just print the backslash and following character.
5354 bb_putchar('\\');
5355 ++G.prog.nchars;
5356 bb_putchar(c);
5357 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005358 }
5359 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005360 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005361 }
5362}
5363
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005364static void bc_num_printNewline(void)
5365{
5366 if (G.prog.nchars == G.prog.len - 1) {
5367 bb_putchar('\\');
5368 bb_putchar('\n');
5369 G.prog.nchars = 0;
5370 }
5371}
5372
5373#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005374static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005375{
5376 (void) radix;
5377 bb_putchar((char) num);
5378 G.prog.nchars += width;
5379}
5380#endif
5381
5382static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5383{
5384 size_t exp, pow;
5385
5386 bc_num_printNewline();
5387 bb_putchar(radix ? '.' : ' ');
5388 ++G.prog.nchars;
5389
5390 bc_num_printNewline();
5391 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5392 continue;
5393
5394 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5395 size_t dig;
5396 bc_num_printNewline();
5397 dig = num / pow;
5398 num -= dig * pow;
5399 bb_putchar(((char) dig) + '0');
5400 }
5401}
5402
5403static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5404{
5405 if (radix) {
5406 bc_num_printNewline();
5407 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005408 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005409 }
5410
5411 bc_num_printNewline();
5412 bb_putchar(bb_hexdigits_upcase[num]);
5413 G.prog.nchars += width;
5414}
5415
5416static void bc_num_printDecimal(BcNum *n)
5417{
5418 size_t i, rdx = n->rdx - 1;
5419
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005420 if (n->neg) {
5421 bb_putchar('-');
5422 G.prog.nchars++;
5423 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005424
5425 for (i = n->len - 1; i < n->len; --i)
5426 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5427}
5428
Denys Vlasenkod3401432018-12-18 17:00:35 +01005429static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005430{
5431 BcStatus s;
5432 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005433 BcNum base;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005434 BcNum intp, fracp, digit, frac_len;
5435 unsigned long dig, *ptr;
5436 size_t i;
5437 bool radix;
5438
5439 if (n->len == 0) {
5440 print(0, width, false);
5441 RETURN_STATUS(BC_STATUS_SUCCESS);
5442 }
5443
5444 bc_vec_init(&stack, sizeof(long), NULL);
5445 bc_num_init(&intp, n->len);
5446 bc_num_init(&fracp, n->rdx);
5447 bc_num_init(&digit, width);
5448 bc_num_init(&frac_len, BC_NUM_INT(n));
5449 bc_num_copy(&intp, n);
5450 bc_num_one(&frac_len);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005451//TODO: have BcNumSmall type, with static buffer
5452 bc_num_init_DEF_SIZE(&base);
5453 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005454
5455 bc_num_truncate(&intp, intp.rdx);
5456 s = zbc_num_sub(n, &intp, &fracp, 0);
5457 if (s) goto err;
5458
5459 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005460 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005461 if (s) goto err;
5462 s = zbc_num_ulong(&digit, &dig);
5463 if (s) goto err;
5464 bc_vec_push(&stack, &dig);
5465 }
5466
5467 for (i = 0; i < stack.len; ++i) {
5468 ptr = bc_vec_item_rev(&stack, i);
5469 print(*ptr, width, false);
5470 }
5471
5472 if (!n->rdx) goto err;
5473
5474 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005475 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005476 if (s) goto err;
5477 s = zbc_num_ulong(&fracp, &dig);
5478 if (s) goto err;
5479 bc_num_ulong2num(&intp, dig);
5480 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5481 if (s) goto err;
5482 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005483 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005484 if (s) goto err;
5485 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005486 err:
Denys Vlasenkod3401432018-12-18 17:00:35 +01005487 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005488 bc_num_free(&frac_len);
5489 bc_num_free(&digit);
5490 bc_num_free(&fracp);
5491 bc_num_free(&intp);
5492 bc_vec_free(&stack);
5493 RETURN_STATUS(s);
5494}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005495#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005496
5497static BC_STATUS zbc_num_printBase(BcNum *n)
5498{
5499 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005500 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005501 BcNumDigitOp print;
5502 bool neg = n->neg;
5503
5504 if (neg) {
5505 bb_putchar('-');
5506 G.prog.nchars++;
5507 }
5508
5509 n->neg = false;
5510
5511 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5512 width = 1;
5513 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005514 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005515 unsigned i = G.prog.ob_t - 1;
5516 width = 0;
5517 for (;;) {
5518 width++;
5519 i /= 10;
5520 if (i == 0)
5521 break;
5522 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005523 print = bc_num_printDigits;
5524 }
5525
Denys Vlasenkod3401432018-12-18 17:00:35 +01005526 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005527 n->neg = neg;
5528
5529 RETURN_STATUS(s);
5530}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005531#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005532
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005533static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5534{
5535 BcStatus s = BC_STATUS_SUCCESS;
5536
5537 bc_num_printNewline();
5538
5539 if (n->len == 0) {
5540 bb_putchar('0');
5541 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005542 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005543 bc_num_printDecimal(n);
5544 else
5545 s = zbc_num_printBase(n);
5546
5547 if (newline) {
5548 bb_putchar('\n');
5549 G.prog.nchars = 0;
5550 }
5551
5552 RETURN_STATUS(s);
5553}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005554#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005555
Denys Vlasenko29301232018-12-11 15:29:32 +01005556static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005557{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005558 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005559 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005560 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005561 bool pop = inst != BC_INST_PRINT;
5562
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005563 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005564 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005565
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005566 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005567 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005568 s = zbc_program_num(r, &num, false);
5569 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005570
5571 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005572 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005573#if ENABLE_BC
5574 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5575#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005576 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005577 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005578
5579 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005580 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005581
5582 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005583 for (;;) {
5584 char c = *str++;
5585 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005586 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005587 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005588 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005589 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005590 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005591 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005592 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005593 }
5594 }
5595
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005596 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005597
Denys Vlasenko29301232018-12-11 15:29:32 +01005598 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005599}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005600#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005601
Denys Vlasenko29301232018-12-11 15:29:32 +01005602static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005603{
5604 BcStatus s;
5605 BcResult res, *ptr;
5606 BcNum *num = NULL;
5607
Denys Vlasenko29301232018-12-11 15:29:32 +01005608 s = zbc_program_prep(&ptr, &num);
5609 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005610
5611 bc_num_init(&res.d.n, num->len);
5612 bc_num_copy(&res.d.n, num);
5613 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5614
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005615 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005616
Denys Vlasenko29301232018-12-11 15:29:32 +01005617 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005618}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005619#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005620
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005621static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005622{
5623 BcStatus s;
5624 BcResult *opd1, *opd2, res;
5625 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005626 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005627
Denys Vlasenko29301232018-12-11 15:29:32 +01005628 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005629 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005630
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005631 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005632
5633 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005634 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005635 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005636 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005637 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005638 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005639 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005640 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005641 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005642 break;
5643 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005644 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005645 break;
5646 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005647 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005648 break;
5649 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005650 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005651 break;
5652 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005653 cond = (cond > 0);
5654 break;
5655 default: // = case BC_INST_REL_NE:
5656 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005657 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005658 }
5659 }
5660
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005661 if (cond) bc_num_one(&res.d.n);
5662 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005663
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005664 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005665
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005666 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005667}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005668#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005669
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005670#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005671static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005672{
5673 BcNum n2;
5674 BcResult res;
5675
5676 memset(&n2, 0, sizeof(BcNum));
5677 n2.rdx = res.d.id.idx = r->d.id.idx;
5678 res.t = BC_RESULT_STR;
5679
5680 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005681 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005682 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005683 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005684 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005685 }
5686
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005687 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005688
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005689 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005690 bc_vec_push(v, &n2);
5691
Denys Vlasenko29301232018-12-11 15:29:32 +01005692 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005693}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005694#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005695#endif // ENABLE_DC
5696
Denys Vlasenko29301232018-12-11 15:29:32 +01005697static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005698{
5699 BcStatus s;
5700 BcResult *ptr, r;
5701 BcVec *v;
5702 BcNum *n;
5703
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005704 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005705 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005706
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005707 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005708 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005709 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005710 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005711
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005712#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005713 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005714 RETURN_STATUS(bc_error_variable_is_wrong_type());
5715 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005716 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005717#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005718
Denys Vlasenko29301232018-12-11 15:29:32 +01005719 s = zbc_program_num(ptr, &n, false);
5720 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005721
5722 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005723 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005724
5725 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005726 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005727 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005728 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005729 bc_array_init(&r.d.v, true);
5730 bc_array_copy(&r.d.v, (BcVec *) n);
5731 }
5732
5733 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005734 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005735
Denys Vlasenko29301232018-12-11 15:29:32 +01005736 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005737}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005738#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005739
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005740static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005741{
5742 BcStatus s;
5743 BcResult *left, *right, res;
5744 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005745 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5746
Denys Vlasenko29301232018-12-11 15:29:32 +01005747 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005748 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005749
5750 ib = left->t == BC_RESULT_IBASE;
5751 sc = left->t == BC_RESULT_SCALE;
5752
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005753#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005754 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005755 BcVec *v;
5756
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005757 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005758 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005759 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005760
Denys Vlasenkofa210792018-12-19 19:35:40 +01005761 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005762 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005763#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005764
5765 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005766 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005767 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005768 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005769 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005770
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005771#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005772 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005773 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005774
5775 if (assign)
5776 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005777 else {
5778 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005779 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 +01005780 }
5781 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005782#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005783 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005784#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005785
5786 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005787 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005788 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5789 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5790 NULL, //can't happen //BC_RESULT_LAST
5791 NULL, //can't happen //BC_RESULT_CONSTANT
5792 NULL, //can't happen //BC_RESULT_ONE
5793 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005794 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005795 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005796 size_t max;
5797 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005798
Denys Vlasenko29301232018-12-11 15:29:32 +01005799 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005800 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005801 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005802 if (sc) {
5803 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005804 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005805 } else {
5806 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005807 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005808 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005809 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005810 }
5811
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005812 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005813 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005814
5815 *ptr = (size_t) val;
5816 s = BC_STATUS_SUCCESS;
5817 }
5818
5819 bc_num_init(&res.d.n, l->len);
5820 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005821 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005822
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005823 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005824}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005825#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005826
Denys Vlasenko416ce762018-12-02 20:57:17 +01005827#if !ENABLE_DC
5828#define bc_program_pushVar(code, bgn, pop, copy) \
5829 bc_program_pushVar(code, bgn)
5830// for bc, 'pop' and 'copy' are always false
5831#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005832static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005833 bool pop, bool copy)
5834{
Gavin Howard01055ba2018-11-03 11:00:21 -06005835 BcResult r;
5836 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005837
5838 r.t = BC_RESULT_VAR;
5839 r.d.id.name = name;
5840
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005841#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005842 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005843 BcVec *v = bc_program_search(name, true);
5844 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005845
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005846 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005847 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005848 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005849 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005850
5851 if (!BC_PROG_STR(num)) {
5852 r.t = BC_RESULT_TEMP;
5853 bc_num_init_DEF_SIZE(&r.d.n);
5854 bc_num_copy(&r.d.n, num);
5855 } else {
5856 r.t = BC_RESULT_STR;
5857 r.d.id.idx = num->rdx;
5858 }
5859
5860 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005861 }
5862#endif // ENABLE_DC
5863
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005864 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005865
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005866 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005867}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005868#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005869
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005870static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005871{
5872 BcStatus s = BC_STATUS_SUCCESS;
5873 BcResult r;
5874 BcNum *num;
5875
5876 r.d.id.name = bc_program_name(code, bgn);
5877
5878 if (inst == BC_INST_ARRAY) {
5879 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005880 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005881 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005882 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 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005898 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005899 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005900 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005901}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005902#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005903
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005904#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005905static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005906{
5907 BcStatus s;
5908 BcResult *ptr, res, copy;
5909 BcNum *num = NULL;
5910 char inst2 = inst;
5911
Denys Vlasenko29301232018-12-11 15:29:32 +01005912 s = zbc_program_prep(&ptr, &num);
5913 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914
5915 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5916 copy.t = BC_RESULT_TEMP;
5917 bc_num_init(&copy.d.n, num->len);
5918 bc_num_copy(&copy.d.n, num);
5919 }
5920
5921 res.t = BC_RESULT_ONE;
5922 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5923 BC_INST_ASSIGN_PLUS :
5924 BC_INST_ASSIGN_MINUS;
5925
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005926 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005927 s = zbc_program_assign(inst);
5928 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005929
5930 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005931 bc_vec_pop(&G.prog.results);
5932 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005933 }
5934
Denys Vlasenko29301232018-12-11 15:29:32 +01005935 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005936}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005937#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005938
Denys Vlasenko29301232018-12-11 15:29:32 +01005939static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005940{
Gavin Howard01055ba2018-11-03 11:00:21 -06005941 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005942 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005944 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005945 BcResult *arg;
5946
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005947 nparams = bc_program_index(code, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005948 ip.idx = 0;
5949 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005950 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005951
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005952 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005953 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005954 }
5955 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005956 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005957 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005958 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005959
5960 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005961 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005962
5963 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005964 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005965
5966 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005967 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005968
Denys Vlasenko29301232018-12-11 15:29:32 +01005969 s = zbc_program_copyToVar(a->name, a->idx);
5970 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005971 }
5972
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005973 a = bc_vec_item(&func->autos, i);
5974 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005975 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005976
Denys Vlasenkodf515392018-12-02 19:27:48 +01005977 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005978 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005979 BcNum n2;
5980 bc_num_init_DEF_SIZE(&n2);
5981 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005982 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005983 BcVec v2;
5984 bc_array_init(&v2, true);
5985 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005986 }
5987 }
5988
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005989 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005990
Denys Vlasenko29301232018-12-11 15:29:32 +01005991 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005992}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005993#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005994
Denys Vlasenko29301232018-12-11 15:29:32 +01005995static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005996{
Gavin Howard01055ba2018-11-03 11:00:21 -06005997 BcResult res;
5998 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005999 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006000 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006001 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006002
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006003 if (!STACK_HAS_EQUAL_OR_MORE_THAN(&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);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006018 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006019 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006020 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 }
6022
6023 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006024 a = (void*)f->autos.v;
6025 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006027 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006028 bc_vec_pop(v);
6029 }
6030
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006031 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6032 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006033 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006034
Denys Vlasenko29301232018-12-11 15:29:32 +01006035 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006037#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006038#endif // ENABLE_BC
6039
6040static unsigned long bc_program_scale(BcNum *n)
6041{
6042 return (unsigned long) n->rdx;
6043}
6044
6045static unsigned long bc_program_len(BcNum *n)
6046{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006047 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006048
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006049 if (n->rdx != len) return len;
6050 for (;;) {
6051 if (len == 0) break;
6052 len--;
6053 if (n->num[len] != 0) break;
6054 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006055 return len;
6056}
6057
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006058static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006059{
6060 BcStatus s;
6061 BcResult *opnd;
6062 BcNum *num = NULL;
6063 BcResult res;
6064 bool len = inst == BC_INST_LENGTH;
6065
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006066 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006067 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006068 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006069
Denys Vlasenko29301232018-12-11 15:29:32 +01006070 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006071 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006072
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006073#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006074 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006075 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006076#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006077
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006078 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006079
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006080 if (inst == BC_INST_SQRT)
6081 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006082#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006083 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006084 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006085 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006086#endif
6087#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006088 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 char **str;
6090 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6091
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006092 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006093 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006094 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006095#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006097 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006098 }
6099
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006100 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006101
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006102 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006103}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006104#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006106#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006107static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006108{
6109 BcStatus s;
6110 BcResult *opd1, *opd2, res, res2;
6111 BcNum *n1, *n2 = NULL;
6112
Denys Vlasenko29301232018-12-11 15:29:32 +01006113 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006114 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006115
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006116 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006117 bc_num_init(&res2.d.n, n2->len);
6118
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006119 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006120 if (s) goto err;
6121
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006122 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006124 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006126 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006127 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006128 bc_num_free(&res2.d.n);
6129 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006130 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006131}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006132#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006133
Denys Vlasenkofa210792018-12-19 19:35:40 +01006134static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006135{
6136 BcStatus s;
6137 BcResult *r1, *r2, *r3, res;
6138 BcNum *n1, *n2, *n3;
6139
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006140 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006141 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006142 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006143 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006144
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006145 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006146 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006147 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006148 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006149 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
6151 // Make sure that the values have their pointers updated, if necessary.
6152 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006153 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006154 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006155 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006156 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006157 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006158 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006159 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 }
6161 }
6162
6163 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006164 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 if (s) goto err;
6166
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006167 bc_vec_pop(&G.prog.results);
6168 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006169
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006170 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006171 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006172 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006173 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006175#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006176
Denys Vlasenkofa210792018-12-19 19:35:40 +01006177static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006178{
Gavin Howard01055ba2018-11-03 11:00:21 -06006179 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006180 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006181
6182 res.t = BC_RESULT_TEMP;
6183
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006184 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006185 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006186 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006187}
6188
Denys Vlasenkofa210792018-12-19 19:35:40 +01006189static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006190{
6191 BcStatus s;
6192 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006193 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006194 char **strs;
6195 char *str;
6196 char c;
6197 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006198 unsigned long val;
6199
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006200 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006201 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006202 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006204 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006205 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006206 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006207
6208 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01006209 BcNum strmb;
6210
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006211 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006212 bc_num_copy(&n, num);
6213 bc_num_truncate(&n, n.rdx);
6214
Denys Vlasenkod3401432018-12-18 17:00:35 +01006215//TODO: have BcNumSmall type, with static buffer
6216 bc_num_init_DEF_SIZE(&strmb);
6217 bc_num_ulong2num(&strmb, 0x100);
6218 s = zbc_num_mod(&n, &strmb, &n, 0);
6219 bc_num_free(&strmb);
6220
Gavin Howard01055ba2018-11-03 11:00:21 -06006221 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006222 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006223 if (s) goto num_err;
6224
6225 c = (char) val;
6226
6227 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006228 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006229 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006230 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006231 sp = *bc_program_str(idx);
6232 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006233 }
6234
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006235 strs = (void*)G.prog.strs.v;
6236 for (idx = 0; idx < G.prog.strs.len; idx++) {
6237 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6238 goto dup;
6239 }
6240 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006241 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006242 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006243 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006244 bc_vec_push(&G.prog.strs, &str);
6245 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006246 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006247 res.d.id.idx = idx;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006248 bc_vec_pop(&G.prog.results);
6249 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006250
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006251 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006252 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006253 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006254 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006256#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
Denys Vlasenkofa210792018-12-19 19:35:40 +01006258static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006259{
6260 BcStatus s;
6261 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006262 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006263 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006265 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006266 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006267 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006268
Denys Vlasenkofa210792018-12-19 19:35:40 +01006269 n = NULL; // is this needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006270 s = zbc_program_num(r, &n, false);
6271 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006272
Denys Vlasenko57734c92018-12-17 21:14:05 +01006273 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006274 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006275 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006276 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006277 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006278 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006279 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006280 }
6281
Denys Vlasenko29301232018-12-11 15:29:32 +01006282 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006283}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006284#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006285
Denys Vlasenkofa210792018-12-19 19:35:40 +01006286static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006287{
6288 BcStatus s;
6289 BcResult *opnd;
6290 BcNum *num = NULL;
6291 unsigned long val;
6292
Denys Vlasenko29301232018-12-11 15:29:32 +01006293 s = zbc_program_prep(&opnd, &num);
6294 if (s) RETURN_STATUS(s);
6295 s = zbc_num_ulong(num, &val);
6296 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006297
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006298 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006299
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006300 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006301 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006302 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006303 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006304 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006305
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006306 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307
Denys Vlasenko29301232018-12-11 15:29:32 +01006308 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006309}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006310#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006311
Denys Vlasenkofa210792018-12-19 19:35:40 +01006312static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006313{
6314 BcStatus s = BC_STATUS_SUCCESS;
6315 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006316 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006317 BcInstPtr ip;
6318 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006319
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006320 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006321 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006322
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006323 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324
6325 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006326 BcNum *n = n; // for compiler
6327 bool exec;
6328 char *name;
6329 char *then_name = bc_program_name(code, bgn);
6330 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
6332 if (code[*bgn] == BC_PARSE_STREND)
6333 (*bgn) += 1;
6334 else
6335 else_name = bc_program_name(code, bgn);
6336
6337 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006338 name = then_name;
6339 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006340 exec = true;
6341 name = else_name;
6342 }
6343
6344 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006345 BcVec *v;
6346 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006347 n = bc_vec_top(v);
6348 }
6349
6350 free(then_name);
6351 free(else_name);
6352
6353 if (!exec) goto exit;
6354 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006355 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 goto exit;
6357 }
6358
6359 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006360 } else {
6361 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006363 } else if (r->t == BC_RESULT_VAR) {
6364 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006365 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006366 if (s || !BC_PROG_STR(n)) goto exit;
6367 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006368 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006369 goto exit;
6370 }
6371
6372 fidx = sidx + BC_PROG_REQ_FUNCS;
6373
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006374 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006375
6376 if (f->code.len == 0) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006377 BcParse prs;
6378 char *str;
6379
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006380 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006381 str = *bc_program_str(sidx);
6382 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006383 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006384 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006385 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06006386 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006387 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006388 err:
6389 bc_parse_free(&prs);
6390 bc_vec_pop_all(&f->code);
6391 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006392 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006393 bc_parse_free(&prs);
6394 }
6395
6396 ip.idx = 0;
Denys Vlasenko503faf92018-12-20 16:24:18 +01006397 IF_BC(ip.len = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06006398 ip.func = fidx;
6399
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006400 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006401 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006402
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006403 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006404 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006406 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006407}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006408#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006409#endif // ENABLE_DC
6410
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006411static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006412{
Gavin Howard01055ba2018-11-03 11:00:21 -06006413 BcResult res;
6414 unsigned long val;
6415
6416 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6417 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006418 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006419 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006420 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006421 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006422 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006423
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006424 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006425 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006426 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006427}
6428
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006429static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006430{
Gavin Howard01055ba2018-11-03 11:00:21 -06006431 BcResult r, *ptr;
6432 BcNum *num;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006433 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006434 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006435 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006436
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006437 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006438 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006439 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006440 char inst = code[ip->idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006441
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006442 dbg_exec("inst at %zd:%d", ip->idx - 1, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006444#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006445 case BC_INST_JUMP_ZERO: {
6446 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006447 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006448 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006449 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006450 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006451 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006452 if (!zero) {
6453 bc_program_index(code, &ip->idx);
6454 break;
6455 }
6456 // else: fall through
6457 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006458 case BC_INST_JUMP: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006459 size_t idx = bc_program_index(code, &ip->idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006460 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006461 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006462 ip->idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006463 break;
6464 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006465 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006466 dbg_exec("BC_INST_CALL:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006467 s = zbc_program_call(code, &ip->idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006468 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006469 case BC_INST_INC_PRE:
6470 case BC_INST_DEC_PRE:
6471 case BC_INST_INC_POST:
6472 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006473 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006474 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006476 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006477 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006478 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006479 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 case BC_INST_RET:
6481 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006482 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006483 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006484 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006485 case BC_INST_BOOL_OR:
6486 case BC_INST_BOOL_AND:
6487#endif // ENABLE_BC
6488 case BC_INST_REL_EQ:
6489 case BC_INST_REL_LE:
6490 case BC_INST_REL_GE:
6491 case BC_INST_REL_NE:
6492 case BC_INST_REL_LT:
6493 case BC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006494 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006495 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006496 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006497 case BC_INST_READ:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006498 dbg_exec("BC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006499 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006500 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006501 case BC_INST_VAR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006502 dbg_exec("BC_INST_VAR:");
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006503 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006504 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006505 case BC_INST_ARRAY_ELEM:
6506 case BC_INST_ARRAY:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006507 dbg_exec("BC_INST_ARRAY[_ELEM]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006508 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006509 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006510 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006512 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006513 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006514 case BC_INST_IBASE:
6515 case BC_INST_SCALE:
6516 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006517 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006518 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 case BC_INST_SCALE_FUNC:
6520 case BC_INST_LENGTH:
6521 case BC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006522 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006523 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006524 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006525 case BC_INST_NUM:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006526 dbg_exec("BC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006527 r.t = BC_RESULT_CONSTANT;
6528 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006529 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006530 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006531 case BC_INST_POP:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006532 dbg_exec("BC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006533 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006534 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006535 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006536 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006537 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006538 case BC_INST_POP_EXEC:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006539 dbg_exec("BC_INST_POP_EXEC:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006540 bc_vec_pop(&G.prog.exestack);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006541 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006542 case BC_INST_PRINT:
6543 case BC_INST_PRINT_POP:
6544 case BC_INST_PRINT_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006545 dbg_exec("BC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006546 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006547 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006548 case BC_INST_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006549 dbg_exec("BC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006550 r.t = BC_RESULT_STR;
6551 r.d.id.idx = bc_program_index(code, &ip->idx);
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_POWER:
6555 case BC_INST_MULTIPLY:
6556 case BC_INST_DIVIDE:
6557 case BC_INST_MODULUS:
6558 case BC_INST_PLUS:
6559 case BC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006560 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006561 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006562 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006563 case BC_INST_BOOL_NOT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006564 dbg_exec("BC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006565 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006566 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006567 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006568 if (!bc_num_cmp(num, &G.prog.zero))
6569 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006570 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006571 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006572 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006573 case BC_INST_NEG:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006574 dbg_exec("BC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006575 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006576 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006577#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 case BC_INST_ASSIGN_POWER:
6579 case BC_INST_ASSIGN_MULTIPLY:
6580 case BC_INST_ASSIGN_DIVIDE:
6581 case BC_INST_ASSIGN_MODULUS:
6582 case BC_INST_ASSIGN_PLUS:
6583 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006584#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006585 case BC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006586 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006587 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006588 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006589#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 case BC_INST_MODEXP:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006591 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006592 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 case BC_INST_DIVMOD:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006594 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006596 case BC_INST_EXECUTE:
6597 case BC_INST_EXEC_COND:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006598 s = zdc_program_execStr(code, &ip->idx, inst == BC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006599 goto read_updated_ip;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006600 case BC_INST_PRINT_STACK: {
6601 size_t idx;
6602 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006603 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006604 if (s) break;
6605 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006606 break;
6607 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006608 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006609 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006610 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006611 case BC_INST_STACK_LEN:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006612 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006613 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006614 case BC_INST_DUPLICATE:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006615 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006616 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006617 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006618 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006619 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006621 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006622 BcResult *ptr2;
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006623 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006624 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006625 ptr = bc_vec_item_rev(&G.prog.results, 0);
6626 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006627 memcpy(&r, ptr, sizeof(BcResult));
6628 memcpy(ptr, ptr2, sizeof(BcResult));
6629 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006630 break;
6631 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006632 case BC_INST_ASCIIFY:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006633 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006634 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006635 case BC_INST_PRINT_STREAM:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006636 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006637 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006638 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006639 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006640 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006641 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006642 break;
6643 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006644 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006645 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006646 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006647 free(name);
6648 break;
6649 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006650 case BC_INST_QUIT:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006651 dbg_exec("BC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006652 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006653 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006654 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006655 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006656 case BC_INST_NQUIT:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006657 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006658 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006659#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006660 read_updated_ip:
6661 // Instruction stack has changed, read new pointers
6662 ip = bc_vec_top(&G.prog.exestack);
6663 func = bc_program_func(ip->func);
6664 code = func->code.v;
6665 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006666 }
6667
Denys Vlasenkod38af482018-12-04 19:11:02 +01006668 if (s || G_interrupt) {
6669 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006670 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006671 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006672
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006673 fflush_and_check();
6674
Gavin Howard01055ba2018-11-03 11:00:21 -06006675 }
6676
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006677 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006678}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006679#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006680
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006681static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006682{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006683 char *lenv;
6684 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006685
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006686 lenv = getenv(var);
6687 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006688 if (!lenv) return len;
6689
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006690 len = bb_strtou(lenv, NULL, 10) - 1;
6691 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006692 len = BC_NUM_PRINT_WIDTH;
6693
6694 return len;
6695}
6696
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006697static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006698{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006699 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006700
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006701 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
6702 s = zbc_parse_text_init(&G.prs, text);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006703 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006704
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006705 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006706 dbg_lex("%s:%d G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenkoec603182018-12-17 10:34:02 +01006707 s = zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006708 if (s) RETURN_STATUS(s);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006709 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006710 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006711 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006712 break;
6713 }
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006714 // bc discards strings, constants and code after each
6715 // top-level statement in the "main program".
6716 // This prevents "yes 1 | bc" from growing its memory
6717 // without bound. This can be done because data stack
6718 // is empty and thus can't hold any references to
6719 // strings or constants, there is no generated code
6720 // which can hold references (after we discard one
6721 // we just executed). Code of functions can have references,
6722 // but bc stores function strings/constants in per-function
6723 // storage.
6724 if (IS_BC) {
6725 BcFunc *f;
6726 BcInstPtr *ip;
6727
6728 //FIXME: this does not clear up the stack
6729 //for(i=1; i<3; i++) {
6730 // i
6731 // if(i==2) continue
6732 // 77
6733 //}
6734// if (G.prog.results.len != 0)
6735// bb_error_msg_and_die("data stack not empty: %d slots", G.prog.results.len);
6736
6737 if (G.prog.exestack.len != 1) // should be empty
6738 bb_error_msg_and_die("BUG:call stack");
6739
6740 ip = (void*)G.prog.exestack.v;
6741 if (ip->func != BC_PROG_MAIN)
6742 bb_error_msg_and_die("BUG:not MAIN");
6743//bb_error_msg("ip->func:%d >idx:%d >len:%d", ip->func, ip->idx, ip->len);
6744 f = bc_program_func_BC_PROG_MAIN();
6745//bb_error_msg("MAIN->code.len:%d >strs.len:%d >consts.len:%d", f->code.len, f->strs.len, f->consts.len); // labels, autos, nparams
6746 bc_vec_pop_all(&f->code);
6747 ip->idx = 0;
6748 IF_BC(bc_vec_pop_all(&f->strs);)
6749 IF_BC(bc_vec_pop_all(&f->consts);)
6750 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006751 }
6752
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006753 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006754 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006755}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006756#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006757
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006758static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006759{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006760 // So far bc/dc have no way to include a file from another file,
6761 // therefore we know G.prog.file == NULL on entry
6762 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006763 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006764
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006765 G.prog.file = filename;
6766 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006767 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006768
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006769 do {
6770 s = zbc_vm_process("");
6771 // We do not stop looping on errors here if reading stdin.
6772 // Example: start interactive bc and enter "return".
6773 // It should say "'return' not in a function"
6774 // but should not exit.
6775 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006776 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006777 RETURN_STATUS(s);
6778}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006779#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006780
6781static BC_STATUS zbc_vm_file(const char *file)
6782{
6783 BcStatus s;
6784 FILE *fp;
6785
6786 fp = xfopen_for_read(file);
6787 s = zbc_vm_execute_FILE(fp, file);
6788 fclose(fp);
6789
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006790 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006791}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006792#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006793
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006794#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006795static void bc_vm_info(void)
6796{
6797 printf("%s "BB_VER"\n"
6798 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6799 , applet_name);
6800}
6801
6802static void bc_args(char **argv)
6803{
6804 unsigned opts;
6805 int i;
6806
6807 GETOPT_RESET();
6808#if ENABLE_FEATURE_BC_LONG_OPTIONS
6809 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6810 "warn\0" No_argument "w"
6811 "version\0" No_argument "v"
6812 "standard\0" No_argument "s"
6813 "quiet\0" No_argument "q"
6814 "mathlib\0" No_argument "l"
6815 "interactive\0" No_argument "i"
6816 );
6817#else
6818 opts = option_mask32 |= getopt32(argv, "wvsqli");
6819#endif
6820 if (getenv("POSIXLY_CORRECT"))
6821 option_mask32 |= BC_FLAG_S;
6822
6823 if (opts & BC_FLAG_V) {
6824 bc_vm_info();
6825 exit(0);
6826 }
6827
6828 for (i = optind; argv[i]; ++i)
6829 bc_vec_push(&G.files, argv + i);
6830}
6831
6832static void bc_vm_envArgs(void)
6833{
6834 BcVec v;
6835 char *buf;
6836 char *env_args = getenv("BC_ENV_ARGS");
6837
6838 if (!env_args) return;
6839
6840 G.env_args = xstrdup(env_args);
6841 buf = G.env_args;
6842
6843 bc_vec_init(&v, sizeof(char *), NULL);
6844
6845 while (*(buf = skip_whitespace(buf)) != '\0') {
6846 bc_vec_push(&v, &buf);
6847 buf = skip_non_whitespace(buf);
6848 if (!*buf)
6849 break;
6850 *buf++ = '\0';
6851 }
6852
6853 // NULL terminate, and pass argv[] so that first arg is argv[1]
6854 if (sizeof(int) == sizeof(char*)) {
6855 bc_vec_push(&v, &const_int_0);
6856 } else {
6857 static char *const nullptr = NULL;
6858 bc_vec_push(&v, &nullptr);
6859 }
6860 bc_args(((char **)v.v) - 1);
6861
6862 bc_vec_free(&v);
6863}
6864
6865static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006866 "scale=20"
6867"\n" "define e(x){"
6868"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006869////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6870//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6871//e(-.998896): GNU:.36828580434569428695
6872// above code:.36828580434569428696
6873// actual value:.3682858043456942869594...
6874// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006875"\n" "b=ibase"
6876"\n" "ibase=A"
6877"\n" "if(x<0){"
6878"\n" "n=1"
6879"\n" "x=-x"
6880"\n" "}"
6881"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006882"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006883"\n" "scale=scale(x)+1"
6884"\n" "while(x>1){"
6885"\n" "d+=1"
6886"\n" "x/=2"
6887"\n" "scale+=1"
6888"\n" "}"
6889"\n" "scale=r"
6890"\n" "r=x+1"
6891"\n" "p=x"
6892"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006893"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006894"\n" "p*=x"
6895"\n" "f*=i"
6896"\n" "v=p/f"
6897"\n" "r+=v"
6898"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006899"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006900"\n" "scale=s"
6901"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006902"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006903"\n" "return(r/1)"
6904"\n" "}"
6905"\n" "define l(x){"
6906"\n" "auto b,s,r,p,a,q,i,v"
6907"\n" "b=ibase"
6908"\n" "ibase=A"
6909"\n" "if(x<=0){"
6910"\n" "r=(1-10^scale)/1"
6911"\n" "ibase=b"
6912"\n" "return(r)"
6913"\n" "}"
6914"\n" "s=scale"
6915"\n" "scale+=6"
6916"\n" "p=2"
6917"\n" "while(x>=2){"
6918"\n" "p*=2"
6919"\n" "x=sqrt(x)"
6920"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006921"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006922"\n" "p*=2"
6923"\n" "x=sqrt(x)"
6924"\n" "}"
6925"\n" "r=a=(x-1)/(x+1)"
6926"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006927"\n" "v=1"
6928"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006929"\n" "a*=q"
6930"\n" "v=a/i"
6931"\n" "r+=v"
6932"\n" "}"
6933"\n" "r*=p"
6934"\n" "scale=s"
6935"\n" "ibase=b"
6936"\n" "return(r/1)"
6937"\n" "}"
6938"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006939"\n" "auto b,s,r,a,q,i"
6940"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006941"\n" "b=ibase"
6942"\n" "ibase=A"
6943"\n" "s=scale"
6944"\n" "scale=1.1*s+2"
6945"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006946"\n" "scale=0"
6947"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006948"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006949"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006950"\n" "scale=s+2"
6951"\n" "r=a=x"
6952"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006953"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006954"\n" "a*=q/(i*(i-1))"
6955"\n" "r+=a"
6956"\n" "}"
6957"\n" "scale=s"
6958"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006959"\n" "return(r/1)"
6960"\n" "}"
6961"\n" "define c(x){"
6962"\n" "auto b,s"
6963"\n" "b=ibase"
6964"\n" "ibase=A"
6965"\n" "s=scale"
6966"\n" "scale*=1.2"
6967"\n" "x=s(2*a(1)+x)"
6968"\n" "scale=s"
6969"\n" "ibase=b"
6970"\n" "return(x/1)"
6971"\n" "}"
6972"\n" "define a(x){"
6973"\n" "auto b,s,r,n,a,m,t,f,i,u"
6974"\n" "b=ibase"
6975"\n" "ibase=A"
6976"\n" "n=1"
6977"\n" "if(x<0){"
6978"\n" "n=-1"
6979"\n" "x=-x"
6980"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006981"\n" "if(scale<65){"
6982"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
6983"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006984"\n" "}"
6985"\n" "s=scale"
6986"\n" "if(x>.2){"
6987"\n" "scale+=5"
6988"\n" "a=a(.2)"
6989"\n" "}"
6990"\n" "scale=s+3"
6991"\n" "while(x>.2){"
6992"\n" "m+=1"
6993"\n" "x=(x-.2)/(1+.2*x)"
6994"\n" "}"
6995"\n" "r=u=x"
6996"\n" "f=-x*x"
6997"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006998"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006999"\n" "u*=f"
7000"\n" "t=u/i"
7001"\n" "r+=t"
7002"\n" "}"
7003"\n" "scale=s"
7004"\n" "ibase=b"
7005"\n" "return((m*a+r)/n)"
7006"\n" "}"
7007"\n" "define j(n,x){"
7008"\n" "auto b,s,o,a,i,v,f"
7009"\n" "b=ibase"
7010"\n" "ibase=A"
7011"\n" "s=scale"
7012"\n" "scale=0"
7013"\n" "n/=1"
7014"\n" "if(n<0){"
7015"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007016"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007017"\n" "}"
7018"\n" "a=1"
7019"\n" "for(i=2;i<=n;++i)a*=i"
7020"\n" "scale=1.5*s"
7021"\n" "a=(x^n)/2^n/a"
7022"\n" "r=v=1"
7023"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007024"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007025"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007026"\n" "v=v*f/i/(n+i)"
7027"\n" "r+=v"
7028"\n" "}"
7029"\n" "scale=s"
7030"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007031"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007032"\n" "return(a*r/1)"
7033"\n" "}"
7034};
7035#endif // ENABLE_BC
7036
Denys Vlasenko19f11072018-12-12 22:48:19 +01007037static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007038{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007039 char **fname;
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 Vlasenkoea5cad22018-12-19 18:09:31 +01007055 fname = (void*)G.files.v;
7056 for (i = 0; i < G.files.len; i++) {
7057 s = zbc_vm_file(*fname++);
7058 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7059 // Debug config, non-interactive mode:
7060 // return all the way back to main.
7061 // Non-debug builds do not come here
7062 // in non-interactive mode, they exit.
7063 RETURN_STATUS(s);
7064 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007065 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007066
Denys Vlasenko91cde952018-12-10 20:56:08 +01007067 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007068 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007069
Denys Vlasenko19f11072018-12-12 22:48:19 +01007070 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007071}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007072#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007073
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007074#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007075static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007076{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007077 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007078 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007079 bc_vec_free(&G.prog.vars);
7080 bc_vec_free(&G.prog.var_map);
7081 bc_vec_free(&G.prog.arrs);
7082 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007083 IF_DC(bc_vec_free(&G.prog.strs);)
7084 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007085 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007086 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007087 IF_BC(bc_num_free(&G.prog.last);)
7088 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007089 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007090 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007091}
7092
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007093static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007094{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007095 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007096 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007097 bc_parse_free(&G.prs);
7098 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007099}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007100#endif
7101
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007102static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007103{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007104 BcInstPtr ip;
7105
Denys Vlasenko82269122018-12-14 16:30:56 +01007106 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007107 memset(&ip, 0, sizeof(BcInstPtr));
7108
Denys Vlasenko82269122018-12-14 16:30:56 +01007109 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007110 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007111 G.prog.ob_t = 10;
7112
Denys Vlasenko503faf92018-12-20 16:24:18 +01007113 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7114 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007115
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007116 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007117 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007118
Denys Vlasenko503faf92018-12-20 16:24:18 +01007119 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7120 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007121
7122 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007123 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007124
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007125 if (IS_BC) {
Denys Vlasenko04715442018-12-21 00:10:26 +01007126 // Names are chosen simply to never match
7127 // a valid function name (and be short)
7128 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
7129 IF_BC(bc_program_addFunc(xstrdup(""))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007130 } else {
7131 bc_program_add_fn();
7132 bc_program_add_fn();
7133 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007134
7135 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007136 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007137
7138 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007139 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007140
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007141 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7142 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007143 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007144 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7145 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007146
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007147 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007148}
Gavin Howard01055ba2018-11-03 11:00:21 -06007149
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007150static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007151{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007152#if ENABLE_FEATURE_EDITING
7153 G.line_input_state = new_line_input_t(DO_HISTORY);
7154#endif
7155 G.prog.len = bc_vm_envLen(env_len);
7156
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007157 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007158 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007159 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007160 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007161
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007162//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7163//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007164 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007165#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007166 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007167 // With SA_RESTART, most system calls will restart
7168 // (IOW: they won't fail with EINTR).
7169 // In particular, this means ^C won't cause
7170 // stdout to get into "error state" if SIGINT hits
7171 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007172 //
7173 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007174 // will only be handled after [Enter] since read()
7175 // from stdin is not interrupted by ^C either,
7176 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007177 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007178 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7179
7180 // Without SA_RESTART, this exhibits a bug:
7181 // "while (1) print 1" and try ^C-ing it.
7182 // Intermittently, instead of returning to input line,
7183 // you'll get "output error: Interrupted system call"
7184 // and exit.
7185 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007186#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007187 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007188 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007189 return 0; // "not a tty"
7190}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007191
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007192static BcStatus bc_vm_run(void)
7193{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007194 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007195#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007196 if (G_exiting) // it was actually "halt" or "quit"
7197 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007198 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007199# if ENABLE_FEATURE_EDITING
7200 free_line_input_t(G.line_input_state);
7201# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007202 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007203#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007204 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007205 return st;
7206}
7207
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007208#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007209int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007210int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007211{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007212 int is_tty;
7213
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007214 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007215
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007216 is_tty = bc_vm_init("BC_LINE_LENGTH");
7217
7218 bc_args(argv);
7219
7220 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7221 bc_vm_info();
7222
7223 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007224}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007225#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007226
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007227#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007228int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007229int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007230{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007231 int noscript;
7232
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007233 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007234
7235 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7236 // 1 char wider than bc from the same package.
7237 // Both default width, and xC_LINE_LENGTH=N are wider:
7238 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007239 // |1234\ |
7240 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007241 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007242 // |123\ |
7243 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007244 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007245 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007246
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007247 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7248 noscript = BC_FLAG_I;
7249 for (;;) {
7250 int n = getopt(argc, argv, "e:f:x");
7251 if (n <= 0)
7252 break;
7253 switch (n) {
7254 case 'e':
7255 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007256 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007257 if (n) return n;
7258 break;
7259 case 'f':
7260 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007261 n = zbc_vm_file(optarg);
7262 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007263 break;
7264 case 'x':
7265 option_mask32 |= DC_FLAG_X;
7266 break;
7267 default:
7268 bb_show_usage();
7269 }
7270 }
7271 argv += optind;
7272
7273 while (*argv) {
7274 noscript = 0;
7275 bc_vec_push(&G.files, argv++);
7276 }
7277
7278 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7279
7280 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007281}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007282#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007283
7284#endif // not DC_SMALL