blob: 8fa312a2b0b987c7c37c2d077abf48ec45847fef [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.
Gavin Howardfa495ce2018-12-18 10:03:14 -07004 * Adapted from https://github.com/gavinhoward/bc
5 * Original code copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06006 */
7//config:config BC
8//config: bool "bc (45 kb; 49 kb when combined with dc)"
9//config: default y
10//config: help
11//config: bc is a command-line, arbitrary-precision calculator with a
12//config: Turing-complete language. See the GNU bc manual
13//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010014//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060015//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010016//config: This bc has five differences to the GNU bc:
17//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060018//config: 2) Arrays are copied before being passed as arguments to
19//config: functions. This behavior is required by the bc spec.
20//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010021//config: the number of elements in the array. This prints "1":
22//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060023//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010024//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060025//config: allows POSIX-compliant scripts to work while somewhat
26//config: preserving expected behavior (versus C) and making parsing
27//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010028//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060029//config:
30//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060031//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010032//config: -q --quiet don't print version and copyright
33//config: -s --standard error if any non-POSIX extensions are used
34//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060035//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010036//config: s(expr) sine in radians
37//config: c(expr) cosine in radians
38//config: a(expr) arctangent, returning radians
39//config: l(expr) natural log
40//config: e(expr) raises e to the power of expr
41//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060042//config:
43//config:config DC
44//config: bool "dc (38 kb; 49 kb when combined with bc)"
45//config: default y
46//config: help
47//config: dc is a reverse-polish notation command-line calculator which
48//config: supports unlimited precision arithmetic. See the FreeBSD man page
49//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010050//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060051//config:
52//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010053//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060054//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010055//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060056//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010057//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060058//config: 4) Like the FreeBSD dc, this dc supports extended registers.
59//config: However, they are implemented differently. When it encounters
60//config: whitespace where a register should be, it skips the whitespace.
61//config: If the character following is not a lowercase letter, an error
62//config: is issued. Otherwise, the register name is parsed by the
63//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010064//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060065//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010066//config: whitespace. Examples:
67//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060068//config: Also note that, like the FreeBSD dc, extended registers are not
69//config: allowed unless the "-x" option is given.
70//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010071//config:config FEATURE_DC_SMALL
72//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
73//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010074//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010075//config:
76//config:config FEATURE_DC_LIBM
77//config: bool "Enable power and exp functions (requires libm)"
78//config: default y
79//config: depends on FEATURE_DC_SMALL
80//config: help
81//config: Enable power and exp functions.
82//config: NOTE: This will require libm to be present for linking.
83//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060084//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010085//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060086//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010087//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060088//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010089//config: Enable interactive mode: when started on a tty,
90//config: ^C interrupts execution and returns to command line,
91//config: errors also return to command line instead of exiting,
92//config: line editing with history is available.
93//config:
94//config: With this option off, input can still be taken from tty,
95//config: but all errors are fatal, ^C is fatal,
96//config: tty is treated exactly the same as any other
97//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060098//config:
99//config:config FEATURE_BC_LONG_OPTIONS
100//config: bool "Enable bc/dc long options"
101//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100102//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600103//config: help
104//config: Enable long options for bc and dc.
105
106//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
107//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
108
109//kbuild:lib-$(CONFIG_BC) += bc.o
110//kbuild:lib-$(CONFIG_DC) += bc.o
111
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100112//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600113//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100114//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600115//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100116//usage:#define bc_full_usage "\n"
117//usage: "\nArbitrary precision calculator"
118//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100119///////: "\n -i Interactive" - has no effect for now
120//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100121//usage: "\n -l Load standard math library"
122//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100123//usage: "\n -w Warn if extensions are used"
124///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100125//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100126//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600127//usage:
128//usage:#define bc_example_usage
129//usage: "3 + 4.129\n"
130//usage: "1903 - 2893\n"
131//usage: "-129 * 213.28935\n"
132//usage: "12 / -1932\n"
133//usage: "12 % 12\n"
134//usage: "34 ^ 189\n"
135//usage: "scale = 13\n"
136//usage: "ibase = 2\n"
137//usage: "obase = A\n"
138//usage:
139//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100140//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600141//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100142//usage:#define dc_full_usage "\n"
143//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100144//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100145//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100146//usage: "\nf - print entire stack"
147//usage: "\nk - pop the value and set the precision"
148//usage: "\ni - pop the value and set input radix"
149//usage: "\no - pop the value and set output radix"
150//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600151//usage:
152//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100153//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600154//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100155//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600156//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100157//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600158//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100159//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600160//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100161//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600162//usage: "64\n"
163
164#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100165#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600166
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100167#if ENABLE_FEATURE_DC_SMALL
168# include "dc.c"
169#else
170
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100171#define DEBUG_LEXER 0
172#define DEBUG_COMPILE 0
173#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100174// This can be left enabled for production as well:
175#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100176
177#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100178static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100179#define dbg_lex(...) \
180 do { \
181 fprintf(stderr, "%*s", lex_indent, ""); \
182 bb_error_msg(__VA_ARGS__); \
183 } while (0)
184#define dbg_lex_enter(...) \
185 do { \
186 dbg_lex(__VA_ARGS__); \
187 lex_indent++; \
188 } while (0)
189#define dbg_lex_done(...) \
190 do { \
191 lex_indent--; \
192 dbg_lex(__VA_ARGS__); \
193 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100194#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100195# define dbg_lex(...) ((void)0)
196# define dbg_lex_enter(...) ((void)0)
197# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100198#endif
199
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100200#if DEBUG_COMPILE
201# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
202#else
203# define dbg_compile(...) ((void)0)
204#endif
205
Denys Vlasenko99b37622018-12-15 20:06:59 +0100206#if DEBUG_EXEC
207# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
208#else
209# define dbg_exec(...) ((void)0)
210#endif
211
Gavin Howard01055ba2018-11-03 11:00:21 -0600212typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100213 BC_STATUS_SUCCESS = 0,
214 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100215 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600216} BcStatus;
217
Gavin Howard01055ba2018-11-03 11:00:21 -0600218#define BC_VEC_INVALID_IDX ((size_t) -1)
219#define BC_VEC_START_CAP (1 << 5)
220
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100221typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600222
223typedef struct BcVec {
224 char *v;
225 size_t len;
226 size_t cap;
227 size_t size;
228 BcVecFree dtor;
229} BcVec;
230
Gavin Howard01055ba2018-11-03 11:00:21 -0600231typedef signed char BcDig;
232
233typedef struct BcNum {
234 BcDig *restrict num;
235 size_t rdx;
236 size_t len;
237 size_t cap;
238 bool neg;
239} BcNum;
240
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100241#define BC_NUM_MAX_IBASE ((unsigned long) 16)
242// larger value might speed up BIGNUM calculations a bit:
243#define BC_NUM_DEF_SIZE (16)
244#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600245
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100246#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600247
Gavin Howard01055ba2018-11-03 11:00:21 -0600248typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100249#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600250 BC_INST_INC_PRE,
251 BC_INST_DEC_PRE,
252 BC_INST_INC_POST,
253 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100254#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100255 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600256
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100257 XC_INST_REL_EQ, // should
258 XC_INST_REL_LE, // match
259 XC_INST_REL_GE, // LEX
260 XC_INST_REL_NE, // constants
261 XC_INST_REL_LT, // for
262 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600263
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100264 XC_INST_POWER, // operations
265 XC_INST_MULTIPLY, // |
266 XC_INST_DIVIDE, // |
267 XC_INST_MODULUS, // |
268 XC_INST_PLUS, // |
269 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600270
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100271 XC_INST_BOOL_NOT, // |
272 XC_INST_BOOL_OR, // |
273 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100274#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100275 BC_INST_ASSIGN_POWER, // |
276 BC_INST_ASSIGN_MULTIPLY,// |
277 BC_INST_ASSIGN_DIVIDE, // |
278 BC_INST_ASSIGN_MODULUS, // |
279 BC_INST_ASSIGN_PLUS, // |
280 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100281#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100282 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600283
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100284 XC_INST_NUM,
285 XC_INST_VAR,
286 XC_INST_ARRAY_ELEM,
287 XC_INST_ARRAY,
288 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100289
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100290 XC_INST_IBASE, // order of these constans should match other enums
291 XC_INST_OBASE, // order of these constans should match other enums
292 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100293 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100294 XC_INST_LENGTH,
295 XC_INST_READ,
296 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600297
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100298 XC_INST_PRINT,
299 XC_INST_PRINT_POP,
300 XC_INST_STR,
301 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600302
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100303#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100304 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600305 BC_INST_JUMP,
306 BC_INST_JUMP_ZERO,
307
308 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600309 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100310#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100311 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600312
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100313 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100314#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100315 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600316
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100317 DC_INST_MODEXP,
318 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600319
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100320 DC_INST_EXECUTE,
321 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600322
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100323 DC_INST_ASCIIFY,
324 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600325
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100326 DC_INST_PRINT_STACK,
327 DC_INST_CLEAR_STACK,
328 DC_INST_STACK_LEN,
329 DC_INST_DUPLICATE,
330 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600331
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100332 DC_INST_LOAD,
333 DC_INST_PUSH_VAR,
334 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600335
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100336 DC_INST_QUIT,
337 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600338
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100339 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600341} BcInst;
342
343typedef struct BcId {
344 char *name;
345 size_t idx;
346} BcId;
347
348typedef struct BcFunc {
349 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100350 IF_BC(BcVec labels;)
351 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100352 IF_BC(BcVec strs;)
353 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100354 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600355} BcFunc;
356
357typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100358 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600359
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100360 XC_RESULT_VAR,
361 XC_RESULT_ARRAY_ELEM,
362 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600363
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100364 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600365
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100366 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
367 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
368 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
369 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100370 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100371 XC_RESULT_CONSTANT,
372 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600373} BcResultType;
374
375typedef union BcResultData {
376 BcNum n;
377 BcVec v;
378 BcId id;
379} BcResultData;
380
381typedef struct BcResult {
382 BcResultType t;
383 BcResultData d;
384} BcResult;
385
386typedef struct BcInstPtr {
387 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100388 size_t inst_idx;
389 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600390} BcInstPtr;
391
Gavin Howard01055ba2018-11-03 11:00:21 -0600392typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100393 XC_LEX_EOF,
394 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600395
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100396 XC_LEX_NLINE,
397 XC_LEX_WHITESPACE,
398 XC_LEX_STR,
399 XC_LEX_NAME,
400 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100401
Denys Vlasenko69560f42018-12-24 14:14:23 +0100402 XC_LEX_1st_op,
403 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100404
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100405 XC_LEX_OP_REL_EQ, // should
406 XC_LEX_OP_REL_LE, // match
407 XC_LEX_OP_REL_GE, // INST
408 XC_LEX_OP_REL_NE, // constants
409 XC_LEX_OP_REL_LT, // for
410 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100411
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100412 XC_LEX_OP_POWER, // operations
413 XC_LEX_OP_MULTIPLY, // |
414 XC_LEX_OP_DIVIDE, // |
415 XC_LEX_OP_MODULUS, // |
416 XC_LEX_OP_PLUS, // |
417 XC_LEX_OP_MINUS, // |
418 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100419#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100420 BC_LEX_OP_BOOL_NOT, // |
421 BC_LEX_OP_BOOL_OR, // |
422 BC_LEX_OP_BOOL_AND, // |
423
424 BC_LEX_OP_ASSIGN_POWER, // |
425 BC_LEX_OP_ASSIGN_MULTIPLY, // |
426 BC_LEX_OP_ASSIGN_DIVIDE, // |
427 BC_LEX_OP_ASSIGN_MODULUS, // |
428 BC_LEX_OP_ASSIGN_PLUS, // |
429 BC_LEX_OP_ASSIGN_MINUS, // |
430
431 BC_LEX_OP_ASSIGN, // V
432
Gavin Howard01055ba2018-11-03 11:00:21 -0600433 BC_LEX_OP_INC,
434 BC_LEX_OP_DEC,
435
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100436 BC_LEX_LPAREN, // () are 0x28 and 0x29
437 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600438
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100439 BC_LEX_LBRACKET, // [] are 0x5B and 5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600440 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100441 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600442
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100443 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600444 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100445 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600446
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100447 BC_LEX_KEY_1st_keyword,
448 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600449 BC_LEX_KEY_BREAK,
450 BC_LEX_KEY_CONTINUE,
451 BC_LEX_KEY_DEFINE,
452 BC_LEX_KEY_ELSE,
453 BC_LEX_KEY_FOR,
454 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100455 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
456 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
457 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600458 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100459 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600460 BC_LEX_KEY_LENGTH,
461 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600462 BC_LEX_KEY_PRINT,
463 BC_LEX_KEY_QUIT,
464 BC_LEX_KEY_READ,
465 BC_LEX_KEY_RETURN,
466 BC_LEX_KEY_SCALE,
467 BC_LEX_KEY_SQRT,
468 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100469#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600470
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100471#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100472 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
473 DC_LEX_OP_ASSIGN,
474
475 DC_LEX_LPAREN,
476 DC_LEX_SCOLON,
477 DC_LEX_READ,
478 DC_LEX_IBASE,
479 DC_LEX_SCALE,
480 DC_LEX_OBASE,
481 DC_LEX_LENGTH,
482 DC_LEX_PRINT,
483 DC_LEX_QUIT,
484 DC_LEX_SQRT,
485 DC_LEX_LBRACE,
486
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100487 DC_LEX_EQ_NO_REG,
488 DC_LEX_OP_MODEXP,
489 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600490
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100491 DC_LEX_COLON,
492 DC_LEX_ELSE,
493 DC_LEX_EXECUTE,
494 DC_LEX_PRINT_STACK,
495 DC_LEX_CLEAR_STACK,
496 DC_LEX_STACK_LEVEL,
497 DC_LEX_DUPLICATE,
498 DC_LEX_SWAP,
499 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600500
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100501 DC_LEX_ASCIIFY,
502 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600503
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100504 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
505 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
506 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
507 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
508 DC_LEX_LOAD,
509 DC_LEX_LOAD_POP,
510 DC_LEX_STORE_PUSH,
511 DC_LEX_PRINT_POP,
512 DC_LEX_NQUIT,
513 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100514#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600515} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100516// must match order of BC_LEX_KEY_foo etc above
517#if ENABLE_BC
518struct BcLexKeyword {
519 char name8[8];
520};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100521#define BC_LEX_KW_ENTRY(a, b) \
522 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100523static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100524 BC_LEX_KW_ENTRY("auto" , 1), // 0
525 BC_LEX_KW_ENTRY("break" , 1), // 1
526 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
527 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100528 BC_LEX_KW_ENTRY("else" , 0), // 4
529 BC_LEX_KW_ENTRY("for" , 1), // 5
530 BC_LEX_KW_ENTRY("halt" , 0), // 6
531 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100532 BC_LEX_KW_ENTRY("obase" , 1), // 8
533 BC_LEX_KW_ENTRY("if" , 1), // 9
534 BC_LEX_KW_ENTRY("last" , 0), // 10
535 BC_LEX_KW_ENTRY("length" , 1), // 11
536 BC_LEX_KW_ENTRY("limits" , 0), // 12
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100537 BC_LEX_KW_ENTRY("print" , 0), // 13
538 BC_LEX_KW_ENTRY("quit" , 1), // 14
539 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100540 BC_LEX_KW_ENTRY("return" , 1), // 16
541 BC_LEX_KW_ENTRY("scale" , 1), // 17
542 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
543 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100544};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100545#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100547#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100548#define STRING_if (bc_lex_kws[9].name8)
549#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100550enum {
551 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100552 | (1 << 0) // 0
553 | (1 << 1) // 1
554 | (0 << 2) // 2
555 | (1 << 3) // 3
556 | (0 << 4) // 4
557 | (1 << 5) // 5
558 | (0 << 6) // 6
559 | (1 << 7) // 7
560 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100561 | (1 << 9) // 9
562 | (0 << 10) // 10
563 | (1 << 11) // 11
564 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100565 | (0 << 13) // 13
566 | (1 << 14) // 14
567 | (0 << 15) // 15
568 | (1 << 16) // 16
569 | (1 << 17) // 17
570 | (1 << 18) // 18
571 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100572};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100573#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100574
575// This is a bit array that corresponds to token types. An entry is
576// true if the token is valid in an expression, false otherwise.
577// Used to figure out when expr parsing should stop *without error message*
578// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100579// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100580// by later processing.
581enum {
582#define EXBITS(a,b,c,d,e,f,g,h) \
583 ((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 +0100584 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100585 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100586 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
587 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100588 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
589 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100590 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
591 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
592 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100593#undef EXBITS
594};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100595static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100596{
597#if ULONG_MAX > 0xffffffff
598 // 64-bit version (will not work correctly for 32-bit longs!)
599 return BC_PARSE_EXPRS_BITS & (1UL << i);
600#else
601 // 32-bit version
602 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
603 if (i >= 32) {
604 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
605 i &= 31;
606 }
607 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100608#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100609}
610
611// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100612// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100613static const uint8_t bc_parse_ops[] = {
614#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100615 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100616 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100617 OP(2, false), // pow
618 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
619 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100620 OP(1, false), // not
621 OP(7, true ), OP( 7, true ), // or and
622 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
623 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100624 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100625#undef OP
626};
627#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
628#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
629#endif // ENABLE_BC
630
631#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100632static const //BcLexType - should be this type
633uint8_t
634dc_char_to_LEX[] = {
635 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100636 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100637 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100638 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100639 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100640 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100641 /* 0123456789 */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
644 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100645 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100646 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
647 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100648 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100649 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
650 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100651 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100652 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
653 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100654 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100655 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
656 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100657 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100658 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100659 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100660 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100661 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100662 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
663 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100664 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100665 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100666 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100667 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100668 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
669 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100670 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100671 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100672 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100673 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100674};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100675static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100676int8_t
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100677dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
678 XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY
679 XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS
680 XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100681 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
682 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
683 XC_INST_REL_GT, // DC_LEX_LPAREN
684 DC_INST_INVALID, // DC_LEX_SCOLON
685 DC_INST_INVALID, // DC_LEX_READ
686 XC_INST_IBASE, // DC_LEX_IBASE
687 XC_INST_SCALE, // DC_LEX_SCALE
688 XC_INST_OBASE, // DC_LEX_OBASE
689 XC_INST_LENGTH, // DC_LEX_LENGTH
690 XC_INST_PRINT, // DC_LEX_PRINT
691 DC_INST_QUIT, // DC_LEX_QUIT
692 XC_INST_SQRT, // DC_LEX_SQRT
693 XC_INST_REL_GE, // DC_LEX_LBRACE
694 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
695 DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD
696 DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE
697 DC_INST_EXECUTE, // EXECUTE
698 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK
699 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE
700 DC_INST_SWAP, XC_INST_POP, // SWAP POP
701 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM
702 DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE
703 DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD
704 DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH
705 XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT
706 XC_INST_SCALE_FUNC, // SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100707 // DC_INST_INVALID in this table either means that corresponding LEX
708 // is not possible for dc, or that it does not compile one-to-one
709 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100710};
711#endif // ENABLE_DC
712
Gavin Howard01055ba2018-11-03 11:00:21 -0600713typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600714 const char *buf;
Denys Vlasenko26384542018-12-25 18:37:52 +0100715 const char *lex_next_at; // last lex_next() was called at this string
Gavin Howard01055ba2018-11-03 11:00:21 -0600716 size_t i;
717 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600718 size_t len;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +0100719 bool newline;
720 smallint lex; // was BcLexType
721 smallint lex_last; // was BcLexType
722 BcVec lex_buf;
Gavin Howard01055ba2018-11-03 11:00:21 -0600723} BcLex;
724
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100725#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600726
Denys Vlasenko39287e02018-12-22 02:23:08 +0100727#if ENABLE_BC
728# define BC_PARSE_REL (1 << 0)
729# define BC_PARSE_PRINT (1 << 1)
730# define BC_PARSE_ARRAY (1 << 2)
731# define BC_PARSE_NOCALL (1 << 3)
732#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600733
Gavin Howard01055ba2018-11-03 11:00:21 -0600734typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600735 BcLex l;
736
Denys Vlasenko503faf92018-12-20 16:24:18 +0100737 IF_BC(BcVec exits;)
738 IF_BC(BcVec conds;)
739 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600740
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100741 const char *filename;
742 FILE *input_fp;
743
Gavin Howard01055ba2018-11-03 11:00:21 -0600744 BcFunc *func;
745 size_t fidx;
746
Denys Vlasenko503faf92018-12-20 16:24:18 +0100747 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600748} BcParse;
749
Gavin Howard01055ba2018-11-03 11:00:21 -0600750typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600751 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100752 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600753
Denys Vlasenko503faf92018-12-20 16:24:18 +0100754 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600755 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600756 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600757
Gavin Howard01055ba2018-11-03 11:00:21 -0600758 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100759 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600760
761 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100762 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600763
764 BcVec vars;
765 BcVec var_map;
766
767 BcVec arrs;
768 BcVec arr_map;
769
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100770 IF_DC(BcVec strs;)
771 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600772
Gavin Howard01055ba2018-11-03 11:00:21 -0600773 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100774 IF_BC(BcNum one;)
775 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600776} BcProgram;
777
Gavin Howard01055ba2018-11-03 11:00:21 -0600778#define BC_PROG_MAIN (0)
779#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100780#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600781#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100782#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600783
784#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
785#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100786 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600787
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100788#define BC_FLAG_W (1 << 0)
789#define BC_FLAG_V (1 << 1)
790#define BC_FLAG_S (1 << 2)
791#define BC_FLAG_Q (1 << 3)
792#define BC_FLAG_L (1 << 4)
793#define BC_FLAG_I (1 << 5)
794#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600795
796#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
797#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
798
Denys Vlasenko64074a12018-12-07 15:50:14 +0100799#define BC_MAX_OBASE ((unsigned) 999)
800#define BC_MAX_DIM ((unsigned) INT_MAX)
801#define BC_MAX_SCALE ((unsigned) UINT_MAX)
802#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
803#define BC_MAX_NUM BC_MAX_STRING
804// Unused apart from "limits" message. Just show a "biggish number" there.
805//#define BC_MAX_NAME BC_MAX_STRING
806//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
807//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
808#define BC_MAX_NAME_STR "999999999"
809#define BC_MAX_EXP_STR "999999999"
810#define BC_MAX_VARS_STR "999999999"
811
812#define BC_MAX_OBASE_STR "999"
813
814#if INT_MAX == 2147483647
815# define BC_MAX_DIM_STR "2147483647"
816#elif INT_MAX == 9223372036854775807
817# define BC_MAX_DIM_STR "9223372036854775807"
818#else
819# error Strange INT_MAX
820#endif
821
822#if UINT_MAX == 4294967295
823# define BC_MAX_SCALE_STR "4294967295"
824# define BC_MAX_STRING_STR "4294967294"
825#elif UINT_MAX == 18446744073709551615
826# define BC_MAX_SCALE_STR "18446744073709551615"
827# define BC_MAX_STRING_STR "18446744073709551614"
828#else
829# error Strange UINT_MAX
830#endif
831#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600832
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100833struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100834 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100835 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600836
837 BcParse prs;
838 BcProgram prog;
839
Denys Vlasenko5318f812018-12-05 17:48:01 +0100840 // For error messages. Can be set to current parsed line,
841 // or [TODO] to current executing line (can be before last parsed one)
842 unsigned err_line;
843
Gavin Howard01055ba2018-11-03 11:00:21 -0600844 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100845 BcVec input_buffer;
Gavin Howard01055ba2018-11-03 11:00:21 -0600846
847 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100848
849#if ENABLE_FEATURE_EDITING
850 line_input_t *line_input_state;
851#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100852} FIX_ALIASING;
853#define G (*ptr_to_globals)
854#define INIT_G() do { \
855 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
856} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100857#define FREE_G() do { \
858 FREE_PTR_TO_GLOBALS(); \
859} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100860#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
861#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100862#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100863#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100864# define G_interrupt bb_got_signal
865# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100866#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100867# define G_interrupt 0
868# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100869#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100870#if ENABLE_FEATURE_CLEAN_UP
871# define G_exiting G.exiting
872#else
873# define G_exiting 0
874#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100875#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100876#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100877
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100878// In configurations where errors abort instead of propagating error
879// return code up the call chain, functions returning BC_STATUS
880// actually don't return anything, they always succeed and return "void".
881// A macro wrapper is provided, which makes this statement work:
882// s = zbc_func(...)
883// and makes it visible to the compiler that s is always zero,
884// allowing compiler to optimize dead code after the statement.
885//
886// To make code more readable, each such function has a "z"
887// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
888//
889#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100890# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100891# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100892# define IF_ERROR_RETURN_POSSIBLE(a) a
893# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100894# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100895# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100896#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100897# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100898# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100899# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
900# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100902# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100903#endif
904
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100905//
906// Utility routines
907//
Gavin Howard01055ba2018-11-03 11:00:21 -0600908
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100909static void fflush_and_check(void)
910{
911 fflush_all();
912 if (ferror(stdout) || ferror(stderr))
913 bb_perror_msg_and_die("output error");
914}
915
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100916#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100917#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100918do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100919 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100920 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100921 return BC_STATUS_FAILURE; \
922} while (0)
923#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100924#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100925#endif
926
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100927static void quit(void) NORETURN;
928static void quit(void)
929{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100930 if (ferror(stdin))
931 bb_perror_msg_and_die("input error");
932 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100933 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100934 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100935}
936
Denys Vlasenko5318f812018-12-05 17:48:01 +0100937static void bc_verror_msg(const char *fmt, va_list p)
938{
Denys Vlasenko82269122018-12-14 16:30:56 +0100939 const char *sv = sv; // for compiler
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100940 if (G.prs.filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100941 sv = applet_name;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100942 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100943 }
944 bb_verror_msg(fmt, p, NULL);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100945 if (G.prs.filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100946 free((char*)applet_name);
947 applet_name = sv;
948 }
949}
950
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100951static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100952{
953 va_list p;
954
955 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100956 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100957 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100958
Denys Vlasenkoec603182018-12-17 10:34:02 +0100959 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
960 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
961 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100962}
963
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100964#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100965static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100966{
967 va_list p;
968
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100969 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100970 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100971 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100972
973 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100974 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100975 va_end(p);
976
977 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100978 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100979 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
980
Denys Vlasenkoec603182018-12-17 10:34:02 +0100981 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100982 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100983 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100984}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100985#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100986#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100987
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100988// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
989// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100990// function must not have caller-cleaned parameters on stack.
991// Unfortunately, vararg function API does exactly that on most arches.
992// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100993static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100994{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100995 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100996}
997static ERRORFUNC int bc_error_bad_character(char c)
998{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100999 if (!c)
1000 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001001 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001002}
1003static ERRORFUNC int bc_error_bad_expression(void)
1004{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001005 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_bad_token(void)
1008{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
1011static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1012{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001013 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001014}
1015static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1016{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001017 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001018}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001019#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001020static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001021{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001022 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001023}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001024#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1025static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001026{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001027 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001028}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001029#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1030static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001031{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001032 RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; this is bad:", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001033}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001034#define zbc_POSIX_does_not_allow_bool_ops_this_is_bad(...) (zbc_POSIX_does_not_allow_bool_ops_this_is_bad(__VA_ARGS__) COMMA_SUCCESS)
1035static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001036{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001037 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001038}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001039#define zbc_POSIX_does_not_allow_empty_X_expression_in_for(...) (zbc_POSIX_does_not_allow_empty_X_expression_in_for(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001040#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001041
Gavin Howard01055ba2018-11-03 11:00:21 -06001042static void bc_vec_grow(BcVec *v, size_t n)
1043{
1044 size_t cap = v->cap * 2;
1045 while (cap < v->len + n) cap *= 2;
1046 v->v = xrealloc(v->v, v->size * cap);
1047 v->cap = cap;
1048}
1049
1050static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1051{
1052 v->size = esize;
1053 v->cap = BC_VEC_START_CAP;
1054 v->len = 0;
1055 v->dtor = dtor;
1056 v->v = xmalloc(esize * BC_VEC_START_CAP);
1057}
1058
Denys Vlasenko7d628012018-12-04 21:46:47 +01001059static void bc_char_vec_init(BcVec *v)
1060{
1061 bc_vec_init(v, sizeof(char), NULL);
1062}
1063
Gavin Howard01055ba2018-11-03 11:00:21 -06001064static void bc_vec_expand(BcVec *v, size_t req)
1065{
1066 if (v->cap < req) {
1067 v->v = xrealloc(v->v, v->size * req);
1068 v->cap = req;
1069 }
1070}
1071
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001072static void bc_vec_pop(BcVec *v)
1073{
1074 v->len--;
1075 if (v->dtor)
1076 v->dtor(v->v + (v->size * v->len));
1077}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001078
Gavin Howard01055ba2018-11-03 11:00:21 -06001079static void bc_vec_npop(BcVec *v, size_t n)
1080{
1081 if (!v->dtor)
1082 v->len -= n;
1083 else {
1084 size_t len = v->len - n;
1085 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1086 }
1087}
1088
Denys Vlasenko7d628012018-12-04 21:46:47 +01001089static void bc_vec_pop_all(BcVec *v)
1090{
1091 bc_vec_npop(v, v->len);
1092}
1093
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001094static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001095{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001096 size_t len = v->len;
1097 if (len >= v->cap) bc_vec_grow(v, 1);
1098 memmove(v->v + (v->size * len), data, v->size);
1099 v->len++;
1100 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001101}
1102
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001103// G.prog.results often needs "pop old operand, push result" idiom.
1104// Can do this without a few extra ops
1105static size_t bc_result_pop_and_push(const void *data)
1106{
1107 BcVec *v = &G.prog.results;
1108 char *last;
1109 size_t len = v->len - 1;
1110
1111 last = v->v + (v->size * len);
1112 if (v->dtor)
1113 v->dtor(last);
1114 memmove(last, data, v->size);
1115 return len;
1116}
1117
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001118static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001119{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001121}
1122
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001123static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001124{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001125 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001126 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001127 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001128}
1129
Gavin Howard01055ba2018-11-03 11:00:21 -06001130static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1131{
1132 if (idx == v->len)
1133 bc_vec_push(v, data);
1134 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001135 char *ptr;
1136
1137 if (v->len == v->cap) bc_vec_grow(v, 1);
1138
1139 ptr = v->v + v->size * idx;
1140
1141 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1142 memmove(ptr, data, v->size);
1143 }
1144}
1145
1146static void bc_vec_string(BcVec *v, size_t len, const char *str)
1147{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001148 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001149 bc_vec_expand(v, len + 1);
1150 memcpy(v->v, str, len);
1151 v->len = len;
1152
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001153 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001154}
1155
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001156#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001157static void bc_vec_concat(BcVec *v, const char *str)
1158{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001159 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001160
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001161 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001162
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001163 slen = strlen(str);
1164 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001165
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001166 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001167 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168
1169 v->len = len;
1170}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001171#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001172
1173static void *bc_vec_item(const BcVec *v, size_t idx)
1174{
1175 return v->v + v->size * idx;
1176}
1177
1178static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1179{
1180 return v->v + v->size * (v->len - idx - 1);
1181}
1182
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001183static void *bc_vec_top(const BcVec *v)
1184{
1185 return v->v + v->size * (v->len - 1);
1186}
1187
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001188static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001189{
1190 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001191 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001192 free(v->v);
1193}
1194
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001195static BcFunc* bc_program_func(size_t idx)
1196{
1197 return bc_vec_item(&G.prog.fns, idx);
1198}
1199// BC_PROG_MAIN is zeroth element, so:
1200#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1201
1202#if ENABLE_BC
1203static BcFunc* bc_program_current_func(void)
1204{
1205 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1206 BcFunc *func = bc_program_func(ip->func);
1207 return func;
1208}
1209#endif
1210
1211static char** bc_program_str(size_t idx)
1212{
1213#if ENABLE_BC
1214 if (IS_BC) {
1215 BcFunc *func = bc_program_current_func();
1216 return bc_vec_item(&func->strs, idx);
1217 }
1218#endif
1219 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1220}
1221
1222static char** bc_program_const(size_t idx)
1223{
1224#if ENABLE_BC
1225 if (IS_BC) {
1226 BcFunc *func = bc_program_current_func();
1227 return bc_vec_item(&func->consts, idx);
1228 }
1229#endif
1230 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1231}
1232
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001233static int bc_id_cmp(const void *e1, const void *e2)
1234{
1235 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1236}
1237
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001238static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001239{
1240 free(((BcId *) id)->name);
1241}
1242
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001243static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001244{
1245 size_t low = 0, high = v->len;
1246
1247 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001248 size_t mid = (low + high) / 2;
1249 BcId *id = bc_vec_item(v, mid);
1250 int result = bc_id_cmp(ptr, id);
1251
1252 if (result == 0)
1253 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001254 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001255 high = mid;
1256 else
1257 low = mid + 1;
1258 }
1259
1260 return low;
1261}
1262
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001263static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001264{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001265 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001266
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001267 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001268 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1270 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001271 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001272 bc_vec_pushAt(v, ptr, n);
1273 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001274}
1275
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001276#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001277static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001278{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001279 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001280 if (i >= v->len) return BC_VEC_INVALID_IDX;
1281 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1282}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001283#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001284
Gavin Howard01055ba2018-11-03 11:00:21 -06001285static void bc_num_setToZero(BcNum *n, size_t scale)
1286{
1287 n->len = 0;
1288 n->neg = false;
1289 n->rdx = scale;
1290}
1291
1292static void bc_num_zero(BcNum *n)
1293{
1294 bc_num_setToZero(n, 0);
1295}
1296
1297static void bc_num_one(BcNum *n)
1298{
1299 bc_num_setToZero(n, 0);
1300 n->len = 1;
1301 n->num[0] = 1;
1302}
1303
Denys Vlasenko3129f702018-12-09 12:04:44 +01001304// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001305static void bc_num_init(BcNum *n, size_t req)
1306{
1307 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001308 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001309 n->num = xmalloc(req);
1310 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001311 n->rdx = 0;
1312 n->len = 0;
1313 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001314}
1315
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001316static void bc_num_init_DEF_SIZE(BcNum *n)
1317{
1318 bc_num_init(n, BC_NUM_DEF_SIZE);
1319}
1320
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001321static void bc_num_expand(BcNum *n, size_t req)
1322{
1323 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1324 if (req > n->cap) {
1325 n->num = xrealloc(n->num, req);
1326 n->cap = req;
1327 }
1328}
1329
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001330static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001331{
1332 free(((BcNum *) num)->num);
1333}
1334
1335static void bc_num_copy(BcNum *d, BcNum *s)
1336{
1337 if (d != s) {
1338 bc_num_expand(d, s->cap);
1339 d->len = s->len;
1340 d->neg = s->neg;
1341 d->rdx = s->rdx;
1342 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1343 }
1344}
1345
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001346static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347{
1348 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001349 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001350
Denys Vlasenko1557b762018-12-22 21:37:46 +01001351 result = 0;
1352 i = n->len;
1353 while (i > n->rdx) {
1354 unsigned long prev = result;
1355 result = result * 10 + n->num[--i];
1356 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1357 // 0x1ff00000 * 10 is 0x13f600000,
1358 // or 0x3f600000 truncated to 32 bits. Which is larger.
1359 // However, (N*10)/8 < N check is always correct.
1360 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001361 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001363 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001364
Denys Vlasenko29301232018-12-11 15:29:32 +01001365 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001366}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001367#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1368
1369static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1370{
1371 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1372
1373 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1374}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001375#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001376
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001377#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1378# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1379#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1380# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1381#endif
1382// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1383// would not hit realloc() code path - not good if num[] is not malloced
1384
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001385static void bc_num_ulong2num(BcNum *n, unsigned long val)
1386{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001387 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001388
1389 bc_num_zero(n);
1390
1391 if (val == 0) return;
1392
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001393 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001394
1395 ptr = n->num;
1396 for (;;) {
1397 n->len++;
1398 *ptr++ = val % 10;
1399 val /= 10;
1400 if (val == 0) break;
1401 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001402}
1403
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001404static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001405{
1406 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001407 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001408 a[i] -= b[i];
1409 for (j = i; a[j] < 0;) {
1410 a[j++] += 10;
1411 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001412 }
1413 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001414}
1415
1416static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1417{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001418 size_t i = len;
1419 for (;;) {
1420 int c;
1421 if (i == 0)
1422 return 0;
1423 i--;
1424 c = a[i] - b[i];
1425 if (c != 0) {
1426 i++;
1427 if (c < 0)
1428 return -i;
1429 return i;
1430 }
1431 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001432}
1433
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001434#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1435#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1436#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1437//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1438static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1439{
1440 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1441}
1442//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1443static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1444{
1445 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1446}
1447
Gavin Howard01055ba2018-11-03 11:00:21 -06001448static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1449{
1450 size_t i, min, a_int, b_int, diff;
1451 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001452 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001453 ssize_t cmp;
1454
1455 if (a == b) return 0;
1456 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1457 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001458
1459 if (a->neg != b->neg) // signs of a and b differ
1460 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1461 return (int)b->neg - (int)a->neg;
1462 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001463
1464 a_int = BC_NUM_INT(a);
1465 b_int = BC_NUM_INT(b);
1466 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001467
1468 if (a_int != 0) return (ssize_t) a_int;
1469
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001470 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001471 if (a_max) {
1472 min = b->rdx;
1473 diff = a->rdx - b->rdx;
1474 max_num = a->num + diff;
1475 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001476 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1477 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001478 min = a->rdx;
1479 diff = b->rdx - a->rdx;
1480 max_num = b->num + diff;
1481 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001482 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001483 }
1484
1485 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001486 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001487
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001488 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001489 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001490 }
1491
1492 return 0;
1493}
1494
1495static void bc_num_truncate(BcNum *n, size_t places)
1496{
1497 if (places == 0) return;
1498
1499 n->rdx -= places;
1500
1501 if (n->len != 0) {
1502 n->len -= places;
1503 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1504 }
1505}
1506
1507static void bc_num_extend(BcNum *n, size_t places)
1508{
1509 size_t len = n->len + places;
1510
1511 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001512 if (n->cap < len) bc_num_expand(n, len);
1513
1514 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1515 memset(n->num, 0, sizeof(BcDig) * places);
1516
1517 n->len += places;
1518 n->rdx += places;
1519 }
1520}
1521
1522static void bc_num_clean(BcNum *n)
1523{
1524 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1525 if (n->len == 0)
1526 n->neg = false;
1527 else if (n->len < n->rdx)
1528 n->len = n->rdx;
1529}
1530
1531static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1532{
1533 if (n->rdx < scale)
1534 bc_num_extend(n, scale - n->rdx);
1535 else
1536 bc_num_truncate(n, n->rdx - scale);
1537
1538 bc_num_clean(n);
1539 if (n->len != 0) n->neg = !neg1 != !neg2;
1540}
1541
1542static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1543 BcNum *restrict b)
1544{
1545 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001546 b->len = n->len - idx;
1547 a->len = idx;
1548 a->rdx = b->rdx = 0;
1549
1550 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1551 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001552 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001553 bc_num_zero(b);
1554 bc_num_copy(a, n);
1555 }
1556
1557 bc_num_clean(a);
1558 bc_num_clean(b);
1559}
1560
Denys Vlasenko29301232018-12-11 15:29:32 +01001561static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001562{
Denys Vlasenko29301232018-12-11 15:29:32 +01001563 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001564
1565 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1566 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1567 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001568 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001569 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001570
1571 if (n->rdx >= places)
1572 n->rdx -= places;
1573 else {
1574 bc_num_extend(n, places - n->rdx);
1575 n->rdx = 0;
1576 }
1577
1578 bc_num_clean(n);
1579
Denys Vlasenko29301232018-12-11 15:29:32 +01001580 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001581}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001582#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001583
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001584typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1585
1586static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1587 BcNumBinaryOp op, size_t req)
1588{
1589 BcStatus s;
1590 BcNum num2, *ptr_a, *ptr_b;
1591 bool init = false;
1592
1593 if (c == a) {
1594 ptr_a = &num2;
1595 memcpy(ptr_a, c, sizeof(BcNum));
1596 init = true;
1597 } else
1598 ptr_a = a;
1599
1600 if (c == b) {
1601 ptr_b = &num2;
1602 if (c != a) {
1603 memcpy(ptr_b, c, sizeof(BcNum));
1604 init = true;
1605 }
1606 } else
1607 ptr_b = b;
1608
1609 if (init)
1610 bc_num_init(c, req);
1611 else
1612 bc_num_expand(c, req);
1613
1614 s = BC_STATUS_SUCCESS;
1615 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1616
1617 if (init) bc_num_free(&num2);
1618
1619 RETURN_STATUS(s);
1620}
1621#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1622
1623static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1624static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1625static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1626static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1627static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1628static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1629
1630static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1631{
1632 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1633 (void) scale;
1634 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1635}
1636
1637static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1638{
1639 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1640 (void) scale;
1641 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1642}
1643
1644static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1645{
1646 size_t req = BC_NUM_MREQ(a, b, scale);
1647 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1648}
1649
1650static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1651{
1652 size_t req = BC_NUM_MREQ(a, b, scale);
1653 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1654}
1655
1656static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1657{
1658 size_t req = BC_NUM_MREQ(a, b, scale);
1659 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1660}
1661
1662static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1663{
1664 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1665}
1666
1667static const BcNumBinaryOp zbc_program_ops[] = {
1668 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1669};
1670#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1671#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1672#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1673#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1674#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1675#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1676
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001677static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001678{
1679 BcNum one;
1680 BcDig num[2];
1681
1682 one.cap = 2;
1683 one.num = num;
1684 bc_num_one(&one);
1685
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001686 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001687}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001688#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001689
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001690static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001691{
1692 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1693 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001694 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001695
1696 // Because this function doesn't need to use scale (per the bc spec),
1697 // I am hijacking it to say whether it's doing an add or a subtract.
1698
1699 if (a->len == 0) {
1700 bc_num_copy(c, b);
1701 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001702 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001703 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001704 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001705 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001706 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001707 }
1708
1709 c->neg = a->neg;
1710 c->rdx = BC_MAX(a->rdx, b->rdx);
1711 min_rdx = BC_MIN(a->rdx, b->rdx);
1712 c->len = 0;
1713
1714 if (a->rdx > b->rdx) {
1715 diff = a->rdx - b->rdx;
1716 ptr = a->num;
1717 ptr_a = a->num + diff;
1718 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001719 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001720 diff = b->rdx - a->rdx;
1721 ptr = b->num;
1722 ptr_a = a->num;
1723 ptr_b = b->num + diff;
1724 }
1725
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001726 ptr_c = c->num;
1727 for (i = 0; i < diff; ++i, ++c->len)
1728 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001729
1730 ptr_c += diff;
1731 a_int = BC_NUM_INT(a);
1732 b_int = BC_NUM_INT(b);
1733
1734 if (a_int > b_int) {
1735 min_int = b_int;
1736 max = a_int;
1737 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001738 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001739 min_int = a_int;
1740 max = b_int;
1741 ptr = ptr_b;
1742 }
1743
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001744 carry = 0;
1745 for (i = 0; i < min_rdx + min_int; ++i) {
1746 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001747 carry = in / 10;
1748 ptr_c[i] = (BcDig)(in % 10);
1749 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001750 for (; i < max + min_rdx; ++i) {
1751 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001752 carry = in / 10;
1753 ptr_c[i] = (BcDig)(in % 10);
1754 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001755 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001756
1757 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1758
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001759 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001760}
1761
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001763{
Gavin Howard01055ba2018-11-03 11:00:21 -06001764 ssize_t cmp;
1765 BcNum *minuend, *subtrahend;
1766 size_t start;
1767 bool aneg, bneg, neg;
1768
1769 // Because this function doesn't need to use scale (per the bc spec),
1770 // I am hijacking it to say whether it's doing an add or a subtract.
1771
1772 if (a->len == 0) {
1773 bc_num_copy(c, b);
1774 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001775 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001776 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001777 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001778 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001779 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001780 }
1781
1782 aneg = a->neg;
1783 bneg = b->neg;
1784 a->neg = b->neg = false;
1785
1786 cmp = bc_num_cmp(a, b);
1787
1788 a->neg = aneg;
1789 b->neg = bneg;
1790
1791 if (cmp == 0) {
1792 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001793 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001795 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001796 neg = a->neg;
1797 minuend = a;
1798 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001799 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 neg = b->neg;
1801 if (sub) neg = !neg;
1802 minuend = b;
1803 subtrahend = a;
1804 }
1805
1806 bc_num_copy(c, minuend);
1807 c->neg = neg;
1808
1809 if (c->rdx < subtrahend->rdx) {
1810 bc_num_extend(c, subtrahend->rdx - c->rdx);
1811 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001812 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 start = c->rdx - subtrahend->rdx;
1814
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001815 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001816
1817 bc_num_clean(c);
1818
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001819 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001820}
1821
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001822static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001824#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001825{
1826 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001827 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001828 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001829 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001830
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 if (a->len == 0 || b->len == 0) {
1832 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001833 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001835 aone = BC_NUM_ONE(a);
1836 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001838 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001839 }
1840
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001841 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1842 || a->len < BC_NUM_KARATSUBA_LEN
1843 || b->len < BC_NUM_KARATSUBA_LEN
1844 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001845 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001846
Gavin Howard01055ba2018-11-03 11:00:21 -06001847 bc_num_expand(c, a->len + b->len + 1);
1848
1849 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001850 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001851
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001852 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001853 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001854 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001855 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001856 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001857 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001858 carry = in / 10;
1859 c->num[i + j] = (BcDig)(in % 10);
1860 }
1861
1862 c->num[i + j] += (BcDig) carry;
1863 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001864
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001865#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001866 // a=2^1000000
1867 // a*a <- without check below, this will not be interruptible
1868 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001869#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 }
1871
1872 c->len = len;
1873
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001874 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 }
1876
1877 bc_num_init(&l1, max);
1878 bc_num_init(&h1, max);
1879 bc_num_init(&l2, max);
1880 bc_num_init(&h2, max);
1881 bc_num_init(&m1, max);
1882 bc_num_init(&m2, max);
1883 bc_num_init(&z0, max);
1884 bc_num_init(&z1, max);
1885 bc_num_init(&z2, max);
1886 bc_num_init(&temp, max + max);
1887
1888 bc_num_split(a, max2, &l1, &h1);
1889 bc_num_split(b, max2, &l2, &h2);
1890
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001891 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001893 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001894 if (s) goto err;
1895
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001897 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001898 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001900 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001901 if (s) goto err;
1902
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001903 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001905 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001906 if (s) goto err;
1907
Denys Vlasenko29301232018-12-11 15:29:32 +01001908 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001910 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001911 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001912 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001913 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001914 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001915 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 bc_num_free(&temp);
1917 bc_num_free(&z2);
1918 bc_num_free(&z1);
1919 bc_num_free(&z0);
1920 bc_num_free(&m2);
1921 bc_num_free(&m1);
1922 bc_num_free(&h2);
1923 bc_num_free(&l2);
1924 bc_num_free(&h1);
1925 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001926 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001927}
1928
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001929static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001930{
1931 BcStatus s;
1932 BcNum cpa, cpb;
1933 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1934
1935 scale = BC_MAX(scale, a->rdx);
1936 scale = BC_MAX(scale, b->rdx);
1937 scale = BC_MIN(a->rdx + b->rdx, scale);
1938 maxrdx = BC_MAX(maxrdx, scale);
1939
1940 bc_num_init(&cpa, a->len);
1941 bc_num_init(&cpb, b->len);
1942
1943 bc_num_copy(&cpa, a);
1944 bc_num_copy(&cpb, b);
1945 cpa.neg = cpb.neg = false;
1946
Denys Vlasenko29301232018-12-11 15:29:32 +01001947 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001948 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001949 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001951 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001952 if (s) goto err;
1953
1954 maxrdx += scale;
1955 bc_num_expand(c, c->len + maxrdx);
1956
1957 if (c->len < maxrdx) {
1958 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1959 c->len += maxrdx;
1960 }
1961
1962 c->rdx = maxrdx;
1963 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001964 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 bc_num_free(&cpb);
1966 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001967 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001969#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001970
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001971static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001972{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001973 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001974 size_t len, end, i;
1975 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001976
1977 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001978 RETURN_STATUS(bc_error("divide by zero"));
1979 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001980 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001981 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001983 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001984 bc_num_copy(c, a);
1985 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001986 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001987 }
1988
1989 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1990 bc_num_copy(&cp, a);
1991 len = b->len;
1992
1993 if (len > cp.len) {
1994 bc_num_expand(&cp, len + 2);
1995 bc_num_extend(&cp, len - cp.len);
1996 }
1997
1998 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1999 cp.rdx -= b->rdx;
2000 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2001
2002 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002003 for (;;) {
2004 if (len == 0) break;
2005 len--;
2006 if (b->num[len] != 0)
2007 break;
2008 }
2009 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002010 }
2011
2012 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2013
2014 // We want an extra zero in front to make things simpler.
2015 cp.num[cp.len++] = 0;
2016 end = cp.len - len;
2017
2018 bc_num_expand(c, cp.len);
2019
2020 bc_num_zero(c);
2021 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2022 c->rdx = cp.rdx;
2023 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002024
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002025 s = BC_STATUS_SUCCESS;
2026 for (i = end - 1; i < end; --i) {
2027 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002028 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002029 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2030 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002031 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002032#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002033 // a=2^100000
2034 // scale=40000
2035 // 1/a <- without check below, this will not be interruptible
2036 if (G_interrupt) {
2037 s = BC_STATUS_FAILURE;
2038 break;
2039 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002040#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002041 }
2042
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002043 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002044 bc_num_free(&cp);
2045
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002046 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002047}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002048#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002049
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002050static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002051 BcNum *restrict d, size_t scale, size_t ts)
2052{
2053 BcStatus s;
2054 BcNum temp;
2055 bool neg;
2056
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002057 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002059
2060 if (a->len == 0) {
2061 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002062 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002063 }
2064
2065 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002066 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002067 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002068
2069 if (scale != 0) scale = ts;
2070
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002071 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002072 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002073 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002074 if (s) goto err;
2075
2076 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2077
2078 neg = d->neg;
2079 bc_num_retireMul(d, ts, a->neg, b->neg);
2080 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002081 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002082 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002083 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002084}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002085#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002086
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002087static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002088{
2089 BcStatus s;
2090 BcNum c1;
2091 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2092
2093 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002094 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002095 bc_num_free(&c1);
2096
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002098}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002099#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002100
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002102{
2103 BcStatus s = BC_STATUS_SUCCESS;
2104 BcNum copy;
2105 unsigned long pow;
2106 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002107 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002108
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002109 // GNU bc does not allow 2^2.0 - we do
2110 for (i = 0; i < b->rdx; i++)
2111 if (b->num[i] != 0)
2112 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002113
2114 if (b->len == 0) {
2115 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002116 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002118 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002119 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002120 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002122 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 if (!b->neg)
2124 bc_num_copy(c, a);
2125 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002126 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 }
2129
2130 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002131 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002132 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002133 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002134
2135 bc_num_init(&copy, a->len);
2136 bc_num_copy(&copy, a);
2137
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002138 if (!neg) {
2139 if (a->rdx > scale)
2140 scale = a->rdx;
2141 if (a->rdx * pow < scale)
2142 scale = a->rdx * pow;
2143 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002144
Gavin Howard01055ba2018-11-03 11:00:21 -06002145
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002146 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002148 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002150 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002151 //if (G_interrupt) {
2152 // s = BC_STATUS_FAILURE;
2153 // goto err;
2154 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002155 }
2156
Gavin Howard01055ba2018-11-03 11:00:21 -06002157 bc_num_copy(c, &copy);
2158
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002159 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002160 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002161 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 if (s) goto err;
2163
2164 if (pow & 1) {
2165 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002166 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002167 if (s) goto err;
2168 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002169 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002170 //if (G_interrupt) {
2171 // s = BC_STATUS_FAILURE;
2172 // goto err;
2173 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002174 }
2175
2176 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002177 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002178 if (s) goto err;
2179 }
2180
Gavin Howard01055ba2018-11-03 11:00:21 -06002181 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2182
2183 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002184 for (i = 0; i < c->len; ++i)
2185 if (c->num[i] != 0)
2186 goto skip;
2187 bc_num_setToZero(c, scale);
2188 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002189
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002190 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002191 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002193}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002194#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002195
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002196static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002197{
2198 BcStatus s;
2199 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2200 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2201 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2202
2203 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2204 bc_num_expand(b, req);
2205
2206 if (a->len == 0) {
2207 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002208 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002209 }
2210 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002211 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002212 }
2213 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002214 bc_num_one(b);
2215 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002216 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002217 }
2218
2219 scale = BC_MAX(scale, a->rdx) + 1;
2220 len = a->len + scale;
2221
2222 bc_num_init(&num1, len);
2223 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002224 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002225
2226 bc_num_one(&half);
2227 half.num[0] = 5;
2228 half.rdx = 1;
2229
2230 bc_num_init(&f, len);
2231 bc_num_init(&fprime, len);
2232
2233 x0 = &num1;
2234 x1 = &num2;
2235
2236 bc_num_one(x0);
2237 pow = BC_NUM_INT(a);
2238
2239 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002240 if (pow & 1)
2241 x0->num[0] = 2;
2242 else
2243 x0->num[0] = 6;
2244
2245 pow -= 2 - (pow & 1);
2246
2247 bc_num_extend(x0, pow);
2248
2249 // Make sure to move the radix back.
2250 x0->rdx -= pow;
2251 }
2252
2253 x0->rdx = digs = digs1 = 0;
2254 resrdx = scale + 2;
2255 len = BC_NUM_INT(x0) + resrdx - 1;
2256
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002257 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002258 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002259 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002260 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002261 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002262 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002263 if (s) goto err;
2264
2265 cmp = bc_num_cmp(x1, x0);
2266 digs = x1->len - (unsigned long long) llabs(cmp);
2267
2268 if (cmp == cmp2 && digs == digs1)
2269 times += 1;
2270 else
2271 times = 0;
2272
2273 resrdx += times > 4;
2274
2275 cmp2 = cmp1;
2276 cmp1 = cmp;
2277 digs1 = digs;
2278
2279 temp = x0;
2280 x0 = x1;
2281 x1 = temp;
2282 }
2283
Gavin Howard01055ba2018-11-03 11:00:21 -06002284 bc_num_copy(b, x0);
2285 scale -= 1;
2286 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002287 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002288 bc_num_free(&fprime);
2289 bc_num_free(&f);
2290 bc_num_free(&half);
2291 bc_num_free(&num2);
2292 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002293 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002294}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002295#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002296
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002297static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002298 size_t scale)
2299{
2300 BcStatus s;
2301 BcNum num2, *ptr_a;
2302 bool init = false;
2303 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2304
2305 if (c == a) {
2306 memcpy(&num2, c, sizeof(BcNum));
2307 ptr_a = &num2;
2308 bc_num_init(c, len);
2309 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002310 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002311 ptr_a = a;
2312 bc_num_expand(c, len);
2313 }
2314
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002315 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002316
2317 if (init) bc_num_free(&num2);
2318
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002319 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002320}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002321#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002322
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002323#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002324static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002325{
2326 BcStatus s;
2327 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002328 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002329
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002330 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002331 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002332 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002333 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002334 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002335 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002336
2337 bc_num_expand(d, c->len);
2338 bc_num_init(&base, c->len);
2339 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002340 bc_num_init(&temp, b->len);
2341
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002342 two.cap = ARRAY_SIZE(two_digs);
2343 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002345 two_digs[0] = 2;
2346
Gavin Howard01055ba2018-11-03 11:00:21 -06002347 bc_num_one(d);
2348
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002349 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002350 if (s) goto err;
2351 bc_num_copy(&exp, b);
2352
2353 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002354 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002355 if (s) goto err;
2356
2357 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002358 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002360 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002361 if (s) goto err;
2362 }
2363
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002364 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002365 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002366 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002367 if (s) goto err;
2368 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002369 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002370 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002371 bc_num_free(&exp);
2372 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002373 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002374}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002375#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002376#endif // ENABLE_DC
2377
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002378static FAST_FUNC void bc_string_free(void *string)
2379{
2380 free(*(char**)string);
2381}
2382
Gavin Howard01055ba2018-11-03 11:00:21 -06002383static void bc_func_init(BcFunc *f)
2384{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002385 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002386 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2387 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002388 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2389 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002390 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002391}
2392
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002393static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002394{
2395 BcFunc *f = (BcFunc *) func;
2396 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002397 IF_BC(bc_vec_free(&f->labels);)
2398 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002399 IF_BC(bc_vec_free(&f->strs);)
2400 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002401}
2402
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002403static void bc_array_expand(BcVec *a, size_t len);
2404
Gavin Howard01055ba2018-11-03 11:00:21 -06002405static void bc_array_init(BcVec *a, bool nums)
2406{
2407 if (nums)
2408 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2409 else
2410 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2411 bc_array_expand(a, 1);
2412}
2413
Gavin Howard01055ba2018-11-03 11:00:21 -06002414static void bc_array_expand(BcVec *a, size_t len)
2415{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002416 if (a->dtor == bc_num_free
2417 // && a->size == sizeof(BcNum) - always true
2418 ) {
2419 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002421 bc_num_init_DEF_SIZE(&n);
2422 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002423 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002424 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002425 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002426 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002427 bc_array_init(&v, true);
2428 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002429 }
2430 }
2431}
2432
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002433static void bc_array_copy(BcVec *d, const BcVec *s)
2434{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002435 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002436 size_t i;
2437
2438 bc_vec_pop_all(d);
2439 bc_vec_expand(d, s->cap);
2440 d->len = s->len;
2441
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002442 dnum = (void*)d->v;
2443 snum = (void*)s->v;
2444 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002445 bc_num_init(dnum, snum->len);
2446 bc_num_copy(dnum, snum);
2447 }
2448}
2449
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002450#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002451static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002452{
2453 d->t = src->t;
2454
2455 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002456 case XC_RESULT_TEMP:
2457 case XC_RESULT_IBASE:
2458 case XC_RESULT_SCALE:
2459 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002460 bc_num_init(&d->d.n, src->d.n.len);
2461 bc_num_copy(&d->d.n, &src->d.n);
2462 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002463 case XC_RESULT_VAR:
2464 case XC_RESULT_ARRAY:
2465 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002466 d->d.id.name = xstrdup(src->d.id.name);
2467 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002468 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002469 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002470 IF_BC(case BC_RESULT_ONE:)
2471 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002472 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2473 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002474 }
2475}
2476#endif // ENABLE_DC
2477
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002478static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002479{
2480 BcResult *r = (BcResult *) result;
2481
2482 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002483 case XC_RESULT_TEMP:
2484 case XC_RESULT_IBASE:
2485 case XC_RESULT_SCALE:
2486 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002487 bc_num_free(&r->d.n);
2488 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002489 case XC_RESULT_VAR:
2490 case XC_RESULT_ARRAY:
2491 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002492 free(r->d.id.name);
2493 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002494 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002495 // Do nothing.
2496 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002497 }
2498}
2499
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002500static int bad_input_byte(char c)
2501{
2502 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2503 || c > 0x7e
2504 ) {
2505 bc_error_fmt("illegal character 0x%02x", c);
2506 return 1;
2507 }
2508 return 0;
2509}
2510
2511// Note: it _appends_ data from fp to vec.
2512static void bc_read_line(BcVec *vec, FILE *fp)
2513{
2514 again:
2515 fflush_and_check();
2516
2517#if ENABLE_FEATURE_BC_SIGNALS
2518 if (G_interrupt) { // ^C was pressed
2519 intr:
2520 if (fp != stdin) {
2521 // ^C while running a script (bc SCRIPT): die.
2522 // We do not return to interactive prompt:
2523 // user might be running us from a shell,
2524 // and SCRIPT might be intended to terminate
2525 // (e.g. contain a "halt" stmt).
2526 // ^C dropping user into a bc prompt instead of
2527 // the shell would be unexpected.
2528 xfunc_die();
2529 }
2530 // ^C while interactive input
2531 G_interrupt = 0;
2532 // GNU bc says "interrupted execution."
2533 // GNU dc says "Interrupt!"
2534 fputs("\ninterrupted execution\n", stderr);
2535 }
2536
2537# if ENABLE_FEATURE_EDITING
2538 if (G_ttyin && fp == stdin) {
2539 int n, i;
2540# define line_buf bb_common_bufsiz1
2541 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2542 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2543 if (n == 0) // ^C
2544 goto intr;
2545 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2546 return;
2547 }
2548 i = 0;
2549 for (;;) {
2550 char c = line_buf[i++];
2551 if (!c) break;
2552 if (bad_input_byte(c)) goto again;
2553 }
2554 bc_vec_concat(vec, line_buf);
2555# undef line_buf
2556 } else
2557# endif
2558#endif
2559 {
2560 int c;
2561 bool bad_chars = 0;
2562 size_t len = vec->len;
2563
2564 do {
2565#if ENABLE_FEATURE_BC_SIGNALS
2566 if (G_interrupt) {
2567 // ^C was pressed: ignore entire line, get another one
2568 vec->len = len;
2569 goto intr;
2570 }
2571#endif
2572 do c = fgetc(fp); while (c == '\0');
2573 if (c == EOF) {
2574 if (ferror(fp))
2575 bb_perror_msg_and_die("input error");
2576 // Note: EOF does not append '\n'
2577 break;
2578 }
2579 bad_chars |= bad_input_byte(c);
2580 bc_vec_pushByte(vec, (char)c);
2581 } while (c != '\n');
2582
2583 if (bad_chars) {
2584 // Bad chars on this line
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01002585 if (!G.prs.filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002586 // ignore entire line, get another one
2587 vec->len = len;
2588 goto again;
2589 }
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01002590 bb_perror_msg_and_die("file '%s' is not text", G.prs.filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002591 }
2592 bc_vec_pushZeroByte(vec);
2593 }
2594}
2595
2596//
2597// Parsing routines
2598//
2599
2600static bool bc_num_strValid(const char *val, size_t base)
2601{
2602 BcDig b;
2603 bool radix;
2604
2605 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2606 radix = false;
2607 for (;;) {
2608 BcDig c = *val++;
2609 if (c == '\0')
2610 break;
2611 if (c == '.') {
2612 if (radix) return false;
2613 radix = true;
2614 continue;
2615 }
2616 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2617 return false;
2618 }
2619 return true;
2620}
2621
2622// Note: n is already "bc_num_zero()"ed,
2623// leading zeroes in "val" are removed
2624static void bc_num_parseDecimal(BcNum *n, const char *val)
2625{
2626 size_t len, i;
2627 const char *ptr;
2628
2629 len = strlen(val);
2630 if (len == 0)
2631 return;
2632
2633 bc_num_expand(n, len);
2634
2635 ptr = strchr(val, '.');
2636
2637 n->rdx = 0;
2638 if (ptr != NULL)
2639 n->rdx = (size_t)((val + len) - (ptr + 1));
2640
2641 for (i = 0; val[i]; ++i) {
2642 if (val[i] != '0' && val[i] != '.') {
2643 // Not entirely zero value - convert it, and exit
2644 i = len - 1;
2645 for (;;) {
2646 n->num[n->len] = val[i] - '0';
2647 ++n->len;
2648 skip_dot:
2649 if (i == 0) break;
2650 if (val[--i] == '.') goto skip_dot;
2651 }
2652 break;
2653 }
2654 }
2655 // if for() exits without hitting if(), the value is entirely zero
2656}
2657
2658// Note: n is already "bc_num_zero()"ed,
2659// leading zeroes in "val" are removed
2660static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2661{
2662 BcStatus s;
2663 BcNum temp, mult, result;
2664 BcNum base;
2665 BcDig base_digs[ULONG_NUM_BUFSIZE];
2666 BcDig c = '\0';
2667 unsigned long v;
2668 size_t i, digits;
2669
2670 for (i = 0; ; ++i) {
2671 if (val[i] == '\0')
2672 return;
2673 if (val[i] != '.' && val[i] != '0')
2674 break;
2675 }
2676
2677 bc_num_init_DEF_SIZE(&temp);
2678 bc_num_init_DEF_SIZE(&mult);
2679 base.cap = ARRAY_SIZE(base_digs);
2680 base.num = base_digs;
2681 bc_num_ulong2num(&base, base_t);
2682
2683 for (;;) {
2684 c = *val++;
2685 if (c == '\0') goto int_err;
2686 if (c == '.') break;
2687
2688 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2689
2690 s = zbc_num_mul(n, &base, &mult, 0);
2691 if (s) goto int_err;
2692 bc_num_ulong2num(&temp, v);
2693 s = zbc_num_add(&mult, &temp, n, 0);
2694 if (s) goto int_err;
2695 }
2696
2697 bc_num_init(&result, base.len);
2698 //bc_num_zero(&result); - already is
2699 bc_num_one(&mult);
2700
2701 digits = 0;
2702 for (;;) {
2703 c = *val++;
2704 if (c == '\0') break;
2705 digits++;
2706
2707 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2708
2709 s = zbc_num_mul(&result, &base, &result, 0);
2710 if (s) goto err;
2711 bc_num_ulong2num(&temp, v);
2712 s = zbc_num_add(&result, &temp, &result, 0);
2713 if (s) goto err;
2714 s = zbc_num_mul(&mult, &base, &mult, 0);
2715 if (s) goto err;
2716 }
2717
2718 s = zbc_num_div(&result, &mult, &result, digits);
2719 if (s) goto err;
2720 s = zbc_num_add(n, &result, n, digits);
2721 if (s) goto err;
2722
2723 if (n->len != 0) {
2724 if (n->rdx < digits)
2725 bc_num_extend(n, digits - n->rdx);
2726 } else
2727 bc_num_zero(n);
2728 err:
2729 bc_num_free(&result);
2730 int_err:
2731 bc_num_free(&mult);
2732 bc_num_free(&temp);
2733}
2734
2735static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2736{
2737 if (!bc_num_strValid(val, base_t))
2738 RETURN_STATUS(bc_error("bad number string"));
2739
2740 bc_num_zero(n);
2741 while (*val == '0') val++;
2742
2743 if (base_t == 10)
2744 bc_num_parseDecimal(n, val);
2745 else
2746 bc_num_parseBase(n, val, base_t);
2747
2748 RETURN_STATUS(BC_STATUS_SUCCESS);
2749}
2750#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2751
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002752static void bc_lex_lineComment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002753{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002754 BcLex *l = &G.prs.l;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002755 // Try: echo -n '#foo' | bc
2756 size_t i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002757 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002758 i = l->i;
2759 while (i < l->len && l->buf[i] != '\n')
2760 i++;
2761 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002762}
2763
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002764static void bc_lex_whitespace(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002765{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002766 BcLex *l = &G.prs.l;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002767 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002768 for (;;) {
2769 char c = l->buf[l->i];
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002770 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002771 break;
2772 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002773 break;
2774 l->i++;
2775 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002776}
2777
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002778static BC_STATUS zbc_lex_number(char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002779{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002780 BcLex *l = &G.prs.l;
Gavin Howard01055ba2018-11-03 11:00:21 -06002781 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002782 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002783 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002784
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002785 pt = (start == '.');
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002786 l->lex = XC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002787 ccnt = i = 0;
2788 for (;;) {
2789 char c = buf[i];
2790 if (c == '\0')
2791 break;
2792 if (c == '\\' && buf[i + 1] == '\n') {
2793 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002794 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002795 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002796 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002797 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2798 if (c != '.') break;
2799 // if '.' was already seen, stop on second one:
2800 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002801 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002802 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002803 // buf[i] is one of "0-9A-F."
2804 i++;
2805 if (c != '.')
2806 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002807 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002808 //ccnt is the number of chars in the number string, excluding possible
2809 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2810 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002811 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002812
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002813 // This might overestimate the size, if there are "\<NL>"'s
2814 // in the number. Subtracting number_of_backslashes*2 correctly
2815 // is not that easy: consider that in the case of "NNN.\<NL>"
2816 // loop above will count "\<NL>" before it realizes it is not
2817 // in fact *inside* the number:
2818 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002819
Denys Vlasenko64074a12018-12-07 15:50:14 +01002820 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2821 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2822 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002823 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002824 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002825
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002826 bc_vec_pop_all(&l->lex_buf);
2827 bc_vec_expand(&l->lex_buf, 1 + len);
2828 bc_vec_push(&l->lex_buf, &start);
Gavin Howard01055ba2018-11-03 11:00:21 -06002829
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002830 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002831 // If we have hit a backslash, skip it. We don't have
2832 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002833 if (*buf == '\\') {
2834 buf += 2;
2835 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002836 continue;
2837 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002838 bc_vec_push(&l->lex_buf, buf);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002839 buf++;
2840 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002841 }
2842
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002843 bc_vec_pushZeroByte(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002844
Denys Vlasenko29301232018-12-11 15:29:32 +01002845 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002846}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002847#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002848
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002849static void bc_lex_name(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002850{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002851 BcLex *l = &G.prs.l;
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002852 size_t i;
2853 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002854
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002855 l->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06002856
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002857 i = 0;
2858 buf = l->buf + l->i - 1;
2859 for (;;) {
2860 char c = buf[i];
2861 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2862 i++;
2863 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002864
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002865#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002866 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2867 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2868 if (i > BC_MAX_STRING)
2869 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2870 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002871#endif
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002872 bc_vec_string(&l->lex_buf, i, buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002873
2874 // Increment the index. We minus 1 because it has already been incremented.
2875 l->i += i - 1;
2876
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002877 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002878}
2879
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002880static void bc_lex_init(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002881{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002882 bc_char_vec_init(&G.prs.l.lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002883}
2884
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002885static void bc_lex_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002886{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002887 bc_vec_free(&G.prs.l.lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002888}
2889
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002890static void bc_lex_file(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002891{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002892 G.err_line = G.prs.l.line = 1;
2893 G.prs.l.newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002894}
2895
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002896static bool bc_lex_more_input(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002897{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002898 BcLex *l = &G.prs.l;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002899 size_t str;
2900 bool comment;
2901
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002902 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002903
2904 // This loop is complex because the vm tries not to send any lines that end
2905 // with a backslash to the parser. The reason for that is because the parser
2906 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2907 // case, and for strings and comments, the parser will expect more stuff.
2908 comment = false;
2909 str = 0;
2910 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002911 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002912 char *string;
2913
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01002914 bc_read_line(&G.input_buffer, G.prs.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002915 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002916 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002917 break;
2918
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002919 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002920 while (*string) {
2921 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002922 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002923 if (IS_BC)
2924 str ^= (c == '"');
2925 else {
2926 if (c == ']')
2927 str -= 1;
2928 else if (c == '[')
2929 str += 1;
2930 }
2931 }
2932 string++;
2933 if (c == '/' && *string == '*') {
2934 comment = true;
2935 string++;
2936 continue;
2937 }
2938 if (c == '*' && *string == '/') {
2939 comment = false;
2940 string++;
2941 }
2942 }
2943 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002944 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002945 continue;
2946 }
2947
2948 // Check for backslash+newline.
2949 // we do not check that last char is '\n' -
2950 // if it is not, then it's EOF, and looping back
2951 // to bc_read_line() will detect it:
2952 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002953 if (string >= G.input_buffer.v && *string == '\\') {
2954 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002955 continue;
2956 }
2957
2958 break;
2959 }
2960
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002961 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002962 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002963// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2964 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002965
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002966 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002967}
2968
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002969IF_BC(static BC_STATUS zbc_lex_token(void);)
2970IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002971#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2972#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002973
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002974static BC_STATUS zbc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002975{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002976 BcLex *l = &G.prs.l;
Gavin Howard01055ba2018-11-03 11:00:21 -06002977 BcStatus s;
2978
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002979 l->lex_last = l->lex;
2980 if (l->lex_last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002981
2982 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002983 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002984 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002985
2986 // Loop until failure or we don't have whitespace. This
2987 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002988 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002989 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002990 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002991 if (l->i == l->len) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002992 l->lex = XC_LEX_EOF;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01002993 if (!G.prs.input_fp)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002994 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002995 if (!bc_lex_more_input()) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01002996 G.prs.input_fp = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002997 RETURN_STATUS(BC_STATUS_SUCCESS);
2998 }
2999 // here it's guaranteed that l->i is below l->len
3000 }
Denys Vlasenko26384542018-12-25 18:37:52 +01003001 l->lex_next_at = l->buf + l->i;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003002 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko26384542018-12-25 18:37:52 +01003003 (int)(strchrnul(l->lex_next_at, '\n') - l->lex_next_at),
3004 l->lex_next_at
3005 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003006 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003007 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003008 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003009 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003010 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003011 } while (!s && l->lex == XC_LEX_WHITESPACE);
3012 dbg_lex("l->lex from string:%d", l->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06003013
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003014 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003015}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003016#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003017
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003018#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003019static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003020{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003021 if (G.prs.l.lex == XC_LEX_NLINE)
3022 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01003023 RETURN_STATUS(BC_STATUS_SUCCESS);
3024}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003025#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003026
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003027static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003028{
3029 BcStatus s;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003030 s = zbc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003031 if (s) RETURN_STATUS(s);
3032 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003033 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003034 RETURN_STATUS(s);
3035}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003036#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003037#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003038
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003039static BC_STATUS zbc_lex_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003040{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003041 G.prs.l.buf = text;
3042 G.prs.l.i = 0;
3043 G.prs.l.len = strlen(text);
3044 G.prs.l.lex = G.prs.l.lex_last = XC_LEX_INVALID;
3045 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003046}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003047#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003048
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003049#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003050static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003051{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003052 BcLex *l = &G.prs.l;
Gavin Howard01055ba2018-11-03 11:00:21 -06003053 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003054 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003055 const char *buf = l->buf + l->i - 1;
3056
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003057 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3058 const char *keyword8 = bc_lex_kws[i].name8;
3059 unsigned j = 0;
3060 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3061 j++;
3062 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003063 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003064 if (keyword8[j] != '\0')
3065 continue;
3066 match:
3067 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003068 if (isalnum(buf[j]) || buf[j]=='_')
3069 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003070 l->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003071 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003072 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3073 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003074 }
3075
3076 // We minus 1 because the index has already been incremented.
3077 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003078 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003079 }
3080
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003081 bc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003082 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003083
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003084 if (l->lex_buf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003085 // Prevent this:
3086 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003087 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003088 // '
3089 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003090 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003091 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003092
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003093 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003094}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003095#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003096
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003097static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003098{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003099 BcLex *l = &G.prs.l;
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003100 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003101
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003102 l->lex = XC_LEX_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06003103
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003104 nls = 0;
3105 i = l->i;
3106 for (;;) {
3107 char c = l->buf[i];
3108 if (c == '\0') {
3109 l->i = i;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003110 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003111 }
3112 if (c == '"')
3113 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003114 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003115 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 }
3117
3118 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003119 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3120 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3121 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003122 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003123 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003124 bc_vec_string(&l->lex_buf, len, l->buf + l->i);
Gavin Howard01055ba2018-11-03 11:00:21 -06003125
3126 l->i = i + 1;
3127 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003128 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003129
Denys Vlasenko26819db2018-12-12 16:08:46 +01003130 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003131}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003132#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003133
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003134static void bc_lex_assign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003135{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003136 BcLex *l = &G.prs.l;
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 if (l->buf[l->i] == '=') {
3138 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003139 with_and_without >>= 8; // store "with" value
3140 } // else store "without" value
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003141 l->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003142}
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003143#define bc_lex_assign(with, without) \
3144 bc_lex_assign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003145
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003146static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003147{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003148 BcLex *l = &G.prs.l;
Gavin Howard01055ba2018-11-03 11:00:21 -06003149 size_t i, nls = 0;
3150 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003151
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003152 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003153 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003154 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003155 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003156 check_star:
3157 if (c == '*') {
3158 c = buf[++i];
3159 if (c == '/')
3160 break;
3161 goto check_star;
3162 }
3163 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 l->i = i;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003165 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003167 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003168 }
3169
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003170 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003171 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003172 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003173
Denys Vlasenko29301232018-12-11 15:29:32 +01003174 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003175}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003176#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003177
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003178#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003179static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003180{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003181 BcLex *l = &G.prs.l;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 BcStatus s = BC_STATUS_SUCCESS;
3183 char c = l->buf[l->i++], c2;
3184
3185 // This is the workhorse of the lexer.
3186 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003187// case '\0': // probably never reached
3188// l->i--;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003189// l->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003190// l->newline = true;
3191// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003192 case '\n':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003193 l->lex = XC_LEX_NLINE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003194 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 case '\t':
3197 case '\v':
3198 case '\f':
3199 case '\r':
3200 case ' ':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003201 bc_lex_whitespace();
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 case '!':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003204 bc_lex_assign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003205 if (l->lex == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003206 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
3207 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003209 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 case '"':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003211 s = zbc_lex_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 case '#':
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003214 s = zbc_POSIX_does_not_allow("'#' script comments");
3215 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003216 bc_lex_lineComment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 case '%':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003219 bc_lex_assign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 c2 = l->buf[l->i];
3223 if (c2 == '&') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003224 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3225 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003227 l->lex = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003228 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003229 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003230 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 case '(':
3234 case ')':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003235 l->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003236 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003237 case '*':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003238 bc_lex_assign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003239 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003240 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003241 c2 = l->buf[l->i];
3242 if (c2 == '+') {
3243 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003244 l->lex = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003245 } else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003246 bc_lex_assign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003247 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003248 case ',':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003249 l->lex = BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003251 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 c2 = l->buf[l->i];
3253 if (c2 == '-') {
3254 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003255 l->lex = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003256 } else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003257 bc_lex_assign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 if (isdigit(l->buf[l->i]))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003261 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003262 else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003263 l->lex = BC_LEX_KEY_LAST;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003264 s = zbc_POSIX_does_not_allow("'.' as 'last'");
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 }
3266 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003268 c2 = l->buf[l->i];
3269 if (c2 == '*')
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003270 s = zbc_lex_comment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003272 bc_lex_assign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 case '0':
3275 case '1':
3276 case '2':
3277 case '3':
3278 case '4':
3279 case '5':
3280 case '6':
3281 case '7':
3282 case '8':
3283 case '9':
3284 case 'A':
3285 case 'B':
3286 case 'C':
3287 case 'D':
3288 case 'E':
3289 case 'F':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003290 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003292 case ';':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003293 l->lex = BC_LEX_SCOLON;
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003295 case '<':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003296 bc_lex_assign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003297 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003298 case '=':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003299 bc_lex_assign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003300 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 case '>':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003302 bc_lex_assign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003303 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003304 case '[':
3305 case ']':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003306 l->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06003307 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003308 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003309 if (l->buf[l->i] == '\n') {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003310 l->lex = XC_LEX_WHITESPACE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003311 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003312 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003313 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003314 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003315 case '^':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003316 bc_lex_assign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003317 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003318 case 'a':
3319 case 'b':
3320 case 'c':
3321 case 'd':
3322 case 'e':
3323 case 'f':
3324 case 'g':
3325 case 'h':
3326 case 'i':
3327 case 'j':
3328 case 'k':
3329 case 'l':
3330 case 'm':
3331 case 'n':
3332 case 'o':
3333 case 'p':
3334 case 'q':
3335 case 'r':
3336 case 's':
3337 case 't':
3338 case 'u':
3339 case 'v':
3340 case 'w':
3341 case 'x':
3342 case 'y':
3343 case 'z':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003344 s = zbc_lex_identifier();
Gavin Howard01055ba2018-11-03 11:00:21 -06003345 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003346 case '{':
3347 case '}':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003348 l->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003349 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003350 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003351 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003352 if (c2 == '|') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003353 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3354 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003355 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003356 l->lex = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003357 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003358 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003359 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003360 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003361 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003362 default:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003363 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003364 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003365 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003366 }
3367
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003368 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003369}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003370#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003371#endif // ENABLE_BC
3372
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003373#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003374static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003375{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003376 BcLex *l = &G.prs.l;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003377 if (G_exreg && isspace(l->buf[l->i])) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003378 bc_lex_whitespace(); // eats whitespace (but not newline)
Denys Vlasenko81293c82018-12-24 01:53:55 +01003379 l->i++; // bc_lex_name() expects this
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003380 bc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003381 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003382 bc_vec_pop_all(&l->lex_buf);
3383 bc_vec_push(&l->lex_buf, &l->buf[l->i++]);
3384 bc_vec_pushZeroByte(&l->lex_buf);
3385 l->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003386 }
3387
Denys Vlasenko29301232018-12-11 15:29:32 +01003388 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003389}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003390#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003391
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003392static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003393{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003394 BcLex *l = &G.prs.l;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003395 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003396
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003397 l->lex = XC_LEX_STR;
3398 bc_vec_pop_all(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003399
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003400 nls = 0;
3401 depth = 1;
3402 i = l->i;
3403 for (;;) {
3404 char c = l->buf[i];
3405 if (c == '\0') {
3406 l->i = i;
3407 RETURN_STATUS(bc_error("string end could not be found"));
3408 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003409 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003410 if (i == l->i || l->buf[i - 1] != '\\') {
3411 if (c == '[') depth++;
3412 if (c == ']')
3413 if (--depth == 0)
3414 break;
3415 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003416 bc_vec_push(&l->lex_buf, &l->buf[i]);
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003417 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003418 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003419 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003420
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003421 bc_vec_pushZeroByte(&l->lex_buf);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003422 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3423 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3424 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003425 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003426 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003427
3428 l->i = i;
3429 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003430 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003431
Denys Vlasenko29301232018-12-11 15:29:32 +01003432 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003433}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003434#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003435
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003436#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003437static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003438{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003439 BcLex *l = &G.prs.l;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003440 static const //BcLexType - should be this type, but narrower type saves size:
3441 uint8_t
3442 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003443 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 +01003444 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3445 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003446 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003447 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003448
Denys Vlasenko81293c82018-12-24 01:53:55 +01003449 BcStatus s;
3450 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003451 size_t i;
3452
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003453 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003454 if (l->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003455 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003456 }
3457
Denys Vlasenko81293c82018-12-24 01:53:55 +01003458 s = BC_STATUS_SUCCESS;
3459 c = l->buf[l->i++];
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003460 if (c >= '%' && c <= '~'
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003461 && (l->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003462 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003463 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003464 }
3465
3466 // This is the workhorse of the lexer.
3467 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003468// case '\0': // probably never reached
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003469// l->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003470// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003471 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003472 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003473 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003474 // only to allow interactive dc have a way to exit
3475 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003476 // on <enter>, not on _next_ token (which would mean
3477 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003478 // IOW: typing "1p<enter>" should print "1" _at once_,
3479 // not after some more input.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003480 l->lex = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003481 l->newline = true;
3482 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003483 case '\t':
3484 case '\v':
3485 case '\f':
3486 case '\r':
3487 case ' ':
Denys Vlasenko81293c82018-12-24 01:53:55 +01003488 l->newline = 0; // was (c == '\n')
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003489 bc_lex_whitespace();
Gavin Howard01055ba2018-11-03 11:00:21 -06003490 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003491 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003492 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003493 if (c2 == '=')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003494 l->lex = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 else if (c2 == '<')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003496 l->lex = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003497 else if (c2 == '>')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003498 l->lex = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003499 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003500 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003501 ++l->i;
3502 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003503 case '#':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003504 bc_lex_lineComment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003505 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003506 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003507 if (isdigit(l->buf[l->i]))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003508 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003509 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003510 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003511 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003512 case '0':
3513 case '1':
3514 case '2':
3515 case '3':
3516 case '4':
3517 case '5':
3518 case '6':
3519 case '7':
3520 case '8':
3521 case '9':
3522 case 'A':
3523 case 'B':
3524 case 'C':
3525 case 'D':
3526 case 'E':
3527 case 'F':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003528 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003529 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003530 case '[':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003531 s = zdc_lex_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06003532 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003533 default:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003534 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003535 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003536 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003537 }
3538
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003539 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003540}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003541#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003542#endif // ENABLE_DC
3543
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003544static void bc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003545{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003546 BcParse *p = &G.prs;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003547 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003548 bc_vec_pushByte(&p->func->code, i);
3549}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003550
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003551static void bc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003552{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003553 while (*name)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003554 bc_parse_push(*name++);
3555 bc_parse_push(BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003556}
3557
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003558static void bc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003559{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003560 size_t mask;
3561 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003562
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003563 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003564 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3565 amt = sizeof(idx);
3566 do {
3567 if (idx & mask) break;
3568 mask >>= 8;
3569 amt--;
3570 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003571
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003572 bc_parse_push(amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003573
3574 while (idx != 0) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003575 bc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003576 idx >>= 8;
3577 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003578}
3579
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003580#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003581static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003582{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003583 bc_parse_push(BC_INST_JUMP);
3584 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003585}
3586
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003587static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003588{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003589 bc_parse_push(BC_INST_JUMP_ZERO);
3590 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003591}
3592
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003593static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003594{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003595 BcParse *p = &G.prs;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003596 char *str = xstrdup(p->l.lex_buf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003597
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003598 bc_parse_push(XC_INST_STR);
3599 bc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003600 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003601
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003602 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003603}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003604#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003605#endif
3606
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003607static void bc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003608{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003609 BcParse *p = &G.prs;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003610 char *num = xstrdup(p->l.lex_buf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003611#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003612 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003613#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003614 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003615#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003616 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003617#endif
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003618 bc_parse_push(XC_INST_NUM);
3619 bc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003620}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003621
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003622static BC_STATUS zbc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003623{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003624 BcParse *p = &G.prs;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003625 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003626
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003627 RETURN_STATUS(zbc_lex_text_init(text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003628}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003629#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003630
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003631// Called when parsing or execution detects a failure,
3632// resets execution structures.
3633static void bc_program_reset(void)
3634{
3635 BcFunc *f;
3636 BcInstPtr *ip;
3637
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003638 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003639 bc_vec_pop_all(&G.prog.results);
3640
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003641 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003642 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003643 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003644}
3645
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003646// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003647// resets parsing structures.
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003648static void bc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003649{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003650 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003651 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003652 bc_func_free(p->func);
3653 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003654
Denys Vlasenko65e10462018-12-19 15:13:14 +01003655 p->fidx = BC_PROG_MAIN;
3656 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003657 }
3658
3659 p->l.i = p->l.len;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003660 p->l.lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003661
Denys Vlasenko503faf92018-12-20 16:24:18 +01003662 IF_BC(bc_vec_pop_all(&p->exits);)
3663 IF_BC(bc_vec_pop_all(&p->conds);)
3664 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003665
Denys Vlasenkod38af482018-12-04 19:11:02 +01003666 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003667}
3668
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003669static void bc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003670{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003671 BcParse *p = &G.prs;
Denys Vlasenko503faf92018-12-20 16:24:18 +01003672 IF_BC(bc_vec_free(&p->exits);)
3673 IF_BC(bc_vec_free(&p->conds);)
3674 IF_BC(bc_vec_free(&p->ops);)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003675 bc_lex_free();
Gavin Howard01055ba2018-11-03 11:00:21 -06003676}
3677
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003678static void bc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003679{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003680 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003681 memset(p, 0, sizeof(BcParse));
3682
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003683 bc_lex_init();
Denys Vlasenko503faf92018-12-20 16:24:18 +01003684 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3685 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3686 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003687
Denys Vlasenkofa210792018-12-19 19:35:40 +01003688 p->fidx = fidx;
3689 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003690}
3691
Denys Vlasenko04715442018-12-21 00:10:26 +01003692static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003693{
Denys Vlasenko04715442018-12-21 00:10:26 +01003694 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003695 BcFunc f;
3696 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003697 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003698 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003699 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003700}
3701
3702#if ENABLE_BC
3703
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003704// Note: takes ownership of 'name' (must be malloced)
3705static size_t bc_program_addFunc(char *name)
3706{
3707 size_t idx;
3708 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003709 int inserted;
3710
3711 entry.name = name;
3712 entry.idx = G.prog.fns.len;
3713
3714 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3715 if (!inserted) free(name);
3716
3717 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3718 idx = entry_ptr->idx;
3719
3720 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003721 // There is already a function with this name.
3722 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003723 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003724 bc_func_free(func);
3725 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003726 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003727 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003728 }
3729
3730 return idx;
3731}
3732
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003733#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003734// We can calculate the conversion between tokens and exprs by subtracting the
3735// position of the first operator in the lex enum and adding the position of the
3736// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003737#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003738
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003739static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003740
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003741static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003742{
3743 BcStatus s;
3744
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003745 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003746 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3747 RETURN_STATUS(bc_error("empty expression"));
3748 RETURN_STATUS(s);
3749}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003750#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003751
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003752static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003753#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003754
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003755static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003756{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003757 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003758}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003759#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003760
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003761static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003762{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003763 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003764 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3765 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003766 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003767 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003768 if (p->l.lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003769 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003770
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003771 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003772}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003773#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003774
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003775static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003776{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003777 BcParse *p = &G.prs;
Denys Vlasenko69560f42018-12-24 14:14:23 +01003778 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3779 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003780
3781 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003782 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003783 if (t == BC_LEX_LPAREN) break;
3784
Denys Vlasenko69560f42018-12-24 14:14:23 +01003785 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003786 if (l >= r && (l != r || !left)) break;
3787
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003788 bc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003789 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003790 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003791 }
3792
3793 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003794}
3795
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003796static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003797{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003798 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 BcLexType top;
3800
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003801 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003802 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003803 top = BC_PARSE_TOP_OP(p);
3804
3805 while (top != BC_LEX_LPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003806 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003807
3808 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003809 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003810
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003811 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003812 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003813 top = BC_PARSE_TOP_OP(p);
3814 }
3815
3816 bc_vec_pop(&p->ops);
3817
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003818 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003819}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003820#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003821
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003822static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003823{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003824 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003825 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003826 size_t nparams;
3827
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003828 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003829 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3830
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003831 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003832 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003833
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003834 nparams = 0;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003835 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003836 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003837 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003838 if (s) RETURN_STATUS(s);
3839 nparams++;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003840 if (p->l.lex != BC_LEX_COMMA) {
3841 if (p->l.lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003842 break;
3843 RETURN_STATUS(bc_error_bad_token());
3844 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003845 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003846 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 }
3848 }
3849
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003850 bc_parse_push(BC_INST_CALL);
3851 bc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003852
Denys Vlasenko26819db2018-12-12 16:08:46 +01003853 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003854}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003855#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003856
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003857// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003858static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003859{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003860 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003861 BcStatus s;
3862 BcId entry, *entry_ptr;
3863 size_t idx;
3864
3865 entry.name = name;
3866
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003867 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 if (s) goto err;
3869
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003870 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003871 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 goto err;
3873 }
3874
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003875 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003876
3877 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003878 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003879 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003880 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003881 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 free(name);
3883
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003884 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003885 bc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003886
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003887 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003888 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003889 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003890 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003891}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003892#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003893
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003894static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003895{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003896 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 BcStatus s;
3898 char *name;
3899
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003900 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003901 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003902 if (s) goto err;
3903
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003904 if (p->l.lex == BC_LEX_LBRACKET) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003905 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003906 if (s) goto err;
3907
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003908 if (p->l.lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003909 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003910 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003911 goto err;
3912 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003913 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003914 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003915 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003916 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003917 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003918 if (s) goto err;
3919 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003920 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003921 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003922 bc_parse_push(*type);
3923 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003924 free(name);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003925 } else if (p->l.lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003926 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003927 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003928 goto err;
3929 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003930 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003931 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003932 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003933 *type = XC_INST_VAR;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003934 bc_parse_push(XC_INST_VAR);
3935 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003936 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003937 }
3938
Denys Vlasenko26819db2018-12-12 16:08:46 +01003939 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003940 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003941 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003942 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003943}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003944#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003945
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003946static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003947{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003948 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003949 BcStatus s;
3950
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003951 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003952 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003953 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003954
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003955 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003956 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003957 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003958
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003959 bc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003960
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003961 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003962}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003963#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003964
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003965static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003966{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003967 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003968 BcStatus s;
3969
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003970 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003971 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003972 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003973
3974 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3975
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003976 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003977 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003978
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003979 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003980 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003981
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003982 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003983
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003984 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003985 bc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003986
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003987 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003988}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003989#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003990
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003991static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003992{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003993 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003994 BcStatus s;
3995
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003996 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003997 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003998
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003999 if (p->l.lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004000 *type = XC_INST_SCALE;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004001 bc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004002 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004003 }
4004
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004005 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06004006 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4007
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004008 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004009 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004010
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004011 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004012 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004013 if (p->l.lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01004014 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004015 bc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004016
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004017 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004018}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004019#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004020
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004021static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
4022 size_t *nexprs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004023{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004024 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004025 BcStatus s;
4026 BcLexType type;
4027 char inst;
4028 BcInst etype = *prev;
4029
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004030 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4031 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4032 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004033 ) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004034 *prev = inst = BC_INST_INC_POST + (p->l.lex != BC_LEX_OP_INC);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004035 bc_parse_push(inst);
4036 s = zbc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004037 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004038 *prev = inst = BC_INST_INC_PRE + (p->l.lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004039 *paren_expr = true;
4040
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004041 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004042 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004043 type = p->l.lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004044
4045 // Because we parse the next part of the expression
4046 // right here, we need to increment this.
4047 *nexprs = *nexprs + 1;
4048
4049 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004050 case XC_LEX_NAME:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004051 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004052 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004053 case BC_LEX_KEY_IBASE:
4054 case BC_LEX_KEY_LAST:
4055 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004056 bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4057 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004058 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004059 case BC_LEX_KEY_SCALE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004060 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004061 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004062 if (p->l.lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004063 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004064 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004065 bc_parse_push(XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004066 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004067 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004068 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004069 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004070 }
4071
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004072 if (!s) bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004073 }
4074
Denys Vlasenko26819db2018-12-12 16:08:46 +01004075 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004076}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004077#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004078
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004079#if 0
4080#define BC_PARSE_LEAF(p, rparen) \
4081 ((rparen) \
4082 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
4083 || (p) == BC_INST_INC_POST \
4084 || (p) == BC_INST_DEC_POST \
4085 )
4086#else
4087static int ok_in_expr(BcInst p)
4088{
4089 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4090 || p == BC_INST_INC_POST
4091 || p == BC_INST_DEC_POST
4092 ;
4093}
4094#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
4095#endif
4096
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004097static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
4098 bool rparen, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004099{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004100 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004101 BcStatus s;
4102 BcLexType type;
4103 BcInst etype = *prev;
4104
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004105 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004106 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004107
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004108 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004109 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004110
4111 // We can just push onto the op stack because this is the largest
4112 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004113 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004114 bc_vec_push(&p->ops, &type);
4115 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004116 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004117
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004118 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004119}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004120#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004121
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004122static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004123{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004124 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004125 BcStatus s;
4126 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004127
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004128 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004129 s = zbc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004130 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004131 type = p->l.lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004132 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004133 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004134 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004135 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004136 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004137 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004138 bc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004139 if (p->l.lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004140 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004141 }
4142
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004143 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004144}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004145#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004146
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004147static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004148{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004149 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004150 BcStatus s;
4151 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004152
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004153 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004154 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004155 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004157 t = p->l.lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004158 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004159 bc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004160 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004161 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004162 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004163 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004164 bc_parse_push(BC_INST_RET0);
4165 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004166 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004167 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004168
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004169 if (!paren || p->l.lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004170 s = zbc_POSIX_requires("parentheses around return expressions");
4171 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004172 }
4173
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004174 bc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004175 }
4176
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004177 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004178 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004179}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004180#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004181
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004182static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004183{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004184 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004185 size_t *label = bc_vec_item(&p->func->labels, idx);
4186 *label = p->func->code.len;
4187}
4188
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004189static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004190{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004191 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004192 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004193 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004194
4195 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004196 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004197 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004198 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004199
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004200 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004201 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004202 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004203 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004204 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004205
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004206 // Encode "if zero, jump to ..."
4207 // Pushed value (destination of the jump) is uninitialized,
4208 // will be rewritten to be address of "end of if()" or of "else".
4209 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004210 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004211
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004212 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004213 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004214
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004215 dbg_lex("%s:%d in if after stmt: p->l.lex:%d", __func__, __LINE__, p->l.lex);
4216 if (p->l.lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004217 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004218
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004219 // Encode "after then_stmt, jump to end of if()"
4220 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4221 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004222 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004223
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004224 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004225 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004226
Denys Vlasenko15850832018-12-16 21:40:54 +01004227 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004228
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004229 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004230 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004231 }
4232
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004233 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004234 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004235
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004236 dbg_lex_done("%s:%d done", __func__, __LINE__);
4237 RETURN_STATUS(s);
4238}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004239#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004240
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004241static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004242{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004243 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004244 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004245 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004246 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004247
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004248 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004249 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004250 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004251 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004252 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004253
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004254 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004255 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004256 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004257
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004258 bc_vec_push(&p->exits, &ip_idx);
4259 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004260
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004261 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004262 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004263 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004264
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004265 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004266
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004267 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004268 if (s) RETURN_STATUS(s);
4269
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004270 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004271 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004272
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004273 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004274 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004275
4276 bc_vec_pop(&p->exits);
4277 bc_vec_pop(&p->conds);
4278
4279 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004280}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004281#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004282
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004283static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004284{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004285 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004286 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004287 size_t cond_idx, exit_idx, body_idx, update_idx;
4288
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004289 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004290 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004291 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004292 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004293 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004294 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004295
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004296 if (p->l.lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004297 s = zbc_parse_expr(0);
4298 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004299 if (s) RETURN_STATUS(s);
4300 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004301 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4302 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004303 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004305 if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004306 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004307 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004308
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004309 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004310 update_idx = cond_idx + 1;
4311 body_idx = update_idx + 1;
4312 exit_idx = body_idx + 1;
4313
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004314 if (p->l.lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004315 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004316 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004317 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004318 // This is safe to set because the current token is a semicolon,
4319 // which has no string requirement.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004320 bc_vec_string(&p->l.lex_buf, 1, "1");
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004321 bc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004322 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004323 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004324 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004325
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004326 if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004327
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004328 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004329 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004330
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004331 bc_parse_pushJUMP_ZERO(exit_idx);
4332 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004333
Gavin Howard01055ba2018-11-03 11:00:21 -06004334 bc_vec_push(&p->conds, &update_idx);
4335 bc_vec_push(&p->func->labels, &p->func->code.len);
4336
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004337 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004338 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004339 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004340 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004341 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004342 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004343 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4344 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004345 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004346
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004347 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004348 bc_vec_push(&p->func->labels, &p->func->code.len);
4349
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004350 bc_vec_push(&p->exits, &exit_idx);
4351 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004352
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004353 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004354 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004355
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004356 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004357 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004358
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004359 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004360 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004361
4362 bc_vec_pop(&p->exits);
4363 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004364
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004365 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004366}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004367#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004368
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004369static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004370{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004371 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004372 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004373
Gavin Howard01055ba2018-11-03 11:00:21 -06004374 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004375 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4376 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004377 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004378 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004379 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004380 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004381 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004382
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004383 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004384}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004385#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004386
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004387static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4388{
4389 BcId *autoid;
4390 BcId a;
4391 size_t i;
4392
4393 autoid = (void*)f->autos.v;
4394 for (i = 0; i < f->autos.len; i++, autoid++) {
4395 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004396 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004397 }
4398
4399 a.idx = var;
4400 a.name = name;
4401
4402 bc_vec_push(&f->autos, &a);
4403
4404 RETURN_STATUS(BC_STATUS_SUCCESS);
4405}
4406#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4407
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004408static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004409{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004410 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004411 BcStatus s;
4412 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004413 char *name;
4414
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004415 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004416 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004417 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004418 if (p->l.lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004419 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004420
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004421 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004422 p->fidx = bc_program_addFunc(name);
4423 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004424
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004425 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004426 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004427 if (p->l.lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004428 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004429 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004430 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004431
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004432 while (p->l.lex != BC_LEX_RPAREN) {
4433 if (p->l.lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004434 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004435
4436 ++p->func->nparams;
4437
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004438 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004439 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004440 if (s) goto err;
4441
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004442 var = p->l.lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004443
4444 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004445 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004446 if (s) goto err;
4447
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004448 if (p->l.lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004449 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004450 goto err;
4451 }
4452
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004453 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 if (s) goto err;
4455 }
4456
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004457 comma = p->l.lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004458 if (comma) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004459 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 if (s) goto err;
4461 }
4462
Denys Vlasenko29301232018-12-11 15:29:32 +01004463 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004464 if (s) goto err;
4465 }
4466
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004467 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004468
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004469 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004470 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004471
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004472 if (p->l.lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004473 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004474 if (s) RETURN_STATUS(s);
4475 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004476
Denys Vlasenko202dd192018-12-16 17:30:35 +01004477 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004478 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004479 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004480 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004481 if (p->l.lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004482 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004483
4484 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004485 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004486 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004487 if (s) RETURN_STATUS(s);
4488
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004489 bc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004490
4491 // Subsequent code generation is into main program
4492 p->fidx = BC_PROG_MAIN;
4493 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004494
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004495 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004496 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004497 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004498 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004500 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004501}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004502#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004503
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004504static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004505{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004506 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004507 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004508 char *name;
4509
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004510 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004511 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004512 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004513
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004514 for (;;) {
4515 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004516
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004517 if (p->l.lex != XC_LEX_NAME)
4518 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4519
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004520 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004521 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004522 if (s) goto err;
4523
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004524 var = (p->l.lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004525 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004526 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004527 if (s) goto err;
4528
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004529 if (p->l.lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004530 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004531 goto err;
4532 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004533 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 if (s) goto err;
4535 }
4536
Denys Vlasenko29301232018-12-11 15:29:32 +01004537 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004538 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004539
4540 if (p->l.lex == XC_LEX_NLINE
4541 || p->l.lex == BC_LEX_SCOLON
4542 //|| p->l.lex == BC_LEX_RBRACE // allow "define f() {auto a}"
4543 ) {
4544 break;
4545 }
4546 if (p->l.lex != BC_LEX_COMMA)
4547 RETURN_STATUS(bc_error("bad 'auto' syntax"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004548 s = zbc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004549 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 }
4551
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004552 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004553 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004554 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004555 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004556 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004557 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004558}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004559#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004560
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004561#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004562static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004563{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004564 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004565 BcStatus s = BC_STATUS_SUCCESS;
4566
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004567 dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004568
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004569 if (p->l.lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004570 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004571 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004572 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004573 if (p->l.lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004574 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004575 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004576 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004577
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004578 if (p->l.lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004579 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4580 do {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004581 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004582 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004583 } while (p->l.lex == XC_LEX_NLINE);
4584 if (auto_allowed && p->l.lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004585 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004586 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004587 if (s) RETURN_STATUS(s);
4588 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004589 while (p->l.lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004590 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004591 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004592 if (s) RETURN_STATUS(s);
4593 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004594 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004595 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4596 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004597 }
4598
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004599 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
4600 switch (p->l.lex) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004601 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004602 case BC_LEX_OP_INC:
4603 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 case BC_LEX_OP_BOOL_NOT:
4605 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004606 case XC_LEX_NAME:
4607 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004608 case BC_LEX_KEY_IBASE:
4609 case BC_LEX_KEY_LAST:
4610 case BC_LEX_KEY_LENGTH:
4611 case BC_LEX_KEY_OBASE:
4612 case BC_LEX_KEY_READ:
4613 case BC_LEX_KEY_SCALE:
4614 case BC_LEX_KEY_SQRT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004615 s = zbc_parse_expr(BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004617 case XC_LEX_STR:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004618 s = zbc_parse_pushSTR();
4619 bc_parse_push(XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 case BC_LEX_KEY_BREAK:
4622 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004623 s = zbc_parse_break_or_continue(p->l.lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004625 case BC_LEX_KEY_FOR:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004626 s = zbc_parse_for();
Gavin Howard01055ba2018-11-03 11:00:21 -06004627 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 case BC_LEX_KEY_HALT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004629 bc_parse_push(BC_INST_HALT);
4630 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004631 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004632 case BC_LEX_KEY_IF:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004633 s = zbc_parse_if();
Gavin Howard01055ba2018-11-03 11:00:21 -06004634 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004635 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004636 // "limits" is a compile-time command,
4637 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004638 printf(
4639 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4640 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4641 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4642 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4643 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4644 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4645 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4646 "Number of vars = "BC_MAX_VARS_STR "\n"
4647 );
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004648 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004649 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004650 case BC_LEX_KEY_PRINT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004651 s = zbc_parse_print();
Gavin Howard01055ba2018-11-03 11:00:21 -06004652 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004653 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004654 // "quit" is a compile-time command. For example,
4655 // "if (0 == 1) quit" terminates when parsing the statement,
4656 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004657 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004658 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004659 if (!p->in_funcdef)
4660 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004661 s = zbc_parse_return();
Gavin Howard01055ba2018-11-03 11:00:21 -06004662 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004663 case BC_LEX_KEY_WHILE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004664 s = zbc_parse_while();
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004667 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004668 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004669 }
4670
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004671 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004672 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004673}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004674#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004675
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004676static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004677{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004678 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004679 BcStatus s;
4680
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004681 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004682 if (p->l.lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004683 s = bc_error("end of file");
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004684 else if (p->l.lex == BC_LEX_KEY_DEFINE) {
4685 dbg_lex("%s:%d p->l.lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004686 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004687 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004688 dbg_lex("%s:%d p->l.lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004689 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004690 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004691
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004692 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004693 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004694}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004695#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004696
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004697// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004698static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004699{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004700 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004701 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004703 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004704 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004705
Denys Vlasenko17df8822018-12-14 23:00:24 +01004706 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004707 paren_first = (p->l.lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004708 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004709 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004710 bin_last = true;
4711
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004712 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004713 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004714 BcStatus s;
4715 BcLexType t = p->l.lex;
4716
4717 if (!lex_allowed_in_bc_expr(t))
4718 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004719
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004720 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004721 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004722 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004723 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004724 case BC_LEX_OP_INC:
4725 case BC_LEX_OP_DEC:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004726 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004727 rprn = bin_last = false;
4728 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004729 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004730 case XC_LEX_OP_MINUS:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004731 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004732 rprn = false;
4733 //get_token = false; - already is
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004734 bin_last = (prev == XC_INST_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004735 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 case BC_LEX_OP_ASSIGN_POWER:
4737 case BC_LEX_OP_ASSIGN_MULTIPLY:
4738 case BC_LEX_OP_ASSIGN_DIVIDE:
4739 case BC_LEX_OP_ASSIGN_MODULUS:
4740 case BC_LEX_OP_ASSIGN_PLUS:
4741 case BC_LEX_OP_ASSIGN_MINUS:
4742 case BC_LEX_OP_ASSIGN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004743 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4744 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4745 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004746 ) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004747 return bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004748 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004749 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004750 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004751 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004752 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004753 case XC_LEX_OP_POWER:
4754 case XC_LEX_OP_MULTIPLY:
4755 case XC_LEX_OP_DIVIDE:
4756 case XC_LEX_OP_MODULUS:
4757 case XC_LEX_OP_PLUS:
4758 case XC_LEX_OP_REL_EQ:
4759 case XC_LEX_OP_REL_LE:
4760 case XC_LEX_OP_REL_GE:
4761 case XC_LEX_OP_REL_NE:
4762 case XC_LEX_OP_REL_LT:
4763 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004764 case BC_LEX_OP_BOOL_NOT:
4765 case BC_LEX_OP_BOOL_OR:
4766 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004767 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004768 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004769 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004770 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004771 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004772 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004773 prev = BC_TOKEN_2_INST(t);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004774 bc_parse_operator(t, ops_bgn, &nexprs);
4775 s = zbc_lex_next();
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004776 rprn = false;
4777 //get_token = false; - already is
Denys Vlasenko69560f42018-12-24 14:14:23 +01004778 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004780 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004781 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004782 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 bc_vec_push(&p->ops, &t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004784 nparens++;
4785 get_token = true;
4786 paren_expr = false;
4787 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004788 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004789 case BC_LEX_RPAREN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004790 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004791 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004792 if (nparens == 0) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004793 goto exit_loop;
Gavin Howard01055ba2018-11-03 11:00:21 -06004794 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004795 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004796 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004797 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004798 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004799 s = zbc_parse_rightParen(ops_bgn, &nexprs);
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004800 nparens--;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004801 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004802 paren_expr = rprn = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004803 bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004804 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004805 case XC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004806 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004807 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004808 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004809 paren_expr = true;
4810 rprn = bin_last = false;
4811 //get_token = false; - already is
Denys Vlasenkobb116032018-12-25 01:16:37 +01004812 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004813 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004814 case XC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004815 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004816 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004817 bc_parse_pushNUM();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004818 prev = XC_INST_NUM;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004819 get_token = true;
4820 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004821 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004822 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004823 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004824 case BC_LEX_KEY_IBASE:
4825 case BC_LEX_KEY_LAST:
4826 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004827 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004828 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004829 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004830 bc_parse_push((char) prev);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004831 get_token = true;
4832 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004833 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004834 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004835 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004836 case BC_LEX_KEY_LENGTH:
4837 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004838 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004839 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004840 s = zbc_parse_builtin(t, flags, &prev);
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004841 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004842 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004843 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004844 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004845 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004846 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004847 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004848 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004849 s = zbc_parse_read();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004850 prev = XC_INST_READ;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004851 get_token = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004852 paren_expr = true;
4853 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004854 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004855 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004856 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004857 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004858 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004859 s = zbc_parse_scale(&prev, flags);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004860 prev = XC_INST_SCALE;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004861 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004862 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004863 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004864 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004865 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004866 default:
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004867 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004868 }
4869
Denys Vlasenkobb116032018-12-25 01:16:37 +01004870 if (s || G_interrupt) // error, or ^C: stop parsing
4871 return BC_STATUS_FAILURE;
4872 if (get_token) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004873 s = zbc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004874 if (s) return s;
4875 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004876 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004877 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004878
4879 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004880 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004881 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004882
4883 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004884 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004885
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004886 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004887
Denys Vlasenko69560f42018-12-24 14:14:23 +01004888 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004889 bc_vec_pop(&p->ops);
4890 }
4891
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004892 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004893 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004894
Gavin Howard01055ba2018-11-03 11:00:21 -06004895 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004896 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004897 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4898 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004899 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004900 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004901 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4902 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004903 }
4904
4905 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004906 if (paren_first || !assign)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004907 bc_parse_push(XC_INST_PRINT);
4908 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004909 }
4910
Denys Vlasenko17df8822018-12-14 23:00:24 +01004911 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004912 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004913}
4914
Gavin Howard01055ba2018-11-03 11:00:21 -06004915#endif // ENABLE_BC
4916
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004917#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004918
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004919static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004920{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004921 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004922 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004923
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004924 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004925 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004926 if (p->l.lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004927
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004928 bc_parse_pushName(p->l.lex_buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004929
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004930 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004931}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004932#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004933
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004934static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004935{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004936 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004937 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004938 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004939
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004940 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004941
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004942 str = xstrdup(p->l.lex_buf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004943 bc_parse_push(XC_INST_STR);
4944 bc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004945 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004946
4947 // Explanation needed here
4948 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004949 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004950
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004951 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004952}
4953
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004954static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004955{
4956 BcStatus s;
4957
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004958 bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004959 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004960 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004961 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004962 }
4963
4964 if (store) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004965 bc_parse_push(DC_INST_SWAP);
4966 bc_parse_push(XC_INST_ASSIGN);
4967 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004968 }
4969
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004970 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004971}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004972#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004973
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004974static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004975{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004976 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004977 BcStatus s;
4978
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004979 bc_parse_push(inst);
4980 bc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004982 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004983 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004984
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004985 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004986 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004987
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004988 // Note that 'else' part can not be on the next line:
4989 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4990 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004991 if (p->l.lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004992 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004993 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004994 s = zbc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004995 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004996 bc_parse_push(BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004997 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004998
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004999 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005000}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005001#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005002
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005003static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005004{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005005 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005006 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005007 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005008
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005009 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005010 s = BC_STATUS_SUCCESS;
5011 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005012 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01005013 case XC_LEX_OP_REL_EQ:
5014 case XC_LEX_OP_REL_LE:
5015 case XC_LEX_OP_REL_GE:
5016 case XC_LEX_OP_REL_NE:
5017 case XC_LEX_OP_REL_LT:
5018 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005019 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005020 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005021 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06005022 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005023 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005024 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005025 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005026 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005027 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005028 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005029 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005030 dc_parse_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06005031 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01005032 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005033 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005034 s = zbc_lex_next();
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005035 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005036 if (G.prs.l.lex != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005037 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005038 bc_parse_pushNUM();
5039 bc_parse_push(XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005040 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005041 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005042 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005043 bc_parse_pushNUM();
Gavin Howard01055ba2018-11-03 11:00:21 -06005044 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005045 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005046 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005047 bc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005048 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005049 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005050 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005051 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005052 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005053 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005054 s = zdc_parse_mem(inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005055 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005056 case DC_LEX_LOAD:
5057 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005058 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005059 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005060 s = zdc_parse_mem(inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005061 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005062 case DC_LEX_STORE_IBASE:
5063 case DC_LEX_STORE_SCALE:
5064 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005065 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005066 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005067 s = zdc_parse_mem(inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005068 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005069 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005070 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5071 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005072 }
5073
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005074 if (!s && get_token) s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005075
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005076 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005077 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005078}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005079#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005080
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005081static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005082{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005083 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005084 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005085
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005086 i = (int)p->l.lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005087 if (i >= 0) {
5088 BcInst inst = dc_LEX_to_INST[i];
5089 if (inst != DC_INST_INVALID) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005090 bc_parse_push(inst);
5091 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005092 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005093 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005094 RETURN_STATUS(zdc_parse_token(p->l.lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005095}
5096#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5097
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005098static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005099{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005100 BcParse *p = &G.prs;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005101 dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex);
5102 while (p->l.lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005103 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005104 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005105 }
5106
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005107 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005108 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005109}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005110#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005111
Gavin Howard01055ba2018-11-03 11:00:21 -06005112#endif // ENABLE_DC
5113
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005114//
5115// Execution engine
5116//
5117
5118#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5119#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5120
Denys Vlasenkodf515392018-12-02 19:27:48 +01005121static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005122{
Gavin Howard01055ba2018-11-03 11:00:21 -06005123 BcId e, *ptr;
5124 BcVec *v, *map;
5125 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005126 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005127
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005128 v = var ? &G.prog.vars : &G.prog.arrs;
5129 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005130
5131 e.name = id;
5132 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005133 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005134
5135 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005136 BcVec v2;
5137 bc_array_init(&v2, var);
5138 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005139 }
5140
5141 ptr = bc_vec_item(map, i);
5142 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005143 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005144}
5145
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005146// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005147static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005148{
Gavin Howard01055ba2018-11-03 11:00:21 -06005149 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005150 case XC_RESULT_STR:
5151 case XC_RESULT_TEMP:
5152 case XC_RESULT_IBASE:
5153 case XC_RESULT_SCALE:
5154 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005155 *num = &r->d.n;
5156 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005157 case XC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005158 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005159 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005160 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005161 size_t len;
5162
5163 str = *bc_program_const(r->d.id.idx);
5164 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005165
5166 bc_num_init(&r->d.n, len);
5167
5168 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005169 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005170 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005171 if (s) {
5172 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005173 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005174 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005175 *num = &r->d.n;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005176 r->t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005177 break;
5178 }
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005179 case XC_RESULT_VAR:
5180 case XC_RESULT_ARRAY:
5181 case XC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005182 BcVec *v;
5183
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005184 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005185
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005186 if (r->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005187 v = bc_vec_top(v);
5188 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5189 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005190 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005191 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005192 break;
5193 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005194#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005195 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005196 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005197 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005198 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005199 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005200 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005201#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005202#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005203 default:
5204 // Testing the theory that dc does not reach LAST/ONE
5205 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005206#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005207 }
5208
Denys Vlasenko29301232018-12-11 15:29:32 +01005209 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005210}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005211#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005212
Denys Vlasenko29301232018-12-11 15:29:32 +01005213static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005214 BcResult **r, BcNum **rn, bool assign)
5215{
5216 BcStatus s;
5217 bool hex;
5218 BcResultType lt, rt;
5219
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005220 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005221 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005222
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005223 *r = bc_vec_item_rev(&G.prog.results, 0);
5224 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005225
5226 lt = (*l)->t;
5227 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005228 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005229
Denys Vlasenko29301232018-12-11 15:29:32 +01005230 s = zbc_program_num(*l, ln, false);
5231 if (s) RETURN_STATUS(s);
5232 s = zbc_program_num(*r, rn, hex);
5233 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005234
5235 // We run this again under these conditions in case any vector has been
5236 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005237 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005238 s = zbc_program_num(*l, ln, false);
5239 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005240 }
5241
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005242 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005243 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005244 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005245 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005246
Denys Vlasenko29301232018-12-11 15:29:32 +01005247 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005248}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005249#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005250
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005251static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005252{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005253 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005254 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005255 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005256}
5257
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005258// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005259static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005260{
5261 BcStatus s;
5262
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005263 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005264 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005265 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005266
Denys Vlasenko29301232018-12-11 15:29:32 +01005267 s = zbc_program_num(*r, n, false);
5268 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005269
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005270 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005271 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005272
Denys Vlasenko29301232018-12-11 15:29:32 +01005273 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005274}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005275#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005276
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005277static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005278{
5279 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005280 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005281}
5282
Denys Vlasenko259137d2018-12-11 19:42:05 +01005283static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005284{
5285 BcStatus s;
5286 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005287 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005288
Denys Vlasenko29301232018-12-11 15:29:32 +01005289 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005290 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005291 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005292
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005293 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005294 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 -06005295 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005296 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005297
Denys Vlasenko259137d2018-12-11 19:42:05 +01005298 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005299 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005300 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005301 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005302}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005303#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005304
Denys Vlasenko26819db2018-12-12 16:08:46 +01005305static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005306{
5307 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005308 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005309 BcVec buf;
5310 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005311 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005312
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005313 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005314 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005315
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005316 f = bc_program_func(BC_PROG_READ);
5317 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005318
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005319 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005320 bc_parse_create(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005321 //bc_lex_file(&G.prs.l); - not needed, error line info is not printed for read()
5322
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005323 s = zbc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005324 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005325 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005326 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005327 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005328 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005329 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005330 if (s) goto exec_err;
5331
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005332 if (G.prs.l.lex != XC_LEX_NLINE && G.prs.l.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005333 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005334 goto exec_err;
5335 }
5336
5337 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005338 ip.inst_idx = 0;
5339 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005340
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005341 bc_parse_push(XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005342 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005343
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005344 exec_err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005345 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005346 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005347 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005348 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005349}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005350#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005351
5352static size_t bc_program_index(char *code, size_t *bgn)
5353{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005354 unsigned char *bytes = (void*)(code + *bgn);
5355 unsigned amt;
5356 unsigned i;
5357 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005358
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005359 amt = *bytes++;
5360 *bgn += amt + 1;
5361
5362 amt *= 8;
5363 res = 0;
5364 for (i = 0; i < amt; i += 8)
5365 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005366
5367 return res;
5368}
5369
5370static char *bc_program_name(char *code, size_t *bgn)
5371{
5372 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005373 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005374
Denys Vlasenko694d2982018-12-18 19:17:11 +01005375 code += *bgn;
5376 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005377 i = 0;
5378 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005379 char c = *code++;
5380 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005381 break;
5382 s[i++] = c;
5383 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005384 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005385 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005386
5387 return s;
5388}
5389
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005390static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005391{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005392#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005393 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005394 // Example: echo '[]ap' | dc
5395 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005396 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005397 return;
5398 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005399#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005400 while (*str) {
5401 int c = *str++;
5402 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005403 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005404 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005405 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005406 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005407 case 'a':
5408 bb_putchar('\a');
5409 break;
5410 case 'b':
5411 bb_putchar('\b');
5412 break;
5413 case '\\':
5414 case 'e':
5415 bb_putchar('\\');
5416 break;
5417 case 'f':
5418 bb_putchar('\f');
5419 break;
5420 case 'n':
5421 bb_putchar('\n');
5422 G.prog.nchars = SIZE_MAX;
5423 break;
5424 case 'r':
5425 bb_putchar('\r');
5426 break;
5427 case 'q':
5428 bb_putchar('"');
5429 break;
5430 case 't':
5431 bb_putchar('\t');
5432 break;
5433 default:
5434 // Just print the backslash and following character.
5435 bb_putchar('\\');
5436 ++G.prog.nchars;
5437 bb_putchar(c);
5438 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005439 }
5440 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005441 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005442 }
5443}
5444
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005445static void bc_num_printNewline(void)
5446{
5447 if (G.prog.nchars == G.prog.len - 1) {
5448 bb_putchar('\\');
5449 bb_putchar('\n');
5450 G.prog.nchars = 0;
5451 }
5452}
5453
5454#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005455static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005456{
5457 (void) radix;
5458 bb_putchar((char) num);
5459 G.prog.nchars += width;
5460}
5461#endif
5462
5463static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5464{
5465 size_t exp, pow;
5466
5467 bc_num_printNewline();
5468 bb_putchar(radix ? '.' : ' ');
5469 ++G.prog.nchars;
5470
5471 bc_num_printNewline();
5472 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5473 continue;
5474
5475 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5476 size_t dig;
5477 bc_num_printNewline();
5478 dig = num / pow;
5479 num -= dig * pow;
5480 bb_putchar(((char) dig) + '0');
5481 }
5482}
5483
5484static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5485{
5486 if (radix) {
5487 bc_num_printNewline();
5488 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005489 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005490 }
5491
5492 bc_num_printNewline();
5493 bb_putchar(bb_hexdigits_upcase[num]);
5494 G.prog.nchars += width;
5495}
5496
5497static void bc_num_printDecimal(BcNum *n)
5498{
5499 size_t i, rdx = n->rdx - 1;
5500
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005501 if (n->neg) {
5502 bb_putchar('-');
5503 G.prog.nchars++;
5504 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005505
5506 for (i = n->len - 1; i < n->len; --i)
5507 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5508}
5509
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005510typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5511
Denys Vlasenkod3401432018-12-18 17:00:35 +01005512static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005513{
5514 BcStatus s;
5515 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005516 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005517 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005518 BcNum intp, fracp, digit, frac_len;
5519 unsigned long dig, *ptr;
5520 size_t i;
5521 bool radix;
5522
5523 if (n->len == 0) {
5524 print(0, width, false);
5525 RETURN_STATUS(BC_STATUS_SUCCESS);
5526 }
5527
5528 bc_vec_init(&stack, sizeof(long), NULL);
5529 bc_num_init(&intp, n->len);
5530 bc_num_init(&fracp, n->rdx);
5531 bc_num_init(&digit, width);
5532 bc_num_init(&frac_len, BC_NUM_INT(n));
5533 bc_num_copy(&intp, n);
5534 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005535 base.cap = ARRAY_SIZE(base_digs);
5536 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005537 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005538
5539 bc_num_truncate(&intp, intp.rdx);
5540 s = zbc_num_sub(n, &intp, &fracp, 0);
5541 if (s) goto err;
5542
5543 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005544 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005545 if (s) goto err;
5546 s = zbc_num_ulong(&digit, &dig);
5547 if (s) goto err;
5548 bc_vec_push(&stack, &dig);
5549 }
5550
5551 for (i = 0; i < stack.len; ++i) {
5552 ptr = bc_vec_item_rev(&stack, i);
5553 print(*ptr, width, false);
5554 }
5555
5556 if (!n->rdx) goto err;
5557
5558 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005559 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005560 if (s) goto err;
5561 s = zbc_num_ulong(&fracp, &dig);
5562 if (s) goto err;
5563 bc_num_ulong2num(&intp, dig);
5564 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5565 if (s) goto err;
5566 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005567 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005568 if (s) goto err;
5569 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005570 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005571 bc_num_free(&frac_len);
5572 bc_num_free(&digit);
5573 bc_num_free(&fracp);
5574 bc_num_free(&intp);
5575 bc_vec_free(&stack);
5576 RETURN_STATUS(s);
5577}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005578#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005579
5580static BC_STATUS zbc_num_printBase(BcNum *n)
5581{
5582 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005583 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005584 BcNumDigitOp print;
5585 bool neg = n->neg;
5586
5587 if (neg) {
5588 bb_putchar('-');
5589 G.prog.nchars++;
5590 }
5591
5592 n->neg = false;
5593
5594 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5595 width = 1;
5596 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005597 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005598 unsigned i = G.prog.ob_t - 1;
5599 width = 0;
5600 for (;;) {
5601 width++;
5602 i /= 10;
5603 if (i == 0)
5604 break;
5605 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005606 print = bc_num_printDigits;
5607 }
5608
Denys Vlasenkod3401432018-12-18 17:00:35 +01005609 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005610 n->neg = neg;
5611
5612 RETURN_STATUS(s);
5613}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005614#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005615
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005616static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5617{
5618 BcStatus s = BC_STATUS_SUCCESS;
5619
5620 bc_num_printNewline();
5621
5622 if (n->len == 0) {
5623 bb_putchar('0');
5624 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005625 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005626 bc_num_printDecimal(n);
5627 else
5628 s = zbc_num_printBase(n);
5629
5630 if (newline) {
5631 bb_putchar('\n');
5632 G.prog.nchars = 0;
5633 }
5634
5635 RETURN_STATUS(s);
5636}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005637#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005638
Denys Vlasenko29301232018-12-11 15:29:32 +01005639static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005640{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005641 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005642 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005643 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005644 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005645
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005646 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005647 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005648
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005649 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005650 s = zbc_program_num(r, &num, false);
5651 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005652
5653 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005654 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005655#if ENABLE_BC
5656 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5657#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005658 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005659 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005660
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005661 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005662 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005663
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005664 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005665 for (;;) {
5666 char c = *str++;
5667 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005668 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005669 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005670 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005671 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005672 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005673 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005674 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005675 }
5676 }
5677
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005678 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005679
Denys Vlasenko29301232018-12-11 15:29:32 +01005680 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005681}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005682#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005683
Denys Vlasenko29301232018-12-11 15:29:32 +01005684static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005685{
5686 BcStatus s;
5687 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005688 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005689
Denys Vlasenko29301232018-12-11 15:29:32 +01005690 s = zbc_program_prep(&ptr, &num);
5691 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005692
5693 bc_num_init(&res.d.n, num->len);
5694 bc_num_copy(&res.d.n, num);
5695 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5696
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005697 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005698
Denys Vlasenko29301232018-12-11 15:29:32 +01005699 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005700}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005701#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005702
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005703static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005704{
5705 BcStatus s;
5706 BcResult *opd1, *opd2, res;
5707 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005708 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005709
Denys Vlasenko29301232018-12-11 15:29:32 +01005710 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005711 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005712
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005713 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005714
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005715 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005716 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005717 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005718 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005719 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005720 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005721 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005722 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005723 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005724 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005725 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005726 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005727 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005728 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005729 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005730 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005731 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005732 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005733 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005734 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005735 cond = (cond > 0);
5736 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005737 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005738 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005739 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005740 }
5741 }
5742
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005743 if (cond) bc_num_one(&res.d.n);
5744 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005745
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005746 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005747
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005748 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005749}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005750#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005751
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005752#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005753static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005754{
5755 BcNum n2;
5756 BcResult res;
5757
5758 memset(&n2, 0, sizeof(BcNum));
5759 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005760 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005761
5762 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005763 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005764 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005765 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005766 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005767 }
5768
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005769 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005770 bc_vec_push(v, &n2);
5771
Denys Vlasenko29301232018-12-11 15:29:32 +01005772 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005773}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005774#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005775#endif // ENABLE_DC
5776
Denys Vlasenko29301232018-12-11 15:29:32 +01005777static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005778{
5779 BcStatus s;
5780 BcResult *ptr, r;
5781 BcVec *v;
5782 BcNum *n;
5783
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005784 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005785 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005786
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005787 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005788 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005789 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005790 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005791
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005792#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005793 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005794 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005795 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005796 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005797#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005798
Denys Vlasenko29301232018-12-11 15:29:32 +01005799 s = zbc_program_num(ptr, &n, false);
5800 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005801
5802 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005803 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005804
5805 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005806 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005807 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005808 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005809 bc_array_init(&r.d.v, true);
5810 bc_array_copy(&r.d.v, (BcVec *) n);
5811 }
5812
5813 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005814 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005815
Denys Vlasenko29301232018-12-11 15:29:32 +01005816 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005817}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005818#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005819
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005820static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005821{
5822 BcStatus s;
5823 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005824 BcNum *l, *r;
5825 bool assign = (inst == XC_INST_ASSIGN);
5826 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
Denys Vlasenko29301232018-12-11 15:29:32 +01005828 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005829 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005830
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005831 ib = left->t == XC_RESULT_IBASE;
5832 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005833
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005834#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005835 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005836 BcVec *v;
5837
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005838 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005839 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005840 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005841
Denys Vlasenkofa210792018-12-19 19:35:40 +01005842 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005843 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005844#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005845
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005846 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005847 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005848 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005849 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005850 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005851
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005852#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005853 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005854 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005855
5856 if (assign)
5857 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005858 else {
5859 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005860 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 +01005861 }
5862 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005863#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005864 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005865#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005866
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005867 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005868 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005869 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5870 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5871 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005872 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005873 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005874 size_t max;
5875 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005876
Denys Vlasenko29301232018-12-11 15:29:32 +01005877 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005878 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005879 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005880 if (sc) {
5881 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005882 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005883 } else {
5884 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005885 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005886 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005887 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005888 }
5889
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005890 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005891 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005892
5893 *ptr = (size_t) val;
5894 s = BC_STATUS_SUCCESS;
5895 }
5896
5897 bc_num_init(&res.d.n, l->len);
5898 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005899 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005901 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005903#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005904
Denys Vlasenko416ce762018-12-02 20:57:17 +01005905#if !ENABLE_DC
5906#define bc_program_pushVar(code, bgn, pop, copy) \
5907 bc_program_pushVar(code, bgn)
5908// for bc, 'pop' and 'copy' are always false
5909#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005910static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005911 bool pop, bool copy)
5912{
Gavin Howard01055ba2018-11-03 11:00:21 -06005913 BcResult r;
5914 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005915
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005916 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005917 r.d.id.name = name;
5918
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005919#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005920 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005921 BcVec *v = bc_program_search(name, true);
5922 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005923
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005924 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005925 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005926 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005927 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005928
5929 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005930 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005931 bc_num_init_DEF_SIZE(&r.d.n);
5932 bc_num_copy(&r.d.n, num);
5933 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005934 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005935 r.d.id.idx = num->rdx;
5936 }
5937
5938 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005939 }
5940#endif // ENABLE_DC
5941
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005942 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005943
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005944 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005945}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005946#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005947
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005948static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005949{
5950 BcStatus s = BC_STATUS_SUCCESS;
5951 BcResult r;
5952 BcNum *num;
5953
5954 r.d.id.name = bc_program_name(code, bgn);
5955
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005956 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005957 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005958 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005959 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005960 BcResult *operand;
5961 unsigned long temp;
5962
Denys Vlasenko29301232018-12-11 15:29:32 +01005963 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005965 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005966 if (s) goto err;
5967
5968 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005969 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005970 goto err;
5971 }
5972
5973 r.d.id.idx = (size_t) temp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005974 bc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005975 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005976 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005977 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005978 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005979}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005980#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005982#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005983static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005984{
5985 BcStatus s;
5986 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005987 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005988 char inst2 = inst;
5989
Denys Vlasenko29301232018-12-11 15:29:32 +01005990 s = zbc_program_prep(&ptr, &num);
5991 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005992
5993 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005994 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 bc_num_init(&copy.d.n, num->len);
5996 bc_num_copy(&copy.d.n, num);
5997 }
5998
5999 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006000 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
6001 ? BC_INST_ASSIGN_PLUS
6002 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006003
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006004 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006005 s = zbc_program_assign(inst);
6006 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
6008 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006009 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006010 }
6011
Denys Vlasenko29301232018-12-11 15:29:32 +01006012 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006013}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006014#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko29301232018-12-11 15:29:32 +01006016static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006017{
Gavin Howard01055ba2018-11-03 11:00:21 -06006018 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006019 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006020 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006022 BcResult *arg;
6023
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006024 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006025 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006027 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006028
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006029 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006030 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006031 }
6032 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006033 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006034 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01006035 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
6037 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006038 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006039
6040 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006041 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006042
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006043 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006044 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006045
Denys Vlasenko29301232018-12-11 15:29:32 +01006046 s = zbc_program_copyToVar(a->name, a->idx);
6047 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006048 }
6049
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006050 a = bc_vec_item(&func->autos, i);
6051 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006052 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006053
Denys Vlasenkodf515392018-12-02 19:27:48 +01006054 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006055 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006056 BcNum n2;
6057 bc_num_init_DEF_SIZE(&n2);
6058 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006059 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006060 BcVec v2;
6061 bc_array_init(&v2, true);
6062 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006063 }
6064 }
6065
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006066 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006067
Denys Vlasenko29301232018-12-11 15:29:32 +01006068 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006069}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006070#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenko29301232018-12-11 15:29:32 +01006072static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006073{
Gavin Howard01055ba2018-11-03 11:00:21 -06006074 BcResult res;
6075 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006076 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006077 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006078 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006079
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006080 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 +01006081 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006082
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006083 f = bc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006084 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006085
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006086 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006087 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006088 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006089 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006090
Denys Vlasenko29301232018-12-11 15:29:32 +01006091 s = zbc_program_num(operand, &num, false);
6092 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 bc_num_init(&res.d.n, num->len);
6094 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006095 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006096 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006097 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006098 }
6099
6100 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006101 a = (void*)f->autos.v;
6102 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006103 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006104 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006105 bc_vec_pop(v);
6106 }
6107
Denys Vlasenko24e41942018-12-21 23:01:26 +01006108 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006109 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006110 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006111
Denys Vlasenko29301232018-12-11 15:29:32 +01006112 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006113}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006114#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006115#endif // ENABLE_BC
6116
6117static unsigned long bc_program_scale(BcNum *n)
6118{
6119 return (unsigned long) n->rdx;
6120}
6121
6122static unsigned long bc_program_len(BcNum *n)
6123{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006124 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006125
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006126 if (n->rdx != len) return len;
6127 for (;;) {
6128 if (len == 0) break;
6129 len--;
6130 if (n->num[len] != 0) break;
6131 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 return len;
6133}
6134
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006135static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006136{
6137 BcStatus s;
6138 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006139 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006140 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006141 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006142
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006143 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006144 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006145 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006146
Denys Vlasenko29301232018-12-11 15:29:32 +01006147 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006148 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006149
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006150#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006151 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006152 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006153#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006154
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006155 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006156
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006157 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006158 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006159#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006160 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006161 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006162 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006163#endif
6164#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006166 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006167 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006168
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006169 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006170 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006171 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006172#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006173 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006174 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006175 }
6176
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006177 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006178
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006179 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006180}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006181#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006182
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006183#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006184static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006185{
6186 BcStatus s;
6187 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006188 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006189
Denys Vlasenko29301232018-12-11 15:29:32 +01006190 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006191 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006192
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006193 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006194 bc_num_init(&res2.d.n, n2->len);
6195
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006196 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006197 if (s) goto err;
6198
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006199 bc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006200 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006201 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006203 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006204 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006205 bc_num_free(&res2.d.n);
6206 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006207 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006208}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006209#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006210
Denys Vlasenkofa210792018-12-19 19:35:40 +01006211static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006212{
6213 BcStatus s;
6214 BcResult *r1, *r2, *r3, res;
6215 BcNum *n1, *n2, *n3;
6216
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006217 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006218 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006219 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006220 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006221
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006222 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006223 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006224 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006225 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006226 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006227
6228 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006229 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006230 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006231 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006232 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006233 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006234 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006235 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006236 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006237 }
6238 }
6239
6240 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006241 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006242 if (s) goto err;
6243
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006244 bc_vec_pop(&G.prog.results);
6245 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006246
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006247 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006248 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006249 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006250 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006251}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006252#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006253
Denys Vlasenkofa210792018-12-19 19:35:40 +01006254static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006255{
Gavin Howard01055ba2018-11-03 11:00:21 -06006256 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006257 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006258
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006259 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006260
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006261 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006262 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006263 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006264}
6265
Denys Vlasenkofa210792018-12-19 19:35:40 +01006266static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006267{
6268 BcStatus s;
6269 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006270 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006271 char **strs;
6272 char *str;
6273 char c;
6274 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006275
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006276 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006277 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006278 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279
Denys Vlasenko29301232018-12-11 15:29:32 +01006280 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006281 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006282
6283 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006284 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006285 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006286 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006287
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006288 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 bc_num_copy(&n, num);
6290 bc_num_truncate(&n, n.rdx);
6291
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006292 strmb.cap = ARRAY_SIZE(strmb_digs);
6293 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006294 bc_num_ulong2num(&strmb, 0x100);
6295 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006296
Gavin Howard01055ba2018-11-03 11:00:21 -06006297 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006298 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 if (s) goto num_err;
6300
6301 c = (char) val;
6302
6303 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006304 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006305 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006306 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006307 sp = *bc_program_str(idx);
6308 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006309 }
6310
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006311 strs = (void*)G.prog.strs.v;
6312 for (idx = 0; idx < G.prog.strs.len; idx++) {
6313 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6314 goto dup;
6315 }
6316 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006317 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006319 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006320 bc_vec_push(&G.prog.strs, &str);
6321 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006322 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006323 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006324 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006325
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006326 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006327 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006328 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006329 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006330}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006331#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006332
Denys Vlasenkofa210792018-12-19 19:35:40 +01006333static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006334{
6335 BcStatus s;
6336 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006337 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006338 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006339
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006340 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006341 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006342 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006343
Denys Vlasenko29301232018-12-11 15:29:32 +01006344 s = zbc_program_num(r, &n, false);
6345 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006346
Denys Vlasenko57734c92018-12-17 21:14:05 +01006347 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006348 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006349 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006350 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006351 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006352 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006353 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006354 }
6355
Denys Vlasenko29301232018-12-11 15:29:32 +01006356 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006357}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006358#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006359
Denys Vlasenkofa210792018-12-19 19:35:40 +01006360static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006361{
6362 BcStatus s;
6363 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006364 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006365 unsigned long val;
6366
Denys Vlasenko29301232018-12-11 15:29:32 +01006367 s = zbc_program_prep(&opnd, &num);
6368 if (s) RETURN_STATUS(s);
6369 s = zbc_num_ulong(num, &val);
6370 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006371
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006372 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006373
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006374 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006375 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006376 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006377 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006378 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006379
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006380 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006381
Denys Vlasenko29301232018-12-11 15:29:32 +01006382 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006383}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006384#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006385
Denys Vlasenkofa210792018-12-19 19:35:40 +01006386static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006387{
6388 BcStatus s = BC_STATUS_SUCCESS;
6389 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006390 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 BcInstPtr ip;
6392 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006393
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006394 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006395 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006396
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006397 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006398
6399 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006400 BcNum *n = n; // for compiler
6401 bool exec;
6402 char *name;
6403 char *then_name = bc_program_name(code, bgn);
6404 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006405
6406 if (code[*bgn] == BC_PARSE_STREND)
6407 (*bgn) += 1;
6408 else
6409 else_name = bc_program_name(code, bgn);
6410
6411 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006412 name = then_name;
6413 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006414 exec = true;
6415 name = else_name;
6416 }
6417
6418 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006419 BcVec *v;
6420 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006421 n = bc_vec_top(v);
6422 }
6423
6424 free(then_name);
6425 free(else_name);
6426
6427 if (!exec) goto exit;
6428 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006429 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006430 goto exit;
6431 }
6432
6433 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006434 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006435 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006436 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006437 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006438 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006439 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006440 if (s || !BC_PROG_STR(n)) goto exit;
6441 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006442 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 goto exit;
6444 }
6445
6446 fidx = sidx + BC_PROG_REQ_FUNCS;
6447
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006448 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006449
6450 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006451 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006452 char *str;
6453
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006454 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006455 bc_parse_create(fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006456 str = *bc_program_str(sidx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006457 s = zbc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006458 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006459
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006460 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006461 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006462 bc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006463 if (G.prs.l.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006464 s = bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006465 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006466 G.prs = sv_parse; // struct copy
6467 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006468 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006469 bc_vec_pop_all(&f->code);
6470 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006471 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006472 }
6473
Denys Vlasenko24e41942018-12-21 23:01:26 +01006474 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 ip.func = fidx;
6476
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006477 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006478 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006479
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006480 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006481 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006482 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006483 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006484}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006485#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006486#endif // ENABLE_DC
6487
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006488static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006489{
Gavin Howard01055ba2018-11-03 11:00:21 -06006490 BcResult res;
6491 unsigned long val;
6492
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006493 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006494 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006495 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006496 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006497 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006499 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006500
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006501 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006502 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006503 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006504}
6505
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006506static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006507{
Gavin Howard01055ba2018-11-03 11:00:21 -06006508 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006509 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006510 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006512
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006513 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006514 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6515 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006516 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006517 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006518
Denys Vlasenko24e41942018-12-21 23:01:26 +01006519 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 -06006520 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006521#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006522 case BC_INST_JUMP_ZERO: {
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006523 BcNum *num;
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006524 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006525 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006526 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006527 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006528 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006529 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006530 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006531 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006532 break;
6533 }
6534 // else: fall through
6535 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006536 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006537 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006538 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006539 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006540 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006541 break;
6542 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006543 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006544 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006545 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006546 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006547 case BC_INST_INC_PRE:
6548 case BC_INST_DEC_PRE:
6549 case BC_INST_INC_POST:
6550 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006551 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006552 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006555 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006556 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006557 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006558 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006560 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006561 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006562 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006563 case XC_INST_BOOL_OR:
6564 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006565#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006566 case XC_INST_REL_EQ:
6567 case XC_INST_REL_LE:
6568 case XC_INST_REL_GE:
6569 case XC_INST_REL_NE:
6570 case XC_INST_REL_LT:
6571 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006572 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006573 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006575 case XC_INST_READ:
6576 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006577 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006578 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006579 case XC_INST_VAR:
6580 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006581 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006582 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006583 case XC_INST_ARRAY_ELEM:
6584 case XC_INST_ARRAY:
6585 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006586 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006587 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006588#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006589 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006590 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006591 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006592 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006594#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006595 case XC_INST_IBASE:
6596 case XC_INST_OBASE:
6597 case XC_INST_SCALE:
6598 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006599 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006600 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006601 case XC_INST_SCALE_FUNC:
6602 case XC_INST_LENGTH:
6603 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006604 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006605 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006606 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006607 case XC_INST_NUM:
6608 dbg_exec("XC_INST_NUM:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006609 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006610 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006611 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006612 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006613 case XC_INST_POP:
6614 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006615 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006616 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006617 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006618 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006619 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006620 case XC_INST_PRINT:
6621 case XC_INST_PRINT_POP:
6622 case XC_INST_PRINT_STR:
6623 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006624 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006626 case XC_INST_STR:
6627 dbg_exec("XC_INST_STR:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006628 r.t = XC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006629 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006630 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006631 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006632 case XC_INST_POWER:
6633 case XC_INST_MULTIPLY:
6634 case XC_INST_DIVIDE:
6635 case XC_INST_MODULUS:
6636 case XC_INST_PLUS:
6637 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006638 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006639 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006640 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006641 case XC_INST_BOOL_NOT: {
6642 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006643 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006644 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006645 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006646 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006647 if (bc_num_cmp(num, &G.prog.zero) == 0)
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006648 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006649 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006650 bc_program_retire(&r, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006651 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006652 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006653 case XC_INST_NEG:
6654 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006655 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006656 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006657#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006658 case BC_INST_ASSIGN_POWER:
6659 case BC_INST_ASSIGN_MULTIPLY:
6660 case BC_INST_ASSIGN_DIVIDE:
6661 case BC_INST_ASSIGN_MODULUS:
6662 case BC_INST_ASSIGN_PLUS:
6663 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006664#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006665 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006666 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006667 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006668 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006669#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006670 case DC_INST_POP_EXEC:
6671 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006672 bc_vec_pop(&G.prog.exestack);
6673 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006674 case DC_INST_MODEXP:
6675 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006676 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006677 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006678 case DC_INST_DIVMOD:
6679 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006680 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006681 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006682 case DC_INST_EXECUTE:
6683 case DC_INST_EXEC_COND:
6684 dbg_exec("DC_INST_EXEC[_COND]:");
6685 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006686 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006687 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006688 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006689 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006690 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006691 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006692 if (s) break;
6693 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006694 break;
6695 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006696 case DC_INST_CLEAR_STACK:
6697 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006698 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006699 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006700 case DC_INST_STACK_LEN:
6701 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006702 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006703 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006704 case DC_INST_DUPLICATE:
6705 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006706 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006707 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006708 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006709 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006710 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006711 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006712 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006713 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006714 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006715 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006716 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006717 ptr = bc_vec_item_rev(&G.prog.results, 0);
6718 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006719 memcpy(&r, ptr, sizeof(BcResult));
6720 memcpy(ptr, ptr2, sizeof(BcResult));
6721 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006722 break;
6723 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006724 case DC_INST_ASCIIFY:
6725 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006726 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006727 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006728 case DC_INST_PRINT_STREAM:
6729 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006730 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006732 case DC_INST_LOAD:
6733 case DC_INST_PUSH_VAR: {
6734 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006735 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006736 break;
6737 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006738 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006739 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006740 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006741 free(name);
6742 break;
6743 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006744 case DC_INST_QUIT:
6745 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006746 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006747 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006748 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006749 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006750 case DC_INST_NQUIT:
6751 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006752 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006753 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006754#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006755 read_updated_ip:
6756 // Instruction stack has changed, read new pointers
6757 ip = bc_vec_top(&G.prog.exestack);
6758 func = bc_program_func(ip->func);
6759 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006760 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006761 }
6762
Denys Vlasenkod38af482018-12-04 19:11:02 +01006763 if (s || G_interrupt) {
6764 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006765 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006766 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006767
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006768 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 }
6770
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006771 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006772}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006773#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006774
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006775static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006776{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006777 char *lenv;
6778 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006779
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006780 lenv = getenv(var);
6781 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006782 if (!lenv) return len;
6783
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006784 len = bb_strtou(lenv, NULL, 10) - 1;
6785 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006786 len = BC_NUM_PRINT_WIDTH;
6787
6788 return len;
6789}
6790
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006791static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006792{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006793 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006794
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006795 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006796 s = zbc_parse_text_init(text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006797 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006798
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006799 IF_BC(check_eof:)
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006800 while (G.prs.l.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006801 BcInstPtr *ip;
6802 BcFunc *f;
6803
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006804 dbg_lex("%s:%d G.prs.l.lex:%d, parsing...", __func__, __LINE__, G.prs.l.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006805 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006806#if ENABLE_BC
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006807 if (G.prs.l.lex == BC_LEX_SCOLON
6808 || G.prs.l.lex == XC_LEX_NLINE
6809 ) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006810 s = zbc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006811 if (s) goto err;
6812 goto check_eof;
6813 }
6814
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006815 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006816 if (s) goto err;
6817
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006818 // Check that next token is a correct stmt delimiter -
6819 // disallows "print 1 print 2" and such.
6820 if (G.prs.l.lex != BC_LEX_SCOLON
6821 && G.prs.l.lex != XC_LEX_NLINE
6822 && G.prs.l.lex != XC_LEX_EOF
6823 ) {
6824 const char *err_at;
6825//TODO: commonalize for other parse errors:
6826 err_at = G.prs.l.lex_next_at ? G.prs.l.lex_next_at : "UNKNOWN";
6827 bc_error_fmt("bad statement terminator at '%.*s'",
6828 (int)(strchrnul(err_at, '\n') - err_at),
6829 err_at
6830 );
6831 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006832 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006833 // The above logic is fragile. Check these examples:
6834 // - interative read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006835#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006836 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006837#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006838 // Most of dc parsing assumes all whitespace,
6839 // including '\n', is eaten.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006840 while (G.prs.l.lex == XC_LEX_NLINE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006841 s = zbc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006842 if (s) goto err;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006843 if (G.prs.l.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006844 goto done;
6845 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006846 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006847#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006848 }
6849 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006850 err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006851 bc_parse_reset(); // includes bc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006852 RETURN_STATUS(BC_STATUS_FAILURE);
6853 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006854
Denys Vlasenko39287e02018-12-22 02:23:08 +01006855 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006856 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006857 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006858 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006859 break;
6860 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006861
6862 ip = (void*)G.prog.exestack.v;
6863#if SANITY_CHECKS
6864 if (G.prog.exestack.len != 1) // should have only main's IP
6865 bb_error_msg_and_die("BUG:call stack");
6866 if (ip->func != BC_PROG_MAIN)
6867 bb_error_msg_and_die("BUG:not MAIN");
6868#endif
6869 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006870 // bc discards strings, constants and code after each
6871 // top-level statement in the "main program".
6872 // This prevents "yes 1 | bc" from growing its memory
6873 // without bound. This can be done because data stack
6874 // is empty and thus can't hold any references to
6875 // strings or constants, there is no generated code
6876 // which can hold references (after we discard one
6877 // we just executed). Code of functions can have references,
6878 // but bc stores function strings/constants in per-function
6879 // storage.
6880 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006881#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006882 if (G.prog.results.len != 0) // should be empty
6883 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006884#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006885 IF_BC(bc_vec_pop_all(&f->strs);)
6886 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006887 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006888 if (G.prog.results.len == 0
6889 && G.prog.vars.len == 0
6890 ) {
6891 // If stack is empty and no registers exist (TODO: or they are all empty),
6892 // we can get rid of accumulated strings and constants.
6893 // In this example dc process should not grow
6894 // its memory consumption with time:
6895 // yes 1pc | dc
6896 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6897 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6898 }
6899 // The code is discarded always (below), thus this example
6900 // should also not grow its memory consumption with time,
6901 // even though its data stack is not empty:
6902 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006903 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006904 // We drop generated and executed code for both bc and dc:
6905 bc_vec_pop_all(&f->code);
6906 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006907 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006908 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006909 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006910 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006911}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006912#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006913
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006914static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006915{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006916 // So far bc/dc have no way to include a file from another file,
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006917 // therefore we know G.prs.filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006918 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006919 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006920
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006921 G.prs.filename = filename;
6922 G.prs.input_fp = fp;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006923 bc_lex_file();
Gavin Howard01055ba2018-11-03 11:00:21 -06006924
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006925 do {
6926 s = zbc_vm_process("");
6927 // We do not stop looping on errors here if reading stdin.
6928 // Example: start interactive bc and enter "return".
6929 // It should say "'return' not in a function"
6930 // but should not exit.
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006931 } while (G.prs.input_fp == stdin);
6932 G.prs.filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006933 RETURN_STATUS(s);
6934}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006935#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006936
6937static BC_STATUS zbc_vm_file(const char *file)
6938{
6939 BcStatus s;
6940 FILE *fp;
6941
6942 fp = xfopen_for_read(file);
6943 s = zbc_vm_execute_FILE(fp, file);
6944 fclose(fp);
6945
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006946 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006947}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006948#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006949
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006950#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006951static void bc_vm_info(void)
6952{
6953 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006954 "Adapted from https://github.com/gavinhoward/bc\n"
6955 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006956 , applet_name);
6957}
6958
6959static void bc_args(char **argv)
6960{
6961 unsigned opts;
6962 int i;
6963
6964 GETOPT_RESET();
6965#if ENABLE_FEATURE_BC_LONG_OPTIONS
6966 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6967 "warn\0" No_argument "w"
6968 "version\0" No_argument "v"
6969 "standard\0" No_argument "s"
6970 "quiet\0" No_argument "q"
6971 "mathlib\0" No_argument "l"
6972 "interactive\0" No_argument "i"
6973 );
6974#else
6975 opts = option_mask32 |= getopt32(argv, "wvsqli");
6976#endif
6977 if (getenv("POSIXLY_CORRECT"))
6978 option_mask32 |= BC_FLAG_S;
6979
6980 if (opts & BC_FLAG_V) {
6981 bc_vm_info();
6982 exit(0);
6983 }
6984
6985 for (i = optind; argv[i]; ++i)
6986 bc_vec_push(&G.files, argv + i);
6987}
6988
6989static void bc_vm_envArgs(void)
6990{
6991 BcVec v;
6992 char *buf;
6993 char *env_args = getenv("BC_ENV_ARGS");
6994
6995 if (!env_args) return;
6996
6997 G.env_args = xstrdup(env_args);
6998 buf = G.env_args;
6999
7000 bc_vec_init(&v, sizeof(char *), NULL);
7001
7002 while (*(buf = skip_whitespace(buf)) != '\0') {
7003 bc_vec_push(&v, &buf);
7004 buf = skip_non_whitespace(buf);
7005 if (!*buf)
7006 break;
7007 *buf++ = '\0';
7008 }
7009
7010 // NULL terminate, and pass argv[] so that first arg is argv[1]
7011 if (sizeof(int) == sizeof(char*)) {
7012 bc_vec_push(&v, &const_int_0);
7013 } else {
7014 static char *const nullptr = NULL;
7015 bc_vec_push(&v, &nullptr);
7016 }
7017 bc_args(((char **)v.v) - 1);
7018
7019 bc_vec_free(&v);
7020}
7021
7022static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007023 "scale=20"
7024"\n" "define e(x){"
7025"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007026////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7027//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7028//e(-.998896): GNU:.36828580434569428695
7029// above code:.36828580434569428696
7030// actual value:.3682858043456942869594...
7031// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007032"\n" "b=ibase"
7033"\n" "ibase=A"
7034"\n" "if(x<0){"
7035"\n" "n=1"
7036"\n" "x=-x"
7037"\n" "}"
7038"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007039"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007040"\n" "scale=scale(x)+1"
7041"\n" "while(x>1){"
7042"\n" "d+=1"
7043"\n" "x/=2"
7044"\n" "scale+=1"
7045"\n" "}"
7046"\n" "scale=r"
7047"\n" "r=x+1"
7048"\n" "p=x"
7049"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007050"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007051"\n" "p*=x"
7052"\n" "f*=i"
7053"\n" "v=p/f"
7054"\n" "r+=v"
7055"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007056"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007057"\n" "scale=s"
7058"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007059"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007060"\n" "return(r/1)"
7061"\n" "}"
7062"\n" "define l(x){"
7063"\n" "auto b,s,r,p,a,q,i,v"
7064"\n" "b=ibase"
7065"\n" "ibase=A"
7066"\n" "if(x<=0){"
7067"\n" "r=(1-10^scale)/1"
7068"\n" "ibase=b"
7069"\n" "return(r)"
7070"\n" "}"
7071"\n" "s=scale"
7072"\n" "scale+=6"
7073"\n" "p=2"
7074"\n" "while(x>=2){"
7075"\n" "p*=2"
7076"\n" "x=sqrt(x)"
7077"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007078"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007079"\n" "p*=2"
7080"\n" "x=sqrt(x)"
7081"\n" "}"
7082"\n" "r=a=(x-1)/(x+1)"
7083"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007084"\n" "v=1"
7085"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007086"\n" "a*=q"
7087"\n" "v=a/i"
7088"\n" "r+=v"
7089"\n" "}"
7090"\n" "r*=p"
7091"\n" "scale=s"
7092"\n" "ibase=b"
7093"\n" "return(r/1)"
7094"\n" "}"
7095"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007096"\n" "auto b,s,r,a,q,i"
7097"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007098"\n" "b=ibase"
7099"\n" "ibase=A"
7100"\n" "s=scale"
7101"\n" "scale=1.1*s+2"
7102"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007103"\n" "scale=0"
7104"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007105"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007106"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007107"\n" "scale=s+2"
7108"\n" "r=a=x"
7109"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007110"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007111"\n" "a*=q/(i*(i-1))"
7112"\n" "r+=a"
7113"\n" "}"
7114"\n" "scale=s"
7115"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007116"\n" "return(r/1)"
7117"\n" "}"
7118"\n" "define c(x){"
7119"\n" "auto b,s"
7120"\n" "b=ibase"
7121"\n" "ibase=A"
7122"\n" "s=scale"
7123"\n" "scale*=1.2"
7124"\n" "x=s(2*a(1)+x)"
7125"\n" "scale=s"
7126"\n" "ibase=b"
7127"\n" "return(x/1)"
7128"\n" "}"
7129"\n" "define a(x){"
7130"\n" "auto b,s,r,n,a,m,t,f,i,u"
7131"\n" "b=ibase"
7132"\n" "ibase=A"
7133"\n" "n=1"
7134"\n" "if(x<0){"
7135"\n" "n=-1"
7136"\n" "x=-x"
7137"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007138"\n" "if(scale<65){"
7139"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7140"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007141"\n" "}"
7142"\n" "s=scale"
7143"\n" "if(x>.2){"
7144"\n" "scale+=5"
7145"\n" "a=a(.2)"
7146"\n" "}"
7147"\n" "scale=s+3"
7148"\n" "while(x>.2){"
7149"\n" "m+=1"
7150"\n" "x=(x-.2)/(1+.2*x)"
7151"\n" "}"
7152"\n" "r=u=x"
7153"\n" "f=-x*x"
7154"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007155"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007156"\n" "u*=f"
7157"\n" "t=u/i"
7158"\n" "r+=t"
7159"\n" "}"
7160"\n" "scale=s"
7161"\n" "ibase=b"
7162"\n" "return((m*a+r)/n)"
7163"\n" "}"
7164"\n" "define j(n,x){"
7165"\n" "auto b,s,o,a,i,v,f"
7166"\n" "b=ibase"
7167"\n" "ibase=A"
7168"\n" "s=scale"
7169"\n" "scale=0"
7170"\n" "n/=1"
7171"\n" "if(n<0){"
7172"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007173"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007174"\n" "}"
7175"\n" "a=1"
7176"\n" "for(i=2;i<=n;++i)a*=i"
7177"\n" "scale=1.5*s"
7178"\n" "a=(x^n)/2^n/a"
7179"\n" "r=v=1"
7180"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007181"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007182"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007183"\n" "v=v*f/i/(n+i)"
7184"\n" "r+=v"
7185"\n" "}"
7186"\n" "scale=s"
7187"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007188"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007189"\n" "return(a*r/1)"
7190"\n" "}"
7191};
7192#endif // ENABLE_BC
7193
Denys Vlasenko19f11072018-12-12 22:48:19 +01007194static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007195{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007196 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007197 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007198 size_t i;
7199
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007200#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007201 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007202 // We know that internal library is not buggy,
7203 // thus error checking is normally disabled.
7204# define DEBUG_LIB 0
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007205 bc_lex_file();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007206 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007207 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007208 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007209#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007210
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007211 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007212 fname = (void*)G.files.v;
7213 for (i = 0; i < G.files.len; i++) {
7214 s = zbc_vm_file(*fname++);
7215 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7216 // Debug config, non-interactive mode:
7217 // return all the way back to main.
7218 // Non-debug builds do not come here
7219 // in non-interactive mode, they exit.
7220 RETURN_STATUS(s);
7221 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007222 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007223
Denys Vlasenko91cde952018-12-10 20:56:08 +01007224 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007225 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007226
Denys Vlasenko19f11072018-12-12 22:48:19 +01007227 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007228}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007229#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007230
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007231#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007232static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007233{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007234 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007235 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007236 bc_vec_free(&G.prog.vars);
7237 bc_vec_free(&G.prog.var_map);
7238 bc_vec_free(&G.prog.arrs);
7239 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007240 IF_DC(bc_vec_free(&G.prog.strs);)
7241 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007242 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007243 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007244 IF_BC(bc_num_free(&G.prog.last);)
7245 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007246 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007247 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007248}
7249
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007250static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007251{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007252 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007253 bc_program_free();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007254 bc_parse_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007255 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007256}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007257#endif
7258
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007259static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007260{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007261 BcInstPtr ip;
7262
Denys Vlasenko82269122018-12-14 16:30:56 +01007263 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007264 memset(&ip, 0, sizeof(BcInstPtr));
7265
Denys Vlasenko82269122018-12-14 16:30:56 +01007266 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007267 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007268 G.prog.ob_t = 10;
7269
Denys Vlasenko503faf92018-12-20 16:24:18 +01007270 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7271 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007272
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007273 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007274 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007275
Denys Vlasenko503faf92018-12-20 16:24:18 +01007276 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7277 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007278
7279 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007280 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007281
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007282 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007283 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007284 // a valid function name (and be short)
7285 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007286 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007287 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007288 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007289 bc_program_add_fn();
7290 bc_program_add_fn();
7291 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007292
7293 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007294 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007295
7296 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007297 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007298
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007299 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7300 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007301 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007302 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7303 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007304
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007305 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007306}
Gavin Howard01055ba2018-11-03 11:00:21 -06007307
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007308static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007309{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007310#if ENABLE_FEATURE_EDITING
7311 G.line_input_state = new_line_input_t(DO_HISTORY);
7312#endif
7313 G.prog.len = bc_vm_envLen(env_len);
7314
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007315 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007316 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007317 bc_program_init();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007318 bc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007319
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007320//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7321//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007322 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007323#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007324 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007325 // With SA_RESTART, most system calls will restart
7326 // (IOW: they won't fail with EINTR).
7327 // In particular, this means ^C won't cause
7328 // stdout to get into "error state" if SIGINT hits
7329 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007330 //
7331 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007332 // will only be handled after [Enter] since read()
7333 // from stdin is not interrupted by ^C either,
7334 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007335 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007336 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7337
7338 // Without SA_RESTART, this exhibits a bug:
7339 // "while (1) print 1" and try ^C-ing it.
7340 // Intermittently, instead of returning to input line,
7341 // you'll get "output error: Interrupted system call"
7342 // and exit.
7343 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007344#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007345 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007346 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007347 return 0; // "not a tty"
7348}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007349
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007350static BcStatus bc_vm_run(void)
7351{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007352 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007353#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007354 if (G_exiting) // it was actually "halt" or "quit"
7355 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007356 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007357# if ENABLE_FEATURE_EDITING
7358 free_line_input_t(G.line_input_state);
7359# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007360 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007361#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007362 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007363 return st;
7364}
7365
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007366#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007367int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007368int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007369{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007370 int is_tty;
7371
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007372 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007373
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007374 is_tty = bc_vm_init("BC_LINE_LENGTH");
7375
7376 bc_args(argv);
7377
7378 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7379 bc_vm_info();
7380
7381 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007382}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007383#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007384
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007385#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007386int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007387int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007388{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007389 int noscript;
7390
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007391 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007392
7393 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7394 // 1 char wider than bc from the same package.
7395 // Both default width, and xC_LINE_LENGTH=N are wider:
7396 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007397 // |1234\ |
7398 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007399 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007400 // |123\ |
7401 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007402 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007403 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007404
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007405 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7406 noscript = BC_FLAG_I;
7407 for (;;) {
7408 int n = getopt(argc, argv, "e:f:x");
7409 if (n <= 0)
7410 break;
7411 switch (n) {
7412 case 'e':
7413 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007414 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007415 if (n) return n;
7416 break;
7417 case 'f':
7418 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007419 n = zbc_vm_file(optarg);
7420 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007421 break;
7422 case 'x':
7423 option_mask32 |= DC_FLAG_X;
7424 break;
7425 default:
7426 bb_show_usage();
7427 }
7428 }
7429 argv += optind;
7430
7431 while (*argv) {
7432 noscript = 0;
7433 bc_vec_push(&G.files, argv++);
7434 }
7435
7436 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7437
7438 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007439}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007440#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007441
7442#endif // not DC_SMALL