blob: d2b74ea909a327b34ac63412a3ee2081927a5c3a [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 Vlasenkoad0bd382018-12-24 00:50:32 +0100144//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100145//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
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100254 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600255
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100256 XC_INST_POWER, // should
257 XC_INST_MULTIPLY, // match
258 XC_INST_DIVIDE, // LEX
259 XC_INST_MODULUS, // constants
260 XC_INST_PLUS, // for
261 XC_INST_MINUS, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600262
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100263 XC_INST_REL_EQ, // operations
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100264 XC_INST_REL_LE, // |
265 XC_INST_REL_GE, // |
266 XC_INST_REL_NE, // |
267 XC_INST_REL_LT, // |
268 XC_INST_REL_GT, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600269
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100270 XC_INST_BOOL_NOT, // |
271 XC_INST_BOOL_OR, // |
272 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100273#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100274 BC_INST_ASSIGN_POWER, // |
275 BC_INST_ASSIGN_MULTIPLY,// |
276 BC_INST_ASSIGN_DIVIDE, // |
277 BC_INST_ASSIGN_MODULUS, // |
278 BC_INST_ASSIGN_PLUS, // |
279 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100281 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600282
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100283 XC_INST_NUM,
284 XC_INST_VAR,
285 XC_INST_ARRAY_ELEM,
286 XC_INST_ARRAY,
287 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100288
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100289 XC_INST_IBASE, // order of these constans should match other enums
290 XC_INST_OBASE, // order of these constans should match other enums
291 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100292 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100293 XC_INST_LENGTH,
294 XC_INST_READ,
295 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600296
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100297 XC_INST_PRINT,
298 XC_INST_PRINT_POP,
299 XC_INST_STR,
300 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600301
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100302#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100303 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600304 BC_INST_JUMP,
305 BC_INST_JUMP_ZERO,
306
307 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600308 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100309#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600311
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100312 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100313#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100314 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_MODEXP,
317 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600318
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100319 DC_INST_EXECUTE,
320 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600321
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100322 DC_INST_ASCIIFY,
323 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600324
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100325 DC_INST_PRINT_STACK,
326 DC_INST_CLEAR_STACK,
327 DC_INST_STACK_LEN,
328 DC_INST_DUPLICATE,
329 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100331 DC_INST_LOAD,
332 DC_INST_PUSH_VAR,
333 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600334
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100335 DC_INST_QUIT,
336 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600337
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100338 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100339#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600340} BcInst;
341
342typedef struct BcId {
343 char *name;
344 size_t idx;
345} BcId;
346
347typedef struct BcFunc {
348 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100349 IF_BC(BcVec labels;)
350 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100351 IF_BC(BcVec strs;)
352 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100353 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600354} BcFunc;
355
356typedef enum BcResultType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600357 BC_RESULT_TEMP,
358
359 BC_RESULT_VAR,
360 BC_RESULT_ARRAY_ELEM,
361 BC_RESULT_ARRAY,
362
363 BC_RESULT_STR,
364
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100365 //code uses "inst - XC_INST_IBASE + BC_RESULT_IBASE" construct,
366 BC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
367 BC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
368 BC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100369 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600370 BC_RESULT_CONSTANT,
371 BC_RESULT_ONE,
Gavin Howard01055ba2018-11-03 11:00:21 -0600372} BcResultType;
373
374typedef union BcResultData {
375 BcNum n;
376 BcVec v;
377 BcId id;
378} BcResultData;
379
380typedef struct BcResult {
381 BcResultType t;
382 BcResultData d;
383} BcResult;
384
385typedef struct BcInstPtr {
386 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100387 size_t inst_idx;
388 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600389} BcInstPtr;
390
Gavin Howard01055ba2018-11-03 11:00:21 -0600391typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100392 XC_LEX_EOF,
393 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600394
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100395 XC_LEX_NLINE,
396 XC_LEX_WHITESPACE,
397 XC_LEX_STR,
398 XC_LEX_NAME,
399 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100400
Denys Vlasenko69560f42018-12-24 14:14:23 +0100401 XC_LEX_1st_op,
402 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100403
Denys Vlasenko69560f42018-12-24 14:14:23 +0100404 XC_LEX_OP_POWER, // should
405 XC_LEX_OP_MULTIPLY, // match
406 XC_LEX_OP_DIVIDE, // INST
407 XC_LEX_OP_MODULUS, // constants
408 XC_LEX_OP_PLUS, // for
409 XC_LEX_OP_MINUS, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100410
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100411 XC_LEX_OP_REL_EQ, // operations
Denys Vlasenko69560f42018-12-24 14:14:23 +0100412 XC_LEX_OP_REL_LE, // |
413 XC_LEX_OP_REL_GE, // |
414 XC_LEX_OP_REL_NE, // |
415 XC_LEX_OP_REL_LT, // |
416 XC_LEX_OP_REL_GT, // |
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100417 XC_LEX_OP_last = XC_LEX_OP_REL_GT,
418#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100419 BC_LEX_OP_BOOL_NOT, // |
420 BC_LEX_OP_BOOL_OR, // |
421 BC_LEX_OP_BOOL_AND, // |
422
423 BC_LEX_OP_ASSIGN_POWER, // |
424 BC_LEX_OP_ASSIGN_MULTIPLY, // |
425 BC_LEX_OP_ASSIGN_DIVIDE, // |
426 BC_LEX_OP_ASSIGN_MODULUS, // |
427 BC_LEX_OP_ASSIGN_PLUS, // |
428 BC_LEX_OP_ASSIGN_MINUS, // |
429
430 BC_LEX_OP_ASSIGN, // V
431
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_OP_INC,
433 BC_LEX_OP_DEC,
434
Gavin Howard01055ba2018-11-03 11:00:21 -0600435 BC_LEX_LPAREN,
436 BC_LEX_RPAREN,
437
438 BC_LEX_LBRACKET,
439 BC_LEX_COMMA,
440 BC_LEX_RBRACKET,
441
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100442 BC_LEX_LBRACE, // '{' is 0x7B, '}' is 0x7D,
Gavin Howard01055ba2018-11-03 11:00:21 -0600443 BC_LEX_SCOLON,
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100444 BC_LEX_RBRACE, // should be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600445
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100446 BC_LEX_KEY_1st_keyword,
447 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600448 BC_LEX_KEY_BREAK,
449 BC_LEX_KEY_CONTINUE,
450 BC_LEX_KEY_DEFINE,
451 BC_LEX_KEY_ELSE,
452 BC_LEX_KEY_FOR,
453 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100454 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
455 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
456 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100458 IF_BC(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,
Gavin Howard01055ba2018-11-03 11:00:21 -0600461 BC_LEX_KEY_PRINT,
462 BC_LEX_KEY_QUIT,
463 BC_LEX_KEY_READ,
464 BC_LEX_KEY_RETURN,
465 BC_LEX_KEY_SCALE,
466 BC_LEX_KEY_SQRT,
467 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100468#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600469
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100470#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100471 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
472 DC_LEX_OP_ASSIGN,
473
474 DC_LEX_LPAREN,
475 DC_LEX_SCOLON,
476 DC_LEX_READ,
477 DC_LEX_IBASE,
478 DC_LEX_SCALE,
479 DC_LEX_OBASE,
480 DC_LEX_LENGTH,
481 DC_LEX_PRINT,
482 DC_LEX_QUIT,
483 DC_LEX_SQRT,
484 DC_LEX_LBRACE,
485
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100486 DC_LEX_EQ_NO_REG,
487 DC_LEX_OP_MODEXP,
488 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600489
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100490 DC_LEX_COLON,
491 DC_LEX_ELSE,
492 DC_LEX_EXECUTE,
493 DC_LEX_PRINT_STACK,
494 DC_LEX_CLEAR_STACK,
495 DC_LEX_STACK_LEVEL,
496 DC_LEX_DUPLICATE,
497 DC_LEX_SWAP,
498 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600499
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100500 DC_LEX_ASCIIFY,
501 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600502
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100503 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
504 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
505 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
506 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
507 DC_LEX_LOAD,
508 DC_LEX_LOAD_POP,
509 DC_LEX_STORE_PUSH,
510 DC_LEX_PRINT_POP,
511 DC_LEX_NQUIT,
512 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600514} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515// must match order of BC_LEX_KEY_foo etc above
516#if ENABLE_BC
517struct BcLexKeyword {
518 char name8[8];
519};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520#define BC_LEX_KW_ENTRY(a, b) \
521 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100522static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100523 BC_LEX_KW_ENTRY("auto" , 1), // 0
524 BC_LEX_KW_ENTRY("break" , 1), // 1
525 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
526 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527 BC_LEX_KW_ENTRY("else" , 0), // 4
528 BC_LEX_KW_ENTRY("for" , 1), // 5
529 BC_LEX_KW_ENTRY("halt" , 0), // 6
530 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100531 BC_LEX_KW_ENTRY("obase" , 1), // 8
532 BC_LEX_KW_ENTRY("if" , 1), // 9
533 BC_LEX_KW_ENTRY("last" , 0), // 10
534 BC_LEX_KW_ENTRY("length" , 1), // 11
535 BC_LEX_KW_ENTRY("limits" , 0), // 12
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100536 BC_LEX_KW_ENTRY("print" , 0), // 13
537 BC_LEX_KW_ENTRY("quit" , 1), // 14
538 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100539 BC_LEX_KW_ENTRY("return" , 1), // 16
540 BC_LEX_KW_ENTRY("scale" , 1), // 17
541 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
542 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100545#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100547#define STRING_if (bc_lex_kws[9].name8)
548#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100549enum {
550 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100551 | (1 << 0) // 0
552 | (1 << 1) // 1
553 | (0 << 2) // 2
554 | (1 << 3) // 3
555 | (0 << 4) // 4
556 | (1 << 5) // 5
557 | (0 << 6) // 6
558 | (1 << 7) // 7
559 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100560 | (1 << 9) // 9
561 | (0 << 10) // 10
562 | (1 << 11) // 11
563 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100564 | (0 << 13) // 13
565 | (1 << 14) // 14
566 | (0 << 15) // 15
567 | (1 << 16) // 16
568 | (1 << 17) // 17
569 | (1 << 18) // 18
570 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100571};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100572#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100573
574// This is a bit array that corresponds to token types. An entry is
575// true if the token is valid in an expression, false otherwise.
576// Used to figure out when expr parsing should stop *without error message*
577// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100578// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100579// by later processing.
580enum {
581#define EXBITS(a,b,c,d,e,f,g,h) \
582 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100583 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100584 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
585 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: ^ * / % + - == <=
586 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: >= != < > ! || && ^=
587 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
588 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100589 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
590 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
591 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100592#undef EXBITS
593};
594static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
595{
596#if ULONG_MAX > 0xffffffff
597 // 64-bit version (will not work correctly for 32-bit longs!)
598 return BC_PARSE_EXPRS_BITS & (1UL << i);
599#else
600 // 32-bit version
601 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
602 if (i >= 32) {
603 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
604 i &= 31;
605 }
606 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100607#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100608}
609
610// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100611// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612static const uint8_t bc_parse_ops[] = {
613#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614 OP(1, false), // neg
615 OP(2, false), // pow
616 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
617 OP(4, true ), OP( 4, true ), // + -
618 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
619 OP(1, false), // not
620 OP(7, true ), OP( 7, true ), // or and
621 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
622 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100623 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100624#undef OP
625};
626#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
627#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
628#endif // ENABLE_BC
629
630#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100631static const //BcLexType - should be this type
632uint8_t
633dc_char_to_LEX[] = {
634 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100635 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100636 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100637 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100638 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100639 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100640 /* 0123456789 */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100641 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100644 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100645 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
646 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100647 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100648 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
649 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100650 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100651 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
652 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100653 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100654 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
655 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100656 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100657 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100658 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100659 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100660 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100661 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
662 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100663 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100666 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100667 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
668 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100669 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100670 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100671 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100672 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100673};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100674static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100675int8_t
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100676dc_LEX_to_INST[] = { // (so many INVALIDs b/c dc parser does not generate these LEXs) // corresponding XC/DC_LEX_xyz:
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100677 DC_INST_INVALID, DC_INST_INVALID, // EOF INVALID
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100678 DC_INST_INVALID, DC_INST_INVALID, // NLINE WHITESPACE
679 DC_INST_INVALID, DC_INST_INVALID, DC_INST_INVALID, // STR NAME NUMBER
Denys Vlasenko69560f42018-12-24 14:14:23 +0100680 DC_INST_INVALID, // NEG
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100681 XC_INST_POWER, XC_INST_MULTIPLY, XC_INST_DIVIDE, // OP_POWER OP_MULTIPLY OP_DIVIDE
682 XC_INST_MODULUS, XC_INST_PLUS, XC_INST_MINUS, // OP_MODULUS OP_PLUS OP_MINUS
683 DC_INST_INVALID, DC_INST_INVALID, DC_INST_INVALID, DC_INST_INVALID, // OP_REL_EQ OP_REL_LE OP_REL_GE OP_REL_NE
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100684 DC_INST_INVALID, DC_INST_INVALID, // OP_REL_LT OP_REL_GT
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100685 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
686 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
687 XC_INST_REL_GT, // DC_LEX_LPAREN
688 DC_INST_INVALID, // DC_LEX_SCOLON
689 DC_INST_INVALID, // DC_LEX_READ
690 XC_INST_IBASE, // DC_LEX_IBASE
691 XC_INST_SCALE, // DC_LEX_SCALE
692 XC_INST_OBASE, // DC_LEX_OBASE
693 XC_INST_LENGTH, // DC_LEX_LENGTH
694 XC_INST_PRINT, // DC_LEX_PRINT
695 DC_INST_QUIT, // DC_LEX_QUIT
696 XC_INST_SQRT, // DC_LEX_SQRT
697 XC_INST_REL_GE, // DC_LEX_LBRACE
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100698 XC_INST_REL_EQ, DC_INST_MODEXP, DC_INST_DIVMOD, DC_INST_INVALID, // EQ_NO_REG OP_MODEXP OP_DIVMOD COLON
699 DC_INST_INVALID, DC_INST_EXECUTE, DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, //ELSE EXECUTE PRINT_STACK CLEAR_STACK
700 DC_INST_STACK_LEN, DC_INST_DUPLICATE, DC_INST_SWAP, XC_INST_POP, // STACK_LEVEL DUPLICATE SWAP POP
701 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, DC_INST_INVALID, DC_INST_INVALID, //ASCIIFY PRINT_STREAM STORE_IBASE STORE_OBASE
702 DC_INST_INVALID, DC_INST_INVALID, DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD LOAD_POP STORE_PUSH
703 XC_INST_PRINT, DC_INST_NQUIT, XC_INST_SCALE_FUNC, // PRINT_POP NQUIT SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100704 // DC_INST_INVALID in this table either means that corresponding LEX
705 // is not possible for dc, or that it does not compile one-to-one
706 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100707};
708#endif // ENABLE_DC
709
Gavin Howard01055ba2018-11-03 11:00:21 -0600710typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600711 const char *buf;
712 size_t i;
713 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600714 size_t len;
715 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600716 struct {
717 BcLexType t;
718 BcLexType last;
719 BcVec v;
720 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600721} BcLex;
722
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100723#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600724
Denys Vlasenko39287e02018-12-22 02:23:08 +0100725#if ENABLE_BC
726# define BC_PARSE_REL (1 << 0)
727# define BC_PARSE_PRINT (1 << 1)
728# define BC_PARSE_ARRAY (1 << 2)
729# define BC_PARSE_NOCALL (1 << 3)
730#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600731
Gavin Howard01055ba2018-11-03 11:00:21 -0600732typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600733 BcLex l;
734
Denys Vlasenko503faf92018-12-20 16:24:18 +0100735 IF_BC(BcVec exits;)
736 IF_BC(BcVec conds;)
737 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600738
Gavin Howard01055ba2018-11-03 11:00:21 -0600739 BcFunc *func;
740 size_t fidx;
741
Denys Vlasenko503faf92018-12-20 16:24:18 +0100742 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600743} BcParse;
744
Gavin Howard01055ba2018-11-03 11:00:21 -0600745typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600746 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100747 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600748
Denys Vlasenko503faf92018-12-20 16:24:18 +0100749 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600750 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600751 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600752
Gavin Howard01055ba2018-11-03 11:00:21 -0600753 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100754 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600755
756 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100757 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600758
759 BcVec vars;
760 BcVec var_map;
761
762 BcVec arrs;
763 BcVec arr_map;
764
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100765 IF_DC(BcVec strs;)
766 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600767
768 const char *file;
769
Gavin Howard01055ba2018-11-03 11:00:21 -0600770 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100771 IF_BC(BcNum one;)
772 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600773} BcProgram;
774
Gavin Howard01055ba2018-11-03 11:00:21 -0600775#define BC_PROG_MAIN (0)
776#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100777#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600778#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100779#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600780
781#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
782#define BC_PROG_NUM(r, n) \
783 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
784
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100785#define BC_FLAG_W (1 << 0)
786#define BC_FLAG_V (1 << 1)
787#define BC_FLAG_S (1 << 2)
788#define BC_FLAG_Q (1 << 3)
789#define BC_FLAG_L (1 << 4)
790#define BC_FLAG_I (1 << 5)
791#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600792
793#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
794#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
795
Denys Vlasenko64074a12018-12-07 15:50:14 +0100796#define BC_MAX_OBASE ((unsigned) 999)
797#define BC_MAX_DIM ((unsigned) INT_MAX)
798#define BC_MAX_SCALE ((unsigned) UINT_MAX)
799#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
800#define BC_MAX_NUM BC_MAX_STRING
801// Unused apart from "limits" message. Just show a "biggish number" there.
802//#define BC_MAX_NAME BC_MAX_STRING
803//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
804//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
805#define BC_MAX_NAME_STR "999999999"
806#define BC_MAX_EXP_STR "999999999"
807#define BC_MAX_VARS_STR "999999999"
808
809#define BC_MAX_OBASE_STR "999"
810
811#if INT_MAX == 2147483647
812# define BC_MAX_DIM_STR "2147483647"
813#elif INT_MAX == 9223372036854775807
814# define BC_MAX_DIM_STR "9223372036854775807"
815#else
816# error Strange INT_MAX
817#endif
818
819#if UINT_MAX == 4294967295
820# define BC_MAX_SCALE_STR "4294967295"
821# define BC_MAX_STRING_STR "4294967294"
822#elif UINT_MAX == 18446744073709551615
823# define BC_MAX_SCALE_STR "18446744073709551615"
824# define BC_MAX_STRING_STR "18446744073709551614"
825#else
826# error Strange UINT_MAX
827#endif
828#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600829
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100830struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100831 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100832 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600833
834 BcParse prs;
835 BcProgram prog;
836
Denys Vlasenko5318f812018-12-05 17:48:01 +0100837 // For error messages. Can be set to current parsed line,
838 // or [TODO] to current executing line (can be before last parsed one)
839 unsigned err_line;
840
Gavin Howard01055ba2018-11-03 11:00:21 -0600841 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100842 BcVec input_buffer;
843 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600844
845 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100846
847#if ENABLE_FEATURE_EDITING
848 line_input_t *line_input_state;
849#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100850} FIX_ALIASING;
851#define G (*ptr_to_globals)
852#define INIT_G() do { \
853 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
854} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100855#define FREE_G() do { \
856 FREE_PTR_TO_GLOBALS(); \
857} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100858#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
859#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100860#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100861#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100862# define G_interrupt bb_got_signal
863# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100864#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100865# define G_interrupt 0
866# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100867#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100868#if ENABLE_FEATURE_CLEAN_UP
869# define G_exiting G.exiting
870#else
871# define G_exiting 0
872#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100873#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100874#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100875
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100876// In configurations where errors abort instead of propagating error
877// return code up the call chain, functions returning BC_STATUS
878// actually don't return anything, they always succeed and return "void".
879// A macro wrapper is provided, which makes this statement work:
880// s = zbc_func(...)
881// and makes it visible to the compiler that s is always zero,
882// allowing compiler to optimize dead code after the statement.
883//
884// To make code more readable, each such function has a "z"
885// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
886//
887#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100888# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100889# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100890# define IF_ERROR_RETURN_POSSIBLE(a) a
891# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100892# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100893# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100894#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100895# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100896# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100897# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
898# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100899# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100900# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901#endif
902
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100903//
904// Utility routines
905//
Gavin Howard01055ba2018-11-03 11:00:21 -0600906
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100907static void fflush_and_check(void)
908{
909 fflush_all();
910 if (ferror(stdout) || ferror(stderr))
911 bb_perror_msg_and_die("output error");
912}
913
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100914#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100915#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100916do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100917 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100918 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100919 return BC_STATUS_FAILURE; \
920} while (0)
921#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100922#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100923#endif
924
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100925static void quit(void) NORETURN;
926static void quit(void)
927{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100928 if (ferror(stdin))
929 bb_perror_msg_and_die("input error");
930 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100931 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100932 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100933}
934
Denys Vlasenko5318f812018-12-05 17:48:01 +0100935static void bc_verror_msg(const char *fmt, va_list p)
936{
Denys Vlasenko82269122018-12-14 16:30:56 +0100937 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100938 if (G.prog.file) {
939 sv = applet_name;
940 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
941 }
942 bb_verror_msg(fmt, p, NULL);
943 if (G.prog.file) {
944 free((char*)applet_name);
945 applet_name = sv;
946 }
947}
948
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100949static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100950{
951 va_list p;
952
953 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100954 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100955 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100956
Denys Vlasenkoec603182018-12-17 10:34:02 +0100957 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
958 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
959 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100960}
961
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100962#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100963static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100964{
965 va_list p;
966
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100967 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100968 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100969 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100970
971 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100972 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100973 va_end(p);
974
975 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100976 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100977 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenkoec603182018-12-17 10:34:02 +0100978 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
979 return BC_STATUS_FAILURE;
980 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100981}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100982#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100983
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100984// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
985// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100986// function must not have caller-cleaned parameters on stack.
987// Unfortunately, vararg function API does exactly that on most arches.
988// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100989static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100990{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100991 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100992}
993static ERRORFUNC int bc_error_bad_character(char c)
994{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100995 if (!c)
996 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100997 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100998}
999static ERRORFUNC int bc_error_bad_expression(void)
1000{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001001 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001002}
1003static ERRORFUNC int bc_error_bad_token(void)
1004{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001005 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1008{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
1011static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1012{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001013 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001014}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001015#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001016static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001017{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001018 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001019}
Denys Vlasenko00646792018-12-05 18:12:27 +01001020static int bc_POSIX_does_not_allow(const char *msg)
1021{
1022 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1023}
1024static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1025{
1026 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1027}
1028static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1029{
1030 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1031}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001032#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001033
Gavin Howard01055ba2018-11-03 11:00:21 -06001034static void bc_vec_grow(BcVec *v, size_t n)
1035{
1036 size_t cap = v->cap * 2;
1037 while (cap < v->len + n) cap *= 2;
1038 v->v = xrealloc(v->v, v->size * cap);
1039 v->cap = cap;
1040}
1041
1042static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1043{
1044 v->size = esize;
1045 v->cap = BC_VEC_START_CAP;
1046 v->len = 0;
1047 v->dtor = dtor;
1048 v->v = xmalloc(esize * BC_VEC_START_CAP);
1049}
1050
Denys Vlasenko7d628012018-12-04 21:46:47 +01001051static void bc_char_vec_init(BcVec *v)
1052{
1053 bc_vec_init(v, sizeof(char), NULL);
1054}
1055
Gavin Howard01055ba2018-11-03 11:00:21 -06001056static void bc_vec_expand(BcVec *v, size_t req)
1057{
1058 if (v->cap < req) {
1059 v->v = xrealloc(v->v, v->size * req);
1060 v->cap = req;
1061 }
1062}
1063
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001064static void bc_vec_pop(BcVec *v)
1065{
1066 v->len--;
1067 if (v->dtor)
1068 v->dtor(v->v + (v->size * v->len));
1069}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001070
Gavin Howard01055ba2018-11-03 11:00:21 -06001071static void bc_vec_npop(BcVec *v, size_t n)
1072{
1073 if (!v->dtor)
1074 v->len -= n;
1075 else {
1076 size_t len = v->len - n;
1077 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1078 }
1079}
1080
Denys Vlasenko7d628012018-12-04 21:46:47 +01001081static void bc_vec_pop_all(BcVec *v)
1082{
1083 bc_vec_npop(v, v->len);
1084}
1085
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001086static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001087{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001088 size_t len = v->len;
1089 if (len >= v->cap) bc_vec_grow(v, 1);
1090 memmove(v->v + (v->size * len), data, v->size);
1091 v->len++;
1092 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001093}
1094
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001095// G.prog.results often needs "pop old operand, push result" idiom.
1096// Can do this without a few extra ops
1097static size_t bc_result_pop_and_push(const void *data)
1098{
1099 BcVec *v = &G.prog.results;
1100 char *last;
1101 size_t len = v->len - 1;
1102
1103 last = v->v + (v->size * len);
1104 if (v->dtor)
1105 v->dtor(last);
1106 memmove(last, data, v->size);
1107 return len;
1108}
1109
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001110static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001111{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001112 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001113}
1114
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001115static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001116{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001117 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001118 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001119 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001120}
1121
Gavin Howard01055ba2018-11-03 11:00:21 -06001122static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1123{
1124 if (idx == v->len)
1125 bc_vec_push(v, data);
1126 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001127 char *ptr;
1128
1129 if (v->len == v->cap) bc_vec_grow(v, 1);
1130
1131 ptr = v->v + v->size * idx;
1132
1133 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1134 memmove(ptr, data, v->size);
1135 }
1136}
1137
1138static void bc_vec_string(BcVec *v, size_t len, const char *str)
1139{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001140 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001141 bc_vec_expand(v, len + 1);
1142 memcpy(v->v, str, len);
1143 v->len = len;
1144
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001145 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001146}
1147
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001148#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001149static void bc_vec_concat(BcVec *v, const char *str)
1150{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001151 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001152
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001153 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001154
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001155 slen = strlen(str);
1156 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001157
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001158 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001159 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001160
1161 v->len = len;
1162}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001163#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001164
1165static void *bc_vec_item(const BcVec *v, size_t idx)
1166{
1167 return v->v + v->size * idx;
1168}
1169
1170static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1171{
1172 return v->v + v->size * (v->len - idx - 1);
1173}
1174
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001175static void *bc_vec_top(const BcVec *v)
1176{
1177 return v->v + v->size * (v->len - 1);
1178}
1179
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001180static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001181{
1182 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001183 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001184 free(v->v);
1185}
1186
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001187static BcFunc* bc_program_func(size_t idx)
1188{
1189 return bc_vec_item(&G.prog.fns, idx);
1190}
1191// BC_PROG_MAIN is zeroth element, so:
1192#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1193
1194#if ENABLE_BC
1195static BcFunc* bc_program_current_func(void)
1196{
1197 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1198 BcFunc *func = bc_program_func(ip->func);
1199 return func;
1200}
1201#endif
1202
1203static char** bc_program_str(size_t idx)
1204{
1205#if ENABLE_BC
1206 if (IS_BC) {
1207 BcFunc *func = bc_program_current_func();
1208 return bc_vec_item(&func->strs, idx);
1209 }
1210#endif
1211 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1212}
1213
1214static char** bc_program_const(size_t idx)
1215{
1216#if ENABLE_BC
1217 if (IS_BC) {
1218 BcFunc *func = bc_program_current_func();
1219 return bc_vec_item(&func->consts, idx);
1220 }
1221#endif
1222 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1223}
1224
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001225static int bc_id_cmp(const void *e1, const void *e2)
1226{
1227 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1228}
1229
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001230static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001231{
1232 free(((BcId *) id)->name);
1233}
1234
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001235static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001236{
1237 size_t low = 0, high = v->len;
1238
1239 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001240 size_t mid = (low + high) / 2;
1241 BcId *id = bc_vec_item(v, mid);
1242 int result = bc_id_cmp(ptr, id);
1243
1244 if (result == 0)
1245 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001246 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001247 high = mid;
1248 else
1249 low = mid + 1;
1250 }
1251
1252 return low;
1253}
1254
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001255static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001256{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001257 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001258
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001259 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001260 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001261 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1262 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001263 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001264 bc_vec_pushAt(v, ptr, n);
1265 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001266}
1267
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001268#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001269static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001270{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001271 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001272 if (i >= v->len) return BC_VEC_INVALID_IDX;
1273 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1274}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001275#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001276
Gavin Howard01055ba2018-11-03 11:00:21 -06001277static void bc_num_setToZero(BcNum *n, size_t scale)
1278{
1279 n->len = 0;
1280 n->neg = false;
1281 n->rdx = scale;
1282}
1283
1284static void bc_num_zero(BcNum *n)
1285{
1286 bc_num_setToZero(n, 0);
1287}
1288
1289static void bc_num_one(BcNum *n)
1290{
1291 bc_num_setToZero(n, 0);
1292 n->len = 1;
1293 n->num[0] = 1;
1294}
1295
Denys Vlasenko3129f702018-12-09 12:04:44 +01001296// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001297static void bc_num_init(BcNum *n, size_t req)
1298{
1299 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001300 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001301 n->num = xmalloc(req);
1302 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001303 n->rdx = 0;
1304 n->len = 0;
1305 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001306}
1307
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001308static void bc_num_init_DEF_SIZE(BcNum *n)
1309{
1310 bc_num_init(n, BC_NUM_DEF_SIZE);
1311}
1312
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001313static void bc_num_expand(BcNum *n, size_t req)
1314{
1315 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1316 if (req > n->cap) {
1317 n->num = xrealloc(n->num, req);
1318 n->cap = req;
1319 }
1320}
1321
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001322static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001323{
1324 free(((BcNum *) num)->num);
1325}
1326
1327static void bc_num_copy(BcNum *d, BcNum *s)
1328{
1329 if (d != s) {
1330 bc_num_expand(d, s->cap);
1331 d->len = s->len;
1332 d->neg = s->neg;
1333 d->rdx = s->rdx;
1334 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1335 }
1336}
1337
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001338static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001339{
1340 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001341 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001342
Denys Vlasenko1557b762018-12-22 21:37:46 +01001343 result = 0;
1344 i = n->len;
1345 while (i > n->rdx) {
1346 unsigned long prev = result;
1347 result = result * 10 + n->num[--i];
1348 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1349 // 0x1ff00000 * 10 is 0x13f600000,
1350 // or 0x3f600000 truncated to 32 bits. Which is larger.
1351 // However, (N*10)/8 < N check is always correct.
1352 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001353 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001354 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001355 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001356
Denys Vlasenko29301232018-12-11 15:29:32 +01001357 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001358}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001359#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1360
1361static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1362{
1363 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1364
1365 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1366}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001367#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001368
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001369#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1370# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1371#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1372# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1373#endif
1374// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1375// would not hit realloc() code path - not good if num[] is not malloced
1376
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001377static void bc_num_ulong2num(BcNum *n, unsigned long val)
1378{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001379 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001380
1381 bc_num_zero(n);
1382
1383 if (val == 0) return;
1384
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001385 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001386
1387 ptr = n->num;
1388 for (;;) {
1389 n->len++;
1390 *ptr++ = val % 10;
1391 val /= 10;
1392 if (val == 0) break;
1393 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001394}
1395
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001396static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001397{
1398 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001399 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001400 a[i] -= b[i];
1401 for (j = i; a[j] < 0;) {
1402 a[j++] += 10;
1403 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001404 }
1405 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001406}
1407
1408static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1409{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001410 size_t i = len;
1411 for (;;) {
1412 int c;
1413 if (i == 0)
1414 return 0;
1415 i--;
1416 c = a[i] - b[i];
1417 if (c != 0) {
1418 i++;
1419 if (c < 0)
1420 return -i;
1421 return i;
1422 }
1423 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001424}
1425
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001426#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1427#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1428#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1429//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1430static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1431{
1432 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1433}
1434//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1435static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1436{
1437 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1438}
1439
Gavin Howard01055ba2018-11-03 11:00:21 -06001440static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1441{
1442 size_t i, min, a_int, b_int, diff;
1443 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001444 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001445 ssize_t cmp;
1446
1447 if (a == b) return 0;
1448 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1449 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001450
1451 if (a->neg != b->neg) // signs of a and b differ
1452 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1453 return (int)b->neg - (int)a->neg;
1454 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001455
1456 a_int = BC_NUM_INT(a);
1457 b_int = BC_NUM_INT(b);
1458 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001459
1460 if (a_int != 0) return (ssize_t) a_int;
1461
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001462 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001463 if (a_max) {
1464 min = b->rdx;
1465 diff = a->rdx - b->rdx;
1466 max_num = a->num + diff;
1467 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001468 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1469 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001470 min = a->rdx;
1471 diff = b->rdx - a->rdx;
1472 max_num = b->num + diff;
1473 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001474 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001475 }
1476
1477 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001478 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001479
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001480 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001481 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001482 }
1483
1484 return 0;
1485}
1486
1487static void bc_num_truncate(BcNum *n, size_t places)
1488{
1489 if (places == 0) return;
1490
1491 n->rdx -= places;
1492
1493 if (n->len != 0) {
1494 n->len -= places;
1495 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1496 }
1497}
1498
1499static void bc_num_extend(BcNum *n, size_t places)
1500{
1501 size_t len = n->len + places;
1502
1503 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001504 if (n->cap < len) bc_num_expand(n, len);
1505
1506 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1507 memset(n->num, 0, sizeof(BcDig) * places);
1508
1509 n->len += places;
1510 n->rdx += places;
1511 }
1512}
1513
1514static void bc_num_clean(BcNum *n)
1515{
1516 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1517 if (n->len == 0)
1518 n->neg = false;
1519 else if (n->len < n->rdx)
1520 n->len = n->rdx;
1521}
1522
1523static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1524{
1525 if (n->rdx < scale)
1526 bc_num_extend(n, scale - n->rdx);
1527 else
1528 bc_num_truncate(n, n->rdx - scale);
1529
1530 bc_num_clean(n);
1531 if (n->len != 0) n->neg = !neg1 != !neg2;
1532}
1533
1534static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1535 BcNum *restrict b)
1536{
1537 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001538 b->len = n->len - idx;
1539 a->len = idx;
1540 a->rdx = b->rdx = 0;
1541
1542 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1543 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001544 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001545 bc_num_zero(b);
1546 bc_num_copy(a, n);
1547 }
1548
1549 bc_num_clean(a);
1550 bc_num_clean(b);
1551}
1552
Denys Vlasenko29301232018-12-11 15:29:32 +01001553static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001554{
Denys Vlasenko29301232018-12-11 15:29:32 +01001555 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001556
1557 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1558 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1559 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001560 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001561 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001562
1563 if (n->rdx >= places)
1564 n->rdx -= places;
1565 else {
1566 bc_num_extend(n, places - n->rdx);
1567 n->rdx = 0;
1568 }
1569
1570 bc_num_clean(n);
1571
Denys Vlasenko29301232018-12-11 15:29:32 +01001572 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001573}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001574#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001575
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001576typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1577
1578static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1579 BcNumBinaryOp op, size_t req)
1580{
1581 BcStatus s;
1582 BcNum num2, *ptr_a, *ptr_b;
1583 bool init = false;
1584
1585 if (c == a) {
1586 ptr_a = &num2;
1587 memcpy(ptr_a, c, sizeof(BcNum));
1588 init = true;
1589 } else
1590 ptr_a = a;
1591
1592 if (c == b) {
1593 ptr_b = &num2;
1594 if (c != a) {
1595 memcpy(ptr_b, c, sizeof(BcNum));
1596 init = true;
1597 }
1598 } else
1599 ptr_b = b;
1600
1601 if (init)
1602 bc_num_init(c, req);
1603 else
1604 bc_num_expand(c, req);
1605
1606 s = BC_STATUS_SUCCESS;
1607 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1608
1609 if (init) bc_num_free(&num2);
1610
1611 RETURN_STATUS(s);
1612}
1613#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1614
1615static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1616static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1617static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1618static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1619static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1620static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1621
1622static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1623{
1624 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1625 (void) scale;
1626 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1627}
1628
1629static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1630{
1631 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1632 (void) scale;
1633 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1634}
1635
1636static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1637{
1638 size_t req = BC_NUM_MREQ(a, b, scale);
1639 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1640}
1641
1642static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1643{
1644 size_t req = BC_NUM_MREQ(a, b, scale);
1645 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1646}
1647
1648static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1649{
1650 size_t req = BC_NUM_MREQ(a, b, scale);
1651 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1652}
1653
1654static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1655{
1656 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1657}
1658
1659static const BcNumBinaryOp zbc_program_ops[] = {
1660 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1661};
1662#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1663#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1664#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1665#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1666#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1667#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1668
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001669static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001670{
1671 BcNum one;
1672 BcDig num[2];
1673
1674 one.cap = 2;
1675 one.num = num;
1676 bc_num_one(&one);
1677
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001678 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001679}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001680#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001681
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001682static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001683{
1684 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1685 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001686 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001687
1688 // Because this function doesn't need to use scale (per the bc spec),
1689 // I am hijacking it to say whether it's doing an add or a subtract.
1690
1691 if (a->len == 0) {
1692 bc_num_copy(c, b);
1693 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001694 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001695 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001696 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001697 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001698 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001699 }
1700
1701 c->neg = a->neg;
1702 c->rdx = BC_MAX(a->rdx, b->rdx);
1703 min_rdx = BC_MIN(a->rdx, b->rdx);
1704 c->len = 0;
1705
1706 if (a->rdx > b->rdx) {
1707 diff = a->rdx - b->rdx;
1708 ptr = a->num;
1709 ptr_a = a->num + diff;
1710 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001711 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001712 diff = b->rdx - a->rdx;
1713 ptr = b->num;
1714 ptr_a = a->num;
1715 ptr_b = b->num + diff;
1716 }
1717
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001718 ptr_c = c->num;
1719 for (i = 0; i < diff; ++i, ++c->len)
1720 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001721
1722 ptr_c += diff;
1723 a_int = BC_NUM_INT(a);
1724 b_int = BC_NUM_INT(b);
1725
1726 if (a_int > b_int) {
1727 min_int = b_int;
1728 max = a_int;
1729 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001730 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001731 min_int = a_int;
1732 max = b_int;
1733 ptr = ptr_b;
1734 }
1735
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001736 carry = 0;
1737 for (i = 0; i < min_rdx + min_int; ++i) {
1738 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001739 carry = in / 10;
1740 ptr_c[i] = (BcDig)(in % 10);
1741 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001742 for (; i < max + min_rdx; ++i) {
1743 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001744 carry = in / 10;
1745 ptr_c[i] = (BcDig)(in % 10);
1746 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001747 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001748
1749 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1750
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001751 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001752}
1753
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001754static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001755{
Gavin Howard01055ba2018-11-03 11:00:21 -06001756 ssize_t cmp;
1757 BcNum *minuend, *subtrahend;
1758 size_t start;
1759 bool aneg, bneg, neg;
1760
1761 // Because this function doesn't need to use scale (per the bc spec),
1762 // I am hijacking it to say whether it's doing an add or a subtract.
1763
1764 if (a->len == 0) {
1765 bc_num_copy(c, b);
1766 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001767 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001768 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001769 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001770 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001771 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001772 }
1773
1774 aneg = a->neg;
1775 bneg = b->neg;
1776 a->neg = b->neg = false;
1777
1778 cmp = bc_num_cmp(a, b);
1779
1780 a->neg = aneg;
1781 b->neg = bneg;
1782
1783 if (cmp == 0) {
1784 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001785 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001786 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001787 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001788 neg = a->neg;
1789 minuend = a;
1790 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001791 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001792 neg = b->neg;
1793 if (sub) neg = !neg;
1794 minuend = b;
1795 subtrahend = a;
1796 }
1797
1798 bc_num_copy(c, minuend);
1799 c->neg = neg;
1800
1801 if (c->rdx < subtrahend->rdx) {
1802 bc_num_extend(c, subtrahend->rdx - c->rdx);
1803 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001804 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001805 start = c->rdx - subtrahend->rdx;
1806
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001807 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001808
1809 bc_num_clean(c);
1810
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001811 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001812}
1813
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001814static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001815 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001816#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001817{
1818 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001819 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001820 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001821 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001822
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 if (a->len == 0 || b->len == 0) {
1824 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001825 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001826 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001827 aone = BC_NUM_ONE(a);
1828 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001829 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001830 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 }
1832
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001833 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1834 || a->len < BC_NUM_KARATSUBA_LEN
1835 || b->len < BC_NUM_KARATSUBA_LEN
1836 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001837 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001838
Gavin Howard01055ba2018-11-03 11:00:21 -06001839 bc_num_expand(c, a->len + b->len + 1);
1840
1841 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001842 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001843
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001844 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001845 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001846 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001847 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001848 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001849 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001850 carry = in / 10;
1851 c->num[i + j] = (BcDig)(in % 10);
1852 }
1853
1854 c->num[i + j] += (BcDig) carry;
1855 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001856
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001857#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001858 // a=2^1000000
1859 // a*a <- without check below, this will not be interruptible
1860 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001861#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001862 }
1863
1864 c->len = len;
1865
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001866 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 }
1868
1869 bc_num_init(&l1, max);
1870 bc_num_init(&h1, max);
1871 bc_num_init(&l2, max);
1872 bc_num_init(&h2, max);
1873 bc_num_init(&m1, max);
1874 bc_num_init(&m2, max);
1875 bc_num_init(&z0, max);
1876 bc_num_init(&z1, max);
1877 bc_num_init(&z2, max);
1878 bc_num_init(&temp, max + max);
1879
1880 bc_num_split(a, max2, &l1, &h1);
1881 bc_num_split(b, max2, &l2, &h2);
1882
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001883 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001884 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001885 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 if (s) goto err;
1887
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001888 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001889 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001890 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001892 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 if (s) goto err;
1894
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001895 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001896 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001897 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 if (s) goto err;
1899
Denys Vlasenko29301232018-12-11 15:29:32 +01001900 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001901 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001902 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001903 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001904 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001905 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001906 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001907 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001908 bc_num_free(&temp);
1909 bc_num_free(&z2);
1910 bc_num_free(&z1);
1911 bc_num_free(&z0);
1912 bc_num_free(&m2);
1913 bc_num_free(&m1);
1914 bc_num_free(&h2);
1915 bc_num_free(&l2);
1916 bc_num_free(&h1);
1917 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001918 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001919}
1920
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001921static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001922{
1923 BcStatus s;
1924 BcNum cpa, cpb;
1925 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1926
1927 scale = BC_MAX(scale, a->rdx);
1928 scale = BC_MAX(scale, b->rdx);
1929 scale = BC_MIN(a->rdx + b->rdx, scale);
1930 maxrdx = BC_MAX(maxrdx, scale);
1931
1932 bc_num_init(&cpa, a->len);
1933 bc_num_init(&cpb, b->len);
1934
1935 bc_num_copy(&cpa, a);
1936 bc_num_copy(&cpb, b);
1937 cpa.neg = cpb.neg = false;
1938
Denys Vlasenko29301232018-12-11 15:29:32 +01001939 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001940 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001941 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001942 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001943 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001944 if (s) goto err;
1945
1946 maxrdx += scale;
1947 bc_num_expand(c, c->len + maxrdx);
1948
1949 if (c->len < maxrdx) {
1950 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1951 c->len += maxrdx;
1952 }
1953
1954 c->rdx = maxrdx;
1955 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001956 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001957 bc_num_free(&cpb);
1958 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001959 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001960}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001961#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001962
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001963static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001964{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001965 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001966 size_t len, end, i;
1967 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001968
1969 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001970 RETURN_STATUS(bc_error("divide by zero"));
1971 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001972 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001973 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001974 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001975 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001976 bc_num_copy(c, a);
1977 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001978 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001979 }
1980
1981 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1982 bc_num_copy(&cp, a);
1983 len = b->len;
1984
1985 if (len > cp.len) {
1986 bc_num_expand(&cp, len + 2);
1987 bc_num_extend(&cp, len - cp.len);
1988 }
1989
1990 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1991 cp.rdx -= b->rdx;
1992 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1993
1994 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001995 for (;;) {
1996 if (len == 0) break;
1997 len--;
1998 if (b->num[len] != 0)
1999 break;
2000 }
2001 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002002 }
2003
2004 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2005
2006 // We want an extra zero in front to make things simpler.
2007 cp.num[cp.len++] = 0;
2008 end = cp.len - len;
2009
2010 bc_num_expand(c, cp.len);
2011
2012 bc_num_zero(c);
2013 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2014 c->rdx = cp.rdx;
2015 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002016
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002017 s = BC_STATUS_SUCCESS;
2018 for (i = end - 1; i < end; --i) {
2019 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002020 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002021 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2022 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002023 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002024#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002025 // a=2^100000
2026 // scale=40000
2027 // 1/a <- without check below, this will not be interruptible
2028 if (G_interrupt) {
2029 s = BC_STATUS_FAILURE;
2030 break;
2031 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002032#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002033 }
2034
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002035 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002036 bc_num_free(&cp);
2037
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002038 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002039}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002040#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002041
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002042static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002043 BcNum *restrict d, size_t scale, size_t ts)
2044{
2045 BcStatus s;
2046 BcNum temp;
2047 bool neg;
2048
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002049 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002050 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002051
2052 if (a->len == 0) {
2053 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002054 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002055 }
2056
2057 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002058 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002059 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002060
2061 if (scale != 0) scale = ts;
2062
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002063 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002064 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002065 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002066 if (s) goto err;
2067
2068 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2069
2070 neg = d->neg;
2071 bc_num_retireMul(d, ts, a->neg, b->neg);
2072 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002073 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002074 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002075 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002076}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002077#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002078
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002079static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002080{
2081 BcStatus s;
2082 BcNum c1;
2083 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2084
2085 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002086 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002087 bc_num_free(&c1);
2088
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002089 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002090}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002091#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002092
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002094{
2095 BcStatus s = BC_STATUS_SUCCESS;
2096 BcNum copy;
2097 unsigned long pow;
2098 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002099 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002100
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002101 // GNU bc does not allow 2^2.0 - we do
2102 for (i = 0; i < b->rdx; i++)
2103 if (b->num[i] != 0)
2104 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002105
2106 if (b->len == 0) {
2107 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002108 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002109 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002110 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002111 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002112 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002113 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002114 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 if (!b->neg)
2116 bc_num_copy(c, a);
2117 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002118 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002119 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 }
2121
2122 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002123 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002124 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002125 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002126
2127 bc_num_init(&copy, a->len);
2128 bc_num_copy(&copy, a);
2129
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002130 if (!neg) {
2131 if (a->rdx > scale)
2132 scale = a->rdx;
2133 if (a->rdx * pow < scale)
2134 scale = a->rdx * pow;
2135 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002136
Gavin Howard01055ba2018-11-03 11:00:21 -06002137
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002138 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002139 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002140 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002142 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002143 //if (G_interrupt) {
2144 // s = BC_STATUS_FAILURE;
2145 // goto err;
2146 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 }
2148
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 bc_num_copy(c, &copy);
2150
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002151 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002153 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002154 if (s) goto err;
2155
2156 if (pow & 1) {
2157 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002158 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 if (s) goto err;
2160 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002161 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002162 //if (G_interrupt) {
2163 // s = BC_STATUS_FAILURE;
2164 // goto err;
2165 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 }
2167
2168 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002169 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002170 if (s) goto err;
2171 }
2172
Gavin Howard01055ba2018-11-03 11:00:21 -06002173 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2174
2175 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002176 for (i = 0; i < c->len; ++i)
2177 if (c->num[i] != 0)
2178 goto skip;
2179 bc_num_setToZero(c, scale);
2180 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002181
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002182 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002183 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002184 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002185}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002186#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002187
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002188static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002189{
2190 BcStatus s;
2191 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2192 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2193 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2194
2195 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2196 bc_num_expand(b, req);
2197
2198 if (a->len == 0) {
2199 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002200 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002201 }
2202 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002203 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002204 }
2205 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002206 bc_num_one(b);
2207 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002208 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002209 }
2210
2211 scale = BC_MAX(scale, a->rdx) + 1;
2212 len = a->len + scale;
2213
2214 bc_num_init(&num1, len);
2215 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002216 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002217
2218 bc_num_one(&half);
2219 half.num[0] = 5;
2220 half.rdx = 1;
2221
2222 bc_num_init(&f, len);
2223 bc_num_init(&fprime, len);
2224
2225 x0 = &num1;
2226 x1 = &num2;
2227
2228 bc_num_one(x0);
2229 pow = BC_NUM_INT(a);
2230
2231 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002232 if (pow & 1)
2233 x0->num[0] = 2;
2234 else
2235 x0->num[0] = 6;
2236
2237 pow -= 2 - (pow & 1);
2238
2239 bc_num_extend(x0, pow);
2240
2241 // Make sure to move the radix back.
2242 x0->rdx -= pow;
2243 }
2244
2245 x0->rdx = digs = digs1 = 0;
2246 resrdx = scale + 2;
2247 len = BC_NUM_INT(x0) + resrdx - 1;
2248
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002249 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002250 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002251 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002252 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002253 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002254 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002255 if (s) goto err;
2256
2257 cmp = bc_num_cmp(x1, x0);
2258 digs = x1->len - (unsigned long long) llabs(cmp);
2259
2260 if (cmp == cmp2 && digs == digs1)
2261 times += 1;
2262 else
2263 times = 0;
2264
2265 resrdx += times > 4;
2266
2267 cmp2 = cmp1;
2268 cmp1 = cmp;
2269 digs1 = digs;
2270
2271 temp = x0;
2272 x0 = x1;
2273 x1 = temp;
2274 }
2275
Gavin Howard01055ba2018-11-03 11:00:21 -06002276 bc_num_copy(b, x0);
2277 scale -= 1;
2278 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002279 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002280 bc_num_free(&fprime);
2281 bc_num_free(&f);
2282 bc_num_free(&half);
2283 bc_num_free(&num2);
2284 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002285 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002286}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002287#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002288
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002289static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002290 size_t scale)
2291{
2292 BcStatus s;
2293 BcNum num2, *ptr_a;
2294 bool init = false;
2295 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2296
2297 if (c == a) {
2298 memcpy(&num2, c, sizeof(BcNum));
2299 ptr_a = &num2;
2300 bc_num_init(c, len);
2301 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002302 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002303 ptr_a = a;
2304 bc_num_expand(c, len);
2305 }
2306
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002307 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002308
2309 if (init) bc_num_free(&num2);
2310
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002311 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002312}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002313#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002314
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002315#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002316static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002317{
2318 BcStatus s;
2319 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002320 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002321
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002322 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002323 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002324 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002325 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002326 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002327 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002328
2329 bc_num_expand(d, c->len);
2330 bc_num_init(&base, c->len);
2331 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002332 bc_num_init(&temp, b->len);
2333
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002334 two.cap = ARRAY_SIZE(two_digs);
2335 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002336 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002337 two_digs[0] = 2;
2338
Gavin Howard01055ba2018-11-03 11:00:21 -06002339 bc_num_one(d);
2340
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002341 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002342 if (s) goto err;
2343 bc_num_copy(&exp, b);
2344
2345 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002346 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002347 if (s) goto err;
2348
2349 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002350 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002351 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002352 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002353 if (s) goto err;
2354 }
2355
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002356 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002357 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002358 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359 if (s) goto err;
2360 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002361 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002362 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002363 bc_num_free(&exp);
2364 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002365 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002366}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002367#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002368#endif // ENABLE_DC
2369
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002370static FAST_FUNC void bc_string_free(void *string)
2371{
2372 free(*(char**)string);
2373}
2374
Gavin Howard01055ba2018-11-03 11:00:21 -06002375static void bc_func_init(BcFunc *f)
2376{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002377 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002378 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2379 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002380 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2381 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002382 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002383}
2384
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002385static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002386{
2387 BcFunc *f = (BcFunc *) func;
2388 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002389 IF_BC(bc_vec_free(&f->labels);)
2390 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002391 IF_BC(bc_vec_free(&f->strs);)
2392 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002393}
2394
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002395static void bc_array_expand(BcVec *a, size_t len);
2396
Gavin Howard01055ba2018-11-03 11:00:21 -06002397static void bc_array_init(BcVec *a, bool nums)
2398{
2399 if (nums)
2400 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2401 else
2402 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2403 bc_array_expand(a, 1);
2404}
2405
Gavin Howard01055ba2018-11-03 11:00:21 -06002406static void bc_array_expand(BcVec *a, size_t len)
2407{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002408 if (a->dtor == bc_num_free
2409 // && a->size == sizeof(BcNum) - always true
2410 ) {
2411 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002412 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002413 bc_num_init_DEF_SIZE(&n);
2414 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002415 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002416 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002417 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002418 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002419 bc_array_init(&v, true);
2420 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002421 }
2422 }
2423}
2424
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002425static void bc_array_copy(BcVec *d, const BcVec *s)
2426{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002427 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002428 size_t i;
2429
2430 bc_vec_pop_all(d);
2431 bc_vec_expand(d, s->cap);
2432 d->len = s->len;
2433
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002434 dnum = (void*)d->v;
2435 snum = (void*)s->v;
2436 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002437 bc_num_init(dnum, snum->len);
2438 bc_num_copy(dnum, snum);
2439 }
2440}
2441
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002442#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002443static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002444{
2445 d->t = src->t;
2446
2447 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002448 case BC_RESULT_TEMP:
2449 case BC_RESULT_IBASE:
2450 case BC_RESULT_SCALE:
2451 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002452 bc_num_init(&d->d.n, src->d.n.len);
2453 bc_num_copy(&d->d.n, &src->d.n);
2454 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002455 case BC_RESULT_VAR:
2456 case BC_RESULT_ARRAY:
2457 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002458 d->d.id.name = xstrdup(src->d.id.name);
2459 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002460 case BC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002461 IF_BC(case BC_RESULT_LAST:)
Gavin Howard01055ba2018-11-03 11:00:21 -06002462 case BC_RESULT_ONE:
2463 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002464 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2465 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002466 }
2467}
2468#endif // ENABLE_DC
2469
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002470static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002471{
2472 BcResult *r = (BcResult *) result;
2473
2474 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002475 case BC_RESULT_TEMP:
2476 case BC_RESULT_IBASE:
2477 case BC_RESULT_SCALE:
2478 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002479 bc_num_free(&r->d.n);
2480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002481 case BC_RESULT_VAR:
2482 case BC_RESULT_ARRAY:
2483 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002484 free(r->d.id.name);
2485 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002486 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002487 // Do nothing.
2488 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002489 }
2490}
2491
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002492static int bad_input_byte(char c)
2493{
2494 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2495 || c > 0x7e
2496 ) {
2497 bc_error_fmt("illegal character 0x%02x", c);
2498 return 1;
2499 }
2500 return 0;
2501}
2502
2503// Note: it _appends_ data from fp to vec.
2504static void bc_read_line(BcVec *vec, FILE *fp)
2505{
2506 again:
2507 fflush_and_check();
2508
2509#if ENABLE_FEATURE_BC_SIGNALS
2510 if (G_interrupt) { // ^C was pressed
2511 intr:
2512 if (fp != stdin) {
2513 // ^C while running a script (bc SCRIPT): die.
2514 // We do not return to interactive prompt:
2515 // user might be running us from a shell,
2516 // and SCRIPT might be intended to terminate
2517 // (e.g. contain a "halt" stmt).
2518 // ^C dropping user into a bc prompt instead of
2519 // the shell would be unexpected.
2520 xfunc_die();
2521 }
2522 // ^C while interactive input
2523 G_interrupt = 0;
2524 // GNU bc says "interrupted execution."
2525 // GNU dc says "Interrupt!"
2526 fputs("\ninterrupted execution\n", stderr);
2527 }
2528
2529# if ENABLE_FEATURE_EDITING
2530 if (G_ttyin && fp == stdin) {
2531 int n, i;
2532# define line_buf bb_common_bufsiz1
2533 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2534 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2535 if (n == 0) // ^C
2536 goto intr;
2537 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2538 return;
2539 }
2540 i = 0;
2541 for (;;) {
2542 char c = line_buf[i++];
2543 if (!c) break;
2544 if (bad_input_byte(c)) goto again;
2545 }
2546 bc_vec_concat(vec, line_buf);
2547# undef line_buf
2548 } else
2549# endif
2550#endif
2551 {
2552 int c;
2553 bool bad_chars = 0;
2554 size_t len = vec->len;
2555
2556 do {
2557#if ENABLE_FEATURE_BC_SIGNALS
2558 if (G_interrupt) {
2559 // ^C was pressed: ignore entire line, get another one
2560 vec->len = len;
2561 goto intr;
2562 }
2563#endif
2564 do c = fgetc(fp); while (c == '\0');
2565 if (c == EOF) {
2566 if (ferror(fp))
2567 bb_perror_msg_and_die("input error");
2568 // Note: EOF does not append '\n'
2569 break;
2570 }
2571 bad_chars |= bad_input_byte(c);
2572 bc_vec_pushByte(vec, (char)c);
2573 } while (c != '\n');
2574
2575 if (bad_chars) {
2576 // Bad chars on this line
2577 if (!G.prog.file) { // stdin
2578 // ignore entire line, get another one
2579 vec->len = len;
2580 goto again;
2581 }
2582 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
2583 }
2584 bc_vec_pushZeroByte(vec);
2585 }
2586}
2587
2588//
2589// Parsing routines
2590//
2591
2592static bool bc_num_strValid(const char *val, size_t base)
2593{
2594 BcDig b;
2595 bool radix;
2596
2597 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2598 radix = false;
2599 for (;;) {
2600 BcDig c = *val++;
2601 if (c == '\0')
2602 break;
2603 if (c == '.') {
2604 if (radix) return false;
2605 radix = true;
2606 continue;
2607 }
2608 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2609 return false;
2610 }
2611 return true;
2612}
2613
2614// Note: n is already "bc_num_zero()"ed,
2615// leading zeroes in "val" are removed
2616static void bc_num_parseDecimal(BcNum *n, const char *val)
2617{
2618 size_t len, i;
2619 const char *ptr;
2620
2621 len = strlen(val);
2622 if (len == 0)
2623 return;
2624
2625 bc_num_expand(n, len);
2626
2627 ptr = strchr(val, '.');
2628
2629 n->rdx = 0;
2630 if (ptr != NULL)
2631 n->rdx = (size_t)((val + len) - (ptr + 1));
2632
2633 for (i = 0; val[i]; ++i) {
2634 if (val[i] != '0' && val[i] != '.') {
2635 // Not entirely zero value - convert it, and exit
2636 i = len - 1;
2637 for (;;) {
2638 n->num[n->len] = val[i] - '0';
2639 ++n->len;
2640 skip_dot:
2641 if (i == 0) break;
2642 if (val[--i] == '.') goto skip_dot;
2643 }
2644 break;
2645 }
2646 }
2647 // if for() exits without hitting if(), the value is entirely zero
2648}
2649
2650// Note: n is already "bc_num_zero()"ed,
2651// leading zeroes in "val" are removed
2652static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2653{
2654 BcStatus s;
2655 BcNum temp, mult, result;
2656 BcNum base;
2657 BcDig base_digs[ULONG_NUM_BUFSIZE];
2658 BcDig c = '\0';
2659 unsigned long v;
2660 size_t i, digits;
2661
2662 for (i = 0; ; ++i) {
2663 if (val[i] == '\0')
2664 return;
2665 if (val[i] != '.' && val[i] != '0')
2666 break;
2667 }
2668
2669 bc_num_init_DEF_SIZE(&temp);
2670 bc_num_init_DEF_SIZE(&mult);
2671 base.cap = ARRAY_SIZE(base_digs);
2672 base.num = base_digs;
2673 bc_num_ulong2num(&base, base_t);
2674
2675 for (;;) {
2676 c = *val++;
2677 if (c == '\0') goto int_err;
2678 if (c == '.') break;
2679
2680 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2681
2682 s = zbc_num_mul(n, &base, &mult, 0);
2683 if (s) goto int_err;
2684 bc_num_ulong2num(&temp, v);
2685 s = zbc_num_add(&mult, &temp, n, 0);
2686 if (s) goto int_err;
2687 }
2688
2689 bc_num_init(&result, base.len);
2690 //bc_num_zero(&result); - already is
2691 bc_num_one(&mult);
2692
2693 digits = 0;
2694 for (;;) {
2695 c = *val++;
2696 if (c == '\0') break;
2697 digits++;
2698
2699 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2700
2701 s = zbc_num_mul(&result, &base, &result, 0);
2702 if (s) goto err;
2703 bc_num_ulong2num(&temp, v);
2704 s = zbc_num_add(&result, &temp, &result, 0);
2705 if (s) goto err;
2706 s = zbc_num_mul(&mult, &base, &mult, 0);
2707 if (s) goto err;
2708 }
2709
2710 s = zbc_num_div(&result, &mult, &result, digits);
2711 if (s) goto err;
2712 s = zbc_num_add(n, &result, n, digits);
2713 if (s) goto err;
2714
2715 if (n->len != 0) {
2716 if (n->rdx < digits)
2717 bc_num_extend(n, digits - n->rdx);
2718 } else
2719 bc_num_zero(n);
2720 err:
2721 bc_num_free(&result);
2722 int_err:
2723 bc_num_free(&mult);
2724 bc_num_free(&temp);
2725}
2726
2727static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2728{
2729 if (!bc_num_strValid(val, base_t))
2730 RETURN_STATUS(bc_error("bad number string"));
2731
2732 bc_num_zero(n);
2733 while (*val == '0') val++;
2734
2735 if (base_t == 10)
2736 bc_num_parseDecimal(n, val);
2737 else
2738 bc_num_parseBase(n, val, base_t);
2739
2740 RETURN_STATUS(BC_STATUS_SUCCESS);
2741}
2742#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2743
Gavin Howard01055ba2018-11-03 11:00:21 -06002744static void bc_lex_lineComment(BcLex *l)
2745{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002746 // Try: echo -n '#foo' | bc
2747 size_t i;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002748 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002749 i = l->i;
2750 while (i < l->len && l->buf[i] != '\n')
2751 i++;
2752 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002753}
2754
2755static void bc_lex_whitespace(BcLex *l)
2756{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002757 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002758 for (;;) {
2759 char c = l->buf[l->i];
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002760 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002761 break;
2762 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002763 break;
2764 l->i++;
2765 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002766}
2767
Denys Vlasenko29301232018-12-11 15:29:32 +01002768static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002769{
2770 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002771 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002772 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002773
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002774 pt = (start == '.');
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002775 l->t.t = XC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002776 ccnt = i = 0;
2777 for (;;) {
2778 char c = buf[i];
2779 if (c == '\0')
2780 break;
2781 if (c == '\\' && buf[i + 1] == '\n') {
2782 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002783 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002784 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002785 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002786 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2787 if (c != '.') break;
2788 // if '.' was already seen, stop on second one:
2789 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002790 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002791 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002792 // buf[i] is one of "0-9A-F."
2793 i++;
2794 if (c != '.')
2795 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002796 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002797 //ccnt is the number of chars in the number string, excluding possible
2798 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2799 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002800 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002801
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002802 // This might overestimate the size, if there are "\<NL>"'s
2803 // in the number. Subtracting number_of_backslashes*2 correctly
2804 // is not that easy: consider that in the case of "NNN.\<NL>"
2805 // loop above will count "\<NL>" before it realizes it is not
2806 // in fact *inside* the number:
2807 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002808
Denys Vlasenko64074a12018-12-07 15:50:14 +01002809 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2810 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2811 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002812 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002813 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002814
Denys Vlasenko7d628012018-12-04 21:46:47 +01002815 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002816 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002817 bc_vec_push(&l->t.v, &start);
2818
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002819 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002820 // If we have hit a backslash, skip it. We don't have
2821 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002822 if (*buf == '\\') {
2823 buf += 2;
2824 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002825 continue;
2826 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002827 bc_vec_push(&l->t.v, buf);
2828 buf++;
2829 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002830 }
2831
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002832 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002833
Denys Vlasenko29301232018-12-11 15:29:32 +01002834 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002835}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002836#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002837
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002838static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002839{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002840 size_t i;
2841 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002842
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002843 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06002844
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002845 i = 0;
2846 buf = l->buf + l->i - 1;
2847 for (;;) {
2848 char c = buf[i];
2849 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2850 i++;
2851 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002852
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002853#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002854 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2855 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2856 if (i > BC_MAX_STRING)
2857 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2858 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002859#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002860 bc_vec_string(&l->t.v, i, buf);
2861
2862 // Increment the index. We minus 1 because it has already been incremented.
2863 l->i += i - 1;
2864
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002865 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002866}
2867
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002868static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002869{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002870 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002871}
2872
2873static void bc_lex_free(BcLex *l)
2874{
2875 bc_vec_free(&l->t.v);
2876}
2877
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002878static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002879{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002880 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002881 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002882}
2883
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002884static bool bc_lex_more_input(BcLex *l)
2885{
2886 size_t str;
2887 bool comment;
2888
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002889 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002890
2891 // This loop is complex because the vm tries not to send any lines that end
2892 // with a backslash to the parser. The reason for that is because the parser
2893 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2894 // case, and for strings and comments, the parser will expect more stuff.
2895 comment = false;
2896 str = 0;
2897 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002898 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002899 char *string;
2900
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002901 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002902 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002903 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002904 break;
2905
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002906 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002907 while (*string) {
2908 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002909 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002910 if (IS_BC)
2911 str ^= (c == '"');
2912 else {
2913 if (c == ']')
2914 str -= 1;
2915 else if (c == '[')
2916 str += 1;
2917 }
2918 }
2919 string++;
2920 if (c == '/' && *string == '*') {
2921 comment = true;
2922 string++;
2923 continue;
2924 }
2925 if (c == '*' && *string == '/') {
2926 comment = false;
2927 string++;
2928 }
2929 }
2930 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002931 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002932 continue;
2933 }
2934
2935 // Check for backslash+newline.
2936 // we do not check that last char is '\n' -
2937 // if it is not, then it's EOF, and looping back
2938 // to bc_read_line() will detect it:
2939 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002940 if (string >= G.input_buffer.v && *string == '\\') {
2941 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002942 continue;
2943 }
2944
2945 break;
2946 }
2947
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002948 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002949 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002950// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2951 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002952
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002953 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002954}
2955
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002956IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2957IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002958#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2959#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002960
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002961static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002962{
2963 BcStatus s;
2964
2965 l->t.last = l->t.t;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002966 if (l->t.last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002967
2968 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002969 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002970 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002971
2972 // Loop until failure or we don't have whitespace. This
2973 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002974 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002975 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002976 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002977 if (l->i == l->len) {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002978 l->t.t = XC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002979 if (!G.input_fp)
2980 RETURN_STATUS(BC_STATUS_SUCCESS);
2981 if (!bc_lex_more_input(l)) {
2982 G.input_fp = NULL;
2983 RETURN_STATUS(BC_STATUS_SUCCESS);
2984 }
2985 // here it's guaranteed that l->i is below l->len
2986 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002987 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002988 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2989 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002990 if (IS_BC) {
2991 IF_BC(s = zbc_lex_token(l));
2992 } else {
2993 IF_DC(s = zdc_lex_token(l));
2994 }
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002995 } while (!s && l->t.t == XC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002996 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002997
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002998 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002999}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003000#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003001
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003002#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01003003static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
3004{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003005 if (l->t.t == XC_LEX_NLINE)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003006 RETURN_STATUS(zbc_lex_next(l));
3007 RETURN_STATUS(BC_STATUS_SUCCESS);
3008}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003009#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003010
3011static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
3012{
3013 BcStatus s;
3014 s = zbc_lex_next(l);
3015 if (s) RETURN_STATUS(s);
3016 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
3017 s = zbc_lex_skip_if_at_NLINE(l);
3018 RETURN_STATUS(s);
3019}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003020#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003021#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003022
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003023static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003024{
3025 l->buf = text;
3026 l->i = 0;
3027 l->len = strlen(text);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003028 l->t.t = l->t.last = XC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003029 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003030}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003031#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003032
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003033#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003034static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003035{
3036 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003037 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003038 const char *buf = l->buf + l->i - 1;
3039
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003040 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3041 const char *keyword8 = bc_lex_kws[i].name8;
3042 unsigned j = 0;
3043 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3044 j++;
3045 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003046 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003047 if (keyword8[j] != '\0')
3048 continue;
3049 match:
3050 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003051 if (isalnum(buf[j]) || buf[j]=='_')
3052 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003053 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003054 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003055 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003056 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003057 }
3058
3059 // We minus 1 because the index has already been incremented.
3060 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003061 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003062 }
3063
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003064 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003065 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003066
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003067 if (l->t.v.len > 2) {
3068 // Prevent this:
3069 // >>> qwe=1
3070 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3071 // '
3072 unsigned len = strchrnul(buf, '\n') - buf;
3073 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3074 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003075
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003076 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003077}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003078#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003079
Denys Vlasenko26819db2018-12-12 16:08:46 +01003080static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003081{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003082 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003083
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003084 l->t.t = XC_LEX_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06003085
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003086 nls = 0;
3087 i = l->i;
3088 for (;;) {
3089 char c = l->buf[i];
3090 if (c == '\0') {
3091 l->i = i;
3092 RETURN_STATUS(bc_error("string end could not be found"));
3093 }
3094 if (c == '"')
3095 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003096 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003097 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003098 }
3099
3100 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003101 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3102 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3103 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003104 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003105 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003106 bc_vec_string(&l->t.v, len, l->buf + l->i);
3107
3108 l->i = i + 1;
3109 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003110 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003111
Denys Vlasenko26819db2018-12-12 16:08:46 +01003112 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003113}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003114#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003115
Denys Vlasenko0a238142018-12-14 16:48:34 +01003116static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003117{
3118 if (l->buf[l->i] == '=') {
3119 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003120 with_and_without >>= 8; // store "with" value
3121 } // else store "without" value
3122 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003123}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003124#define bc_lex_assign(l, with, without) \
3125 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003126
Denys Vlasenko29301232018-12-11 15:29:32 +01003127static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003128{
3129 size_t i, nls = 0;
3130 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003131
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003132 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003133 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003134 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003135 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003136 check_star:
3137 if (c == '*') {
3138 c = buf[++i];
3139 if (c == '/')
3140 break;
3141 goto check_star;
3142 }
3143 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003145 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003147 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 }
3149
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003150 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003151 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003152 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003153
Denys Vlasenko29301232018-12-11 15:29:32 +01003154 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003155}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003156#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003157
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003158#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003159static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003160{
3161 BcStatus s = BC_STATUS_SUCCESS;
3162 char c = l->buf[l->i++], c2;
3163
3164 // This is the workhorse of the lexer.
3165 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003166// case '\0': // probably never reached
3167// l->i--;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003168// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003169// l->newline = true;
3170// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003171 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003172 l->t.t = XC_LEX_NLINE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003173 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 case '\t':
3176 case '\v':
3177 case '\f':
3178 case '\r':
3179 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 bc_lex_whitespace(l);
3181 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 case '!':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003183 bc_lex_assign(l, XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003184 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003185 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003186 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003187 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003188 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003189 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003190 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003191 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003192 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003193 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003194 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 case '%':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003198 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 c2 = l->buf[l->i];
3202 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003203 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003204 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 ++l->i;
3206 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003207 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003208 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003209 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003211 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 case '(':
3213 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3215 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 case '*':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003217 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 c2 = l->buf[l->i];
3221 if (c2 == '+') {
3222 ++l->i;
3223 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003224 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003225 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 l->t.t = BC_LEX_COMMA;
3229 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 c2 = l->buf[l->i];
3232 if (c2 == '-') {
3233 ++l->i;
3234 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003235 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003236 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
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 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003240 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003241 else {
3242 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003243 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003244 }
3245 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003247 c2 = l->buf[l->i];
3248 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003249 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003251 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003253 case '0':
3254 case '1':
3255 case '2':
3256 case '3':
3257 case '4':
3258 case '5':
3259 case '6':
3260 case '7':
3261 case '8':
3262 case '9':
3263 case 'A':
3264 case 'B':
3265 case 'C':
3266 case 'D':
3267 case 'E':
3268 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003269 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003272 l->t.t = BC_LEX_SCOLON;
3273 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 case '<':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003275 bc_lex_assign(l, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003277 case '=':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003278 bc_lex_assign(l, XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 case '>':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003281 bc_lex_assign(l, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003283 case '[':
3284 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3286 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003287 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 if (l->buf[l->i] == '\n') {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003289 l->t.t = XC_LEX_WHITESPACE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003291 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003292 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003293 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 case '^':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003295 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003297 case 'a':
3298 case 'b':
3299 case 'c':
3300 case 'd':
3301 case 'e':
3302 case 'f':
3303 case 'g':
3304 case 'h':
3305 case 'i':
3306 case 'j':
3307 case 'k':
3308 case 'l':
3309 case 'm':
3310 case 'n':
3311 case 'o':
3312 case 'p':
3313 case 'q':
3314 case 'r':
3315 case 's':
3316 case 't':
3317 case 'u':
3318 case 'v':
3319 case 'w':
3320 case 'x':
3321 case 'y':
3322 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003323 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003324 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003325 case '{':
3326 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003327 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3328 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003329 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003330 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003331 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003332 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003333 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003334 ++l->i;
3335 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003336 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003337 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003338 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003339 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003340 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003341 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003342 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003343 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003344 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003345 }
3346
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003347 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003348}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003349#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003350#endif // ENABLE_BC
3351
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003352#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003353static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003354{
Denys Vlasenko81293c82018-12-24 01:53:55 +01003355 if (G_exreg && isspace(l->buf[l->i])) {
3356 bc_lex_whitespace(l); // eats whitespace (but not newline)
3357 l->i++; // bc_lex_name() expects this
Denys Vlasenko29301232018-12-11 15:29:32 +01003358 bc_lex_name(l);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003359 } else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003360 bc_vec_pop_all(&l->t.v);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003361 bc_vec_push(&l->t.v, &l->buf[l->i++]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003362 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003363 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003364 }
3365
Denys Vlasenko29301232018-12-11 15:29:32 +01003366 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003367}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003368#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003369
Denys Vlasenko29301232018-12-11 15:29:32 +01003370static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003371{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003372 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003373
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003374 l->t.t = XC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003375 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003376
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003377 nls = 0;
3378 depth = 1;
3379 i = l->i;
3380 for (;;) {
3381 char c = l->buf[i];
3382 if (c == '\0') {
3383 l->i = i;
3384 RETURN_STATUS(bc_error("string end could not be found"));
3385 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003386 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003387 if (i == l->i || l->buf[i - 1] != '\\') {
3388 if (c == '[') depth++;
3389 if (c == ']')
3390 if (--depth == 0)
3391 break;
3392 }
3393 bc_vec_push(&l->t.v, &l->buf[i]);
3394 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003395 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003396 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003397
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003398 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003399 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3400 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3401 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003402 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003403 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003404
3405 l->i = i;
3406 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003407 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003408
Denys Vlasenko29301232018-12-11 15:29:32 +01003409 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003410}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003411#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003412
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003413#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003414static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003415{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003416 static const //BcLexType - should be this type, but narrower type saves size:
3417 uint8_t
3418 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003419 XC_LEX_OP_REL_EQ, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_NE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01003420 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3421 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003422 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003423 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003424
Denys Vlasenko81293c82018-12-24 01:53:55 +01003425 BcStatus s;
3426 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003427 size_t i;
3428
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003429 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3430 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003431 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003432 }
3433
Denys Vlasenko81293c82018-12-24 01:53:55 +01003434 s = BC_STATUS_SUCCESS;
3435 c = l->buf[l->i++];
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003436 if (c >= '%' && c <= '~'
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003437 && (l->t.t = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003438 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003439 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003440 }
3441
3442 // This is the workhorse of the lexer.
3443 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003444// case '\0': // probably never reached
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003445// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003446// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003447 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003448 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003449 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003450 // only to allow interactive dc have a way to exit
3451 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003452 // on <enter>, not on _next_ token (which would mean
3453 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003454 // IOW: typing "1p<enter>" should print "1" _at once_,
3455 // not after some more input.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003456 l->t.t = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003457 l->newline = true;
3458 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003459 case '\t':
3460 case '\v':
3461 case '\f':
3462 case '\r':
3463 case ' ':
Denys Vlasenko81293c82018-12-24 01:53:55 +01003464 l->newline = 0; // was (c == '\n')
Gavin Howard01055ba2018-11-03 11:00:21 -06003465 bc_lex_whitespace(l);
3466 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003467 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003468 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003469 if (c2 == '=')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003470 l->t.t = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003471 else if (c2 == '<')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003472 l->t.t = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003473 else if (c2 == '>')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003474 l->t.t = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003475 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003476 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003477 ++l->i;
3478 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 bc_lex_lineComment(l);
3481 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003482 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003483 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003484 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003485 else
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 case '0':
3489 case '1':
3490 case '2':
3491 case '3':
3492 case '4':
3493 case '5':
3494 case '6':
3495 case '7':
3496 case '8':
3497 case '9':
3498 case 'A':
3499 case 'B':
3500 case 'C':
3501 case 'D':
3502 case 'E':
3503 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003504 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003505 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003506 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003507 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003508 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003509 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003510 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003511 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003512 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003513 }
3514
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003515 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003516}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003517#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003518#endif // ENABLE_DC
3519
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003520static void bc_parse_push(BcParse *p, char i)
3521{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003522 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003523 bc_vec_pushByte(&p->func->code, i);
3524}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003525
Gavin Howard01055ba2018-11-03 11:00:21 -06003526static void bc_parse_pushName(BcParse *p, char *name)
3527{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003528 while (*name)
3529 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003530 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003531}
3532
3533static void bc_parse_pushIndex(BcParse *p, size_t idx)
3534{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003535 size_t mask;
3536 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003537
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003538 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003539 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3540 amt = sizeof(idx);
3541 do {
3542 if (idx & mask) break;
3543 mask >>= 8;
3544 amt--;
3545 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003546
3547 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003548
3549 while (idx != 0) {
3550 bc_parse_push(p, (unsigned char)idx);
3551 idx >>= 8;
3552 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003553}
3554
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003555#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003556static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3557{
3558 bc_parse_push(p, BC_INST_JUMP);
3559 bc_parse_pushIndex(p, idx);
3560}
3561
3562static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3563{
3564 bc_parse_push(p, BC_INST_JUMP_ZERO);
3565 bc_parse_pushIndex(p, idx);
3566}
3567
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003568static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003569{
3570 char *str = xstrdup(p->l.t.v.v);
3571
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003572 bc_parse_push(p, XC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003573 bc_parse_pushIndex(p, p->func->strs.len);
3574 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003575
3576 RETURN_STATUS(zbc_lex_next(&p->l));
3577}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003578#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003579#endif
3580
3581static void bc_parse_pushNUM(BcParse *p)
3582{
3583 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003584#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003585 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003586#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003587 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003588#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003589 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003590#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003591 bc_parse_push(p, XC_INST_NUM);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003592 bc_parse_pushIndex(p, idx);
3593}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003594
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003595static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003596{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003597 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003598
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003599 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003600}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003601#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003602
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003603// Called when parsing or execution detects a failure,
3604// resets execution structures.
3605static void bc_program_reset(void)
3606{
3607 BcFunc *f;
3608 BcInstPtr *ip;
3609
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003610 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003611 bc_vec_pop_all(&G.prog.results);
3612
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003613 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003614 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003615 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003616}
3617
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003618// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003619// resets parsing structures.
3620static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003621{
3622 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003623 bc_func_free(p->func);
3624 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003625
Denys Vlasenko65e10462018-12-19 15:13:14 +01003626 p->fidx = BC_PROG_MAIN;
3627 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003628 }
3629
3630 p->l.i = p->l.len;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003631 p->l.t.t = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003632
Denys Vlasenko503faf92018-12-20 16:24:18 +01003633 IF_BC(bc_vec_pop_all(&p->exits);)
3634 IF_BC(bc_vec_pop_all(&p->conds);)
3635 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003636
Denys Vlasenkod38af482018-12-04 19:11:02 +01003637 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003638}
3639
3640static void bc_parse_free(BcParse *p)
3641{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003642 IF_BC(bc_vec_free(&p->exits);)
3643 IF_BC(bc_vec_free(&p->conds);)
3644 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003645 bc_lex_free(&p->l);
3646}
3647
Denys Vlasenkofa210792018-12-19 19:35:40 +01003648static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003649{
3650 memset(p, 0, sizeof(BcParse));
3651
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003652 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003653 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3654 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3655 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003656
Denys Vlasenkofa210792018-12-19 19:35:40 +01003657 p->fidx = fidx;
3658 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003659}
3660
Denys Vlasenko04715442018-12-21 00:10:26 +01003661static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003662{
Denys Vlasenko04715442018-12-21 00:10:26 +01003663 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003664 BcFunc f;
3665 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003666 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003667 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003668 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003669}
3670
3671#if ENABLE_BC
3672
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003673// Note: takes ownership of 'name' (must be malloced)
3674static size_t bc_program_addFunc(char *name)
3675{
3676 size_t idx;
3677 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003678 int inserted;
3679
3680 entry.name = name;
3681 entry.idx = G.prog.fns.len;
3682
3683 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3684 if (!inserted) free(name);
3685
3686 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3687 idx = entry_ptr->idx;
3688
3689 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003690 // There is already a function with this name.
3691 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003692 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003693 bc_func_free(func);
3694 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003695 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003696 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003697 }
3698
3699 return idx;
3700}
3701
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003702#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3703#define BC_PARSE_LEAF(p, rparen) \
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003704 (((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) || (rparen) || \
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003705 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3706
3707// We can calculate the conversion between tokens and exprs by subtracting the
3708// position of the first operator in the lex enum and adding the position of the
3709// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003710#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003711
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003712static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3713
3714static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3715{
3716 BcStatus s;
3717
3718 s = bc_parse_expr_empty_ok(p, flags);
3719 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3720 RETURN_STATUS(bc_error("empty expression"));
3721 RETURN_STATUS(s);
3722}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003723#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003724
3725static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003726#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003727
3728static BC_STATUS zbc_parse_stmt(BcParse *p)
3729{
3730 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3731}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003732#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003733
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003734static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003735{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003736 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3737 // Same for "else", "while()", "for()".
3738 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3739 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003740 if (p->l.t.t == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003741 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003742
3743 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003744}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003745#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003746
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003747static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3748 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003749{
Denys Vlasenko69560f42018-12-24 14:14:23 +01003750 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3751 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003752
3753 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003754 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003755 if (t == BC_LEX_LPAREN) break;
3756
Denys Vlasenko69560f42018-12-24 14:14:23 +01003757 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003758 if (l >= r && (l != r || !left)) break;
3759
Denys Vlasenko0154d782018-12-14 23:32:51 +01003760 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003761 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003762 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 }
3764
3765 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003766}
3767
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003768static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003769{
3770 BcLexType top;
3771
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003772 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003773 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003774 top = BC_PARSE_TOP_OP(p);
3775
3776 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003777 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003778
3779 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003780 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003781
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003782 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003783 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003784 top = BC_PARSE_TOP_OP(p);
3785 }
3786
3787 bc_vec_pop(&p->ops);
3788
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003789 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003790}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003791#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003792
Denys Vlasenko26819db2018-12-12 16:08:46 +01003793static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003794{
3795 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003796 size_t nparams;
3797
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003798 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003799 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3800
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003801 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003802 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003803
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003804 nparams = 0;
3805 if (p->l.t.t != BC_LEX_RPAREN) {
3806 for (;;) {
3807 s = zbc_parse_expr(p, flags);
3808 if (s) RETURN_STATUS(s);
3809 nparams++;
3810 if (p->l.t.t != BC_LEX_COMMA) {
3811 if (p->l.t.t == BC_LEX_RPAREN)
3812 break;
3813 RETURN_STATUS(bc_error_bad_token());
3814 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003815 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003816 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 }
3818 }
3819
Gavin Howard01055ba2018-11-03 11:00:21 -06003820 bc_parse_push(p, BC_INST_CALL);
3821 bc_parse_pushIndex(p, nparams);
3822
Denys Vlasenko26819db2018-12-12 16:08:46 +01003823 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003824}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003825#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003826
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003827// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003828static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003829{
3830 BcStatus s;
3831 BcId entry, *entry_ptr;
3832 size_t idx;
3833
3834 entry.name = name;
3835
Denys Vlasenko26819db2018-12-12 16:08:46 +01003836 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003837 if (s) goto err;
3838
3839 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003840 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003841 goto err;
3842 }
3843
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003844 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003845
3846 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003847 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003848 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003849 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003850 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003851 free(name);
3852
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003853 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003854 bc_parse_pushIndex(p, entry_ptr->idx);
3855
Denys Vlasenko26819db2018-12-12 16:08:46 +01003856 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003857 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003858 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003859 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003860}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003861#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003862
Denys Vlasenko26819db2018-12-12 16:08:46 +01003863static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003864{
3865 BcStatus s;
3866 char *name;
3867
3868 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003869 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003870 if (s) goto err;
3871
3872 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003873 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003874 if (s) goto err;
3875
3876 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003877 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003878 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003879 goto err;
3880 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003881 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003882 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003883 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003885 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003886 if (s) goto err;
3887 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003888 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003889 if (s) goto err;
3890 bc_parse_push(p, *type);
3891 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003892 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003893 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003895 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003896 goto err;
3897 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003898 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003899 s = zbc_parse_call(p, name, flags);
3900 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003901 *type = XC_INST_VAR;
3902 bc_parse_push(p, XC_INST_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06003903 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003904 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 }
3906
Denys Vlasenko26819db2018-12-12 16:08:46 +01003907 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003908 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003909 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003910 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003911}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003912#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003913
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003914static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003915{
3916 BcStatus s;
3917
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003918 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003919 if (s) RETURN_STATUS(s);
3920 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003921
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003922 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003923 if (s) RETURN_STATUS(s);
3924 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003925
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003926 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003927
Denys Vlasenko8cd468f2018-12-12 14:54:38 +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_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003931
Denys Vlasenko26819db2018-12-12 16:08:46 +01003932static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003933 BcInst *prev)
3934{
3935 BcStatus s;
3936
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003937 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003938 if (s) RETURN_STATUS(s);
3939 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003940
3941 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3942
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003943 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003944 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003945
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003946 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003947 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003948
Denys Vlasenko26819db2018-12-12 16:08:46 +01003949 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003951 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Gavin Howard01055ba2018-11-03 11:00:21 -06003952 bc_parse_push(p, *prev);
3953
Denys Vlasenko26819db2018-12-12 16:08:46 +01003954 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003955}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003956#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003957
Denys Vlasenko26819db2018-12-12 16:08:46 +01003958static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003959{
3960 BcStatus s;
3961
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003962 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003963 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003964
3965 if (p->l.t.t != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003966 *type = XC_INST_SCALE;
3967 bc_parse_push(p, XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003968 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003969 }
3970
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003971 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003972 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3973
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003974 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003975 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003976
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003977 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003978 if (s) RETURN_STATUS(s);
3979 if (p->l.t.t != BC_LEX_RPAREN)
3980 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003981 bc_parse_push(p, XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003982
Denys Vlasenko26819db2018-12-12 16:08:46 +01003983 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003984}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003985#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003986
Denys Vlasenko26819db2018-12-12 16:08:46 +01003987static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003988 size_t *nexprs, uint8_t flags)
3989{
3990 BcStatus s;
3991 BcLexType type;
3992 char inst;
3993 BcInst etype = *prev;
3994
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003995 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3996 || etype == XC_INST_SCALE || etype == BC_INST_LAST
3997 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003998 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003999 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4000 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004001 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004002 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004003 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4004 *paren_expr = true;
4005
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004006 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004007 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004008 type = p->l.t.t;
4009
4010 // Because we parse the next part of the expression
4011 // right here, we need to increment this.
4012 *nexprs = *nexprs + 1;
4013
4014 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004015 case XC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004016 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004017 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004018 case BC_LEX_KEY_IBASE:
4019 case BC_LEX_KEY_LAST:
4020 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004021 bc_parse_push(p, type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004022 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004024 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004025 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004026 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004027 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004028 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004029 else
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004030 bc_parse_push(p, XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004031 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004032 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004033 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004034 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004035 }
4036
4037 if (!s) bc_parse_push(p, inst);
4038 }
4039
Denys Vlasenko26819db2018-12-12 16:08:46 +01004040 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004041}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004042#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004043
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004044static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004045 bool rparen, size_t *nexprs)
4046{
4047 BcStatus s;
4048 BcLexType type;
4049 BcInst etype = *prev;
4050
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004051 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004052 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004053
4054 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004055 (etype >= XC_INST_NUM && etype <= XC_INST_SQRT) ?
Denys Vlasenko69560f42018-12-24 14:14:23 +01004056 XC_LEX_OP_MINUS :
4057 XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004058 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004059
4060 // We can just push onto the op stack because this is the largest
4061 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004062 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 bc_vec_push(&p->ops, &type);
4064 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004065 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004066
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004067 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004068}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004069#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004070
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004071static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004072{
4073 BcStatus s;
4074 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004075
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004076 for (;;) {
4077 s = zbc_lex_next(&p->l);
4078 if (s) RETURN_STATUS(s);
4079 type = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004080 if (type == XC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004081 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004082 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004083 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004084 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004085 if (s) RETURN_STATUS(s);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004086 bc_parse_push(p, XC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004087 if (p->l.t.t != BC_LEX_COMMA)
4088 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004089 }
4090
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004091 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004092}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004093#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004094
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004095static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004096{
4097 BcStatus s;
4098 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004099
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004100 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004101 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004102 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004103
4104 t = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004105 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Gavin Howard01055ba2018-11-03 11:00:21 -06004106 bc_parse_push(p, BC_INST_RET0);
4107 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004108 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004109 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004110 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004111 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004112 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004113 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004114 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004115
4116 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004117 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004118 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004119 }
4120
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004121 bc_parse_push(p, XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004122 }
4123
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004124 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004125 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004126}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004127#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004128
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004129static void rewrite_label_to_current(BcParse *p, size_t idx)
4130{
4131 size_t *label = bc_vec_item(&p->func->labels, idx);
4132 *label = p->func->code.len;
4133}
4134
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004135static BC_STATUS zbc_parse_if(BcParse *p)
4136{
4137 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004138 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004139
4140 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4141 s = zbc_lex_next(&p->l);
4142 if (s) RETURN_STATUS(s);
4143 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4144
4145 s = zbc_lex_next(&p->l);
4146 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004147 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004148 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004149 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004150
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004151 // Encode "if zero, jump to ..."
4152 // Pushed value (destination of the jump) is uninitialized,
4153 // will be rewritten to be address of "end of if()" or of "else".
4154 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004155 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004156
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004157 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004158 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004159
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004160 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4161 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004162 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004163
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004164 // Encode "after then_stmt, jump to end of if()"
4165 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4166 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004167 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004168
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004169 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 +01004170 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004171
Denys Vlasenko15850832018-12-16 21:40:54 +01004172 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004173
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004174 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004175 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004176 }
4177
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004178 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 +01004179 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004180
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004181 dbg_lex_done("%s:%d done", __func__, __LINE__);
4182 RETURN_STATUS(s);
4183}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004184#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004185
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004186static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004187{
4188 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004189 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004190 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004191
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004192 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004193 if (s) RETURN_STATUS(s);
4194 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004195 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004196 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004197
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004198 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004199 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004200 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004201
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004202 bc_vec_push(&p->exits, &ip_idx);
4203 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004204
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004205 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004206 if (s) RETURN_STATUS(s);
4207 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004208
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004209 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004210
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004211 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004212 if (s) RETURN_STATUS(s);
4213
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004214 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004215 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004216
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004217 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004218 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004219
4220 bc_vec_pop(&p->exits);
4221 bc_vec_pop(&p->conds);
4222
4223 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004224}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004225#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004226
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004227static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004228{
4229 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004230 size_t cond_idx, exit_idx, body_idx, update_idx;
4231
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004232 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004233 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004234 if (s) RETURN_STATUS(s);
4235 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004236 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004237 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004238
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004239 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004240 s = zbc_parse_expr(p, 0);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004241 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004242 if (s) RETURN_STATUS(s);
4243 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004244 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004245 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4246 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004247
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004248 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004249 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004250 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004251
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004252 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004253 update_idx = cond_idx + 1;
4254 body_idx = update_idx + 1;
4255 exit_idx = body_idx + 1;
4256
Gavin Howard01055ba2018-11-03 11:00:21 -06004257 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004258 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004259 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004260 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004261 // This is safe to set because the current token is a semicolon,
4262 // which has no string requirement.
4263 bc_vec_string(&p->l.t.v, 1, "1");
4264 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004265 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004266 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004267 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004268
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004269 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004270
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004271 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004272 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004273
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004274 bc_parse_pushJUMP_ZERO(p, exit_idx);
4275 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004276
Gavin Howard01055ba2018-11-03 11:00:21 -06004277 bc_vec_push(&p->conds, &update_idx);
4278 bc_vec_push(&p->func->labels, &p->func->code.len);
4279
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004280 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004281 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004282 if (s) RETURN_STATUS(s);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004283 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004284 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004285 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004286 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004287 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4288 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004289
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004290 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004291 bc_vec_push(&p->func->labels, &p->func->code.len);
4292
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004293 bc_vec_push(&p->exits, &exit_idx);
4294 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004295
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004296 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004297 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004298
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004299 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004300 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004301
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004302 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004303 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004304
4305 bc_vec_pop(&p->exits);
4306 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004307
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004308 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004309}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004310#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004311
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004312static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004313{
4314 BcStatus s;
4315 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004316
Gavin Howard01055ba2018-11-03 11:00:21 -06004317 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004318 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4319 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004320 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004321 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004322 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004323 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004324
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004325 bc_parse_pushJUMP(p, i);
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);
Gavin Howard01055ba2018-11-03 11:00:21 -06004329
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004330 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != XC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004331 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004332
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004333 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004334}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004335#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004336
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004337static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4338{
4339 BcId *autoid;
4340 BcId a;
4341 size_t i;
4342
4343 autoid = (void*)f->autos.v;
4344 for (i = 0; i < f->autos.len; i++, autoid++) {
4345 if (strcmp(name, autoid->name) == 0)
4346 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
4347 }
4348
4349 a.idx = var;
4350 a.name = name;
4351
4352 bc_vec_push(&f->autos, &a);
4353
4354 RETURN_STATUS(BC_STATUS_SUCCESS);
4355}
4356#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4357
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004358static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004359{
4360 BcStatus s;
4361 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004362 char *name;
4363
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004364 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004365 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004366 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004367 if (p->l.t.t != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004368 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004369
4370 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004371 p->fidx = bc_program_addFunc(name);
4372 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004373
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004374 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004375 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004376 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004377 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004378 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004379 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004380
4381 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004382 if (p->l.t.t != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004383 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004384
4385 ++p->func->nparams;
4386
4387 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004388 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004389 if (s) goto err;
4390
4391 var = p->l.t.t != BC_LEX_LBRACKET;
4392
4393 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004394 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004395 if (s) goto err;
4396
4397 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004398 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004399 goto err;
4400 }
4401
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004402 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004403 if (s) goto err;
4404 }
4405
4406 comma = p->l.t.t == BC_LEX_COMMA;
4407 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004408 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004409 if (s) goto err;
4410 }
4411
Denys Vlasenko29301232018-12-11 15:29:32 +01004412 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004413 if (s) goto err;
4414 }
4415
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004416 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004417
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004418 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004419 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004420
4421 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004422 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004423
Denys Vlasenko202dd192018-12-16 17:30:35 +01004424 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4425 s = zbc_lex_skip_if_at_NLINE(&p->l);
4426 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004427 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4428 if (p->l.t.t != BC_LEX_LBRACE)
4429 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004430
4431 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004432 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004433 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004434 if (s) RETURN_STATUS(s);
4435
4436 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004437
4438 // Subsequent code generation is into main program
4439 p->fidx = BC_PROG_MAIN;
4440 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004441
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004442 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004443 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004444 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004445 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004446 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004447 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004448}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004449#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004450
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004451static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004452{
4453 BcStatus s;
4454 bool comma, var, one;
4455 char *name;
4456
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004457 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004458 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004459 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004460
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004461 comma = false;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004462 one = p->l.t.t == XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06004463
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004464 while (p->l.t.t == XC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004465 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004466 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004467 if (s) goto err;
4468
4469 var = p->l.t.t != BC_LEX_LBRACKET;
4470 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004471 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004472 if (s) goto err;
4473
4474 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004475 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004476 goto err;
4477 }
4478
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004479 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004480 if (s) goto err;
4481 }
4482
4483 comma = p->l.t.t == BC_LEX_COMMA;
4484 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004485 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004486 if (s) goto err;
4487 }
4488
Denys Vlasenko29301232018-12-11 15:29:32 +01004489 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004490 if (s) goto err;
4491 }
4492
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004493 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4494 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004495
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004496 if (p->l.t.t != XC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004497 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004498
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004499 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004500 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004501 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004502 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004503 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004504 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004505}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004506#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004507
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004508#undef zbc_parse_stmt_possibly_auto
4509static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004510{
4511 BcStatus s = BC_STATUS_SUCCESS;
4512
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004513 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004514
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004515 if (p->l.t.t == XC_LEX_NLINE) {
4516 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004517 RETURN_STATUS(zbc_lex_next(&p->l));
4518 }
4519 if (p->l.t.t == BC_LEX_SCOLON) {
4520 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4521 RETURN_STATUS(zbc_lex_next(&p->l));
4522 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004523
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004524 if (p->l.t.t == BC_LEX_LBRACE) {
4525 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4526 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004527 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004528 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004529 } while (p->l.t.t == XC_LEX_NLINE);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004530 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4531 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4532 s = zbc_parse_auto(p);
4533 if (s) RETURN_STATUS(s);
4534 }
4535 while (p->l.t.t != BC_LEX_RBRACE) {
4536 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4537 s = zbc_parse_stmt(p);
4538 if (s) RETURN_STATUS(s);
4539 }
4540 s = zbc_lex_next(&p->l);
4541 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4542 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 }
4544
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004545 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004546 switch (p->l.t.t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004547 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004548 case BC_LEX_OP_INC:
4549 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 case BC_LEX_OP_BOOL_NOT:
4551 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004552 case XC_LEX_NAME:
4553 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004554 case BC_LEX_KEY_IBASE:
4555 case BC_LEX_KEY_LAST:
4556 case BC_LEX_KEY_LENGTH:
4557 case BC_LEX_KEY_OBASE:
4558 case BC_LEX_KEY_READ:
4559 case BC_LEX_KEY_SCALE:
4560 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004561 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004563 case XC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004564 s = zbc_parse_pushSTR(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004565 bc_parse_push(p, XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004566 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 case BC_LEX_KEY_BREAK:
4568 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004569 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004572 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004574 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004576 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004577 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004579 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004582 // "limits" is a compile-time command,
4583 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004584 printf(
4585 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4586 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4587 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4588 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4589 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4590 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4591 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4592 "Number of vars = "BC_MAX_VARS_STR "\n"
4593 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004594 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004595 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004596 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004597 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004598 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004599 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004600 // "quit" is a compile-time command. For example,
4601 // "if (0 == 1) quit" terminates when parsing the statement,
4602 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004603 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004605 if (!p->in_funcdef)
4606 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004607 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004608 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004610 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004611 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004613 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004615 }
4616
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004617 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004618 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004619}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004620#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004621
Denys Vlasenko99b37622018-12-15 20:06:59 +01004622static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004623{
4624 BcStatus s;
4625
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004626 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004627 if (p->l.t.t == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004628 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004629 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004630 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004631 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004632 } else {
4633 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 +01004634 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004635 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004636
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004637 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004638 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004639}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004640#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004641
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004642// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004643static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004644{
4645 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004646 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004647 BcLexType top, t = p->l.t.t;
4648 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004649 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004650 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4651
Denys Vlasenko17df8822018-12-14 23:00:24 +01004652 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004653 paren_first = p->l.t.t == BC_LEX_LPAREN;
4654 nparens = nrelops = 0;
4655 paren_expr = rprn = done = get_token = assign = false;
4656 bin_last = true;
4657
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004658 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004659 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004660 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004661 case BC_LEX_OP_INC:
4662 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004663 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004664 rprn = get_token = bin_last = false;
4665 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004666 case XC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004667 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004668 rprn = get_token = false;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004669 bin_last = prev == XC_INST_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004670 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004671 case BC_LEX_OP_ASSIGN_POWER:
4672 case BC_LEX_OP_ASSIGN_MULTIPLY:
4673 case BC_LEX_OP_ASSIGN_DIVIDE:
4674 case BC_LEX_OP_ASSIGN_MODULUS:
4675 case BC_LEX_OP_ASSIGN_PLUS:
4676 case BC_LEX_OP_ASSIGN_MINUS:
4677 case BC_LEX_OP_ASSIGN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004678 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4679 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4680 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004681 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004682 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004683 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004684 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004685 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 break;
4687 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004688 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004689 case XC_LEX_OP_POWER:
4690 case XC_LEX_OP_MULTIPLY:
4691 case XC_LEX_OP_DIVIDE:
4692 case XC_LEX_OP_MODULUS:
4693 case XC_LEX_OP_PLUS:
4694 case XC_LEX_OP_REL_EQ:
4695 case XC_LEX_OP_REL_LE:
4696 case XC_LEX_OP_REL_GE:
4697 case XC_LEX_OP_REL_NE:
4698 case XC_LEX_OP_REL_LT:
4699 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004700 case BC_LEX_OP_BOOL_NOT:
4701 case BC_LEX_OP_BOOL_OR:
4702 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004703 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004704 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004705 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004706 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004707 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004708 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004709 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004710 bc_parse_operator(p, t, ops_bgn, &nexprs);
4711 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004712 rprn = get_token = false;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004713 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004714 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004715 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004716 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004717 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004718 ++nparens;
4719 paren_expr = rprn = bin_last = false;
4720 get_token = true;
4721 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004722 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004723 case BC_LEX_RPAREN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004724 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004725 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004726 if (nparens == 0) {
4727 s = BC_STATUS_SUCCESS;
4728 done = true;
4729 get_token = false;
4730 break;
4731 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004732 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004733 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004734 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004735 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 --nparens;
4737 paren_expr = rprn = true;
4738 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004739 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004740 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004741 case XC_LEX_NAME:
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();
Gavin Howard01055ba2018-11-03 11:00:21 -06004744 paren_expr = true;
4745 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004746 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004747 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004748 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004749 case XC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004750 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004751 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004752 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004753 nexprs++;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004754 prev = XC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004755 paren_expr = get_token = true;
4756 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004757 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004758 case BC_LEX_KEY_IBASE:
4759 case BC_LEX_KEY_LAST:
4760 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004761 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004762 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004763 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004764 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004765 paren_expr = get_token = true;
4766 rprn = bin_last = false;
4767 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004768 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004769 case BC_LEX_KEY_LENGTH:
4770 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004771 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004772 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004773 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004774 paren_expr = true;
4775 rprn = get_token = bin_last = false;
4776 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004777 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004778 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004780 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004781 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004782 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 paren_expr = true;
4784 rprn = get_token = bin_last = false;
4785 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004786 prev = XC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004788 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004789 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004790 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004791 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004792 paren_expr = true;
4793 rprn = get_token = bin_last = false;
4794 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004795 prev = XC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004796 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004797 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004798 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004799 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004800 }
4801
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004802 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004803 }
4804
4805 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004806 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004807
4808 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004809 top = BC_PARSE_TOP_OP(p);
4810 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4811
4812 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004813 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004814
Denys Vlasenko0154d782018-12-14 23:32:51 +01004815 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004816
Denys Vlasenko69560f42018-12-24 14:14:23 +01004817 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004818 bc_vec_pop(&p->ops);
4819 }
4820
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004821 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004822 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004823
Gavin Howard01055ba2018-11-03 11:00:21 -06004824 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004825 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004826 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004827 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004828 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004829 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004830 }
4831
4832 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004833 if (paren_first || !assign) bc_parse_push(p, XC_INST_PRINT);
4834 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004835 }
4836
Denys Vlasenko17df8822018-12-14 23:00:24 +01004837 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004838 return s;
4839}
4840
Gavin Howard01055ba2018-11-03 11:00:21 -06004841#endif // ENABLE_BC
4842
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004843#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004844
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004845static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004846{
4847 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004848
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004849 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004850 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004851 if (p->l.t.t != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004852
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004853 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004854
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004855 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004856}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004857#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004858
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004859static void dc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004860{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004861 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004862 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004863
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004864 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004865
4866 str = xstrdup(p->l.t.v.v);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004867 bc_parse_push(p, XC_INST_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004868 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004869 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004870
4871 // Explanation needed here
4872 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004873 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004874
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004875 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004876}
4877
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004878static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004879{
4880 BcStatus s;
4881
4882 bc_parse_push(p, inst);
4883 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004884 s = zdc_parse_register(p);
4885 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004886 }
4887
4888 if (store) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004889 bc_parse_push(p, DC_INST_SWAP);
4890 bc_parse_push(p, XC_INST_ASSIGN);
4891 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004892 }
4893
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004894 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004895}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004896#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004897
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004898static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004899{
4900 BcStatus s;
4901
4902 bc_parse_push(p, inst);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004903 bc_parse_push(p, DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004904
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004905 s = zdc_parse_register(p);
4906 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004907
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004908 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004909 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004910
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004911 // Note that 'else' part can not be on the next line:
4912 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4913 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004914 if (p->l.t.t == DC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004915 s = zdc_parse_register(p);
4916 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004917 s = zbc_lex_next(&p->l);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004918 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004919 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004920 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004921
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004922 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004923}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004924#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004925
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004926static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004927{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004928 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004929 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004930 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004931
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004932 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004933 s = BC_STATUS_SUCCESS;
4934 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004935 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004936 case XC_LEX_OP_REL_EQ:
4937 case XC_LEX_OP_REL_LE:
4938 case XC_LEX_OP_REL_GE:
4939 case XC_LEX_OP_REL_NE:
4940 case XC_LEX_OP_REL_LT:
4941 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004942 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenko69560f42018-12-24 14:14:23 +01004943 s = zdc_parse_cond(p, t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004944 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004945 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004946 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004947 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004948 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004949 s = zdc_parse_mem(p, XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004950 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004951 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004952 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004953 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004954 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004955 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004956 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4957 s = zbc_lex_next(&p->l);
4958 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004959 if (p->l.t.t != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004960 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004961 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004962 bc_parse_push(p, XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004963 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004964 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004965 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4966 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004967 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004968 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004969 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004970 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004971 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004972 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004973 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004974 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004975 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004976 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004977 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004978 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004979 case DC_LEX_LOAD:
4980 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004981 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004982 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004983 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004984 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004985 case DC_LEX_STORE_IBASE:
4986 case DC_LEX_STORE_SCALE:
4987 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004988 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004989 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004990 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004991 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004992 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004993 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4994 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004995 }
4996
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004997 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004998
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004999 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005000 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005001}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005002#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005003
Denys Vlasenko39287e02018-12-22 02:23:08 +01005004static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005005{
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005006 BcInst inst;
5007 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005008
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005009 inst = dc_LEX_to_INST[p->l.t.t];
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005010 if (inst != DC_INST_INVALID) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005011 bc_parse_push(p, inst);
5012 s = zbc_lex_next(&p->l);
5013 } else {
5014 s = zdc_parse_token(p, p->l.t.t);
5015 }
5016 RETURN_STATUS(s);
5017}
5018#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5019
5020static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
5021{
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01005022 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005023 while (p->l.t.t != XC_LEX_EOF) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005024 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005025 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005026 }
5027
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005028 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005029 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005030}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005031#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005032
Gavin Howard01055ba2018-11-03 11:00:21 -06005033#endif // ENABLE_DC
5034
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005035//
5036// Execution engine
5037//
5038
5039#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5040#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5041
Denys Vlasenkodf515392018-12-02 19:27:48 +01005042static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005043{
Gavin Howard01055ba2018-11-03 11:00:21 -06005044 BcId e, *ptr;
5045 BcVec *v, *map;
5046 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005047 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005048
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005049 v = var ? &G.prog.vars : &G.prog.arrs;
5050 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005051
5052 e.name = id;
5053 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005054 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005055
5056 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005057 BcVec v2;
5058 bc_array_init(&v2, var);
5059 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005060 }
5061
5062 ptr = bc_vec_item(map, i);
5063 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005064 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005065}
5066
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005067// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005068static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005069{
Gavin Howard01055ba2018-11-03 11:00:21 -06005070 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005071 case BC_RESULT_STR:
5072 case BC_RESULT_TEMP:
5073 case BC_RESULT_IBASE:
5074 case BC_RESULT_SCALE:
5075 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 *num = &r->d.n;
5077 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005078 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005079 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005080 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005081 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005082 size_t len;
5083
5084 str = *bc_program_const(r->d.id.idx);
5085 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005086
5087 bc_num_init(&r->d.n, len);
5088
5089 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005090 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005091 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005092 if (s) {
5093 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005094 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005095 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005096 *num = &r->d.n;
5097 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005098 break;
5099 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005100 case BC_RESULT_VAR:
5101 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005102 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005103 BcVec *v;
5104
Denys Vlasenkodf515392018-12-02 19:27:48 +01005105 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005106
5107 if (r->t == BC_RESULT_ARRAY_ELEM) {
5108 v = bc_vec_top(v);
5109 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5110 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005111 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005112 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005113 break;
5114 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005115#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005116 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005117 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005118 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005119 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005120 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005121 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005122#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005123#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005124 default:
5125 // Testing the theory that dc does not reach LAST/ONE
5126 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005127#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005128 }
5129
Denys Vlasenko29301232018-12-11 15:29:32 +01005130 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005131}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005132#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005133
Denys Vlasenko29301232018-12-11 15:29:32 +01005134static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005135 BcResult **r, BcNum **rn, bool assign)
5136{
5137 BcStatus s;
5138 bool hex;
5139 BcResultType lt, rt;
5140
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005141 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005142 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005143
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005144 *r = bc_vec_item_rev(&G.prog.results, 0);
5145 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005146
5147 lt = (*l)->t;
5148 rt = (*r)->t;
5149 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5150
Denys Vlasenko29301232018-12-11 15:29:32 +01005151 s = zbc_program_num(*l, ln, false);
5152 if (s) RETURN_STATUS(s);
5153 s = zbc_program_num(*r, rn, hex);
5154 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005155
5156 // We run this again under these conditions in case any vector has been
5157 // reallocated out from under the BcNums or arrays we had.
5158 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005159 s = zbc_program_num(*l, ln, false);
5160 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005161 }
5162
5163 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005164 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005165 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005166 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005167
Denys Vlasenko29301232018-12-11 15:29:32 +01005168 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005169}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005170#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005171
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005172static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005173{
5174 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005175 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005176 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005177}
5178
Denys Vlasenko29301232018-12-11 15:29:32 +01005179static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005180{
5181 BcStatus s;
5182
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005183 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005184 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005185 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005186
Denys Vlasenko29301232018-12-11 15:29:32 +01005187 s = zbc_program_num(*r, n, false);
5188 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005189
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005190 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005191 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005192
Denys Vlasenko29301232018-12-11 15:29:32 +01005193 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005194}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005195#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005196
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005197static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005198{
5199 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005200 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005201}
5202
Denys Vlasenko259137d2018-12-11 19:42:05 +01005203static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005204{
5205 BcStatus s;
5206 BcResult *opd1, *opd2, res;
5207 BcNum *n1, *n2 = NULL;
5208
Denys Vlasenko29301232018-12-11 15:29:32 +01005209 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005210 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005211 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005212
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005213 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005214 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005215 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005216 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenko259137d2018-12-11 19:42:05 +01005218 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005219 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005220 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005221 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005222}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005223#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005224
Denys Vlasenko26819db2018-12-12 16:08:46 +01005225static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005226{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005227 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005228 BcStatus s;
5229 BcParse parse;
5230 BcVec buf;
5231 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005232 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005234 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005235 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005236
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005237 sv_file = G.prog.file;
5238 G.prog.file = NULL;
5239
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005240 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005241 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005242
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005243 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005244 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005245
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005246 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005247 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005248 if (IS_BC) {
5249 IF_BC(s = zbc_parse_expr(&parse, 0));
5250 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005251 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005252 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005253 if (s) goto exec_err;
5254
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005255 if (parse.l.t.t != XC_LEX_NLINE && parse.l.t.t != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005256 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005257 goto exec_err;
5258 }
5259
5260 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005261 ip.inst_idx = 0;
5262 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005263
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005264 bc_parse_push(&parse, XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005265 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005266 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005267 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005268 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005269 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005270 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005271}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005272#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005273
5274static size_t bc_program_index(char *code, size_t *bgn)
5275{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005276 unsigned char *bytes = (void*)(code + *bgn);
5277 unsigned amt;
5278 unsigned i;
5279 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005280
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005281 amt = *bytes++;
5282 *bgn += amt + 1;
5283
5284 amt *= 8;
5285 res = 0;
5286 for (i = 0; i < amt; i += 8)
5287 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005288
5289 return res;
5290}
5291
5292static char *bc_program_name(char *code, size_t *bgn)
5293{
5294 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005295 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005296
Denys Vlasenko694d2982018-12-18 19:17:11 +01005297 code += *bgn;
5298 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005299 i = 0;
5300 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005301 char c = *code++;
5302 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005303 break;
5304 s[i++] = c;
5305 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005306 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005307 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005308
5309 return s;
5310}
5311
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005312static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005313{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005314#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005315 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005316 // Example: echo '[]ap' | dc
5317 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005318 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005319 return;
5320 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005321#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005322 while (*str) {
5323 int c = *str++;
5324 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005325 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005326 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005327 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005328 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005329 case 'a':
5330 bb_putchar('\a');
5331 break;
5332 case 'b':
5333 bb_putchar('\b');
5334 break;
5335 case '\\':
5336 case 'e':
5337 bb_putchar('\\');
5338 break;
5339 case 'f':
5340 bb_putchar('\f');
5341 break;
5342 case 'n':
5343 bb_putchar('\n');
5344 G.prog.nchars = SIZE_MAX;
5345 break;
5346 case 'r':
5347 bb_putchar('\r');
5348 break;
5349 case 'q':
5350 bb_putchar('"');
5351 break;
5352 case 't':
5353 bb_putchar('\t');
5354 break;
5355 default:
5356 // Just print the backslash and following character.
5357 bb_putchar('\\');
5358 ++G.prog.nchars;
5359 bb_putchar(c);
5360 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005361 }
5362 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005363 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005364 }
5365}
5366
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005367static void bc_num_printNewline(void)
5368{
5369 if (G.prog.nchars == G.prog.len - 1) {
5370 bb_putchar('\\');
5371 bb_putchar('\n');
5372 G.prog.nchars = 0;
5373 }
5374}
5375
5376#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005377static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005378{
5379 (void) radix;
5380 bb_putchar((char) num);
5381 G.prog.nchars += width;
5382}
5383#endif
5384
5385static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5386{
5387 size_t exp, pow;
5388
5389 bc_num_printNewline();
5390 bb_putchar(radix ? '.' : ' ');
5391 ++G.prog.nchars;
5392
5393 bc_num_printNewline();
5394 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5395 continue;
5396
5397 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5398 size_t dig;
5399 bc_num_printNewline();
5400 dig = num / pow;
5401 num -= dig * pow;
5402 bb_putchar(((char) dig) + '0');
5403 }
5404}
5405
5406static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5407{
5408 if (radix) {
5409 bc_num_printNewline();
5410 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005411 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005412 }
5413
5414 bc_num_printNewline();
5415 bb_putchar(bb_hexdigits_upcase[num]);
5416 G.prog.nchars += width;
5417}
5418
5419static void bc_num_printDecimal(BcNum *n)
5420{
5421 size_t i, rdx = n->rdx - 1;
5422
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005423 if (n->neg) {
5424 bb_putchar('-');
5425 G.prog.nchars++;
5426 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005427
5428 for (i = n->len - 1; i < n->len; --i)
5429 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5430}
5431
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005432typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5433
Denys Vlasenkod3401432018-12-18 17:00:35 +01005434static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005435{
5436 BcStatus s;
5437 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005438 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005439 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005440 BcNum intp, fracp, digit, frac_len;
5441 unsigned long dig, *ptr;
5442 size_t i;
5443 bool radix;
5444
5445 if (n->len == 0) {
5446 print(0, width, false);
5447 RETURN_STATUS(BC_STATUS_SUCCESS);
5448 }
5449
5450 bc_vec_init(&stack, sizeof(long), NULL);
5451 bc_num_init(&intp, n->len);
5452 bc_num_init(&fracp, n->rdx);
5453 bc_num_init(&digit, width);
5454 bc_num_init(&frac_len, BC_NUM_INT(n));
5455 bc_num_copy(&intp, n);
5456 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005457 base.cap = ARRAY_SIZE(base_digs);
5458 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005459 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005460
5461 bc_num_truncate(&intp, intp.rdx);
5462 s = zbc_num_sub(n, &intp, &fracp, 0);
5463 if (s) goto err;
5464
5465 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005466 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005467 if (s) goto err;
5468 s = zbc_num_ulong(&digit, &dig);
5469 if (s) goto err;
5470 bc_vec_push(&stack, &dig);
5471 }
5472
5473 for (i = 0; i < stack.len; ++i) {
5474 ptr = bc_vec_item_rev(&stack, i);
5475 print(*ptr, width, false);
5476 }
5477
5478 if (!n->rdx) goto err;
5479
5480 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005481 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005482 if (s) goto err;
5483 s = zbc_num_ulong(&fracp, &dig);
5484 if (s) goto err;
5485 bc_num_ulong2num(&intp, dig);
5486 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5487 if (s) goto err;
5488 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005489 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005490 if (s) goto err;
5491 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005492 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005493 bc_num_free(&frac_len);
5494 bc_num_free(&digit);
5495 bc_num_free(&fracp);
5496 bc_num_free(&intp);
5497 bc_vec_free(&stack);
5498 RETURN_STATUS(s);
5499}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005500#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005501
5502static BC_STATUS zbc_num_printBase(BcNum *n)
5503{
5504 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005505 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005506 BcNumDigitOp print;
5507 bool neg = n->neg;
5508
5509 if (neg) {
5510 bb_putchar('-');
5511 G.prog.nchars++;
5512 }
5513
5514 n->neg = false;
5515
5516 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5517 width = 1;
5518 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005519 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005520 unsigned i = G.prog.ob_t - 1;
5521 width = 0;
5522 for (;;) {
5523 width++;
5524 i /= 10;
5525 if (i == 0)
5526 break;
5527 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005528 print = bc_num_printDigits;
5529 }
5530
Denys Vlasenkod3401432018-12-18 17:00:35 +01005531 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005532 n->neg = neg;
5533
5534 RETURN_STATUS(s);
5535}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005536#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005537
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005538static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5539{
5540 BcStatus s = BC_STATUS_SUCCESS;
5541
5542 bc_num_printNewline();
5543
5544 if (n->len == 0) {
5545 bb_putchar('0');
5546 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005547 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005548 bc_num_printDecimal(n);
5549 else
5550 s = zbc_num_printBase(n);
5551
5552 if (newline) {
5553 bb_putchar('\n');
5554 G.prog.nchars = 0;
5555 }
5556
5557 RETURN_STATUS(s);
5558}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005559#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005560
Denys Vlasenko29301232018-12-11 15:29:32 +01005561static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005562{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005563 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005564 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005565 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005566 bool pop = inst != XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06005567
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005568 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005569 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005570
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005571 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005572 s = zbc_program_num(r, &num, false);
5573 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005574
5575 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005576 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005577#if ENABLE_BC
5578 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5579#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005580 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005581 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005582
5583 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005584 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005585
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005586 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005587 for (;;) {
5588 char c = *str++;
5589 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005590 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005591 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005592 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005593 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005594 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005595 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005596 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005597 }
5598 }
5599
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005600 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005601
Denys Vlasenko29301232018-12-11 15:29:32 +01005602 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005603}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005604#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005605
Denys Vlasenko29301232018-12-11 15:29:32 +01005606static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005607{
5608 BcStatus s;
5609 BcResult res, *ptr;
5610 BcNum *num = NULL;
5611
Denys Vlasenko29301232018-12-11 15:29:32 +01005612 s = zbc_program_prep(&ptr, &num);
5613 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005614
5615 bc_num_init(&res.d.n, num->len);
5616 bc_num_copy(&res.d.n, num);
5617 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5618
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005619 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005620
Denys Vlasenko29301232018-12-11 15:29:32 +01005621 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005622}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005623#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005624
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005625static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005626{
5627 BcStatus s;
5628 BcResult *opd1, *opd2, res;
5629 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005630 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005631
Denys Vlasenko29301232018-12-11 15:29:32 +01005632 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005633 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005634
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005635 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005636
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005637 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005638 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005639 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005640 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005641 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005642 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005643 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005644 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005645 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005646 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005647 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005648 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005649 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005650 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005651 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005652 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005653 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005654 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005655 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005656 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005657 cond = (cond > 0);
5658 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005659 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005660 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005661 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005662 }
5663 }
5664
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005665 if (cond) bc_num_one(&res.d.n);
5666 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005667
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005668 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005669
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005670 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005671}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005672#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005673
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005674#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005675static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005676{
5677 BcNum n2;
5678 BcResult res;
5679
5680 memset(&n2, 0, sizeof(BcNum));
5681 n2.rdx = res.d.id.idx = r->d.id.idx;
5682 res.t = BC_RESULT_STR;
5683
5684 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005685 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005686 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005687 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005688 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005689 }
5690
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005691 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005692 bc_vec_push(v, &n2);
5693
Denys Vlasenko29301232018-12-11 15:29:32 +01005694 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005695}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005696#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005697#endif // ENABLE_DC
5698
Denys Vlasenko29301232018-12-11 15:29:32 +01005699static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005700{
5701 BcStatus s;
5702 BcResult *ptr, r;
5703 BcVec *v;
5704 BcNum *n;
5705
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005706 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005707 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005708
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005709 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005710 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005711 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005712 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005713
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005714#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005715 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005716 RETURN_STATUS(bc_error_variable_is_wrong_type());
5717 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005718 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005719#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005720
Denys Vlasenko29301232018-12-11 15:29:32 +01005721 s = zbc_program_num(ptr, &n, false);
5722 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005723
5724 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005725 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
5727 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005728 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005729 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005730 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005731 bc_array_init(&r.d.v, true);
5732 bc_array_copy(&r.d.v, (BcVec *) n);
5733 }
5734
5735 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005736 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005737
Denys Vlasenko29301232018-12-11 15:29:32 +01005738 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005739}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005740#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005741
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005742static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005743{
5744 BcStatus s;
5745 BcResult *left, *right, res;
5746 BcNum *l = NULL, *r = NULL;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005747 bool assign = inst == XC_INST_ASSIGN, ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005748
Denys Vlasenko29301232018-12-11 15:29:32 +01005749 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005750 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005751
5752 ib = left->t == BC_RESULT_IBASE;
5753 sc = left->t == BC_RESULT_SCALE;
5754
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005755#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005756 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005757 BcVec *v;
5758
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005759 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005760 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005761 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005762
Denys Vlasenkofa210792018-12-19 19:35:40 +01005763 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005764 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005765#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005766
5767 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005768 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005769 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005770 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005771 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005772
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005773#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005774 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005775 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005776
5777 if (assign)
5778 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005779 else {
5780 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005781 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 +01005782 }
5783 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005784#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005785 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005786#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005787
5788 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005789 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005790 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
Denys Vlasenko64074a12018-12-07 15:50:14 +01005791 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005792 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005793 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005794 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005795 size_t max;
5796 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005797
Denys Vlasenko29301232018-12-11 15:29:32 +01005798 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005799 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005800 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005801 if (sc) {
5802 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005803 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005804 } else {
5805 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005806 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005807 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005808 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005809 }
5810
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005811 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005812 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005813
5814 *ptr = (size_t) val;
5815 s = BC_STATUS_SUCCESS;
5816 }
5817
5818 bc_num_init(&res.d.n, l->len);
5819 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005820 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005821
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005822 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005823}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005824#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005825
Denys Vlasenko416ce762018-12-02 20:57:17 +01005826#if !ENABLE_DC
5827#define bc_program_pushVar(code, bgn, pop, copy) \
5828 bc_program_pushVar(code, bgn)
5829// for bc, 'pop' and 'copy' are always false
5830#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005831static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005832 bool pop, bool copy)
5833{
Gavin Howard01055ba2018-11-03 11:00:21 -06005834 BcResult r;
5835 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005836
5837 r.t = BC_RESULT_VAR;
5838 r.d.id.name = name;
5839
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005840#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005841 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005842 BcVec *v = bc_program_search(name, true);
5843 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005844
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005845 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005846 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005847 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005848 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005849
5850 if (!BC_PROG_STR(num)) {
5851 r.t = BC_RESULT_TEMP;
5852 bc_num_init_DEF_SIZE(&r.d.n);
5853 bc_num_copy(&r.d.n, num);
5854 } else {
5855 r.t = BC_RESULT_STR;
5856 r.d.id.idx = num->rdx;
5857 }
5858
5859 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005860 }
5861#endif // ENABLE_DC
5862
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005863 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005864
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005865 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005866}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005867#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005868
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005869static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005870{
5871 BcStatus s = BC_STATUS_SUCCESS;
5872 BcResult r;
5873 BcNum *num;
5874
5875 r.d.id.name = bc_program_name(code, bgn);
5876
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005877 if (inst == XC_INST_ARRAY) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005878 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005879 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005880 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005881 BcResult *operand;
5882 unsigned long temp;
5883
Denys Vlasenko29301232018-12-11 15:29:32 +01005884 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005885 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005886 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005887 if (s) goto err;
5888
5889 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005890 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005891 goto err;
5892 }
5893
5894 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005895 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005896 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005897 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005898 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005899 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005901#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005902
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005903#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005904static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005905{
5906 BcStatus s;
5907 BcResult *ptr, res, copy;
5908 BcNum *num = NULL;
5909 char inst2 = inst;
5910
Denys Vlasenko29301232018-12-11 15:29:32 +01005911 s = zbc_program_prep(&ptr, &num);
5912 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005913
5914 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5915 copy.t = BC_RESULT_TEMP;
5916 bc_num_init(&copy.d.n, num->len);
5917 bc_num_copy(&copy.d.n, num);
5918 }
5919
5920 res.t = BC_RESULT_ONE;
5921 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5922 BC_INST_ASSIGN_PLUS :
5923 BC_INST_ASSIGN_MINUS;
5924
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005925 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005926 s = zbc_program_assign(inst);
5927 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005928
5929 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005930 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005931 }
5932
Denys Vlasenko29301232018-12-11 15:29:32 +01005933 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005934}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005935#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005936
Denys Vlasenko29301232018-12-11 15:29:32 +01005937static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005938{
Gavin Howard01055ba2018-11-03 11:00:21 -06005939 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005940 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005941 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005942 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 BcResult *arg;
5944
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005945 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005946 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005948 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005949
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005950 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005951 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005952 }
5953 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005954 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005955 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005956 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005957
5958 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005959 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005960
5961 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005962 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005963
5964 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005965 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005966
Denys Vlasenko29301232018-12-11 15:29:32 +01005967 s = zbc_program_copyToVar(a->name, a->idx);
5968 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005969 }
5970
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005971 a = bc_vec_item(&func->autos, i);
5972 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005973 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005974
Denys Vlasenkodf515392018-12-02 19:27:48 +01005975 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005976 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005977 BcNum n2;
5978 bc_num_init_DEF_SIZE(&n2);
5979 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005980 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005981 BcVec v2;
5982 bc_array_init(&v2, true);
5983 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005984 }
5985 }
5986
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005987 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005988
Denys Vlasenko29301232018-12-11 15:29:32 +01005989 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005990}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005991#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005992
Denys Vlasenko29301232018-12-11 15:29:32 +01005993static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005994{
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 BcResult res;
5996 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005997 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005998 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005999 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006000
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006001 if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == XC_INST_RET)))
Denys Vlasenko29301232018-12-11 15:29:32 +01006002 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006003
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006004 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 res.t = BC_RESULT_TEMP;
6006
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006007 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006008 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006009 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006010 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
Denys Vlasenko29301232018-12-11 15:29:32 +01006012 s = zbc_program_num(operand, &num, false);
6013 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006014 bc_num_init(&res.d.n, num->len);
6015 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006016 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006017 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006018 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006019 }
6020
6021 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006022 a = (void*)f->autos.v;
6023 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006025 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 bc_vec_pop(v);
6027 }
6028
Denys Vlasenko24e41942018-12-21 23:01:26 +01006029 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006030 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006031 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006032
Denys Vlasenko29301232018-12-11 15:29:32 +01006033 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006034}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006035#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006036#endif // ENABLE_BC
6037
6038static unsigned long bc_program_scale(BcNum *n)
6039{
6040 return (unsigned long) n->rdx;
6041}
6042
6043static unsigned long bc_program_len(BcNum *n)
6044{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006045 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006046
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006047 if (n->rdx != len) return len;
6048 for (;;) {
6049 if (len == 0) break;
6050 len--;
6051 if (n->num[len] != 0) break;
6052 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006053 return len;
6054}
6055
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006056static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006057{
6058 BcStatus s;
6059 BcResult *opnd;
6060 BcNum *num = NULL;
6061 BcResult res;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006062 bool len = inst == XC_INST_LENGTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006063
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006064 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006065 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006066 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006067
Denys Vlasenko29301232018-12-11 15:29:32 +01006068 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006069 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006070
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006071#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006072 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006073 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006074#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006075
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006076 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006077
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006078 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006079 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006080#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006081 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006082 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006083 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006084#endif
6085#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006086 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006087 char **str;
6088 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6089
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006090 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006091 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006092 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006093#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006094 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006095 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 }
6097
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006098 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006099
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006100 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006101}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006102#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006103
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006104#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006105static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006106{
6107 BcStatus s;
6108 BcResult *opd1, *opd2, res, res2;
6109 BcNum *n1, *n2 = NULL;
6110
Denys Vlasenko29301232018-12-11 15:29:32 +01006111 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006112 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006114 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006115 bc_num_init(&res2.d.n, n2->len);
6116
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006117 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006118 if (s) goto err;
6119
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006120 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006121 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006122 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006124 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006125 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006126 bc_num_free(&res2.d.n);
6127 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006128 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006129}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006130#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006131
Denys Vlasenkofa210792018-12-19 19:35:40 +01006132static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006133{
6134 BcStatus s;
6135 BcResult *r1, *r2, *r3, res;
6136 BcNum *n1, *n2, *n3;
6137
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006138 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006139 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006140 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006141 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006142
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006143 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006144 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006145 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006146 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006147 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
6149 // Make sure that the values have their pointers updated, if necessary.
6150 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006152 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006153 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006154 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006155 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006156 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006157 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158 }
6159 }
6160
6161 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006162 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006163 if (s) goto err;
6164
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006165 bc_vec_pop(&G.prog.results);
6166 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006167
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006168 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006169 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006170 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006171 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006172}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006173#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006174
Denys Vlasenkofa210792018-12-19 19:35:40 +01006175static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006176{
Gavin Howard01055ba2018-11-03 11:00:21 -06006177 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006178 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006179
6180 res.t = BC_RESULT_TEMP;
6181
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006182 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006183 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006184 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006185}
6186
Denys Vlasenkofa210792018-12-19 19:35:40 +01006187static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006188{
6189 BcStatus s;
6190 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006191 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006192 char **strs;
6193 char *str;
6194 char c;
6195 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006196
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006197 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006198 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006199 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200
Denys Vlasenko29301232018-12-11 15:29:32 +01006201 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006202 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203
6204 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006205 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006206 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006207 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006208
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006209 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 bc_num_copy(&n, num);
6211 bc_num_truncate(&n, n.rdx);
6212
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006213 strmb.cap = ARRAY_SIZE(strmb_digs);
6214 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006215 bc_num_ulong2num(&strmb, 0x100);
6216 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006217
Gavin Howard01055ba2018-11-03 11:00:21 -06006218 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006219 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006220 if (s) goto num_err;
6221
6222 c = (char) val;
6223
6224 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006225 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006226 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006227 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006228 sp = *bc_program_str(idx);
6229 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006230 }
6231
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006232 strs = (void*)G.prog.strs.v;
6233 for (idx = 0; idx < G.prog.strs.len; idx++) {
6234 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6235 goto dup;
6236 }
6237 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006238 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006239 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006240 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006241 bc_vec_push(&G.prog.strs, &str);
6242 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006243 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006244 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006245 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006246
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006247 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006248 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006249 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006250 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006251}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006252#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006253
Denys Vlasenkofa210792018-12-19 19:35:40 +01006254static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006255{
6256 BcStatus s;
6257 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006258 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006259 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006260
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006261 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006262 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006263 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
Denys Vlasenko29301232018-12-11 15:29:32 +01006265 s = zbc_program_num(r, &n, false);
6266 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006267
Denys Vlasenko57734c92018-12-17 21:14:05 +01006268 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006269 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006270 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006271 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006272 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006273 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006274 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006275 }
6276
Denys Vlasenko29301232018-12-11 15:29:32 +01006277 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006278}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006279#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006280
Denys Vlasenkofa210792018-12-19 19:35:40 +01006281static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006282{
6283 BcStatus s;
6284 BcResult *opnd;
6285 BcNum *num = NULL;
6286 unsigned long val;
6287
Denys Vlasenko29301232018-12-11 15:29:32 +01006288 s = zbc_program_prep(&opnd, &num);
6289 if (s) RETURN_STATUS(s);
6290 s = zbc_num_ulong(num, &val);
6291 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006292
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006293 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006295 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006296 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006297 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006298 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006299 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006301 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006302
Denys Vlasenko29301232018-12-11 15:29:32 +01006303 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006304}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006305#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006306
Denys Vlasenkofa210792018-12-19 19:35:40 +01006307static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006308{
6309 BcStatus s = BC_STATUS_SUCCESS;
6310 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006311 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006312 BcInstPtr ip;
6313 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006314
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006315 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006316 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006317
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006318 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006319
6320 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006321 BcNum *n = n; // for compiler
6322 bool exec;
6323 char *name;
6324 char *then_name = bc_program_name(code, bgn);
6325 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006326
6327 if (code[*bgn] == BC_PARSE_STREND)
6328 (*bgn) += 1;
6329 else
6330 else_name = bc_program_name(code, bgn);
6331
6332 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006333 name = then_name;
6334 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006335 exec = true;
6336 name = else_name;
6337 }
6338
6339 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006340 BcVec *v;
6341 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006342 n = bc_vec_top(v);
6343 }
6344
6345 free(then_name);
6346 free(else_name);
6347
6348 if (!exec) goto exit;
6349 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006350 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006351 goto exit;
6352 }
6353
6354 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006355 } else {
6356 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006357 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006358 } else if (r->t == BC_RESULT_VAR) {
6359 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006360 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006361 if (s || !BC_PROG_STR(n)) goto exit;
6362 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006363 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006364 goto exit;
6365 }
6366
6367 fidx = sidx + BC_PROG_REQ_FUNCS;
6368
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006369 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006370
6371 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006372 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006373 BcParse prs;
6374 char *str;
6375
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006376 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006377 str = *bc_program_str(sidx);
6378 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006379 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006380
6381 sv_input_fp = G.input_fp;
6382 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6383 s = zdc_parse_exprs_until_eof(&prs);
6384 G.input_fp = sv_input_fp;
6385
Gavin Howard01055ba2018-11-03 11:00:21 -06006386 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006387 if (prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006388 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006389 err:
6390 bc_parse_free(&prs);
6391 bc_vec_pop_all(&f->code);
6392 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006393 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006394 bc_parse_push(&prs, DC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006395 bc_parse_free(&prs);
6396 }
6397
Denys Vlasenko24e41942018-12-21 23:01:26 +01006398 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 ip.func = fidx;
6400
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006401 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006402 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006404 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006405 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006406 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006407 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006408}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006409#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006410#endif // ENABLE_DC
6411
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006412static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006413{
Gavin Howard01055ba2018-11-03 11:00:21 -06006414 BcResult res;
6415 unsigned long val;
6416
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006417 res.t = inst - XC_INST_IBASE + BC_RESULT_IBASE;
6418 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006419 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006420 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006421 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006422 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006423 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006424
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006425 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006426 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006427 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006428}
6429
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006430static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006431{
Gavin Howard01055ba2018-11-03 11:00:21 -06006432 BcResult r, *ptr;
6433 BcNum *num;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006434 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006435 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006436 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006437
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006438 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006439 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6440 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006441 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006442 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006443
Denys Vlasenko24e41942018-12-21 23:01:26 +01006444 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 -06006445 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006446#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006447 case BC_INST_JUMP_ZERO: {
6448 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006449 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006450 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006451 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006452 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006453 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006454 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006455 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006456 break;
6457 }
6458 // else: fall through
6459 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006460 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006461 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006462 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006463 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006464 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006465 break;
6466 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006467 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006468 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006469 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006470 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006471 case BC_INST_INC_PRE:
6472 case BC_INST_DEC_PRE:
6473 case BC_INST_INC_POST:
6474 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006475 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006476 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006478 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006479 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006480 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006481 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006482 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006483 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006484 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006485 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006486 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006487 case XC_INST_BOOL_OR:
6488 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006489#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006490 case XC_INST_REL_EQ:
6491 case XC_INST_REL_LE:
6492 case XC_INST_REL_GE:
6493 case XC_INST_REL_NE:
6494 case XC_INST_REL_LT:
6495 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006496 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006497 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006499 case XC_INST_READ:
6500 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006501 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006502 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006503 case XC_INST_VAR:
6504 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006505 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006506 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006507 case XC_INST_ARRAY_ELEM:
6508 case XC_INST_ARRAY:
6509 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006510 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006512#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006513 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006514 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006516 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006518#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006519 case XC_INST_IBASE:
6520 case XC_INST_OBASE:
6521 case XC_INST_SCALE:
6522 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006523 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006524 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006525 case XC_INST_SCALE_FUNC:
6526 case XC_INST_LENGTH:
6527 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006528 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006529 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006530 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006531 case XC_INST_NUM:
6532 dbg_exec("XC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006533 r.t = BC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006534 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006537 case XC_INST_POP:
6538 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006539 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006540 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006541 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006542 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006543 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006544 case XC_INST_PRINT:
6545 case XC_INST_PRINT_POP:
6546 case XC_INST_PRINT_STR:
6547 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006548 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006549 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006550 case XC_INST_STR:
6551 dbg_exec("XC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006552 r.t = BC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006553 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006554 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006555 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006556 case XC_INST_POWER:
6557 case XC_INST_MULTIPLY:
6558 case XC_INST_DIVIDE:
6559 case XC_INST_MODULUS:
6560 case XC_INST_PLUS:
6561 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006562 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006563 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006564 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006565 case XC_INST_BOOL_NOT:
6566 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006567 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006568 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006569 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006570 if (!bc_num_cmp(num, &G.prog.zero))
6571 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006572 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006573 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006575 case XC_INST_NEG:
6576 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006577 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006579#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006580 case BC_INST_ASSIGN_POWER:
6581 case BC_INST_ASSIGN_MULTIPLY:
6582 case BC_INST_ASSIGN_DIVIDE:
6583 case BC_INST_ASSIGN_MODULUS:
6584 case BC_INST_ASSIGN_PLUS:
6585 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006586#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006587 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006588 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006589 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006591#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006592 case DC_INST_POP_EXEC:
6593 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006594 bc_vec_pop(&G.prog.exestack);
6595 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006596 case DC_INST_MODEXP:
6597 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006598 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006599 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006600 case DC_INST_DIVMOD:
6601 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006602 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006603 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006604 case DC_INST_EXECUTE:
6605 case DC_INST_EXEC_COND:
6606 dbg_exec("DC_INST_EXEC[_COND]:");
6607 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006608 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006609 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006610 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006611 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006612 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006613 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006614 if (s) break;
6615 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006616 break;
6617 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006618 case DC_INST_CLEAR_STACK:
6619 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006620 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006621 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006622 case DC_INST_STACK_LEN:
6623 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006624 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006626 case DC_INST_DUPLICATE:
6627 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006628 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006629 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006630 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006631 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006632 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006634 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006635 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006636 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006637 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006638 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006639 ptr = bc_vec_item_rev(&G.prog.results, 0);
6640 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 memcpy(&r, ptr, sizeof(BcResult));
6642 memcpy(ptr, ptr2, sizeof(BcResult));
6643 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006644 break;
6645 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006646 case DC_INST_ASCIIFY:
6647 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006648 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006649 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006650 case DC_INST_PRINT_STREAM:
6651 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006652 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006654 case DC_INST_LOAD:
6655 case DC_INST_PUSH_VAR: {
6656 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006657 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006658 break;
6659 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006660 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006661 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006662 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006663 free(name);
6664 break;
6665 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006666 case DC_INST_QUIT:
6667 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006668 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006669 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006670 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006671 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006672 case DC_INST_NQUIT:
6673 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006674 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006675 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006676#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006677 read_updated_ip:
6678 // Instruction stack has changed, read new pointers
6679 ip = bc_vec_top(&G.prog.exestack);
6680 func = bc_program_func(ip->func);
6681 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006682 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006683 }
6684
Denys Vlasenkod38af482018-12-04 19:11:02 +01006685 if (s || G_interrupt) {
6686 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006687 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006688 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006689
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006690 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006691 }
6692
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006693 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006694}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006695#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006696
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006697static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006698{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006699 char *lenv;
6700 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006701
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006702 lenv = getenv(var);
6703 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006704 if (!lenv) return len;
6705
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006706 len = bb_strtou(lenv, NULL, 10) - 1;
6707 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006708 len = BC_NUM_PRINT_WIDTH;
6709
6710 return len;
6711}
6712
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006713static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006714{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006715 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006716
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006717 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006718 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006719 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006720
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006721 while (G.prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006722 BcInstPtr *ip;
6723 BcFunc *f;
6724
Denys Vlasenko39287e02018-12-22 02:23:08 +01006725 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 +01006726 if (IS_BC) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006727// FIXME: "eating" of stmt delimiters is coded inconsistently
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006728// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6729// which causes bugs such as "print 1 print 2" erroneously accepted,
6730// or "print 1 else 2" detecting parse error only after executing
6731// "print 1" part.
6732 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6733 } else {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006734 // Most of dc parsing assumes all whitespace,
6735 // including '\n', is eaten.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01006736 while (G.prs.l.t.t == XC_LEX_NLINE) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006737 s = zbc_lex_next(&G.prs.l);
6738 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006739 if (G.prs.l.t.t == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006740 goto done;
6741 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006742 IF_DC(s = zdc_parse_expr(&G.prs));
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006743 }
6744 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006745 err:
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006746 bc_parse_reset(&G.prs); // includes bc_program_reset()
6747 RETURN_STATUS(BC_STATUS_FAILURE);
6748 }
6749
Denys Vlasenko39287e02018-12-22 02:23:08 +01006750 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006751 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006752 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006753 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006754 break;
6755 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006756
6757 ip = (void*)G.prog.exestack.v;
6758#if SANITY_CHECKS
6759 if (G.prog.exestack.len != 1) // should have only main's IP
6760 bb_error_msg_and_die("BUG:call stack");
6761 if (ip->func != BC_PROG_MAIN)
6762 bb_error_msg_and_die("BUG:not MAIN");
6763#endif
6764 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006765 // bc discards strings, constants and code after each
6766 // top-level statement in the "main program".
6767 // This prevents "yes 1 | bc" from growing its memory
6768 // without bound. This can be done because data stack
6769 // is empty and thus can't hold any references to
6770 // strings or constants, there is no generated code
6771 // which can hold references (after we discard one
6772 // we just executed). Code of functions can have references,
6773 // but bc stores function strings/constants in per-function
6774 // storage.
6775 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006776#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006777 if (G.prog.results.len != 0) // should be empty
6778 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006779#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006780 IF_BC(bc_vec_pop_all(&f->strs);)
6781 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006782 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006783 if (G.prog.results.len == 0
6784 && G.prog.vars.len == 0
6785 ) {
6786 // If stack is empty and no registers exist (TODO: or they are all empty),
6787 // we can get rid of accumulated strings and constants.
6788 // In this example dc process should not grow
6789 // its memory consumption with time:
6790 // yes 1pc | dc
6791 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6792 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6793 }
6794 // The code is discarded always (below), thus this example
6795 // should also not grow its memory consumption with time,
6796 // even though its data stack is not empty:
6797 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006798 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006799 // We drop generated and executed code for both bc and dc:
6800 bc_vec_pop_all(&f->code);
6801 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006802 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006803 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006804 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006805 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006806}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006807#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006808
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006809static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006810{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006811 // So far bc/dc have no way to include a file from another file,
6812 // therefore we know G.prog.file == NULL on entry
6813 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006814 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006815
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006816 G.prog.file = filename;
6817 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006818 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006819
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006820 do {
6821 s = zbc_vm_process("");
6822 // We do not stop looping on errors here if reading stdin.
6823 // Example: start interactive bc and enter "return".
6824 // It should say "'return' not in a function"
6825 // but should not exit.
6826 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006827 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006828 RETURN_STATUS(s);
6829}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006830#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006831
6832static BC_STATUS zbc_vm_file(const char *file)
6833{
6834 BcStatus s;
6835 FILE *fp;
6836
6837 fp = xfopen_for_read(file);
6838 s = zbc_vm_execute_FILE(fp, file);
6839 fclose(fp);
6840
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006841 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006842}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006843#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006844
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006845#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006846static void bc_vm_info(void)
6847{
6848 printf("%s "BB_VER"\n"
6849 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6850 , applet_name);
6851}
6852
6853static void bc_args(char **argv)
6854{
6855 unsigned opts;
6856 int i;
6857
6858 GETOPT_RESET();
6859#if ENABLE_FEATURE_BC_LONG_OPTIONS
6860 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6861 "warn\0" No_argument "w"
6862 "version\0" No_argument "v"
6863 "standard\0" No_argument "s"
6864 "quiet\0" No_argument "q"
6865 "mathlib\0" No_argument "l"
6866 "interactive\0" No_argument "i"
6867 );
6868#else
6869 opts = option_mask32 |= getopt32(argv, "wvsqli");
6870#endif
6871 if (getenv("POSIXLY_CORRECT"))
6872 option_mask32 |= BC_FLAG_S;
6873
6874 if (opts & BC_FLAG_V) {
6875 bc_vm_info();
6876 exit(0);
6877 }
6878
6879 for (i = optind; argv[i]; ++i)
6880 bc_vec_push(&G.files, argv + i);
6881}
6882
6883static void bc_vm_envArgs(void)
6884{
6885 BcVec v;
6886 char *buf;
6887 char *env_args = getenv("BC_ENV_ARGS");
6888
6889 if (!env_args) return;
6890
6891 G.env_args = xstrdup(env_args);
6892 buf = G.env_args;
6893
6894 bc_vec_init(&v, sizeof(char *), NULL);
6895
6896 while (*(buf = skip_whitespace(buf)) != '\0') {
6897 bc_vec_push(&v, &buf);
6898 buf = skip_non_whitespace(buf);
6899 if (!*buf)
6900 break;
6901 *buf++ = '\0';
6902 }
6903
6904 // NULL terminate, and pass argv[] so that first arg is argv[1]
6905 if (sizeof(int) == sizeof(char*)) {
6906 bc_vec_push(&v, &const_int_0);
6907 } else {
6908 static char *const nullptr = NULL;
6909 bc_vec_push(&v, &nullptr);
6910 }
6911 bc_args(((char **)v.v) - 1);
6912
6913 bc_vec_free(&v);
6914}
6915
6916static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006917 "scale=20"
6918"\n" "define e(x){"
6919"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006920////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6921//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6922//e(-.998896): GNU:.36828580434569428695
6923// above code:.36828580434569428696
6924// actual value:.3682858043456942869594...
6925// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006926"\n" "b=ibase"
6927"\n" "ibase=A"
6928"\n" "if(x<0){"
6929"\n" "n=1"
6930"\n" "x=-x"
6931"\n" "}"
6932"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006933"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006934"\n" "scale=scale(x)+1"
6935"\n" "while(x>1){"
6936"\n" "d+=1"
6937"\n" "x/=2"
6938"\n" "scale+=1"
6939"\n" "}"
6940"\n" "scale=r"
6941"\n" "r=x+1"
6942"\n" "p=x"
6943"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006944"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006945"\n" "p*=x"
6946"\n" "f*=i"
6947"\n" "v=p/f"
6948"\n" "r+=v"
6949"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006950"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006951"\n" "scale=s"
6952"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006953"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006954"\n" "return(r/1)"
6955"\n" "}"
6956"\n" "define l(x){"
6957"\n" "auto b,s,r,p,a,q,i,v"
6958"\n" "b=ibase"
6959"\n" "ibase=A"
6960"\n" "if(x<=0){"
6961"\n" "r=(1-10^scale)/1"
6962"\n" "ibase=b"
6963"\n" "return(r)"
6964"\n" "}"
6965"\n" "s=scale"
6966"\n" "scale+=6"
6967"\n" "p=2"
6968"\n" "while(x>=2){"
6969"\n" "p*=2"
6970"\n" "x=sqrt(x)"
6971"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006972"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006973"\n" "p*=2"
6974"\n" "x=sqrt(x)"
6975"\n" "}"
6976"\n" "r=a=(x-1)/(x+1)"
6977"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006978"\n" "v=1"
6979"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006980"\n" "a*=q"
6981"\n" "v=a/i"
6982"\n" "r+=v"
6983"\n" "}"
6984"\n" "r*=p"
6985"\n" "scale=s"
6986"\n" "ibase=b"
6987"\n" "return(r/1)"
6988"\n" "}"
6989"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006990"\n" "auto b,s,r,a,q,i"
6991"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006992"\n" "b=ibase"
6993"\n" "ibase=A"
6994"\n" "s=scale"
6995"\n" "scale=1.1*s+2"
6996"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006997"\n" "scale=0"
6998"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006999"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007000"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007001"\n" "scale=s+2"
7002"\n" "r=a=x"
7003"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007004"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007005"\n" "a*=q/(i*(i-1))"
7006"\n" "r+=a"
7007"\n" "}"
7008"\n" "scale=s"
7009"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007010"\n" "return(r/1)"
7011"\n" "}"
7012"\n" "define c(x){"
7013"\n" "auto b,s"
7014"\n" "b=ibase"
7015"\n" "ibase=A"
7016"\n" "s=scale"
7017"\n" "scale*=1.2"
7018"\n" "x=s(2*a(1)+x)"
7019"\n" "scale=s"
7020"\n" "ibase=b"
7021"\n" "return(x/1)"
7022"\n" "}"
7023"\n" "define a(x){"
7024"\n" "auto b,s,r,n,a,m,t,f,i,u"
7025"\n" "b=ibase"
7026"\n" "ibase=A"
7027"\n" "n=1"
7028"\n" "if(x<0){"
7029"\n" "n=-1"
7030"\n" "x=-x"
7031"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007032"\n" "if(scale<65){"
7033"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7034"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007035"\n" "}"
7036"\n" "s=scale"
7037"\n" "if(x>.2){"
7038"\n" "scale+=5"
7039"\n" "a=a(.2)"
7040"\n" "}"
7041"\n" "scale=s+3"
7042"\n" "while(x>.2){"
7043"\n" "m+=1"
7044"\n" "x=(x-.2)/(1+.2*x)"
7045"\n" "}"
7046"\n" "r=u=x"
7047"\n" "f=-x*x"
7048"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007049"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007050"\n" "u*=f"
7051"\n" "t=u/i"
7052"\n" "r+=t"
7053"\n" "}"
7054"\n" "scale=s"
7055"\n" "ibase=b"
7056"\n" "return((m*a+r)/n)"
7057"\n" "}"
7058"\n" "define j(n,x){"
7059"\n" "auto b,s,o,a,i,v,f"
7060"\n" "b=ibase"
7061"\n" "ibase=A"
7062"\n" "s=scale"
7063"\n" "scale=0"
7064"\n" "n/=1"
7065"\n" "if(n<0){"
7066"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007067"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007068"\n" "}"
7069"\n" "a=1"
7070"\n" "for(i=2;i<=n;++i)a*=i"
7071"\n" "scale=1.5*s"
7072"\n" "a=(x^n)/2^n/a"
7073"\n" "r=v=1"
7074"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007075"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007076"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007077"\n" "v=v*f/i/(n+i)"
7078"\n" "r+=v"
7079"\n" "}"
7080"\n" "scale=s"
7081"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007082"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007083"\n" "return(a*r/1)"
7084"\n" "}"
7085};
7086#endif // ENABLE_BC
7087
Denys Vlasenko19f11072018-12-12 22:48:19 +01007088static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007089{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007090 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007091 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007092 size_t i;
7093
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007094#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007095 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007096 // We know that internal library is not buggy,
7097 // thus error checking is normally disabled.
7098# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007099 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007100 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007101 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007102 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007103#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007104
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007105 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007106 fname = (void*)G.files.v;
7107 for (i = 0; i < G.files.len; i++) {
7108 s = zbc_vm_file(*fname++);
7109 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7110 // Debug config, non-interactive mode:
7111 // return all the way back to main.
7112 // Non-debug builds do not come here
7113 // in non-interactive mode, they exit.
7114 RETURN_STATUS(s);
7115 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007116 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007117
Denys Vlasenko91cde952018-12-10 20:56:08 +01007118 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007119 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007120
Denys Vlasenko19f11072018-12-12 22:48:19 +01007121 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007122}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007123#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007124
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007125#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007126static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007127{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007128 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007129 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007130 bc_vec_free(&G.prog.vars);
7131 bc_vec_free(&G.prog.var_map);
7132 bc_vec_free(&G.prog.arrs);
7133 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007134 IF_DC(bc_vec_free(&G.prog.strs);)
7135 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007136 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007137 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007138 IF_BC(bc_num_free(&G.prog.last);)
7139 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007140 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007141 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007142}
7143
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007144static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007145{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007146 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007147 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007148 bc_parse_free(&G.prs);
7149 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007150}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007151#endif
7152
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007153static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007154{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007155 BcInstPtr ip;
7156
Denys Vlasenko82269122018-12-14 16:30:56 +01007157 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007158 memset(&ip, 0, sizeof(BcInstPtr));
7159
Denys Vlasenko82269122018-12-14 16:30:56 +01007160 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007161 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007162 G.prog.ob_t = 10;
7163
Denys Vlasenko503faf92018-12-20 16:24:18 +01007164 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7165 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007166
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007167 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007168 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007169
Denys Vlasenko503faf92018-12-20 16:24:18 +01007170 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7171 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007172
7173 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007174 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007175
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007176 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007177 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007178 // a valid function name (and be short)
7179 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007180 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007181 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007182 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007183 bc_program_add_fn();
7184 bc_program_add_fn();
7185 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007186
7187 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007188 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007189
7190 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007191 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007192
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007193 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7194 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007195 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007196 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7197 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007198
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007199 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007200}
Gavin Howard01055ba2018-11-03 11:00:21 -06007201
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007202static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007203{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007204#if ENABLE_FEATURE_EDITING
7205 G.line_input_state = new_line_input_t(DO_HISTORY);
7206#endif
7207 G.prog.len = bc_vm_envLen(env_len);
7208
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007209 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007210 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007211 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007212 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007213
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007214//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7215//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007216 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007217#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007218 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007219 // With SA_RESTART, most system calls will restart
7220 // (IOW: they won't fail with EINTR).
7221 // In particular, this means ^C won't cause
7222 // stdout to get into "error state" if SIGINT hits
7223 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007224 //
7225 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007226 // will only be handled after [Enter] since read()
7227 // from stdin is not interrupted by ^C either,
7228 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007229 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007230 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7231
7232 // Without SA_RESTART, this exhibits a bug:
7233 // "while (1) print 1" and try ^C-ing it.
7234 // Intermittently, instead of returning to input line,
7235 // you'll get "output error: Interrupted system call"
7236 // and exit.
7237 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007238#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007239 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007240 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007241 return 0; // "not a tty"
7242}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007243
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007244static BcStatus bc_vm_run(void)
7245{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007246 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007247#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007248 if (G_exiting) // it was actually "halt" or "quit"
7249 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007250 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007251# if ENABLE_FEATURE_EDITING
7252 free_line_input_t(G.line_input_state);
7253# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007254 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007255#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007256 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007257 return st;
7258}
7259
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007260#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007261int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007262int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007263{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007264 int is_tty;
7265
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007266 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007267
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007268 is_tty = bc_vm_init("BC_LINE_LENGTH");
7269
7270 bc_args(argv);
7271
7272 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7273 bc_vm_info();
7274
7275 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007276}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007277#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007278
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007279#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007280int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007281int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007282{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007283 int noscript;
7284
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007285 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007286
7287 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7288 // 1 char wider than bc from the same package.
7289 // Both default width, and xC_LINE_LENGTH=N are wider:
7290 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007291 // |1234\ |
7292 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007293 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007294 // |123\ |
7295 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007296 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007297 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007298
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007299 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7300 noscript = BC_FLAG_I;
7301 for (;;) {
7302 int n = getopt(argc, argv, "e:f:x");
7303 if (n <= 0)
7304 break;
7305 switch (n) {
7306 case 'e':
7307 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007308 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007309 if (n) return n;
7310 break;
7311 case 'f':
7312 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007313 n = zbc_vm_file(optarg);
7314 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007315 break;
7316 case 'x':
7317 option_mask32 |= DC_FLAG_X;
7318 break;
7319 default:
7320 bb_show_usage();
7321 }
7322 }
7323 argv += optind;
7324
7325 while (*argv) {
7326 noscript = 0;
7327 bc_vec_push(&G.files, argv++);
7328 }
7329
7330 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7331
7332 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007333}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007334#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007335
7336#endif // not DC_SMALL