blob: bb83e0a142c53126d70cf538f4cb3b8086a5b1c3 [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 Vlasenko19eee8e2018-12-21 20:29:34 +0100173// This can be left enabled for production as well:
174#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100175
176#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100177static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100178#define dbg_lex(...) \
179 do { \
180 fprintf(stderr, "%*s", lex_indent, ""); \
181 bb_error_msg(__VA_ARGS__); \
182 } while (0)
183#define dbg_lex_enter(...) \
184 do { \
185 dbg_lex(__VA_ARGS__); \
186 lex_indent++; \
187 } while (0)
188#define dbg_lex_done(...) \
189 do { \
190 lex_indent--; \
191 dbg_lex(__VA_ARGS__); \
192 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100193#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100194# define dbg_lex(...) ((void)0)
195# define dbg_lex_enter(...) ((void)0)
196# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100197#endif
198
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100199#if DEBUG_COMPILE
200# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
201#else
202# define dbg_compile(...) ((void)0)
203#endif
204
Denys Vlasenko99b37622018-12-15 20:06:59 +0100205#if DEBUG_EXEC
206# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
207#else
208# define dbg_exec(...) ((void)0)
209#endif
210
Gavin Howard01055ba2018-11-03 11:00:21 -0600211typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100212 BC_STATUS_SUCCESS = 0,
213 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100214 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600215} BcStatus;
216
Gavin Howard01055ba2018-11-03 11:00:21 -0600217#define BC_VEC_INVALID_IDX ((size_t) -1)
218#define BC_VEC_START_CAP (1 << 5)
219
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100220typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
222typedef struct BcVec {
223 char *v;
224 size_t len;
225 size_t cap;
226 size_t size;
227 BcVecFree dtor;
228} BcVec;
229
Gavin Howard01055ba2018-11-03 11:00:21 -0600230typedef signed char BcDig;
231
232typedef struct BcNum {
233 BcDig *restrict num;
234 size_t rdx;
235 size_t len;
236 size_t cap;
237 bool neg;
238} BcNum;
239
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100240#define BC_NUM_MAX_IBASE ((unsigned long) 16)
241// larger value might speed up BIGNUM calculations a bit:
242#define BC_NUM_DEF_SIZE (16)
243#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100245#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Gavin Howard01055ba2018-11-03 11:00:21 -0600247typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100248#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600249 BC_INST_INC_PRE,
250 BC_INST_DEC_PRE,
251 BC_INST_INC_POST,
252 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600254
255 BC_INST_NEG,
256
257 BC_INST_POWER,
258 BC_INST_MULTIPLY,
259 BC_INST_DIVIDE,
260 BC_INST_MODULUS,
261 BC_INST_PLUS,
262 BC_INST_MINUS,
263
264 BC_INST_REL_EQ,
265 BC_INST_REL_LE,
266 BC_INST_REL_GE,
267 BC_INST_REL_NE,
268 BC_INST_REL_LT,
269 BC_INST_REL_GT,
270
271 BC_INST_BOOL_NOT,
272 BC_INST_BOOL_OR,
273 BC_INST_BOOL_AND,
274
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100275#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600276 BC_INST_ASSIGN_POWER,
277 BC_INST_ASSIGN_MULTIPLY,
278 BC_INST_ASSIGN_DIVIDE,
279 BC_INST_ASSIGN_MODULUS,
280 BC_INST_ASSIGN_PLUS,
281 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100282#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600283 BC_INST_ASSIGN,
284
285 BC_INST_NUM,
286 BC_INST_VAR,
287 BC_INST_ARRAY_ELEM,
288 BC_INST_ARRAY,
289
290 BC_INST_SCALE_FUNC,
291 BC_INST_IBASE,
292 BC_INST_SCALE,
293 BC_INST_LAST,
294 BC_INST_LENGTH,
295 BC_INST_READ,
296 BC_INST_OBASE,
297 BC_INST_SQRT,
298
299 BC_INST_PRINT,
300 BC_INST_PRINT_POP,
301 BC_INST_STR,
302 BC_INST_PRINT_STR,
303
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100304#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100305 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600306 BC_INST_JUMP,
307 BC_INST_JUMP_ZERO,
308
309 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600310 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100311#endif
Denys Vlasenko39287e02018-12-22 02:23:08 +0100312 BC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600313
314 BC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100315#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600316 BC_INST_POP_EXEC,
317
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;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100389 size_t inst_idx;
390 IF_BC(size_t results_len_before_call;)
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 Vlasenko39287e02018-12-22 02:23:08 +0100660#if ENABLE_BC
661# define BC_PARSE_REL (1 << 0)
662# define BC_PARSE_PRINT (1 << 1)
663# define BC_PARSE_ARRAY (1 << 2)
664# define BC_PARSE_NOCALL (1 << 3)
665#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600666
Gavin Howard01055ba2018-11-03 11:00:21 -0600667typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600668 BcLex l;
669
Denys Vlasenko503faf92018-12-20 16:24:18 +0100670 IF_BC(BcVec exits;)
671 IF_BC(BcVec conds;)
672 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600673
Gavin Howard01055ba2018-11-03 11:00:21 -0600674 BcFunc *func;
675 size_t fidx;
676
Denys Vlasenko503faf92018-12-20 16:24:18 +0100677 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600678} BcParse;
679
Gavin Howard01055ba2018-11-03 11:00:21 -0600680typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600681 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100682 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600683
Denys Vlasenko503faf92018-12-20 16:24:18 +0100684 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600685 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600686 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600687
Gavin Howard01055ba2018-11-03 11:00:21 -0600688 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100689 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600690
691 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100692 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600693
694 BcVec vars;
695 BcVec var_map;
696
697 BcVec arrs;
698 BcVec arr_map;
699
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100700 IF_DC(BcVec strs;)
701 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600702
703 const char *file;
704
Gavin Howard01055ba2018-11-03 11:00:21 -0600705 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100706 IF_BC(BcNum one;)
707 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600708} BcProgram;
709
Gavin Howard01055ba2018-11-03 11:00:21 -0600710#define BC_PROG_MAIN (0)
711#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100712#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600713#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100714#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600715
716#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
717#define BC_PROG_NUM(r, n) \
718 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
719
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100720#define BC_FLAG_W (1 << 0)
721#define BC_FLAG_V (1 << 1)
722#define BC_FLAG_S (1 << 2)
723#define BC_FLAG_Q (1 << 3)
724#define BC_FLAG_L (1 << 4)
725#define BC_FLAG_I (1 << 5)
726#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600727
728#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
729#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
730
Denys Vlasenko64074a12018-12-07 15:50:14 +0100731#define BC_MAX_OBASE ((unsigned) 999)
732#define BC_MAX_DIM ((unsigned) INT_MAX)
733#define BC_MAX_SCALE ((unsigned) UINT_MAX)
734#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
735#define BC_MAX_NUM BC_MAX_STRING
736// Unused apart from "limits" message. Just show a "biggish number" there.
737//#define BC_MAX_NAME BC_MAX_STRING
738//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
739//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
740#define BC_MAX_NAME_STR "999999999"
741#define BC_MAX_EXP_STR "999999999"
742#define BC_MAX_VARS_STR "999999999"
743
744#define BC_MAX_OBASE_STR "999"
745
746#if INT_MAX == 2147483647
747# define BC_MAX_DIM_STR "2147483647"
748#elif INT_MAX == 9223372036854775807
749# define BC_MAX_DIM_STR "9223372036854775807"
750#else
751# error Strange INT_MAX
752#endif
753
754#if UINT_MAX == 4294967295
755# define BC_MAX_SCALE_STR "4294967295"
756# define BC_MAX_STRING_STR "4294967294"
757#elif UINT_MAX == 18446744073709551615
758# define BC_MAX_SCALE_STR "18446744073709551615"
759# define BC_MAX_STRING_STR "18446744073709551614"
760#else
761# error Strange UINT_MAX
762#endif
763#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600764
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100765struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100766 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100767 IF_FEATURE_CLEAN_UP(smallint exiting;)
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}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001023#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001024static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001025{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001026 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001027}
Denys Vlasenko00646792018-12-05 18:12:27 +01001028static int bc_POSIX_does_not_allow(const char *msg)
1029{
1030 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1031}
1032static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1033{
1034 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1035}
1036static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1037{
1038 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1039}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001040#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001041
Gavin Howard01055ba2018-11-03 11:00:21 -06001042static void bc_vec_grow(BcVec *v, size_t n)
1043{
1044 size_t cap = v->cap * 2;
1045 while (cap < v->len + n) cap *= 2;
1046 v->v = xrealloc(v->v, v->size * cap);
1047 v->cap = cap;
1048}
1049
1050static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1051{
1052 v->size = esize;
1053 v->cap = BC_VEC_START_CAP;
1054 v->len = 0;
1055 v->dtor = dtor;
1056 v->v = xmalloc(esize * BC_VEC_START_CAP);
1057}
1058
Denys Vlasenko7d628012018-12-04 21:46:47 +01001059static void bc_char_vec_init(BcVec *v)
1060{
1061 bc_vec_init(v, sizeof(char), NULL);
1062}
1063
Gavin Howard01055ba2018-11-03 11:00:21 -06001064static void bc_vec_expand(BcVec *v, size_t req)
1065{
1066 if (v->cap < req) {
1067 v->v = xrealloc(v->v, v->size * req);
1068 v->cap = req;
1069 }
1070}
1071
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001072static void bc_vec_pop(BcVec *v)
1073{
1074 v->len--;
1075 if (v->dtor)
1076 v->dtor(v->v + (v->size * v->len));
1077}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001078
Gavin Howard01055ba2018-11-03 11:00:21 -06001079static void bc_vec_npop(BcVec *v, size_t n)
1080{
1081 if (!v->dtor)
1082 v->len -= n;
1083 else {
1084 size_t len = v->len - n;
1085 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1086 }
1087}
1088
Denys Vlasenko7d628012018-12-04 21:46:47 +01001089static void bc_vec_pop_all(BcVec *v)
1090{
1091 bc_vec_npop(v, v->len);
1092}
1093
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001094static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001095{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001096 size_t len = v->len;
1097 if (len >= v->cap) bc_vec_grow(v, 1);
1098 memmove(v->v + (v->size * len), data, v->size);
1099 v->len++;
1100 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001101}
1102
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001103// G.prog.results often needs "pop old operand, push result" idiom.
1104// Can do this without a few extra ops
1105static size_t bc_result_pop_and_push(const void *data)
1106{
1107 BcVec *v = &G.prog.results;
1108 char *last;
1109 size_t len = v->len - 1;
1110
1111 last = v->v + (v->size * len);
1112 if (v->dtor)
1113 v->dtor(last);
1114 memmove(last, data, v->size);
1115 return len;
1116}
1117
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001118static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001119{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001121}
1122
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001123static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001124{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001125 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001126 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001127 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001128}
1129
Gavin Howard01055ba2018-11-03 11:00:21 -06001130static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1131{
1132 if (idx == v->len)
1133 bc_vec_push(v, data);
1134 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001135 char *ptr;
1136
1137 if (v->len == v->cap) bc_vec_grow(v, 1);
1138
1139 ptr = v->v + v->size * idx;
1140
1141 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1142 memmove(ptr, data, v->size);
1143 }
1144}
1145
1146static void bc_vec_string(BcVec *v, size_t len, const char *str)
1147{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001148 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001149 bc_vec_expand(v, len + 1);
1150 memcpy(v->v, str, len);
1151 v->len = len;
1152
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001153 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001154}
1155
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001156#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001157static void bc_vec_concat(BcVec *v, const char *str)
1158{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001159 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001160
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001161 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001162
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001163 slen = strlen(str);
1164 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001165
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001166 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001167 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168
1169 v->len = len;
1170}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001171#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001172
1173static void *bc_vec_item(const BcVec *v, size_t idx)
1174{
1175 return v->v + v->size * idx;
1176}
1177
1178static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1179{
1180 return v->v + v->size * (v->len - idx - 1);
1181}
1182
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001183static void *bc_vec_top(const BcVec *v)
1184{
1185 return v->v + v->size * (v->len - 1);
1186}
1187
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001188static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001189{
1190 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001191 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001192 free(v->v);
1193}
1194
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001195static BcFunc* bc_program_func(size_t idx)
1196{
1197 return bc_vec_item(&G.prog.fns, idx);
1198}
1199// BC_PROG_MAIN is zeroth element, so:
1200#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1201
1202#if ENABLE_BC
1203static BcFunc* bc_program_current_func(void)
1204{
1205 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1206 BcFunc *func = bc_program_func(ip->func);
1207 return func;
1208}
1209#endif
1210
1211static char** bc_program_str(size_t idx)
1212{
1213#if ENABLE_BC
1214 if (IS_BC) {
1215 BcFunc *func = bc_program_current_func();
1216 return bc_vec_item(&func->strs, idx);
1217 }
1218#endif
1219 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1220}
1221
1222static char** bc_program_const(size_t idx)
1223{
1224#if ENABLE_BC
1225 if (IS_BC) {
1226 BcFunc *func = bc_program_current_func();
1227 return bc_vec_item(&func->consts, idx);
1228 }
1229#endif
1230 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1231}
1232
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001233static int bc_id_cmp(const void *e1, const void *e2)
1234{
1235 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1236}
1237
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001238static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001239{
1240 free(((BcId *) id)->name);
1241}
1242
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001243static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001244{
1245 size_t low = 0, high = v->len;
1246
1247 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001248 size_t mid = (low + high) / 2;
1249 BcId *id = bc_vec_item(v, mid);
1250 int result = bc_id_cmp(ptr, id);
1251
1252 if (result == 0)
1253 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001254 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001255 high = mid;
1256 else
1257 low = mid + 1;
1258 }
1259
1260 return low;
1261}
1262
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001263static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001264{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001265 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001266
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001267 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001268 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1270 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001271 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001272 bc_vec_pushAt(v, ptr, n);
1273 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001274}
1275
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001276#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001277static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001278{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001279 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001280 if (i >= v->len) return BC_VEC_INVALID_IDX;
1281 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1282}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001283#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001284
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001285static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001286{
1287 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1288 || c > 0x7e
1289 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001290 bc_error_fmt("illegal character 0x%02x", c);
1291 return 1;
1292 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001293 return 0;
1294}
1295
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001296// Note: it _appends_ data from fp to vec.
1297static void bc_read_line(BcVec *vec, FILE *fp)
Gavin Howard01055ba2018-11-03 11:00:21 -06001298{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001299 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001300 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001301
Gavin Howard01055ba2018-11-03 11:00:21 -06001302#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001303 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001304 intr:
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001305 if (fp != stdin) {
1306 // ^C while running a script (bc SCRIPT): die.
1307 // We do not return to interactive prompt:
1308 // user might be running us from a shell,
1309 // and SCRIPT might be intended to terminate
1310 // (e.g. contain a "halt" stmt).
1311 // ^C dropping user into a bc prompt instead of
1312 // the shell would be unexpected.
1313 xfunc_die();
1314 }
1315 // ^C while interactive input
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001316 G_interrupt = 0;
1317 // GNU bc says "interrupted execution."
1318 // GNU dc says "Interrupt!"
1319 fputs("\ninterrupted execution\n", stderr);
1320 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001321
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001322# if ENABLE_FEATURE_EDITING
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001323 if (G_ttyin && fp == stdin) {
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001324 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001325# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001326 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1327 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1328 if (n == 0) // ^C
1329 goto intr;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001330 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01001331 return;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001332 }
1333 i = 0;
1334 for (;;) {
1335 char c = line_buf[i++];
1336 if (!c) break;
1337 if (bad_input_byte(c)) goto again;
1338 }
1339 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001340# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001341 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001342# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001343#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001344 {
1345 int c;
1346 bool bad_chars = 0;
1347 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001348
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001349 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001350#if ENABLE_FEATURE_BC_SIGNALS
1351 if (G_interrupt) {
1352 // ^C was pressed: ignore entire line, get another one
1353 vec->len = len;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001354 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001355 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001356#endif
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001357 do c = fgetc(fp); while (c == '\0');
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001358 if (c == EOF) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001359 if (ferror(fp))
1360 bb_perror_msg_and_die("input error");
1361 // Note: EOF does not append '\n'
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001362 break;
1363 }
1364 bad_chars |= bad_input_byte(c);
1365 bc_vec_pushByte(vec, (char)c);
1366 } while (c != '\n');
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001367
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001368 if (bad_chars) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001369 // Bad chars on this line
1370 if (!G.prog.file) { // stdin
1371 // ignore entire line, get another one
1372 vec->len = len;
1373 goto again;
1374 }
1375 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
Denys Vlasenkoed849352018-12-06 10:26:13 +01001376 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001377 bc_vec_pushZeroByte(vec);
1378 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001379}
1380
Gavin Howard01055ba2018-11-03 11:00:21 -06001381static void bc_num_setToZero(BcNum *n, size_t scale)
1382{
1383 n->len = 0;
1384 n->neg = false;
1385 n->rdx = scale;
1386}
1387
1388static void bc_num_zero(BcNum *n)
1389{
1390 bc_num_setToZero(n, 0);
1391}
1392
1393static void bc_num_one(BcNum *n)
1394{
1395 bc_num_setToZero(n, 0);
1396 n->len = 1;
1397 n->num[0] = 1;
1398}
1399
Denys Vlasenko3129f702018-12-09 12:04:44 +01001400// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001401static void bc_num_init(BcNum *n, size_t req)
1402{
1403 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001404 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001405 n->num = xmalloc(req);
1406 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001407 n->rdx = 0;
1408 n->len = 0;
1409 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001410}
1411
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001412static void bc_num_init_DEF_SIZE(BcNum *n)
1413{
1414 bc_num_init(n, BC_NUM_DEF_SIZE);
1415}
1416
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001417static void bc_num_expand(BcNum *n, size_t req)
1418{
1419 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1420 if (req > n->cap) {
1421 n->num = xrealloc(n->num, req);
1422 n->cap = req;
1423 }
1424}
1425
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001426static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001427{
1428 free(((BcNum *) num)->num);
1429}
1430
1431static void bc_num_copy(BcNum *d, BcNum *s)
1432{
1433 if (d != s) {
1434 bc_num_expand(d, s->cap);
1435 d->len = s->len;
1436 d->neg = s->neg;
1437 d->rdx = s->rdx;
1438 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1439 }
1440}
1441
Denys Vlasenko29301232018-12-11 15:29:32 +01001442static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001443{
1444 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001445 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001446
Denys Vlasenko29301232018-12-11 15:29:32 +01001447 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001448
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001449 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001450 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001451
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001452 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001453 pow *= 10;
1454
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001455 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001456 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001457 prev = result;
1458 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001459 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001460 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001461
Denys Vlasenko29301232018-12-11 15:29:32 +01001462 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001463}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001464#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001465
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001466#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1467# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1468#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1469# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1470#endif
1471// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1472// would not hit realloc() code path - not good if num[] is not malloced
1473
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001474static void bc_num_ulong2num(BcNum *n, unsigned long val)
1475{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001476 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001477
1478 bc_num_zero(n);
1479
1480 if (val == 0) return;
1481
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001482 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001483
1484 ptr = n->num;
1485 for (;;) {
1486 n->len++;
1487 *ptr++ = val % 10;
1488 val /= 10;
1489 if (val == 0) break;
1490 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001491}
1492
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001493static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001494{
1495 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001496 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001497 a[i] -= b[i];
1498 for (j = i; a[j] < 0;) {
1499 a[j++] += 10;
1500 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001501 }
1502 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001503}
1504
1505static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1506{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001507 size_t i = len;
1508 for (;;) {
1509 int c;
1510 if (i == 0)
1511 return 0;
1512 i--;
1513 c = a[i] - b[i];
1514 if (c != 0) {
1515 i++;
1516 if (c < 0)
1517 return -i;
1518 return i;
1519 }
1520 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001521}
1522
1523static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1524{
1525 size_t i, min, a_int, b_int, diff;
1526 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001527 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001528 ssize_t cmp;
1529
1530 if (a == b) return 0;
1531 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1532 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001533
1534 if (a->neg != b->neg) // signs of a and b differ
1535 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1536 return (int)b->neg - (int)a->neg;
1537 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001538
1539 a_int = BC_NUM_INT(a);
1540 b_int = BC_NUM_INT(b);
1541 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001542
1543 if (a_int != 0) return (ssize_t) a_int;
1544
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001545 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001546 if (a_max) {
1547 min = b->rdx;
1548 diff = a->rdx - b->rdx;
1549 max_num = a->num + diff;
1550 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001551 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1552 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001553 min = a->rdx;
1554 diff = b->rdx - a->rdx;
1555 max_num = b->num + diff;
1556 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001557 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001558 }
1559
1560 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001561 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001562
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001563 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001564 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001565 }
1566
1567 return 0;
1568}
1569
1570static void bc_num_truncate(BcNum *n, size_t places)
1571{
1572 if (places == 0) return;
1573
1574 n->rdx -= places;
1575
1576 if (n->len != 0) {
1577 n->len -= places;
1578 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1579 }
1580}
1581
1582static void bc_num_extend(BcNum *n, size_t places)
1583{
1584 size_t len = n->len + places;
1585
1586 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001587 if (n->cap < len) bc_num_expand(n, len);
1588
1589 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1590 memset(n->num, 0, sizeof(BcDig) * places);
1591
1592 n->len += places;
1593 n->rdx += places;
1594 }
1595}
1596
1597static void bc_num_clean(BcNum *n)
1598{
1599 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1600 if (n->len == 0)
1601 n->neg = false;
1602 else if (n->len < n->rdx)
1603 n->len = n->rdx;
1604}
1605
1606static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1607{
1608 if (n->rdx < scale)
1609 bc_num_extend(n, scale - n->rdx);
1610 else
1611 bc_num_truncate(n, n->rdx - scale);
1612
1613 bc_num_clean(n);
1614 if (n->len != 0) n->neg = !neg1 != !neg2;
1615}
1616
1617static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1618 BcNum *restrict b)
1619{
1620 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001621 b->len = n->len - idx;
1622 a->len = idx;
1623 a->rdx = b->rdx = 0;
1624
1625 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1626 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001627 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001628 bc_num_zero(b);
1629 bc_num_copy(a, n);
1630 }
1631
1632 bc_num_clean(a);
1633 bc_num_clean(b);
1634}
1635
Denys Vlasenko29301232018-12-11 15:29:32 +01001636static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001637{
Denys Vlasenko29301232018-12-11 15:29:32 +01001638 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001639
1640 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1641 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1642 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001643 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001644 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001645
1646 if (n->rdx >= places)
1647 n->rdx -= places;
1648 else {
1649 bc_num_extend(n, places - n->rdx);
1650 n->rdx = 0;
1651 }
1652
1653 bc_num_clean(n);
1654
Denys Vlasenko29301232018-12-11 15:29:32 +01001655 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001656}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001657#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001658
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001659static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001660{
1661 BcNum one;
1662 BcDig num[2];
1663
1664 one.cap = 2;
1665 one.num = num;
1666 bc_num_one(&one);
1667
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001668 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001669}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001670#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001671
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001672static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001673{
1674 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1675 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001676 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001677
1678 // Because this function doesn't need to use scale (per the bc spec),
1679 // I am hijacking it to say whether it's doing an add or a subtract.
1680
1681 if (a->len == 0) {
1682 bc_num_copy(c, b);
1683 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001684 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001685 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001686 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001687 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001688 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001689 }
1690
1691 c->neg = a->neg;
1692 c->rdx = BC_MAX(a->rdx, b->rdx);
1693 min_rdx = BC_MIN(a->rdx, b->rdx);
1694 c->len = 0;
1695
1696 if (a->rdx > b->rdx) {
1697 diff = a->rdx - b->rdx;
1698 ptr = a->num;
1699 ptr_a = a->num + diff;
1700 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001701 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001702 diff = b->rdx - a->rdx;
1703 ptr = b->num;
1704 ptr_a = a->num;
1705 ptr_b = b->num + diff;
1706 }
1707
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001708 ptr_c = c->num;
1709 for (i = 0; i < diff; ++i, ++c->len)
1710 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001711
1712 ptr_c += diff;
1713 a_int = BC_NUM_INT(a);
1714 b_int = BC_NUM_INT(b);
1715
1716 if (a_int > b_int) {
1717 min_int = b_int;
1718 max = a_int;
1719 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001720 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001721 min_int = a_int;
1722 max = b_int;
1723 ptr = ptr_b;
1724 }
1725
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001726 carry = 0;
1727 for (i = 0; i < min_rdx + min_int; ++i) {
1728 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001729 carry = in / 10;
1730 ptr_c[i] = (BcDig)(in % 10);
1731 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001732 for (; i < max + min_rdx; ++i) {
1733 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001734 carry = in / 10;
1735 ptr_c[i] = (BcDig)(in % 10);
1736 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001737 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001738
1739 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1740
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001741 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001742}
1743
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001744static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001745{
Gavin Howard01055ba2018-11-03 11:00:21 -06001746 ssize_t cmp;
1747 BcNum *minuend, *subtrahend;
1748 size_t start;
1749 bool aneg, bneg, neg;
1750
1751 // Because this function doesn't need to use scale (per the bc spec),
1752 // I am hijacking it to say whether it's doing an add or a subtract.
1753
1754 if (a->len == 0) {
1755 bc_num_copy(c, b);
1756 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001757 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001758 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001759 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001760 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001761 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001762 }
1763
1764 aneg = a->neg;
1765 bneg = b->neg;
1766 a->neg = b->neg = false;
1767
1768 cmp = bc_num_cmp(a, b);
1769
1770 a->neg = aneg;
1771 b->neg = bneg;
1772
1773 if (cmp == 0) {
1774 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001775 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001776 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001777 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001778 neg = a->neg;
1779 minuend = a;
1780 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001781 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001782 neg = b->neg;
1783 if (sub) neg = !neg;
1784 minuend = b;
1785 subtrahend = a;
1786 }
1787
1788 bc_num_copy(c, minuend);
1789 c->neg = neg;
1790
1791 if (c->rdx < subtrahend->rdx) {
1792 bc_num_extend(c, subtrahend->rdx - c->rdx);
1793 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001794 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001795 start = c->rdx - subtrahend->rdx;
1796
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001797 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001798
1799 bc_num_clean(c);
1800
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001801 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001802}
1803
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001804static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001805 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001806#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001807{
1808 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001809 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001810 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001811 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001812
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 if (a->len == 0 || b->len == 0) {
1814 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001815 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001817 aone = BC_NUM_ONE(a);
1818 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001819 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001820 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001821 }
1822
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001823 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1824 || a->len < BC_NUM_KARATSUBA_LEN
1825 || b->len < BC_NUM_KARATSUBA_LEN
1826 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001827 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001828
Gavin Howard01055ba2018-11-03 11:00:21 -06001829 bc_num_expand(c, a->len + b->len + 1);
1830
1831 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001832 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001833
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001834 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001835 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001836 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001837 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001838 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001839 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001840 carry = in / 10;
1841 c->num[i + j] = (BcDig)(in % 10);
1842 }
1843
1844 c->num[i + j] += (BcDig) carry;
1845 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001846
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001847#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001848 // a=2^1000000
1849 // a*a <- without check below, this will not be interruptible
1850 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001851#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001852 }
1853
1854 c->len = len;
1855
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001856 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001857 }
1858
1859 bc_num_init(&l1, max);
1860 bc_num_init(&h1, max);
1861 bc_num_init(&l2, max);
1862 bc_num_init(&h2, max);
1863 bc_num_init(&m1, max);
1864 bc_num_init(&m2, max);
1865 bc_num_init(&z0, max);
1866 bc_num_init(&z1, max);
1867 bc_num_init(&z2, max);
1868 bc_num_init(&temp, max + max);
1869
1870 bc_num_split(a, max2, &l1, &h1);
1871 bc_num_split(b, max2, &l2, &h2);
1872
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001873 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001875 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 if (s) goto err;
1877
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001878 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001879 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001880 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001882 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001883 if (s) goto err;
1884
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001885 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001887 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001888 if (s) goto err;
1889
Denys Vlasenko29301232018-12-11 15:29:32 +01001890 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001892 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001894 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001897 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 bc_num_free(&temp);
1899 bc_num_free(&z2);
1900 bc_num_free(&z1);
1901 bc_num_free(&z0);
1902 bc_num_free(&m2);
1903 bc_num_free(&m1);
1904 bc_num_free(&h2);
1905 bc_num_free(&l2);
1906 bc_num_free(&h1);
1907 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001908 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909}
1910
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001911static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001912{
1913 BcStatus s;
1914 BcNum cpa, cpb;
1915 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1916
1917 scale = BC_MAX(scale, a->rdx);
1918 scale = BC_MAX(scale, b->rdx);
1919 scale = BC_MIN(a->rdx + b->rdx, scale);
1920 maxrdx = BC_MAX(maxrdx, scale);
1921
1922 bc_num_init(&cpa, a->len);
1923 bc_num_init(&cpb, b->len);
1924
1925 bc_num_copy(&cpa, a);
1926 bc_num_copy(&cpb, b);
1927 cpa.neg = cpb.neg = false;
1928
Denys Vlasenko29301232018-12-11 15:29:32 +01001929 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001930 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001931 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001932 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001933 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001934 if (s) goto err;
1935
1936 maxrdx += scale;
1937 bc_num_expand(c, c->len + maxrdx);
1938
1939 if (c->len < maxrdx) {
1940 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1941 c->len += maxrdx;
1942 }
1943
1944 c->rdx = maxrdx;
1945 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001946 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001947 bc_num_free(&cpb);
1948 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001949 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001950}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001951#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001952
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001953static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001954{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001955 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 size_t len, end, i;
1957 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001958
1959 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001960 RETURN_STATUS(bc_error("divide by zero"));
1961 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001962 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001963 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001964 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001965 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001966 bc_num_copy(c, a);
1967 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001968 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001969 }
1970
1971 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1972 bc_num_copy(&cp, a);
1973 len = b->len;
1974
1975 if (len > cp.len) {
1976 bc_num_expand(&cp, len + 2);
1977 bc_num_extend(&cp, len - cp.len);
1978 }
1979
1980 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1981 cp.rdx -= b->rdx;
1982 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1983
1984 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001985 for (;;) {
1986 if (len == 0) break;
1987 len--;
1988 if (b->num[len] != 0)
1989 break;
1990 }
1991 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001992 }
1993
1994 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1995
1996 // We want an extra zero in front to make things simpler.
1997 cp.num[cp.len++] = 0;
1998 end = cp.len - len;
1999
2000 bc_num_expand(c, cp.len);
2001
2002 bc_num_zero(c);
2003 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2004 c->rdx = cp.rdx;
2005 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002006
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002007 s = BC_STATUS_SUCCESS;
2008 for (i = end - 1; i < end; --i) {
2009 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002010 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002011 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2012 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002014#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002015 // a=2^100000
2016 // scale=40000
2017 // 1/a <- without check below, this will not be interruptible
2018 if (G_interrupt) {
2019 s = BC_STATUS_FAILURE;
2020 break;
2021 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002022#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002023 }
2024
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002025 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002026 bc_num_free(&cp);
2027
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002028 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002029}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002030#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002031
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002032static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002033 BcNum *restrict d, size_t scale, size_t ts)
2034{
2035 BcStatus s;
2036 BcNum temp;
2037 bool neg;
2038
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002039 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002040 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002041
2042 if (a->len == 0) {
2043 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002044 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002045 }
2046
2047 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002048 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002049 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002050
2051 if (scale != 0) scale = ts;
2052
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002053 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002054 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002055 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002056 if (s) goto err;
2057
2058 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2059
2060 neg = d->neg;
2061 bc_num_retireMul(d, ts, a->neg, b->neg);
2062 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002063 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002064 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002065 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002066}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002067#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002068
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002069static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002070{
2071 BcStatus s;
2072 BcNum c1;
2073 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2074
2075 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002076 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002077 bc_num_free(&c1);
2078
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002079 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002080}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002081#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002082
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002083static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002084{
2085 BcStatus s = BC_STATUS_SUCCESS;
2086 BcNum copy;
2087 unsigned long pow;
2088 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002089 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002090
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002091 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002092
2093 if (b->len == 0) {
2094 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002095 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002096 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002098 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002099 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002100 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002102 if (!b->neg)
2103 bc_num_copy(c, a);
2104 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002105 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002106 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002107 }
2108
2109 neg = b->neg;
2110 b->neg = false;
2111
Denys Vlasenko29301232018-12-11 15:29:32 +01002112 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002113 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002114
2115 bc_num_init(&copy, a->len);
2116 bc_num_copy(&copy, a);
2117
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002118 if (!neg) {
2119 if (a->rdx > scale)
2120 scale = a->rdx;
2121 if (a->rdx * pow < scale)
2122 scale = a->rdx * pow;
2123 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002124
2125 b->neg = neg;
2126
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002127 for (powrdx = a->rdx; !(pow & 1); 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;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002131 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002132 //if (G_interrupt) {
2133 // s = BC_STATUS_FAILURE;
2134 // goto err;
2135 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002136 }
2137
Gavin Howard01055ba2018-11-03 11:00:21 -06002138 bc_num_copy(c, &copy);
2139
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002140 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002142 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002143 if (s) goto err;
2144
2145 if (pow & 1) {
2146 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002147 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002148 if (s) goto err;
2149 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002150 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002151 //if (G_interrupt) {
2152 // s = BC_STATUS_FAILURE;
2153 // goto err;
2154 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002155 }
2156
2157 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002158 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 if (s) goto err;
2160 }
2161
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2163
2164 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002165 for (i = 0; i < c->len; ++i)
2166 if (c->num[i] != 0)
2167 goto skip;
2168 bc_num_setToZero(c, scale);
2169 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002170
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002171 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002172 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002173 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002174}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002175#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002176
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002177static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002178 BcNumBinaryOp op, size_t req)
2179{
2180 BcStatus s;
2181 BcNum num2, *ptr_a, *ptr_b;
2182 bool init = false;
2183
2184 if (c == a) {
2185 ptr_a = &num2;
2186 memcpy(ptr_a, c, sizeof(BcNum));
2187 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002188 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002189 ptr_a = a;
2190
2191 if (c == b) {
2192 ptr_b = &num2;
2193 if (c != a) {
2194 memcpy(ptr_b, c, sizeof(BcNum));
2195 init = true;
2196 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002197 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002198 ptr_b = b;
2199
2200 if (init)
2201 bc_num_init(c, req);
2202 else
2203 bc_num_expand(c, req);
2204
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002205 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01002206 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002207
2208 if (init) bc_num_free(&num2);
2209
Denys Vlasenko29301232018-12-11 15:29:32 +01002210 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002211}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002212#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002213
Denys Vlasenko9311e012018-12-10 11:54:18 +01002214static bool bc_num_strValid(const char *val, size_t base)
2215{
2216 BcDig b;
2217 bool radix;
2218
2219 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2220 radix = false;
2221 for (;;) {
2222 BcDig c = *val++;
2223 if (c == '\0')
2224 break;
2225 if (c == '.') {
2226 if (radix) return false;
2227 radix = true;
2228 continue;
2229 }
2230 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2231 return false;
2232 }
2233 return true;
2234}
2235
2236// Note: n is already "bc_num_zero()"ed,
2237// leading zeroes in "val" are removed
2238static void bc_num_parseDecimal(BcNum *n, const char *val)
2239{
2240 size_t len, i;
2241 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002242
2243 len = strlen(val);
2244 if (len == 0)
2245 return;
2246
Denys Vlasenko9311e012018-12-10 11:54:18 +01002247 bc_num_expand(n, len);
2248
2249 ptr = strchr(val, '.');
2250
2251 n->rdx = 0;
2252 if (ptr != NULL)
2253 n->rdx = (size_t)((val + len) - (ptr + 1));
2254
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002255 for (i = 0; val[i]; ++i) {
2256 if (val[i] != '0' && val[i] != '.') {
2257 // Not entirely zero value - convert it, and exit
2258 i = len - 1;
2259 for (;;) {
2260 n->num[n->len] = val[i] - '0';
2261 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002262 skip_dot:
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002263 if (i == 0) break;
2264 if (val[--i] == '.') goto skip_dot;
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002265 }
2266 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002267 }
2268 }
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002269 // if for() exits without hitting if(), the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002270}
2271
2272// Note: n is already "bc_num_zero()"ed,
2273// leading zeroes in "val" are removed
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002274static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko9311e012018-12-10 11:54:18 +01002275{
2276 BcStatus s;
2277 BcNum temp, mult, result;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002278 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01002279 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko9311e012018-12-10 11:54:18 +01002280 BcDig c = '\0';
2281 unsigned long v;
2282 size_t i, digits;
2283
2284 for (i = 0; ; ++i) {
2285 if (val[i] == '\0')
2286 return;
2287 if (val[i] != '.' && val[i] != '0')
2288 break;
2289 }
2290
2291 bc_num_init_DEF_SIZE(&temp);
2292 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01002293 base.cap = ARRAY_SIZE(base_digs);
2294 base.num = base_digs;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002295 bc_num_ulong2num(&base, base_t);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002296
2297 for (;;) {
2298 c = *val++;
2299 if (c == '\0') goto int_err;
2300 if (c == '.') break;
2301
2302 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2303
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002304 s = zbc_num_mul(n, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002305 if (s) goto int_err;
2306 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002307 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002308 if (s) goto int_err;
2309 }
2310
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002311 bc_num_init(&result, base.len);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002312 //bc_num_zero(&result); - already is
2313 bc_num_one(&mult);
2314
2315 digits = 0;
2316 for (;;) {
2317 c = *val++;
2318 if (c == '\0') break;
2319 digits++;
2320
2321 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2322
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002323 s = zbc_num_mul(&result, &base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002324 if (s) goto err;
2325 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002326 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002327 if (s) goto err;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002328 s = zbc_num_mul(&mult, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002329 if (s) goto err;
2330 }
2331
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002332 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002333 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002334 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002335 if (s) goto err;
2336
2337 if (n->len != 0) {
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002338 if (n->rdx < digits)
2339 bc_num_extend(n, digits - n->rdx);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002340 } else
2341 bc_num_zero(n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002342 err:
Denys Vlasenko9311e012018-12-10 11:54:18 +01002343 bc_num_free(&result);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002344 int_err:
Denys Vlasenko9311e012018-12-10 11:54:18 +01002345 bc_num_free(&mult);
2346 bc_num_free(&temp);
2347}
2348
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002349static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
Gavin Howard01055ba2018-11-03 11:00:21 -06002350{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002351 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002352 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002353
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002354 bc_num_zero(n);
2355 while (*val == '0') val++;
2356
Gavin Howard01055ba2018-11-03 11:00:21 -06002357 if (base_t == 10)
2358 bc_num_parseDecimal(n, val);
2359 else
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002360 bc_num_parseBase(n, val, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002361
Denys Vlasenko29301232018-12-11 15:29:32 +01002362 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002363}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002364#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002365
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002366static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002367{
2368 BcStatus s;
2369 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2370 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2371 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2372
2373 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2374 bc_num_expand(b, req);
2375
2376 if (a->len == 0) {
2377 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002378 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002379 }
2380 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002381 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002382 }
2383 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002384 bc_num_one(b);
2385 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002386 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002387 }
2388
2389 scale = BC_MAX(scale, a->rdx) + 1;
2390 len = a->len + scale;
2391
2392 bc_num_init(&num1, len);
2393 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002394 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002395
2396 bc_num_one(&half);
2397 half.num[0] = 5;
2398 half.rdx = 1;
2399
2400 bc_num_init(&f, len);
2401 bc_num_init(&fprime, len);
2402
2403 x0 = &num1;
2404 x1 = &num2;
2405
2406 bc_num_one(x0);
2407 pow = BC_NUM_INT(a);
2408
2409 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002410 if (pow & 1)
2411 x0->num[0] = 2;
2412 else
2413 x0->num[0] = 6;
2414
2415 pow -= 2 - (pow & 1);
2416
2417 bc_num_extend(x0, pow);
2418
2419 // Make sure to move the radix back.
2420 x0->rdx -= pow;
2421 }
2422
2423 x0->rdx = digs = digs1 = 0;
2424 resrdx = scale + 2;
2425 len = BC_NUM_INT(x0) + resrdx - 1;
2426
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002427 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002428 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002429 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002430 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002431 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002432 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002433 if (s) goto err;
2434
2435 cmp = bc_num_cmp(x1, x0);
2436 digs = x1->len - (unsigned long long) llabs(cmp);
2437
2438 if (cmp == cmp2 && digs == digs1)
2439 times += 1;
2440 else
2441 times = 0;
2442
2443 resrdx += times > 4;
2444
2445 cmp2 = cmp1;
2446 cmp1 = cmp;
2447 digs1 = digs;
2448
2449 temp = x0;
2450 x0 = x1;
2451 x1 = temp;
2452 }
2453
Gavin Howard01055ba2018-11-03 11:00:21 -06002454 bc_num_copy(b, x0);
2455 scale -= 1;
2456 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002457 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002458 bc_num_free(&fprime);
2459 bc_num_free(&f);
2460 bc_num_free(&half);
2461 bc_num_free(&num2);
2462 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002463 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002464}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002465#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002466
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002467static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 size_t scale)
2469{
2470 BcStatus s;
2471 BcNum num2, *ptr_a;
2472 bool init = false;
2473 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2474
2475 if (c == a) {
2476 memcpy(&num2, c, sizeof(BcNum));
2477 ptr_a = &num2;
2478 bc_num_init(c, len);
2479 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002480 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002481 ptr_a = a;
2482 bc_num_expand(c, len);
2483 }
2484
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002485 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002486
2487 if (init) bc_num_free(&num2);
2488
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002489 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002490}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002491#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002492
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002493#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002494static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002495{
2496 BcStatus s;
2497 BcNum base, exp, two, temp;
2498
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002499 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002500 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002501 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002502 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002503 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002504 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002505
2506 bc_num_expand(d, c->len);
2507 bc_num_init(&base, c->len);
2508 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002509 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002510 bc_num_init(&temp, b->len);
2511
2512 bc_num_one(&two);
2513 two.num[0] = 2;
2514 bc_num_one(d);
2515
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002516 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002517 if (s) goto err;
2518 bc_num_copy(&exp, b);
2519
2520 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002521 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002522 if (s) goto err;
2523
2524 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002525 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002526 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002527 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002528 if (s) goto err;
2529 }
2530
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002531 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002532 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002533 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002534 if (s) goto err;
2535 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002536 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002537 bc_num_free(&temp);
2538 bc_num_free(&two);
2539 bc_num_free(&exp);
2540 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002541 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002542}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002543#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002544#endif // ENABLE_DC
2545
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002546#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002547static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002548{
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002549 BcId *autoid;
Gavin Howard01055ba2018-11-03 11:00:21 -06002550 BcId a;
2551 size_t i;
2552
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002553 autoid = (void*)f->autos.v;
2554 for (i = 0; i < f->autos.len; i++, autoid++) {
2555 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002556 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002557 }
2558
2559 a.idx = var;
2560 a.name = name;
2561
2562 bc_vec_push(&f->autos, &a);
2563
Denys Vlasenko29301232018-12-11 15:29:32 +01002564 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002565}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002566#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002567#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002568
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002569static FAST_FUNC void bc_string_free(void *string)
2570{
2571 free(*(char**)string);
2572}
2573
Gavin Howard01055ba2018-11-03 11:00:21 -06002574static void bc_func_init(BcFunc *f)
2575{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002576 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002577 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2578 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002579 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2580 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002581 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002582}
2583
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002584static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002585{
2586 BcFunc *f = (BcFunc *) func;
2587 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002588 IF_BC(bc_vec_free(&f->labels);)
2589 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002590 IF_BC(bc_vec_free(&f->strs);)
2591 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002592}
2593
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002594static void bc_array_expand(BcVec *a, size_t len);
2595
Gavin Howard01055ba2018-11-03 11:00:21 -06002596static void bc_array_init(BcVec *a, bool nums)
2597{
2598 if (nums)
2599 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2600 else
2601 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2602 bc_array_expand(a, 1);
2603}
2604
Gavin Howard01055ba2018-11-03 11:00:21 -06002605static void bc_array_expand(BcVec *a, size_t len)
2606{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002607 if (a->dtor == bc_num_free
2608 // && a->size == sizeof(BcNum) - always true
2609 ) {
2610 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002611 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002612 bc_num_init_DEF_SIZE(&n);
2613 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002614 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002615 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002616 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002617 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002618 bc_array_init(&v, true);
2619 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002620 }
2621 }
2622}
2623
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002624static void bc_array_copy(BcVec *d, const BcVec *s)
2625{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002626 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002627 size_t i;
2628
2629 bc_vec_pop_all(d);
2630 bc_vec_expand(d, s->cap);
2631 d->len = s->len;
2632
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002633 dnum = (void*)d->v;
2634 snum = (void*)s->v;
2635 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002636 bc_num_init(dnum, snum->len);
2637 bc_num_copy(dnum, snum);
2638 }
2639}
2640
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002641#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002642static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002643{
2644 d->t = src->t;
2645
2646 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002647 case BC_RESULT_TEMP:
2648 case BC_RESULT_IBASE:
2649 case BC_RESULT_SCALE:
2650 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002651 bc_num_init(&d->d.n, src->d.n.len);
2652 bc_num_copy(&d->d.n, &src->d.n);
2653 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002654 case BC_RESULT_VAR:
2655 case BC_RESULT_ARRAY:
2656 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002657 d->d.id.name = xstrdup(src->d.id.name);
2658 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002659 case BC_RESULT_CONSTANT:
2660 case BC_RESULT_LAST:
2661 case BC_RESULT_ONE:
2662 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002663 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2664 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002665 }
2666}
2667#endif // ENABLE_DC
2668
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002669static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002670{
2671 BcResult *r = (BcResult *) result;
2672
2673 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002674 case BC_RESULT_TEMP:
2675 case BC_RESULT_IBASE:
2676 case BC_RESULT_SCALE:
2677 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002678 bc_num_free(&r->d.n);
2679 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002680 case BC_RESULT_VAR:
2681 case BC_RESULT_ARRAY:
2682 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002683 free(r->d.id.name);
2684 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002685 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002686 // Do nothing.
2687 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002688 }
2689}
2690
2691static void bc_lex_lineComment(BcLex *l)
2692{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002693 // Try: echo -n '#foo' | bc
2694 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002695 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002696 i = l->i;
2697 while (i < l->len && l->buf[i] != '\n')
2698 i++;
2699 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002700}
2701
2702static void bc_lex_whitespace(BcLex *l)
2703{
Gavin Howard01055ba2018-11-03 11:00:21 -06002704 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002705 for (;;) {
2706 char c = l->buf[l->i];
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002707 if (c == '\n') // this is BC_LEX_NLINE, not BC_LEX_WHITESPACE
2708 break;
2709 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002710 break;
2711 l->i++;
2712 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002713}
2714
Denys Vlasenko29301232018-12-11 15:29:32 +01002715static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002716{
2717 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002718 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002719 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002720
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002721 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002722 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002723 ccnt = i = 0;
2724 for (;;) {
2725 char c = buf[i];
2726 if (c == '\0')
2727 break;
2728 if (c == '\\' && buf[i + 1] == '\n') {
2729 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002730 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002731 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002732 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002733 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2734 if (c != '.') break;
2735 // if '.' was already seen, stop on second one:
2736 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002737 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002738 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002739 // buf[i] is one of "0-9A-F."
2740 i++;
2741 if (c != '.')
2742 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002743 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002744 //ccnt is the number of chars in the number string, excluding possible
2745 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2746 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002747 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002748
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002749 // This might overestimate the size, if there are "\<NL>"'s
2750 // in the number. Subtracting number_of_backslashes*2 correctly
2751 // is not that easy: consider that in the case of "NNN.\<NL>"
2752 // loop above will count "\<NL>" before it realizes it is not
2753 // in fact *inside* the number:
2754 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002755
Denys Vlasenko64074a12018-12-07 15:50:14 +01002756 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2757 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2758 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002759 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002760 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002761
Denys Vlasenko7d628012018-12-04 21:46:47 +01002762 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002763 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002764 bc_vec_push(&l->t.v, &start);
2765
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002766 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002767 // If we have hit a backslash, skip it. We don't have
2768 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002769 if (*buf == '\\') {
2770 buf += 2;
2771 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002772 continue;
2773 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002774 bc_vec_push(&l->t.v, buf);
2775 buf++;
2776 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002777 }
2778
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002779 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002780
Denys Vlasenko29301232018-12-11 15:29:32 +01002781 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002782}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002783#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002784
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002785static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002786{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002787 size_t i;
2788 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002789
2790 l->t.t = BC_LEX_NAME;
2791
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002792 i = 0;
2793 buf = l->buf + l->i - 1;
2794 for (;;) {
2795 char c = buf[i];
2796 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2797 i++;
2798 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002799
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002800#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002801 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2802 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2803 if (i > BC_MAX_STRING)
2804 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2805 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002806#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002807 bc_vec_string(&l->t.v, i, buf);
2808
2809 // Increment the index. We minus 1 because it has already been incremented.
2810 l->i += i - 1;
2811
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002812 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002813}
2814
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002815static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002816{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002817 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002818}
2819
2820static void bc_lex_free(BcLex *l)
2821{
2822 bc_vec_free(&l->t.v);
2823}
2824
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002825static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002826{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002827 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002828 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002829}
2830
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002831static bool bc_lex_more_input(BcLex *l)
2832{
2833 size_t str;
2834 bool comment;
2835
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002836 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002837
2838 // This loop is complex because the vm tries not to send any lines that end
2839 // with a backslash to the parser. The reason for that is because the parser
2840 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2841 // case, and for strings and comments, the parser will expect more stuff.
2842 comment = false;
2843 str = 0;
2844 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002845 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002846 char *string;
2847
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002848 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002849 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002850 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002851 break;
2852
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002853 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002854 while (*string) {
2855 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002856 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002857 if (IS_BC)
2858 str ^= (c == '"');
2859 else {
2860 if (c == ']')
2861 str -= 1;
2862 else if (c == '[')
2863 str += 1;
2864 }
2865 }
2866 string++;
2867 if (c == '/' && *string == '*') {
2868 comment = true;
2869 string++;
2870 continue;
2871 }
2872 if (c == '*' && *string == '/') {
2873 comment = false;
2874 string++;
2875 }
2876 }
2877 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002878 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002879 continue;
2880 }
2881
2882 // Check for backslash+newline.
2883 // we do not check that last char is '\n' -
2884 // if it is not, then it's EOF, and looping back
2885 // to bc_read_line() will detect it:
2886 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002887 if (string >= G.input_buffer.v && *string == '\\') {
2888 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002889 continue;
2890 }
2891
2892 break;
2893 }
2894
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002895 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002896 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002897// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2898 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002899
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002900 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002901}
2902
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002903IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2904IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
2905
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002906static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002907{
2908 BcStatus s;
2909
2910 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002911 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002912
2913 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002914 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002915 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002916
2917 // Loop until failure or we don't have whitespace. This
2918 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002919 // Comments are also BC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002920 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002921 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002922 if (l->i == l->len) {
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002923 l->t.t = BC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002924 if (!G.input_fp)
2925 RETURN_STATUS(BC_STATUS_SUCCESS);
2926 if (!bc_lex_more_input(l)) {
2927 G.input_fp = NULL;
2928 RETURN_STATUS(BC_STATUS_SUCCESS);
2929 }
2930 // here it's guaranteed that l->i is below l->len
2931 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002932 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002933 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2934 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002935 if (IS_BC) {
2936 IF_BC(s = zbc_lex_token(l));
2937 } else {
2938 IF_DC(s = zdc_lex_token(l));
2939 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002940 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002941 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002942
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002943 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002944}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002945#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002946
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002947#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01002948static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
2949{
2950 if (l->t.t == BC_LEX_NLINE)
2951 RETURN_STATUS(zbc_lex_next(l));
2952 RETURN_STATUS(BC_STATUS_SUCCESS);
2953}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002954#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002955
2956static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
2957{
2958 BcStatus s;
2959 s = zbc_lex_next(l);
2960 if (s) RETURN_STATUS(s);
2961 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
2962 s = zbc_lex_skip_if_at_NLINE(l);
2963 RETURN_STATUS(s);
2964}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002965#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002966#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01002967
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002968static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002969{
2970 l->buf = text;
2971 l->i = 0;
2972 l->len = strlen(text);
2973 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002974 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002975}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002976#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002977
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002978#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002979static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002980{
2981 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002982 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002983 const char *buf = l->buf + l->i - 1;
2984
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002985 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2986 const char *keyword8 = bc_lex_kws[i].name8;
2987 unsigned j = 0;
2988 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2989 j++;
2990 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002991 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002992 if (keyword8[j] != '\0')
2993 continue;
2994 match:
2995 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002996 if (isalnum(buf[j]) || buf[j]=='_')
2997 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002998 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002999 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003000 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003001 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003002 }
3003
3004 // We minus 1 because the index has already been incremented.
3005 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003006 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003007 }
3008
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003009 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003010 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003011
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003012 if (l->t.v.len > 2) {
3013 // Prevent this:
3014 // >>> qwe=1
3015 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3016 // '
3017 unsigned len = strchrnul(buf, '\n') - buf;
3018 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3019 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003020
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003021 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003022}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003023#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003024
Denys Vlasenko26819db2018-12-12 16:08:46 +01003025static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003026{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003027 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003028
3029 l->t.t = BC_LEX_STR;
3030
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003031 nls = 0;
3032 i = l->i;
3033 for (;;) {
3034 char c = l->buf[i];
3035 if (c == '\0') {
3036 l->i = i;
3037 RETURN_STATUS(bc_error("string end could not be found"));
3038 }
3039 if (c == '"')
3040 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003041 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003042 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003043 }
3044
3045 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003046 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3047 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3048 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003049 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003050 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003051 bc_vec_string(&l->t.v, len, l->buf + l->i);
3052
3053 l->i = i + 1;
3054 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003055 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003056
Denys Vlasenko26819db2018-12-12 16:08:46 +01003057 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003058}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003059#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003060
Denys Vlasenko0a238142018-12-14 16:48:34 +01003061static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003062{
3063 if (l->buf[l->i] == '=') {
3064 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003065 with_and_without >>= 8; // store "with" value
3066 } // else store "without" value
3067 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003068}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003069#define bc_lex_assign(l, with, without) \
3070 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003071
Denys Vlasenko29301232018-12-11 15:29:32 +01003072static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003073{
3074 size_t i, nls = 0;
3075 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003076
3077 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003078 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003079 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003080 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003081 check_star:
3082 if (c == '*') {
3083 c = buf[++i];
3084 if (c == '/')
3085 break;
3086 goto check_star;
3087 }
3088 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003089 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003090 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003091 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003092 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003093 }
3094
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003095 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003096 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003097 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003098
Denys Vlasenko29301232018-12-11 15:29:32 +01003099 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003100}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003101#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003102
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003103static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003104{
3105 BcStatus s = BC_STATUS_SUCCESS;
3106 char c = l->buf[l->i++], c2;
3107
3108 // This is the workhorse of the lexer.
3109 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003110// case '\0': // probably never reached
3111// l->i--;
3112// l->t.t = BC_LEX_EOF;
3113// l->newline = true;
3114// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003115 case '\n':
3116 l->t.t = BC_LEX_NLINE;
3117 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003119 case '\t':
3120 case '\v':
3121 case '\f':
3122 case '\r':
3123 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 bc_lex_whitespace(l);
3125 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003126 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003129 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
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 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003134 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003135 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003137 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003138 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3143 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 c2 = l->buf[l->i];
3146 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003147 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003148 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003149 ++l->i;
3150 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003151 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003153 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003154 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 case '(':
3157 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003158 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3159 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003160 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003161 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3162 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 c2 = l->buf[l->i];
3165 if (c2 == '+') {
3166 ++l->i;
3167 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003168 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3170 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003171 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 l->t.t = BC_LEX_COMMA;
3173 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 c2 = l->buf[l->i];
3176 if (c2 == '-') {
3177 ++l->i;
3178 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003179 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3181 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003183 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003184 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003185 else {
3186 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003187 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003188 }
3189 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003190 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003191 c2 = l->buf[l->i];
3192 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003193 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003194 else
3195 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3196 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 case '0':
3198 case '1':
3199 case '2':
3200 case '3':
3201 case '4':
3202 case '5':
3203 case '6':
3204 case '7':
3205 case '8':
3206 case '9':
3207 case 'A':
3208 case 'B':
3209 case 'C':
3210 case 'D':
3211 case 'E':
3212 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003213 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003215 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 l->t.t = BC_LEX_SCOLON;
3217 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3220 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3223 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003225 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3226 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 case '[':
3228 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3230 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 if (l->buf[l->i] == '\n') {
3233 l->t.t = BC_LEX_WHITESPACE;
3234 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003235 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003236 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003237 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003238 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003239 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3240 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003241 case 'a':
3242 case 'b':
3243 case 'c':
3244 case 'd':
3245 case 'e':
3246 case 'f':
3247 case 'g':
3248 case 'h':
3249 case 'i':
3250 case 'j':
3251 case 'k':
3252 case 'l':
3253 case 'm':
3254 case 'n':
3255 case 'o':
3256 case 'p':
3257 case 'q':
3258 case 'r':
3259 case 's':
3260 case 't':
3261 case 'u':
3262 case 'v':
3263 case 'w':
3264 case 'x':
3265 case 'y':
3266 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003267 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003268 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003269 case '{':
3270 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3272 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003275 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003276 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003277 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 ++l->i;
3279 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003280 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003282 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003283 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003287 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 }
3290
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003291 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003292}
3293#endif // ENABLE_BC
3294
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003295#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003296static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003297{
Gavin Howard01055ba2018-11-03 11:00:21 -06003298 if (isspace(l->buf[l->i - 1])) {
3299 bc_lex_whitespace(l);
3300 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003301 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003302 RETURN_STATUS(bc_error("extended register"));
3303 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003304 }
3305 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003306 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003307 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003308 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003309 l->t.t = BC_LEX_NAME;
3310 }
3311
Denys Vlasenko29301232018-12-11 15:29:32 +01003312 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003313}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003314#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003315
Denys Vlasenko29301232018-12-11 15:29:32 +01003316static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003317{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003318 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003319
3320 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003321 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003322
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003323 nls = 0;
3324 depth = 1;
3325 i = l->i;
3326 for (;;) {
3327 char c = l->buf[i];
3328 if (c == '\0') {
3329 l->i = i;
3330 RETURN_STATUS(bc_error("string end could not be found"));
3331 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003332 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003333 if (i == l->i || l->buf[i - 1] != '\\') {
3334 if (c == '[') depth++;
3335 if (c == ']')
3336 if (--depth == 0)
3337 break;
3338 }
3339 bc_vec_push(&l->t.v, &l->buf[i]);
3340 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003341 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003342 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003343
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003344 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003345 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3346 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3347 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003348 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003349 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003350
3351 l->i = i;
3352 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003353 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003354
Denys Vlasenko29301232018-12-11 15:29:32 +01003355 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003357#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003358
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003359static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003360{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003361 static const //BcLexType - should be this type, but narrower type saves size:
3362 uint8_t
3363 dc_lex_regs[] = {
3364 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
3365 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
3366 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
3367 BC_LEX_STORE_PUSH,
3368 };
3369 static const //BcLexType - should be this type
3370 uint8_t
3371 dc_lex_tokens[] = {
3372 /* %&'( */
3373 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
3374 /* )*+, */
3375 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
3376 /* -./ */
3377 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
3378 /* 0123456789 */
3379 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3380 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3381 BC_LEX_INVALID, BC_LEX_INVALID,
3382 /* :;<=>?@ */
3383 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
3384 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
3385 /* ABCDEFGH */
3386 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3387 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
3388 /* IJKLMNOP */
3389 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
3390 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
3391 /* QRSTUVWXY */
3392 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
3393 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
3394 /* Z[\] */
3395 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3396 /* ^_` */
3397 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
3398 /* abcdefgh */
3399 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
3400 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
3401 /* ijklmnop */
3402 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
3403 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
3404 /* qrstuvwx */
3405 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
3406 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
3407 /* yz */
3408 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
3409 /* {|}~ */
3410 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
3411 };
3412
Gavin Howard01055ba2018-11-03 11:00:21 -06003413 BcStatus s = BC_STATUS_SUCCESS;
3414 char c = l->buf[l->i++], c2;
3415 size_t i;
3416
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003417 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3418 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003419 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003420 }
3421
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003422 if (c >= '%' && c <= '~'
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003423 && (l->t.t = dc_lex_tokens[c - '%']) != BC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003424 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003425 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003426 }
3427
3428 // This is the workhorse of the lexer.
3429 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003430// case '\0': // probably never reached
3431// l->t.t = BC_LEX_EOF;
3432// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003433 case '\n':
3434 case '\t':
3435 case '\v':
3436 case '\f':
3437 case '\r':
3438 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003439 l->newline = (c == '\n');
3440 bc_lex_whitespace(l);
3441 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003442 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003443 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003444 if (c2 == '=')
3445 l->t.t = BC_LEX_OP_REL_NE;
3446 else if (c2 == '<')
3447 l->t.t = BC_LEX_OP_REL_LE;
3448 else if (c2 == '>')
3449 l->t.t = BC_LEX_OP_REL_GE;
3450 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003451 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003452 ++l->i;
3453 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003454 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003455 bc_lex_lineComment(l);
3456 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003457 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003458 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003459 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003460 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003461 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003462 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003463 case '0':
3464 case '1':
3465 case '2':
3466 case '3':
3467 case '4':
3468 case '5':
3469 case '6':
3470 case '7':
3471 case '8':
3472 case '9':
3473 case 'A':
3474 case 'B':
3475 case 'C':
3476 case 'D':
3477 case 'E':
3478 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003479 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003481 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003482 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003483 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003484 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003485 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003486 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003488 }
3489
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003490 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003491}
3492#endif // ENABLE_DC
3493
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003494static void bc_parse_push(BcParse *p, char i)
3495{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003496 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003497 bc_vec_pushByte(&p->func->code, i);
3498}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003499
Gavin Howard01055ba2018-11-03 11:00:21 -06003500static void bc_parse_pushName(BcParse *p, char *name)
3501{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003502 while (*name)
3503 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003504 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003505}
3506
3507static void bc_parse_pushIndex(BcParse *p, size_t idx)
3508{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003509 size_t mask;
3510 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003511
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003512 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003513 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3514 amt = sizeof(idx);
3515 do {
3516 if (idx & mask) break;
3517 mask >>= 8;
3518 amt--;
3519 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003520
3521 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003522
3523 while (idx != 0) {
3524 bc_parse_push(p, (unsigned char)idx);
3525 idx >>= 8;
3526 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003527}
3528
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003529#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003530static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3531{
3532 bc_parse_push(p, BC_INST_JUMP);
3533 bc_parse_pushIndex(p, idx);
3534}
3535
3536static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3537{
3538 bc_parse_push(p, BC_INST_JUMP_ZERO);
3539 bc_parse_pushIndex(p, idx);
3540}
3541
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003542static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003543{
3544 char *str = xstrdup(p->l.t.v.v);
3545
3546 bc_parse_push(p, BC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003547 bc_parse_pushIndex(p, p->func->strs.len);
3548 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003549
3550 RETURN_STATUS(zbc_lex_next(&p->l));
3551}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003552#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003553#endif
3554
3555static void bc_parse_pushNUM(BcParse *p)
3556{
3557 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003558#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003559 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003560#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003561 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003562#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003563 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003564#endif
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003565 bc_parse_push(p, BC_INST_NUM);
3566 bc_parse_pushIndex(p, idx);
3567}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003568
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003569static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003570{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003571 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003572
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003573 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003574}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003575#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003576
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003577// Called when parsing or execution detects a failure,
3578// resets execution structures.
3579static void bc_program_reset(void)
3580{
3581 BcFunc *f;
3582 BcInstPtr *ip;
3583
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003584 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003585 bc_vec_pop_all(&G.prog.results);
3586
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003587 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003588 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003589 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003590}
3591
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003592// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003593// resets parsing structures.
3594static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003595{
3596 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003597 bc_func_free(p->func);
3598 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003599
Denys Vlasenko65e10462018-12-19 15:13:14 +01003600 p->fidx = BC_PROG_MAIN;
3601 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003602 }
3603
3604 p->l.i = p->l.len;
3605 p->l.t.t = BC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003606
Denys Vlasenko503faf92018-12-20 16:24:18 +01003607 IF_BC(bc_vec_pop_all(&p->exits);)
3608 IF_BC(bc_vec_pop_all(&p->conds);)
3609 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003610
Denys Vlasenkod38af482018-12-04 19:11:02 +01003611 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003612}
3613
3614static void bc_parse_free(BcParse *p)
3615{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003616 IF_BC(bc_vec_free(&p->exits);)
3617 IF_BC(bc_vec_free(&p->conds);)
3618 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003619 bc_lex_free(&p->l);
3620}
3621
Denys Vlasenkofa210792018-12-19 19:35:40 +01003622static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003623{
3624 memset(p, 0, sizeof(BcParse));
3625
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003626 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003627 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3628 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3629 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003630
Denys Vlasenkofa210792018-12-19 19:35:40 +01003631 p->fidx = fidx;
3632 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003633}
3634
Denys Vlasenko04715442018-12-21 00:10:26 +01003635static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003636{
Denys Vlasenko04715442018-12-21 00:10:26 +01003637 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003638 BcFunc f;
3639 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003640 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003641 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003642 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003643}
3644
3645#if ENABLE_BC
3646
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003647// Note: takes ownership of 'name' (must be malloced)
3648static size_t bc_program_addFunc(char *name)
3649{
3650 size_t idx;
3651 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003652 int inserted;
3653
3654 entry.name = name;
3655 entry.idx = G.prog.fns.len;
3656
3657 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3658 if (!inserted) free(name);
3659
3660 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3661 idx = entry_ptr->idx;
3662
3663 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003664 // There is already a function with this name.
3665 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003666 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003667 bc_func_free(func);
3668 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003669 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003670 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003671 }
3672
3673 return idx;
3674}
3675
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003676#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3677#define BC_PARSE_LEAF(p, rparen) \
3678 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3679 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3680
3681// We can calculate the conversion between tokens and exprs by subtracting the
3682// position of the first operator in the lex enum and adding the position of the
3683// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko0154d782018-12-14 23:32:51 +01003684#define BC_TOKEN_2_INST(t) ((char) ((t) - BC_LEX_NEG + BC_INST_NEG))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003685
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003686static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3687
3688static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3689{
3690 BcStatus s;
3691
3692 s = bc_parse_expr_empty_ok(p, flags);
3693 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3694 RETURN_STATUS(bc_error("empty expression"));
3695 RETURN_STATUS(s);
3696}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003697#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003698
3699static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003700#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003701
3702static BC_STATUS zbc_parse_stmt(BcParse *p)
3703{
3704 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3705}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003706#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003707
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003708static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003709{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003710 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3711 // Same for "else", "while()", "for()".
3712 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3713 if (s) RETURN_STATUS(s);
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003714 if (p->l.t.t == BC_LEX_NLINE)
3715 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003716
3717 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003718}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003719#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003720
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003721static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3722 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003723{
Denys Vlasenko65437582018-12-05 19:37:19 +01003724 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3725 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003726
3727 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003728 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003729 if (t == BC_LEX_LPAREN) break;
3730
Denys Vlasenko65437582018-12-05 19:37:19 +01003731 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003732 if (l >= r && (l != r || !left)) break;
3733
Denys Vlasenko0154d782018-12-14 23:32:51 +01003734 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003735 bc_vec_pop(&p->ops);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003736 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003737 }
3738
3739 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003740}
3741
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003742static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003743{
3744 BcLexType top;
3745
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003746 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003747 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003748 top = BC_PARSE_TOP_OP(p);
3749
3750 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003751 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003752
3753 bc_vec_pop(&p->ops);
3754 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3755
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003756 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003757 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003758 top = BC_PARSE_TOP_OP(p);
3759 }
3760
3761 bc_vec_pop(&p->ops);
3762
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003763 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003764}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003765#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003766
Denys Vlasenko26819db2018-12-12 16:08:46 +01003767static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003768{
3769 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003770 size_t nparams;
3771
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003772 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003773 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3774
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003775 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003776 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003777
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003778 nparams = 0;
3779 if (p->l.t.t != BC_LEX_RPAREN) {
3780 for (;;) {
3781 s = zbc_parse_expr(p, flags);
3782 if (s) RETURN_STATUS(s);
3783 nparams++;
3784 if (p->l.t.t != BC_LEX_COMMA) {
3785 if (p->l.t.t == BC_LEX_RPAREN)
3786 break;
3787 RETURN_STATUS(bc_error_bad_token());
3788 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003789 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003790 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003791 }
3792 }
3793
Gavin Howard01055ba2018-11-03 11:00:21 -06003794 bc_parse_push(p, BC_INST_CALL);
3795 bc_parse_pushIndex(p, nparams);
3796
Denys Vlasenko26819db2018-12-12 16:08:46 +01003797 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003798}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003799#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003800
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003801// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003802static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003803{
3804 BcStatus s;
3805 BcId entry, *entry_ptr;
3806 size_t idx;
3807
3808 entry.name = name;
3809
Denys Vlasenko26819db2018-12-12 16:08:46 +01003810 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003811 if (s) goto err;
3812
3813 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003814 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003815 goto err;
3816 }
3817
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003818 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003819
3820 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003821 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003822 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003823 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003824 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003825 free(name);
3826
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003827 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003828 bc_parse_pushIndex(p, entry_ptr->idx);
3829
Denys Vlasenko26819db2018-12-12 16:08:46 +01003830 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003831 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003832 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003833 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003834}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003835#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003836
Denys Vlasenko26819db2018-12-12 16:08:46 +01003837static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003838{
3839 BcStatus s;
3840 char *name;
3841
3842 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003843 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003844 if (s) goto err;
3845
3846 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003847 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003848 if (s) goto err;
3849
3850 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003851 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003852 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003853 goto err;
3854 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003855 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003856 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003857 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003858 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003859 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003860 if (s) goto err;
3861 }
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 bc_parse_push(p, *type);
3865 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003866 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003867 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003869 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003870 goto err;
3871 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003873 s = zbc_parse_call(p, name, flags);
3874 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003875 *type = BC_INST_VAR;
3876 bc_parse_push(p, BC_INST_VAR);
3877 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003878 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003879 }
3880
Denys Vlasenko26819db2018-12-12 16:08:46 +01003881 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003882 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003883 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003884 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003885}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003886#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003887
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003888static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003889{
3890 BcStatus s;
3891
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003892 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003893 if (s) RETURN_STATUS(s);
3894 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003895
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003896 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003897 if (s) RETURN_STATUS(s);
3898 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003899
3900 bc_parse_push(p, BC_INST_READ);
3901
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003902 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003903}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003904#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003905
Denys Vlasenko26819db2018-12-12 16:08:46 +01003906static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 BcInst *prev)
3908{
3909 BcStatus s;
3910
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003911 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003912 if (s) RETURN_STATUS(s);
3913 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003914
3915 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3916
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003917 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003918 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003919
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003920 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003921 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003922
Denys Vlasenko26819db2018-12-12 16:08:46 +01003923 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003924
3925 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3926 bc_parse_push(p, *prev);
3927
Denys Vlasenko26819db2018-12-12 16:08:46 +01003928 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003929}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003930#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003931
Denys Vlasenko26819db2018-12-12 16:08:46 +01003932static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003933{
3934 BcStatus s;
3935
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003936 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003937 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003938
3939 if (p->l.t.t != BC_LEX_LPAREN) {
3940 *type = BC_INST_SCALE;
3941 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003942 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003943 }
3944
3945 *type = BC_INST_SCALE_FUNC;
3946 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3947
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003948 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003949 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003951 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
3953 if (p->l.t.t != BC_LEX_RPAREN)
3954 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003955 bc_parse_push(p, BC_INST_SCALE_FUNC);
3956
Denys Vlasenko26819db2018-12-12 16:08:46 +01003957 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003958}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003959#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003960
Denys Vlasenko26819db2018-12-12 16:08:46 +01003961static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003962 size_t *nexprs, uint8_t flags)
3963{
3964 BcStatus s;
3965 BcLexType type;
3966 char inst;
3967 BcInst etype = *prev;
3968
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003969 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM
3970 || etype == BC_INST_SCALE || etype == BC_INST_LAST
3971 || etype == BC_INST_IBASE || etype == BC_INST_OBASE
3972 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003973 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3974 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003975 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003976 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3978 *paren_expr = true;
3979
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003980 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003981 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003982 type = p->l.t.t;
3983
3984 // Because we parse the next part of the expression
3985 // right here, we need to increment this.
3986 *nexprs = *nexprs + 1;
3987
3988 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003989 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01003990 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003991 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003992 case BC_LEX_KEY_IBASE:
3993 case BC_LEX_KEY_LAST:
3994 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06003995 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003996 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003997 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003998 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003999 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004000 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004001 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004002 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004003 else
4004 bc_parse_push(p, BC_INST_SCALE);
4005 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004006 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004007 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004008 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004009 }
4010
4011 if (!s) bc_parse_push(p, inst);
4012 }
4013
Denys Vlasenko26819db2018-12-12 16:08:46 +01004014 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004015}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004016#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004017
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004018static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004019 bool rparen, size_t *nexprs)
4020{
4021 BcStatus s;
4022 BcLexType type;
4023 BcInst etype = *prev;
4024
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004025 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004026 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004027
4028 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4029 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4030 BC_LEX_OP_MINUS :
4031 BC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004032 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004033
4034 // We can just push onto the op stack because this is the largest
4035 // precedence operator that gets pushed. Inc/dec does not.
4036 if (type != BC_LEX_OP_MINUS)
4037 bc_vec_push(&p->ops, &type);
4038 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004039 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004040
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004041 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004042}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004043#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004044
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004045static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004046{
4047 BcStatus s;
4048 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004049
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004050 for (;;) {
4051 s = zbc_lex_next(&p->l);
4052 if (s) RETURN_STATUS(s);
4053 type = p->l.t.t;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004054 if (type == BC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004055 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004056 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004057 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004058 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004059 if (s) RETURN_STATUS(s);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004060 bc_parse_push(p, BC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004061 if (p->l.t.t != BC_LEX_COMMA)
4062 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 }
4064
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004065 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004066}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004067#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004068
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004069static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004070{
4071 BcStatus s;
4072 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004073
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004074 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004075 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004076 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004077
4078 t = p->l.t.t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004079 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4080 bc_parse_push(p, BC_INST_RET0);
4081 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004082 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004083 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004084 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004085 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004086 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004087 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004088 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004089
4090 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004091 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004092 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004093 }
4094
4095 bc_parse_push(p, BC_INST_RET);
4096 }
4097
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004098 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004100}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004101#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004102
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004103static void rewrite_label_to_current(BcParse *p, size_t idx)
4104{
4105 size_t *label = bc_vec_item(&p->func->labels, idx);
4106 *label = p->func->code.len;
4107}
4108
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004109static BC_STATUS zbc_parse_if(BcParse *p)
4110{
4111 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004112 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004113
4114 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4115 s = zbc_lex_next(&p->l);
4116 if (s) RETURN_STATUS(s);
4117 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4118
4119 s = zbc_lex_next(&p->l);
4120 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004121 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004122 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004123 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004124
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004125 // Encode "if zero, jump to ..."
4126 // Pushed value (destination of the jump) is uninitialized,
4127 // will be rewritten to be address of "end of if()" or of "else".
4128 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004129 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004130
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004131 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004132 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004133
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004134 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4135 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004136 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004137
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004138 // Encode "after then_stmt, jump to end of if()"
4139 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4140 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004141 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004142
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004143 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 +01004144 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004145
Denys Vlasenko15850832018-12-16 21:40:54 +01004146 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004147
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004148 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004149 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004150 }
4151
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004152 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 +01004153 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004154
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004155 dbg_lex_done("%s:%d done", __func__, __LINE__);
4156 RETURN_STATUS(s);
4157}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004158#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004160static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004161{
4162 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004163 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004164 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004165
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004166 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004167 if (s) RETURN_STATUS(s);
4168 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004169 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004170 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004171
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004172 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004173 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004174 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004175
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004176 bc_vec_push(&p->exits, &ip_idx);
4177 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004178
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004179 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004180 if (s) RETURN_STATUS(s);
4181 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004182
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004183 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004184
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004185 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004186 if (s) RETURN_STATUS(s);
4187
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004188 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004189 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004190
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004191 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004192 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004193
4194 bc_vec_pop(&p->exits);
4195 bc_vec_pop(&p->conds);
4196
4197 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004198}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004199#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004200
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004201static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004202{
4203 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004204 size_t cond_idx, exit_idx, body_idx, update_idx;
4205
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004206 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004207 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004208 if (s) RETURN_STATUS(s);
4209 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004210 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004211 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004212
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004213 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004214 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004215 bc_parse_push(p, BC_INST_POP);
4216 if (s) RETURN_STATUS(s);
4217 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004218 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004219 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4220 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004221
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004222 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004223 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004224 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004225
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004226 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004227 update_idx = cond_idx + 1;
4228 body_idx = update_idx + 1;
4229 exit_idx = body_idx + 1;
4230
Gavin Howard01055ba2018-11-03 11:00:21 -06004231 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004232 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004233 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004234 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004235 // This is safe to set because the current token is a semicolon,
4236 // which has no string requirement.
4237 bc_vec_string(&p->l.t.v, 1, "1");
4238 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004239 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004240 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004241 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004242
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004243 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004244
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004245 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004246 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004247
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004248 bc_parse_pushJUMP_ZERO(p, exit_idx);
4249 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004250
Gavin Howard01055ba2018-11-03 11:00:21 -06004251 bc_vec_push(&p->conds, &update_idx);
4252 bc_vec_push(&p->func->labels, &p->func->code.len);
4253
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004254 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004255 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004256 if (s) RETURN_STATUS(s);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004257 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
4258 bc_parse_push(p, BC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004259 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004260 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004261 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4262 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004263
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004264 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004265 bc_vec_push(&p->func->labels, &p->func->code.len);
4266
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004267 bc_vec_push(&p->exits, &exit_idx);
4268 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004269
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004270 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004271 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004272
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004273 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004274 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004275
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004276 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004277 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004278
4279 bc_vec_pop(&p->exits);
4280 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004282 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004283}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004284#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004285
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004286static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004287{
4288 BcStatus s;
4289 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004290
Gavin Howard01055ba2018-11-03 11:00:21 -06004291 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004292 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4293 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004294 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004295 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004296 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004297 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004298
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004299 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004300
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004301 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004302 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004303
4304 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004305 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004306
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004307 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004308}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004309#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004311static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004312{
4313 BcStatus s;
4314 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004315 char *name;
4316
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004317 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004318 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004319 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004320 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004321 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004322
4323 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004324 p->fidx = bc_program_addFunc(name);
4325 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004326
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004327 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004328 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004329 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004330 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004331 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004332 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004333
4334 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004335 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004336 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004337
4338 ++p->func->nparams;
4339
4340 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004341 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004342 if (s) goto err;
4343
4344 var = p->l.t.t != BC_LEX_LBRACKET;
4345
4346 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004347 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004348 if (s) goto err;
4349
4350 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004351 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004352 goto err;
4353 }
4354
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
4359 comma = p->l.t.t == BC_LEX_COMMA;
4360 if (comma) {
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
Denys Vlasenko29301232018-12-11 15:29:32 +01004365 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004366 if (s) goto err;
4367 }
4368
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004369 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004370
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004371 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004372 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004373
4374 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004375 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004376
Denys Vlasenko202dd192018-12-16 17:30:35 +01004377 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4378 s = zbc_lex_skip_if_at_NLINE(&p->l);
4379 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004380 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4381 if (p->l.t.t != BC_LEX_LBRACE)
4382 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004383
4384 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004385 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004386 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004387 if (s) RETURN_STATUS(s);
4388
4389 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004390
4391 // Subsequent code generation is into main program
4392 p->fidx = BC_PROG_MAIN;
4393 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004394
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004395 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004396 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004397 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004398 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004399 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004400 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004401}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004402#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004403
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004404static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004405{
4406 BcStatus s;
4407 bool comma, var, one;
4408 char *name;
4409
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004410 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004411 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004412 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004413
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004414 comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004415 one = p->l.t.t == BC_LEX_NAME;
4416
4417 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004418 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004419 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004420 if (s) goto err;
4421
4422 var = p->l.t.t != BC_LEX_LBRACKET;
4423 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004424 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004425 if (s) goto err;
4426
4427 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004428 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004429 goto err;
4430 }
4431
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004432 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004433 if (s) goto err;
4434 }
4435
4436 comma = p->l.t.t == BC_LEX_COMMA;
4437 if (comma) {
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
Denys Vlasenko29301232018-12-11 15:29:32 +01004442 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004443 if (s) goto err;
4444 }
4445
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004446 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4447 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004448
4449 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004450 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004451
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004452 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004453 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004454 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004455 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004456 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004457 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004458}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004459#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004460
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004461#undef zbc_parse_stmt_possibly_auto
4462static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004463{
4464 BcStatus s = BC_STATUS_SUCCESS;
4465
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004466 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004467
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004468 if (p->l.t.t == BC_LEX_NLINE) {
4469 dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__);
4470 RETURN_STATUS(zbc_lex_next(&p->l));
4471 }
4472 if (p->l.t.t == BC_LEX_SCOLON) {
4473 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4474 RETURN_STATUS(zbc_lex_next(&p->l));
4475 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004476
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004477 if (p->l.t.t == BC_LEX_LBRACE) {
4478 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4479 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004480 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004481 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004482 } while (p->l.t.t == BC_LEX_NLINE);
4483 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4484 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4485 s = zbc_parse_auto(p);
4486 if (s) RETURN_STATUS(s);
4487 }
4488 while (p->l.t.t != BC_LEX_RBRACE) {
4489 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4490 s = zbc_parse_stmt(p);
4491 if (s) RETURN_STATUS(s);
4492 }
4493 s = zbc_lex_next(&p->l);
4494 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4495 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004496 }
4497
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004498 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004500 case BC_LEX_OP_INC:
4501 case BC_LEX_OP_DEC:
4502 case BC_LEX_OP_MINUS:
4503 case BC_LEX_OP_BOOL_NOT:
4504 case BC_LEX_LPAREN:
4505 case BC_LEX_NAME:
4506 case BC_LEX_NUMBER:
4507 case BC_LEX_KEY_IBASE:
4508 case BC_LEX_KEY_LAST:
4509 case BC_LEX_KEY_LENGTH:
4510 case BC_LEX_KEY_OBASE:
4511 case BC_LEX_KEY_READ:
4512 case BC_LEX_KEY_SCALE:
4513 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004514 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004515 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004516 case BC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004517 s = zbc_parse_pushSTR(p);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004518 bc_parse_push(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004519 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004520 case BC_LEX_KEY_BREAK:
4521 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004522 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004523 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004524 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004525 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004526 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004527 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004528 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004529 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004530 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004531 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004532 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004533 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004535 // "limits" is a compile-time command,
4536 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004537 printf(
4538 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4539 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4540 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4541 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4542 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4543 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4544 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4545 "Number of vars = "BC_MAX_VARS_STR "\n"
4546 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004547 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004548 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004550 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004552 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004553 // "quit" is a compile-time command. For example,
4554 // "if (0 == 1) quit" terminates when parsing the statement,
4555 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004556 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004557 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004558 if (!p->in_funcdef)
4559 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004560 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004561 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004563 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004564 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004565 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004566 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 }
4569
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004570 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004571 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004572}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004573#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004574
Denys Vlasenko99b37622018-12-15 20:06:59 +01004575static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004576{
4577 BcStatus s;
4578
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004579 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004581 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004583 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004584 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004585 } else {
4586 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 +01004587 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004588 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004589
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004590 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004591 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004592}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004593#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004594
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004595// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004596static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004597{
4598 BcStatus s = BC_STATUS_SUCCESS;
4599 BcInst prev = BC_INST_PRINT;
4600 BcLexType top, t = p->l.t.t;
4601 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004602 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004603 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4604
Denys Vlasenko17df8822018-12-14 23:00:24 +01004605 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004606 paren_first = p->l.t.t == BC_LEX_LPAREN;
4607 nparens = nrelops = 0;
4608 paren_expr = rprn = done = get_token = assign = false;
4609 bin_last = true;
4610
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004611 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004612 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004613 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 case BC_LEX_OP_INC:
4615 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004616 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 rprn = get_token = bin_last = false;
4618 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004619 case BC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004620 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 rprn = get_token = false;
4622 bin_last = prev == BC_INST_MINUS;
4623 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 case BC_LEX_OP_ASSIGN_POWER:
4625 case BC_LEX_OP_ASSIGN_MULTIPLY:
4626 case BC_LEX_OP_ASSIGN_DIVIDE:
4627 case BC_LEX_OP_ASSIGN_MODULUS:
4628 case BC_LEX_OP_ASSIGN_PLUS:
4629 case BC_LEX_OP_ASSIGN_MINUS:
4630 case BC_LEX_OP_ASSIGN:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004631 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM
4632 && prev != BC_INST_SCALE && prev != BC_INST_IBASE
4633 && prev != BC_INST_OBASE && prev != BC_INST_LAST
4634 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004635 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004636 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004637 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004638 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004639 break;
4640 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004641 // Fallthrough.
4642 case BC_LEX_OP_POWER:
4643 case BC_LEX_OP_MULTIPLY:
4644 case BC_LEX_OP_DIVIDE:
4645 case BC_LEX_OP_MODULUS:
4646 case BC_LEX_OP_PLUS:
4647 case BC_LEX_OP_REL_EQ:
4648 case BC_LEX_OP_REL_LE:
4649 case BC_LEX_OP_REL_GE:
4650 case BC_LEX_OP_REL_NE:
4651 case BC_LEX_OP_REL_LT:
4652 case BC_LEX_OP_REL_GT:
4653 case BC_LEX_OP_BOOL_NOT:
4654 case BC_LEX_OP_BOOL_OR:
4655 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004656 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4657 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4658 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004659 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004660 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004661 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004662 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004663 bc_parse_operator(p, t, ops_bgn, &nexprs);
4664 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 rprn = get_token = false;
4666 bin_last = t != BC_LEX_OP_BOOL_NOT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004667 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004668 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004669 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004670 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004671 ++nparens;
4672 paren_expr = rprn = bin_last = false;
4673 get_token = true;
4674 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004675 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004676 case BC_LEX_RPAREN:
Gavin Howard01055ba2018-11-03 11:00:21 -06004677 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004678 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004679 if (nparens == 0) {
4680 s = BC_STATUS_SUCCESS;
4681 done = true;
4682 get_token = false;
4683 break;
4684 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004685 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004686 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004687 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004688 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004689 --nparens;
4690 paren_expr = rprn = true;
4691 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004692 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004693 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004694 case BC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004695 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004696 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004697 paren_expr = true;
4698 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004699 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004700 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004701 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 case BC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004703 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004704 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004705 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004706 nexprs++;
4707 prev = BC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004708 paren_expr = get_token = true;
4709 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004710 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004711 case BC_LEX_KEY_IBASE:
4712 case BC_LEX_KEY_LAST:
4713 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004714 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004715 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004716 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4717 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004718 paren_expr = get_token = true;
4719 rprn = bin_last = false;
4720 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004721 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004722 case BC_LEX_KEY_LENGTH:
4723 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004724 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004725 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004726 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004727 paren_expr = true;
4728 rprn = get_token = bin_last = false;
4729 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004730 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004731 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004732 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 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004735 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 paren_expr = true;
4737 rprn = get_token = bin_last = false;
4738 ++nexprs;
4739 prev = BC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004740 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004741 case BC_LEX_KEY_SCALE:
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_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004745 paren_expr = true;
4746 rprn = get_token = bin_last = false;
4747 ++nexprs;
4748 prev = BC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004749 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004750 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004751 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004752 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004753 }
4754
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004755 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004756 }
4757
4758 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004759 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004760
4761 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004762 top = BC_PARSE_TOP_OP(p);
4763 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4764
4765 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004766 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004767
Denys Vlasenko0154d782018-12-14 23:32:51 +01004768 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004769
4770 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4771 bc_vec_pop(&p->ops);
4772 }
4773
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004774 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004775 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004776
Gavin Howard01055ba2018-11-03 11:00:21 -06004777 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004778 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004779 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004780 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004781 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004782 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 }
4784
4785 if (flags & BC_PARSE_PRINT) {
4786 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4787 bc_parse_push(p, BC_INST_POP);
4788 }
4789
Denys Vlasenko17df8822018-12-14 23:00:24 +01004790 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004791 return s;
4792}
4793
Gavin Howard01055ba2018-11-03 11:00:21 -06004794#endif // ENABLE_BC
4795
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004796#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004797
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004798static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004799{
4800 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004801
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004802 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004803 if (s) RETURN_STATUS(s);
4804 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004805
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004806 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004807
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004808 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004809}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004810#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004811
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004812static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004813{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004814 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004815 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004816
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004817 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004818
4819 str = xstrdup(p->l.t.v.v);
4820 bc_parse_push(p, BC_INST_STR);
4821 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004822 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004823
4824 // Explanation needed here
4825 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004826 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004827
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004828 dbg_lex_done("%s:%d done", __func__, __LINE__);
4829
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004830 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004831}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004832#define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004833
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004834static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004835{
4836 BcStatus s;
4837
4838 bc_parse_push(p, inst);
4839 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004840 s = zdc_parse_register(p);
4841 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004842 }
4843
4844 if (store) {
4845 bc_parse_push(p, BC_INST_SWAP);
4846 bc_parse_push(p, BC_INST_ASSIGN);
4847 bc_parse_push(p, BC_INST_POP);
4848 }
4849
Denys Vlasenko8cd468f2018-12-12 14:54:38 +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_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004853
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004854static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004855{
4856 BcStatus s;
4857
4858 bc_parse_push(p, inst);
4859 bc_parse_push(p, BC_INST_EXEC_COND);
4860
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004861 s = zdc_parse_register(p);
4862 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004863
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004864 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004865 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004866
4867 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004868 s = zdc_parse_register(p);
4869 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004870 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004871 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06004872 bc_parse_push(p, BC_PARSE_STREND);
4873
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004874 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004875}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004876#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004877
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004878static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004879{
4880 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004881 uint8_t inst;
4882 bool assign, get_token = false;
4883
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004884 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004885 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004886 case BC_LEX_OP_REL_EQ:
4887 case BC_LEX_OP_REL_LE:
4888 case BC_LEX_OP_REL_GE:
4889 case BC_LEX_OP_REL_NE:
4890 case BC_LEX_OP_REL_LT:
4891 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004892 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004893 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004894 case BC_LEX_SCOLON:
4895 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004896 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004898 case BC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004899 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004900 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004901 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004902 case BC_LEX_NEG:
4903 case BC_LEX_NUMBER:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004904 dbg_lex("%s:%d LEX_NEG/NUMBER", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004905 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004906 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004907 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004908 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004909 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004910 }
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004911 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004912 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
4913 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004914 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004915 case BC_LEX_KEY_READ:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004916 bc_parse_push(p, BC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004917 get_token = true;
4918 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004919 case BC_LEX_OP_ASSIGN:
4920 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06004921 assign = t == BC_LEX_OP_ASSIGN;
4922 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004923 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004924 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004925 case BC_LEX_LOAD:
4926 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06004927 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004928 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004929 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 case BC_LEX_STORE_IBASE:
4931 case BC_LEX_STORE_SCALE:
4932 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004933 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004934 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004935 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004936 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004937 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004938 get_token = true;
4939 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004940 }
4941
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004942 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004943
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004944 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004945 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004946}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004947#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004948
Denys Vlasenko39287e02018-12-22 02:23:08 +01004949static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004950{
Gavin Howard01055ba2018-11-03 11:00:21 -06004951 BcLexType t;
4952
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01004953 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004954 for (;;) {
4955 BcInst inst;
4956 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004957
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004958 t = p->l.t.t;
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01004959 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004960 if (t == BC_LEX_EOF) break;
4961
4962 inst = dc_parse_insts[t];
Gavin Howard01055ba2018-11-03 11:00:21 -06004963 if (inst != BC_INST_INVALID) {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004964 dbg_lex("%s:%d", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004965 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004966 s = zbc_lex_next(&p->l);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004967 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004968 dbg_lex("%s:%d", __func__, __LINE__);
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004969 s = zdc_parse_token(p, t);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004970 }
4971 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004972 }
4973
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004974 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004975 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004976}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004977#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004978
Gavin Howard01055ba2018-11-03 11:00:21 -06004979#endif // ENABLE_DC
4980
Denys Vlasenkodf515392018-12-02 19:27:48 +01004981static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06004982{
Gavin Howard01055ba2018-11-03 11:00:21 -06004983 BcId e, *ptr;
4984 BcVec *v, *map;
4985 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01004986 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06004987
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004988 v = var ? &G.prog.vars : &G.prog.arrs;
4989 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06004990
4991 e.name = id;
4992 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01004993 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06004994
4995 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01004996 BcVec v2;
4997 bc_array_init(&v2, var);
4998 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 }
5000
5001 ptr = bc_vec_item(map, i);
5002 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005003 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005004}
5005
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005006// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005007static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005008{
Gavin Howard01055ba2018-11-03 11:00:21 -06005009 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005010 case BC_RESULT_STR:
5011 case BC_RESULT_TEMP:
5012 case BC_RESULT_IBASE:
5013 case BC_RESULT_SCALE:
5014 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005015 *num = &r->d.n;
5016 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005017 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005018 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005019 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005020 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005021 size_t len;
5022
5023 str = *bc_program_const(r->d.id.idx);
5024 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005025
5026 bc_num_init(&r->d.n, len);
5027
5028 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005029 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005030 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005031 if (s) {
5032 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005033 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005034 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005035 *num = &r->d.n;
5036 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005037 break;
5038 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005039 case BC_RESULT_VAR:
5040 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005041 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005042 BcVec *v;
5043
Denys Vlasenkodf515392018-12-02 19:27:48 +01005044 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005045
5046 if (r->t == BC_RESULT_ARRAY_ELEM) {
5047 v = bc_vec_top(v);
5048 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5049 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005050 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005051 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 break;
5053 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005054#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005055 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005056 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005058 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005059 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005060 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005061#endif
5062 default:
5063 // Testing the theory that dc does not reach LAST/ONE
5064 bb_error_msg_and_die("BUG:%d", r->t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005065 }
5066
Denys Vlasenko29301232018-12-11 15:29:32 +01005067 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005068}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005069#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005070
Denys Vlasenko29301232018-12-11 15:29:32 +01005071static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005072 BcResult **r, BcNum **rn, bool assign)
5073{
5074 BcStatus s;
5075 bool hex;
5076 BcResultType lt, rt;
5077
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005078 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005079 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005080
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005081 *r = bc_vec_item_rev(&G.prog.results, 0);
5082 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005083
5084 lt = (*l)->t;
5085 rt = (*r)->t;
5086 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5087
Denys Vlasenko29301232018-12-11 15:29:32 +01005088 s = zbc_program_num(*l, ln, false);
5089 if (s) RETURN_STATUS(s);
5090 s = zbc_program_num(*r, rn, hex);
5091 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005092
5093 // We run this again under these conditions in case any vector has been
5094 // reallocated out from under the BcNums or arrays we had.
5095 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005096 s = zbc_program_num(*l, ln, false);
5097 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005098 }
5099
5100 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005101 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005102 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005103 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005104
Denys Vlasenko29301232018-12-11 15:29:32 +01005105 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005106}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005107#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005108
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005109static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005110{
5111 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005112 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005113 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005114}
5115
Denys Vlasenko29301232018-12-11 15:29:32 +01005116static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005117{
5118 BcStatus s;
5119
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005120 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005121 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005122 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005123
Denys Vlasenko29301232018-12-11 15:29:32 +01005124 s = zbc_program_num(*r, n, false);
5125 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005126
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005127 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005128 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005129
Denys Vlasenko29301232018-12-11 15:29:32 +01005130 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005131}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005132#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005133
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005134static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005135{
5136 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005137 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005138}
5139
Denys Vlasenko259137d2018-12-11 19:42:05 +01005140static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005141{
5142 BcStatus s;
5143 BcResult *opd1, *opd2, res;
5144 BcNum *n1, *n2 = NULL;
5145
Denys Vlasenko29301232018-12-11 15:29:32 +01005146 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005147 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005148 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005149
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005150 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005151 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 -06005152 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005153 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005154
Denys Vlasenko259137d2018-12-11 19:42:05 +01005155 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005156 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005157 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005158 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005159}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005160#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005161
Denys Vlasenko26819db2018-12-12 16:08:46 +01005162static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005163{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005164 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005165 BcStatus s;
5166 BcParse parse;
5167 BcVec buf;
5168 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005169 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005170
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005171 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005172 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005173
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005174 sv_file = G.prog.file;
5175 G.prog.file = NULL;
5176
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005177 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005178 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005179
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005180 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005181 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005182
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005183 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005184 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005185 if (IS_BC) {
5186 IF_BC(s = zbc_parse_expr(&parse, 0));
5187 } else {
5188 IF_DC(s = zdc_parse_expr(&parse));
5189 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005190 if (s) goto exec_err;
5191
5192 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005193 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005194 goto exec_err;
5195 }
5196
5197 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005198 ip.inst_idx = 0;
5199 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005200
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01005201 bc_parse_push(&parse, BC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005202 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005203 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005204 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005205 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005206 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005207 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005208}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005209#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005210
5211static size_t bc_program_index(char *code, size_t *bgn)
5212{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005213 unsigned char *bytes = (void*)(code + *bgn);
5214 unsigned amt;
5215 unsigned i;
5216 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005218 amt = *bytes++;
5219 *bgn += amt + 1;
5220
5221 amt *= 8;
5222 res = 0;
5223 for (i = 0; i < amt; i += 8)
5224 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005225
5226 return res;
5227}
5228
5229static char *bc_program_name(char *code, size_t *bgn)
5230{
5231 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005232 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
Denys Vlasenko694d2982018-12-18 19:17:11 +01005234 code += *bgn;
5235 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005236 i = 0;
5237 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005238 char c = *code++;
5239 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005240 break;
5241 s[i++] = c;
5242 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005243 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005244 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005245
5246 return s;
5247}
5248
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005249static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005250{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005251#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005252 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005253 // Example: echo '[]ap' | dc
5254 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005255 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005256 return;
5257 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005258#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005259 while (*str) {
5260 int c = *str++;
5261 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005262 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005263 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005264 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005265 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005266 case 'a':
5267 bb_putchar('\a');
5268 break;
5269 case 'b':
5270 bb_putchar('\b');
5271 break;
5272 case '\\':
5273 case 'e':
5274 bb_putchar('\\');
5275 break;
5276 case 'f':
5277 bb_putchar('\f');
5278 break;
5279 case 'n':
5280 bb_putchar('\n');
5281 G.prog.nchars = SIZE_MAX;
5282 break;
5283 case 'r':
5284 bb_putchar('\r');
5285 break;
5286 case 'q':
5287 bb_putchar('"');
5288 break;
5289 case 't':
5290 bb_putchar('\t');
5291 break;
5292 default:
5293 // Just print the backslash and following character.
5294 bb_putchar('\\');
5295 ++G.prog.nchars;
5296 bb_putchar(c);
5297 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005298 }
5299 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005300 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005301 }
5302}
5303
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005304static void bc_num_printNewline(void)
5305{
5306 if (G.prog.nchars == G.prog.len - 1) {
5307 bb_putchar('\\');
5308 bb_putchar('\n');
5309 G.prog.nchars = 0;
5310 }
5311}
5312
5313#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005314static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005315{
5316 (void) radix;
5317 bb_putchar((char) num);
5318 G.prog.nchars += width;
5319}
5320#endif
5321
5322static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5323{
5324 size_t exp, pow;
5325
5326 bc_num_printNewline();
5327 bb_putchar(radix ? '.' : ' ');
5328 ++G.prog.nchars;
5329
5330 bc_num_printNewline();
5331 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5332 continue;
5333
5334 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5335 size_t dig;
5336 bc_num_printNewline();
5337 dig = num / pow;
5338 num -= dig * pow;
5339 bb_putchar(((char) dig) + '0');
5340 }
5341}
5342
5343static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5344{
5345 if (radix) {
5346 bc_num_printNewline();
5347 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005348 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005349 }
5350
5351 bc_num_printNewline();
5352 bb_putchar(bb_hexdigits_upcase[num]);
5353 G.prog.nchars += width;
5354}
5355
5356static void bc_num_printDecimal(BcNum *n)
5357{
5358 size_t i, rdx = n->rdx - 1;
5359
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005360 if (n->neg) {
5361 bb_putchar('-');
5362 G.prog.nchars++;
5363 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005364
5365 for (i = n->len - 1; i < n->len; --i)
5366 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5367}
5368
Denys Vlasenkod3401432018-12-18 17:00:35 +01005369static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005370{
5371 BcStatus s;
5372 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005373 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005374 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005375 BcNum intp, fracp, digit, frac_len;
5376 unsigned long dig, *ptr;
5377 size_t i;
5378 bool radix;
5379
5380 if (n->len == 0) {
5381 print(0, width, false);
5382 RETURN_STATUS(BC_STATUS_SUCCESS);
5383 }
5384
5385 bc_vec_init(&stack, sizeof(long), NULL);
5386 bc_num_init(&intp, n->len);
5387 bc_num_init(&fracp, n->rdx);
5388 bc_num_init(&digit, width);
5389 bc_num_init(&frac_len, BC_NUM_INT(n));
5390 bc_num_copy(&intp, n);
5391 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005392 base.cap = ARRAY_SIZE(base_digs);
5393 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005394 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005395
5396 bc_num_truncate(&intp, intp.rdx);
5397 s = zbc_num_sub(n, &intp, &fracp, 0);
5398 if (s) goto err;
5399
5400 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005401 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005402 if (s) goto err;
5403 s = zbc_num_ulong(&digit, &dig);
5404 if (s) goto err;
5405 bc_vec_push(&stack, &dig);
5406 }
5407
5408 for (i = 0; i < stack.len; ++i) {
5409 ptr = bc_vec_item_rev(&stack, i);
5410 print(*ptr, width, false);
5411 }
5412
5413 if (!n->rdx) goto err;
5414
5415 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005416 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005417 if (s) goto err;
5418 s = zbc_num_ulong(&fracp, &dig);
5419 if (s) goto err;
5420 bc_num_ulong2num(&intp, dig);
5421 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5422 if (s) goto err;
5423 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005424 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005425 if (s) goto err;
5426 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005427 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005428 bc_num_free(&frac_len);
5429 bc_num_free(&digit);
5430 bc_num_free(&fracp);
5431 bc_num_free(&intp);
5432 bc_vec_free(&stack);
5433 RETURN_STATUS(s);
5434}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005435#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005436
5437static BC_STATUS zbc_num_printBase(BcNum *n)
5438{
5439 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005440 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005441 BcNumDigitOp print;
5442 bool neg = n->neg;
5443
5444 if (neg) {
5445 bb_putchar('-');
5446 G.prog.nchars++;
5447 }
5448
5449 n->neg = false;
5450
5451 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5452 width = 1;
5453 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005454 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005455 unsigned i = G.prog.ob_t - 1;
5456 width = 0;
5457 for (;;) {
5458 width++;
5459 i /= 10;
5460 if (i == 0)
5461 break;
5462 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005463 print = bc_num_printDigits;
5464 }
5465
Denys Vlasenkod3401432018-12-18 17:00:35 +01005466 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005467 n->neg = neg;
5468
5469 RETURN_STATUS(s);
5470}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005471#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005472
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005473static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5474{
5475 BcStatus s = BC_STATUS_SUCCESS;
5476
5477 bc_num_printNewline();
5478
5479 if (n->len == 0) {
5480 bb_putchar('0');
5481 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005482 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005483 bc_num_printDecimal(n);
5484 else
5485 s = zbc_num_printBase(n);
5486
5487 if (newline) {
5488 bb_putchar('\n');
5489 G.prog.nchars = 0;
5490 }
5491
5492 RETURN_STATUS(s);
5493}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005494#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005495
Denys Vlasenko29301232018-12-11 15:29:32 +01005496static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005497{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005498 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005499 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005500 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005501 bool pop = inst != BC_INST_PRINT;
5502
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005503 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005504 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005505
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005506 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005507 s = zbc_program_num(r, &num, false);
5508 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005509
5510 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005511 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005512#if ENABLE_BC
5513 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5514#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005515 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005516 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005517
5518 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005519 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005520
5521 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005522 for (;;) {
5523 char c = *str++;
5524 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005525 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005526 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005527 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005528 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005529 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005530 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005531 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005532 }
5533 }
5534
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005535 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005536
Denys Vlasenko29301232018-12-11 15:29:32 +01005537 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005538}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005539#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005540
Denys Vlasenko29301232018-12-11 15:29:32 +01005541static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005542{
5543 BcStatus s;
5544 BcResult res, *ptr;
5545 BcNum *num = NULL;
5546
Denys Vlasenko29301232018-12-11 15:29:32 +01005547 s = zbc_program_prep(&ptr, &num);
5548 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005549
5550 bc_num_init(&res.d.n, num->len);
5551 bc_num_copy(&res.d.n, num);
5552 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5553
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005554 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005555
Denys Vlasenko29301232018-12-11 15:29:32 +01005556 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005557}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005558#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005559
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005560static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005561{
5562 BcStatus s;
5563 BcResult *opd1, *opd2, res;
5564 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005565 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005566
Denys Vlasenko29301232018-12-11 15:29:32 +01005567 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005568 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005569
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005570 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005571
5572 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005573 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005574 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005575 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005576 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005577 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005578 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005579 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005580 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005581 break;
5582 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005583 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005584 break;
5585 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005586 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005587 break;
5588 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005589 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005590 break;
5591 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005592 cond = (cond > 0);
5593 break;
5594 default: // = case BC_INST_REL_NE:
5595 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005596 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005597 }
5598 }
5599
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005600 if (cond) bc_num_one(&res.d.n);
5601 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005602
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005603 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005604
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005605 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005606}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005607#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005608
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005609#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005610static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005611{
5612 BcNum n2;
5613 BcResult res;
5614
5615 memset(&n2, 0, sizeof(BcNum));
5616 n2.rdx = res.d.id.idx = r->d.id.idx;
5617 res.t = BC_RESULT_STR;
5618
5619 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005620 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005621 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005622 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005623 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005624 }
5625
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005626 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005627 bc_vec_push(v, &n2);
5628
Denys Vlasenko29301232018-12-11 15:29:32 +01005629 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005630}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005631#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005632#endif // ENABLE_DC
5633
Denys Vlasenko29301232018-12-11 15:29:32 +01005634static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005635{
5636 BcStatus s;
5637 BcResult *ptr, r;
5638 BcVec *v;
5639 BcNum *n;
5640
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005641 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005642 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005643
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005644 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005645 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005646 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005647 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005648
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005649#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005650 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005651 RETURN_STATUS(bc_error_variable_is_wrong_type());
5652 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005653 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005654#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005655
Denys Vlasenko29301232018-12-11 15:29:32 +01005656 s = zbc_program_num(ptr, &n, false);
5657 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005658
5659 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005660 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005661
5662 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005663 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005664 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005665 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005666 bc_array_init(&r.d.v, true);
5667 bc_array_copy(&r.d.v, (BcVec *) n);
5668 }
5669
5670 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005671 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005672
Denys Vlasenko29301232018-12-11 15:29:32 +01005673 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005674}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005675#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005676
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005677static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005678{
5679 BcStatus s;
5680 BcResult *left, *right, res;
5681 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005682 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5683
Denys Vlasenko29301232018-12-11 15:29:32 +01005684 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005685 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005686
5687 ib = left->t == BC_RESULT_IBASE;
5688 sc = left->t == BC_RESULT_SCALE;
5689
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005690#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005691 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005692 BcVec *v;
5693
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005694 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005695 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005696 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005697
Denys Vlasenkofa210792018-12-19 19:35:40 +01005698 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005699 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005700#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005701
5702 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005703 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005704 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005705 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005706 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005707
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005708#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005709 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005710 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005711
5712 if (assign)
5713 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005714 else {
5715 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005716 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 +01005717 }
5718 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005719#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005720 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005721#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005722
5723 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005724 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005725 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5726 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5727 NULL, //can't happen //BC_RESULT_LAST
5728 NULL, //can't happen //BC_RESULT_CONSTANT
5729 NULL, //can't happen //BC_RESULT_ONE
5730 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005731 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005732 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005733 size_t max;
5734 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005735
Denys Vlasenko29301232018-12-11 15:29:32 +01005736 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005737 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005738 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005739 if (sc) {
5740 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005741 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005742 } else {
5743 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005744 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005745 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005746 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005747 }
5748
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005749 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005750 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005751
5752 *ptr = (size_t) val;
5753 s = BC_STATUS_SUCCESS;
5754 }
5755
5756 bc_num_init(&res.d.n, l->len);
5757 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005758 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005759
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005760 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005762#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005763
Denys Vlasenko416ce762018-12-02 20:57:17 +01005764#if !ENABLE_DC
5765#define bc_program_pushVar(code, bgn, pop, copy) \
5766 bc_program_pushVar(code, bgn)
5767// for bc, 'pop' and 'copy' are always false
5768#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005769static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005770 bool pop, bool copy)
5771{
Gavin Howard01055ba2018-11-03 11:00:21 -06005772 BcResult r;
5773 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005774
5775 r.t = BC_RESULT_VAR;
5776 r.d.id.name = name;
5777
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005778#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005779 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005780 BcVec *v = bc_program_search(name, true);
5781 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005782
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005783 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005784 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005785 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005786 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005787
5788 if (!BC_PROG_STR(num)) {
5789 r.t = BC_RESULT_TEMP;
5790 bc_num_init_DEF_SIZE(&r.d.n);
5791 bc_num_copy(&r.d.n, num);
5792 } else {
5793 r.t = BC_RESULT_STR;
5794 r.d.id.idx = num->rdx;
5795 }
5796
5797 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005798 }
5799#endif // ENABLE_DC
5800
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005801 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005802
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005803 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005804}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005805#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005806
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005807static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005808{
5809 BcStatus s = BC_STATUS_SUCCESS;
5810 BcResult r;
5811 BcNum *num;
5812
5813 r.d.id.name = bc_program_name(code, bgn);
5814
5815 if (inst == BC_INST_ARRAY) {
5816 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005817 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005818 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005819 BcResult *operand;
5820 unsigned long temp;
5821
Denys Vlasenko29301232018-12-11 15:29:32 +01005822 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005823 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005824 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005825 if (s) goto err;
5826
5827 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005828 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005829 goto err;
5830 }
5831
5832 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005833 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005834 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005835 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005836 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005837 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005838}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005839#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005840
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005841#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005842static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005843{
5844 BcStatus s;
5845 BcResult *ptr, res, copy;
5846 BcNum *num = NULL;
5847 char inst2 = inst;
5848
Denys Vlasenko29301232018-12-11 15:29:32 +01005849 s = zbc_program_prep(&ptr, &num);
5850 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005851
5852 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5853 copy.t = BC_RESULT_TEMP;
5854 bc_num_init(&copy.d.n, num->len);
5855 bc_num_copy(&copy.d.n, num);
5856 }
5857
5858 res.t = BC_RESULT_ONE;
5859 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5860 BC_INST_ASSIGN_PLUS :
5861 BC_INST_ASSIGN_MINUS;
5862
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005863 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005864 s = zbc_program_assign(inst);
5865 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005866
5867 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005868 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005869 }
5870
Denys Vlasenko29301232018-12-11 15:29:32 +01005871 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005872}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005873#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005874
Denys Vlasenko29301232018-12-11 15:29:32 +01005875static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005876{
Gavin Howard01055ba2018-11-03 11:00:21 -06005877 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005878 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005880 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005881 BcResult *arg;
5882
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005883 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005884 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005885 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005886 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005887
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005888 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005889 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005890 }
5891 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005892 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005893 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005894 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005895
5896 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005897 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005898
5899 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005900 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005901
5902 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005903 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005904
Denys Vlasenko29301232018-12-11 15:29:32 +01005905 s = zbc_program_copyToVar(a->name, a->idx);
5906 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005907 }
5908
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005909 a = bc_vec_item(&func->autos, i);
5910 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005911 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenkodf515392018-12-02 19:27:48 +01005913 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005915 BcNum n2;
5916 bc_num_init_DEF_SIZE(&n2);
5917 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005918 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005919 BcVec v2;
5920 bc_array_init(&v2, true);
5921 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005922 }
5923 }
5924
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005925 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005926
Denys Vlasenko29301232018-12-11 15:29:32 +01005927 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005928}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005929#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005930
Denys Vlasenko29301232018-12-11 15:29:32 +01005931static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005932{
Gavin Howard01055ba2018-11-03 11:00:21 -06005933 BcResult res;
5934 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005935 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005936 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005937 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005938
Denys Vlasenko24e41942018-12-21 23:01:26 +01005939 if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == BC_INST_RET)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005940 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005941
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005942 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 res.t = BC_RESULT_TEMP;
5944
5945 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005946 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005948 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005949
Denys Vlasenko29301232018-12-11 15:29:32 +01005950 s = zbc_program_num(operand, &num, false);
5951 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005952 bc_num_init(&res.d.n, num->len);
5953 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005954 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005955 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01005956 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005957 }
5958
5959 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005960 a = (void*)f->autos.v;
5961 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005962 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01005963 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 bc_vec_pop(v);
5965 }
5966
Denys Vlasenko24e41942018-12-21 23:01:26 +01005967 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005968 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005969 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
Denys Vlasenko29301232018-12-11 15:29:32 +01005971 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005972}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005973#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005974#endif // ENABLE_BC
5975
5976static unsigned long bc_program_scale(BcNum *n)
5977{
5978 return (unsigned long) n->rdx;
5979}
5980
5981static unsigned long bc_program_len(BcNum *n)
5982{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01005983 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005984
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01005985 if (n->rdx != len) return len;
5986 for (;;) {
5987 if (len == 0) break;
5988 len--;
5989 if (n->num[len] != 0) break;
5990 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005991 return len;
5992}
5993
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005994static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005995{
5996 BcStatus s;
5997 BcResult *opnd;
5998 BcNum *num = NULL;
5999 BcResult res;
6000 bool len = inst == BC_INST_LENGTH;
6001
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006002 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006003 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006004 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006005
Denys Vlasenko29301232018-12-11 15:29:32 +01006006 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006007 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006008
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006009#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006010 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006011 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006012#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006014 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006016 if (inst == BC_INST_SQRT)
6017 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006018#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006019 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006020 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006022#endif
6023#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006025 char **str;
6026 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6027
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006028 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006029 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006030 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006031#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006032 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006033 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006034 }
6035
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006036 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006037
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006038 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006039}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006040#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006041
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006042#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006043static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006044{
6045 BcStatus s;
6046 BcResult *opd1, *opd2, res, res2;
6047 BcNum *n1, *n2 = NULL;
6048
Denys Vlasenko29301232018-12-11 15:29:32 +01006049 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006050 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006051
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006052 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006053 bc_num_init(&res2.d.n, n2->len);
6054
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006055 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006056 if (s) goto err;
6057
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006058 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006060 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006061
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006062 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006063 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006064 bc_num_free(&res2.d.n);
6065 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006066 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006067}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006068#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006069
Denys Vlasenkofa210792018-12-19 19:35:40 +01006070static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006071{
6072 BcStatus s;
6073 BcResult *r1, *r2, *r3, res;
6074 BcNum *n1, *n2, *n3;
6075
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006076 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006077 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006078 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006079 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006080
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006081 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006082 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006083 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006084 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006085 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006086
6087 // Make sure that the values have their pointers updated, if necessary.
6088 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006090 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006091 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006092 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006094 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006095 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 }
6097 }
6098
6099 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006100 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 if (s) goto err;
6102
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006103 bc_vec_pop(&G.prog.results);
6104 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006106 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006107 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006108 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006109 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006110}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006111#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006112
Denys Vlasenkofa210792018-12-19 19:35:40 +01006113static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006114{
Gavin Howard01055ba2018-11-03 11:00:21 -06006115 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006116 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006117
6118 res.t = BC_RESULT_TEMP;
6119
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006120 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006121 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006122 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123}
6124
Denys Vlasenkofa210792018-12-19 19:35:40 +01006125static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006126{
6127 BcStatus s;
6128 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006129 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006130 char **strs;
6131 char *str;
6132 char c;
6133 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006134 unsigned long val;
6135
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006136 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006137 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006138 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006139
Denys Vlasenko29301232018-12-11 15:29:32 +01006140 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006141 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006142
6143 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01006144 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006145 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006146
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006147 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006148 bc_num_copy(&n, num);
6149 bc_num_truncate(&n, n.rdx);
6150
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006151 strmb.cap = ARRAY_SIZE(strmb_digs);
6152 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006153 bc_num_ulong2num(&strmb, 0x100);
6154 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006155
Gavin Howard01055ba2018-11-03 11:00:21 -06006156 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006157 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158 if (s) goto num_err;
6159
6160 c = (char) val;
6161
6162 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006163 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006164 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006166 sp = *bc_program_str(idx);
6167 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006168 }
6169
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006170 strs = (void*)G.prog.strs.v;
6171 for (idx = 0; idx < G.prog.strs.len; idx++) {
6172 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6173 goto dup;
6174 }
6175 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006176 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006177 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006178 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006179 bc_vec_push(&G.prog.strs, &str);
6180 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006182 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006183 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006184
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006185 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006186 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006187 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006188 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006189}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006190#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006191
Denys Vlasenkofa210792018-12-19 19:35:40 +01006192static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006193{
6194 BcStatus s;
6195 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006196 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006197 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006198
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006199 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006200 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006201 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202
Denys Vlasenko29301232018-12-11 15:29:32 +01006203 s = zbc_program_num(r, &n, false);
6204 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006205
Denys Vlasenko57734c92018-12-17 21:14:05 +01006206 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006207 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006208 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006209 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006211 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006212 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006213 }
6214
Denys Vlasenko29301232018-12-11 15:29:32 +01006215 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006216}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006217#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006218
Denys Vlasenkofa210792018-12-19 19:35:40 +01006219static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006220{
6221 BcStatus s;
6222 BcResult *opnd;
6223 BcNum *num = NULL;
6224 unsigned long val;
6225
Denys Vlasenko29301232018-12-11 15:29:32 +01006226 s = zbc_program_prep(&opnd, &num);
6227 if (s) RETURN_STATUS(s);
6228 s = zbc_num_ulong(num, &val);
6229 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006230
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006231 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006232
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006233 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006234 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006235 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006236 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006237 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006238
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006239 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006240
Denys Vlasenko29301232018-12-11 15:29:32 +01006241 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006242}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006243#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006244
Denys Vlasenkofa210792018-12-19 19:35:40 +01006245static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006246{
6247 BcStatus s = BC_STATUS_SUCCESS;
6248 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006249 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006250 BcInstPtr ip;
6251 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006252
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006253 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006254 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006255
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006256 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
6258 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006259 BcNum *n = n; // for compiler
6260 bool exec;
6261 char *name;
6262 char *then_name = bc_program_name(code, bgn);
6263 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
6265 if (code[*bgn] == BC_PARSE_STREND)
6266 (*bgn) += 1;
6267 else
6268 else_name = bc_program_name(code, bgn);
6269
6270 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006271 name = then_name;
6272 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006273 exec = true;
6274 name = else_name;
6275 }
6276
6277 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006278 BcVec *v;
6279 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006280 n = bc_vec_top(v);
6281 }
6282
6283 free(then_name);
6284 free(else_name);
6285
6286 if (!exec) goto exit;
6287 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006288 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 goto exit;
6290 }
6291
6292 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006293 } else {
6294 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006295 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006296 } else if (r->t == BC_RESULT_VAR) {
6297 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006298 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 if (s || !BC_PROG_STR(n)) goto exit;
6300 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006301 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006302 goto exit;
6303 }
6304
6305 fidx = sidx + BC_PROG_REQ_FUNCS;
6306
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006307 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006308
6309 if (f->code.len == 0) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006310 BcParse prs;
6311 char *str;
6312
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006313 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006314 str = *bc_program_str(sidx);
6315 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006316 if (s) goto err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01006317 s = zdc_parse_expr(&prs);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06006319 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006320 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006321 err:
6322 bc_parse_free(&prs);
6323 bc_vec_pop_all(&f->code);
6324 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006325 }
Denys Vlasenko39287e02018-12-22 02:23:08 +01006326 bc_parse_push(&prs, BC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327 bc_parse_free(&prs);
6328 }
6329
Denys Vlasenko24e41942018-12-21 23:01:26 +01006330 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006331 ip.func = fidx;
6332
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006333 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006334 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006335
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006336 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006337 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006338 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006339 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006340}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006341#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006342#endif // ENABLE_DC
6343
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006344static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006345{
Gavin Howard01055ba2018-11-03 11:00:21 -06006346 BcResult res;
6347 unsigned long val;
6348
6349 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6350 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006351 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006352 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006353 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006354 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006355 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006356
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006357 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006358 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006359 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006360}
6361
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006362static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006363{
Gavin Howard01055ba2018-11-03 11:00:21 -06006364 BcResult r, *ptr;
6365 BcNum *num;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006366 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006367 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006368 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006369
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006370 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006371 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6372 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006373 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006374 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006375
Denys Vlasenko24e41942018-12-21 23:01:26 +01006376 dbg_exec("inst at %zd:%d results.len:%d", ip->inst_idx - 1, inst, G.prog.results.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006377 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006378#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006379 case BC_INST_JUMP_ZERO: {
6380 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006381 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006382 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006383 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006384 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006385 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006386 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006387 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006388 break;
6389 }
6390 // else: fall through
6391 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006392 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006393 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006394 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006395 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006396 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 break;
6398 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006400 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006401 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006402 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 case BC_INST_INC_PRE:
6404 case BC_INST_DEC_PRE:
6405 case BC_INST_INC_POST:
6406 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006407 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006408 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006409 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006410 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006411 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006412 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006413 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006414 case BC_INST_RET:
6415 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006416 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006417 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006418 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006419 case BC_INST_BOOL_OR:
6420 case BC_INST_BOOL_AND:
6421#endif // ENABLE_BC
6422 case BC_INST_REL_EQ:
6423 case BC_INST_REL_LE:
6424 case BC_INST_REL_GE:
6425 case BC_INST_REL_NE:
6426 case BC_INST_REL_LT:
6427 case BC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006428 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006429 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006430 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006431 case BC_INST_READ:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006432 dbg_exec("BC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006433 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006434 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006435 case BC_INST_VAR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006436 dbg_exec("BC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006437 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006438 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006439 case BC_INST_ARRAY_ELEM:
6440 case BC_INST_ARRAY:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006441 dbg_exec("BC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006442 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006444 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006445 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006446 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006447 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006448 case BC_INST_IBASE:
6449 case BC_INST_SCALE:
6450 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006451 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006453 case BC_INST_SCALE_FUNC:
6454 case BC_INST_LENGTH:
6455 case BC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006456 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006457 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006458 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006459 case BC_INST_NUM:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006460 dbg_exec("BC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006461 r.t = BC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006462 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006463 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006464 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006465 case BC_INST_POP:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006466 dbg_exec("BC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006467 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006468 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006469 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006470 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006471 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006472 case BC_INST_PRINT:
6473 case BC_INST_PRINT_POP:
6474 case BC_INST_PRINT_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006475 dbg_exec("BC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006476 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006478 case BC_INST_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006479 dbg_exec("BC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 r.t = BC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006481 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006482 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006483 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006484 case BC_INST_POWER:
6485 case BC_INST_MULTIPLY:
6486 case BC_INST_DIVIDE:
6487 case BC_INST_MODULUS:
6488 case BC_INST_PLUS:
6489 case BC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006490 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006491 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006492 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006493 case BC_INST_BOOL_NOT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006494 dbg_exec("BC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006495 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006496 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006497 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006498 if (!bc_num_cmp(num, &G.prog.zero))
6499 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006500 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006501 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006502 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 case BC_INST_NEG:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006504 dbg_exec("BC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006505 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006506 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006507#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006508 case BC_INST_ASSIGN_POWER:
6509 case BC_INST_ASSIGN_MULTIPLY:
6510 case BC_INST_ASSIGN_DIVIDE:
6511 case BC_INST_ASSIGN_MODULUS:
6512 case BC_INST_ASSIGN_PLUS:
6513 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006514#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 case BC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006516 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006517 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006518 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006519#if ENABLE_DC
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006520 case BC_INST_POP_EXEC:
6521 dbg_exec("BC_INST_POP_EXEC:");
6522 bc_vec_pop(&G.prog.exestack);
6523 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006524 case BC_INST_MODEXP:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006525 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006526 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006527 case BC_INST_DIVMOD:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006528 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006529 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006530 case BC_INST_EXECUTE:
6531 case BC_INST_EXEC_COND:
Denys Vlasenko24e41942018-12-21 23:01:26 +01006532 s = zdc_program_execStr(code, &ip->inst_idx, inst == BC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006533 goto read_updated_ip;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006534 case BC_INST_PRINT_STACK: {
6535 size_t idx;
6536 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006537 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006538 if (s) break;
6539 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 break;
6541 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006542 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006543 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006545 case BC_INST_STACK_LEN:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006546 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006547 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006548 case BC_INST_DUPLICATE:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006549 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006550 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006551 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006552 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006553 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006555 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006556 BcResult *ptr2;
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006557 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006558 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006559 ptr = bc_vec_item_rev(&G.prog.results, 0);
6560 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006561 memcpy(&r, ptr, sizeof(BcResult));
6562 memcpy(ptr, ptr2, sizeof(BcResult));
6563 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006564 break;
6565 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006566 case BC_INST_ASCIIFY:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006567 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006568 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 case BC_INST_PRINT_STREAM:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006570 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006571 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006572 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006573 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 bool copy = inst == BC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006575 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006576 break;
6577 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006578 case BC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006579 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006580 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 free(name);
6582 break;
6583 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006584 case BC_INST_QUIT:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006585 dbg_exec("BC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006586 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006587 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006588 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006589 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 case BC_INST_NQUIT:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006591 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006592 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006593#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006594 read_updated_ip:
6595 // Instruction stack has changed, read new pointers
6596 ip = bc_vec_top(&G.prog.exestack);
6597 func = bc_program_func(ip->func);
6598 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006599 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006600 }
6601
Denys Vlasenkod38af482018-12-04 19:11:02 +01006602 if (s || G_interrupt) {
6603 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006604 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006605 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006606
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006607 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006608 }
6609
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006610 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006611}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006612#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006613
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006614static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006615{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006616 char *lenv;
6617 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006618
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006619 lenv = getenv(var);
6620 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006621 if (!lenv) return len;
6622
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006623 len = bb_strtou(lenv, NULL, 10) - 1;
6624 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 len = BC_NUM_PRINT_WIDTH;
6626
6627 return len;
6628}
6629
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006630static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006631{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006632 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006633
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006634 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
6635 s = zbc_parse_text_init(&G.prs, text);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006636 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006637
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006638 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko39287e02018-12-22 02:23:08 +01006639 dbg_lex("%s:%d G.prs.l.t.t:%d, parsing...", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006640 if (IS_BC) {
6641// FIXME: "eating" of stmt delemiters is coded inconsistently
6642// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6643// which causes bugs such as "print 1 print 2" erroneously accepted,
6644// or "print 1 else 2" detecting parse error only after executing
6645// "print 1" part.
6646 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6647 } else {
6648#if ENABLE_DC
6649 if (G.prs.l.t.t == BC_LEX_EOF)
6650 s = bc_error("end of file");
6651 else
6652 s = zdc_parse_expr(&G.prs);
6653#endif
6654 }
6655 if (s || G_interrupt) {
6656 bc_parse_reset(&G.prs); // includes bc_program_reset()
6657 RETURN_STATUS(BC_STATUS_FAILURE);
6658 }
6659
Denys Vlasenko39287e02018-12-22 02:23:08 +01006660 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006661 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006662 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006663 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006664 break;
6665 }
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006666 // bc discards strings, constants and code after each
6667 // top-level statement in the "main program".
6668 // This prevents "yes 1 | bc" from growing its memory
6669 // without bound. This can be done because data stack
6670 // is empty and thus can't hold any references to
6671 // strings or constants, there is no generated code
6672 // which can hold references (after we discard one
6673 // we just executed). Code of functions can have references,
6674 // but bc stores function strings/constants in per-function
6675 // storage.
6676 if (IS_BC) {
6677 BcFunc *f;
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006678 BcInstPtr *ip = (void*)G.prog.exestack.v;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006679
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006680#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006681 if (G.prog.results.len != 0) // should be empty
6682 bb_error_msg_and_die("BUG:data stack");
6683 if (G.prog.exestack.len != 1) // should have only main's IP
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006684 bb_error_msg_and_die("BUG:call stack");
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006685 if (ip->func != BC_PROG_MAIN)
6686 bb_error_msg_and_die("BUG:not MAIN");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006687#endif
Denys Vlasenko24e41942018-12-21 23:01:26 +01006688//bb_error_msg("ip->func:%d >idx:%d >len:%d", ip->func, ip->inst_idx, ip->len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006689 f = bc_program_func_BC_PROG_MAIN();
6690//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
6691 bc_vec_pop_all(&f->code);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006692 ip->inst_idx = 0;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006693 IF_BC(bc_vec_pop_all(&f->strs);)
6694 IF_BC(bc_vec_pop_all(&f->consts);)
6695 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 }
6697
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006698 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006699 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006701#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006702
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006703static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006704{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006705 // So far bc/dc have no way to include a file from another file,
6706 // therefore we know G.prog.file == NULL on entry
6707 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006708 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006709
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006710 G.prog.file = filename;
6711 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006712 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006713
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006714 do {
6715 s = zbc_vm_process("");
6716 // We do not stop looping on errors here if reading stdin.
6717 // Example: start interactive bc and enter "return".
6718 // It should say "'return' not in a function"
6719 // but should not exit.
6720 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006721 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006722 RETURN_STATUS(s);
6723}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006724#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006725
6726static BC_STATUS zbc_vm_file(const char *file)
6727{
6728 BcStatus s;
6729 FILE *fp;
6730
6731 fp = xfopen_for_read(file);
6732 s = zbc_vm_execute_FILE(fp, file);
6733 fclose(fp);
6734
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006735 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006736}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006737#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006738
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006739#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006740static void bc_vm_info(void)
6741{
6742 printf("%s "BB_VER"\n"
6743 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6744 , applet_name);
6745}
6746
6747static void bc_args(char **argv)
6748{
6749 unsigned opts;
6750 int i;
6751
6752 GETOPT_RESET();
6753#if ENABLE_FEATURE_BC_LONG_OPTIONS
6754 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6755 "warn\0" No_argument "w"
6756 "version\0" No_argument "v"
6757 "standard\0" No_argument "s"
6758 "quiet\0" No_argument "q"
6759 "mathlib\0" No_argument "l"
6760 "interactive\0" No_argument "i"
6761 );
6762#else
6763 opts = option_mask32 |= getopt32(argv, "wvsqli");
6764#endif
6765 if (getenv("POSIXLY_CORRECT"))
6766 option_mask32 |= BC_FLAG_S;
6767
6768 if (opts & BC_FLAG_V) {
6769 bc_vm_info();
6770 exit(0);
6771 }
6772
6773 for (i = optind; argv[i]; ++i)
6774 bc_vec_push(&G.files, argv + i);
6775}
6776
6777static void bc_vm_envArgs(void)
6778{
6779 BcVec v;
6780 char *buf;
6781 char *env_args = getenv("BC_ENV_ARGS");
6782
6783 if (!env_args) return;
6784
6785 G.env_args = xstrdup(env_args);
6786 buf = G.env_args;
6787
6788 bc_vec_init(&v, sizeof(char *), NULL);
6789
6790 while (*(buf = skip_whitespace(buf)) != '\0') {
6791 bc_vec_push(&v, &buf);
6792 buf = skip_non_whitespace(buf);
6793 if (!*buf)
6794 break;
6795 *buf++ = '\0';
6796 }
6797
6798 // NULL terminate, and pass argv[] so that first arg is argv[1]
6799 if (sizeof(int) == sizeof(char*)) {
6800 bc_vec_push(&v, &const_int_0);
6801 } else {
6802 static char *const nullptr = NULL;
6803 bc_vec_push(&v, &nullptr);
6804 }
6805 bc_args(((char **)v.v) - 1);
6806
6807 bc_vec_free(&v);
6808}
6809
6810static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006811 "scale=20"
6812"\n" "define e(x){"
6813"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006814////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6815//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6816//e(-.998896): GNU:.36828580434569428695
6817// above code:.36828580434569428696
6818// actual value:.3682858043456942869594...
6819// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006820"\n" "b=ibase"
6821"\n" "ibase=A"
6822"\n" "if(x<0){"
6823"\n" "n=1"
6824"\n" "x=-x"
6825"\n" "}"
6826"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006827"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006828"\n" "scale=scale(x)+1"
6829"\n" "while(x>1){"
6830"\n" "d+=1"
6831"\n" "x/=2"
6832"\n" "scale+=1"
6833"\n" "}"
6834"\n" "scale=r"
6835"\n" "r=x+1"
6836"\n" "p=x"
6837"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006838"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006839"\n" "p*=x"
6840"\n" "f*=i"
6841"\n" "v=p/f"
6842"\n" "r+=v"
6843"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006844"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006845"\n" "scale=s"
6846"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006847"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006848"\n" "return(r/1)"
6849"\n" "}"
6850"\n" "define l(x){"
6851"\n" "auto b,s,r,p,a,q,i,v"
6852"\n" "b=ibase"
6853"\n" "ibase=A"
6854"\n" "if(x<=0){"
6855"\n" "r=(1-10^scale)/1"
6856"\n" "ibase=b"
6857"\n" "return(r)"
6858"\n" "}"
6859"\n" "s=scale"
6860"\n" "scale+=6"
6861"\n" "p=2"
6862"\n" "while(x>=2){"
6863"\n" "p*=2"
6864"\n" "x=sqrt(x)"
6865"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006866"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006867"\n" "p*=2"
6868"\n" "x=sqrt(x)"
6869"\n" "}"
6870"\n" "r=a=(x-1)/(x+1)"
6871"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006872"\n" "v=1"
6873"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006874"\n" "a*=q"
6875"\n" "v=a/i"
6876"\n" "r+=v"
6877"\n" "}"
6878"\n" "r*=p"
6879"\n" "scale=s"
6880"\n" "ibase=b"
6881"\n" "return(r/1)"
6882"\n" "}"
6883"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006884"\n" "auto b,s,r,a,q,i"
6885"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006886"\n" "b=ibase"
6887"\n" "ibase=A"
6888"\n" "s=scale"
6889"\n" "scale=1.1*s+2"
6890"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006891"\n" "scale=0"
6892"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006893"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006894"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006895"\n" "scale=s+2"
6896"\n" "r=a=x"
6897"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006898"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006899"\n" "a*=q/(i*(i-1))"
6900"\n" "r+=a"
6901"\n" "}"
6902"\n" "scale=s"
6903"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006904"\n" "return(r/1)"
6905"\n" "}"
6906"\n" "define c(x){"
6907"\n" "auto b,s"
6908"\n" "b=ibase"
6909"\n" "ibase=A"
6910"\n" "s=scale"
6911"\n" "scale*=1.2"
6912"\n" "x=s(2*a(1)+x)"
6913"\n" "scale=s"
6914"\n" "ibase=b"
6915"\n" "return(x/1)"
6916"\n" "}"
6917"\n" "define a(x){"
6918"\n" "auto b,s,r,n,a,m,t,f,i,u"
6919"\n" "b=ibase"
6920"\n" "ibase=A"
6921"\n" "n=1"
6922"\n" "if(x<0){"
6923"\n" "n=-1"
6924"\n" "x=-x"
6925"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006926"\n" "if(scale<65){"
6927"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
6928"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006929"\n" "}"
6930"\n" "s=scale"
6931"\n" "if(x>.2){"
6932"\n" "scale+=5"
6933"\n" "a=a(.2)"
6934"\n" "}"
6935"\n" "scale=s+3"
6936"\n" "while(x>.2){"
6937"\n" "m+=1"
6938"\n" "x=(x-.2)/(1+.2*x)"
6939"\n" "}"
6940"\n" "r=u=x"
6941"\n" "f=-x*x"
6942"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006943"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006944"\n" "u*=f"
6945"\n" "t=u/i"
6946"\n" "r+=t"
6947"\n" "}"
6948"\n" "scale=s"
6949"\n" "ibase=b"
6950"\n" "return((m*a+r)/n)"
6951"\n" "}"
6952"\n" "define j(n,x){"
6953"\n" "auto b,s,o,a,i,v,f"
6954"\n" "b=ibase"
6955"\n" "ibase=A"
6956"\n" "s=scale"
6957"\n" "scale=0"
6958"\n" "n/=1"
6959"\n" "if(n<0){"
6960"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006961"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006962"\n" "}"
6963"\n" "a=1"
6964"\n" "for(i=2;i<=n;++i)a*=i"
6965"\n" "scale=1.5*s"
6966"\n" "a=(x^n)/2^n/a"
6967"\n" "r=v=1"
6968"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01006969"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006970"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006971"\n" "v=v*f/i/(n+i)"
6972"\n" "r+=v"
6973"\n" "}"
6974"\n" "scale=s"
6975"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006976"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006977"\n" "return(a*r/1)"
6978"\n" "}"
6979};
6980#endif // ENABLE_BC
6981
Denys Vlasenko19f11072018-12-12 22:48:19 +01006982static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006983{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01006984 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006985 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006986 size_t i;
6987
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006988#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01006989 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006990 // We know that internal library is not buggy,
6991 // thus error checking is normally disabled.
6992# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006993 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006994 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01006995 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006996 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006997#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006998
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006999 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007000 fname = (void*)G.files.v;
7001 for (i = 0; i < G.files.len; i++) {
7002 s = zbc_vm_file(*fname++);
7003 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7004 // Debug config, non-interactive mode:
7005 // return all the way back to main.
7006 // Non-debug builds do not come here
7007 // in non-interactive mode, they exit.
7008 RETURN_STATUS(s);
7009 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007010 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007011
Denys Vlasenko91cde952018-12-10 20:56:08 +01007012 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007013 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007014
Denys Vlasenko19f11072018-12-12 22:48:19 +01007015 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007016}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007017#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007018
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007019#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007020static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007021{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007022 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007023 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007024 bc_vec_free(&G.prog.vars);
7025 bc_vec_free(&G.prog.var_map);
7026 bc_vec_free(&G.prog.arrs);
7027 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007028 IF_DC(bc_vec_free(&G.prog.strs);)
7029 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007030 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007031 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007032 IF_BC(bc_num_free(&G.prog.last);)
7033 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007034 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007035 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007036}
7037
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007038static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007039{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007040 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007041 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007042 bc_parse_free(&G.prs);
7043 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007044}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007045#endif
7046
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007047static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007048{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007049 BcInstPtr ip;
7050
Denys Vlasenko82269122018-12-14 16:30:56 +01007051 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007052 memset(&ip, 0, sizeof(BcInstPtr));
7053
Denys Vlasenko82269122018-12-14 16:30:56 +01007054 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007055 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007056 G.prog.ob_t = 10;
7057
Denys Vlasenko503faf92018-12-20 16:24:18 +01007058 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7059 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007060
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007061 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007062 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007063
Denys Vlasenko503faf92018-12-20 16:24:18 +01007064 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7065 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007066
7067 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007068 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007069
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007070 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007071 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007072 // a valid function name (and be short)
7073 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007074 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007075 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007076 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007077 bc_program_add_fn();
7078 bc_program_add_fn();
7079 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007080
7081 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007082 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007083
7084 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007085 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007086
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007087 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7088 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007089 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007090 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7091 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007092
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007093 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007094}
Gavin Howard01055ba2018-11-03 11:00:21 -06007095
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007096static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007097{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007098#if ENABLE_FEATURE_EDITING
7099 G.line_input_state = new_line_input_t(DO_HISTORY);
7100#endif
7101 G.prog.len = bc_vm_envLen(env_len);
7102
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007103 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007104 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007105 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007106 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007107
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007108//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7109//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007110 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007111#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007112 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007113 // With SA_RESTART, most system calls will restart
7114 // (IOW: they won't fail with EINTR).
7115 // In particular, this means ^C won't cause
7116 // stdout to get into "error state" if SIGINT hits
7117 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007118 //
7119 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007120 // will only be handled after [Enter] since read()
7121 // from stdin is not interrupted by ^C either,
7122 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007123 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007124 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7125
7126 // Without SA_RESTART, this exhibits a bug:
7127 // "while (1) print 1" and try ^C-ing it.
7128 // Intermittently, instead of returning to input line,
7129 // you'll get "output error: Interrupted system call"
7130 // and exit.
7131 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007132#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007133 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007134 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007135 return 0; // "not a tty"
7136}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007137
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007138static BcStatus bc_vm_run(void)
7139{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007140 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007141#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007142 if (G_exiting) // it was actually "halt" or "quit"
7143 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007144 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007145# if ENABLE_FEATURE_EDITING
7146 free_line_input_t(G.line_input_state);
7147# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007148 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007149#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007150 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007151 return st;
7152}
7153
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007154#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007155int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007156int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007157{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007158 int is_tty;
7159
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007160 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007161
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007162 is_tty = bc_vm_init("BC_LINE_LENGTH");
7163
7164 bc_args(argv);
7165
7166 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7167 bc_vm_info();
7168
7169 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007170}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007171#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007172
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007173#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007174int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007175int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007176{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007177 int noscript;
7178
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007179 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007180
7181 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7182 // 1 char wider than bc from the same package.
7183 // Both default width, and xC_LINE_LENGTH=N are wider:
7184 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007185 // |1234\ |
7186 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007187 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007188 // |123\ |
7189 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007190 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007191 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007192
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007193 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7194 noscript = BC_FLAG_I;
7195 for (;;) {
7196 int n = getopt(argc, argv, "e:f:x");
7197 if (n <= 0)
7198 break;
7199 switch (n) {
7200 case 'e':
7201 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007202 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007203 if (n) return n;
7204 break;
7205 case 'f':
7206 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007207 n = zbc_vm_file(optarg);
7208 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007209 break;
7210 case 'x':
7211 option_mask32 |= DC_FLAG_X;
7212 break;
7213 default:
7214 bb_show_usage();
7215 }
7216 }
7217 argv += optind;
7218
7219 while (*argv) {
7220 noscript = 0;
7221 bc_vec_push(&G.files, argv++);
7222 }
7223
7224 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7225
7226 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007227}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007228#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007229
7230#endif // not DC_SMALL