blob: 2237a41b5899354520355e82ce37a77d557c4945 [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 Vlasenko0b0e8d02018-12-25 21:44:10 +0100521#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100522 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100523static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100524 LEX_KW_ENTRY("auto" , 1), // 0
525 LEX_KW_ENTRY("break" , 1), // 1
526 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
527 LEX_KW_ENTRY("define" , 1), // 3
528 LEX_KW_ENTRY("else" , 0), // 4
529 LEX_KW_ENTRY("for" , 1), // 5
530 LEX_KW_ENTRY("halt" , 0), // 6
531 LEX_KW_ENTRY("ibase" , 1), // 7
532 LEX_KW_ENTRY("obase" , 1), // 8
533 LEX_KW_ENTRY("if" , 1), // 9
534 LEX_KW_ENTRY("last" , 0), // 10
535 LEX_KW_ENTRY("length" , 1), // 11
536 LEX_KW_ENTRY("limits" , 0), // 12
537 LEX_KW_ENTRY("print" , 0), // 13
538 LEX_KW_ENTRY("quit" , 1), // 14
539 LEX_KW_ENTRY("read" , 0), // 15
540 LEX_KW_ENTRY("return" , 1), // 16
541 LEX_KW_ENTRY("scale" , 1), // 17
542 LEX_KW_ENTRY("sqrt" , 1), // 18
543 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100544};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100545#undef 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 Vlasenko0b0e8d02018-12-25 21:44:10 +0100573#define keyword_is_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
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100713typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100714 smallint lex; // was BcLexType // first member is most used
715 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100716 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100717 const char *lex_inbuf;
718 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100719 const char *lex_filename;
720 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100721 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100722
Gavin Howard01055ba2018-11-03 11:00:21 -0600723 BcFunc *func;
724 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100725 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100726 IF_BC(BcVec exits;)
727 IF_BC(BcVec conds;)
728 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600729} BcParse;
730
Gavin Howard01055ba2018-11-03 11:00:21 -0600731typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600732 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100733 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600734
Denys Vlasenko503faf92018-12-20 16:24:18 +0100735 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600736 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600737 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600738
Gavin Howard01055ba2018-11-03 11:00:21 -0600739 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100740 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600741
742 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100743 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600744
745 BcVec vars;
746 BcVec var_map;
747
748 BcVec arrs;
749 BcVec arr_map;
750
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100751 IF_DC(BcVec strs;)
752 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600753
Gavin Howard01055ba2018-11-03 11:00:21 -0600754 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100755 IF_BC(BcNum one;)
756 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600757} BcProgram;
758
Denys Vlasenko6e618232018-12-25 22:20:14 +0100759struct globals {
760 BcParse prs; // first member is most used
761
762 // For error messages. Can be set to current parsed line,
763 // or [TODO] to current executing line (can be before last parsed one)
764 unsigned err_line;
765
766 BcVec input_buffer;
767
768 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
769 IF_FEATURE_CLEAN_UP(smallint exiting;)
770
771 BcProgram prog;
772
773 BcVec files;
774
775 char *env_args;
776
777#if ENABLE_FEATURE_EDITING
778 line_input_t *line_input_state;
779#endif
780} FIX_ALIASING;
781#define G (*ptr_to_globals)
782#define INIT_G() do { \
783 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
784} while (0)
785#define FREE_G() do { \
786 FREE_PTR_TO_GLOBALS(); \
787} while (0)
788#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
789#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
790#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
791#if ENABLE_FEATURE_BC_SIGNALS
792# define G_interrupt bb_got_signal
793# define G_ttyin G.ttyin
794#else
795# define G_interrupt 0
796# define G_ttyin 0
797#endif
798#if ENABLE_FEATURE_CLEAN_UP
799# define G_exiting G.exiting
800#else
801# define G_exiting 0
802#endif
803#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
804#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
805
806#define BC_PARSE_STREND (0xff)
807
808#if ENABLE_BC
809# define BC_PARSE_REL (1 << 0)
810# define BC_PARSE_PRINT (1 << 1)
811# define BC_PARSE_ARRAY (1 << 2)
812# define BC_PARSE_NOCALL (1 << 3)
813#endif
814
Gavin Howard01055ba2018-11-03 11:00:21 -0600815#define BC_PROG_MAIN (0)
816#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100817#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600818#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100819#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600820
821#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
822#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100823 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600824
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100825#define BC_FLAG_W (1 << 0)
826#define BC_FLAG_V (1 << 1)
827#define BC_FLAG_S (1 << 2)
828#define BC_FLAG_Q (1 << 3)
829#define BC_FLAG_L (1 << 4)
830#define BC_FLAG_I (1 << 5)
831#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600832
833#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
834#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
835
Denys Vlasenko64074a12018-12-07 15:50:14 +0100836#define BC_MAX_OBASE ((unsigned) 999)
837#define BC_MAX_DIM ((unsigned) INT_MAX)
838#define BC_MAX_SCALE ((unsigned) UINT_MAX)
839#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
840#define BC_MAX_NUM BC_MAX_STRING
841// Unused apart from "limits" message. Just show a "biggish number" there.
842//#define BC_MAX_NAME BC_MAX_STRING
843//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
844//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
845#define BC_MAX_NAME_STR "999999999"
846#define BC_MAX_EXP_STR "999999999"
847#define BC_MAX_VARS_STR "999999999"
848
849#define BC_MAX_OBASE_STR "999"
850
851#if INT_MAX == 2147483647
852# define BC_MAX_DIM_STR "2147483647"
853#elif INT_MAX == 9223372036854775807
854# define BC_MAX_DIM_STR "9223372036854775807"
855#else
856# error Strange INT_MAX
857#endif
858
859#if UINT_MAX == 4294967295
860# define BC_MAX_SCALE_STR "4294967295"
861# define BC_MAX_STRING_STR "4294967294"
862#elif UINT_MAX == 18446744073709551615
863# define BC_MAX_SCALE_STR "18446744073709551615"
864# define BC_MAX_STRING_STR "18446744073709551614"
865#else
866# error Strange UINT_MAX
867#endif
868#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600869
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100870// In configurations where errors abort instead of propagating error
871// return code up the call chain, functions returning BC_STATUS
872// actually don't return anything, they always succeed and return "void".
873// A macro wrapper is provided, which makes this statement work:
874// s = zbc_func(...)
875// and makes it visible to the compiler that s is always zero,
876// allowing compiler to optimize dead code after the statement.
877//
878// To make code more readable, each such function has a "z"
879// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
880//
881#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100882# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100883# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100884# define IF_ERROR_RETURN_POSSIBLE(a) a
885# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100886# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100887# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100888#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100889# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100890# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100891# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
892# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100893# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100894# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100895#endif
896
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100897//
898// Utility routines
899//
Gavin Howard01055ba2018-11-03 11:00:21 -0600900
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100901static void fflush_and_check(void)
902{
903 fflush_all();
904 if (ferror(stdout) || ferror(stderr))
905 bb_perror_msg_and_die("output error");
906}
907
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100908#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100909#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100910do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100911 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100912 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100913 return BC_STATUS_FAILURE; \
914} while (0)
915#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100916#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100917#endif
918
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100919static void quit(void) NORETURN;
920static void quit(void)
921{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100922 if (ferror(stdin))
923 bb_perror_msg_and_die("input error");
924 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100925 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100926 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100927}
928
Denys Vlasenko5318f812018-12-05 17:48:01 +0100929static void bc_verror_msg(const char *fmt, va_list p)
930{
Denys Vlasenko82269122018-12-14 16:30:56 +0100931 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100932 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100933 sv = applet_name;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100934 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100935 }
936 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100937 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100938 free((char*)applet_name);
939 applet_name = sv;
940 }
941}
942
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100943static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100944{
945 va_list p;
946
947 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100948 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100949 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100950
Denys Vlasenkoec603182018-12-17 10:34:02 +0100951 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
952 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
953 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100954}
955
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100956#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100957static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100958{
959 va_list p;
960
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100961 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100962 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100963 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100964
965 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100966 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100967 va_end(p);
968
969 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100970 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100971 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
972
Denys Vlasenkoec603182018-12-17 10:34:02 +0100973 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100974 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100975 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100976}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100977#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100978#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100979
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100980// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
981// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100982// function must not have caller-cleaned parameters on stack.
983// Unfortunately, vararg function API does exactly that on most arches.
984// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100985static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100986{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100987 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100988}
989static ERRORFUNC int bc_error_bad_character(char c)
990{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100991 if (!c)
992 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100993 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100994}
995static ERRORFUNC int bc_error_bad_expression(void)
996{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100997 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100998}
999static ERRORFUNC int bc_error_bad_token(void)
1000{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001001 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001002}
1003static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1004{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001005 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1008{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001011#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001012static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001013{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001014 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001015}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001016#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1017static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001018{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001019 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001020}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001021#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1022static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001023{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001024 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 +01001025}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001026#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)
1027static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001028{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001029 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001030}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001031#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 +01001032#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001033
Gavin Howard01055ba2018-11-03 11:00:21 -06001034static void bc_vec_grow(BcVec *v, size_t n)
1035{
1036 size_t cap = v->cap * 2;
1037 while (cap < v->len + n) cap *= 2;
1038 v->v = xrealloc(v->v, v->size * cap);
1039 v->cap = cap;
1040}
1041
1042static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1043{
1044 v->size = esize;
1045 v->cap = BC_VEC_START_CAP;
1046 v->len = 0;
1047 v->dtor = dtor;
1048 v->v = xmalloc(esize * BC_VEC_START_CAP);
1049}
1050
Denys Vlasenko7d628012018-12-04 21:46:47 +01001051static void bc_char_vec_init(BcVec *v)
1052{
1053 bc_vec_init(v, sizeof(char), NULL);
1054}
1055
Gavin Howard01055ba2018-11-03 11:00:21 -06001056static void bc_vec_expand(BcVec *v, size_t req)
1057{
1058 if (v->cap < req) {
1059 v->v = xrealloc(v->v, v->size * req);
1060 v->cap = req;
1061 }
1062}
1063
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001064static void bc_vec_pop(BcVec *v)
1065{
1066 v->len--;
1067 if (v->dtor)
1068 v->dtor(v->v + (v->size * v->len));
1069}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001070
Gavin Howard01055ba2018-11-03 11:00:21 -06001071static void bc_vec_npop(BcVec *v, size_t n)
1072{
1073 if (!v->dtor)
1074 v->len -= n;
1075 else {
1076 size_t len = v->len - n;
1077 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1078 }
1079}
1080
Denys Vlasenko7d628012018-12-04 21:46:47 +01001081static void bc_vec_pop_all(BcVec *v)
1082{
1083 bc_vec_npop(v, v->len);
1084}
1085
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001086static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001087{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001088 size_t len = v->len;
1089 if (len >= v->cap) bc_vec_grow(v, 1);
1090 memmove(v->v + (v->size * len), data, v->size);
1091 v->len++;
1092 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001093}
1094
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001095// G.prog.results often needs "pop old operand, push result" idiom.
1096// Can do this without a few extra ops
1097static size_t bc_result_pop_and_push(const void *data)
1098{
1099 BcVec *v = &G.prog.results;
1100 char *last;
1101 size_t len = v->len - 1;
1102
1103 last = v->v + (v->size * len);
1104 if (v->dtor)
1105 v->dtor(last);
1106 memmove(last, data, v->size);
1107 return len;
1108}
1109
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001110static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001111{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001112 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001113}
1114
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001115static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001116{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001117 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001118 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001119 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001120}
1121
Gavin Howard01055ba2018-11-03 11:00:21 -06001122static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1123{
1124 if (idx == v->len)
1125 bc_vec_push(v, data);
1126 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001127 char *ptr;
1128
1129 if (v->len == v->cap) bc_vec_grow(v, 1);
1130
1131 ptr = v->v + v->size * idx;
1132
1133 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1134 memmove(ptr, data, v->size);
1135 }
1136}
1137
1138static void bc_vec_string(BcVec *v, size_t len, const char *str)
1139{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001140 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001141 bc_vec_expand(v, len + 1);
1142 memcpy(v->v, str, len);
1143 v->len = len;
1144
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001145 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001146}
1147
Gavin Howard01055ba2018-11-03 11:00:21 -06001148static void *bc_vec_item(const BcVec *v, size_t idx)
1149{
1150 return v->v + v->size * idx;
1151}
1152
1153static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1154{
1155 return v->v + v->size * (v->len - idx - 1);
1156}
1157
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001158static void *bc_vec_top(const BcVec *v)
1159{
1160 return v->v + v->size * (v->len - 1);
1161}
1162
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001163static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001164{
1165 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001166 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001167 free(v->v);
1168}
1169
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001170static BcFunc* bc_program_func(size_t idx)
1171{
1172 return bc_vec_item(&G.prog.fns, idx);
1173}
1174// BC_PROG_MAIN is zeroth element, so:
1175#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1176
1177#if ENABLE_BC
1178static BcFunc* bc_program_current_func(void)
1179{
1180 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1181 BcFunc *func = bc_program_func(ip->func);
1182 return func;
1183}
1184#endif
1185
1186static char** bc_program_str(size_t idx)
1187{
1188#if ENABLE_BC
1189 if (IS_BC) {
1190 BcFunc *func = bc_program_current_func();
1191 return bc_vec_item(&func->strs, idx);
1192 }
1193#endif
1194 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1195}
1196
1197static char** bc_program_const(size_t idx)
1198{
1199#if ENABLE_BC
1200 if (IS_BC) {
1201 BcFunc *func = bc_program_current_func();
1202 return bc_vec_item(&func->consts, idx);
1203 }
1204#endif
1205 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1206}
1207
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001208static int bc_id_cmp(const void *e1, const void *e2)
1209{
1210 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1211}
1212
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001213static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001214{
1215 free(((BcId *) id)->name);
1216}
1217
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001218static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001219{
1220 size_t low = 0, high = v->len;
1221
1222 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001223 size_t mid = (low + high) / 2;
1224 BcId *id = bc_vec_item(v, mid);
1225 int result = bc_id_cmp(ptr, id);
1226
1227 if (result == 0)
1228 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001229 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001230 high = mid;
1231 else
1232 low = mid + 1;
1233 }
1234
1235 return low;
1236}
1237
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001238static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001239{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001240 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001241
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001242 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001243 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001244 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1245 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001246 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001247 bc_vec_pushAt(v, ptr, n);
1248 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001249}
1250
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001251#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001252static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001253{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001254 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001255 if (i >= v->len) return BC_VEC_INVALID_IDX;
1256 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1257}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001258#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001259
Gavin Howard01055ba2018-11-03 11:00:21 -06001260static void bc_num_setToZero(BcNum *n, size_t scale)
1261{
1262 n->len = 0;
1263 n->neg = false;
1264 n->rdx = scale;
1265}
1266
1267static void bc_num_zero(BcNum *n)
1268{
1269 bc_num_setToZero(n, 0);
1270}
1271
1272static void bc_num_one(BcNum *n)
1273{
1274 bc_num_setToZero(n, 0);
1275 n->len = 1;
1276 n->num[0] = 1;
1277}
1278
Denys Vlasenko3129f702018-12-09 12:04:44 +01001279// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001280static void bc_num_init(BcNum *n, size_t req)
1281{
1282 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001283 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001284 n->num = xmalloc(req);
1285 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001286 n->rdx = 0;
1287 n->len = 0;
1288 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001289}
1290
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001291static void bc_num_init_DEF_SIZE(BcNum *n)
1292{
1293 bc_num_init(n, BC_NUM_DEF_SIZE);
1294}
1295
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001296static void bc_num_expand(BcNum *n, size_t req)
1297{
1298 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1299 if (req > n->cap) {
1300 n->num = xrealloc(n->num, req);
1301 n->cap = req;
1302 }
1303}
1304
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001305static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001306{
1307 free(((BcNum *) num)->num);
1308}
1309
1310static void bc_num_copy(BcNum *d, BcNum *s)
1311{
1312 if (d != s) {
1313 bc_num_expand(d, s->cap);
1314 d->len = s->len;
1315 d->neg = s->neg;
1316 d->rdx = s->rdx;
1317 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1318 }
1319}
1320
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001321static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001322{
1323 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001324 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001325
Denys Vlasenko1557b762018-12-22 21:37:46 +01001326 result = 0;
1327 i = n->len;
1328 while (i > n->rdx) {
1329 unsigned long prev = result;
1330 result = result * 10 + n->num[--i];
1331 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1332 // 0x1ff00000 * 10 is 0x13f600000,
1333 // or 0x3f600000 truncated to 32 bits. Which is larger.
1334 // However, (N*10)/8 < N check is always correct.
1335 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001336 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001337 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001338 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001339
Denys Vlasenko29301232018-12-11 15:29:32 +01001340 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001341}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001342#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1343
1344static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1345{
1346 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1347
1348 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1349}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001350#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001351
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001352#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1353# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1354#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1355# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1356#endif
1357// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1358// would not hit realloc() code path - not good if num[] is not malloced
1359
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001360static void bc_num_ulong2num(BcNum *n, unsigned long val)
1361{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001363
1364 bc_num_zero(n);
1365
1366 if (val == 0) return;
1367
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001368 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001369
1370 ptr = n->num;
1371 for (;;) {
1372 n->len++;
1373 *ptr++ = val % 10;
1374 val /= 10;
1375 if (val == 0) break;
1376 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001377}
1378
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001379static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001380{
1381 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001382 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001383 a[i] -= b[i];
1384 for (j = i; a[j] < 0;) {
1385 a[j++] += 10;
1386 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001387 }
1388 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001389}
1390
1391static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1392{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001393 size_t i = len;
1394 for (;;) {
1395 int c;
1396 if (i == 0)
1397 return 0;
1398 i--;
1399 c = a[i] - b[i];
1400 if (c != 0) {
1401 i++;
1402 if (c < 0)
1403 return -i;
1404 return i;
1405 }
1406 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001407}
1408
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001409#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1410#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1411#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1412//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1413static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1414{
1415 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1416}
1417//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1418static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1419{
1420 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1421}
1422
Gavin Howard01055ba2018-11-03 11:00:21 -06001423static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1424{
1425 size_t i, min, a_int, b_int, diff;
1426 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001427 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001428 ssize_t cmp;
1429
1430 if (a == b) return 0;
1431 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1432 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001433
1434 if (a->neg != b->neg) // signs of a and b differ
1435 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1436 return (int)b->neg - (int)a->neg;
1437 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001438
1439 a_int = BC_NUM_INT(a);
1440 b_int = BC_NUM_INT(b);
1441 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001442
1443 if (a_int != 0) return (ssize_t) a_int;
1444
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001445 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001446 if (a_max) {
1447 min = b->rdx;
1448 diff = a->rdx - b->rdx;
1449 max_num = a->num + diff;
1450 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001451 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1452 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001453 min = a->rdx;
1454 diff = b->rdx - a->rdx;
1455 max_num = b->num + diff;
1456 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001457 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001458 }
1459
1460 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001461 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001462
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001463 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001464 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001465 }
1466
1467 return 0;
1468}
1469
1470static void bc_num_truncate(BcNum *n, size_t places)
1471{
1472 if (places == 0) return;
1473
1474 n->rdx -= places;
1475
1476 if (n->len != 0) {
1477 n->len -= places;
1478 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1479 }
1480}
1481
1482static void bc_num_extend(BcNum *n, size_t places)
1483{
1484 size_t len = n->len + places;
1485
1486 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001487 if (n->cap < len) bc_num_expand(n, len);
1488
1489 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1490 memset(n->num, 0, sizeof(BcDig) * places);
1491
1492 n->len += places;
1493 n->rdx += places;
1494 }
1495}
1496
1497static void bc_num_clean(BcNum *n)
1498{
1499 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1500 if (n->len == 0)
1501 n->neg = false;
1502 else if (n->len < n->rdx)
1503 n->len = n->rdx;
1504}
1505
1506static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1507{
1508 if (n->rdx < scale)
1509 bc_num_extend(n, scale - n->rdx);
1510 else
1511 bc_num_truncate(n, n->rdx - scale);
1512
1513 bc_num_clean(n);
1514 if (n->len != 0) n->neg = !neg1 != !neg2;
1515}
1516
1517static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1518 BcNum *restrict b)
1519{
1520 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001521 b->len = n->len - idx;
1522 a->len = idx;
1523 a->rdx = b->rdx = 0;
1524
1525 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1526 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001527 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001528 bc_num_zero(b);
1529 bc_num_copy(a, n);
1530 }
1531
1532 bc_num_clean(a);
1533 bc_num_clean(b);
1534}
1535
Denys Vlasenko29301232018-12-11 15:29:32 +01001536static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001537{
Denys Vlasenko29301232018-12-11 15:29:32 +01001538 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001539
1540 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1541 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1542 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001543 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001544 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001545
1546 if (n->rdx >= places)
1547 n->rdx -= places;
1548 else {
1549 bc_num_extend(n, places - n->rdx);
1550 n->rdx = 0;
1551 }
1552
1553 bc_num_clean(n);
1554
Denys Vlasenko29301232018-12-11 15:29:32 +01001555 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001556}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001557#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001558
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001559typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1560
1561static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1562 BcNumBinaryOp op, size_t req)
1563{
1564 BcStatus s;
1565 BcNum num2, *ptr_a, *ptr_b;
1566 bool init = false;
1567
1568 if (c == a) {
1569 ptr_a = &num2;
1570 memcpy(ptr_a, c, sizeof(BcNum));
1571 init = true;
1572 } else
1573 ptr_a = a;
1574
1575 if (c == b) {
1576 ptr_b = &num2;
1577 if (c != a) {
1578 memcpy(ptr_b, c, sizeof(BcNum));
1579 init = true;
1580 }
1581 } else
1582 ptr_b = b;
1583
1584 if (init)
1585 bc_num_init(c, req);
1586 else
1587 bc_num_expand(c, req);
1588
1589 s = BC_STATUS_SUCCESS;
1590 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1591
1592 if (init) bc_num_free(&num2);
1593
1594 RETURN_STATUS(s);
1595}
1596#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1597
1598static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1599static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1600static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1601static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1602static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1603static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1604
1605static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1606{
1607 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1608 (void) scale;
1609 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1610}
1611
1612static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1613{
1614 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1615 (void) scale;
1616 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1617}
1618
1619static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1620{
1621 size_t req = BC_NUM_MREQ(a, b, scale);
1622 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1623}
1624
1625static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1626{
1627 size_t req = BC_NUM_MREQ(a, b, scale);
1628 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1629}
1630
1631static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1632{
1633 size_t req = BC_NUM_MREQ(a, b, scale);
1634 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1635}
1636
1637static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1638{
1639 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1640}
1641
1642static const BcNumBinaryOp zbc_program_ops[] = {
1643 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1644};
1645#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1646#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1647#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1648#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1649#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1650#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1651
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001652static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001653{
1654 BcNum one;
1655 BcDig num[2];
1656
1657 one.cap = 2;
1658 one.num = num;
1659 bc_num_one(&one);
1660
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001661 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001662}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001663#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001664
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001665static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001666{
1667 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1668 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001669 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001670
1671 // Because this function doesn't need to use scale (per the bc spec),
1672 // I am hijacking it to say whether it's doing an add or a subtract.
1673
1674 if (a->len == 0) {
1675 bc_num_copy(c, b);
1676 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001677 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001678 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001679 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001680 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001681 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001682 }
1683
1684 c->neg = a->neg;
1685 c->rdx = BC_MAX(a->rdx, b->rdx);
1686 min_rdx = BC_MIN(a->rdx, b->rdx);
1687 c->len = 0;
1688
1689 if (a->rdx > b->rdx) {
1690 diff = a->rdx - b->rdx;
1691 ptr = a->num;
1692 ptr_a = a->num + diff;
1693 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001694 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001695 diff = b->rdx - a->rdx;
1696 ptr = b->num;
1697 ptr_a = a->num;
1698 ptr_b = b->num + diff;
1699 }
1700
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001701 ptr_c = c->num;
1702 for (i = 0; i < diff; ++i, ++c->len)
1703 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001704
1705 ptr_c += diff;
1706 a_int = BC_NUM_INT(a);
1707 b_int = BC_NUM_INT(b);
1708
1709 if (a_int > b_int) {
1710 min_int = b_int;
1711 max = a_int;
1712 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001713 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001714 min_int = a_int;
1715 max = b_int;
1716 ptr = ptr_b;
1717 }
1718
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001719 carry = 0;
1720 for (i = 0; i < min_rdx + min_int; ++i) {
1721 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001722 carry = in / 10;
1723 ptr_c[i] = (BcDig)(in % 10);
1724 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001725 for (; i < max + min_rdx; ++i) {
1726 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001727 carry = in / 10;
1728 ptr_c[i] = (BcDig)(in % 10);
1729 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001730 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001731
1732 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1733
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001734 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001735}
1736
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001737static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001738{
Gavin Howard01055ba2018-11-03 11:00:21 -06001739 ssize_t cmp;
1740 BcNum *minuend, *subtrahend;
1741 size_t start;
1742 bool aneg, bneg, neg;
1743
1744 // Because this function doesn't need to use scale (per the bc spec),
1745 // I am hijacking it to say whether it's doing an add or a subtract.
1746
1747 if (a->len == 0) {
1748 bc_num_copy(c, b);
1749 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001750 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001751 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001752 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001753 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001754 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001755 }
1756
1757 aneg = a->neg;
1758 bneg = b->neg;
1759 a->neg = b->neg = false;
1760
1761 cmp = bc_num_cmp(a, b);
1762
1763 a->neg = aneg;
1764 b->neg = bneg;
1765
1766 if (cmp == 0) {
1767 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001768 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001769 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001770 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001771 neg = a->neg;
1772 minuend = a;
1773 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001774 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001775 neg = b->neg;
1776 if (sub) neg = !neg;
1777 minuend = b;
1778 subtrahend = a;
1779 }
1780
1781 bc_num_copy(c, minuend);
1782 c->neg = neg;
1783
1784 if (c->rdx < subtrahend->rdx) {
1785 bc_num_extend(c, subtrahend->rdx - c->rdx);
1786 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001787 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001788 start = c->rdx - subtrahend->rdx;
1789
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001790 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001791
1792 bc_num_clean(c);
1793
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001794 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001795}
1796
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001797static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001798 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001799#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001800{
1801 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001802 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001804 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001805
Gavin Howard01055ba2018-11-03 11:00:21 -06001806 if (a->len == 0 || b->len == 0) {
1807 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001808 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001809 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001810 aone = BC_NUM_ONE(a);
1811 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001812 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001813 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001814 }
1815
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001816 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1817 || a->len < BC_NUM_KARATSUBA_LEN
1818 || b->len < BC_NUM_KARATSUBA_LEN
1819 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001820 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001821
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 bc_num_expand(c, a->len + b->len + 1);
1823
1824 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001825 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001826
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001827 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001828 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001829 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001830 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001831 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001832 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001833 carry = in / 10;
1834 c->num[i + j] = (BcDig)(in % 10);
1835 }
1836
1837 c->num[i + j] += (BcDig) carry;
1838 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001839
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001840#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001841 // a=2^1000000
1842 // a*a <- without check below, this will not be interruptible
1843 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001844#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001845 }
1846
1847 c->len = len;
1848
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001849 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001850 }
1851
1852 bc_num_init(&l1, max);
1853 bc_num_init(&h1, max);
1854 bc_num_init(&l2, max);
1855 bc_num_init(&h2, max);
1856 bc_num_init(&m1, max);
1857 bc_num_init(&m2, max);
1858 bc_num_init(&z0, max);
1859 bc_num_init(&z1, max);
1860 bc_num_init(&z2, max);
1861 bc_num_init(&temp, max + max);
1862
1863 bc_num_split(a, max2, &l1, &h1);
1864 bc_num_split(b, max2, &l2, &h2);
1865
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001866 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001868 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001869 if (s) goto err;
1870
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001871 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001872 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001873 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001875 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 if (s) goto err;
1877
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001878 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001879 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001880 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 if (s) goto err;
1882
Denys Vlasenko29301232018-12-11 15:29:32 +01001883 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001884 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001885 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001887 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001888 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001889 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001890 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 bc_num_free(&temp);
1892 bc_num_free(&z2);
1893 bc_num_free(&z1);
1894 bc_num_free(&z0);
1895 bc_num_free(&m2);
1896 bc_num_free(&m1);
1897 bc_num_free(&h2);
1898 bc_num_free(&l2);
1899 bc_num_free(&h1);
1900 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001901 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001902}
1903
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001904static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001905{
1906 BcStatus s;
1907 BcNum cpa, cpb;
1908 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1909
1910 scale = BC_MAX(scale, a->rdx);
1911 scale = BC_MAX(scale, b->rdx);
1912 scale = BC_MIN(a->rdx + b->rdx, scale);
1913 maxrdx = BC_MAX(maxrdx, scale);
1914
1915 bc_num_init(&cpa, a->len);
1916 bc_num_init(&cpb, b->len);
1917
1918 bc_num_copy(&cpa, a);
1919 bc_num_copy(&cpb, b);
1920 cpa.neg = cpb.neg = false;
1921
Denys Vlasenko29301232018-12-11 15:29:32 +01001922 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001923 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001924 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001925 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001926 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001927 if (s) goto err;
1928
1929 maxrdx += scale;
1930 bc_num_expand(c, c->len + maxrdx);
1931
1932 if (c->len < maxrdx) {
1933 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1934 c->len += maxrdx;
1935 }
1936
1937 c->rdx = maxrdx;
1938 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001939 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001940 bc_num_free(&cpb);
1941 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001942 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001943}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001944#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001945
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001946static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001947{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001948 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 size_t len, end, i;
1950 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001951
1952 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001953 RETURN_STATUS(bc_error("divide by zero"));
1954 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001955 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001956 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001957 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001958 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 bc_num_copy(c, a);
1960 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001961 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001962 }
1963
1964 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1965 bc_num_copy(&cp, a);
1966 len = b->len;
1967
1968 if (len > cp.len) {
1969 bc_num_expand(&cp, len + 2);
1970 bc_num_extend(&cp, len - cp.len);
1971 }
1972
1973 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1974 cp.rdx -= b->rdx;
1975 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1976
1977 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001978 for (;;) {
1979 if (len == 0) break;
1980 len--;
1981 if (b->num[len] != 0)
1982 break;
1983 }
1984 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001985 }
1986
1987 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1988
1989 // We want an extra zero in front to make things simpler.
1990 cp.num[cp.len++] = 0;
1991 end = cp.len - len;
1992
1993 bc_num_expand(c, cp.len);
1994
1995 bc_num_zero(c);
1996 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1997 c->rdx = cp.rdx;
1998 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001999
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002000 s = BC_STATUS_SUCCESS;
2001 for (i = end - 1; i < end; --i) {
2002 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002003 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002004 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2005 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002006 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002007#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002008 // a=2^100000
2009 // scale=40000
2010 // 1/a <- without check below, this will not be interruptible
2011 if (G_interrupt) {
2012 s = BC_STATUS_FAILURE;
2013 break;
2014 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002015#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002016 }
2017
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002018 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002019 bc_num_free(&cp);
2020
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002021 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002022}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002023#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002024
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002025static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002026 BcNum *restrict d, size_t scale, size_t ts)
2027{
2028 BcStatus s;
2029 BcNum temp;
2030 bool neg;
2031
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002032 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002033 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002034
2035 if (a->len == 0) {
2036 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002037 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002038 }
2039
2040 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002041 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002042 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002043
2044 if (scale != 0) scale = ts;
2045
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002046 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002047 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002048 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002049 if (s) goto err;
2050
2051 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2052
2053 neg = d->neg;
2054 bc_num_retireMul(d, ts, a->neg, b->neg);
2055 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002056 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002057 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002059}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002060#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002061
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002062static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002063{
2064 BcStatus s;
2065 BcNum c1;
2066 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2067
2068 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002069 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002070 bc_num_free(&c1);
2071
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002072 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002073}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002074#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002075
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002076static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002077{
2078 BcStatus s = BC_STATUS_SUCCESS;
2079 BcNum copy;
2080 unsigned long pow;
2081 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002082 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002083
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002084 // GNU bc does not allow 2^2.0 - we do
2085 for (i = 0; i < b->rdx; i++)
2086 if (b->num[i] != 0)
2087 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002088
2089 if (b->len == 0) {
2090 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002091 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002092 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002094 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002095 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002096 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002098 if (!b->neg)
2099 bc_num_copy(c, a);
2100 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002101 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002102 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002103 }
2104
2105 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002106 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002107 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002108 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002109
2110 bc_num_init(&copy, a->len);
2111 bc_num_copy(&copy, a);
2112
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002113 if (!neg) {
2114 if (a->rdx > scale)
2115 scale = a->rdx;
2116 if (a->rdx * pow < scale)
2117 scale = a->rdx * pow;
2118 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002119
Gavin Howard01055ba2018-11-03 11:00:21 -06002120
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002121 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002123 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002124 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002125 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002126 //if (G_interrupt) {
2127 // s = BC_STATUS_FAILURE;
2128 // goto err;
2129 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002130 }
2131
Gavin Howard01055ba2018-11-03 11:00:21 -06002132 bc_num_copy(c, &copy);
2133
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002134 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002135 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002136 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002137 if (s) goto err;
2138
2139 if (pow & 1) {
2140 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002141 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002142 if (s) goto err;
2143 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002145 //if (G_interrupt) {
2146 // s = BC_STATUS_FAILURE;
2147 // goto err;
2148 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 }
2150
2151 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002152 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002153 if (s) goto err;
2154 }
2155
Gavin Howard01055ba2018-11-03 11:00:21 -06002156 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2157
2158 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002159 for (i = 0; i < c->len; ++i)
2160 if (c->num[i] != 0)
2161 goto skip;
2162 bc_num_setToZero(c, scale);
2163 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002164
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002165 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002167 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002168}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002169#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002170
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002171static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002172{
2173 BcStatus s;
2174 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2175 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2176 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2177
2178 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2179 bc_num_expand(b, req);
2180
2181 if (a->len == 0) {
2182 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002183 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002184 }
2185 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002186 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002187 }
2188 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002189 bc_num_one(b);
2190 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002191 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002192 }
2193
2194 scale = BC_MAX(scale, a->rdx) + 1;
2195 len = a->len + scale;
2196
2197 bc_num_init(&num1, len);
2198 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002199 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002200
2201 bc_num_one(&half);
2202 half.num[0] = 5;
2203 half.rdx = 1;
2204
2205 bc_num_init(&f, len);
2206 bc_num_init(&fprime, len);
2207
2208 x0 = &num1;
2209 x1 = &num2;
2210
2211 bc_num_one(x0);
2212 pow = BC_NUM_INT(a);
2213
2214 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 if (pow & 1)
2216 x0->num[0] = 2;
2217 else
2218 x0->num[0] = 6;
2219
2220 pow -= 2 - (pow & 1);
2221
2222 bc_num_extend(x0, pow);
2223
2224 // Make sure to move the radix back.
2225 x0->rdx -= pow;
2226 }
2227
2228 x0->rdx = digs = digs1 = 0;
2229 resrdx = scale + 2;
2230 len = BC_NUM_INT(x0) + resrdx - 1;
2231
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002232 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002233 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002234 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002235 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002236 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002237 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002238 if (s) goto err;
2239
2240 cmp = bc_num_cmp(x1, x0);
2241 digs = x1->len - (unsigned long long) llabs(cmp);
2242
2243 if (cmp == cmp2 && digs == digs1)
2244 times += 1;
2245 else
2246 times = 0;
2247
2248 resrdx += times > 4;
2249
2250 cmp2 = cmp1;
2251 cmp1 = cmp;
2252 digs1 = digs;
2253
2254 temp = x0;
2255 x0 = x1;
2256 x1 = temp;
2257 }
2258
Gavin Howard01055ba2018-11-03 11:00:21 -06002259 bc_num_copy(b, x0);
2260 scale -= 1;
2261 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002262 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002263 bc_num_free(&fprime);
2264 bc_num_free(&f);
2265 bc_num_free(&half);
2266 bc_num_free(&num2);
2267 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002268 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002269}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002270#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002271
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002272static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002273 size_t scale)
2274{
2275 BcStatus s;
2276 BcNum num2, *ptr_a;
2277 bool init = false;
2278 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2279
2280 if (c == a) {
2281 memcpy(&num2, c, sizeof(BcNum));
2282 ptr_a = &num2;
2283 bc_num_init(c, len);
2284 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002285 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002286 ptr_a = a;
2287 bc_num_expand(c, len);
2288 }
2289
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002290 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002291
2292 if (init) bc_num_free(&num2);
2293
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002294 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002295}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002296#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002297
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002298#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002299static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002300{
2301 BcStatus s;
2302 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002303 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002304
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002305 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002306 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002307 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002308 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002309 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002310 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002311
2312 bc_num_expand(d, c->len);
2313 bc_num_init(&base, c->len);
2314 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002315 bc_num_init(&temp, b->len);
2316
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002317 two.cap = ARRAY_SIZE(two_digs);
2318 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002319 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002320 two_digs[0] = 2;
2321
Gavin Howard01055ba2018-11-03 11:00:21 -06002322 bc_num_one(d);
2323
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002324 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002325 if (s) goto err;
2326 bc_num_copy(&exp, b);
2327
2328 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002329 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002330 if (s) goto err;
2331
2332 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002333 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002334 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002335 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002336 if (s) goto err;
2337 }
2338
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002339 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002340 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002341 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002342 if (s) goto err;
2343 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002344 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002345 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002346 bc_num_free(&exp);
2347 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002348 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002349}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002350#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002351#endif // ENABLE_DC
2352
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002353static FAST_FUNC void bc_string_free(void *string)
2354{
2355 free(*(char**)string);
2356}
2357
Gavin Howard01055ba2018-11-03 11:00:21 -06002358static void bc_func_init(BcFunc *f)
2359{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002360 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002361 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2362 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002363 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2364 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002365 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002366}
2367
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002368static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002369{
2370 BcFunc *f = (BcFunc *) func;
2371 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002372 IF_BC(bc_vec_free(&f->labels);)
2373 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002374 IF_BC(bc_vec_free(&f->strs);)
2375 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002376}
2377
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002378static void bc_array_expand(BcVec *a, size_t len);
2379
Gavin Howard01055ba2018-11-03 11:00:21 -06002380static void bc_array_init(BcVec *a, bool nums)
2381{
2382 if (nums)
2383 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2384 else
2385 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2386 bc_array_expand(a, 1);
2387}
2388
Gavin Howard01055ba2018-11-03 11:00:21 -06002389static void bc_array_expand(BcVec *a, size_t len)
2390{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002391 if (a->dtor == bc_num_free
2392 // && a->size == sizeof(BcNum) - always true
2393 ) {
2394 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002395 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002396 bc_num_init_DEF_SIZE(&n);
2397 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002398 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002399 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002400 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002401 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002402 bc_array_init(&v, true);
2403 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002404 }
2405 }
2406}
2407
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002408static void bc_array_copy(BcVec *d, const BcVec *s)
2409{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002410 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002411 size_t i;
2412
2413 bc_vec_pop_all(d);
2414 bc_vec_expand(d, s->cap);
2415 d->len = s->len;
2416
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002417 dnum = (void*)d->v;
2418 snum = (void*)s->v;
2419 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002420 bc_num_init(dnum, snum->len);
2421 bc_num_copy(dnum, snum);
2422 }
2423}
2424
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002425#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002426static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002427{
2428 d->t = src->t;
2429
2430 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002431 case XC_RESULT_TEMP:
2432 case XC_RESULT_IBASE:
2433 case XC_RESULT_SCALE:
2434 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002435 bc_num_init(&d->d.n, src->d.n.len);
2436 bc_num_copy(&d->d.n, &src->d.n);
2437 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002438 case XC_RESULT_VAR:
2439 case XC_RESULT_ARRAY:
2440 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002441 d->d.id.name = xstrdup(src->d.id.name);
2442 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002443 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002444 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002445 IF_BC(case BC_RESULT_ONE:)
2446 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002447 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2448 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002449 }
2450}
2451#endif // ENABLE_DC
2452
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002453static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002454{
2455 BcResult *r = (BcResult *) result;
2456
2457 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002458 case XC_RESULT_TEMP:
2459 case XC_RESULT_IBASE:
2460 case XC_RESULT_SCALE:
2461 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002462 bc_num_free(&r->d.n);
2463 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002464 case XC_RESULT_VAR:
2465 case XC_RESULT_ARRAY:
2466 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002467 free(r->d.id.name);
2468 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002469 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002470 // Do nothing.
2471 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002472 }
2473}
2474
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002475static int bad_input_byte(char c)
2476{
2477 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2478 || c > 0x7e
2479 ) {
2480 bc_error_fmt("illegal character 0x%02x", c);
2481 return 1;
2482 }
2483 return 0;
2484}
2485
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002486static void bc_read_line(BcVec *vec, FILE *fp)
2487{
2488 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002489 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002490 fflush_and_check();
2491
2492#if ENABLE_FEATURE_BC_SIGNALS
2493 if (G_interrupt) { // ^C was pressed
2494 intr:
2495 if (fp != stdin) {
2496 // ^C while running a script (bc SCRIPT): die.
2497 // We do not return to interactive prompt:
2498 // user might be running us from a shell,
2499 // and SCRIPT might be intended to terminate
2500 // (e.g. contain a "halt" stmt).
2501 // ^C dropping user into a bc prompt instead of
2502 // the shell would be unexpected.
2503 xfunc_die();
2504 }
2505 // ^C while interactive input
2506 G_interrupt = 0;
2507 // GNU bc says "interrupted execution."
2508 // GNU dc says "Interrupt!"
2509 fputs("\ninterrupted execution\n", stderr);
2510 }
2511
2512# if ENABLE_FEATURE_EDITING
2513 if (G_ttyin && fp == stdin) {
2514 int n, i;
2515# define line_buf bb_common_bufsiz1
2516 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2517 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2518 if (n == 0) // ^C
2519 goto intr;
2520 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2521 return;
2522 }
2523 i = 0;
2524 for (;;) {
2525 char c = line_buf[i++];
2526 if (!c) break;
2527 if (bad_input_byte(c)) goto again;
2528 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002529 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002530# undef line_buf
2531 } else
2532# endif
2533#endif
2534 {
2535 int c;
2536 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002537
2538 do {
2539#if ENABLE_FEATURE_BC_SIGNALS
2540 if (G_interrupt) {
2541 // ^C was pressed: ignore entire line, get another one
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002542 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002543 goto intr;
2544 }
2545#endif
2546 do c = fgetc(fp); while (c == '\0');
2547 if (c == EOF) {
2548 if (ferror(fp))
2549 bb_perror_msg_and_die("input error");
2550 // Note: EOF does not append '\n'
2551 break;
2552 }
2553 bad_chars |= bad_input_byte(c);
2554 bc_vec_pushByte(vec, (char)c);
2555 } while (c != '\n');
2556
2557 if (bad_chars) {
2558 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002559 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002560 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002561 goto again;
2562 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002563 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002564 }
2565 bc_vec_pushZeroByte(vec);
2566 }
2567}
2568
2569//
2570// Parsing routines
2571//
2572
2573static bool bc_num_strValid(const char *val, size_t base)
2574{
2575 BcDig b;
2576 bool radix;
2577
2578 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2579 radix = false;
2580 for (;;) {
2581 BcDig c = *val++;
2582 if (c == '\0')
2583 break;
2584 if (c == '.') {
2585 if (radix) return false;
2586 radix = true;
2587 continue;
2588 }
2589 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2590 return false;
2591 }
2592 return true;
2593}
2594
2595// Note: n is already "bc_num_zero()"ed,
2596// leading zeroes in "val" are removed
2597static void bc_num_parseDecimal(BcNum *n, const char *val)
2598{
2599 size_t len, i;
2600 const char *ptr;
2601
2602 len = strlen(val);
2603 if (len == 0)
2604 return;
2605
2606 bc_num_expand(n, len);
2607
2608 ptr = strchr(val, '.');
2609
2610 n->rdx = 0;
2611 if (ptr != NULL)
2612 n->rdx = (size_t)((val + len) - (ptr + 1));
2613
2614 for (i = 0; val[i]; ++i) {
2615 if (val[i] != '0' && val[i] != '.') {
2616 // Not entirely zero value - convert it, and exit
2617 i = len - 1;
2618 for (;;) {
2619 n->num[n->len] = val[i] - '0';
2620 ++n->len;
2621 skip_dot:
2622 if (i == 0) break;
2623 if (val[--i] == '.') goto skip_dot;
2624 }
2625 break;
2626 }
2627 }
2628 // if for() exits without hitting if(), the value is entirely zero
2629}
2630
2631// Note: n is already "bc_num_zero()"ed,
2632// leading zeroes in "val" are removed
2633static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2634{
2635 BcStatus s;
2636 BcNum temp, mult, result;
2637 BcNum base;
2638 BcDig base_digs[ULONG_NUM_BUFSIZE];
2639 BcDig c = '\0';
2640 unsigned long v;
2641 size_t i, digits;
2642
2643 for (i = 0; ; ++i) {
2644 if (val[i] == '\0')
2645 return;
2646 if (val[i] != '.' && val[i] != '0')
2647 break;
2648 }
2649
2650 bc_num_init_DEF_SIZE(&temp);
2651 bc_num_init_DEF_SIZE(&mult);
2652 base.cap = ARRAY_SIZE(base_digs);
2653 base.num = base_digs;
2654 bc_num_ulong2num(&base, base_t);
2655
2656 for (;;) {
2657 c = *val++;
2658 if (c == '\0') goto int_err;
2659 if (c == '.') break;
2660
2661 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2662
2663 s = zbc_num_mul(n, &base, &mult, 0);
2664 if (s) goto int_err;
2665 bc_num_ulong2num(&temp, v);
2666 s = zbc_num_add(&mult, &temp, n, 0);
2667 if (s) goto int_err;
2668 }
2669
2670 bc_num_init(&result, base.len);
2671 //bc_num_zero(&result); - already is
2672 bc_num_one(&mult);
2673
2674 digits = 0;
2675 for (;;) {
2676 c = *val++;
2677 if (c == '\0') break;
2678 digits++;
2679
2680 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2681
2682 s = zbc_num_mul(&result, &base, &result, 0);
2683 if (s) goto err;
2684 bc_num_ulong2num(&temp, v);
2685 s = zbc_num_add(&result, &temp, &result, 0);
2686 if (s) goto err;
2687 s = zbc_num_mul(&mult, &base, &mult, 0);
2688 if (s) goto err;
2689 }
2690
2691 s = zbc_num_div(&result, &mult, &result, digits);
2692 if (s) goto err;
2693 s = zbc_num_add(n, &result, n, digits);
2694 if (s) goto err;
2695
2696 if (n->len != 0) {
2697 if (n->rdx < digits)
2698 bc_num_extend(n, digits - n->rdx);
2699 } else
2700 bc_num_zero(n);
2701 err:
2702 bc_num_free(&result);
2703 int_err:
2704 bc_num_free(&mult);
2705 bc_num_free(&temp);
2706}
2707
2708static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2709{
2710 if (!bc_num_strValid(val, base_t))
2711 RETURN_STATUS(bc_error("bad number string"));
2712
2713 bc_num_zero(n);
2714 while (*val == '0') val++;
2715
2716 if (base_t == 10)
2717 bc_num_parseDecimal(n, val);
2718 else
2719 bc_num_parseBase(n, val, base_t);
2720
2721 RETURN_STATUS(BC_STATUS_SUCCESS);
2722}
2723#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2724
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002725static bool bc_lex_more_input(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002726{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002727 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002728
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002729 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002730
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002731 bc_read_line(&G.input_buffer, G.prs.lex_input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002732
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002733 p->lex_inbuf = G.input_buffer.v;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002734// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002735
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002736 return G.input_buffer.len > 1;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002737}
2738
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002739// p->lex_inbuf points to the current string to be parsed.
2740// if p->lex_inbuf points to '\0', it's either EOF or it points after
2741// last processed line's terminating '\n' (and more reading needs to be done
2742// to get next character).
2743//
2744// If you are in a situation where that is a possibility, call peek_inbuf().
2745// If necessary, it performs more reading and changes p->lex_inbuf,
2746// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2747// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2748// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2749//
2750// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2751// it returns current char and advances the pointer (if not EOF).
2752// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2753//
2754// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2755// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2756// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002757//
2758// bc cases to test interactively:
2759// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2760// 1 #comment/* - prints "1<newline>" at once
2761// 1 #comment" - prints "1<newline>" at once
2762// 1\#comment - error at once (\ is not a line continuation)
2763// 1 + /*"*/2 - prints "3<newline>" at once
2764// 1 + /*#*/2 - prints "3<newline>" at once
2765// "str\" - prints "str\" at once
2766// "str#" - prints "str#" at once
2767// "str/*" - prints "str/*" at once
2768// "str#\ - waits for second line
2769// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002770static char peek_inbuf(void)
2771{
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002772 if (*G.prs.lex_inbuf == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002773 if (G.prs.lex_input_fp)
2774 if (!bc_lex_more_input())
2775 G.prs.lex_input_fp = NULL;
2776 }
2777 return *G.prs.lex_inbuf;
2778}
2779static char eat_inbuf(void)
2780{
2781 char c = peek_inbuf();
2782 if (c) G.prs.lex_inbuf++;
2783 return c;
2784}
2785
2786static void bc_lex_lineComment(void)
2787{
2788 BcParse *p = &G.prs;
2789 char c;
2790
2791 // Try: echo -n '#foo' | bc
2792 p->lex = XC_LEX_WHITESPACE;
2793
2794 // We depend here on input being done in whole lines:
2795 // '\0' which isn't the EOF can only be seen after '\n'.
2796 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2797 p->lex_inbuf++;
2798}
2799
2800static void bc_lex_whitespace(void)
2801{
2802 BcParse *p = &G.prs;
2803
2804 p->lex = XC_LEX_WHITESPACE;
2805 for (;;) {
2806 // We depend here on input being done in whole lines:
2807 // '\0' which isn't the EOF can only be seen after '\n'.
2808 char c = *p->lex_inbuf;
2809 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2810 break;
2811 if (!isspace(c))
2812 break;
2813 p->lex_inbuf++;
2814 }
2815}
2816
2817static BC_STATUS zbc_lex_number(char last)
2818{
2819 BcParse *p = &G.prs;
2820 bool pt;
2821
2822 bc_vec_pop_all(&p->lex_strnumbuf);
2823 bc_vec_pushByte(&p->lex_strnumbuf, last);
2824
2825 pt = (last == '.');
2826 p->lex = XC_LEX_NUMBER;
2827 for (;;) {
2828 // We depend here on input being done in whole lines:
2829 // '\0' which isn't the EOF can only be seen after '\n'.
2830 char c = *p->lex_inbuf;
2831 check_c:
2832 if (c == '\0')
2833 break;
2834 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2835 p->lex_inbuf += 2;
2836 p->lex_line++;
2837 c = peek_inbuf(); // force next line to be read
2838 goto check_c;
2839 }
2840 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2841 if (c != '.') break;
2842 // if '.' was already seen, stop on second one:
2843 if (pt) break;
2844 pt = true;
2845 }
2846 // c is one of "0-9A-F."
2847 last = c;
2848 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2849 p->lex_inbuf++;
2850 }
2851 if (last == '.') // remove trailing '.' if any
2852 bc_vec_pop(&p->lex_strnumbuf);
2853 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2854
2855 G.err_line = G.prs.lex_line;
2856 RETURN_STATUS(BC_STATUS_SUCCESS);
2857}
2858#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
2859
2860static void bc_lex_name(void)
2861{
2862 BcParse *p = &G.prs;
2863 size_t i;
2864 const char *buf;
2865
2866 p->lex = XC_LEX_NAME;
2867
2868 // Since names can't cross lines with \<newline>,
2869 // we depend on the fact that whole line is in the buffer
2870 i = 0;
2871 buf = p->lex_inbuf - 1;
2872 for (;;) {
2873 char c = buf[i];
2874 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2875 i++;
2876 }
2877
2878#if 0 // We do not protect against people with gigabyte-long names
2879 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2880 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2881 if (i > BC_MAX_STRING)
2882 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2883 }
2884#endif
2885 bc_vec_string(&p->lex_strnumbuf, i, buf);
2886
2887 // Increment the index. We minus 1 because it has already been incremented.
2888 p->lex_inbuf += i - 1;
2889
2890 //return BC_STATUS_SUCCESS;
2891}
2892
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002893IF_BC(static BC_STATUS zbc_lex_token(void);)
2894IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002895#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2896#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002897
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002898static BC_STATUS zbc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002899{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002900 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002901 BcStatus s;
2902
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002903 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002904//why?
2905// if (p->lex_last == XC_LEX_EOF)
2906// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002907
2908 // Loop until failure or we don't have whitespace. This
2909 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002910 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002911 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002912 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002913 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002914 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002915 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002916 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002917 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002918 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002919 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002920 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2921 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002922 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002923 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002924 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002925 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002926 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002927 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002928 } while (!s && p->lex == XC_LEX_WHITESPACE);
2929 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002930
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002931 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002932}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002933#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002934
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002935#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002936static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002937{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002938 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002939 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002940 RETURN_STATUS(BC_STATUS_SUCCESS);
2941}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002942#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002943
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002944static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002945{
2946 BcStatus s;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002947 s = zbc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002948 if (s) RETURN_STATUS(s);
2949 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002950 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002951 RETURN_STATUS(s);
2952}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002953#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002954#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01002955
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002956static BC_STATUS zbc_lex_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002957{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002958 G.prs.lex_inbuf = text;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002959 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002960 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06002961}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002962#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002963
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002964#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002965static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002966{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002967 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002968 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002969 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002970 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002971
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002972 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2973 const char *keyword8 = bc_lex_kws[i].name8;
2974 unsigned j = 0;
2975 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2976 j++;
2977 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002978 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002979 if (keyword8[j] != '\0')
2980 continue;
2981 match:
2982 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002983 if (isalnum(buf[j]) || buf[j]=='_')
2984 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002985 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01002986 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01002987 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
2988 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002989 }
2990
2991 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002992 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002993 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002994 }
2995
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002996 bc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002997 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002998
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002999 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003000 // Prevent this:
3001 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003002 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003003 // '
3004 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003005 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003006 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003007
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003008 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003009}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003010#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003011
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003012static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003013{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003014 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003015
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003016 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003017 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003018 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003019 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003020 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003021 RETURN_STATUS(bc_error("unterminated string1"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003022 }
3023 if (c == '"')
3024 break;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003025 if (c == '\n')
3026 p->lex_line++;
3027 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3028 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003029 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003030 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3031 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003032
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003033 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003034 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003035}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003036#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003038static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003039{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003040 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003041 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003042 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003043 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003044 with_and_without >>= 8; // store "with" value
3045 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003046 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003047}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003048#define parse_lex_by_checking_eq_sign(with, without) \
3049 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003050
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003051static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003052{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003053 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003054
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003055 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003056 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003057 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003058 char c;
3059
3060 p->lex_inbuf++;
3061 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003062 check_star:
3063 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003064 p->lex_inbuf++;
3065 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003066 if (c == '/')
3067 break;
3068 goto check_star;
3069 }
3070 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003071 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003072 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003073 if (c == '\n')
3074 p->lex_line++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003075 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003076 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003077
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003078 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003079 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003080}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003081#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003082
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003083#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003084static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003085{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003086 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003087 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003088 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003089 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
3091 // This is the workhorse of the lexer.
3092 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003093// case '\0': // probably never reached
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003094// p->lex_inbuf--;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003095// p->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003096// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003097 case '\n':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003098 p->lex_line++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003099 p->lex = XC_LEX_NLINE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003101 case '\t':
3102 case '\v':
3103 case '\f':
3104 case '\r':
3105 case ' ':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003106 bc_lex_whitespace();
Gavin Howard01055ba2018-11-03 11:00:21 -06003107 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 case '!':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003109 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003110 if (p->lex == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003111 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
3112 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003113 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003114 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003115 case '"':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003116 s = zbc_lex_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118 case '#':
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003119 s = zbc_POSIX_does_not_allow("'#' script comments");
3120 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003121 bc_lex_lineComment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003122 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003123 case '%':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003124 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003126 case '&':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003127 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 if (c2 == '&') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003129 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3130 if (s) RETURN_STATUS(s);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003131 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003132 p->lex = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003133 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003134 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003135 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003138 case '(':
3139 case ')':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003140 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 case '*':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003143 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 case '+':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003146 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003147 if (c2 == '+') {
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003148 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003149 p->lex = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003150 } else
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003151 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 case ',':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003154 p->lex = BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 case '-':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003157 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003158 if (c2 == '-') {
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003159 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003160 p->lex = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003161 } else
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003162 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 case '.':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003165 if (isdigit(*p->lex_inbuf))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003166 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003168 p->lex = BC_LEX_KEY_LAST;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003169 s = zbc_POSIX_does_not_allow("'.' as 'last'");
Gavin Howard01055ba2018-11-03 11:00:21 -06003170 }
3171 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 case '/':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003173 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 if (c2 == '*')
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003175 s = zbc_lex_comment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003176 else
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003177 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003178 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003179 case '0':
3180 case '1':
3181 case '2':
3182 case '3':
3183 case '4':
3184 case '5':
3185 case '6':
3186 case '7':
3187 case '8':
3188 case '9':
3189 case 'A':
3190 case 'B':
3191 case 'C':
3192 case 'D':
3193 case 'E':
3194 case 'F':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003195 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 case ';':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003198 p->lex = BC_LEX_SCOLON;
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 case '<':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003201 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 case '=':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003204 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 case '>':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003207 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003209 case '[':
3210 case ']':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003211 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 case '\\':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003214 if (*p->lex_inbuf == '\n') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003215 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003216 p->lex_inbuf++;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003217 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003218 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 case '^':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003221 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 case 'a':
3224 case 'b':
3225 case 'c':
3226 case 'd':
3227 case 'e':
3228 case 'f':
3229 case 'g':
3230 case 'h':
3231 case 'i':
3232 case 'j':
3233 case 'k':
3234 case 'l':
3235 case 'm':
3236 case 'n':
3237 case 'o':
3238 case 'p':
3239 case 'q':
3240 case 'r':
3241 case 's':
3242 case 't':
3243 case 'u':
3244 case 'v':
3245 case 'w':
3246 case 'x':
3247 case 'y':
3248 case 'z':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003249 s = zbc_lex_identifier();
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003251 case '{':
3252 case '}':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003253 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 case '|':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003256 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003257 if (c2 == '|') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003258 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3259 if (s) RETURN_STATUS(s);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003260 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003261 p->lex = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003262 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003263 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003264 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 default:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003268 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003269 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 }
3272
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003273 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003274}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003275#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003276#endif // ENABLE_BC
3277
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003278#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003279static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003280{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003281 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003282 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003283 bc_lex_whitespace(); // eats whitespace (but not newline)
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003284 p->lex_inbuf++; // bc_lex_name() expects this
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003285 bc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003286 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003287 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003288 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003289 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3290 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 }
3292
Denys Vlasenko29301232018-12-11 15:29:32 +01003293 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003294}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003295#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003296
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003297static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003298{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003299 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003300 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003301
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003302 p->lex = XC_LEX_STR;
3303 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003304
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003305 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003306 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003307 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003308 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003309 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003310 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003311 if (c == '[') depth++;
3312 if (c == ']')
3313 if (--depth == 0)
3314 break;
3315 if (c == '\n')
3316 p->lex_line++;
3317 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3318 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003319 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003320 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003321 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003322
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003323 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003324 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003325}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003326#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003327
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003328#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003329static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003330{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003331 BcParse *p = &G.prs;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003332 static const //BcLexType - should be this type, but narrower type saves size:
3333 uint8_t
3334 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003335 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 +01003336 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3337 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003338 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003339 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003340
Denys Vlasenko81293c82018-12-24 01:53:55 +01003341 BcStatus s;
3342 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003343 size_t i;
3344
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003345 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003346 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003347 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003348 }
3349
Denys Vlasenko81293c82018-12-24 01:53:55 +01003350 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003351 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003352 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003353 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003354 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003355 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356 }
3357
3358 // This is the workhorse of the lexer.
3359 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003360// case '\0': // probably never reached
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003361// p->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003362// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003363 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003364 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003365 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003366 // only to allow interactive dc have a way to exit
3367 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003368 // on <enter>, not on _next_ token (which would mean
3369 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003370 // IOW: typing "1p<enter>" should print "1" _at once_,
3371 // not after some more input.
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003372 p->lex_line++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003373 p->lex = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003374 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003375 case '\t':
3376 case '\v':
3377 case '\f':
3378 case '\r':
3379 case ' ':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003380 bc_lex_whitespace();
Gavin Howard01055ba2018-11-03 11:00:21 -06003381 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003382 case '!':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003383 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003384 if (c2 == '=')
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003385 p->lex = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003386 else if (c2 == '<')
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003387 p->lex = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003388 else if (c2 == '>')
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003389 p->lex = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003390 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003391 RETURN_STATUS(bc_error_bad_character(c));
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003392 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003393 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003394 case '#':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003395 bc_lex_lineComment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003396 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003397 case '.':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003398 if (isdigit(*p->lex_inbuf))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003399 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003400 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003401 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003402 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003403 case '0':
3404 case '1':
3405 case '2':
3406 case '3':
3407 case '4':
3408 case '5':
3409 case '6':
3410 case '7':
3411 case '8':
3412 case '9':
3413 case 'A':
3414 case 'B':
3415 case 'C':
3416 case 'D':
3417 case 'E':
3418 case 'F':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003419 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003420 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003421 case '[':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003422 s = zdc_lex_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06003423 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003424 default:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003425 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003426 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003427 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003428 }
3429
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003430 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003431}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003432#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003433#endif // ENABLE_DC
3434
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003435static void bc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003436{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003437 BcParse *p = &G.prs;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003438 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003439 bc_vec_pushByte(&p->func->code, i);
3440}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003441
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003442static void bc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003443{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003444 while (*name)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003445 bc_parse_push(*name++);
3446 bc_parse_push(BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003447}
3448
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003449static void bc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003450{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003451 size_t mask;
3452 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003453
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003454 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003455 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3456 amt = sizeof(idx);
3457 do {
3458 if (idx & mask) break;
3459 mask >>= 8;
3460 amt--;
3461 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003462
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003463 bc_parse_push(amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003464
3465 while (idx != 0) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003466 bc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003467 idx >>= 8;
3468 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003469}
3470
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003471#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003472static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003473{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003474 bc_parse_push(BC_INST_JUMP);
3475 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003476}
3477
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003478static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003479{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003480 bc_parse_push(BC_INST_JUMP_ZERO);
3481 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003482}
3483
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003484static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003485{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003486 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003487 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003488
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003489 bc_parse_push(XC_INST_STR);
3490 bc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003491 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003492
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003493 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003494}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003495#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003496#endif
3497
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003498static void bc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003499{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003500 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003501 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003502#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003503 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003504#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003505 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003506#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003507 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003508#endif
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003509 bc_parse_push(XC_INST_NUM);
3510 bc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003511}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003512
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003513static BC_STATUS zbc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003514{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003515 BcParse *p = &G.prs;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003516 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003517
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003518 RETURN_STATUS(zbc_lex_text_init(text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003519}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003520#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003521
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003522// Called when parsing or execution detects a failure,
3523// resets execution structures.
3524static void bc_program_reset(void)
3525{
3526 BcFunc *f;
3527 BcInstPtr *ip;
3528
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003529 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003530 bc_vec_pop_all(&G.prog.results);
3531
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003532 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003533 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003534 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003535}
3536
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003537// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003538// resets parsing structures.
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003539static void bc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003540{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003541 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003542 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003543 bc_func_free(p->func);
3544 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003545
Denys Vlasenko65e10462018-12-19 15:13:14 +01003546 p->fidx = BC_PROG_MAIN;
3547 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003548 }
3549
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003550 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003551 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003552
Denys Vlasenko503faf92018-12-20 16:24:18 +01003553 IF_BC(bc_vec_pop_all(&p->exits);)
3554 IF_BC(bc_vec_pop_all(&p->conds);)
3555 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003556
Denys Vlasenkod38af482018-12-04 19:11:02 +01003557 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003558}
3559
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003560static void bc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003561{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003562 BcParse *p = &G.prs;
Denys Vlasenko503faf92018-12-20 16:24:18 +01003563 IF_BC(bc_vec_free(&p->exits);)
3564 IF_BC(bc_vec_free(&p->conds);)
3565 IF_BC(bc_vec_free(&p->ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003566 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003567}
3568
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003569static void bc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003570{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003571 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003572 memset(p, 0, sizeof(BcParse));
3573
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003574 bc_char_vec_init(&G.prs.lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003575 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3576 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3577 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003578
Denys Vlasenkofa210792018-12-19 19:35:40 +01003579 p->fidx = fidx;
3580 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003581}
3582
Denys Vlasenko04715442018-12-21 00:10:26 +01003583static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003584{
Denys Vlasenko04715442018-12-21 00:10:26 +01003585 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003586 BcFunc f;
3587 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003588 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003589 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003590 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003591}
3592
3593#if ENABLE_BC
3594
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003595// Note: takes ownership of 'name' (must be malloced)
3596static size_t bc_program_addFunc(char *name)
3597{
3598 size_t idx;
3599 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003600 int inserted;
3601
3602 entry.name = name;
3603 entry.idx = G.prog.fns.len;
3604
3605 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3606 if (!inserted) free(name);
3607
3608 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3609 idx = entry_ptr->idx;
3610
3611 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003612 // There is already a function with this name.
3613 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003614 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003615 bc_func_free(func);
3616 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003617 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003618 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003619 }
3620
3621 return idx;
3622}
3623
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003624#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003625// We can calculate the conversion between tokens and exprs by subtracting the
3626// position of the first operator in the lex enum and adding the position of the
3627// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003628#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003629
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003630static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003631
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003632static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003633{
3634 BcStatus s;
3635
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003636 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003637 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3638 RETURN_STATUS(bc_error("empty expression"));
3639 RETURN_STATUS(s);
3640}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003641#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003642
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003643static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003644#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003645
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003646static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003647{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003648 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003649}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003650#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003651
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003652static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003653{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003654 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003655 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3656 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003657 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003658 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003659 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003660 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003661
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003662 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003663}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003664#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003665
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003666static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003667{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003668 BcParse *p = &G.prs;
Denys Vlasenko69560f42018-12-24 14:14:23 +01003669 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3670 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003671
3672 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003673 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003674 if (t == BC_LEX_LPAREN) break;
3675
Denys Vlasenko69560f42018-12-24 14:14:23 +01003676 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003677 if (l >= r && (l != r || !left)) break;
3678
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003679 bc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003680 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003681 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003682 }
3683
3684 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003685}
3686
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003687static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003688{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003689 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003690 BcLexType top;
3691
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003692 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003693 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003694 top = BC_PARSE_TOP_OP(p);
3695
3696 while (top != BC_LEX_LPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003697 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003698
3699 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003700 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003701
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003702 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003703 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003704 top = BC_PARSE_TOP_OP(p);
3705 }
3706
3707 bc_vec_pop(&p->ops);
3708
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003709 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003710}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003711#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003712
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003713static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003714{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003715 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003716 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003717 size_t nparams;
3718
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003719 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003720 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3721
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003722 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003723 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003724
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003725 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003726 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003727 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003728 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003729 if (s) RETURN_STATUS(s);
3730 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003731 if (p->lex != BC_LEX_COMMA) {
3732 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003733 break;
3734 RETURN_STATUS(bc_error_bad_token());
3735 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003736 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003737 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003738 }
3739 }
3740
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003741 bc_parse_push(BC_INST_CALL);
3742 bc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003743
Denys Vlasenko26819db2018-12-12 16:08:46 +01003744 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003745}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003746#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003747
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003748// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003749static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003750{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003751 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003752 BcStatus s;
3753 BcId entry, *entry_ptr;
3754 size_t idx;
3755
3756 entry.name = name;
3757
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003758 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003759 if (s) goto err;
3760
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003761 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003762 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 goto err;
3764 }
3765
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003766 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003767
3768 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003769 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003770 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003771 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003772 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003773 free(name);
3774
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003775 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003776 bc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003777
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003778 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003779 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003780 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003781 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003782}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003783#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003784
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003785static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003786{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003787 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003788 BcStatus s;
3789 char *name;
3790
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003791 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003792 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003793 if (s) goto err;
3794
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003795 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003796 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003797 if (s) goto err;
3798
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003799 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003800 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003801 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003802 goto err;
3803 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003804 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003805 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003806 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003807 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003808 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 if (s) goto err;
3810 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003811 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003812 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003813 bc_parse_push(*type);
3814 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003815 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003816 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003818 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003819 goto err;
3820 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003821 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003822 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003823 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003824 *type = XC_INST_VAR;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003825 bc_parse_push(XC_INST_VAR);
3826 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003827 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003828 }
3829
Denys Vlasenko26819db2018-12-12 16:08:46 +01003830 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003831 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003832 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003833 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003834}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003835#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003836
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003837static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003838{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003839 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003840 BcStatus s;
3841
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003842 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003843 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003844 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003845
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003846 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003847 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003848 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003849
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003850 bc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003851
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003852 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003853}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003854#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003855
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003856static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003857{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003858 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003859 BcStatus s;
3860
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003861 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003862 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003863 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003864
3865 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3866
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003867 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003868 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003869
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003870 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003871 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003872
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003873 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003874
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003875 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003876 bc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003877
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003878 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003879}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003880#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003881
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003882static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003883{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003884 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 BcStatus s;
3886
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003887 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003888 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003889
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003890 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003891 *type = XC_INST_SCALE;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003892 bc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003893 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 }
3895
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003896 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3898
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003899 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003900 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003901
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003902 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003903 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003904 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003905 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003906 bc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003907
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003908 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003909}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003910#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003911
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003912static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
3913 size_t *nexprs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003914{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003915 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003916 BcStatus s;
3917 BcLexType type;
3918 char inst;
3919 BcInst etype = *prev;
3920
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003921 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3922 || etype == XC_INST_SCALE || etype == BC_INST_LAST
3923 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003924 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003925 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003926 bc_parse_push(inst);
3927 s = zbc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003928 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003929 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003930 *paren_expr = true;
3931
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003932 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003933 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003934 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06003935
3936 // Because we parse the next part of the expression
3937 // right here, we need to increment this.
3938 *nexprs = *nexprs + 1;
3939
3940 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003941 case XC_LEX_NAME:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003942 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003943 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003944 case BC_LEX_KEY_IBASE:
3945 case BC_LEX_KEY_LAST:
3946 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003947 bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
3948 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003949 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003950 case BC_LEX_KEY_SCALE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003951 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003953 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003954 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003955 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003956 bc_parse_push(XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003957 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003958 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003959 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003960 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003961 }
3962
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003963 if (!s) bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06003964 }
3965
Denys Vlasenko26819db2018-12-12 16:08:46 +01003966 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003967}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003968#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003969
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003970#if 0
3971#define BC_PARSE_LEAF(p, rparen) \
3972 ((rparen) \
3973 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
3974 || (p) == BC_INST_INC_POST \
3975 || (p) == BC_INST_DEC_POST \
3976 )
3977#else
3978static int ok_in_expr(BcInst p)
3979{
3980 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
3981 || p == BC_INST_INC_POST
3982 || p == BC_INST_DEC_POST
3983 ;
3984}
3985#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
3986#endif
3987
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003988static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
3989 bool rparen, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003990{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003991 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003992 BcStatus s;
3993 BcLexType type;
3994 BcInst etype = *prev;
3995
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003996 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003997 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003998
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003999 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004000 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004001
4002 // We can just push onto the op stack because this is the largest
4003 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004004 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004005 bc_vec_push(&p->ops, &type);
4006 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004007 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004008
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004009 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004010}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004011#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004012
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004013static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004014{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004015 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004016 BcStatus s;
4017 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004018
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004019 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004020 s = zbc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004021 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004022 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004023 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004024 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004025 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004026 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004027 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004028 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004029 bc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004030 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004031 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004032 }
4033
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004034 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004035}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004036#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004037
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004038static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004039{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004040 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004041 BcStatus s;
4042 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004043
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004044 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004045 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004046 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004047
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004048 t = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004049 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004050 bc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004051 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004052 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004053 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004054 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004055 bc_parse_push(BC_INST_RET0);
4056 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004057 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004058 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004059
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004060 if (!paren || p->lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004061 s = zbc_POSIX_requires("parentheses around return expressions");
4062 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 }
4064
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004065 bc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004066 }
4067
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004068 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004069 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004070}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004071#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004072
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004073static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004074{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004075 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004076 size_t *label = bc_vec_item(&p->func->labels, idx);
4077 *label = p->func->code.len;
4078}
4079
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004080static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004081{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004082 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004083 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004084 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004085
4086 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004087 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004088 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004089 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004090
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004091 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004092 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004093 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004094 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004095 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004096
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004097 // Encode "if zero, jump to ..."
4098 // Pushed value (destination of the jump) is uninitialized,
4099 // will be rewritten to be address of "end of if()" or of "else".
4100 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004101 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004102
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004103 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004104 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004105
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004106 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4107 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004108 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004109
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004110 // Encode "after then_stmt, jump to end of if()"
4111 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4112 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004113 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004114
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004115 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 +01004116 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004117
Denys Vlasenko15850832018-12-16 21:40:54 +01004118 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004119
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004120 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004121 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004122 }
4123
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004124 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 +01004125 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004126
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004127 dbg_lex_done("%s:%d done", __func__, __LINE__);
4128 RETURN_STATUS(s);
4129}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004130#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004131
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004132static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004133{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004134 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004135 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004136 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004137 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004138
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004139 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004140 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004141 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004142 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004143 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004144
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004145 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004146 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004147 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004148
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004149 bc_vec_push(&p->exits, &ip_idx);
4150 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004151
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004152 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004153 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004154 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004155
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004156 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004157
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004158 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159 if (s) RETURN_STATUS(s);
4160
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004161 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004162 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004163
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004164 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004165 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004166
4167 bc_vec_pop(&p->exits);
4168 bc_vec_pop(&p->conds);
4169
4170 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004171}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004172#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004173
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004174static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004175{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004176 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004177 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004178 size_t cond_idx, exit_idx, body_idx, update_idx;
4179
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004180 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004181 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004182 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004183 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004184 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004185 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004186
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004187 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004188 s = zbc_parse_expr(0);
4189 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004190 if (s) RETURN_STATUS(s);
4191 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004192 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4193 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004194 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004195
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004196 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004197 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004198 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004199
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004200 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004201 update_idx = cond_idx + 1;
4202 body_idx = update_idx + 1;
4203 exit_idx = body_idx + 1;
4204
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004205 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004206 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004207 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004208 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004209 // This is safe to set because the current token is a semicolon,
4210 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004211 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004212 bc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004213 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004214 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004215 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004216
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004217 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004218
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004219 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004220 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004221
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004222 bc_parse_pushJUMP_ZERO(exit_idx);
4223 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004224
Gavin Howard01055ba2018-11-03 11:00:21 -06004225 bc_vec_push(&p->conds, &update_idx);
4226 bc_vec_push(&p->func->labels, &p->func->code.len);
4227
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004228 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004229 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004230 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004231 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004232 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004233 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004234 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4235 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004236 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004237
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004238 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004239 bc_vec_push(&p->func->labels, &p->func->code.len);
4240
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004241 bc_vec_push(&p->exits, &exit_idx);
4242 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004243
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004244 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004245 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004246
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004247 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004248 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004249
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004250 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004251 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004252
4253 bc_vec_pop(&p->exits);
4254 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004255
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004256 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004257}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004258#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004259
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004260static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004261{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004262 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004263 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004264
Gavin Howard01055ba2018-11-03 11:00:21 -06004265 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004266 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4267 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004268 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004269 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004270 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004271 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004272 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004273
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004274 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004275}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004276#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004277
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004278static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4279{
4280 BcId *autoid;
4281 BcId a;
4282 size_t i;
4283
4284 autoid = (void*)f->autos.v;
4285 for (i = 0; i < f->autos.len; i++, autoid++) {
4286 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004287 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004288 }
4289
4290 a.idx = var;
4291 a.name = name;
4292
4293 bc_vec_push(&f->autos, &a);
4294
4295 RETURN_STATUS(BC_STATUS_SUCCESS);
4296}
4297#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4298
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004299static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004300{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004301 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004302 BcStatus s;
4303 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004304 char *name;
4305
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004306 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004307 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004308 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004309 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004310 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004311
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004312 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004313 p->fidx = bc_program_addFunc(name);
4314 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004315
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004316 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004317 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004318 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004319 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004320 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004321 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004322
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004323 while (p->lex != BC_LEX_RPAREN) {
4324 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004325 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004326
4327 ++p->func->nparams;
4328
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004329 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004330 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004331 if (s) goto err;
4332
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004333 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004334
4335 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004336 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004337 if (s) goto err;
4338
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004339 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004340 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004341 goto err;
4342 }
4343
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004344 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004345 if (s) goto err;
4346 }
4347
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004348 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004349 if (comma) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004350 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004351 if (s) goto err;
4352 }
4353
Denys Vlasenko29301232018-12-11 15:29:32 +01004354 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004355 if (s) goto err;
4356 }
4357
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004358 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004359
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004360 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004361 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004362
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004363 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004364 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004365 if (s) RETURN_STATUS(s);
4366 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004367
Denys Vlasenko202dd192018-12-16 17:30:35 +01004368 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004369 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004370 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004371 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004372 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004373 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004374
4375 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004376 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004377 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004378 if (s) RETURN_STATUS(s);
4379
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004380 bc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004381
4382 // Subsequent code generation is into main program
4383 p->fidx = BC_PROG_MAIN;
4384 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004385
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004386 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004387 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004388 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004389 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004390 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004391 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004392}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004393#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004394
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004395static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004396{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004397 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004398 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004399 char *name;
4400
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004401 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004402 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004403 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004404
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004405 for (;;) {
4406 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004407
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004408 if (p->lex != XC_LEX_NAME)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004409 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4410
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004411 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004412 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004413 if (s) goto err;
4414
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004415 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004416 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004417 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004418 if (s) goto err;
4419
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004420 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004421 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004422 goto err;
4423 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004424 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004425 if (s) goto err;
4426 }
4427
Denys Vlasenko29301232018-12-11 15:29:32 +01004428 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004429 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004430
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004431 if (p->lex == XC_LEX_NLINE
4432 || p->lex == BC_LEX_SCOLON
4433 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004434 ) {
4435 break;
4436 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004437 if (p->lex != BC_LEX_COMMA)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004438 RETURN_STATUS(bc_error("bad 'auto' syntax"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004439 s = zbc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004440 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004441 }
4442
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004443 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004444 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004445 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004446 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004447 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004448 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004449}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004450#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004451
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004452#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004453static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004454{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004455 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 BcStatus s = BC_STATUS_SUCCESS;
4457
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004458 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004459
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004460 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004461 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004462 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004463 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004464 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004465 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004466 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004467 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004468
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004469 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004470 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4471 do {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004472 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004473 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004474 } while (p->lex == XC_LEX_NLINE);
4475 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004476 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004477 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004478 if (s) RETURN_STATUS(s);
4479 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004480 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004481 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004482 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004483 if (s) RETURN_STATUS(s);
4484 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004485 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004486 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4487 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004488 }
4489
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004490 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4491 switch (p->lex) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004492 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004493 case BC_LEX_OP_INC:
4494 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004495 case BC_LEX_OP_BOOL_NOT:
4496 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004497 case XC_LEX_NAME:
4498 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 case BC_LEX_KEY_IBASE:
4500 case BC_LEX_KEY_LAST:
4501 case BC_LEX_KEY_LENGTH:
4502 case BC_LEX_KEY_OBASE:
4503 case BC_LEX_KEY_READ:
4504 case BC_LEX_KEY_SCALE:
4505 case BC_LEX_KEY_SQRT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004506 s = zbc_parse_expr(BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004507 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004508 case XC_LEX_STR:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004509 s = zbc_parse_pushSTR();
4510 bc_parse_push(XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004511 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004512 case BC_LEX_KEY_BREAK:
4513 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004514 s = zbc_parse_break_or_continue(p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004515 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004516 case BC_LEX_KEY_FOR:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004517 s = zbc_parse_for();
Gavin Howard01055ba2018-11-03 11:00:21 -06004518 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004519 case BC_LEX_KEY_HALT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004520 bc_parse_push(BC_INST_HALT);
4521 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004522 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004523 case BC_LEX_KEY_IF:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004524 s = zbc_parse_if();
Gavin Howard01055ba2018-11-03 11:00:21 -06004525 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004526 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004527 // "limits" is a compile-time command,
4528 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004529 printf(
4530 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4531 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4532 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4533 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4534 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4535 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4536 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4537 "Number of vars = "BC_MAX_VARS_STR "\n"
4538 );
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004539 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004540 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 case BC_LEX_KEY_PRINT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004542 s = zbc_parse_print();
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004544 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004545 // "quit" is a compile-time command. For example,
4546 // "if (0 == 1) quit" terminates when parsing the statement,
4547 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004548 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004550 if (!p->in_funcdef)
4551 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004552 s = zbc_parse_return();
Gavin Howard01055ba2018-11-03 11:00:21 -06004553 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004554 case BC_LEX_KEY_WHILE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004555 s = zbc_parse_while();
Gavin Howard01055ba2018-11-03 11:00:21 -06004556 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004557 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004558 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004559 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004560 }
4561
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004562 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004563 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004564}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004565#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004566
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004567static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004568{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004569 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 BcStatus s;
4571
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004572 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004573 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004574 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004575 else if (p->lex == BC_LEX_KEY_DEFINE) {
4576 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004577 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004578 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004579 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004580 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004581 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004582
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004583 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004584 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004585}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004586#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004587
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004588// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004589static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004590{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004591 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004592 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004594 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004595 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004596
Denys Vlasenko17df8822018-12-14 23:00:24 +01004597 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004598 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004599 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004600 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004601 bin_last = true;
4602
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004603 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004604 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004605 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004606 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004607
4608 if (!lex_allowed_in_bc_expr(t))
4609 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004610
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004611 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004612 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004613 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004615 case BC_LEX_OP_INC:
4616 case BC_LEX_OP_DEC:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004617 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004618 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004619 rprn = bin_last = false;
4620 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004622 case XC_LEX_OP_MINUS:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004623 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004624 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004625 rprn = false;
4626 //get_token = false; - already is
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004627 bin_last = (prev == XC_INST_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004629 case BC_LEX_OP_ASSIGN_POWER:
4630 case BC_LEX_OP_ASSIGN_MULTIPLY:
4631 case BC_LEX_OP_ASSIGN_DIVIDE:
4632 case BC_LEX_OP_ASSIGN_MODULUS:
4633 case BC_LEX_OP_ASSIGN_PLUS:
4634 case BC_LEX_OP_ASSIGN_MINUS:
4635 case BC_LEX_OP_ASSIGN:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004636 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004637 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4638 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4639 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004640 ) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004641 return bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004642 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004643 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004644 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004645 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004646 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004647 case XC_LEX_OP_POWER:
4648 case XC_LEX_OP_MULTIPLY:
4649 case XC_LEX_OP_DIVIDE:
4650 case XC_LEX_OP_MODULUS:
4651 case XC_LEX_OP_PLUS:
4652 case XC_LEX_OP_REL_EQ:
4653 case XC_LEX_OP_REL_LE:
4654 case XC_LEX_OP_REL_GE:
4655 case XC_LEX_OP_REL_NE:
4656 case XC_LEX_OP_REL_LT:
4657 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004658 case BC_LEX_OP_BOOL_NOT:
4659 case BC_LEX_OP_BOOL_OR:
4660 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004661 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004662 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004663 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004664 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004665 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004667 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004668 prev = BC_TOKEN_2_INST(t);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004669 bc_parse_operator(t, ops_bgn, &nexprs);
4670 s = zbc_lex_next();
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004671 rprn = false;
4672 //get_token = false; - already is
Denys Vlasenko69560f42018-12-24 14:14:23 +01004673 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004674 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004675 case BC_LEX_LPAREN:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004676 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004677 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004678 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004679 bc_vec_push(&p->ops, &t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004680 nparens++;
4681 get_token = true;
4682 paren_expr = false;
4683 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004684 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004685 case BC_LEX_RPAREN:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004686 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004687 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004688 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004689 if (nparens == 0) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004690 goto exit_loop;
Gavin Howard01055ba2018-11-03 11:00:21 -06004691 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004692 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004693 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004694 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004695 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004696 s = zbc_parse_rightParen(ops_bgn, &nexprs);
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004697 nparens--;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004698 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004699 paren_expr = rprn = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004700 bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004701 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004702 case XC_LEX_NAME:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004703 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004704 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004705 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004706 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004707 paren_expr = true;
4708 rprn = bin_last = false;
4709 //get_token = false; - already is
Denys Vlasenkobb116032018-12-25 01:16:37 +01004710 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004711 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004712 case XC_LEX_NUMBER:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004713 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004714 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004715 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004716 bc_parse_pushNUM();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004717 prev = XC_INST_NUM;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004718 get_token = true;
4719 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004721 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004722 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004723 case BC_LEX_KEY_IBASE:
4724 case BC_LEX_KEY_LAST:
4725 case BC_LEX_KEY_OBASE:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004726 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004727 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004728 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004729 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004730 bc_parse_push((char) prev);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004731 get_token = true;
4732 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004733 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004734 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004735 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 case BC_LEX_KEY_LENGTH:
4737 case BC_LEX_KEY_SQRT:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004738 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004739 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004740 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004741 s = zbc_parse_builtin(t, flags, &prev);
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004742 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004743 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004744 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004745 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004746 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004747 case BC_LEX_KEY_READ:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004748 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004749 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004750 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004751 s = zbc_parse_read();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004752 prev = XC_INST_READ;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004753 get_token = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004754 paren_expr = true;
4755 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004756 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004757 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004758 case BC_LEX_KEY_SCALE:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004759 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004760 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004761 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004762 s = zbc_parse_scale(&prev, flags);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004763 prev = XC_INST_SCALE;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004764 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004765 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004766 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004767 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004768 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004769 default:
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004770 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004771 }
4772
Denys Vlasenkobb116032018-12-25 01:16:37 +01004773 if (s || G_interrupt) // error, or ^C: stop parsing
4774 return BC_STATUS_FAILURE;
4775 if (get_token) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004776 s = zbc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004777 if (s) return s;
4778 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004780 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004781
4782 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004783 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004784 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004785
4786 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004787 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004788
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004789 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004790
Denys Vlasenko69560f42018-12-24 14:14:23 +01004791 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004792 bc_vec_pop(&p->ops);
4793 }
4794
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004795 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004796 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004797
Gavin Howard01055ba2018-11-03 11:00:21 -06004798 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004799 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004800 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4801 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004802 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004803 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004804 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4805 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004806 }
4807
4808 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004809 if (paren_first || !assign)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004810 bc_parse_push(XC_INST_PRINT);
4811 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004812 }
4813
Denys Vlasenko17df8822018-12-14 23:00:24 +01004814 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004815 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004816}
4817
Gavin Howard01055ba2018-11-03 11:00:21 -06004818#endif // ENABLE_BC
4819
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004820#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004821
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004822static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004823{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004824 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004825 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004826
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004827 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004828 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004829 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004830
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004831 bc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004832
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004833 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004834}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004835#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004836
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004837static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004838{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004839 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004840 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004841 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004842
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004843 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004844
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004845 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004846 bc_parse_push(XC_INST_STR);
4847 bc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004848 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004849
4850 // Explanation needed here
4851 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004852 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004853
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004854 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004855}
4856
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004857static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004858{
4859 BcStatus s;
4860
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004861 bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004862 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004863 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004864 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004865 }
4866
4867 if (store) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004868 bc_parse_push(DC_INST_SWAP);
4869 bc_parse_push(XC_INST_ASSIGN);
4870 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004871 }
4872
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004873 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004874}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004875#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004876
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004877static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004878{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004879 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004880 BcStatus s;
4881
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004882 bc_parse_push(inst);
4883 bc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004884
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004885 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004886 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004887
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004888 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004889 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004890
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004891 // Note that 'else' part can not be on the next line:
4892 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4893 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004894 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004895 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004896 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004897 s = zbc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004898 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004899 bc_parse_push(BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004900 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004901
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004902 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004903}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004904#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004905
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004906static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004907{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004908 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004909 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004910 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004911
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004912 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004913 s = BC_STATUS_SUCCESS;
4914 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004915 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004916 case XC_LEX_OP_REL_EQ:
4917 case XC_LEX_OP_REL_LE:
4918 case XC_LEX_OP_REL_GE:
4919 case XC_LEX_OP_REL_NE:
4920 case XC_LEX_OP_REL_LT:
4921 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004922 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004923 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004924 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004925 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004926 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004927 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004928 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004929 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004931 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004932 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004933 dc_parse_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06004934 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004935 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004936 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004937 s = zbc_lex_next();
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004938 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004939 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004940 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004941 bc_parse_pushNUM();
4942 bc_parse_push(XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004943 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004944 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004945 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004946 bc_parse_pushNUM();
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004948 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004949 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004950 bc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004951 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004952 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004953 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004954 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004955 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004956 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004957 s = zdc_parse_mem(inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004958 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004959 case DC_LEX_LOAD:
4960 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004961 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004962 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004963 s = zdc_parse_mem(inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004964 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004965 case DC_LEX_STORE_IBASE:
4966 case DC_LEX_STORE_SCALE:
4967 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004968 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004969 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004970 s = zdc_parse_mem(inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004971 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004972 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004973 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4974 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004975 }
4976
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004977 if (!s && get_token) s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004978
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004979 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004980 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004981}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004982#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004983
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004984static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004985{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004986 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004987 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004988
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004989 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004990 if (i >= 0) {
4991 BcInst inst = dc_LEX_to_INST[i];
4992 if (inst != DC_INST_INVALID) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004993 bc_parse_push(inst);
4994 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004995 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004996 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004997 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004998}
4999#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5000
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005001static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005002{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005003 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005004 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5005 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005006 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005007 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005008 }
5009
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005010 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005011 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005012}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005013#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005014
Gavin Howard01055ba2018-11-03 11:00:21 -06005015#endif // ENABLE_DC
5016
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005017//
5018// Execution engine
5019//
5020
5021#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5022#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5023
Denys Vlasenkodf515392018-12-02 19:27:48 +01005024static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005025{
Gavin Howard01055ba2018-11-03 11:00:21 -06005026 BcId e, *ptr;
5027 BcVec *v, *map;
5028 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005029 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005030
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005031 v = var ? &G.prog.vars : &G.prog.arrs;
5032 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005033
5034 e.name = id;
5035 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005036 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005037
5038 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005039 BcVec v2;
5040 bc_array_init(&v2, var);
5041 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005042 }
5043
5044 ptr = bc_vec_item(map, i);
5045 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005046 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005047}
5048
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005049// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005050static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005051{
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005053 case XC_RESULT_STR:
5054 case XC_RESULT_TEMP:
5055 case XC_RESULT_IBASE:
5056 case XC_RESULT_SCALE:
5057 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005058 *num = &r->d.n;
5059 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005060 case XC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005061 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005062 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005063 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005064 size_t len;
5065
5066 str = *bc_program_const(r->d.id.idx);
5067 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005068
5069 bc_num_init(&r->d.n, len);
5070
5071 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005072 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005073 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005074 if (s) {
5075 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005076 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005077 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 *num = &r->d.n;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005079 r->t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005080 break;
5081 }
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005082 case XC_RESULT_VAR:
5083 case XC_RESULT_ARRAY:
5084 case XC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005085 BcVec *v;
5086
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005087 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005088
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005089 if (r->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005090 v = bc_vec_top(v);
5091 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5092 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005093 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005094 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005095 break;
5096 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005097#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005098 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005099 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005100 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005101 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005102 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005103 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005104#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005105#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005106 default:
5107 // Testing the theory that dc does not reach LAST/ONE
5108 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005109#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005110 }
5111
Denys Vlasenko29301232018-12-11 15:29:32 +01005112 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005113}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005114#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005115
Denys Vlasenko29301232018-12-11 15:29:32 +01005116static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005117 BcResult **r, BcNum **rn, bool assign)
5118{
5119 BcStatus s;
5120 bool hex;
5121 BcResultType lt, rt;
5122
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005123 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005124 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005125
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005126 *r = bc_vec_item_rev(&G.prog.results, 0);
5127 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005128
5129 lt = (*l)->t;
5130 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005131 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005132
Denys Vlasenko29301232018-12-11 15:29:32 +01005133 s = zbc_program_num(*l, ln, false);
5134 if (s) RETURN_STATUS(s);
5135 s = zbc_program_num(*r, rn, hex);
5136 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005137
5138 // We run this again under these conditions in case any vector has been
5139 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005140 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005141 s = zbc_program_num(*l, ln, false);
5142 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005143 }
5144
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005145 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005146 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005147 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005148 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005149
Denys Vlasenko29301232018-12-11 15:29:32 +01005150 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005151}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005152#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005153
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005154static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005155{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005156 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005157 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005158 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005159}
5160
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005161// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005162static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005163{
5164 BcStatus s;
5165
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005166 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005167 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005168 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005169
Denys Vlasenko29301232018-12-11 15:29:32 +01005170 s = zbc_program_num(*r, n, false);
5171 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005172
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005173 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005174 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005175
Denys Vlasenko29301232018-12-11 15:29:32 +01005176 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005177}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005178#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005179
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005180static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005181{
5182 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005183 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005184}
5185
Denys Vlasenko259137d2018-12-11 19:42:05 +01005186static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005187{
5188 BcStatus s;
5189 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005190 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005191
Denys Vlasenko29301232018-12-11 15:29:32 +01005192 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005193 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005194 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005195
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005196 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005197 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 -06005198 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005199 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005200
Denys Vlasenko259137d2018-12-11 19:42:05 +01005201 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005202 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005203 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005204 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005205}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005206#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005207
Denys Vlasenko26819db2018-12-12 16:08:46 +01005208static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005209{
5210 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005211 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005212 BcVec buf;
5213 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005214 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005215
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005216 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005217 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005218
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005219 f = bc_program_func(BC_PROG_READ);
5220 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005221
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005222 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005223 bc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005224 //G.err_line = G.prs.lex_line = 1; - not needed, error line info is not printed for read()
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005225
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005226 s = zbc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005227 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005228 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005229 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005230 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005231 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005232 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005233 if (s) goto exec_err;
5234
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005235 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005236 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005237 goto exec_err;
5238 }
5239
5240 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005241 ip.inst_idx = 0;
5242 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005243
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005244 bc_parse_push(XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005245 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005246
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005247 exec_err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005248 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005249 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005250 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005251 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005252}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005253#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005254
5255static size_t bc_program_index(char *code, size_t *bgn)
5256{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005257 unsigned char *bytes = (void*)(code + *bgn);
5258 unsigned amt;
5259 unsigned i;
5260 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005261
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005262 amt = *bytes++;
5263 *bgn += amt + 1;
5264
5265 amt *= 8;
5266 res = 0;
5267 for (i = 0; i < amt; i += 8)
5268 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005269
5270 return res;
5271}
5272
5273static char *bc_program_name(char *code, size_t *bgn)
5274{
5275 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005276 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005277
Denys Vlasenko694d2982018-12-18 19:17:11 +01005278 code += *bgn;
5279 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005280 i = 0;
5281 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005282 char c = *code++;
5283 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005284 break;
5285 s[i++] = c;
5286 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005287 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005288 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005289
5290 return s;
5291}
5292
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005293static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005294{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005295#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005296 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005297 // Example: echo '[]ap' | dc
5298 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005299 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005300 return;
5301 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005302#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005303 while (*str) {
5304 int c = *str++;
5305 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005306 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005307 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005308 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005309 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005310 case 'a':
5311 bb_putchar('\a');
5312 break;
5313 case 'b':
5314 bb_putchar('\b');
5315 break;
5316 case '\\':
5317 case 'e':
5318 bb_putchar('\\');
5319 break;
5320 case 'f':
5321 bb_putchar('\f');
5322 break;
5323 case 'n':
5324 bb_putchar('\n');
5325 G.prog.nchars = SIZE_MAX;
5326 break;
5327 case 'r':
5328 bb_putchar('\r');
5329 break;
5330 case 'q':
5331 bb_putchar('"');
5332 break;
5333 case 't':
5334 bb_putchar('\t');
5335 break;
5336 default:
5337 // Just print the backslash and following character.
5338 bb_putchar('\\');
5339 ++G.prog.nchars;
5340 bb_putchar(c);
5341 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005342 }
5343 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005344 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005345 }
5346}
5347
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005348static void bc_num_printNewline(void)
5349{
5350 if (G.prog.nchars == G.prog.len - 1) {
5351 bb_putchar('\\');
5352 bb_putchar('\n');
5353 G.prog.nchars = 0;
5354 }
5355}
5356
5357#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005358static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005359{
5360 (void) radix;
5361 bb_putchar((char) num);
5362 G.prog.nchars += width;
5363}
5364#endif
5365
5366static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5367{
5368 size_t exp, pow;
5369
5370 bc_num_printNewline();
5371 bb_putchar(radix ? '.' : ' ');
5372 ++G.prog.nchars;
5373
5374 bc_num_printNewline();
5375 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5376 continue;
5377
5378 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5379 size_t dig;
5380 bc_num_printNewline();
5381 dig = num / pow;
5382 num -= dig * pow;
5383 bb_putchar(((char) dig) + '0');
5384 }
5385}
5386
5387static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5388{
5389 if (radix) {
5390 bc_num_printNewline();
5391 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005392 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005393 }
5394
5395 bc_num_printNewline();
5396 bb_putchar(bb_hexdigits_upcase[num]);
5397 G.prog.nchars += width;
5398}
5399
5400static void bc_num_printDecimal(BcNum *n)
5401{
5402 size_t i, rdx = n->rdx - 1;
5403
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005404 if (n->neg) {
5405 bb_putchar('-');
5406 G.prog.nchars++;
5407 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005408
5409 for (i = n->len - 1; i < n->len; --i)
5410 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5411}
5412
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005413typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5414
Denys Vlasenkod3401432018-12-18 17:00:35 +01005415static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005416{
5417 BcStatus s;
5418 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005419 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005420 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005421 BcNum intp, fracp, digit, frac_len;
5422 unsigned long dig, *ptr;
5423 size_t i;
5424 bool radix;
5425
5426 if (n->len == 0) {
5427 print(0, width, false);
5428 RETURN_STATUS(BC_STATUS_SUCCESS);
5429 }
5430
5431 bc_vec_init(&stack, sizeof(long), NULL);
5432 bc_num_init(&intp, n->len);
5433 bc_num_init(&fracp, n->rdx);
5434 bc_num_init(&digit, width);
5435 bc_num_init(&frac_len, BC_NUM_INT(n));
5436 bc_num_copy(&intp, n);
5437 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005438 base.cap = ARRAY_SIZE(base_digs);
5439 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005440 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005441
5442 bc_num_truncate(&intp, intp.rdx);
5443 s = zbc_num_sub(n, &intp, &fracp, 0);
5444 if (s) goto err;
5445
5446 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005447 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005448 if (s) goto err;
5449 s = zbc_num_ulong(&digit, &dig);
5450 if (s) goto err;
5451 bc_vec_push(&stack, &dig);
5452 }
5453
5454 for (i = 0; i < stack.len; ++i) {
5455 ptr = bc_vec_item_rev(&stack, i);
5456 print(*ptr, width, false);
5457 }
5458
5459 if (!n->rdx) goto err;
5460
5461 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005462 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005463 if (s) goto err;
5464 s = zbc_num_ulong(&fracp, &dig);
5465 if (s) goto err;
5466 bc_num_ulong2num(&intp, dig);
5467 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5468 if (s) goto err;
5469 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005470 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005471 if (s) goto err;
5472 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005473 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005474 bc_num_free(&frac_len);
5475 bc_num_free(&digit);
5476 bc_num_free(&fracp);
5477 bc_num_free(&intp);
5478 bc_vec_free(&stack);
5479 RETURN_STATUS(s);
5480}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005481#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005482
5483static BC_STATUS zbc_num_printBase(BcNum *n)
5484{
5485 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005486 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005487 BcNumDigitOp print;
5488 bool neg = n->neg;
5489
5490 if (neg) {
5491 bb_putchar('-');
5492 G.prog.nchars++;
5493 }
5494
5495 n->neg = false;
5496
5497 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5498 width = 1;
5499 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005500 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005501 unsigned i = G.prog.ob_t - 1;
5502 width = 0;
5503 for (;;) {
5504 width++;
5505 i /= 10;
5506 if (i == 0)
5507 break;
5508 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005509 print = bc_num_printDigits;
5510 }
5511
Denys Vlasenkod3401432018-12-18 17:00:35 +01005512 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005513 n->neg = neg;
5514
5515 RETURN_STATUS(s);
5516}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005517#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005518
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005519static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5520{
5521 BcStatus s = BC_STATUS_SUCCESS;
5522
5523 bc_num_printNewline();
5524
5525 if (n->len == 0) {
5526 bb_putchar('0');
5527 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005528 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005529 bc_num_printDecimal(n);
5530 else
5531 s = zbc_num_printBase(n);
5532
5533 if (newline) {
5534 bb_putchar('\n');
5535 G.prog.nchars = 0;
5536 }
5537
5538 RETURN_STATUS(s);
5539}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005540#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005541
Denys Vlasenko29301232018-12-11 15:29:32 +01005542static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005543{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005544 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005545 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005546 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005547 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005548
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005549 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005550 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005551
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005552 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005553 s = zbc_program_num(r, &num, false);
5554 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005555
5556 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005557 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005558#if ENABLE_BC
5559 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5560#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005561 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005562 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005563
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005564 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005565 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005566
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005567 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005568 for (;;) {
5569 char c = *str++;
5570 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005571 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005572 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005573 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005574 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005575 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005576 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005577 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005578 }
5579 }
5580
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005581 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005582
Denys Vlasenko29301232018-12-11 15:29:32 +01005583 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005584}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005585#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005586
Denys Vlasenko29301232018-12-11 15:29:32 +01005587static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005588{
5589 BcStatus s;
5590 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005591 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005592
Denys Vlasenko29301232018-12-11 15:29:32 +01005593 s = zbc_program_prep(&ptr, &num);
5594 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005595
5596 bc_num_init(&res.d.n, num->len);
5597 bc_num_copy(&res.d.n, num);
5598 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5599
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005600 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005601
Denys Vlasenko29301232018-12-11 15:29:32 +01005602 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005603}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005604#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005605
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005606static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005607{
5608 BcStatus s;
5609 BcResult *opd1, *opd2, res;
5610 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005611 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005612
Denys Vlasenko29301232018-12-11 15:29:32 +01005613 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005614 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005615
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005616 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005617
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005618 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005619 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005620 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005621 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005622 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005623 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005624 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005625 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005626 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005627 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005628 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005629 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005630 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005631 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005632 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005633 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005634 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005635 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005636 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005637 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005638 cond = (cond > 0);
5639 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005640 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005641 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005642 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005643 }
5644 }
5645
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005646 if (cond) bc_num_one(&res.d.n);
5647 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005648
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005649 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005650
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005651 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005652}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005653#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005654
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005655#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005656static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005657{
5658 BcNum n2;
5659 BcResult res;
5660
5661 memset(&n2, 0, sizeof(BcNum));
5662 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005663 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005664
5665 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005666 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005667 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005668 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005669 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005670 }
5671
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005672 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005673 bc_vec_push(v, &n2);
5674
Denys Vlasenko29301232018-12-11 15:29:32 +01005675 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005676}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005677#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005678#endif // ENABLE_DC
5679
Denys Vlasenko29301232018-12-11 15:29:32 +01005680static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005681{
5682 BcStatus s;
5683 BcResult *ptr, r;
5684 BcVec *v;
5685 BcNum *n;
5686
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005687 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005688 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005689
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005690 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005691 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005692 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005693 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005694
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005695#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005696 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005697 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005698 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005699 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005700#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005701
Denys Vlasenko29301232018-12-11 15:29:32 +01005702 s = zbc_program_num(ptr, &n, false);
5703 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005704
5705 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005706 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005707
5708 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005709 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005710 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005711 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005712 bc_array_init(&r.d.v, true);
5713 bc_array_copy(&r.d.v, (BcVec *) n);
5714 }
5715
5716 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005717 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005718
Denys Vlasenko29301232018-12-11 15:29:32 +01005719 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005720}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005721#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005722
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005723static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005724{
5725 BcStatus s;
5726 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005727 BcNum *l, *r;
5728 bool assign = (inst == XC_INST_ASSIGN);
5729 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005730
Denys Vlasenko29301232018-12-11 15:29:32 +01005731 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005732 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005733
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005734 ib = left->t == XC_RESULT_IBASE;
5735 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005736
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005737#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005738 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005739 BcVec *v;
5740
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005741 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005742 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005743 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005744
Denys Vlasenkofa210792018-12-19 19:35:40 +01005745 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005746 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005747#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005748
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005749 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005750 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005751 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005752 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005753 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005754
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005755#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005756 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005757 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005758
5759 if (assign)
5760 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005761 else {
5762 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005763 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 +01005764 }
5765 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005766#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005767 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005768#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005769
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005770 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005771 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005772 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5773 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5774 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005775 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005776 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005777 size_t max;
5778 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005779
Denys Vlasenko29301232018-12-11 15:29:32 +01005780 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005781 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005782 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005783 if (sc) {
5784 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005785 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005786 } else {
5787 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005788 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005789 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005790 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005791 }
5792
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005793 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005794 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005795
5796 *ptr = (size_t) val;
5797 s = BC_STATUS_SUCCESS;
5798 }
5799
5800 bc_num_init(&res.d.n, l->len);
5801 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005802 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005803
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005804 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005805}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005806#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005807
Denys Vlasenko416ce762018-12-02 20:57:17 +01005808#if !ENABLE_DC
5809#define bc_program_pushVar(code, bgn, pop, copy) \
5810 bc_program_pushVar(code, bgn)
5811// for bc, 'pop' and 'copy' are always false
5812#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005813static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005814 bool pop, bool copy)
5815{
Gavin Howard01055ba2018-11-03 11:00:21 -06005816 BcResult r;
5817 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005818
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005819 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005820 r.d.id.name = name;
5821
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005822#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005823 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005824 BcVec *v = bc_program_search(name, true);
5825 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005826
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005827 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005828 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005829 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005830 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005831
5832 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005833 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005834 bc_num_init_DEF_SIZE(&r.d.n);
5835 bc_num_copy(&r.d.n, num);
5836 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005837 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005838 r.d.id.idx = num->rdx;
5839 }
5840
5841 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005842 }
5843#endif // ENABLE_DC
5844
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005845 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005846
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005847 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005849#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005850
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005851static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005852{
5853 BcStatus s = BC_STATUS_SUCCESS;
5854 BcResult r;
5855 BcNum *num;
5856
5857 r.d.id.name = bc_program_name(code, bgn);
5858
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005859 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005860 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005861 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005862 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005863 BcResult *operand;
5864 unsigned long temp;
5865
Denys Vlasenko29301232018-12-11 15:29:32 +01005866 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005867 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005868 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005869 if (s) goto err;
5870
5871 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005872 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005873 goto err;
5874 }
5875
5876 r.d.id.idx = (size_t) temp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005877 bc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005878 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005879 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005880 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005881 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005882}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005883#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005884
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005885#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005886static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005887{
5888 BcStatus s;
5889 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005890 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005891 char inst2 = inst;
5892
Denys Vlasenko29301232018-12-11 15:29:32 +01005893 s = zbc_program_prep(&ptr, &num);
5894 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005895
5896 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005897 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005898 bc_num_init(&copy.d.n, num->len);
5899 bc_num_copy(&copy.d.n, num);
5900 }
5901
5902 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005903 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5904 ? BC_INST_ASSIGN_PLUS
5905 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005906
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005907 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005908 s = zbc_program_assign(inst);
5909 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005910
5911 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005912 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005913 }
5914
Denys Vlasenko29301232018-12-11 15:29:32 +01005915 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005916}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005917#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005918
Denys Vlasenko29301232018-12-11 15:29:32 +01005919static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005920{
Gavin Howard01055ba2018-11-03 11:00:21 -06005921 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005922 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005923 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 BcResult *arg;
5926
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005927 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005928 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005929 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005930 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005931
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005932 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005933 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005934 }
5935 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005936 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005937 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005938 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005939
5940 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005941 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005942
5943 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005944 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005945
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005946 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005947 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005948
Denys Vlasenko29301232018-12-11 15:29:32 +01005949 s = zbc_program_copyToVar(a->name, a->idx);
5950 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005951 }
5952
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005953 a = bc_vec_item(&func->autos, i);
5954 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005955 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005956
Denys Vlasenkodf515392018-12-02 19:27:48 +01005957 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005958 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005959 BcNum n2;
5960 bc_num_init_DEF_SIZE(&n2);
5961 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005962 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005963 BcVec v2;
5964 bc_array_init(&v2, true);
5965 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005966 }
5967 }
5968
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005969 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
Denys Vlasenko29301232018-12-11 15:29:32 +01005971 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005972}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005973#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005974
Denys Vlasenko29301232018-12-11 15:29:32 +01005975static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005976{
Gavin Howard01055ba2018-11-03 11:00:21 -06005977 BcResult res;
5978 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005979 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005980 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005981 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005983 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 +01005984 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005985
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005986 f = bc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005987 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005988
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005989 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005990 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005991 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005992 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005993
Denys Vlasenko29301232018-12-11 15:29:32 +01005994 s = zbc_program_num(operand, &num, false);
5995 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005996 bc_num_init(&res.d.n, num->len);
5997 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005998 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005999 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006000 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006001 }
6002
6003 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006004 a = (void*)f->autos.v;
6005 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006006 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006007 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006008 bc_vec_pop(v);
6009 }
6010
Denys Vlasenko24e41942018-12-21 23:01:26 +01006011 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006012 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006013 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006014
Denys Vlasenko29301232018-12-11 15:29:32 +01006015 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006016}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006017#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006018#endif // ENABLE_BC
6019
6020static unsigned long bc_program_scale(BcNum *n)
6021{
6022 return (unsigned long) n->rdx;
6023}
6024
6025static unsigned long bc_program_len(BcNum *n)
6026{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006027 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006028
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006029 if (n->rdx != len) return len;
6030 for (;;) {
6031 if (len == 0) break;
6032 len--;
6033 if (n->num[len] != 0) break;
6034 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006035 return len;
6036}
6037
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006038static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006039{
6040 BcStatus s;
6041 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006042 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006043 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006044 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006045
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006046 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006047 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006048 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006049
Denys Vlasenko29301232018-12-11 15:29:32 +01006050 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006051 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006052
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006053#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006054 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006055 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006056#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006057
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006058 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006059
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006060 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006061 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006062#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006063 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006064 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006065 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006066#endif
6067#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006068 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006069 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006070 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006072 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006073 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006074 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006075#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006076 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006077 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006078 }
6079
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006080 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006081
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006082 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006083}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006084#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006085
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006086#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006087static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006088{
6089 BcStatus s;
6090 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006091 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006092
Denys Vlasenko29301232018-12-11 15:29:32 +01006093 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006094 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006095
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006096 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 bc_num_init(&res2.d.n, n2->len);
6098
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006099 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006100 if (s) goto err;
6101
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006102 bc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006103 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006104 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006106 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006107 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006108 bc_num_free(&res2.d.n);
6109 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006110 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006111}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006112#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
Denys Vlasenkofa210792018-12-19 19:35:40 +01006114static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006115{
6116 BcStatus s;
6117 BcResult *r1, *r2, *r3, res;
6118 BcNum *n1, *n2, *n3;
6119
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006120 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006121 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006122 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006123 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006124
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006125 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006126 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006127 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006128 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006129 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006130
6131 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006132 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006133 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006134 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006135 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006136 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006137 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006138 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006139 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006140 }
6141 }
6142
6143 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006144 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006145 if (s) goto err;
6146
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006147 bc_vec_pop(&G.prog.results);
6148 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006149
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006150 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006151 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006152 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006153 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006154}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006155#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006156
Denys Vlasenkofa210792018-12-19 19:35:40 +01006157static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006158{
Gavin Howard01055ba2018-11-03 11:00:21 -06006159 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006160 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006161
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006162 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006163
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006164 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006165 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006166 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006167}
6168
Denys Vlasenkofa210792018-12-19 19:35:40 +01006169static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006170{
6171 BcStatus s;
6172 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006173 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006174 char **strs;
6175 char *str;
6176 char c;
6177 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006178
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006179 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006180 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006181 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006182
Denys Vlasenko29301232018-12-11 15:29:32 +01006183 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006184 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006185
6186 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006187 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006188 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006189 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006190
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006191 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006192 bc_num_copy(&n, num);
6193 bc_num_truncate(&n, n.rdx);
6194
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006195 strmb.cap = ARRAY_SIZE(strmb_digs);
6196 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006197 bc_num_ulong2num(&strmb, 0x100);
6198 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006199
Gavin Howard01055ba2018-11-03 11:00:21 -06006200 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006201 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202 if (s) goto num_err;
6203
6204 c = (char) val;
6205
6206 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006207 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006208 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006209 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006210 sp = *bc_program_str(idx);
6211 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006212 }
6213
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006214 strs = (void*)G.prog.strs.v;
6215 for (idx = 0; idx < G.prog.strs.len; idx++) {
6216 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6217 goto dup;
6218 }
6219 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006220 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006221 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006222 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006223 bc_vec_push(&G.prog.strs, &str);
6224 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006225 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006226 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006227 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006228
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006229 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006230 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006231 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006232 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006233}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006234#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006235
Denys Vlasenkofa210792018-12-19 19:35:40 +01006236static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006237{
6238 BcStatus s;
6239 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006240 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006241 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006242
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006243 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006244 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006245 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006246
Denys Vlasenko29301232018-12-11 15:29:32 +01006247 s = zbc_program_num(r, &n, false);
6248 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006249
Denys Vlasenko57734c92018-12-17 21:14:05 +01006250 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006251 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006252 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006253 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006254 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006255 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006256 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006257 }
6258
Denys Vlasenko29301232018-12-11 15:29:32 +01006259 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006260}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006261#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006262
Denys Vlasenkofa210792018-12-19 19:35:40 +01006263static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006264{
6265 BcStatus s;
6266 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006267 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006268 unsigned long val;
6269
Denys Vlasenko29301232018-12-11 15:29:32 +01006270 s = zbc_program_prep(&opnd, &num);
6271 if (s) RETURN_STATUS(s);
6272 s = zbc_num_ulong(num, &val);
6273 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006274
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006275 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006276
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006277 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006278 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006279 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006280 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006281 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006282
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006283 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284
Denys Vlasenko29301232018-12-11 15:29:32 +01006285 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006286}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006287#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006288
Denys Vlasenkofa210792018-12-19 19:35:40 +01006289static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006290{
6291 BcStatus s = BC_STATUS_SUCCESS;
6292 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006293 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006294 BcInstPtr ip;
6295 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006296
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006297 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006298 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006299
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006300 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006301
6302 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006303 BcNum *n = n; // for compiler
6304 bool exec;
6305 char *name;
6306 char *then_name = bc_program_name(code, bgn);
6307 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006308
6309 if (code[*bgn] == BC_PARSE_STREND)
6310 (*bgn) += 1;
6311 else
6312 else_name = bc_program_name(code, bgn);
6313
6314 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006315 name = then_name;
6316 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006317 exec = true;
6318 name = else_name;
6319 }
6320
6321 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006322 BcVec *v;
6323 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324 n = bc_vec_top(v);
6325 }
6326
6327 free(then_name);
6328 free(else_name);
6329
6330 if (!exec) goto exit;
6331 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006332 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006333 goto exit;
6334 }
6335
6336 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006337 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006338 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006339 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006340 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006341 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006342 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006343 if (s || !BC_PROG_STR(n)) goto exit;
6344 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006345 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006346 goto exit;
6347 }
6348
6349 fidx = sidx + BC_PROG_REQ_FUNCS;
6350
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006351 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006352
6353 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006354 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006355 char *str;
6356
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006357 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006358 bc_parse_create(fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006359 str = *bc_program_str(sidx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006360 s = zbc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006361 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006362
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006363 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006364 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006365 bc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006366 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006367 s = bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006368 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006369 G.prs = sv_parse; // struct copy
6370 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006371 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006372 bc_vec_pop_all(&f->code);
6373 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006374 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006375 }
6376
Denys Vlasenko24e41942018-12-21 23:01:26 +01006377 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006378 ip.func = fidx;
6379
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006380 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006381 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006382
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006383 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006384 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006385 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006386 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006387}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006388#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006389#endif // ENABLE_DC
6390
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006391static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006392{
Gavin Howard01055ba2018-11-03 11:00:21 -06006393 BcResult res;
6394 unsigned long val;
6395
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006396 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006397 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006398 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006399 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006400 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006401 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006402 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006403
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006404 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006405 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006406 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006407}
6408
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006409static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006410{
Gavin Howard01055ba2018-11-03 11:00:21 -06006411 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006412 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006413 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006414 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006415
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006416 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006417 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6418 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006419 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006420 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006421
Denys Vlasenko24e41942018-12-21 23:01:26 +01006422 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 -06006423 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006424#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006425 case BC_INST_JUMP_ZERO: {
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006426 BcNum *num;
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006427 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006428 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006429 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006430 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006431 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006432 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006433 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006434 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006435 break;
6436 }
6437 // else: fall through
6438 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006439 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006440 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006441 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006442 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006443 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006444 break;
6445 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006446 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006447 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006448 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006449 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006450 case BC_INST_INC_PRE:
6451 case BC_INST_DEC_PRE:
6452 case BC_INST_INC_POST:
6453 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006454 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006455 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006456 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006457 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006458 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006459 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006460 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006461 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006462 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006463 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006464 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006465 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006466 case XC_INST_BOOL_OR:
6467 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006468#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006469 case XC_INST_REL_EQ:
6470 case XC_INST_REL_LE:
6471 case XC_INST_REL_GE:
6472 case XC_INST_REL_NE:
6473 case XC_INST_REL_LT:
6474 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006475 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006476 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006478 case XC_INST_READ:
6479 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006480 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006481 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006482 case XC_INST_VAR:
6483 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006484 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006485 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006486 case XC_INST_ARRAY_ELEM:
6487 case XC_INST_ARRAY:
6488 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006489 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006490 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006491#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006492 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006493 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006494 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006495 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006496 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006497#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006498 case XC_INST_IBASE:
6499 case XC_INST_OBASE:
6500 case XC_INST_SCALE:
6501 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006502 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006504 case XC_INST_SCALE_FUNC:
6505 case XC_INST_LENGTH:
6506 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006507 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006508 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006509 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006510 case XC_INST_NUM:
6511 dbg_exec("XC_INST_NUM:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006512 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006513 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006514 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006516 case XC_INST_POP:
6517 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006518 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006519 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006520 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006521 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006522 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006523 case XC_INST_PRINT:
6524 case XC_INST_PRINT_POP:
6525 case XC_INST_PRINT_STR:
6526 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006527 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006528 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006529 case XC_INST_STR:
6530 dbg_exec("XC_INST_STR:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006531 r.t = XC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006532 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006533 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006534 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006535 case XC_INST_POWER:
6536 case XC_INST_MULTIPLY:
6537 case XC_INST_DIVIDE:
6538 case XC_INST_MODULUS:
6539 case XC_INST_PLUS:
6540 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006541 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006542 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006543 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006544 case XC_INST_BOOL_NOT: {
6545 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006546 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006547 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006548 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006549 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006550 if (bc_num_cmp(num, &G.prog.zero) == 0)
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006551 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006552 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006553 bc_program_retire(&r, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006555 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006556 case XC_INST_NEG:
6557 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006558 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006560#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006561 case BC_INST_ASSIGN_POWER:
6562 case BC_INST_ASSIGN_MULTIPLY:
6563 case BC_INST_ASSIGN_DIVIDE:
6564 case BC_INST_ASSIGN_MODULUS:
6565 case BC_INST_ASSIGN_PLUS:
6566 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006567#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006568 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006569 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006570 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006571 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006572#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006573 case DC_INST_POP_EXEC:
6574 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006575 bc_vec_pop(&G.prog.exestack);
6576 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006577 case DC_INST_MODEXP:
6578 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006579 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006580 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006581 case DC_INST_DIVMOD:
6582 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006583 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006584 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006585 case DC_INST_EXECUTE:
6586 case DC_INST_EXEC_COND:
6587 dbg_exec("DC_INST_EXEC[_COND]:");
6588 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006589 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006590 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006591 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006592 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006593 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006594 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006595 if (s) break;
6596 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 break;
6598 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006599 case DC_INST_CLEAR_STACK:
6600 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006601 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006602 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006603 case DC_INST_STACK_LEN:
6604 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006605 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006606 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006607 case DC_INST_DUPLICATE:
6608 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006609 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006610 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006611 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006612 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006613 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006614 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006615 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006616 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006617 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006618 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006619 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006620 ptr = bc_vec_item_rev(&G.prog.results, 0);
6621 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006622 memcpy(&r, ptr, sizeof(BcResult));
6623 memcpy(ptr, ptr2, sizeof(BcResult));
6624 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 break;
6626 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006627 case DC_INST_ASCIIFY:
6628 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006629 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006630 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006631 case DC_INST_PRINT_STREAM:
6632 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006633 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006634 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006635 case DC_INST_LOAD:
6636 case DC_INST_PUSH_VAR: {
6637 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006638 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006639 break;
6640 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006641 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006642 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006643 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006644 free(name);
6645 break;
6646 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006647 case DC_INST_QUIT:
6648 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006649 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006650 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006651 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006652 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006653 case DC_INST_NQUIT:
6654 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006655 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006656 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006657#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006658 read_updated_ip:
6659 // Instruction stack has changed, read new pointers
6660 ip = bc_vec_top(&G.prog.exestack);
6661 func = bc_program_func(ip->func);
6662 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006663 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006664 }
6665
Denys Vlasenkod38af482018-12-04 19:11:02 +01006666 if (s || G_interrupt) {
6667 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006668 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006669 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006670
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006671 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 }
6673
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006674 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006675}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006676#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006677
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006678static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006679{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006680 char *lenv;
6681 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006682
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006683 lenv = getenv(var);
6684 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006685 if (!lenv) return len;
6686
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006687 len = bb_strtou(lenv, NULL, 10) - 1;
6688 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006689 len = BC_NUM_PRINT_WIDTH;
6690
6691 return len;
6692}
6693
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006694static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006695{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006696 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006697
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006698 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006699 s = zbc_parse_text_init(text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006700 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006701
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006702 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006703 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006704 BcInstPtr *ip;
6705 BcFunc *f;
6706
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006707 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006708 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006709#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006710 if (G.prs.lex == BC_LEX_SCOLON
6711 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006712 ) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006713 s = zbc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006714 if (s) goto err;
6715 goto check_eof;
6716 }
6717
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006718 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006719 if (s) goto err;
6720
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006721 // Check that next token is a correct stmt delimiter -
6722 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006723 if (G.prs.lex != BC_LEX_SCOLON
6724 && G.prs.lex != XC_LEX_NLINE
6725 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006726 ) {
6727 const char *err_at;
6728//TODO: commonalize for other parse errors:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006729 err_at = G.prs.lex_next_at ? G.prs.lex_next_at : "UNKNOWN";
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006730 bc_error_fmt("bad statement terminator at '%.*s'",
6731 (int)(strchrnul(err_at, '\n') - err_at),
6732 err_at
6733 );
6734 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006735 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006736 // The above logic is fragile. Check these examples:
6737 // - interative read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006738#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006739 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006740#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006741 // Most of dc parsing assumes all whitespace,
6742 // including '\n', is eaten.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006743 while (G.prs.lex == XC_LEX_NLINE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006744 s = zbc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006745 if (s) goto err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006746 if (G.prs.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006747 goto done;
6748 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006749 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006750#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006751 }
6752 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006753 err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006754 bc_parse_reset(); // includes bc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006755 RETURN_STATUS(BC_STATUS_FAILURE);
6756 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006757
Denys Vlasenko39287e02018-12-22 02:23:08 +01006758 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006759 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006760 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006761 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006762 break;
6763 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006764
6765 ip = (void*)G.prog.exestack.v;
6766#if SANITY_CHECKS
6767 if (G.prog.exestack.len != 1) // should have only main's IP
6768 bb_error_msg_and_die("BUG:call stack");
6769 if (ip->func != BC_PROG_MAIN)
6770 bb_error_msg_and_die("BUG:not MAIN");
6771#endif
6772 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006773 // bc discards strings, constants and code after each
6774 // top-level statement in the "main program".
6775 // This prevents "yes 1 | bc" from growing its memory
6776 // without bound. This can be done because data stack
6777 // is empty and thus can't hold any references to
6778 // strings or constants, there is no generated code
6779 // which can hold references (after we discard one
6780 // we just executed). Code of functions can have references,
6781 // but bc stores function strings/constants in per-function
6782 // storage.
6783 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006784#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006785 if (G.prog.results.len != 0) // should be empty
6786 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006787#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006788 IF_BC(bc_vec_pop_all(&f->strs);)
6789 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006790 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006791 if (G.prog.results.len == 0
6792 && G.prog.vars.len == 0
6793 ) {
6794 // If stack is empty and no registers exist (TODO: or they are all empty),
6795 // we can get rid of accumulated strings and constants.
6796 // In this example dc process should not grow
6797 // its memory consumption with time:
6798 // yes 1pc | dc
6799 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6800 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6801 }
6802 // The code is discarded always (below), thus this example
6803 // should also not grow its memory consumption with time,
6804 // even though its data stack is not empty:
6805 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006806 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006807 // We drop generated and executed code for both bc and dc:
6808 bc_vec_pop_all(&f->code);
6809 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006810 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006811 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006812 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006813 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006814}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006815#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006816
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006817static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006818{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006819 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006820 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006821 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006822 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006823
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006824 G.prs.lex_filename = filename;
6825 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006826 G.err_line = G.prs.lex_line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06006827
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006828 do {
6829 s = zbc_vm_process("");
6830 // We do not stop looping on errors here if reading stdin.
6831 // Example: start interactive bc and enter "return".
6832 // It should say "'return' not in a function"
6833 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006834 } while (G.prs.lex_input_fp == stdin);
6835 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006836 RETURN_STATUS(s);
6837}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006838#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006839
6840static BC_STATUS zbc_vm_file(const char *file)
6841{
6842 BcStatus s;
6843 FILE *fp;
6844
6845 fp = xfopen_for_read(file);
6846 s = zbc_vm_execute_FILE(fp, file);
6847 fclose(fp);
6848
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006849 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006850}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006851#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006852
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006853#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006854static void bc_vm_info(void)
6855{
6856 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006857 "Adapted from https://github.com/gavinhoward/bc\n"
6858 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006859 , applet_name);
6860}
6861
6862static void bc_args(char **argv)
6863{
6864 unsigned opts;
6865 int i;
6866
6867 GETOPT_RESET();
6868#if ENABLE_FEATURE_BC_LONG_OPTIONS
6869 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6870 "warn\0" No_argument "w"
6871 "version\0" No_argument "v"
6872 "standard\0" No_argument "s"
6873 "quiet\0" No_argument "q"
6874 "mathlib\0" No_argument "l"
6875 "interactive\0" No_argument "i"
6876 );
6877#else
6878 opts = option_mask32 |= getopt32(argv, "wvsqli");
6879#endif
6880 if (getenv("POSIXLY_CORRECT"))
6881 option_mask32 |= BC_FLAG_S;
6882
6883 if (opts & BC_FLAG_V) {
6884 bc_vm_info();
6885 exit(0);
6886 }
6887
6888 for (i = optind; argv[i]; ++i)
6889 bc_vec_push(&G.files, argv + i);
6890}
6891
6892static void bc_vm_envArgs(void)
6893{
6894 BcVec v;
6895 char *buf;
6896 char *env_args = getenv("BC_ENV_ARGS");
6897
6898 if (!env_args) return;
6899
6900 G.env_args = xstrdup(env_args);
6901 buf = G.env_args;
6902
6903 bc_vec_init(&v, sizeof(char *), NULL);
6904
6905 while (*(buf = skip_whitespace(buf)) != '\0') {
6906 bc_vec_push(&v, &buf);
6907 buf = skip_non_whitespace(buf);
6908 if (!*buf)
6909 break;
6910 *buf++ = '\0';
6911 }
6912
6913 // NULL terminate, and pass argv[] so that first arg is argv[1]
6914 if (sizeof(int) == sizeof(char*)) {
6915 bc_vec_push(&v, &const_int_0);
6916 } else {
6917 static char *const nullptr = NULL;
6918 bc_vec_push(&v, &nullptr);
6919 }
6920 bc_args(((char **)v.v) - 1);
6921
6922 bc_vec_free(&v);
6923}
6924
6925static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006926 "scale=20"
6927"\n" "define e(x){"
6928"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006929////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6930//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6931//e(-.998896): GNU:.36828580434569428695
6932// above code:.36828580434569428696
6933// actual value:.3682858043456942869594...
6934// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006935"\n" "b=ibase"
6936"\n" "ibase=A"
6937"\n" "if(x<0){"
6938"\n" "n=1"
6939"\n" "x=-x"
6940"\n" "}"
6941"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006942"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006943"\n" "scale=scale(x)+1"
6944"\n" "while(x>1){"
6945"\n" "d+=1"
6946"\n" "x/=2"
6947"\n" "scale+=1"
6948"\n" "}"
6949"\n" "scale=r"
6950"\n" "r=x+1"
6951"\n" "p=x"
6952"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006953"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006954"\n" "p*=x"
6955"\n" "f*=i"
6956"\n" "v=p/f"
6957"\n" "r+=v"
6958"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006959"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006960"\n" "scale=s"
6961"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006962"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006963"\n" "return(r/1)"
6964"\n" "}"
6965"\n" "define l(x){"
6966"\n" "auto b,s,r,p,a,q,i,v"
6967"\n" "b=ibase"
6968"\n" "ibase=A"
6969"\n" "if(x<=0){"
6970"\n" "r=(1-10^scale)/1"
6971"\n" "ibase=b"
6972"\n" "return(r)"
6973"\n" "}"
6974"\n" "s=scale"
6975"\n" "scale+=6"
6976"\n" "p=2"
6977"\n" "while(x>=2){"
6978"\n" "p*=2"
6979"\n" "x=sqrt(x)"
6980"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006981"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006982"\n" "p*=2"
6983"\n" "x=sqrt(x)"
6984"\n" "}"
6985"\n" "r=a=(x-1)/(x+1)"
6986"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006987"\n" "v=1"
6988"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006989"\n" "a*=q"
6990"\n" "v=a/i"
6991"\n" "r+=v"
6992"\n" "}"
6993"\n" "r*=p"
6994"\n" "scale=s"
6995"\n" "ibase=b"
6996"\n" "return(r/1)"
6997"\n" "}"
6998"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006999"\n" "auto b,s,r,a,q,i"
7000"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007001"\n" "b=ibase"
7002"\n" "ibase=A"
7003"\n" "s=scale"
7004"\n" "scale=1.1*s+2"
7005"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007006"\n" "scale=0"
7007"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007008"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007009"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007010"\n" "scale=s+2"
7011"\n" "r=a=x"
7012"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007013"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007014"\n" "a*=q/(i*(i-1))"
7015"\n" "r+=a"
7016"\n" "}"
7017"\n" "scale=s"
7018"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007019"\n" "return(r/1)"
7020"\n" "}"
7021"\n" "define c(x){"
7022"\n" "auto b,s"
7023"\n" "b=ibase"
7024"\n" "ibase=A"
7025"\n" "s=scale"
7026"\n" "scale*=1.2"
7027"\n" "x=s(2*a(1)+x)"
7028"\n" "scale=s"
7029"\n" "ibase=b"
7030"\n" "return(x/1)"
7031"\n" "}"
7032"\n" "define a(x){"
7033"\n" "auto b,s,r,n,a,m,t,f,i,u"
7034"\n" "b=ibase"
7035"\n" "ibase=A"
7036"\n" "n=1"
7037"\n" "if(x<0){"
7038"\n" "n=-1"
7039"\n" "x=-x"
7040"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007041"\n" "if(scale<65){"
7042"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7043"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007044"\n" "}"
7045"\n" "s=scale"
7046"\n" "if(x>.2){"
7047"\n" "scale+=5"
7048"\n" "a=a(.2)"
7049"\n" "}"
7050"\n" "scale=s+3"
7051"\n" "while(x>.2){"
7052"\n" "m+=1"
7053"\n" "x=(x-.2)/(1+.2*x)"
7054"\n" "}"
7055"\n" "r=u=x"
7056"\n" "f=-x*x"
7057"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007058"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007059"\n" "u*=f"
7060"\n" "t=u/i"
7061"\n" "r+=t"
7062"\n" "}"
7063"\n" "scale=s"
7064"\n" "ibase=b"
7065"\n" "return((m*a+r)/n)"
7066"\n" "}"
7067"\n" "define j(n,x){"
7068"\n" "auto b,s,o,a,i,v,f"
7069"\n" "b=ibase"
7070"\n" "ibase=A"
7071"\n" "s=scale"
7072"\n" "scale=0"
7073"\n" "n/=1"
7074"\n" "if(n<0){"
7075"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007076"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007077"\n" "}"
7078"\n" "a=1"
7079"\n" "for(i=2;i<=n;++i)a*=i"
7080"\n" "scale=1.5*s"
7081"\n" "a=(x^n)/2^n/a"
7082"\n" "r=v=1"
7083"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007084"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007085"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007086"\n" "v=v*f/i/(n+i)"
7087"\n" "r+=v"
7088"\n" "}"
7089"\n" "scale=s"
7090"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007091"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007092"\n" "return(a*r/1)"
7093"\n" "}"
7094};
7095#endif // ENABLE_BC
7096
Denys Vlasenko19f11072018-12-12 22:48:19 +01007097static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007098{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007099 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007100 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007101 size_t i;
7102
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007103#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007104 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007105 // We know that internal library is not buggy,
7106 // thus error checking is normally disabled.
7107# define DEBUG_LIB 0
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007108 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007109 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007110 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007111#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007112
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007113 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007114 fname = (void*)G.files.v;
7115 for (i = 0; i < G.files.len; i++) {
7116 s = zbc_vm_file(*fname++);
7117 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7118 // Debug config, non-interactive mode:
7119 // return all the way back to main.
7120 // Non-debug builds do not come here
7121 // in non-interactive mode, they exit.
7122 RETURN_STATUS(s);
7123 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007124 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007125
Denys Vlasenko91cde952018-12-10 20:56:08 +01007126 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007127 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007128
Denys Vlasenko19f11072018-12-12 22:48:19 +01007129 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007130}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007131#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007132
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007133#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007134static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007135{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007136 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007137 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007138 bc_vec_free(&G.prog.vars);
7139 bc_vec_free(&G.prog.var_map);
7140 bc_vec_free(&G.prog.arrs);
7141 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007142 IF_DC(bc_vec_free(&G.prog.strs);)
7143 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007144 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007145 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007146 IF_BC(bc_num_free(&G.prog.last);)
7147 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007148 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007149 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007150}
7151
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007152static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007153{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007154 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007155 bc_program_free();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007156 bc_parse_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007157 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007158}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007159#endif
7160
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007161static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007162{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007163 BcInstPtr ip;
7164
Denys Vlasenko82269122018-12-14 16:30:56 +01007165 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007166 memset(&ip, 0, sizeof(BcInstPtr));
7167
Denys Vlasenko82269122018-12-14 16:30:56 +01007168 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007169 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007170 G.prog.ob_t = 10;
7171
Denys Vlasenko503faf92018-12-20 16:24:18 +01007172 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7173 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007174
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007175 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007176 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007177
Denys Vlasenko503faf92018-12-20 16:24:18 +01007178 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7179 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007180
7181 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007182 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007183
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007184 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007185 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007186 // a valid function name (and be short)
7187 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007188 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007189 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007190 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007191 bc_program_add_fn();
7192 bc_program_add_fn();
7193 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007194
7195 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007196 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007197
7198 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007199 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007200
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007201 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7202 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007203 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007204 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7205 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007206
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007207 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007208}
Gavin Howard01055ba2018-11-03 11:00:21 -06007209
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007210static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007211{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007212#if ENABLE_FEATURE_EDITING
7213 G.line_input_state = new_line_input_t(DO_HISTORY);
7214#endif
7215 G.prog.len = bc_vm_envLen(env_len);
7216
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007217 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007218 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007219 bc_program_init();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007220 bc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007221
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007222//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7223//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007224 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007225#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007226 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007227 // With SA_RESTART, most system calls will restart
7228 // (IOW: they won't fail with EINTR).
7229 // In particular, this means ^C won't cause
7230 // stdout to get into "error state" if SIGINT hits
7231 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007232 //
7233 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007234 // will only be handled after [Enter] since read()
7235 // from stdin is not interrupted by ^C either,
7236 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007237 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007238 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7239
7240 // Without SA_RESTART, this exhibits a bug:
7241 // "while (1) print 1" and try ^C-ing it.
7242 // Intermittently, instead of returning to input line,
7243 // you'll get "output error: Interrupted system call"
7244 // and exit.
7245 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007246#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007247 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007248 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007249 return 0; // "not a tty"
7250}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007251
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007252static BcStatus bc_vm_run(void)
7253{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007254 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007255#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007256 if (G_exiting) // it was actually "halt" or "quit"
7257 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007258 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007259# if ENABLE_FEATURE_EDITING
7260 free_line_input_t(G.line_input_state);
7261# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007262 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007263#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007264 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007265 return st;
7266}
7267
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007268#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007269int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007270int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007271{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007272 int is_tty;
7273
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007274 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007275
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007276 is_tty = bc_vm_init("BC_LINE_LENGTH");
7277
7278 bc_args(argv);
7279
7280 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7281 bc_vm_info();
7282
7283 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007284}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007285#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007286
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007287#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007288int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007289int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007290{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007291 int noscript;
7292
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007293 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007294
7295 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7296 // 1 char wider than bc from the same package.
7297 // Both default width, and xC_LINE_LENGTH=N are wider:
7298 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007299 // |1234\ |
7300 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007301 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007302 // |123\ |
7303 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007304 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007305 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007306
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007307 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7308 noscript = BC_FLAG_I;
7309 for (;;) {
7310 int n = getopt(argc, argv, "e:f:x");
7311 if (n <= 0)
7312 break;
7313 switch (n) {
7314 case 'e':
7315 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007316 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007317 if (n) return n;
7318 break;
7319 case 'f':
7320 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007321 n = zbc_vm_file(optarg);
7322 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007323 break;
7324 case 'x':
7325 option_mask32 |= DC_FLAG_X;
7326 break;
7327 default:
7328 bb_show_usage();
7329 }
7330 }
7331 argv += optind;
7332
7333 while (*argv) {
7334 noscript = 0;
7335 bc_vec_push(&G.files, argv++);
7336 }
7337
7338 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7339
7340 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007341}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007342#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007343
7344#endif // not DC_SMALL