blob: 537612d5e4385db1b920250312b486d74e64432b [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 Vlasenko10bde142018-12-27 18:23:58 +0100439 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
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 Vlasenko2beb1f62018-12-26 21:17:12 +0100613static const uint8_t bc_parse_ops[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614#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
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100634dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100635 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100636 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100637 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100638 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100639 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100640 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +0100657 // Z[\]
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100658 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100659 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100660 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +0100670 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100671 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +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 Vlasenko10bde142018-12-27 18:23:58 +0100677dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
678 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
679 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
680 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_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
Denys Vlasenko10bde142018-12-27 18:23:58 +0100695 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
696 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
697 DC_INST_EXECUTE, // DC_LEX_EXECUTE
698 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
699 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
700 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
701 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
702 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
703 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
704 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
705 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
706 XC_INST_SCALE_FUNC, // DC_LEX_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
Denys Vlasenko6e618232018-12-25 22:20:14 +0100806#if ENABLE_BC
807# define BC_PARSE_REL (1 << 0)
808# define BC_PARSE_PRINT (1 << 1)
809# define BC_PARSE_ARRAY (1 << 2)
810# define BC_PARSE_NOCALL (1 << 3)
811#endif
812
Gavin Howard01055ba2018-11-03 11:00:21 -0600813#define BC_PROG_MAIN (0)
814#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100815#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600816#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100817#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600818
819#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
820#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100821 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600822
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100823#define BC_FLAG_W (1 << 0)
824#define BC_FLAG_V (1 << 1)
825#define BC_FLAG_S (1 << 2)
826#define BC_FLAG_Q (1 << 3)
827#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100828#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
829#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600830
831#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
832#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
833
Denys Vlasenko64074a12018-12-07 15:50:14 +0100834#define BC_MAX_OBASE ((unsigned) 999)
835#define BC_MAX_DIM ((unsigned) INT_MAX)
836#define BC_MAX_SCALE ((unsigned) UINT_MAX)
837#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
838#define BC_MAX_NUM BC_MAX_STRING
839// Unused apart from "limits" message. Just show a "biggish number" there.
840//#define BC_MAX_NAME BC_MAX_STRING
841//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
842//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
843#define BC_MAX_NAME_STR "999999999"
844#define BC_MAX_EXP_STR "999999999"
845#define BC_MAX_VARS_STR "999999999"
846
847#define BC_MAX_OBASE_STR "999"
848
849#if INT_MAX == 2147483647
850# define BC_MAX_DIM_STR "2147483647"
851#elif INT_MAX == 9223372036854775807
852# define BC_MAX_DIM_STR "9223372036854775807"
853#else
854# error Strange INT_MAX
855#endif
856
857#if UINT_MAX == 4294967295
858# define BC_MAX_SCALE_STR "4294967295"
859# define BC_MAX_STRING_STR "4294967294"
860#elif UINT_MAX == 18446744073709551615
861# define BC_MAX_SCALE_STR "18446744073709551615"
862# define BC_MAX_STRING_STR "18446744073709551614"
863#else
864# error Strange UINT_MAX
865#endif
866#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600867
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100868// In configurations where errors abort instead of propagating error
869// return code up the call chain, functions returning BC_STATUS
870// actually don't return anything, they always succeed and return "void".
871// A macro wrapper is provided, which makes this statement work:
872// s = zbc_func(...)
873// and makes it visible to the compiler that s is always zero,
874// allowing compiler to optimize dead code after the statement.
875//
876// To make code more readable, each such function has a "z"
877// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
878//
879#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100880# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100881# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100882# define IF_ERROR_RETURN_POSSIBLE(a) a
883# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100884# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100885# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100886#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100887# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100888# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100889# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
890# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100891# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100892# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100893#endif
894
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100895//
896// Utility routines
897//
Gavin Howard01055ba2018-11-03 11:00:21 -0600898
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100899static void fflush_and_check(void)
900{
901 fflush_all();
902 if (ferror(stdout) || ferror(stderr))
903 bb_perror_msg_and_die("output error");
904}
905
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100906#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100907#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100908do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100909 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100910 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100911 return BC_STATUS_FAILURE; \
912} while (0)
913#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100914static void quit(void) NORETURN;
915static void quit(void)
916{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100917 if (ferror(stdin))
918 bb_perror_msg_and_die("input error");
919 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100920 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100921 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100922}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100923#define QUIT_OR_RETURN_TO_MAIN quit()
924#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100925
Denys Vlasenko5318f812018-12-05 17:48:01 +0100926static void bc_verror_msg(const char *fmt, va_list p)
927{
Denys Vlasenko82269122018-12-14 16:30:56 +0100928 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100929 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100930 sv = applet_name;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100931 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100932 }
933 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100934 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100935 free((char*)applet_name);
936 applet_name = sv;
937 }
938}
939
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100940static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100941{
942 va_list p;
943
944 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100945 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100946 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100947
Denys Vlasenkoec603182018-12-17 10:34:02 +0100948 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
949 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
950 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100951}
952
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100953#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100954static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100955{
956 va_list p;
957
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100958 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100959 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100960 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100961
962 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100963 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100964 va_end(p);
965
966 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100967 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100968 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
969
Denys Vlasenkoec603182018-12-17 10:34:02 +0100970 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100971 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100972 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100973}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100974#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100975#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100976
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100977// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
978// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100979// function must not have caller-cleaned parameters on stack.
980// Unfortunately, vararg function API does exactly that on most arches.
981// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100982static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100983{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100984 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100985}
986static ERRORFUNC int bc_error_bad_character(char c)
987{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100988 if (!c)
989 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100990 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100991}
992static ERRORFUNC int bc_error_bad_expression(void)
993{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100994 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100995}
996static ERRORFUNC int bc_error_bad_token(void)
997{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100998 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100999}
1000static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1001{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001002 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001003}
1004static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1005{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001006 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001007}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001008#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001009static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001010{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001011 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001012}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001013#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1014static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001015{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001016 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001017}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001018#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1019static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001020{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001021 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 +01001022}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001023#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)
1024static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001025{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001026 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001027}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001028#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 +01001029#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001030
Gavin Howard01055ba2018-11-03 11:00:21 -06001031static void bc_vec_grow(BcVec *v, size_t n)
1032{
1033 size_t cap = v->cap * 2;
1034 while (cap < v->len + n) cap *= 2;
1035 v->v = xrealloc(v->v, v->size * cap);
1036 v->cap = cap;
1037}
1038
1039static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1040{
1041 v->size = esize;
1042 v->cap = BC_VEC_START_CAP;
1043 v->len = 0;
1044 v->dtor = dtor;
1045 v->v = xmalloc(esize * BC_VEC_START_CAP);
1046}
1047
Denys Vlasenko7d628012018-12-04 21:46:47 +01001048static void bc_char_vec_init(BcVec *v)
1049{
1050 bc_vec_init(v, sizeof(char), NULL);
1051}
1052
Gavin Howard01055ba2018-11-03 11:00:21 -06001053static void bc_vec_expand(BcVec *v, size_t req)
1054{
1055 if (v->cap < req) {
1056 v->v = xrealloc(v->v, v->size * req);
1057 v->cap = req;
1058 }
1059}
1060
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001061static void bc_vec_pop(BcVec *v)
1062{
1063 v->len--;
1064 if (v->dtor)
1065 v->dtor(v->v + (v->size * v->len));
1066}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001067
Gavin Howard01055ba2018-11-03 11:00:21 -06001068static void bc_vec_npop(BcVec *v, size_t n)
1069{
1070 if (!v->dtor)
1071 v->len -= n;
1072 else {
1073 size_t len = v->len - n;
1074 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1075 }
1076}
1077
Denys Vlasenko7d628012018-12-04 21:46:47 +01001078static void bc_vec_pop_all(BcVec *v)
1079{
1080 bc_vec_npop(v, v->len);
1081}
1082
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001083static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001084{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001085 size_t len = v->len;
1086 if (len >= v->cap) bc_vec_grow(v, 1);
1087 memmove(v->v + (v->size * len), data, v->size);
1088 v->len++;
1089 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001090}
1091
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001092// G.prog.results often needs "pop old operand, push result" idiom.
1093// Can do this without a few extra ops
1094static size_t bc_result_pop_and_push(const void *data)
1095{
1096 BcVec *v = &G.prog.results;
1097 char *last;
1098 size_t len = v->len - 1;
1099
1100 last = v->v + (v->size * len);
1101 if (v->dtor)
1102 v->dtor(last);
1103 memmove(last, data, v->size);
1104 return len;
1105}
1106
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001107static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001108{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001109 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001110}
1111
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001112static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001113{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001114 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001115 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001116 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001117}
1118
Gavin Howard01055ba2018-11-03 11:00:21 -06001119static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1120{
1121 if (idx == v->len)
1122 bc_vec_push(v, data);
1123 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001124 char *ptr;
1125
1126 if (v->len == v->cap) bc_vec_grow(v, 1);
1127
1128 ptr = v->v + v->size * idx;
1129
1130 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1131 memmove(ptr, data, v->size);
1132 }
1133}
1134
1135static void bc_vec_string(BcVec *v, size_t len, const char *str)
1136{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001137 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001138 bc_vec_expand(v, len + 1);
1139 memcpy(v->v, str, len);
1140 v->len = len;
1141
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001142 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001143}
1144
Gavin Howard01055ba2018-11-03 11:00:21 -06001145static void *bc_vec_item(const BcVec *v, size_t idx)
1146{
1147 return v->v + v->size * idx;
1148}
1149
1150static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1151{
1152 return v->v + v->size * (v->len - idx - 1);
1153}
1154
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001155static void *bc_vec_top(const BcVec *v)
1156{
1157 return v->v + v->size * (v->len - 1);
1158}
1159
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001160static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001161{
1162 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001163 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001164 free(v->v);
1165}
1166
Denys Vlasenko10bde142018-12-27 18:23:58 +01001167static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001168{
1169 return bc_vec_item(&G.prog.fns, idx);
1170}
1171// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001172#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001173
1174#if ENABLE_BC
1175static BcFunc* bc_program_current_func(void)
1176{
1177 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001178 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001179 return func;
1180}
1181#endif
1182
Denys Vlasenko10bde142018-12-27 18:23:58 +01001183static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001184{
1185#if ENABLE_BC
1186 if (IS_BC) {
1187 BcFunc *func = bc_program_current_func();
1188 return bc_vec_item(&func->strs, idx);
1189 }
1190#endif
1191 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1192}
1193
Denys Vlasenko10bde142018-12-27 18:23:58 +01001194static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001195{
1196#if ENABLE_BC
1197 if (IS_BC) {
1198 BcFunc *func = bc_program_current_func();
1199 return bc_vec_item(&func->consts, idx);
1200 }
1201#endif
1202 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1203}
1204
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001205static int bc_id_cmp(const void *e1, const void *e2)
1206{
1207 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1208}
1209
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001210static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001211{
1212 free(((BcId *) id)->name);
1213}
1214
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001215static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001216{
1217 size_t low = 0, high = v->len;
1218
1219 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001220 size_t mid = (low + high) / 2;
1221 BcId *id = bc_vec_item(v, mid);
1222 int result = bc_id_cmp(ptr, id);
1223
1224 if (result == 0)
1225 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001226 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001227 high = mid;
1228 else
1229 low = mid + 1;
1230 }
1231
1232 return low;
1233}
1234
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001235static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001236{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001237 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001238
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001239 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001240 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001241 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1242 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001243 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001244 bc_vec_pushAt(v, ptr, n);
1245 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001246}
1247
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001248#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001249static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001250{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001251 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001252 if (i >= v->len) return BC_VEC_INVALID_IDX;
1253 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1254}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001255#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001256
Gavin Howard01055ba2018-11-03 11:00:21 -06001257static void bc_num_setToZero(BcNum *n, size_t scale)
1258{
1259 n->len = 0;
1260 n->neg = false;
1261 n->rdx = scale;
1262}
1263
1264static void bc_num_zero(BcNum *n)
1265{
1266 bc_num_setToZero(n, 0);
1267}
1268
1269static void bc_num_one(BcNum *n)
1270{
1271 bc_num_setToZero(n, 0);
1272 n->len = 1;
1273 n->num[0] = 1;
1274}
1275
Denys Vlasenko3129f702018-12-09 12:04:44 +01001276// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001277static void bc_num_init(BcNum *n, size_t req)
1278{
1279 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001280 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001281 n->num = xmalloc(req);
1282 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001283 n->rdx = 0;
1284 n->len = 0;
1285 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001286}
1287
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001288static void bc_num_init_DEF_SIZE(BcNum *n)
1289{
1290 bc_num_init(n, BC_NUM_DEF_SIZE);
1291}
1292
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001293static void bc_num_expand(BcNum *n, size_t req)
1294{
1295 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1296 if (req > n->cap) {
1297 n->num = xrealloc(n->num, req);
1298 n->cap = req;
1299 }
1300}
1301
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001302static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001303{
1304 free(((BcNum *) num)->num);
1305}
1306
1307static void bc_num_copy(BcNum *d, BcNum *s)
1308{
1309 if (d != s) {
1310 bc_num_expand(d, s->cap);
1311 d->len = s->len;
1312 d->neg = s->neg;
1313 d->rdx = s->rdx;
1314 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1315 }
1316}
1317
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001318static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001319{
1320 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001321 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001322
Denys Vlasenko1557b762018-12-22 21:37:46 +01001323 result = 0;
1324 i = n->len;
1325 while (i > n->rdx) {
1326 unsigned long prev = result;
1327 result = result * 10 + n->num[--i];
1328 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1329 // 0x1ff00000 * 10 is 0x13f600000,
1330 // or 0x3f600000 truncated to 32 bits. Which is larger.
1331 // However, (N*10)/8 < N check is always correct.
1332 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001333 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001334 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001335 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001336
Denys Vlasenko29301232018-12-11 15:29:32 +01001337 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001338}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001339#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1340
1341static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1342{
1343 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1344
1345 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1346}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001347#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001348
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001349#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1350# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1351#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1352# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1353#endif
1354// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1355// would not hit realloc() code path - not good if num[] is not malloced
1356
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001357static void bc_num_ulong2num(BcNum *n, unsigned long val)
1358{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001359 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001360
1361 bc_num_zero(n);
1362
1363 if (val == 0) return;
1364
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001365 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001366
1367 ptr = n->num;
1368 for (;;) {
1369 n->len++;
1370 *ptr++ = val % 10;
1371 val /= 10;
1372 if (val == 0) break;
1373 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001374}
1375
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001376static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001377{
1378 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001379 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001380 a[i] -= b[i];
1381 for (j = i; a[j] < 0;) {
1382 a[j++] += 10;
1383 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001384 }
1385 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001386}
1387
1388static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1389{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001390 size_t i = len;
1391 for (;;) {
1392 int c;
1393 if (i == 0)
1394 return 0;
1395 i--;
1396 c = a[i] - b[i];
1397 if (c != 0) {
1398 i++;
1399 if (c < 0)
1400 return -i;
1401 return i;
1402 }
1403 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001404}
1405
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001406#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1407#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1408#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1409//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1410static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1411{
1412 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1413}
1414//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1415static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1416{
1417 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1418}
1419
Gavin Howard01055ba2018-11-03 11:00:21 -06001420static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1421{
1422 size_t i, min, a_int, b_int, diff;
1423 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001424 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001425 ssize_t cmp;
1426
1427 if (a == b) return 0;
1428 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1429 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001430
1431 if (a->neg != b->neg) // signs of a and b differ
1432 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1433 return (int)b->neg - (int)a->neg;
1434 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001435
1436 a_int = BC_NUM_INT(a);
1437 b_int = BC_NUM_INT(b);
1438 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001439
1440 if (a_int != 0) return (ssize_t) a_int;
1441
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001442 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001443 if (a_max) {
1444 min = b->rdx;
1445 diff = a->rdx - b->rdx;
1446 max_num = a->num + diff;
1447 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001448 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1449 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001450 min = a->rdx;
1451 diff = b->rdx - a->rdx;
1452 max_num = b->num + diff;
1453 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001454 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001455 }
1456
1457 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001458 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001459
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001460 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001461 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001462 }
1463
1464 return 0;
1465}
1466
1467static void bc_num_truncate(BcNum *n, size_t places)
1468{
1469 if (places == 0) return;
1470
1471 n->rdx -= places;
1472
1473 if (n->len != 0) {
1474 n->len -= places;
1475 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1476 }
1477}
1478
1479static void bc_num_extend(BcNum *n, size_t places)
1480{
1481 size_t len = n->len + places;
1482
1483 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001484 if (n->cap < len) bc_num_expand(n, len);
1485
1486 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1487 memset(n->num, 0, sizeof(BcDig) * places);
1488
1489 n->len += places;
1490 n->rdx += places;
1491 }
1492}
1493
1494static void bc_num_clean(BcNum *n)
1495{
1496 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1497 if (n->len == 0)
1498 n->neg = false;
1499 else if (n->len < n->rdx)
1500 n->len = n->rdx;
1501}
1502
1503static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1504{
1505 if (n->rdx < scale)
1506 bc_num_extend(n, scale - n->rdx);
1507 else
1508 bc_num_truncate(n, n->rdx - scale);
1509
1510 bc_num_clean(n);
1511 if (n->len != 0) n->neg = !neg1 != !neg2;
1512}
1513
1514static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1515 BcNum *restrict b)
1516{
1517 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001518 b->len = n->len - idx;
1519 a->len = idx;
1520 a->rdx = b->rdx = 0;
1521
1522 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1523 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001524 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001525 bc_num_zero(b);
1526 bc_num_copy(a, n);
1527 }
1528
1529 bc_num_clean(a);
1530 bc_num_clean(b);
1531}
1532
Denys Vlasenko29301232018-12-11 15:29:32 +01001533static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001534{
Denys Vlasenko29301232018-12-11 15:29:32 +01001535 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001536
1537 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1538 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1539 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001540 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001541 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001542
1543 if (n->rdx >= places)
1544 n->rdx -= places;
1545 else {
1546 bc_num_extend(n, places - n->rdx);
1547 n->rdx = 0;
1548 }
1549
1550 bc_num_clean(n);
1551
Denys Vlasenko29301232018-12-11 15:29:32 +01001552 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001553}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001554#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001555
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001556typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1557
1558static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1559 BcNumBinaryOp op, size_t req)
1560{
1561 BcStatus s;
1562 BcNum num2, *ptr_a, *ptr_b;
1563 bool init = false;
1564
1565 if (c == a) {
1566 ptr_a = &num2;
1567 memcpy(ptr_a, c, sizeof(BcNum));
1568 init = true;
1569 } else
1570 ptr_a = a;
1571
1572 if (c == b) {
1573 ptr_b = &num2;
1574 if (c != a) {
1575 memcpy(ptr_b, c, sizeof(BcNum));
1576 init = true;
1577 }
1578 } else
1579 ptr_b = b;
1580
1581 if (init)
1582 bc_num_init(c, req);
1583 else
1584 bc_num_expand(c, req);
1585
1586 s = BC_STATUS_SUCCESS;
1587 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1588
1589 if (init) bc_num_free(&num2);
1590
1591 RETURN_STATUS(s);
1592}
1593#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1594
1595static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1596static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1597static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1598static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1599static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1600static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1601
1602static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1603{
1604 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1605 (void) scale;
1606 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1607}
1608
1609static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1610{
1611 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1612 (void) scale;
1613 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1614}
1615
1616static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1617{
1618 size_t req = BC_NUM_MREQ(a, b, scale);
1619 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1620}
1621
1622static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1623{
1624 size_t req = BC_NUM_MREQ(a, b, scale);
1625 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1626}
1627
1628static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1629{
1630 size_t req = BC_NUM_MREQ(a, b, scale);
1631 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1632}
1633
1634static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1635{
1636 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1637}
1638
Denys Vlasenko10bde142018-12-27 18:23:58 +01001639static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001640 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1641};
1642#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1643#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1644#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1645#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1646#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1647#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1648
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001649static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001650{
1651 BcNum one;
1652 BcDig num[2];
1653
1654 one.cap = 2;
1655 one.num = num;
1656 bc_num_one(&one);
1657
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001658 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001659}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001660#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001661
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001662static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001663{
1664 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1665 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001666 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001667
1668 // Because this function doesn't need to use scale (per the bc spec),
1669 // I am hijacking it to say whether it's doing an add or a subtract.
1670
1671 if (a->len == 0) {
1672 bc_num_copy(c, b);
1673 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001674 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001675 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001676 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001677 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001678 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001679 }
1680
1681 c->neg = a->neg;
1682 c->rdx = BC_MAX(a->rdx, b->rdx);
1683 min_rdx = BC_MIN(a->rdx, b->rdx);
1684 c->len = 0;
1685
1686 if (a->rdx > b->rdx) {
1687 diff = a->rdx - b->rdx;
1688 ptr = a->num;
1689 ptr_a = a->num + diff;
1690 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001691 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001692 diff = b->rdx - a->rdx;
1693 ptr = b->num;
1694 ptr_a = a->num;
1695 ptr_b = b->num + diff;
1696 }
1697
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001698 ptr_c = c->num;
1699 for (i = 0; i < diff; ++i, ++c->len)
1700 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001701
1702 ptr_c += diff;
1703 a_int = BC_NUM_INT(a);
1704 b_int = BC_NUM_INT(b);
1705
1706 if (a_int > b_int) {
1707 min_int = b_int;
1708 max = a_int;
1709 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001710 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001711 min_int = a_int;
1712 max = b_int;
1713 ptr = ptr_b;
1714 }
1715
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001716 carry = 0;
1717 for (i = 0; i < min_rdx + min_int; ++i) {
1718 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001719 carry = in / 10;
1720 ptr_c[i] = (BcDig)(in % 10);
1721 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001722 for (; i < max + min_rdx; ++i) {
1723 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001724 carry = in / 10;
1725 ptr_c[i] = (BcDig)(in % 10);
1726 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001727 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001728
1729 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1730
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001731 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001732}
1733
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001734static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001735{
Gavin Howard01055ba2018-11-03 11:00:21 -06001736 ssize_t cmp;
1737 BcNum *minuend, *subtrahend;
1738 size_t start;
1739 bool aneg, bneg, neg;
1740
1741 // Because this function doesn't need to use scale (per the bc spec),
1742 // I am hijacking it to say whether it's doing an add or a subtract.
1743
1744 if (a->len == 0) {
1745 bc_num_copy(c, b);
1746 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001747 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001748 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001749 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001750 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001751 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001752 }
1753
1754 aneg = a->neg;
1755 bneg = b->neg;
1756 a->neg = b->neg = false;
1757
1758 cmp = bc_num_cmp(a, b);
1759
1760 a->neg = aneg;
1761 b->neg = bneg;
1762
1763 if (cmp == 0) {
1764 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001765 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001766 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001767 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001768 neg = a->neg;
1769 minuend = a;
1770 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001771 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001772 neg = b->neg;
1773 if (sub) neg = !neg;
1774 minuend = b;
1775 subtrahend = a;
1776 }
1777
1778 bc_num_copy(c, minuend);
1779 c->neg = neg;
1780
1781 if (c->rdx < subtrahend->rdx) {
1782 bc_num_extend(c, subtrahend->rdx - c->rdx);
1783 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001784 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001785 start = c->rdx - subtrahend->rdx;
1786
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001787 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001788
1789 bc_num_clean(c);
1790
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001791 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001792}
1793
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001794static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001795 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001796#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001797{
1798 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001799 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001801 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001802
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 if (a->len == 0 || b->len == 0) {
1804 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001805 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001806 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001807 aone = BC_NUM_ONE(a);
1808 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001809 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001810 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001811 }
1812
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001813 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1814 || a->len < BC_NUM_KARATSUBA_LEN
1815 || b->len < BC_NUM_KARATSUBA_LEN
1816 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001817 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001818
Gavin Howard01055ba2018-11-03 11:00:21 -06001819 bc_num_expand(c, a->len + b->len + 1);
1820
1821 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001822 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001823
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001824 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001825 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001826 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001827 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001828 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001829 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001830 carry = in / 10;
1831 c->num[i + j] = (BcDig)(in % 10);
1832 }
1833
1834 c->num[i + j] += (BcDig) carry;
1835 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001836
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001837#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001838 // a=2^1000000
1839 // a*a <- without check below, this will not be interruptible
1840 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 }
1843
1844 c->len = len;
1845
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001846 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001847 }
1848
1849 bc_num_init(&l1, max);
1850 bc_num_init(&h1, max);
1851 bc_num_init(&l2, max);
1852 bc_num_init(&h2, max);
1853 bc_num_init(&m1, max);
1854 bc_num_init(&m2, max);
1855 bc_num_init(&z0, max);
1856 bc_num_init(&z1, max);
1857 bc_num_init(&z2, max);
1858 bc_num_init(&temp, max + max);
1859
1860 bc_num_split(a, max2, &l1, &h1);
1861 bc_num_split(b, max2, &l2, &h2);
1862
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001863 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001864 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001865 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001866 if (s) goto err;
1867
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001868 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001869 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001870 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001871 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001872 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 if (s) goto err;
1874
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001875 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001877 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 if (s) goto err;
1879
Denys Vlasenko29301232018-12-11 15:29:32 +01001880 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001882 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001883 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001884 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001885 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001886 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001887 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001888 bc_num_free(&temp);
1889 bc_num_free(&z2);
1890 bc_num_free(&z1);
1891 bc_num_free(&z0);
1892 bc_num_free(&m2);
1893 bc_num_free(&m1);
1894 bc_num_free(&h2);
1895 bc_num_free(&l2);
1896 bc_num_free(&h1);
1897 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001898 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001899}
1900
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001901static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001902{
1903 BcStatus s;
1904 BcNum cpa, cpb;
1905 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1906
1907 scale = BC_MAX(scale, a->rdx);
1908 scale = BC_MAX(scale, b->rdx);
1909 scale = BC_MIN(a->rdx + b->rdx, scale);
1910 maxrdx = BC_MAX(maxrdx, scale);
1911
1912 bc_num_init(&cpa, a->len);
1913 bc_num_init(&cpb, b->len);
1914
1915 bc_num_copy(&cpa, a);
1916 bc_num_copy(&cpb, b);
1917 cpa.neg = cpb.neg = false;
1918
Denys Vlasenko29301232018-12-11 15:29:32 +01001919 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001920 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001921 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001922 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001923 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001924 if (s) goto err;
1925
1926 maxrdx += scale;
1927 bc_num_expand(c, c->len + maxrdx);
1928
1929 if (c->len < maxrdx) {
1930 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1931 c->len += maxrdx;
1932 }
1933
1934 c->rdx = maxrdx;
1935 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001936 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001937 bc_num_free(&cpb);
1938 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001939 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001940}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001941#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001942
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001943static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001944{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001945 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001946 size_t len, end, i;
1947 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001948
1949 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001950 RETURN_STATUS(bc_error("divide by zero"));
1951 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001952 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001953 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001954 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001955 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 bc_num_copy(c, a);
1957 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001958 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 }
1960
1961 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1962 bc_num_copy(&cp, a);
1963 len = b->len;
1964
1965 if (len > cp.len) {
1966 bc_num_expand(&cp, len + 2);
1967 bc_num_extend(&cp, len - cp.len);
1968 }
1969
1970 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1971 cp.rdx -= b->rdx;
1972 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1973
1974 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001975 for (;;) {
1976 if (len == 0) break;
1977 len--;
1978 if (b->num[len] != 0)
1979 break;
1980 }
1981 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 }
1983
1984 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1985
1986 // We want an extra zero in front to make things simpler.
1987 cp.num[cp.len++] = 0;
1988 end = cp.len - len;
1989
1990 bc_num_expand(c, cp.len);
1991
1992 bc_num_zero(c);
1993 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1994 c->rdx = cp.rdx;
1995 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001996
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001997 s = BC_STATUS_SUCCESS;
1998 for (i = end - 1; i < end; --i) {
1999 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002000 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002001 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2002 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002003 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002004#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002005 // a=2^100000
2006 // scale=40000
2007 // 1/a <- without check below, this will not be interruptible
2008 if (G_interrupt) {
2009 s = BC_STATUS_FAILURE;
2010 break;
2011 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002012#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 }
2014
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002015 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002016 bc_num_free(&cp);
2017
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002018 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002019}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002020#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002021
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002022static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002023 BcNum *restrict d, size_t scale, size_t ts)
2024{
2025 BcStatus s;
2026 BcNum temp;
2027 bool neg;
2028
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002029 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002030 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002031
2032 if (a->len == 0) {
2033 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002034 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002035 }
2036
2037 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002038 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002039 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002040
2041 if (scale != 0) scale = ts;
2042
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002043 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002044 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002045 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002046 if (s) goto err;
2047
2048 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2049
2050 neg = d->neg;
2051 bc_num_retireMul(d, ts, a->neg, b->neg);
2052 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002053 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002054 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002055 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002056}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002057#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002058
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002059static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002060{
2061 BcStatus s;
2062 BcNum c1;
2063 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2064
2065 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002066 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002067 bc_num_free(&c1);
2068
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002069 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002070}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002071#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002072
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002073static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002074{
2075 BcStatus s = BC_STATUS_SUCCESS;
2076 BcNum copy;
2077 unsigned long pow;
2078 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002079 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002080
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002081 // GNU bc does not allow 2^2.0 - we do
2082 for (i = 0; i < b->rdx; i++)
2083 if (b->num[i] != 0)
2084 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002085
2086 if (b->len == 0) {
2087 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002088 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002089 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002090 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002091 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002092 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002093 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002094 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002095 if (!b->neg)
2096 bc_num_copy(c, a);
2097 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002098 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002100 }
2101
2102 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002103 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002104 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002105 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002106
2107 bc_num_init(&copy, a->len);
2108 bc_num_copy(&copy, a);
2109
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002110 if (!neg) {
2111 if (a->rdx > scale)
2112 scale = a->rdx;
2113 if (a->rdx * pow < scale)
2114 scale = a->rdx * pow;
2115 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002116
Gavin Howard01055ba2018-11-03 11:00:21 -06002117
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002118 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002119 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002120 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002122 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002123 //if (G_interrupt) {
2124 // s = BC_STATUS_FAILURE;
2125 // goto err;
2126 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002127 }
2128
Gavin Howard01055ba2018-11-03 11:00:21 -06002129 bc_num_copy(c, &copy);
2130
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002131 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002132 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002133 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002134 if (s) goto err;
2135
2136 if (pow & 1) {
2137 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002138 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002139 if (s) goto err;
2140 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002141 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002142 //if (G_interrupt) {
2143 // s = BC_STATUS_FAILURE;
2144 // goto err;
2145 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 }
2147
2148 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002149 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002150 if (s) goto err;
2151 }
2152
Gavin Howard01055ba2018-11-03 11:00:21 -06002153 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2154
2155 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002156 for (i = 0; i < c->len; ++i)
2157 if (c->num[i] != 0)
2158 goto skip;
2159 bc_num_setToZero(c, scale);
2160 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002161
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002162 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002163 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002164 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002165}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002166#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002167
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002168static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002169{
2170 BcStatus s;
2171 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2172 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2173 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2174
2175 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2176 bc_num_expand(b, req);
2177
2178 if (a->len == 0) {
2179 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002180 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002181 }
2182 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002183 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002184 }
2185 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002186 bc_num_one(b);
2187 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002188 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002189 }
2190
2191 scale = BC_MAX(scale, a->rdx) + 1;
2192 len = a->len + scale;
2193
2194 bc_num_init(&num1, len);
2195 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002196 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002197
2198 bc_num_one(&half);
2199 half.num[0] = 5;
2200 half.rdx = 1;
2201
2202 bc_num_init(&f, len);
2203 bc_num_init(&fprime, len);
2204
2205 x0 = &num1;
2206 x1 = &num2;
2207
2208 bc_num_one(x0);
2209 pow = BC_NUM_INT(a);
2210
2211 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002212 if (pow & 1)
2213 x0->num[0] = 2;
2214 else
2215 x0->num[0] = 6;
2216
2217 pow -= 2 - (pow & 1);
2218
2219 bc_num_extend(x0, pow);
2220
2221 // Make sure to move the radix back.
2222 x0->rdx -= pow;
2223 }
2224
2225 x0->rdx = digs = digs1 = 0;
2226 resrdx = scale + 2;
2227 len = BC_NUM_INT(x0) + resrdx - 1;
2228
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002229 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002230 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002231 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002232 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002233 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002234 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002235 if (s) goto err;
2236
2237 cmp = bc_num_cmp(x1, x0);
2238 digs = x1->len - (unsigned long long) llabs(cmp);
2239
2240 if (cmp == cmp2 && digs == digs1)
2241 times += 1;
2242 else
2243 times = 0;
2244
2245 resrdx += times > 4;
2246
2247 cmp2 = cmp1;
2248 cmp1 = cmp;
2249 digs1 = digs;
2250
2251 temp = x0;
2252 x0 = x1;
2253 x1 = temp;
2254 }
2255
Gavin Howard01055ba2018-11-03 11:00:21 -06002256 bc_num_copy(b, x0);
2257 scale -= 1;
2258 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002259 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002260 bc_num_free(&fprime);
2261 bc_num_free(&f);
2262 bc_num_free(&half);
2263 bc_num_free(&num2);
2264 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002265 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002266}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002267#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002268
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002269static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002270 size_t scale)
2271{
2272 BcStatus s;
2273 BcNum num2, *ptr_a;
2274 bool init = false;
2275 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2276
2277 if (c == a) {
2278 memcpy(&num2, c, sizeof(BcNum));
2279 ptr_a = &num2;
2280 bc_num_init(c, len);
2281 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002282 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002283 ptr_a = a;
2284 bc_num_expand(c, len);
2285 }
2286
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002287 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002288
2289 if (init) bc_num_free(&num2);
2290
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002291 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002292}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002293#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002294
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002295#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002296static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002297{
2298 BcStatus s;
2299 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002300 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002301
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002302 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002303 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002304 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002305 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002306 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002307 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002308
2309 bc_num_expand(d, c->len);
2310 bc_num_init(&base, c->len);
2311 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002312 bc_num_init(&temp, b->len);
2313
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002314 two.cap = ARRAY_SIZE(two_digs);
2315 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002316 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002317 two_digs[0] = 2;
2318
Gavin Howard01055ba2018-11-03 11:00:21 -06002319 bc_num_one(d);
2320
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002321 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002322 if (s) goto err;
2323 bc_num_copy(&exp, b);
2324
2325 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002326 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002327 if (s) goto err;
2328
2329 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002330 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002331 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002332 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002333 if (s) goto err;
2334 }
2335
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002336 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002337 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002338 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002339 if (s) goto err;
2340 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002341 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002342 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002343 bc_num_free(&exp);
2344 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002345 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002346}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002347#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002348#endif // ENABLE_DC
2349
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002350static FAST_FUNC void bc_string_free(void *string)
2351{
2352 free(*(char**)string);
2353}
2354
Gavin Howard01055ba2018-11-03 11:00:21 -06002355static void bc_func_init(BcFunc *f)
2356{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002357 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002358 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2359 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002360 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2361 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002362 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002363}
2364
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002365static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002366{
2367 BcFunc *f = (BcFunc *) func;
2368 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002369 IF_BC(bc_vec_free(&f->labels);)
2370 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002371 IF_BC(bc_vec_free(&f->strs);)
2372 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002373}
2374
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002375static void bc_array_expand(BcVec *a, size_t len);
2376
Gavin Howard01055ba2018-11-03 11:00:21 -06002377static void bc_array_init(BcVec *a, bool nums)
2378{
2379 if (nums)
2380 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2381 else
2382 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2383 bc_array_expand(a, 1);
2384}
2385
Gavin Howard01055ba2018-11-03 11:00:21 -06002386static void bc_array_expand(BcVec *a, size_t len)
2387{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002388 if (a->dtor == bc_num_free
2389 // && a->size == sizeof(BcNum) - always true
2390 ) {
2391 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002392 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002393 bc_num_init_DEF_SIZE(&n);
2394 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002395 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002396 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002397 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002398 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002399 bc_array_init(&v, true);
2400 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002401 }
2402 }
2403}
2404
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002405static void bc_array_copy(BcVec *d, const BcVec *s)
2406{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002407 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002408 size_t i;
2409
2410 bc_vec_pop_all(d);
2411 bc_vec_expand(d, s->cap);
2412 d->len = s->len;
2413
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002414 dnum = (void*)d->v;
2415 snum = (void*)s->v;
2416 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002417 bc_num_init(dnum, snum->len);
2418 bc_num_copy(dnum, snum);
2419 }
2420}
2421
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002422#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002423static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002424{
2425 d->t = src->t;
2426
2427 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002428 case XC_RESULT_TEMP:
2429 case XC_RESULT_IBASE:
2430 case XC_RESULT_SCALE:
2431 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002432 bc_num_init(&d->d.n, src->d.n.len);
2433 bc_num_copy(&d->d.n, &src->d.n);
2434 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002435 case XC_RESULT_VAR:
2436 case XC_RESULT_ARRAY:
2437 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002438 d->d.id.name = xstrdup(src->d.id.name);
2439 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002440 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002441 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002442 IF_BC(case BC_RESULT_ONE:)
2443 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002444 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2445 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002446 }
2447}
2448#endif // ENABLE_DC
2449
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002450static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002451{
2452 BcResult *r = (BcResult *) result;
2453
2454 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002455 case XC_RESULT_TEMP:
2456 case XC_RESULT_IBASE:
2457 case XC_RESULT_SCALE:
2458 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002459 bc_num_free(&r->d.n);
2460 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002461 case XC_RESULT_VAR:
2462 case XC_RESULT_ARRAY:
2463 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002464 free(r->d.id.name);
2465 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002466 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002467 // Do nothing.
2468 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002469 }
2470}
2471
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002472static int bad_input_byte(char c)
2473{
2474 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2475 || c > 0x7e
2476 ) {
2477 bc_error_fmt("illegal character 0x%02x", c);
2478 return 1;
2479 }
2480 return 0;
2481}
2482
Denys Vlasenko10bde142018-12-27 18:23:58 +01002483static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002484{
2485 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002486 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002487 fflush_and_check();
2488
2489#if ENABLE_FEATURE_BC_SIGNALS
2490 if (G_interrupt) { // ^C was pressed
2491 intr:
2492 if (fp != stdin) {
2493 // ^C while running a script (bc SCRIPT): die.
2494 // We do not return to interactive prompt:
2495 // user might be running us from a shell,
2496 // and SCRIPT might be intended to terminate
2497 // (e.g. contain a "halt" stmt).
2498 // ^C dropping user into a bc prompt instead of
2499 // the shell would be unexpected.
2500 xfunc_die();
2501 }
2502 // ^C while interactive input
2503 G_interrupt = 0;
2504 // GNU bc says "interrupted execution."
2505 // GNU dc says "Interrupt!"
2506 fputs("\ninterrupted execution\n", stderr);
2507 }
2508
2509# if ENABLE_FEATURE_EDITING
2510 if (G_ttyin && fp == stdin) {
2511 int n, i;
2512# define line_buf bb_common_bufsiz1
2513 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2514 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2515 if (n == 0) // ^C
2516 goto intr;
2517 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2518 return;
2519 }
2520 i = 0;
2521 for (;;) {
2522 char c = line_buf[i++];
2523 if (!c) break;
2524 if (bad_input_byte(c)) goto again;
2525 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002526 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002527# undef line_buf
2528 } else
2529# endif
2530#endif
2531 {
2532 int c;
2533 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002534
2535 do {
2536#if ENABLE_FEATURE_BC_SIGNALS
2537 if (G_interrupt) {
2538 // ^C was pressed: ignore entire line, get another one
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002539 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002540 goto intr;
2541 }
2542#endif
2543 do c = fgetc(fp); while (c == '\0');
2544 if (c == EOF) {
2545 if (ferror(fp))
2546 bb_perror_msg_and_die("input error");
2547 // Note: EOF does not append '\n'
2548 break;
2549 }
2550 bad_chars |= bad_input_byte(c);
2551 bc_vec_pushByte(vec, (char)c);
2552 } while (c != '\n');
2553
2554 if (bad_chars) {
2555 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002556 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002557 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002558 goto again;
2559 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002560 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002561 }
2562 bc_vec_pushZeroByte(vec);
2563 }
2564}
2565
2566//
2567// Parsing routines
2568//
2569
Denys Vlasenko10bde142018-12-27 18:23:58 +01002570static bool xc_num_strValid(const char *val, size_t base)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002571{
2572 BcDig b;
2573 bool radix;
2574
2575 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2576 radix = false;
2577 for (;;) {
2578 BcDig c = *val++;
2579 if (c == '\0')
2580 break;
2581 if (c == '.') {
2582 if (radix) return false;
2583 radix = true;
2584 continue;
2585 }
2586 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2587 return false;
2588 }
2589 return true;
2590}
2591
2592// Note: n is already "bc_num_zero()"ed,
2593// leading zeroes in "val" are removed
2594static void bc_num_parseDecimal(BcNum *n, const char *val)
2595{
2596 size_t len, i;
2597 const char *ptr;
2598
2599 len = strlen(val);
2600 if (len == 0)
2601 return;
2602
2603 bc_num_expand(n, len);
2604
2605 ptr = strchr(val, '.');
2606
2607 n->rdx = 0;
2608 if (ptr != NULL)
2609 n->rdx = (size_t)((val + len) - (ptr + 1));
2610
2611 for (i = 0; val[i]; ++i) {
2612 if (val[i] != '0' && val[i] != '.') {
2613 // Not entirely zero value - convert it, and exit
2614 i = len - 1;
2615 for (;;) {
2616 n->num[n->len] = val[i] - '0';
2617 ++n->len;
2618 skip_dot:
2619 if (i == 0) break;
2620 if (val[--i] == '.') goto skip_dot;
2621 }
2622 break;
2623 }
2624 }
2625 // if for() exits without hitting if(), the value is entirely zero
2626}
2627
2628// Note: n is already "bc_num_zero()"ed,
2629// leading zeroes in "val" are removed
2630static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2631{
2632 BcStatus s;
2633 BcNum temp, mult, result;
2634 BcNum base;
2635 BcDig base_digs[ULONG_NUM_BUFSIZE];
2636 BcDig c = '\0';
2637 unsigned long v;
2638 size_t i, digits;
2639
2640 for (i = 0; ; ++i) {
2641 if (val[i] == '\0')
2642 return;
2643 if (val[i] != '.' && val[i] != '0')
2644 break;
2645 }
2646
2647 bc_num_init_DEF_SIZE(&temp);
2648 bc_num_init_DEF_SIZE(&mult);
2649 base.cap = ARRAY_SIZE(base_digs);
2650 base.num = base_digs;
2651 bc_num_ulong2num(&base, base_t);
2652
2653 for (;;) {
2654 c = *val++;
2655 if (c == '\0') goto int_err;
2656 if (c == '.') break;
2657
2658 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2659
2660 s = zbc_num_mul(n, &base, &mult, 0);
2661 if (s) goto int_err;
2662 bc_num_ulong2num(&temp, v);
2663 s = zbc_num_add(&mult, &temp, n, 0);
2664 if (s) goto int_err;
2665 }
2666
2667 bc_num_init(&result, base.len);
2668 //bc_num_zero(&result); - already is
2669 bc_num_one(&mult);
2670
2671 digits = 0;
2672 for (;;) {
2673 c = *val++;
2674 if (c == '\0') break;
2675 digits++;
2676
2677 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2678
2679 s = zbc_num_mul(&result, &base, &result, 0);
2680 if (s) goto err;
2681 bc_num_ulong2num(&temp, v);
2682 s = zbc_num_add(&result, &temp, &result, 0);
2683 if (s) goto err;
2684 s = zbc_num_mul(&mult, &base, &mult, 0);
2685 if (s) goto err;
2686 }
2687
2688 s = zbc_num_div(&result, &mult, &result, digits);
2689 if (s) goto err;
2690 s = zbc_num_add(n, &result, n, digits);
2691 if (s) goto err;
2692
2693 if (n->len != 0) {
2694 if (n->rdx < digits)
2695 bc_num_extend(n, digits - n->rdx);
2696 } else
2697 bc_num_zero(n);
2698 err:
2699 bc_num_free(&result);
2700 int_err:
2701 bc_num_free(&mult);
2702 bc_num_free(&temp);
2703}
2704
Denys Vlasenko10bde142018-12-27 18:23:58 +01002705static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002706{
Denys Vlasenko10bde142018-12-27 18:23:58 +01002707 if (!xc_num_strValid(val, base_t))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002708 RETURN_STATUS(bc_error("bad number string"));
2709
2710 bc_num_zero(n);
2711 while (*val == '0') val++;
2712
2713 if (base_t == 10)
2714 bc_num_parseDecimal(n, val);
2715 else
2716 bc_num_parseBase(n, val, base_t);
2717
2718 RETURN_STATUS(BC_STATUS_SUCCESS);
2719}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002720#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002721
Denys Vlasenko10bde142018-12-27 18:23:58 +01002722static bool xc_lex_more_input(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002723{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002724 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002725
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002726 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002727
Denys Vlasenko10bde142018-12-27 18:23:58 +01002728 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002729
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002730 p->lex_inbuf = G.input_buffer.v;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002731// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002732
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002733 return G.input_buffer.len > 1;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002734}
2735
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002736// p->lex_inbuf points to the current string to be parsed.
2737// if p->lex_inbuf points to '\0', it's either EOF or it points after
2738// last processed line's terminating '\n' (and more reading needs to be done
2739// to get next character).
2740//
2741// If you are in a situation where that is a possibility, call peek_inbuf().
2742// If necessary, it performs more reading and changes p->lex_inbuf,
2743// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2744// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2745// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2746//
2747// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2748// it returns current char and advances the pointer (if not EOF).
2749// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2750//
2751// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2752// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2753// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002754//
2755// bc cases to test interactively:
2756// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2757// 1 #comment/* - prints "1<newline>" at once
2758// 1 #comment" - prints "1<newline>" at once
2759// 1\#comment - error at once (\ is not a line continuation)
2760// 1 + /*"*/2 - prints "3<newline>" at once
2761// 1 + /*#*/2 - prints "3<newline>" at once
2762// "str\" - prints "str\" at once
2763// "str#" - prints "str#" at once
2764// "str/*" - prints "str/*" at once
2765// "str#\ - waits for second line
2766// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002767static char peek_inbuf(void)
2768{
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002769 if (*G.prs.lex_inbuf == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002770 if (G.prs.lex_input_fp)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002771 if (!xc_lex_more_input())
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002772 G.prs.lex_input_fp = NULL;
2773 }
2774 return *G.prs.lex_inbuf;
2775}
2776static char eat_inbuf(void)
2777{
2778 char c = peek_inbuf();
2779 if (c) G.prs.lex_inbuf++;
2780 return c;
2781}
2782
Denys Vlasenko10bde142018-12-27 18:23:58 +01002783static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002784{
2785 BcParse *p = &G.prs;
2786 char c;
2787
2788 // Try: echo -n '#foo' | bc
2789 p->lex = XC_LEX_WHITESPACE;
2790
2791 // We depend here on input being done in whole lines:
2792 // '\0' which isn't the EOF can only be seen after '\n'.
2793 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2794 p->lex_inbuf++;
2795}
2796
Denys Vlasenko10bde142018-12-27 18:23:58 +01002797static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002798{
2799 BcParse *p = &G.prs;
2800
2801 p->lex = XC_LEX_WHITESPACE;
2802 for (;;) {
2803 // We depend here on input being done in whole lines:
2804 // '\0' which isn't the EOF can only be seen after '\n'.
2805 char c = *p->lex_inbuf;
2806 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2807 break;
2808 if (!isspace(c))
2809 break;
2810 p->lex_inbuf++;
2811 }
2812}
2813
Denys Vlasenko10bde142018-12-27 18:23:58 +01002814static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002815{
2816 BcParse *p = &G.prs;
2817 bool pt;
2818
2819 bc_vec_pop_all(&p->lex_strnumbuf);
2820 bc_vec_pushByte(&p->lex_strnumbuf, last);
2821
2822 pt = (last == '.');
2823 p->lex = XC_LEX_NUMBER;
2824 for (;;) {
2825 // We depend here on input being done in whole lines:
2826 // '\0' which isn't the EOF can only be seen after '\n'.
2827 char c = *p->lex_inbuf;
2828 check_c:
2829 if (c == '\0')
2830 break;
2831 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2832 p->lex_inbuf += 2;
2833 p->lex_line++;
2834 c = peek_inbuf(); // force next line to be read
2835 goto check_c;
2836 }
2837 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2838 if (c != '.') break;
2839 // if '.' was already seen, stop on second one:
2840 if (pt) break;
2841 pt = true;
2842 }
2843 // c is one of "0-9A-F."
2844 last = c;
2845 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2846 p->lex_inbuf++;
2847 }
2848 if (last == '.') // remove trailing '.' if any
2849 bc_vec_pop(&p->lex_strnumbuf);
2850 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2851
2852 G.err_line = G.prs.lex_line;
2853 RETURN_STATUS(BC_STATUS_SUCCESS);
2854}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002855#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002856
Denys Vlasenko10bde142018-12-27 18:23:58 +01002857static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002858{
2859 BcParse *p = &G.prs;
2860 size_t i;
2861 const char *buf;
2862
2863 p->lex = XC_LEX_NAME;
2864
2865 // Since names can't cross lines with \<newline>,
2866 // we depend on the fact that whole line is in the buffer
2867 i = 0;
2868 buf = p->lex_inbuf - 1;
2869 for (;;) {
2870 char c = buf[i];
2871 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2872 i++;
2873 }
2874
2875#if 0 // We do not protect against people with gigabyte-long names
2876 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2877 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2878 if (i > BC_MAX_STRING)
2879 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2880 }
2881#endif
2882 bc_vec_string(&p->lex_strnumbuf, i, buf);
2883
2884 // Increment the index. We minus 1 because it has already been incremented.
2885 p->lex_inbuf += i - 1;
2886
2887 //return BC_STATUS_SUCCESS;
2888}
2889
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002890IF_BC(static BC_STATUS zbc_lex_token(void);)
2891IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002892#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2893#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002894
Denys Vlasenko10bde142018-12-27 18:23:58 +01002895static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002896{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002897 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002898 BcStatus s;
2899
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002900 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002901//why?
2902// if (p->lex_last == XC_LEX_EOF)
2903// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002904
2905 // Loop until failure or we don't have whitespace. This
2906 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002907 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002908 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002909 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002910 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002911 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002912 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002913 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002914 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002915 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002916 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002917 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2918 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002919 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002920 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002921 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002922 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002923 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002924 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002925 } while (!s && p->lex == XC_LEX_WHITESPACE);
2926 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002927
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002928 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002929}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002930#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002931
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002932#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002933static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002934{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002935 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002936 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002937 RETURN_STATUS(BC_STATUS_SUCCESS);
2938}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002939#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002940
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002941static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002942{
2943 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01002944 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002945 if (s) RETURN_STATUS(s);
2946 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002947 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002948 RETURN_STATUS(s);
2949}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002950#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002951
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002952static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002953{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002954 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002955 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002956 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002957 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002958
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002959 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2960 const char *keyword8 = bc_lex_kws[i].name8;
2961 unsigned j = 0;
2962 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2963 j++;
2964 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002965 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002966 if (keyword8[j] != '\0')
2967 continue;
2968 match:
2969 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002970 if (isalnum(buf[j]) || buf[j]=='_')
2971 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002972 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01002973 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01002974 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
2975 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002976 }
2977
2978 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002979 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002980 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002981 }
2982
Denys Vlasenko10bde142018-12-27 18:23:58 +01002983 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002984 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002985
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002986 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002987 // Prevent this:
2988 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01002989 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002990 // '
2991 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01002992 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002993 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002994
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002995 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002996}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002997#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002998
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002999static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003000{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003001 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003002
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003003 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003004 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003005 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003006 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003007 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003008 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003009 }
3010 if (c == '"')
3011 break;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003012 if (c == '\n')
3013 p->lex_line++;
3014 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3015 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003016 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003017 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3018 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003019
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003020 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003021 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003022}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003023#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003024
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003025static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003026{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003027 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003028 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003029 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003030 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003031 with_and_without >>= 8; // store "with" value
3032 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003033 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003034}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003035#define parse_lex_by_checking_eq_sign(with, without) \
3036 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003038static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003039{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003040 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003041
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003042 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003043 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003044 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003045 char c;
3046
3047 p->lex_inbuf++;
3048 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003049 check_star:
3050 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003051 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003052 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003053 if (c == '/')
3054 break;
3055 goto check_star;
3056 }
3057 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003058 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003059 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003060 if (c == '\n')
3061 p->lex_line++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003062 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003063 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003064
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003065 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003066 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003067}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003068#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003069
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003070#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003071static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003072{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003073 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003074 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003075 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003076 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003077
3078 // This is the workhorse of the lexer.
3079 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003080// case '\0': // probably never reached
3081// p->lex_inbuf--;
3082// p->lex = XC_LEX_EOF;
3083// break;
3084 case '\n':
3085 p->lex_line++;
3086 p->lex = XC_LEX_NLINE;
3087 break;
3088 case '\t':
3089 case '\v':
3090 case '\f':
3091 case '\r':
3092 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003093 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003094 break;
3095 case '!':
3096 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3097 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3098 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003099 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003100 }
3101 break;
3102 case '"':
3103 s = zbc_lex_string();
3104 break;
3105 case '#':
3106 s = zbc_POSIX_does_not_allow("'#' script comments");
3107 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003108 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003109 break;
3110 case '%':
3111 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3112 break;
3113 case '&':
3114 c2 = *p->lex_inbuf;
3115 if (c2 == '&') {
3116 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3117 if (s) RETURN_STATUS(s);
3118 p->lex_inbuf++;
3119 p->lex = BC_LEX_OP_BOOL_AND;
3120 } else {
3121 p->lex = XC_LEX_INVALID;
3122 s = bc_error_bad_character('&');
3123 }
3124 break;
3125 case '(':
3126 case ')':
3127 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3128 break;
3129 case '*':
3130 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3131 break;
3132 case '+':
3133 c2 = *p->lex_inbuf;
3134 if (c2 == '+') {
3135 p->lex_inbuf++;
3136 p->lex = BC_LEX_OP_INC;
3137 } else
3138 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3139 break;
3140 case ',':
3141 p->lex = BC_LEX_COMMA;
3142 break;
3143 case '-':
3144 c2 = *p->lex_inbuf;
3145 if (c2 == '-') {
3146 p->lex_inbuf++;
3147 p->lex = BC_LEX_OP_DEC;
3148 } else
3149 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3150 break;
3151 case '.':
3152 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003153 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003154 else {
3155 p->lex = BC_LEX_KEY_LAST;
3156 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3157 }
3158 break;
3159 case '/':
3160 c2 = *p->lex_inbuf;
3161 if (c2 == '*')
3162 s = zbc_lex_comment();
3163 else
3164 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3165 break;
3166 case '0':
3167 case '1':
3168 case '2':
3169 case '3':
3170 case '4':
3171 case '5':
3172 case '6':
3173 case '7':
3174 case '8':
3175 case '9':
3176 case 'A':
3177 case 'B':
3178 case 'C':
3179 case 'D':
3180 case 'E':
3181 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003182 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003183 break;
3184 case ';':
3185 p->lex = BC_LEX_SCOLON;
3186 break;
3187 case '<':
3188 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3189 break;
3190 case '=':
3191 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3192 break;
3193 case '>':
3194 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3195 break;
3196 case '[':
3197 case ']':
3198 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3199 break;
3200 case '\\':
3201 if (*p->lex_inbuf == '\n') {
3202 p->lex = XC_LEX_WHITESPACE;
3203 p->lex_inbuf++;
3204 } else
3205 s = bc_error_bad_character(c);
3206 break;
3207 case '^':
3208 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3209 break;
3210 case 'a':
3211 case 'b':
3212 case 'c':
3213 case 'd':
3214 case 'e':
3215 case 'f':
3216 case 'g':
3217 case 'h':
3218 case 'i':
3219 case 'j':
3220 case 'k':
3221 case 'l':
3222 case 'm':
3223 case 'n':
3224 case 'o':
3225 case 'p':
3226 case 'q':
3227 case 'r':
3228 case 's':
3229 case 't':
3230 case 'u':
3231 case 'v':
3232 case 'w':
3233 case 'x':
3234 case 'y':
3235 case 'z':
3236 s = zbc_lex_identifier();
3237 break;
3238 case '{':
3239 case '}':
3240 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3241 break;
3242 case '|':
3243 c2 = *p->lex_inbuf;
3244 if (c2 == '|') {
3245 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3246 if (s) RETURN_STATUS(s);
3247 p->lex_inbuf++;
3248 p->lex = BC_LEX_OP_BOOL_OR;
3249 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003250 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003251 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003252 }
3253 break;
3254 default:
3255 p->lex = XC_LEX_INVALID;
3256 s = bc_error_bad_character(c);
3257 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 }
3259
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003260 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003261}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003262#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003263#endif // ENABLE_BC
3264
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003265#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003266static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003267{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003268 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003269 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003270 xc_lex_whitespace(); // eats whitespace (but not newline)
3271 p->lex_inbuf++; // xc_lex_name() expects this
3272 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003273 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003274 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003275 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003276 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3277 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 }
3279
Denys Vlasenko29301232018-12-11 15:29:32 +01003280 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003281}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003282#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003283
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003284static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003285{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003286 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003287 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003288
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003289 p->lex = XC_LEX_STR;
3290 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003291
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003292 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003293 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003294 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003295 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003296 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003297 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003298 if (c == '[') depth++;
3299 if (c == ']')
3300 if (--depth == 0)
3301 break;
3302 if (c == '\n')
3303 p->lex_line++;
3304 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3305 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003306 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003307 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003308 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003309
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003310 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003311 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003312}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003313#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003314
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003315#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003316static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003317{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003318 static const //BcLexType - should be this type, but narrower type saves size:
3319 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003320 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003321 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 +01003322 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3323 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003324 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003325 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003326
Denys Vlasenko10bde142018-12-27 18:23:58 +01003327 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003328 BcStatus s;
3329 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003330 size_t i;
3331
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003332 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003333 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003334 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003335 }
3336
Denys Vlasenko81293c82018-12-24 01:53:55 +01003337 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003338 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003339 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003340 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003341 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003342 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003343 }
3344
3345 // This is the workhorse of the lexer.
3346 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003347// case '\0': // probably never reached
3348// p->lex = XC_LEX_EOF;
3349// break;
3350 case '\n':
3351 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3352 // (and "case '\n':" is not just empty here)
3353 // only to allow interactive dc have a way to exit
3354 // "parse" stage of "parse,execute" loop
3355 // on <enter>, not on _next_ token (which would mean
3356 // commands are not executed on pressing <enter>).
3357 // IOW: typing "1p<enter>" should print "1" _at once_,
3358 // not after some more input.
3359 p->lex_line++;
3360 p->lex = XC_LEX_NLINE;
3361 break;
3362 case '\t':
3363 case '\v':
3364 case '\f':
3365 case '\r':
3366 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003367 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003368 break;
3369 case '!':
3370 c2 = *p->lex_inbuf;
3371 if (c2 == '=')
3372 p->lex = XC_LEX_OP_REL_NE;
3373 else if (c2 == '<')
3374 p->lex = XC_LEX_OP_REL_LE;
3375 else if (c2 == '>')
3376 p->lex = XC_LEX_OP_REL_GE;
3377 else
3378 RETURN_STATUS(bc_error_bad_character(c));
3379 p->lex_inbuf++;
3380 break;
3381 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003382 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003383 break;
3384 case '.':
3385 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003386 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003387 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003388 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003389 break;
3390 case '0':
3391 case '1':
3392 case '2':
3393 case '3':
3394 case '4':
3395 case '5':
3396 case '6':
3397 case '7':
3398 case '8':
3399 case '9':
3400 case 'A':
3401 case 'B':
3402 case 'C':
3403 case 'D':
3404 case 'E':
3405 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003406 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003407 break;
3408 case '[':
3409 s = zdc_lex_string();
3410 break;
3411 default:
3412 p->lex = XC_LEX_INVALID;
3413 s = bc_error_bad_character(c);
3414 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003415 }
3416
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003417 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003418}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003419#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003420#endif // ENABLE_DC
3421
Denys Vlasenko10bde142018-12-27 18:23:58 +01003422static void xc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003423{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003424 BcVec *code = &G.prs.func->code;
3425 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
3426 bc_vec_pushByte(code, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003427}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003428
Denys Vlasenko10bde142018-12-27 18:23:58 +01003429static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003430{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003431#if 1
3432 BcVec *code = &G.prs.func->code;
3433 size_t pos = code->len;
3434 size_t len = strlen(name) + 1;
3435
3436 bc_vec_expand(code, pos + len);
3437 strcpy(code->v + pos, name);
3438 code->len = pos + len;
3439#else
3440 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003441 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003442 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003443 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003444#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003445}
3446
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003447// Indexes < 0xfc are encoded verbatim, else first byte is
3448// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3449// followed by that many bytes, lsb first.
3450// (The above describes 32-bit case).
3451#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3452
Denys Vlasenko10bde142018-12-27 18:23:58 +01003453static void xc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003454{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003455 size_t mask;
3456 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003457
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003458 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003459 if (idx < SMALL_INDEX_LIMIT) {
3460 goto push_idx;
3461 }
3462
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003463 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3464 amt = sizeof(idx);
3465 do {
3466 if (idx & mask) break;
3467 mask >>= 8;
3468 amt--;
3469 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003470
Denys Vlasenko10bde142018-12-27 18:23:58 +01003471 xc_parse_push(SMALL_INDEX_LIMIT + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003472
3473 while (idx != 0) {
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003474 push_idx:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003475 xc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003476 idx >>= 8;
3477 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003478}
3479
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003480#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003481static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003482{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003483 xc_parse_push(BC_INST_JUMP);
3484 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003485}
3486
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003487static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003488{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003489 xc_parse_push(BC_INST_JUMP_ZERO);
3490 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003491}
3492
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003493static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003494{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003495 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003496 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003497
Denys Vlasenko10bde142018-12-27 18:23:58 +01003498 xc_parse_push(XC_INST_STR);
3499 xc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003500 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003501
Denys Vlasenko10bde142018-12-27 18:23:58 +01003502 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003503}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003504#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003505#endif
3506
Denys Vlasenko10bde142018-12-27 18:23:58 +01003507static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003508{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003509 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003510 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003511#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003512 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003513#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003514 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003515#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003516 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003517#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01003518 xc_parse_push(XC_INST_NUM);
3519 xc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003520}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003521
Denys Vlasenko10bde142018-12-27 18:23:58 +01003522static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003523{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003524 G.prs.func = xc_program_func(G.prs.fidx);
3525 G.prs.lex_inbuf = text;
3526 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3527 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003528}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003529#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003530
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003531// Called when parsing or execution detects a failure,
3532// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003533static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003534{
3535 BcFunc *f;
3536 BcInstPtr *ip;
3537
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003538 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003539 bc_vec_pop_all(&G.prog.results);
3540
Denys Vlasenko10bde142018-12-27 18:23:58 +01003541 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003542 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003543 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003544}
3545
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003546// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003547// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003548static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003549{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003550 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003551 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003552 bc_func_free(p->func);
3553 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003554
Denys Vlasenko65e10462018-12-19 15:13:14 +01003555 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003556 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003557 }
3558
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003559 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003560 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003561
Denys Vlasenko503faf92018-12-20 16:24:18 +01003562 IF_BC(bc_vec_pop_all(&p->exits);)
3563 IF_BC(bc_vec_pop_all(&p->conds);)
3564 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003565
Denys Vlasenko10bde142018-12-27 18:23:58 +01003566 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003567}
3568
Denys Vlasenko10bde142018-12-27 18:23:58 +01003569static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003570{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003571 IF_BC(bc_vec_free(&G.prs.exits);)
3572 IF_BC(bc_vec_free(&G.prs.conds);)
3573 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003574 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003575}
3576
Denys Vlasenko10bde142018-12-27 18:23:58 +01003577static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003578{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003579 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003580 memset(p, 0, sizeof(BcParse));
3581
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003582 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003583 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3584 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3585 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003586
Denys Vlasenkofa210792018-12-19 19:35:40 +01003587 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003588 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003589}
3590
Denys Vlasenko10bde142018-12-27 18:23:58 +01003591static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003592{
Denys Vlasenko04715442018-12-21 00:10:26 +01003593 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003594 BcFunc f;
3595 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003596 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003597 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003598 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003599}
3600
3601#if ENABLE_BC
3602
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003603// Note: takes ownership of 'name' (must be malloced)
3604static size_t bc_program_addFunc(char *name)
3605{
3606 size_t idx;
3607 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003608 int inserted;
3609
3610 entry.name = name;
3611 entry.idx = G.prog.fns.len;
3612
3613 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3614 if (!inserted) free(name);
3615
3616 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3617 idx = entry_ptr->idx;
3618
3619 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003620 // There is already a function with this name.
3621 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003622 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003623 bc_func_free(func);
3624 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003625 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003626 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003627 }
3628
3629 return idx;
3630}
3631
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003632#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003633// We can calculate the conversion between tokens and exprs by subtracting the
3634// position of the first operator in the lex enum and adding the position of the
3635// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003636#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003637
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003638static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003639
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003640static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003641{
3642 BcStatus s;
3643
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003644 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003645 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3646 RETURN_STATUS(bc_error("empty expression"));
3647 RETURN_STATUS(s);
3648}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003649#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003650
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003651static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003652#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003653
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003654static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003655{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003656 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003657}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003658#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003659
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003660static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003661{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003662 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003663 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3664 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003665 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003666 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003667 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003668 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003669
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003670 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003671}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003672#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003673
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003674static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003675{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003676 BcParse *p = &G.prs;
Denys Vlasenko69560f42018-12-24 14:14:23 +01003677 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3678 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003679
3680 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003681 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003682 if (t == BC_LEX_LPAREN) break;
3683
Denys Vlasenko69560f42018-12-24 14:14:23 +01003684 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003685 if (l >= r && (l != r || !left)) break;
3686
Denys Vlasenko10bde142018-12-27 18:23:58 +01003687 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003688 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003689 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003690 }
3691
3692 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003693}
3694
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003695static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003696{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003697 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003698 BcLexType top;
3699
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003700 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003701 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003702 top = BC_PARSE_TOP_OP(p);
3703
3704 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003705 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003706
3707 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003708 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003709
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003710 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003711 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003712 top = BC_PARSE_TOP_OP(p);
3713 }
3714
3715 bc_vec_pop(&p->ops);
3716
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003717 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003718}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003719#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003720
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003721static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003722{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003723 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003724 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003725 size_t nparams;
3726
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003727 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003728 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3729
Denys Vlasenko10bde142018-12-27 18:23:58 +01003730 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003731 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003732
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003733 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003734 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003735 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003736 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003737 if (s) RETURN_STATUS(s);
3738 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003739 if (p->lex != BC_LEX_COMMA) {
3740 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003741 break;
3742 RETURN_STATUS(bc_error_bad_token());
3743 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003744 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003745 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003746 }
3747 }
3748
Denys Vlasenko10bde142018-12-27 18:23:58 +01003749 xc_parse_push(BC_INST_CALL);
3750 xc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003751
Denys Vlasenko26819db2018-12-12 16:08:46 +01003752 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003753}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003754#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003755
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003756// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003757static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003758{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003759 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 BcStatus s;
3761 BcId entry, *entry_ptr;
3762 size_t idx;
3763
3764 entry.name = name;
3765
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003766 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003767 if (s) goto err;
3768
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003769 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003770 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003771 goto err;
3772 }
3773
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003774 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003775
3776 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003777 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003778 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003779 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003780 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003781 free(name);
3782
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003783 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003784 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003785
Denys Vlasenko10bde142018-12-27 18:23:58 +01003786 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003787 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003788 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003789 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003790}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003791#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003792
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003793static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003794{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003795 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003796 BcStatus s;
3797 char *name;
3798
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003799 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003800 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003801 if (s) goto err;
3802
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003803 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003804 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003805 if (s) goto err;
3806
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003807 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003808 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003809 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003810 goto err;
3811 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003812 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003813 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003814 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003815 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003816 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 if (s) goto err;
3818 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003819 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003820 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003821 xc_parse_push(*type);
3822 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003823 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003824 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003825 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003826 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003827 goto err;
3828 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003829 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003830 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003831 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003832 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003833 xc_parse_push(XC_INST_VAR);
3834 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003835 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003836 }
3837
Denys Vlasenko26819db2018-12-12 16:08:46 +01003838 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003839 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003840 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003841 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003842}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003843#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003844
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003845static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003846{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003847 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003848 BcStatus s;
3849
Denys Vlasenko10bde142018-12-27 18:23:58 +01003850 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003851 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003852 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003853
Denys Vlasenko10bde142018-12-27 18:23:58 +01003854 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003855 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003856 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003857
Denys Vlasenko10bde142018-12-27 18:23:58 +01003858 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003859
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003860 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003861}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003862#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003863
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003864static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003865{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003866 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003867 BcStatus s;
3868
Denys Vlasenko10bde142018-12-27 18:23:58 +01003869 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003870 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003871 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003872
3873 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3874
Denys Vlasenko10bde142018-12-27 18:23:58 +01003875 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003876 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003877
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003878 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003879 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003880
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003881 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003882
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003883 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003884 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003885
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003886 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003887}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003888#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003889
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003890static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003891{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003892 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003893 BcStatus s;
3894
Denys Vlasenko10bde142018-12-27 18:23:58 +01003895 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003896 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003897
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003898 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003899 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003900 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003901 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003902 }
3903
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003904 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3906
Denys Vlasenko10bde142018-12-27 18:23:58 +01003907 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003908 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003909
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003910 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003911 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003912 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003913 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01003914 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003915
Denys Vlasenko10bde142018-12-27 18:23:58 +01003916 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003917}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003918#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003919
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003920static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
3921 size_t *nexprs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003922{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003923 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003924 BcStatus s;
3925 BcLexType type;
3926 char inst;
3927 BcInst etype = *prev;
3928
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003929 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3930 || etype == XC_INST_SCALE || etype == BC_INST_LAST
3931 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003932 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003933 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003934 xc_parse_push(inst);
3935 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003936 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003937 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003938 *paren_expr = true;
3939
Denys Vlasenko10bde142018-12-27 18:23:58 +01003940 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003941 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003942 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06003943
3944 // Because we parse the next part of the expression
3945 // right here, we need to increment this.
3946 *nexprs = *nexprs + 1;
3947
3948 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003949 case XC_LEX_NAME:
3950 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
3951 break;
3952 case BC_LEX_KEY_IBASE:
3953 case BC_LEX_KEY_LAST:
3954 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003955 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
3956 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003957 break;
3958 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003959 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003960 if (s) RETURN_STATUS(s);
3961 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003962 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003963 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01003964 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003965 break;
3966 default:
3967 s = bc_error_bad_token();
3968 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003969 }
3970
Denys Vlasenko10bde142018-12-27 18:23:58 +01003971 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06003972 }
3973
Denys Vlasenko26819db2018-12-12 16:08:46 +01003974 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003975}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003976#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003977
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003978#if 0
3979#define BC_PARSE_LEAF(p, rparen) \
3980 ((rparen) \
3981 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
3982 || (p) == BC_INST_INC_POST \
3983 || (p) == BC_INST_DEC_POST \
3984 )
3985#else
3986static int ok_in_expr(BcInst p)
3987{
3988 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
3989 || p == BC_INST_INC_POST
3990 || p == BC_INST_DEC_POST
3991 ;
3992}
3993#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
3994#endif
3995
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003996static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
3997 bool rparen, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003998{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003999 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004000 BcStatus s;
4001 BcLexType type;
4002 BcInst etype = *prev;
4003
Denys Vlasenko10bde142018-12-27 18:23:58 +01004004 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004005 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004007 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004008 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004009
4010 // We can just push onto the op stack because this is the largest
4011 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004012 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004013 bc_vec_push(&p->ops, &type);
4014 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004015 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004016
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004017 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004018}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004019#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004020
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004021static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004022{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004023 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004024 BcStatus s;
4025 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004026
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004027 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004028 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004029 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004030 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004031 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004032 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004033 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004034 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004035 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004036 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004037 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004038 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004039 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004040 }
4041
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004042 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004043}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004044#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004045
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004046static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004047{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004048 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004049 BcStatus s;
4050 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004051
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004052 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004053 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004054 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004056 t = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004057 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004058 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004059 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004060 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004061 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004062 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004063 xc_parse_push(BC_INST_RET0);
4064 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004065 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004066 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004067
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004068 if (!paren || p->lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004069 s = zbc_POSIX_requires("parentheses around return expressions");
4070 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004071 }
4072
Denys Vlasenko10bde142018-12-27 18:23:58 +01004073 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004074 }
4075
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004076 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004077 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004078}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004079#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004080
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004081static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004082{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004083 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004084 size_t *label = bc_vec_item(&p->func->labels, idx);
4085 *label = p->func->code.len;
4086}
4087
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004088static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004089{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004090 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004091 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004092 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004093
4094 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004095 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004096 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004097 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004098
Denys Vlasenko10bde142018-12-27 18:23:58 +01004099 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004100 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004101 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004102 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004103 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004104
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004105 // Encode "if zero, jump to ..."
4106 // Pushed value (destination of the jump) is uninitialized,
4107 // will be rewritten to be address of "end of if()" or of "else".
4108 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004109 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004110
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004111 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004112 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004113
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004114 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4115 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004116 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004117
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004118 // Encode "after then_stmt, jump to end of if()"
4119 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4120 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004121 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004122
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004123 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 +01004124 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004125
Denys Vlasenko15850832018-12-16 21:40:54 +01004126 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004127
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004128 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004129 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004130 }
4131
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004132 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 +01004133 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004134
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004135 dbg_lex_done("%s:%d done", __func__, __LINE__);
4136 RETURN_STATUS(s);
4137}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004138#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004139
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004140static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004141{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004142 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004143 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004144 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004145 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004146
Denys Vlasenko10bde142018-12-27 18:23:58 +01004147 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004148 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004149 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004150 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004151 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004152
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004153 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004154 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004155 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004157 bc_vec_push(&p->exits, &ip_idx);
4158 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004159
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004160 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004161 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004162 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004163
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004164 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004165
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004166 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004167 if (s) RETURN_STATUS(s);
4168
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004169 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004170 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004171
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004172 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004173 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004174
4175 bc_vec_pop(&p->exits);
4176 bc_vec_pop(&p->conds);
4177
4178 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004179}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004180#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004181
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004182static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004183{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004184 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004185 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004186 size_t cond_idx, exit_idx, body_idx, update_idx;
4187
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004188 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004189 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004190 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004191 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004192 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004193 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004194
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004195 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004196 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004197 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004198 if (s) RETURN_STATUS(s);
4199 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004200 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4201 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004202 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004203
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004204 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004205 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004206 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004207
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004208 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004209 update_idx = cond_idx + 1;
4210 body_idx = update_idx + 1;
4211 exit_idx = body_idx + 1;
4212
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004213 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004214 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004215 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004216 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004217 // This is safe to set because the current token is a semicolon,
4218 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004219 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004220 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004221 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004222 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004223 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004224
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004225 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004226
Denys Vlasenko10bde142018-12-27 18:23:58 +01004227 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004228 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004229
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004230 bc_parse_pushJUMP_ZERO(exit_idx);
4231 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004232
Gavin Howard01055ba2018-11-03 11:00:21 -06004233 bc_vec_push(&p->conds, &update_idx);
4234 bc_vec_push(&p->func->labels, &p->func->code.len);
4235
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004236 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004237 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004238 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004239 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004240 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004241 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004242 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4243 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004244 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004245
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004246 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004247 bc_vec_push(&p->func->labels, &p->func->code.len);
4248
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004249 bc_vec_push(&p->exits, &exit_idx);
4250 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004251
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004252 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004253 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004254
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004255 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004256 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004257
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004258 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004259 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004260
4261 bc_vec_pop(&p->exits);
4262 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004263
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004264 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004265}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004266#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004267
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004268static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004269{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004270 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004271 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
Gavin Howard01055ba2018-11-03 11:00:21 -06004273 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004274 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4275 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004276 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004277 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004278 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004279 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004280 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Denys Vlasenko10bde142018-12-27 18:23:58 +01004282 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004283}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004284#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004285
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004286static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4287{
4288 BcId *autoid;
4289 BcId a;
4290 size_t i;
4291
4292 autoid = (void*)f->autos.v;
4293 for (i = 0; i < f->autos.len; i++, autoid++) {
4294 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004295 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004296 }
4297
4298 a.idx = var;
4299 a.name = name;
4300
4301 bc_vec_push(&f->autos, &a);
4302
4303 RETURN_STATUS(BC_STATUS_SUCCESS);
4304}
4305#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4306
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004307static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004308{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004309 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004310 BcStatus s;
4311 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004312 char *name;
4313
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004314 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004315 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004316 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004317 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004318 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004319
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004320 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004321 p->fidx = bc_program_addFunc(name);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004322 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004323
Denys Vlasenko10bde142018-12-27 18:23:58 +01004324 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004325 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004326 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004327 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004328 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004329 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004330
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004331 while (p->lex != BC_LEX_RPAREN) {
4332 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004333 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004334
4335 ++p->func->nparams;
4336
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004337 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004338 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004339 if (s) goto err;
4340
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004341 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004342
4343 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004344 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004345 if (s) goto err;
4346
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004347 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004348 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004349 goto err;
4350 }
4351
Denys Vlasenko10bde142018-12-27 18:23:58 +01004352 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004353 if (s) goto err;
4354 }
4355
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004356 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004357 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004358 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004359 if (s) goto err;
4360 }
4361
Denys Vlasenko29301232018-12-11 15:29:32 +01004362 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004363 if (s) goto err;
4364 }
4365
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004366 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004367
Denys Vlasenko10bde142018-12-27 18:23:58 +01004368 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004369 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004370
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004371 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004372 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004373 if (s) RETURN_STATUS(s);
4374 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004375
Denys Vlasenko202dd192018-12-16 17:30:35 +01004376 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004377 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004378 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004379 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004380 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004381 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004382
4383 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004384 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004385 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004386 if (s) RETURN_STATUS(s);
4387
Denys Vlasenko10bde142018-12-27 18:23:58 +01004388 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004389
4390 // Subsequent code generation is into main program
4391 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004392 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004393
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004394 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004395 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004396 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004397 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004398 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004399 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004400}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004401#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004402
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004403static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004404{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004405 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004406 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004407 char *name;
4408
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004409 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004410 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004411 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004412
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004413 for (;;) {
4414 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004415
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004416 if (p->lex != XC_LEX_NAME)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004417 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4418
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004419 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004420 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004421 if (s) goto err;
4422
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004423 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004424 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004425 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004426 if (s) goto err;
4427
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004428 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004429 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004430 goto err;
4431 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004432 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004433 if (s) goto err;
4434 }
4435
Denys Vlasenko29301232018-12-11 15:29:32 +01004436 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004437 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004438
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004439 if (p->lex == XC_LEX_NLINE
4440 || p->lex == BC_LEX_SCOLON
4441 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004442 ) {
4443 break;
4444 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004445 if (p->lex != BC_LEX_COMMA)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004446 RETURN_STATUS(bc_error("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004447 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004448 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004449 }
4450
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004451 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004452 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004453 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004455 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004456 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004457}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004458#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004459
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004460#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004461static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004462{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004463 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004464 BcStatus s = BC_STATUS_SUCCESS;
4465
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004466 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004467
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004468 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004469 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004470 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004471 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004472 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004473 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004474 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004475 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004476
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004477 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004478 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4479 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004480 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004481 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004482 } while (p->lex == XC_LEX_NLINE);
4483 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004484 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004485 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004486 if (s) RETURN_STATUS(s);
4487 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004488 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004489 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004490 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004491 if (s) RETURN_STATUS(s);
4492 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004493 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004494 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4495 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004496 }
4497
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004498 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4499 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004500 case XC_LEX_OP_MINUS:
4501 case BC_LEX_OP_INC:
4502 case BC_LEX_OP_DEC:
4503 case BC_LEX_OP_BOOL_NOT:
4504 case BC_LEX_LPAREN:
4505 case XC_LEX_NAME:
4506 case XC_LEX_NUMBER:
4507 case BC_LEX_KEY_IBASE:
4508 case BC_LEX_KEY_LAST:
4509 case BC_LEX_KEY_LENGTH:
4510 case BC_LEX_KEY_OBASE:
4511 case BC_LEX_KEY_READ:
4512 case BC_LEX_KEY_SCALE:
4513 case BC_LEX_KEY_SQRT:
4514 s = zbc_parse_expr(BC_PARSE_PRINT);
4515 break;
4516 case XC_LEX_STR:
4517 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004518 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004519 break;
4520 case BC_LEX_KEY_BREAK:
4521 case BC_LEX_KEY_CONTINUE:
4522 s = zbc_parse_break_or_continue(p->lex);
4523 break;
4524 case BC_LEX_KEY_FOR:
4525 s = zbc_parse_for();
4526 break;
4527 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004528 xc_parse_push(BC_INST_HALT);
4529 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004530 break;
4531 case BC_LEX_KEY_IF:
4532 s = zbc_parse_if();
4533 break;
4534 case BC_LEX_KEY_LIMITS:
4535 // "limits" is a compile-time command,
4536 // the output is produced at _parse time_.
4537 printf(
4538 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4539 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4540 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4541 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4542 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4543 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4544 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4545 "Number of vars = "BC_MAX_VARS_STR "\n"
4546 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004547 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004548 break;
4549 case BC_LEX_KEY_PRINT:
4550 s = zbc_parse_print();
4551 break;
4552 case BC_LEX_KEY_QUIT:
4553 // "quit" is a compile-time command. For example,
4554 // "if (0 == 1) quit" terminates when parsing the statement,
4555 // not when it is executed
4556 QUIT_OR_RETURN_TO_MAIN;
4557 case BC_LEX_KEY_RETURN:
4558 if (!p->in_funcdef)
4559 RETURN_STATUS(bc_error("'return' not in a function"));
4560 s = zbc_parse_return();
4561 break;
4562 case BC_LEX_KEY_WHILE:
4563 s = zbc_parse_while();
4564 break;
4565 default:
4566 s = bc_error_bad_token();
4567 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 }
4569
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004570 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004571 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004572}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004573#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004574
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004575static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004576{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004577 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 BcStatus s;
4579
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004580 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004581 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004582 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004583 else if (p->lex == BC_LEX_KEY_DEFINE) {
4584 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004585 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004586 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004587 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004588 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004589 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004590
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004591 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004592 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004593}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004594#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004595
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004596// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004597static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004598{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004599 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004600 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004601 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004602 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004603 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004604
Denys Vlasenko17df8822018-12-14 23:00:24 +01004605 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004606 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004607 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004608 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 bin_last = true;
4610
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004611 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004612 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004613 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004614 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004615
4616 if (!lex_allowed_in_bc_expr(t))
4617 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004618
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004619 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004620 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004621 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004623 case BC_LEX_OP_INC:
4624 case BC_LEX_OP_DEC:
4625 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
4626 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
4627 rprn = bin_last = false;
4628 //get_token = false; - already is
4629 break;
4630 case XC_LEX_OP_MINUS:
4631 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
4632 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
4633 rprn = false;
4634 //get_token = false; - already is
4635 bin_last = (prev == XC_INST_MINUS);
4636 break;
4637 case BC_LEX_OP_ASSIGN_POWER:
4638 case BC_LEX_OP_ASSIGN_MULTIPLY:
4639 case BC_LEX_OP_ASSIGN_DIVIDE:
4640 case BC_LEX_OP_ASSIGN_MODULUS:
4641 case BC_LEX_OP_ASSIGN_PLUS:
4642 case BC_LEX_OP_ASSIGN_MINUS:
4643 case BC_LEX_OP_ASSIGN:
4644 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4645 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4646 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4647 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4648 ) {
4649 return bc_error("bad assignment:"
4650 " left side must be variable"
4651 " or array element"
4652 ); // note: shared string
4653 }
4654 // Fallthrough.
4655 case XC_LEX_OP_POWER:
4656 case XC_LEX_OP_MULTIPLY:
4657 case XC_LEX_OP_DIVIDE:
4658 case XC_LEX_OP_MODULUS:
4659 case XC_LEX_OP_PLUS:
4660 case XC_LEX_OP_REL_EQ:
4661 case XC_LEX_OP_REL_LE:
4662 case XC_LEX_OP_REL_GE:
4663 case XC_LEX_OP_REL_NE:
4664 case XC_LEX_OP_REL_LT:
4665 case XC_LEX_OP_REL_GT:
4666 case BC_LEX_OP_BOOL_NOT:
4667 case BC_LEX_OP_BOOL_OR:
4668 case BC_LEX_OP_BOOL_AND:
4669 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
4670 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4671 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
4672 ) {
4673 return bc_error_bad_expression();
4674 }
4675 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4676 prev = BC_TOKEN_2_INST(t);
4677 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004678 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004679 rprn = false;
4680 //get_token = false; - already is
4681 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4682 break;
4683 case BC_LEX_LPAREN:
4684 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
4685 if (BC_PARSE_LEAF(prev, rprn))
4686 return bc_error_bad_expression();
4687 bc_vec_push(&p->ops, &t);
4688 nparens++;
4689 get_token = true;
4690 paren_expr = false;
4691 rprn = bin_last = false;
4692 break;
4693 case BC_LEX_RPAREN:
4694 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
4695 if (bin_last || prev == XC_INST_BOOL_NOT)
4696 return bc_error_bad_expression();
4697 if (nparens == 0) {
4698 goto exit_loop;
4699 }
4700 if (!paren_expr) {
4701 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
4702 return BC_STATUS_PARSE_EMPTY_EXP;
4703 }
4704 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4705 nparens--;
4706 get_token = true;
4707 paren_expr = rprn = true;
4708 bin_last = false;
4709 break;
4710 case XC_LEX_NAME:
4711 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
4712 if (BC_PARSE_LEAF(prev, rprn))
4713 return bc_error_bad_expression();
4714 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
4715 paren_expr = true;
4716 rprn = bin_last = false;
4717 //get_token = false; - already is
4718 nexprs++;
4719 break;
4720 case XC_LEX_NUMBER:
4721 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4722 if (BC_PARSE_LEAF(prev, rprn))
4723 return bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004724 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004725 prev = XC_INST_NUM;
4726 get_token = true;
4727 paren_expr = true;
4728 rprn = bin_last = false;
4729 nexprs++;
4730 break;
4731 case BC_LEX_KEY_IBASE:
4732 case BC_LEX_KEY_LAST:
4733 case BC_LEX_KEY_OBASE:
4734 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
4735 if (BC_PARSE_LEAF(prev, rprn))
4736 return bc_error_bad_expression();
4737 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004738 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004739 get_token = true;
4740 paren_expr = true;
4741 rprn = bin_last = false;
4742 nexprs++;
4743 break;
4744 case BC_LEX_KEY_LENGTH:
4745 case BC_LEX_KEY_SQRT:
4746 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
4747 if (BC_PARSE_LEAF(prev, rprn))
4748 return bc_error_bad_expression();
4749 s = zbc_parse_builtin(t, flags, &prev);
4750 get_token = true;
4751 paren_expr = true;
4752 rprn = bin_last = false;
4753 nexprs++;
4754 break;
4755 case BC_LEX_KEY_READ:
4756 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
4757 if (BC_PARSE_LEAF(prev, rprn))
4758 return bc_error_bad_expression();
4759 s = zbc_parse_read();
4760 prev = XC_INST_READ;
4761 get_token = true;
4762 paren_expr = true;
4763 rprn = bin_last = false;
4764 nexprs++;
4765 break;
4766 case BC_LEX_KEY_SCALE:
4767 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
4768 if (BC_PARSE_LEAF(prev, rprn))
4769 return bc_error_bad_expression();
4770 s = zbc_parse_scale(&prev, flags);
4771 prev = XC_INST_SCALE;
4772 //get_token = false; - already is
4773 paren_expr = true;
4774 rprn = bin_last = false;
4775 nexprs++;
4776 break;
4777 default:
4778 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 }
4780
Denys Vlasenkobb116032018-12-25 01:16:37 +01004781 if (s || G_interrupt) // error, or ^C: stop parsing
4782 return BC_STATUS_FAILURE;
4783 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004784 s = zxc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004785 if (s) return s;
4786 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004788 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004789
4790 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004791 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004792 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004793
4794 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004795 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004796
Denys Vlasenko10bde142018-12-27 18:23:58 +01004797 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004798
Denys Vlasenko69560f42018-12-24 14:14:23 +01004799 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004800 bc_vec_pop(&p->ops);
4801 }
4802
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004803 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004804 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004805
Gavin Howard01055ba2018-11-03 11:00:21 -06004806 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004807 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004808 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4809 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004810 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004811 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004812 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4813 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004814 }
4815
4816 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004817 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004818 xc_parse_push(XC_INST_PRINT);
4819 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004820 }
4821
Denys Vlasenko17df8822018-12-14 23:00:24 +01004822 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004823 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004824}
4825
Gavin Howard01055ba2018-11-03 11:00:21 -06004826#endif // ENABLE_BC
4827
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004828#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004829
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004830static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004831{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004832 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004833 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004834
Denys Vlasenko10bde142018-12-27 18:23:58 +01004835 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004836 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004837 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004838
Denys Vlasenko10bde142018-12-27 18:23:58 +01004839 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004840
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004841 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004842}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004843#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004844
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004845static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004846{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004847 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004848 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004849 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004850
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004851 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004852
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004853 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004854 xc_parse_push(XC_INST_STR);
4855 xc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004856 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004857
4858 // Explanation needed here
Denys Vlasenko10bde142018-12-27 18:23:58 +01004859 xc_program_add_fn();
4860 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004861
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004862 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004863}
4864
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004865static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004866{
4867 BcStatus s;
4868
Denys Vlasenko10bde142018-12-27 18:23:58 +01004869 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004870 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004871 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004872 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004873 }
4874
4875 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004876 xc_parse_push(DC_INST_SWAP);
4877 xc_parse_push(XC_INST_ASSIGN);
4878 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004879 }
4880
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004881 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004882}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004883#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004884
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004885static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004886{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004887 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004888 BcStatus s;
4889
Denys Vlasenko10bde142018-12-27 18:23:58 +01004890 xc_parse_push(inst);
4891 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004892
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004893 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004894 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004895
Denys Vlasenko10bde142018-12-27 18:23:58 +01004896 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004897 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004898
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004899 // Note that 'else' part can not be on the next line:
4900 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4901 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004902 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004903 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004904 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004905 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004906 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004907 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004908 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004909
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004910 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004911}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004912#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004913
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004914static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004915{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004916 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004917 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004918 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004919
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004920 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004921 s = BC_STATUS_SUCCESS;
4922 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004923 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004924 case XC_LEX_OP_REL_EQ:
4925 case XC_LEX_OP_REL_LE:
4926 case XC_LEX_OP_REL_GE:
4927 case XC_LEX_OP_REL_NE:
4928 case XC_LEX_OP_REL_LT:
4929 case XC_LEX_OP_REL_GT:
4930 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
4931 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
4932 get_token = false;
4933 break;
4934 case DC_LEX_SCOLON:
4935 case DC_LEX_COLON:
4936 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
4937 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
4938 break;
4939 case XC_LEX_STR:
4940 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
4941 dc_parse_string();
4942 break;
4943 case XC_LEX_NEG:
4944 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004945 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004946 if (s) RETURN_STATUS(s);
4947 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004948 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004949 xc_parse_pushNUM();
4950 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004951 break;
4952 case XC_LEX_NUMBER:
4953 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004954 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004955 break;
4956 case DC_LEX_READ:
4957 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004958 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004959 break;
4960 case DC_LEX_OP_ASSIGN:
4961 case DC_LEX_STORE_PUSH:
4962 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
4963 assign = (t == DC_LEX_OP_ASSIGN);
4964 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
4965 s = zdc_parse_mem(inst, true, assign);
4966 break;
4967 case DC_LEX_LOAD:
4968 case DC_LEX_LOAD_POP:
4969 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
4970 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
4971 s = zdc_parse_mem(inst, true, false);
4972 break;
4973 case DC_LEX_STORE_IBASE:
4974 case DC_LEX_STORE_SCALE:
4975 case DC_LEX_STORE_OBASE:
4976 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
4977 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
4978 s = zdc_parse_mem(inst, false, true);
4979 break;
4980 default:
4981 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4982 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004983 }
4984
Denys Vlasenko10bde142018-12-27 18:23:58 +01004985 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004986
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004987 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004988 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004989}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004990#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004991
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004992static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004993{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004994 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004995 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004996
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004997 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004998 if (i >= 0) {
4999 BcInst inst = dc_LEX_to_INST[i];
5000 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005001 xc_parse_push(inst);
5002 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005003 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005004 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005005 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005006}
5007#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5008
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005009static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005010{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005011 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005012 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5013 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005014 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005016 }
5017
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005018 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005019 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005020}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005021#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005022
Gavin Howard01055ba2018-11-03 11:00:21 -06005023#endif // ENABLE_DC
5024
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005025//
5026// Execution engine
5027//
5028
5029#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5030#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5031
Denys Vlasenko10bde142018-12-27 18:23:58 +01005032static BcVec* xc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005033{
Gavin Howard01055ba2018-11-03 11:00:21 -06005034 BcId e, *ptr;
5035 BcVec *v, *map;
5036 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005037 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005038
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005039 v = var ? &G.prog.vars : &G.prog.arrs;
5040 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005041
5042 e.name = id;
5043 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005044 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005045
5046 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005047 BcVec v2;
5048 bc_array_init(&v2, var);
5049 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005050 }
5051
5052 ptr = bc_vec_item(map, i);
5053 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005054 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005055}
5056
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005057// 'num' need not be initialized on entry
Denys Vlasenko10bde142018-12-27 18:23:58 +01005058static BC_STATUS zxc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005059{
Gavin Howard01055ba2018-11-03 11:00:21 -06005060 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005061 case XC_RESULT_STR:
5062 case XC_RESULT_TEMP:
5063 case XC_RESULT_IBASE:
5064 case XC_RESULT_SCALE:
5065 case XC_RESULT_OBASE:
5066 *num = &r->d.n;
5067 break;
5068 case XC_RESULT_CONSTANT: {
5069 BcStatus s;
5070 char *str;
5071 unsigned base_t;
5072 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005073
Denys Vlasenko10bde142018-12-27 18:23:58 +01005074 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005075 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005076
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005077 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005078
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005079 hex = hex && len == 1;
5080 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005081 s = zxc_num_parse(&r->d.n, str, base_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005082 if (s) {
5083 bc_num_free(&r->d.n);
5084 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005085 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005086 *num = &r->d.n;
5087 r->t = XC_RESULT_TEMP;
5088 break;
5089 }
5090 case XC_RESULT_VAR:
5091 case XC_RESULT_ARRAY:
5092 case XC_RESULT_ARRAY_ELEM: {
5093 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005094
Denys Vlasenko10bde142018-12-27 18:23:58 +01005095 v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005096
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005097 if (r->t == XC_RESULT_ARRAY_ELEM) {
5098 v = bc_vec_top(v);
5099 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5100 *num = bc_vec_item(v, r->d.id.idx);
5101 } else
5102 *num = bc_vec_top(v);
5103 break;
5104 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005105#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005106 case BC_RESULT_LAST:
5107 *num = &G.prog.last;
5108 break;
5109 case BC_RESULT_ONE:
5110 *num = &G.prog.one;
5111 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005112#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005113#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005114 default:
5115 // Testing the theory that dc does not reach LAST/ONE
5116 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005117#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005118 }
5119
Denys Vlasenko29301232018-12-11 15:29:32 +01005120 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005121}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005122#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005123
Denys Vlasenko10bde142018-12-27 18:23:58 +01005124static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005125 BcResult **r, BcNum **rn, bool assign)
5126{
5127 BcStatus s;
5128 bool hex;
5129 BcResultType lt, rt;
5130
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005131 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005132 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005133
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005134 *r = bc_vec_item_rev(&G.prog.results, 0);
5135 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005136
5137 lt = (*l)->t;
5138 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005139 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005140
Denys Vlasenko10bde142018-12-27 18:23:58 +01005141 s = zxc_program_num(*l, ln, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005142 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005143 s = zxc_program_num(*r, rn, hex);
Denys Vlasenko29301232018-12-11 15:29:32 +01005144 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005145
5146 // We run this again under these conditions in case any vector has been
5147 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005148 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005149 s = zxc_program_num(*l, ln, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005150 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005151 }
5152
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005153 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005154 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005155 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005156 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005157
Denys Vlasenko29301232018-12-11 15:29:32 +01005158 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005159}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005160#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005161
Denys Vlasenko10bde142018-12-27 18:23:58 +01005162static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005163{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005164 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005165 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005166 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005167}
5168
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005169// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005170static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005171{
5172 BcStatus s;
5173
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005174 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005175 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005176 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005177
Denys Vlasenko10bde142018-12-27 18:23:58 +01005178 s = zxc_program_num(*r, n, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005179 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005180
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005181 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005182 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005183
Denys Vlasenko29301232018-12-11 15:29:32 +01005184 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005185}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005186#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005187
Denys Vlasenko10bde142018-12-27 18:23:58 +01005188static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005189{
5190 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005191 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005192}
5193
Denys Vlasenko10bde142018-12-27 18:23:58 +01005194static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005195{
5196 BcStatus s;
5197 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005198 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005199
Denys Vlasenko10bde142018-12-27 18:23:58 +01005200 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005201 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005202 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005203
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005204 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005205 IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005206 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005207 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005208
Denys Vlasenko259137d2018-12-11 19:42:05 +01005209 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005210 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005211 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005212 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005213}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005214#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005215
Denys Vlasenko10bde142018-12-27 18:23:58 +01005216static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005217{
5218 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005219 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005220 BcVec buf;
5221 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005222 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005223
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005224 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005225 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005226
Denys Vlasenko10bde142018-12-27 18:23:58 +01005227 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005228 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005229
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005230 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005231 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005232 //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 +01005233
Denys Vlasenko10bde142018-12-27 18:23:58 +01005234 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005235 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005236 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005237 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005238 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005239 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005240 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005241 if (s) goto exec_err;
5242
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005243 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005244 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005245 goto exec_err;
5246 }
5247
5248 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005249 ip.inst_idx = 0;
5250 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005251
Denys Vlasenko10bde142018-12-27 18:23:58 +01005252 xc_parse_push(XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005253 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005254
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005255 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005256 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005257 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005258 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005259 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005260}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005261#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005262
Denys Vlasenko10bde142018-12-27 18:23:58 +01005263static size_t xc_program_index(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005264{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005265 unsigned char *bytes = (void*)(code + *bgn);
5266 unsigned amt;
5267 unsigned i;
5268 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005269
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005270 amt = *bytes++;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01005271 if (amt < SMALL_INDEX_LIMIT) {
5272 *bgn += 1;
5273 return amt;
5274 }
5275 amt -= SMALL_INDEX_LIMIT;
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005276 *bgn += amt + 1;
5277
5278 amt *= 8;
5279 res = 0;
5280 for (i = 0; i < amt; i += 8)
5281 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005282
5283 return res;
5284}
5285
Denys Vlasenko10bde142018-12-27 18:23:58 +01005286static char *xc_program_name(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005287{
Denys Vlasenko694d2982018-12-18 19:17:11 +01005288 code += *bgn;
Denys Vlasenkof706a182018-12-26 20:02:09 +01005289 *bgn += strlen(code) + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005290
Denys Vlasenkof706a182018-12-26 20:02:09 +01005291 return xstrdup(code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005292}
5293
Denys Vlasenko10bde142018-12-27 18:23:58 +01005294static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005295{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005296#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005297 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005298 // Example: echo '[]ap' | dc
5299 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005300 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005301 return;
5302 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005303#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005304 while (*str) {
5305 int c = *str++;
5306 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005307 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005308 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005309 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005310 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005311 case 'a':
5312 bb_putchar('\a');
5313 break;
5314 case 'b':
5315 bb_putchar('\b');
5316 break;
5317 case '\\':
5318 case 'e':
5319 bb_putchar('\\');
5320 break;
5321 case 'f':
5322 bb_putchar('\f');
5323 break;
5324 case 'n':
5325 bb_putchar('\n');
5326 G.prog.nchars = SIZE_MAX;
5327 break;
5328 case 'r':
5329 bb_putchar('\r');
5330 break;
5331 case 'q':
5332 bb_putchar('"');
5333 break;
5334 case 't':
5335 bb_putchar('\t');
5336 break;
5337 default:
5338 // Just print the backslash and following character.
5339 bb_putchar('\\');
5340 ++G.prog.nchars;
5341 bb_putchar(c);
5342 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005343 }
5344 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005345 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005346 }
5347}
5348
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005349static void bc_num_printNewline(void)
5350{
5351 if (G.prog.nchars == G.prog.len - 1) {
5352 bb_putchar('\\');
5353 bb_putchar('\n');
5354 G.prog.nchars = 0;
5355 }
5356}
5357
5358#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005359static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005360{
5361 (void) radix;
5362 bb_putchar((char) num);
5363 G.prog.nchars += width;
5364}
5365#endif
5366
5367static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5368{
5369 size_t exp, pow;
5370
5371 bc_num_printNewline();
5372 bb_putchar(radix ? '.' : ' ');
5373 ++G.prog.nchars;
5374
5375 bc_num_printNewline();
5376 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5377 continue;
5378
5379 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5380 size_t dig;
5381 bc_num_printNewline();
5382 dig = num / pow;
5383 num -= dig * pow;
5384 bb_putchar(((char) dig) + '0');
5385 }
5386}
5387
5388static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5389{
5390 if (radix) {
5391 bc_num_printNewline();
5392 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005393 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005394 }
5395
5396 bc_num_printNewline();
5397 bb_putchar(bb_hexdigits_upcase[num]);
5398 G.prog.nchars += width;
5399}
5400
5401static void bc_num_printDecimal(BcNum *n)
5402{
5403 size_t i, rdx = n->rdx - 1;
5404
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005405 if (n->neg) {
5406 bb_putchar('-');
5407 G.prog.nchars++;
5408 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005409
5410 for (i = n->len - 1; i < n->len; --i)
5411 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5412}
5413
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005414typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5415
Denys Vlasenko10bde142018-12-27 18:23:58 +01005416static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005417{
5418 BcStatus s;
5419 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005420 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005421 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005422 BcNum intp, fracp, digit, frac_len;
5423 unsigned long dig, *ptr;
5424 size_t i;
5425 bool radix;
5426
5427 if (n->len == 0) {
5428 print(0, width, false);
5429 RETURN_STATUS(BC_STATUS_SUCCESS);
5430 }
5431
5432 bc_vec_init(&stack, sizeof(long), NULL);
5433 bc_num_init(&intp, n->len);
5434 bc_num_init(&fracp, n->rdx);
5435 bc_num_init(&digit, width);
5436 bc_num_init(&frac_len, BC_NUM_INT(n));
5437 bc_num_copy(&intp, n);
5438 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005439 base.cap = ARRAY_SIZE(base_digs);
5440 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005441 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005442
5443 bc_num_truncate(&intp, intp.rdx);
5444 s = zbc_num_sub(n, &intp, &fracp, 0);
5445 if (s) goto err;
5446
5447 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005448 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005449 if (s) goto err;
5450 s = zbc_num_ulong(&digit, &dig);
5451 if (s) goto err;
5452 bc_vec_push(&stack, &dig);
5453 }
5454
5455 for (i = 0; i < stack.len; ++i) {
5456 ptr = bc_vec_item_rev(&stack, i);
5457 print(*ptr, width, false);
5458 }
5459
5460 if (!n->rdx) goto err;
5461
5462 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005463 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005464 if (s) goto err;
5465 s = zbc_num_ulong(&fracp, &dig);
5466 if (s) goto err;
5467 bc_num_ulong2num(&intp, dig);
5468 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5469 if (s) goto err;
5470 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005471 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005472 if (s) goto err;
5473 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005474 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005475 bc_num_free(&frac_len);
5476 bc_num_free(&digit);
5477 bc_num_free(&fracp);
5478 bc_num_free(&intp);
5479 bc_vec_free(&stack);
5480 RETURN_STATUS(s);
5481}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005482#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005483
Denys Vlasenko10bde142018-12-27 18:23:58 +01005484static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005485{
5486 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005487 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005488 BcNumDigitOp print;
5489 bool neg = n->neg;
5490
5491 if (neg) {
5492 bb_putchar('-');
5493 G.prog.nchars++;
5494 }
5495
5496 n->neg = false;
5497
5498 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5499 width = 1;
5500 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005501 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005502 unsigned i = G.prog.ob_t - 1;
5503 width = 0;
5504 for (;;) {
5505 width++;
5506 i /= 10;
5507 if (i == 0)
5508 break;
5509 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005510 print = bc_num_printDigits;
5511 }
5512
Denys Vlasenko10bde142018-12-27 18:23:58 +01005513 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005514 n->neg = neg;
5515
5516 RETURN_STATUS(s);
5517}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005518#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005519
Denys Vlasenko10bde142018-12-27 18:23:58 +01005520static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005521{
5522 BcStatus s = BC_STATUS_SUCCESS;
5523
5524 bc_num_printNewline();
5525
5526 if (n->len == 0) {
5527 bb_putchar('0');
5528 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005529 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005530 bc_num_printDecimal(n);
5531 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005532 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005533
5534 if (newline) {
5535 bb_putchar('\n');
5536 G.prog.nchars = 0;
5537 }
5538
5539 RETURN_STATUS(s);
5540}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005541#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005542
Denys Vlasenko10bde142018-12-27 18:23:58 +01005543static BC_STATUS zxc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005544{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005545 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005546 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005547 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005548 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005549
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005550 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005551 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005552
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005553 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005554 s = zxc_program_num(r, &num, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005555 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005556
5557 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005558 s = zxc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005559#if ENABLE_BC
5560 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5561#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005562 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005563 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005564
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005565 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005566 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005567
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005568 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005569 for (;;) {
5570 char c = *str++;
5571 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005572 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005573 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005574 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005575 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005576 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005577 xc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005578 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005579 }
5580 }
5581
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005582 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005583
Denys Vlasenko29301232018-12-11 15:29:32 +01005584 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005585}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005586#define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005587
Denys Vlasenko10bde142018-12-27 18:23:58 +01005588static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005589{
5590 BcStatus s;
5591 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005592 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005593
Denys Vlasenko10bde142018-12-27 18:23:58 +01005594 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005595 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005596
5597 bc_num_init(&res.d.n, num->len);
5598 bc_num_copy(&res.d.n, num);
5599 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5600
Denys Vlasenko10bde142018-12-27 18:23:58 +01005601 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005602
Denys Vlasenko29301232018-12-11 15:29:32 +01005603 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005604}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005605#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005606
Denys Vlasenko10bde142018-12-27 18:23:58 +01005607static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005608{
5609 BcStatus s;
5610 BcResult *opd1, *opd2, res;
5611 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005612 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005613
Denys Vlasenko10bde142018-12-27 18:23:58 +01005614 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005615 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005616
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005617 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005618
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005619 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005620 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005621 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005622 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005623 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005624 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005625 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005626 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005627 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005628 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005629 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005630 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005631 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005632 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005633 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005634 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005635 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005636 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005637 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005638 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005639 cond = (cond > 0);
5640 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005641 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005642 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005643 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005644 }
5645 }
5646
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005647 if (cond) bc_num_one(&res.d.n);
5648 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005649
Denys Vlasenko10bde142018-12-27 18:23:58 +01005650 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005651
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005652 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005653}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005654#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005655
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005656#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005657static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005658{
5659 BcNum n2;
5660 BcResult res;
5661
5662 memset(&n2, 0, sizeof(BcNum));
5663 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005664 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005665
5666 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005667 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005668 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005669 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005670 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005671 }
5672
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005673 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005674 bc_vec_push(v, &n2);
5675
Denys Vlasenko29301232018-12-11 15:29:32 +01005676 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005677}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005678#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005679#endif // ENABLE_DC
5680
Denys Vlasenko10bde142018-12-27 18:23:58 +01005681static BC_STATUS zxc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005682{
5683 BcStatus s;
5684 BcResult *ptr, r;
5685 BcVec *v;
5686 BcNum *n;
5687
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005688 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005689 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005690
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005691 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005692 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005693 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005694 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005695
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005696#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005697 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005698 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005699 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005700 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005701#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005702
Denys Vlasenko10bde142018-12-27 18:23:58 +01005703 s = zxc_program_num(ptr, &n, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005704 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005705
5706 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko10bde142018-12-27 18:23:58 +01005707 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005708
5709 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005710 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005711 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005712 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005713 bc_array_init(&r.d.v, true);
5714 bc_array_copy(&r.d.v, (BcVec *) n);
5715 }
5716
5717 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005718 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005719
Denys Vlasenko29301232018-12-11 15:29:32 +01005720 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005721}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005722#define zxc_program_copyToVar(...) (zxc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005723
Denys Vlasenko10bde142018-12-27 18:23:58 +01005724static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005725{
5726 BcStatus s;
5727 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005728 BcNum *l, *r;
5729 bool assign = (inst == XC_INST_ASSIGN);
5730 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005731
Denys Vlasenko10bde142018-12-27 18:23:58 +01005732 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005733 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005734
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005735 ib = left->t == XC_RESULT_IBASE;
5736 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005737
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005738#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005739 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005740 BcVec *v;
5741
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005742 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005743 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005744 v = xc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005745
Denys Vlasenkofa210792018-12-19 19:35:40 +01005746 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005747 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005748#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005749
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005750 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005751 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005752 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005753 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005754 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005755
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005756#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005757 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005758 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005759
5760 if (assign)
5761 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005762 else {
5763 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005764 IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005765 }
5766 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005767#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005768 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005769#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005770
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005771 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005772 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005773 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5774 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5775 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005776 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005777 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005778 size_t max;
5779 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005780
Denys Vlasenko29301232018-12-11 15:29:32 +01005781 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005782 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005783 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005784 if (sc) {
5785 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005786 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005787 } else {
5788 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005789 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005791 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005792 }
5793
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005794 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005795 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005796
5797 *ptr = (size_t) val;
5798 s = BC_STATUS_SUCCESS;
5799 }
5800
5801 bc_num_init(&res.d.n, l->len);
5802 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005803 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005804
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005805 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005806}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005807#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005808
Denys Vlasenko416ce762018-12-02 20:57:17 +01005809#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005810#define xc_program_pushVar(code, bgn, pop, copy) \
5811 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005812// for bc, 'pop' and 'copy' are always false
5813#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005814static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005815 bool pop, bool copy)
5816{
Gavin Howard01055ba2018-11-03 11:00:21 -06005817 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005818 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005819
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005820 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005821 r.d.id.name = name;
5822
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005823#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005824 if (pop || copy) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005825 BcVec *v = xc_program_search(name, true);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005826 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005828 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005829 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005830 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005831 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005832
5833 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005834 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005835 bc_num_init_DEF_SIZE(&r.d.n);
5836 bc_num_copy(&r.d.n, num);
5837 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005838 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005839 r.d.id.idx = num->rdx;
5840 }
5841
5842 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005843 }
5844#endif // ENABLE_DC
5845
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005846 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005847
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005848 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005849}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005850#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005851
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005852static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005853{
5854 BcStatus s = BC_STATUS_SUCCESS;
5855 BcResult r;
5856 BcNum *num;
5857
Denys Vlasenko10bde142018-12-27 18:23:58 +01005858 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005859
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005860 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005861 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005862 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005863 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005864 BcResult *operand;
5865 unsigned long temp;
5866
Denys Vlasenko10bde142018-12-27 18:23:58 +01005867 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005868 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005869 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005870 if (s) goto err;
5871
5872 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005873 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005874 goto err;
5875 }
5876
5877 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005878 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005880 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005881 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005882 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005883}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005884#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005885
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005886#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005887static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005888{
5889 BcStatus s;
5890 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005891 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005892 char inst2 = inst;
5893
Denys Vlasenko10bde142018-12-27 18:23:58 +01005894 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005895 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005896
5897 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005898 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005899 bc_num_init(&copy.d.n, num->len);
5900 bc_num_copy(&copy.d.n, num);
5901 }
5902
5903 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005904 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5905 ? BC_INST_ASSIGN_PLUS
5906 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005907
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005908 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005909 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005910 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005911
5912 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005913 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914 }
5915
Denys Vlasenko29301232018-12-11 15:29:32 +01005916 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005917}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005918#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005919
Denys Vlasenko29301232018-12-11 15:29:32 +01005920static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005921{
Gavin Howard01055ba2018-11-03 11:00:21 -06005922 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005923 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005926 BcResult *arg;
5927
Denys Vlasenko10bde142018-12-27 18:23:58 +01005928 nparams = xc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005929 ip.inst_idx = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005930 ip.func = xc_program_index(code, idx);
5931 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005932
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005933 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005934 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005935 }
5936 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005937 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005938 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005939 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005940
5941 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005942 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005943
5944 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005945 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005946
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005947 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005948 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005949
Denys Vlasenko10bde142018-12-27 18:23:58 +01005950 s = zxc_program_copyToVar(a->name, a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005951 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005952 }
5953
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005954 a = bc_vec_item(&func->autos, i);
5955 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005956 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005957
Denys Vlasenko10bde142018-12-27 18:23:58 +01005958 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005959 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005960 BcNum n2;
5961 bc_num_init_DEF_SIZE(&n2);
5962 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005963 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005964 BcVec v2;
5965 bc_array_init(&v2, true);
5966 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005967 }
5968 }
5969
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005970 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005971
Denys Vlasenko29301232018-12-11 15:29:32 +01005972 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005973}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005974#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005975
Denys Vlasenko29301232018-12-11 15:29:32 +01005976static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005977{
Gavin Howard01055ba2018-11-03 11:00:21 -06005978 BcResult res;
5979 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005980 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005981 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005982 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005983
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005984 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 +01005985 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005986
Denys Vlasenko10bde142018-12-27 18:23:58 +01005987 f = xc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005988 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005989
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005990 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005991 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005992 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005993 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005994
Denys Vlasenko10bde142018-12-27 18:23:58 +01005995 s = zxc_program_num(operand, &num, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005997 bc_num_init(&res.d.n, num->len);
5998 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005999 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006000 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006001 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006002 }
6003
6004 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006005 a = (void*)f->autos.v;
6006 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006007 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006008 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006009 bc_vec_pop(v);
6010 }
6011
Denys Vlasenko24e41942018-12-21 23:01:26 +01006012 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006013 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006014 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko29301232018-12-11 15:29:32 +01006016 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006017}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006018#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006019#endif // ENABLE_BC
6020
Denys Vlasenko10bde142018-12-27 18:23:58 +01006021static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006022{
6023 return (unsigned long) n->rdx;
6024}
6025
Denys Vlasenko10bde142018-12-27 18:23:58 +01006026static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006027{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006028 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006029
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006030 if (n->rdx != len) return len;
6031 for (;;) {
6032 if (len == 0) break;
6033 len--;
6034 if (n->num[len] != 0) break;
6035 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006036 return len;
6037}
6038
Denys Vlasenko10bde142018-12-27 18:23:58 +01006039static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006040{
6041 BcStatus s;
6042 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006043 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006044 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006045 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006046
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006047 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006048 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006049 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006050
Denys Vlasenko10bde142018-12-27 18:23:58 +01006051 s = zxc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006052 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006053
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006054#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006055 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006056 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006057#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006058
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006059 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006060
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006061 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006062 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006063#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006064 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006065 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006066 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006067#endif
6068#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006069 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006070 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006071 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006072
Denys Vlasenko10bde142018-12-27 18:23:58 +01006073 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006074 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006075 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006076#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006077 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006078 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006079 }
6080
Denys Vlasenko10bde142018-12-27 18:23:58 +01006081 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006082
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006083 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006085#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006086
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006087#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006088static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006089{
6090 BcStatus s;
6091 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006092 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006093
Denys Vlasenko10bde142018-12-27 18:23:58 +01006094 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006095 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006096
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006097 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006098 bc_num_init(&res2.d.n, n2->len);
6099
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006100 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 if (s) goto err;
6102
Denys Vlasenko10bde142018-12-27 18:23:58 +01006103 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006104 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006105 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006106
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006107 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006108 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006109 bc_num_free(&res2.d.n);
6110 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006111 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006112}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006113#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006114
Denys Vlasenkofa210792018-12-19 19:35:40 +01006115static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006116{
6117 BcStatus s;
6118 BcResult *r1, *r2, *r3, res;
6119 BcNum *n1, *n2, *n3;
6120
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006121 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006122 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006123 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006124 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006126 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006127 s = zxc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006128 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006129 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006130 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006131
6132 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006133 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006134 if (r1->t == r2->t) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006135 s = zxc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006136 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006137 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006138 if (r1->t == r3->t) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006139 s = zxc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006140 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006141 }
6142 }
6143
6144 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006145 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006146 if (s) goto err;
6147
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006148 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006149 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006151 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006152 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006153 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006154 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006155}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006156#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006157
Denys Vlasenkofa210792018-12-19 19:35:40 +01006158static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006159{
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006161 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006162
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006163 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006164
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006165 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006166 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006167 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006168}
6169
Denys Vlasenkofa210792018-12-19 19:35:40 +01006170static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006171{
6172 BcStatus s;
6173 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006174 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006175 char **strs;
6176 char *str;
6177 char c;
6178 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006179
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006180 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006181 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006182 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006183
Denys Vlasenko10bde142018-12-27 18:23:58 +01006184 s = zxc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006185 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006186
6187 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006188 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006189 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006190 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006191
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006192 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006193 bc_num_copy(&n, num);
6194 bc_num_truncate(&n, n.rdx);
6195
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006196 strmb.cap = ARRAY_SIZE(strmb_digs);
6197 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006198 bc_num_ulong2num(&strmb, 0x100);
6199 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006200
Gavin Howard01055ba2018-11-03 11:00:21 -06006201 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006202 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 if (s) goto num_err;
6204
6205 c = (char) val;
6206
6207 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006208 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006209 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006210 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006211 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006212 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006213 }
6214
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006215 strs = (void*)G.prog.strs.v;
6216 for (idx = 0; idx < G.prog.strs.len; idx++) {
6217 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6218 goto dup;
6219 }
6220 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006221 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006223 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006224 bc_vec_push(&G.prog.strs, &str);
6225 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006226 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006227 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006228 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006229
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006230 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006231 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006232 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006233 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006234}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006235#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006236
Denys Vlasenkofa210792018-12-19 19:35:40 +01006237static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006238{
6239 BcStatus s;
6240 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006241 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006242 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006243
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006244 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006245 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006246 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006247
Denys Vlasenko10bde142018-12-27 18:23:58 +01006248 s = zxc_program_num(r, &n, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01006249 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006250
Denys Vlasenko57734c92018-12-17 21:14:05 +01006251 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006252 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006253 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006254 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006255 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006256 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006257 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006258 }
6259
Denys Vlasenko29301232018-12-11 15:29:32 +01006260 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006261}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006262#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenkofa210792018-12-19 19:35:40 +01006264static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006265{
6266 BcStatus s;
6267 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006268 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006269 unsigned long val;
6270
Denys Vlasenko10bde142018-12-27 18:23:58 +01006271 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006272 if (s) RETURN_STATUS(s);
6273 s = zbc_num_ulong(num, &val);
6274 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006275
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006276 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006278 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006279 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006280 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006281 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006282 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006283
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006284 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006285
Denys Vlasenko29301232018-12-11 15:29:32 +01006286 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006287}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006288#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006289
Denys Vlasenkofa210792018-12-19 19:35:40 +01006290static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006291{
6292 BcStatus s = BC_STATUS_SUCCESS;
6293 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006294 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006295 BcInstPtr ip;
6296 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006297
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006298 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006299 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006301 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006302
6303 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006304 BcNum *n = n; // for compiler
6305 bool exec;
6306 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006307 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006308 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006309
Denys Vlasenkof706a182018-12-26 20:02:09 +01006310 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006311 (*bgn) += 1;
6312 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006313 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006314
6315 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006316 name = then_name;
6317 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006318 exec = true;
6319 name = else_name;
6320 }
6321
6322 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006323 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006324 v = xc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006325 n = bc_vec_top(v);
6326 }
6327
6328 free(then_name);
6329 free(else_name);
6330
6331 if (!exec) goto exit;
6332 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006333 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006334 goto exit;
6335 }
6336
6337 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006338 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006339 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006340 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006341 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006342 BcNum *n;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006343 s = zxc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006344 if (s || !BC_PROG_STR(n)) goto exit;
6345 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006346 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006347 goto exit;
6348 }
6349
6350 fidx = sidx + BC_PROG_REQ_FUNCS;
6351
Denys Vlasenko10bde142018-12-27 18:23:58 +01006352 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006353
6354 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006355 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006356 char *str;
6357
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006358 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006359 xc_parse_create(fidx);
6360 str = *xc_program_str(sidx);
6361 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006363
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006364 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006365 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006366 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006367 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006368 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006369 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006370 G.prs = sv_parse; // struct copy
6371 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006372 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006373 bc_vec_pop_all(&f->code);
6374 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006375 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006376 }
6377
Denys Vlasenko24e41942018-12-21 23:01:26 +01006378 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006379 ip.func = fidx;
6380
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006381 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006382 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006383
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006384 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006385 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006386 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006387 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006388}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006389#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006390#endif // ENABLE_DC
6391
Denys Vlasenko10bde142018-12-27 18:23:58 +01006392static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006393{
Gavin Howard01055ba2018-11-03 11:00:21 -06006394 BcResult res;
6395 unsigned long val;
6396
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006397 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006398 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006399 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006400 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006401 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006402 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006403 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006404
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006405 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006406 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006407 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006408}
6409
Denys Vlasenko10bde142018-12-27 18:23:58 +01006410static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006411{
Gavin Howard01055ba2018-11-03 11:00:21 -06006412 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006413 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006414 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006415 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006416
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006417 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006418 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6419 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006420 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006421 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006422
Denys Vlasenko24e41942018-12-21 23:01:26 +01006423 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 -06006424 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006425#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006426 case BC_INST_JUMP_ZERO: {
6427 BcNum *num;
6428 bool zero;
6429 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006430 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006431 if (s) RETURN_STATUS(s);
6432 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6433 bc_vec_pop(&G.prog.results);
6434 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006435 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006436 break;
6437 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006438 // else: fall through
6439 }
6440 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006441 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006442 size_t *addr = bc_vec_item(&func->labels, idx);
6443 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6444 ip->inst_idx = *addr;
6445 break;
6446 }
6447 case BC_INST_CALL:
6448 dbg_exec("BC_INST_CALL:");
6449 s = zbc_program_call(code, &ip->inst_idx);
6450 goto read_updated_ip;
6451 case BC_INST_INC_PRE:
6452 case BC_INST_DEC_PRE:
6453 case BC_INST_INC_POST:
6454 case BC_INST_DEC_POST:
6455 dbg_exec("BC_INST_INCDEC:");
6456 s = zbc_program_incdec(inst);
6457 break;
6458 case BC_INST_HALT:
6459 dbg_exec("BC_INST_HALT:");
6460 QUIT_OR_RETURN_TO_MAIN;
6461 break;
6462 case XC_INST_RET:
6463 case BC_INST_RET0:
6464 dbg_exec("BC_INST_RET[0]:");
6465 s = zbc_program_return(inst);
6466 goto read_updated_ip;
6467 case XC_INST_BOOL_OR:
6468 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006469#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006470 case XC_INST_REL_EQ:
6471 case XC_INST_REL_LE:
6472 case XC_INST_REL_GE:
6473 case XC_INST_REL_NE:
6474 case XC_INST_REL_LT:
6475 case XC_INST_REL_GT:
6476 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006477 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006478 break;
6479 case XC_INST_READ:
6480 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006481 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006482 goto read_updated_ip;
6483 case XC_INST_VAR:
6484 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006485 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006486 break;
6487 case XC_INST_ARRAY_ELEM:
6488 case XC_INST_ARRAY:
6489 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6490 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6491 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006492#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006493 case BC_INST_LAST:
6494 dbg_exec("BC_INST_LAST:");
6495 r.t = BC_RESULT_LAST;
6496 bc_vec_push(&G.prog.results, &r);
6497 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006498#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006499 case XC_INST_IBASE:
6500 case XC_INST_OBASE:
6501 case XC_INST_SCALE:
6502 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006503 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006504 break;
6505 case XC_INST_SCALE_FUNC:
6506 case XC_INST_LENGTH:
6507 case XC_INST_SQRT:
6508 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006509 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006510 break;
6511 case XC_INST_NUM:
6512 dbg_exec("XC_INST_NUM:");
6513 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006514 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006515 bc_vec_push(&G.prog.results, &r);
6516 break;
6517 case XC_INST_POP:
6518 dbg_exec("XC_INST_POP:");
6519 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6520 s = bc_error_stack_has_too_few_elements();
6521 else
6522 bc_vec_pop(&G.prog.results);
6523 break;
6524 case XC_INST_PRINT:
6525 case XC_INST_PRINT_POP:
6526 case XC_INST_PRINT_STR:
6527 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006528 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006529 break;
6530 case XC_INST_STR:
6531 dbg_exec("XC_INST_STR:");
6532 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006533 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006534 bc_vec_push(&G.prog.results, &r);
6535 break;
6536 case XC_INST_POWER:
6537 case XC_INST_MULTIPLY:
6538 case XC_INST_DIVIDE:
6539 case XC_INST_MODULUS:
6540 case XC_INST_PLUS:
6541 case XC_INST_MINUS:
6542 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006543 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006544 break;
6545 case XC_INST_BOOL_NOT: {
6546 BcNum *num;
6547 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006548 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006549 if (s) RETURN_STATUS(s);
6550 bc_num_init_DEF_SIZE(&r.d.n);
6551 if (bc_num_cmp(num, &G.prog.zero) == 0)
6552 bc_num_one(&r.d.n);
6553 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006554 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006555 break;
6556 }
6557 case XC_INST_NEG:
6558 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006559 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006560 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006561#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006562 case BC_INST_ASSIGN_POWER:
6563 case BC_INST_ASSIGN_MULTIPLY:
6564 case BC_INST_ASSIGN_DIVIDE:
6565 case BC_INST_ASSIGN_MODULUS:
6566 case BC_INST_ASSIGN_PLUS:
6567 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006568#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006569 case XC_INST_ASSIGN:
6570 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006571 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006572 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006573#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006574 case DC_INST_POP_EXEC:
6575 dbg_exec("DC_INST_POP_EXEC:");
6576 bc_vec_pop(&G.prog.exestack);
6577 goto read_updated_ip;
6578 case DC_INST_MODEXP:
6579 dbg_exec("DC_INST_MODEXP:");
6580 s = zdc_program_modexp();
6581 break;
6582 case DC_INST_DIVMOD:
6583 dbg_exec("DC_INST_DIVMOD:");
6584 s = zdc_program_divmod();
6585 break;
6586 case DC_INST_EXECUTE:
6587 case DC_INST_EXEC_COND:
6588 dbg_exec("DC_INST_EXEC[_COND]:");
6589 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6590 goto read_updated_ip;
6591 case DC_INST_PRINT_STACK: {
6592 size_t idx;
6593 dbg_exec("DC_INST_PRINT_STACK:");
6594 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006595 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006596 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006598 break;
6599 }
6600 case DC_INST_CLEAR_STACK:
6601 dbg_exec("DC_INST_CLEAR_STACK:");
6602 bc_vec_pop_all(&G.prog.results);
6603 break;
6604 case DC_INST_STACK_LEN:
6605 dbg_exec("DC_INST_STACK_LEN:");
6606 dc_program_stackLen();
6607 break;
6608 case DC_INST_DUPLICATE:
6609 dbg_exec("DC_INST_DUPLICATE:");
6610 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6611 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6612 ptr = bc_vec_top(&G.prog.results);
6613 dc_result_copy(&r, ptr);
6614 bc_vec_push(&G.prog.results, &r);
6615 break;
6616 case DC_INST_SWAP: {
6617 BcResult *ptr2;
6618 dbg_exec("DC_INST_SWAP:");
6619 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6620 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6621 ptr = bc_vec_item_rev(&G.prog.results, 0);
6622 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6623 memcpy(&r, ptr, sizeof(BcResult));
6624 memcpy(ptr, ptr2, sizeof(BcResult));
6625 memcpy(ptr2, &r, sizeof(BcResult));
6626 break;
6627 }
6628 case DC_INST_ASCIIFY:
6629 dbg_exec("DC_INST_ASCIIFY:");
6630 s = zdc_program_asciify();
6631 break;
6632 case DC_INST_PRINT_STREAM:
6633 dbg_exec("DC_INST_PRINT_STREAM:");
6634 s = zdc_program_printStream();
6635 break;
6636 case DC_INST_LOAD:
6637 case DC_INST_PUSH_VAR: {
6638 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006639 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006640 break;
6641 }
6642 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006643 char *name = xc_program_name(code, &ip->inst_idx);
6644 s = zxc_program_copyToVar(name, true);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006645 free(name);
6646 break;
6647 }
6648 case DC_INST_QUIT:
6649 dbg_exec("DC_INST_QUIT:");
6650 if (G.prog.exestack.len <= 2)
6651 QUIT_OR_RETURN_TO_MAIN;
6652 bc_vec_npop(&G.prog.exestack, 2);
6653 goto read_updated_ip;
6654 case DC_INST_NQUIT:
6655 dbg_exec("DC_INST_NQUIT:");
6656 s = zdc_program_nquit();
6657 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006658#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006659 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006660 // Instruction stack has changed, read new pointers
6661 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006662 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006663 code = func->code.v;
6664 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006665 }
6666
Denys Vlasenkod38af482018-12-04 19:11:02 +01006667 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006668 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006669 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006670 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006671
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006672 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006673 }
6674
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006675 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006677#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006678
Denys Vlasenko10bde142018-12-27 18:23:58 +01006679static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006680{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006681 char *lenv;
6682 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006683
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006684 lenv = getenv(var);
6685 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006686 if (!lenv) return len;
6687
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006688 len = bb_strtou(lenv, NULL, 10) - 1;
6689 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006690 len = BC_NUM_PRINT_WIDTH;
6691
6692 return len;
6693}
6694
Denys Vlasenko10bde142018-12-27 18:23:58 +01006695static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006696{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006697 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006698
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006699 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006700 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006701 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006702
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006703 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006704 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006705 BcInstPtr *ip;
6706 BcFunc *f;
6707
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006708 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006709 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006710#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006711 if (G.prs.lex == BC_LEX_SCOLON
6712 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006713 ) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006714 s = zxc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006715 if (s) goto err;
6716 goto check_eof;
6717 }
6718
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006719 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006720 if (s) goto err;
6721
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006722 // Check that next token is a correct stmt delimiter -
6723 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006724 if (G.prs.lex != BC_LEX_SCOLON
6725 && G.prs.lex != XC_LEX_NLINE
6726 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006727 ) {
6728 const char *err_at;
6729//TODO: commonalize for other parse errors:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006730 err_at = G.prs.lex_next_at ? G.prs.lex_next_at : "UNKNOWN";
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006731 bc_error_fmt("bad statement terminator at '%.*s'",
6732 (int)(strchrnul(err_at, '\n') - err_at),
6733 err_at
6734 );
6735 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006736 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006737 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006738 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006739#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006740 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006741#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006742 // Most of dc parsing assumes all whitespace,
6743 // including '\n', is eaten.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006744 while (G.prs.lex == XC_LEX_NLINE) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006745 s = zxc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006746 if (s) goto err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006747 if (G.prs.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006748 goto done;
6749 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006750 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006751#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006752 }
6753 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006754 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006755 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006756 RETURN_STATUS(BC_STATUS_FAILURE);
6757 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006758
Denys Vlasenko39287e02018-12-22 02:23:08 +01006759 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006760 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006761 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006762 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006763 break;
6764 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006765
6766 ip = (void*)G.prog.exestack.v;
6767#if SANITY_CHECKS
6768 if (G.prog.exestack.len != 1) // should have only main's IP
6769 bb_error_msg_and_die("BUG:call stack");
6770 if (ip->func != BC_PROG_MAIN)
6771 bb_error_msg_and_die("BUG:not MAIN");
6772#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006773 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006774 // bc discards strings, constants and code after each
6775 // top-level statement in the "main program".
6776 // This prevents "yes 1 | bc" from growing its memory
6777 // without bound. This can be done because data stack
6778 // is empty and thus can't hold any references to
6779 // strings or constants, there is no generated code
6780 // which can hold references (after we discard one
6781 // we just executed). Code of functions can have references,
6782 // but bc stores function strings/constants in per-function
6783 // storage.
6784 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006785#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006786 if (G.prog.results.len != 0) // should be empty
6787 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006788#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006789 IF_BC(bc_vec_pop_all(&f->strs);)
6790 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006791 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006792 if (G.prog.results.len == 0
6793 && G.prog.vars.len == 0
6794 ) {
6795 // If stack is empty and no registers exist (TODO: or they are all empty),
6796 // we can get rid of accumulated strings and constants.
6797 // In this example dc process should not grow
6798 // its memory consumption with time:
6799 // yes 1pc | dc
6800 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6801 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6802 }
6803 // The code is discarded always (below), thus this example
6804 // should also not grow its memory consumption with time,
6805 // even though its data stack is not empty:
6806 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006807 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006808 // We drop generated and executed code for both bc and dc:
6809 bc_vec_pop_all(&f->code);
6810 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006811 }
Denys Vlasenko8a56e362018-12-26 19:09:23 +01006812 IF_DC(done:)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006813 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006814 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006815}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006816#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006817
Denys Vlasenko10bde142018-12-27 18:23:58 +01006818static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006819{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006820 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006821 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006822 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006823 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006824
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006825 G.prs.lex_filename = filename;
6826 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006827 G.err_line = G.prs.lex_line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06006828
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006829 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006830 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006831 // We do not stop looping on errors here if reading stdin.
6832 // Example: start interactive bc and enter "return".
6833 // It should say "'return' not in a function"
6834 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006835 } while (G.prs.lex_input_fp == stdin);
6836 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006837 RETURN_STATUS(s);
6838}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006839#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006840
Denys Vlasenko10bde142018-12-27 18:23:58 +01006841static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006842{
6843 BcStatus s;
6844 FILE *fp;
6845
6846 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006847 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006848 fclose(fp);
6849
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006850 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006851}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006852#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006853
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006854#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006855static void bc_vm_info(void)
6856{
6857 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006858 "Adapted from https://github.com/gavinhoward/bc\n"
6859 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006860 , applet_name);
6861}
6862
6863static void bc_args(char **argv)
6864{
6865 unsigned opts;
6866 int i;
6867
6868 GETOPT_RESET();
6869#if ENABLE_FEATURE_BC_LONG_OPTIONS
6870 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6871 "warn\0" No_argument "w"
6872 "version\0" No_argument "v"
6873 "standard\0" No_argument "s"
6874 "quiet\0" No_argument "q"
6875 "mathlib\0" No_argument "l"
6876 "interactive\0" No_argument "i"
6877 );
6878#else
6879 opts = option_mask32 |= getopt32(argv, "wvsqli");
6880#endif
6881 if (getenv("POSIXLY_CORRECT"))
6882 option_mask32 |= BC_FLAG_S;
6883
6884 if (opts & BC_FLAG_V) {
6885 bc_vm_info();
6886 exit(0);
6887 }
6888
6889 for (i = optind; argv[i]; ++i)
6890 bc_vec_push(&G.files, argv + i);
6891}
6892
6893static void bc_vm_envArgs(void)
6894{
6895 BcVec v;
6896 char *buf;
6897 char *env_args = getenv("BC_ENV_ARGS");
6898
6899 if (!env_args) return;
6900
6901 G.env_args = xstrdup(env_args);
6902 buf = G.env_args;
6903
6904 bc_vec_init(&v, sizeof(char *), NULL);
6905
6906 while (*(buf = skip_whitespace(buf)) != '\0') {
6907 bc_vec_push(&v, &buf);
6908 buf = skip_non_whitespace(buf);
6909 if (!*buf)
6910 break;
6911 *buf++ = '\0';
6912 }
6913
6914 // NULL terminate, and pass argv[] so that first arg is argv[1]
6915 if (sizeof(int) == sizeof(char*)) {
6916 bc_vec_push(&v, &const_int_0);
6917 } else {
6918 static char *const nullptr = NULL;
6919 bc_vec_push(&v, &nullptr);
6920 }
6921 bc_args(((char **)v.v) - 1);
6922
6923 bc_vec_free(&v);
6924}
6925
6926static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006927 "scale=20"
6928"\n" "define e(x){"
6929"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006930////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6931//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6932//e(-.998896): GNU:.36828580434569428695
6933// above code:.36828580434569428696
6934// actual value:.3682858043456942869594...
6935// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006936"\n" "b=ibase"
6937"\n" "ibase=A"
6938"\n" "if(x<0){"
6939"\n" "n=1"
6940"\n" "x=-x"
6941"\n" "}"
6942"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006943"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006944"\n" "scale=scale(x)+1"
6945"\n" "while(x>1){"
6946"\n" "d+=1"
6947"\n" "x/=2"
6948"\n" "scale+=1"
6949"\n" "}"
6950"\n" "scale=r"
6951"\n" "r=x+1"
6952"\n" "p=x"
6953"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006954"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006955"\n" "p*=x"
6956"\n" "f*=i"
6957"\n" "v=p/f"
6958"\n" "r+=v"
6959"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006960"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006961"\n" "scale=s"
6962"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006963"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006964"\n" "return(r/1)"
6965"\n" "}"
6966"\n" "define l(x){"
6967"\n" "auto b,s,r,p,a,q,i,v"
6968"\n" "b=ibase"
6969"\n" "ibase=A"
6970"\n" "if(x<=0){"
6971"\n" "r=(1-10^scale)/1"
6972"\n" "ibase=b"
6973"\n" "return(r)"
6974"\n" "}"
6975"\n" "s=scale"
6976"\n" "scale+=6"
6977"\n" "p=2"
6978"\n" "while(x>=2){"
6979"\n" "p*=2"
6980"\n" "x=sqrt(x)"
6981"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006982"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006983"\n" "p*=2"
6984"\n" "x=sqrt(x)"
6985"\n" "}"
6986"\n" "r=a=(x-1)/(x+1)"
6987"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006988"\n" "v=1"
6989"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006990"\n" "a*=q"
6991"\n" "v=a/i"
6992"\n" "r+=v"
6993"\n" "}"
6994"\n" "r*=p"
6995"\n" "scale=s"
6996"\n" "ibase=b"
6997"\n" "return(r/1)"
6998"\n" "}"
6999"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007000"\n" "auto b,s,r,a,q,i"
7001"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007002"\n" "b=ibase"
7003"\n" "ibase=A"
7004"\n" "s=scale"
7005"\n" "scale=1.1*s+2"
7006"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007007"\n" "scale=0"
7008"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007009"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007010"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007011"\n" "scale=s+2"
7012"\n" "r=a=x"
7013"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007014"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007015"\n" "a*=q/(i*(i-1))"
7016"\n" "r+=a"
7017"\n" "}"
7018"\n" "scale=s"
7019"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007020"\n" "return(r/1)"
7021"\n" "}"
7022"\n" "define c(x){"
7023"\n" "auto b,s"
7024"\n" "b=ibase"
7025"\n" "ibase=A"
7026"\n" "s=scale"
7027"\n" "scale*=1.2"
7028"\n" "x=s(2*a(1)+x)"
7029"\n" "scale=s"
7030"\n" "ibase=b"
7031"\n" "return(x/1)"
7032"\n" "}"
7033"\n" "define a(x){"
7034"\n" "auto b,s,r,n,a,m,t,f,i,u"
7035"\n" "b=ibase"
7036"\n" "ibase=A"
7037"\n" "n=1"
7038"\n" "if(x<0){"
7039"\n" "n=-1"
7040"\n" "x=-x"
7041"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007042"\n" "if(scale<65){"
7043"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7044"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007045"\n" "}"
7046"\n" "s=scale"
7047"\n" "if(x>.2){"
7048"\n" "scale+=5"
7049"\n" "a=a(.2)"
7050"\n" "}"
7051"\n" "scale=s+3"
7052"\n" "while(x>.2){"
7053"\n" "m+=1"
7054"\n" "x=(x-.2)/(1+.2*x)"
7055"\n" "}"
7056"\n" "r=u=x"
7057"\n" "f=-x*x"
7058"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007059"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007060"\n" "u*=f"
7061"\n" "t=u/i"
7062"\n" "r+=t"
7063"\n" "}"
7064"\n" "scale=s"
7065"\n" "ibase=b"
7066"\n" "return((m*a+r)/n)"
7067"\n" "}"
7068"\n" "define j(n,x){"
7069"\n" "auto b,s,o,a,i,v,f"
7070"\n" "b=ibase"
7071"\n" "ibase=A"
7072"\n" "s=scale"
7073"\n" "scale=0"
7074"\n" "n/=1"
7075"\n" "if(n<0){"
7076"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007077"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007078"\n" "}"
7079"\n" "a=1"
7080"\n" "for(i=2;i<=n;++i)a*=i"
7081"\n" "scale=1.5*s"
7082"\n" "a=(x^n)/2^n/a"
7083"\n" "r=v=1"
7084"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007085"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007086"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007087"\n" "v=v*f/i/(n+i)"
7088"\n" "r+=v"
7089"\n" "}"
7090"\n" "scale=s"
7091"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007092"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007093"\n" "return(a*r/1)"
7094"\n" "}"
7095};
7096#endif // ENABLE_BC
7097
Denys Vlasenko10bde142018-12-27 18:23:58 +01007098static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007099{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007100 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007101 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007102 size_t i;
7103
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007104#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007105 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007106 // We know that internal library is not buggy,
7107 // thus error checking is normally disabled.
7108# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007109 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007110 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007111 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007112#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007113
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007114 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007115 fname = (void*)G.files.v;
7116 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007117 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007118 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7119 // Debug config, non-interactive mode:
7120 // return all the way back to main.
7121 // Non-debug builds do not come here
7122 // in non-interactive mode, they exit.
7123 RETURN_STATUS(s);
7124 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007125 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007126
Denys Vlasenko91cde952018-12-10 20:56:08 +01007127 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007128 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007129
Denys Vlasenko19f11072018-12-12 22:48:19 +01007130 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007131}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007132#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007133
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007134#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007135static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007136{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007137 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007138 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007139 bc_vec_free(&G.prog.vars);
7140 bc_vec_free(&G.prog.var_map);
7141 bc_vec_free(&G.prog.arrs);
7142 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007143 IF_DC(bc_vec_free(&G.prog.strs);)
7144 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007145 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007146 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007147 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007148 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007149 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007150 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007151}
7152
Denys Vlasenko10bde142018-12-27 18:23:58 +01007153static void xc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007154{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007155 bc_vec_free(&G.files);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007156 xc_program_free();
7157 xc_parse_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007158 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007159}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007160#endif
7161
Denys Vlasenko10bde142018-12-27 18:23:58 +01007162static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007163{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007164 BcInstPtr ip;
7165
Denys Vlasenko82269122018-12-14 16:30:56 +01007166 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007167 memset(&ip, 0, sizeof(BcInstPtr));
7168
Denys Vlasenko82269122018-12-14 16:30:56 +01007169 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007170 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007171 G.prog.ob_t = 10;
7172
Denys Vlasenko503faf92018-12-20 16:24:18 +01007173 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7174 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007175
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007176 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007177 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007178
Denys Vlasenko503faf92018-12-20 16:24:18 +01007179 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7180 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007181
7182 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007183 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007184
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007185 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007186 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007187 // a valid function name (and be short)
7188 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007189 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007190 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007191 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007192 xc_program_add_fn();
7193 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007194 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007195
7196 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007197 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007198
7199 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007200 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007201
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007202 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7203 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007204 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007205 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7206 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007207
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007208 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007209}
Gavin Howard01055ba2018-11-03 11:00:21 -06007210
Denys Vlasenko10bde142018-12-27 18:23:58 +01007211static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007212{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007213#if ENABLE_FEATURE_EDITING
7214 G.line_input_state = new_line_input_t(DO_HISTORY);
7215#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01007216 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007217
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007218 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007219 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007220 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007221 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007222
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007223//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7224//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007225 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007226#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007227 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007228 // With SA_RESTART, most system calls will restart
7229 // (IOW: they won't fail with EINTR).
7230 // In particular, this means ^C won't cause
7231 // stdout to get into "error state" if SIGINT hits
7232 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007233 //
7234 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007235 // will only be handled after [Enter] since read()
7236 // from stdin is not interrupted by ^C either,
7237 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007238 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007239 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7240
7241 // Without SA_RESTART, this exhibits a bug:
7242 // "while (1) print 1" and try ^C-ing it.
7243 // Intermittently, instead of returning to input line,
7244 // you'll get "output error: Interrupted system call"
7245 // and exit.
7246 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007247#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007248 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007249 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007250 return 0; // "not a tty"
7251}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007252
Denys Vlasenko10bde142018-12-27 18:23:58 +01007253static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007254{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007255 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007256#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007257 if (G_exiting) // it was actually "halt" or "quit"
7258 st = EXIT_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007259 xc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007260# if ENABLE_FEATURE_EDITING
7261 free_line_input_t(G.line_input_state);
7262# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007263 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007264#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007265 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007266 return st;
7267}
7268
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007269#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007270int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007271int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007272{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007273 int is_tty;
7274
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007275 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007276
Denys Vlasenko10bde142018-12-27 18:23:58 +01007277 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007278
7279 bc_args(argv);
7280
7281 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7282 bc_vm_info();
7283
Denys Vlasenko10bde142018-12-27 18:23:58 +01007284 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007285}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007286#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007287
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007288#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007289int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007290int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007291{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007292 int noscript;
7293
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007294 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007295
7296 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7297 // 1 char wider than bc from the same package.
7298 // Both default width, and xC_LINE_LENGTH=N are wider:
7299 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007300 // |1234\ |
7301 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007302 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007303 // |123\ |
7304 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007305 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007306 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007307
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007308 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7309 noscript = BC_FLAG_I;
7310 for (;;) {
7311 int n = getopt(argc, argv, "e:f:x");
7312 if (n <= 0)
7313 break;
7314 switch (n) {
7315 case 'e':
7316 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007317 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007318 if (n) return n;
7319 break;
7320 case 'f':
7321 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007322 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007323 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007324 break;
7325 case 'x':
7326 option_mask32 |= DC_FLAG_X;
7327 break;
7328 default:
7329 bb_show_usage();
7330 }
7331 }
7332 argv += optind;
7333
7334 while (*argv) {
7335 noscript = 0;
7336 bc_vec_push(&G.files, argv++);
7337 }
7338
7339 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7340
Denys Vlasenko10bde142018-12-27 18:23:58 +01007341 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007342}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007343#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007344
7345#endif // not DC_SMALL