blob: 5789869a70281976c956d47007df5a7847c85bd9 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Gavin Howardfa495ce2018-12-18 10:03:14 -07004 * Adapted from https://github.com/gavinhoward/bc
5 * Original code copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06006 */
7//config:config BC
8//config: bool "bc (45 kb; 49 kb when combined with dc)"
9//config: default y
10//config: help
11//config: bc is a command-line, arbitrary-precision calculator with a
12//config: Turing-complete language. See the GNU bc manual
13//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010014//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060015//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010016//config: This bc has five differences to the GNU bc:
17//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060018//config: 2) Arrays are copied before being passed as arguments to
19//config: functions. This behavior is required by the bc spec.
20//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010021//config: the number of elements in the array. This prints "1":
22//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060023//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010024//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060025//config: allows POSIX-compliant scripts to work while somewhat
26//config: preserving expected behavior (versus C) and making parsing
27//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010028//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060029//config:
30//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060031//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010032//config: -q --quiet don't print version and copyright
33//config: -s --standard error if any non-POSIX extensions are used
34//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060035//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010036//config: s(expr) sine in radians
37//config: c(expr) cosine in radians
38//config: a(expr) arctangent, returning radians
39//config: l(expr) natural log
40//config: e(expr) raises e to the power of expr
41//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060042//config:
43//config:config DC
44//config: bool "dc (38 kb; 49 kb when combined with bc)"
45//config: default y
46//config: help
47//config: dc is a reverse-polish notation command-line calculator which
48//config: supports unlimited precision arithmetic. See the FreeBSD man page
49//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010050//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060051//config:
52//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010053//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060054//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010055//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060056//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010057//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060058//config: 4) Like the FreeBSD dc, this dc supports extended registers.
59//config: However, they are implemented differently. When it encounters
60//config: whitespace where a register should be, it skips the whitespace.
61//config: If the character following is not a lowercase letter, an error
62//config: is issued. Otherwise, the register name is parsed by the
63//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010064//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060065//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010066//config: whitespace. Examples:
67//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060068//config: Also note that, like the FreeBSD dc, extended registers are not
69//config: allowed unless the "-x" option is given.
70//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010071//config:config FEATURE_DC_SMALL
72//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
73//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010074//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010075//config:
76//config:config FEATURE_DC_LIBM
77//config: bool "Enable power and exp functions (requires libm)"
78//config: default y
79//config: depends on FEATURE_DC_SMALL
80//config: help
81//config: Enable power and exp functions.
82//config: NOTE: This will require libm to be present for linking.
83//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060084//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010085//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060086//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010087//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060088//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010089//config: Enable interactive mode: when started on a tty,
90//config: ^C interrupts execution and returns to command line,
91//config: errors also return to command line instead of exiting,
92//config: line editing with history is available.
93//config:
94//config: With this option off, input can still be taken from tty,
95//config: but all errors are fatal, ^C is fatal,
96//config: tty is treated exactly the same as any other
97//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060098//config:
99//config:config FEATURE_BC_LONG_OPTIONS
100//config: bool "Enable bc/dc long options"
101//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100102//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600103//config: help
104//config: Enable long options for bc and dc.
105
106//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
107//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
108
109//kbuild:lib-$(CONFIG_BC) += bc.o
110//kbuild:lib-$(CONFIG_DC) += bc.o
111
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100112//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600113//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100114//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600115//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100116//usage:#define bc_full_usage "\n"
117//usage: "\nArbitrary precision calculator"
118//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100119///////: "\n -i Interactive" - has no effect for now
120//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100121//usage: "\n -l Load standard math library"
122//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100123//usage: "\n -w Warn if extensions are used"
124///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100125//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100126//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600127//usage:
128//usage:#define bc_example_usage
129//usage: "3 + 4.129\n"
130//usage: "1903 - 2893\n"
131//usage: "-129 * 213.28935\n"
132//usage: "12 / -1932\n"
133//usage: "12 % 12\n"
134//usage: "34 ^ 189\n"
135//usage: "scale = 13\n"
136//usage: "ibase = 2\n"
137//usage: "obase = A\n"
138//usage:
139//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100140//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600141//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100142//usage:#define dc_full_usage "\n"
143//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100144//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100145//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100146//usage: "\nf - print entire stack"
147//usage: "\nk - pop the value and set the precision"
148//usage: "\ni - pop the value and set input radix"
149//usage: "\no - pop the value and set output radix"
150//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600151//usage:
152//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100153//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600154//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100155//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600156//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100157//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600158//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100159//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600160//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100161//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600162//usage: "64\n"
163
164#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100165#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600166
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100167#if ENABLE_FEATURE_DC_SMALL
168# include "dc.c"
169#else
170
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100171#define DEBUG_LEXER 0
172#define DEBUG_COMPILE 0
173#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100174// This can be left enabled for production as well:
175#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100176
177#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100178static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100179#define dbg_lex(...) \
180 do { \
181 fprintf(stderr, "%*s", lex_indent, ""); \
182 bb_error_msg(__VA_ARGS__); \
183 } while (0)
184#define dbg_lex_enter(...) \
185 do { \
186 dbg_lex(__VA_ARGS__); \
187 lex_indent++; \
188 } while (0)
189#define dbg_lex_done(...) \
190 do { \
191 lex_indent--; \
192 dbg_lex(__VA_ARGS__); \
193 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100194#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100195# define dbg_lex(...) ((void)0)
196# define dbg_lex_enter(...) ((void)0)
197# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100198#endif
199
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100200#if DEBUG_COMPILE
201# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
202#else
203# define dbg_compile(...) ((void)0)
204#endif
205
Denys Vlasenko99b37622018-12-15 20:06:59 +0100206#if DEBUG_EXEC
207# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
208#else
209# define dbg_exec(...) ((void)0)
210#endif
211
Gavin Howard01055ba2018-11-03 11:00:21 -0600212typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100213 BC_STATUS_SUCCESS = 0,
214 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100215 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600216} BcStatus;
217
Gavin Howard01055ba2018-11-03 11:00:21 -0600218#define BC_VEC_INVALID_IDX ((size_t) -1)
219#define BC_VEC_START_CAP (1 << 5)
220
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100221typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600222
223typedef struct BcVec {
224 char *v;
225 size_t len;
226 size_t cap;
227 size_t size;
228 BcVecFree dtor;
229} BcVec;
230
Gavin Howard01055ba2018-11-03 11:00:21 -0600231typedef signed char BcDig;
232
233typedef struct BcNum {
234 BcDig *restrict num;
235 size_t rdx;
236 size_t len;
237 size_t cap;
238 bool neg;
239} BcNum;
240
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100241#define BC_NUM_MAX_IBASE ((unsigned long) 16)
242// larger value might speed up BIGNUM calculations a bit:
243#define BC_NUM_DEF_SIZE (16)
244#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600245
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100246#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600247
Gavin Howard01055ba2018-11-03 11:00:21 -0600248typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100249#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600250 BC_INST_INC_PRE,
251 BC_INST_DEC_PRE,
252 BC_INST_INC_POST,
253 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100254#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100255 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600256
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100257 XC_INST_REL_EQ, // should
258 XC_INST_REL_LE, // match
259 XC_INST_REL_GE, // LEX
260 XC_INST_REL_NE, // constants
261 XC_INST_REL_LT, // for
262 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600263
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100264 XC_INST_POWER, // operations
265 XC_INST_MULTIPLY, // |
266 XC_INST_DIVIDE, // |
267 XC_INST_MODULUS, // |
268 XC_INST_PLUS, // |
269 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600270
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100271 XC_INST_BOOL_NOT, // |
272 XC_INST_BOOL_OR, // |
273 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100274#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100275 BC_INST_ASSIGN_POWER, // |
276 BC_INST_ASSIGN_MULTIPLY,// |
277 BC_INST_ASSIGN_DIVIDE, // |
278 BC_INST_ASSIGN_MODULUS, // |
279 BC_INST_ASSIGN_PLUS, // |
280 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100281#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100282 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600283
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100284 XC_INST_NUM,
285 XC_INST_VAR,
286 XC_INST_ARRAY_ELEM,
287 XC_INST_ARRAY,
288 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100289
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100290 XC_INST_IBASE, // order of these constans should match other enums
291 XC_INST_OBASE, // order of these constans should match other enums
292 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100293 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100294 XC_INST_LENGTH,
295 XC_INST_READ,
296 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600297
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100298 XC_INST_PRINT,
299 XC_INST_PRINT_POP,
300 XC_INST_STR,
301 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600302
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100303#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100304 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600305 BC_INST_JUMP,
306 BC_INST_JUMP_ZERO,
307
308 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600309 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100310#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100311 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600312
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100313 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100314#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100315 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600316
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100317 DC_INST_MODEXP,
318 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600319
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100320 DC_INST_EXECUTE,
321 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600322
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100323 DC_INST_ASCIIFY,
324 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600325
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100326 DC_INST_PRINT_STACK,
327 DC_INST_CLEAR_STACK,
328 DC_INST_STACK_LEN,
329 DC_INST_DUPLICATE,
330 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600331
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100332 DC_INST_LOAD,
333 DC_INST_PUSH_VAR,
334 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600335
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100336 DC_INST_QUIT,
337 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600338
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100339 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600341} BcInst;
342
343typedef struct BcId {
344 char *name;
345 size_t idx;
346} BcId;
347
348typedef struct BcFunc {
349 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100350 IF_BC(BcVec labels;)
351 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100352 IF_BC(BcVec strs;)
353 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100354 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600355} BcFunc;
356
357typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100358 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600359
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100360 XC_RESULT_VAR,
361 XC_RESULT_ARRAY_ELEM,
362 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600363
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100364 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600365
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100366 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
367 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
368 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
369 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100370 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100371 XC_RESULT_CONSTANT,
372 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600373} BcResultType;
374
375typedef union BcResultData {
376 BcNum n;
377 BcVec v;
378 BcId id;
379} BcResultData;
380
381typedef struct BcResult {
382 BcResultType t;
383 BcResultData d;
384} BcResult;
385
386typedef struct BcInstPtr {
387 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100388 size_t inst_idx;
389 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600390} BcInstPtr;
391
Gavin Howard01055ba2018-11-03 11:00:21 -0600392typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100393 XC_LEX_EOF,
394 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600395
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100396 XC_LEX_NLINE,
397 XC_LEX_WHITESPACE,
398 XC_LEX_STR,
399 XC_LEX_NAME,
400 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100401
Denys Vlasenko69560f42018-12-24 14:14:23 +0100402 XC_LEX_1st_op,
403 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100404
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100405 XC_LEX_OP_REL_EQ, // should
406 XC_LEX_OP_REL_LE, // match
407 XC_LEX_OP_REL_GE, // INST
408 XC_LEX_OP_REL_NE, // constants
409 XC_LEX_OP_REL_LT, // for
410 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100411
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100412 XC_LEX_OP_POWER, // operations
413 XC_LEX_OP_MULTIPLY, // |
414 XC_LEX_OP_DIVIDE, // |
415 XC_LEX_OP_MODULUS, // |
416 XC_LEX_OP_PLUS, // |
417 XC_LEX_OP_MINUS, // |
418 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100419#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100420 BC_LEX_OP_BOOL_NOT, // |
421 BC_LEX_OP_BOOL_OR, // |
422 BC_LEX_OP_BOOL_AND, // |
423
424 BC_LEX_OP_ASSIGN_POWER, // |
425 BC_LEX_OP_ASSIGN_MULTIPLY, // |
426 BC_LEX_OP_ASSIGN_DIVIDE, // |
427 BC_LEX_OP_ASSIGN_MODULUS, // |
428 BC_LEX_OP_ASSIGN_PLUS, // |
429 BC_LEX_OP_ASSIGN_MINUS, // |
430
431 BC_LEX_OP_ASSIGN, // V
432
Gavin Howard01055ba2018-11-03 11:00:21 -0600433 BC_LEX_OP_INC,
434 BC_LEX_OP_DEC,
435
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100436 BC_LEX_LPAREN, // () are 0x28 and 0x29
437 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600438
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100439 BC_LEX_LBRACKET, // [] are 0x5B and 5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600440 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100441 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600442
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100443 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600444 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100445 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600446
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100447 BC_LEX_KEY_1st_keyword,
448 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600449 BC_LEX_KEY_BREAK,
450 BC_LEX_KEY_CONTINUE,
451 BC_LEX_KEY_DEFINE,
452 BC_LEX_KEY_ELSE,
453 BC_LEX_KEY_FOR,
454 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100455 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
456 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
457 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600458 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100459 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600460 BC_LEX_KEY_LENGTH,
461 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600462 BC_LEX_KEY_PRINT,
463 BC_LEX_KEY_QUIT,
464 BC_LEX_KEY_READ,
465 BC_LEX_KEY_RETURN,
466 BC_LEX_KEY_SCALE,
467 BC_LEX_KEY_SQRT,
468 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100469#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600470
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100471#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100472 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
473 DC_LEX_OP_ASSIGN,
474
475 DC_LEX_LPAREN,
476 DC_LEX_SCOLON,
477 DC_LEX_READ,
478 DC_LEX_IBASE,
479 DC_LEX_SCALE,
480 DC_LEX_OBASE,
481 DC_LEX_LENGTH,
482 DC_LEX_PRINT,
483 DC_LEX_QUIT,
484 DC_LEX_SQRT,
485 DC_LEX_LBRACE,
486
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100487 DC_LEX_EQ_NO_REG,
488 DC_LEX_OP_MODEXP,
489 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600490
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100491 DC_LEX_COLON,
492 DC_LEX_ELSE,
493 DC_LEX_EXECUTE,
494 DC_LEX_PRINT_STACK,
495 DC_LEX_CLEAR_STACK,
496 DC_LEX_STACK_LEVEL,
497 DC_LEX_DUPLICATE,
498 DC_LEX_SWAP,
499 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600500
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100501 DC_LEX_ASCIIFY,
502 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600503
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100504 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
505 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
506 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
507 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
508 DC_LEX_LOAD,
509 DC_LEX_LOAD_POP,
510 DC_LEX_STORE_PUSH,
511 DC_LEX_PRINT_POP,
512 DC_LEX_NQUIT,
513 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100514#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600515} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100516// must match order of BC_LEX_KEY_foo etc above
517#if ENABLE_BC
518struct BcLexKeyword {
519 char name8[8];
520};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100521#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100522 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100523static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100524 LEX_KW_ENTRY("auto" , 1), // 0
525 LEX_KW_ENTRY("break" , 1), // 1
526 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
527 LEX_KW_ENTRY("define" , 1), // 3
528 LEX_KW_ENTRY("else" , 0), // 4
529 LEX_KW_ENTRY("for" , 1), // 5
530 LEX_KW_ENTRY("halt" , 0), // 6
531 LEX_KW_ENTRY("ibase" , 1), // 7
532 LEX_KW_ENTRY("obase" , 1), // 8
533 LEX_KW_ENTRY("if" , 1), // 9
534 LEX_KW_ENTRY("last" , 0), // 10
535 LEX_KW_ENTRY("length" , 1), // 11
536 LEX_KW_ENTRY("limits" , 0), // 12
537 LEX_KW_ENTRY("print" , 0), // 13
538 LEX_KW_ENTRY("quit" , 1), // 14
539 LEX_KW_ENTRY("read" , 0), // 15
540 LEX_KW_ENTRY("return" , 1), // 16
541 LEX_KW_ENTRY("scale" , 1), // 17
542 LEX_KW_ENTRY("sqrt" , 1), // 18
543 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100544};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100545#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100547#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100548#define STRING_if (bc_lex_kws[9].name8)
549#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100550enum {
551 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100552 | (1 << 0) // 0
553 | (1 << 1) // 1
554 | (0 << 2) // 2
555 | (1 << 3) // 3
556 | (0 << 4) // 4
557 | (1 << 5) // 5
558 | (0 << 6) // 6
559 | (1 << 7) // 7
560 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100561 | (1 << 9) // 9
562 | (0 << 10) // 10
563 | (1 << 11) // 11
564 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100565 | (0 << 13) // 13
566 | (1 << 14) // 14
567 | (0 << 15) // 15
568 | (1 << 16) // 16
569 | (1 << 17) // 17
570 | (1 << 18) // 18
571 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100572};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100573#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100574
575// This is a bit array that corresponds to token types. An entry is
576// true if the token is valid in an expression, false otherwise.
577// Used to figure out when expr parsing should stop *without error message*
578// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100579// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100580// by later processing.
581enum {
582#define EXBITS(a,b,c,d,e,f,g,h) \
583 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100584 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100585 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100586 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
587 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100588 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
589 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100590 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
591 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
592 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100593#undef EXBITS
594};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100595static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100596{
597#if ULONG_MAX > 0xffffffff
598 // 64-bit version (will not work correctly for 32-bit longs!)
599 return BC_PARSE_EXPRS_BITS & (1UL << i);
600#else
601 // 32-bit version
602 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
603 if (i >= 32) {
604 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
605 i &= 31;
606 }
607 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100608#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100609}
610
611// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100612// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100613static const uint8_t bc_parse_ops[] = {
614#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100615 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100616 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100617 OP(2, false), // pow
618 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
619 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100620 OP(1, false), // not
621 OP(7, true ), OP( 7, true ), // or and
622 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
623 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100624 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100625#undef OP
626};
627#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
628#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
629#endif // ENABLE_BC
630
631#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100632static const //BcLexType - should be this type
633uint8_t
634dc_char_to_LEX[] = {
635 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100636 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100637 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100638 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100639 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100640 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100641 /* 0123456789 */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
644 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100645 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100646 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
647 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100648 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100649 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
650 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100651 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100652 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
653 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100654 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100655 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
656 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100657 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100658 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100659 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100660 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100661 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100662 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
663 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100664 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100665 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100666 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100667 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100668 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
669 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100670 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100671 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100672 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100673 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100674};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100675static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100676int8_t
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100677dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
678 XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY
679 XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS
680 XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100681 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
682 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
683 XC_INST_REL_GT, // DC_LEX_LPAREN
684 DC_INST_INVALID, // DC_LEX_SCOLON
685 DC_INST_INVALID, // DC_LEX_READ
686 XC_INST_IBASE, // DC_LEX_IBASE
687 XC_INST_SCALE, // DC_LEX_SCALE
688 XC_INST_OBASE, // DC_LEX_OBASE
689 XC_INST_LENGTH, // DC_LEX_LENGTH
690 XC_INST_PRINT, // DC_LEX_PRINT
691 DC_INST_QUIT, // DC_LEX_QUIT
692 XC_INST_SQRT, // DC_LEX_SQRT
693 XC_INST_REL_GE, // DC_LEX_LBRACE
694 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
695 DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD
696 DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE
697 DC_INST_EXECUTE, // EXECUTE
698 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK
699 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE
700 DC_INST_SWAP, XC_INST_POP, // SWAP POP
701 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM
702 DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE
703 DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD
704 DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH
705 XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT
706 XC_INST_SCALE_FUNC, // SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100707 // DC_INST_INVALID in this table either means that corresponding LEX
708 // is not possible for dc, or that it does not compile one-to-one
709 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100710};
711#endif // ENABLE_DC
712
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100713typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100714 smallint lex; // was BcLexType // first member is most used
715 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100716 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100717 const char *lex_inbuf;
Denys Vlasenkoc192b042018-12-25 23:15:59 +0100718 const char *lex_end;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100719 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100720 const char *lex_filename;
721 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100722 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100723
Gavin Howard01055ba2018-11-03 11:00:21 -0600724 BcFunc *func;
725 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100726 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100727 IF_BC(BcVec exits;)
728 IF_BC(BcVec conds;)
729 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600730} BcParse;
731
Gavin Howard01055ba2018-11-03 11:00:21 -0600732typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600733 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100734 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600735
Denys Vlasenko503faf92018-12-20 16:24:18 +0100736 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600737 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600738 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600739
Gavin Howard01055ba2018-11-03 11:00:21 -0600740 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100741 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600742
743 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100744 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600745
746 BcVec vars;
747 BcVec var_map;
748
749 BcVec arrs;
750 BcVec arr_map;
751
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100752 IF_DC(BcVec strs;)
753 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600754
Gavin Howard01055ba2018-11-03 11:00:21 -0600755 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100756 IF_BC(BcNum one;)
757 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600758} BcProgram;
759
Denys Vlasenko6e618232018-12-25 22:20:14 +0100760struct globals {
761 BcParse prs; // first member is most used
762
763 // For error messages. Can be set to current parsed line,
764 // or [TODO] to current executing line (can be before last parsed one)
765 unsigned err_line;
766
767 BcVec input_buffer;
768
769 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
770 IF_FEATURE_CLEAN_UP(smallint exiting;)
771
772 BcProgram prog;
773
774 BcVec files;
775
776 char *env_args;
777
778#if ENABLE_FEATURE_EDITING
779 line_input_t *line_input_state;
780#endif
781} FIX_ALIASING;
782#define G (*ptr_to_globals)
783#define INIT_G() do { \
784 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
785} while (0)
786#define FREE_G() do { \
787 FREE_PTR_TO_GLOBALS(); \
788} while (0)
789#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
790#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
791#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
792#if ENABLE_FEATURE_BC_SIGNALS
793# define G_interrupt bb_got_signal
794# define G_ttyin G.ttyin
795#else
796# define G_interrupt 0
797# define G_ttyin 0
798#endif
799#if ENABLE_FEATURE_CLEAN_UP
800# define G_exiting G.exiting
801#else
802# define G_exiting 0
803#endif
804#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
805#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
806
807#define BC_PARSE_STREND (0xff)
808
809#if ENABLE_BC
810# define BC_PARSE_REL (1 << 0)
811# define BC_PARSE_PRINT (1 << 1)
812# define BC_PARSE_ARRAY (1 << 2)
813# define BC_PARSE_NOCALL (1 << 3)
814#endif
815
Gavin Howard01055ba2018-11-03 11:00:21 -0600816#define BC_PROG_MAIN (0)
817#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100818#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600819#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100820#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600821
822#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
823#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100824 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600825
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100826#define BC_FLAG_W (1 << 0)
827#define BC_FLAG_V (1 << 1)
828#define BC_FLAG_S (1 << 2)
829#define BC_FLAG_Q (1 << 3)
830#define BC_FLAG_L (1 << 4)
831#define BC_FLAG_I (1 << 5)
832#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600833
834#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
835#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
836
Denys Vlasenko64074a12018-12-07 15:50:14 +0100837#define BC_MAX_OBASE ((unsigned) 999)
838#define BC_MAX_DIM ((unsigned) INT_MAX)
839#define BC_MAX_SCALE ((unsigned) UINT_MAX)
840#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
841#define BC_MAX_NUM BC_MAX_STRING
842// Unused apart from "limits" message. Just show a "biggish number" there.
843//#define BC_MAX_NAME BC_MAX_STRING
844//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
845//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
846#define BC_MAX_NAME_STR "999999999"
847#define BC_MAX_EXP_STR "999999999"
848#define BC_MAX_VARS_STR "999999999"
849
850#define BC_MAX_OBASE_STR "999"
851
852#if INT_MAX == 2147483647
853# define BC_MAX_DIM_STR "2147483647"
854#elif INT_MAX == 9223372036854775807
855# define BC_MAX_DIM_STR "9223372036854775807"
856#else
857# error Strange INT_MAX
858#endif
859
860#if UINT_MAX == 4294967295
861# define BC_MAX_SCALE_STR "4294967295"
862# define BC_MAX_STRING_STR "4294967294"
863#elif UINT_MAX == 18446744073709551615
864# define BC_MAX_SCALE_STR "18446744073709551615"
865# define BC_MAX_STRING_STR "18446744073709551614"
866#else
867# error Strange UINT_MAX
868#endif
869#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600870
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100871// In configurations where errors abort instead of propagating error
872// return code up the call chain, functions returning BC_STATUS
873// actually don't return anything, they always succeed and return "void".
874// A macro wrapper is provided, which makes this statement work:
875// s = zbc_func(...)
876// and makes it visible to the compiler that s is always zero,
877// allowing compiler to optimize dead code after the statement.
878//
879// To make code more readable, each such function has a "z"
880// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
881//
882#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100883# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100884# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100885# define IF_ERROR_RETURN_POSSIBLE(a) a
886# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100887# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100888# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100889#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100890# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100891# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100892# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
893# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100894# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100895# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100896#endif
897
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100898//
899// Utility routines
900//
Gavin Howard01055ba2018-11-03 11:00:21 -0600901
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100902static void fflush_and_check(void)
903{
904 fflush_all();
905 if (ferror(stdout) || ferror(stderr))
906 bb_perror_msg_and_die("output error");
907}
908
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100909#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100910#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100911do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100912 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100913 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100914 return BC_STATUS_FAILURE; \
915} while (0)
916#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100917#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100918#endif
919
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100920static void quit(void) NORETURN;
921static void quit(void)
922{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100923 if (ferror(stdin))
924 bb_perror_msg_and_die("input error");
925 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100926 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100927 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100928}
929
Denys Vlasenko5318f812018-12-05 17:48:01 +0100930static void bc_verror_msg(const char *fmt, va_list p)
931{
Denys Vlasenko82269122018-12-14 16:30:56 +0100932 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100933 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100934 sv = applet_name;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100935 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100936 }
937 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100938 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100939 free((char*)applet_name);
940 applet_name = sv;
941 }
942}
943
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100944static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100945{
946 va_list p;
947
948 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100949 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100950 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100951
Denys Vlasenkoec603182018-12-17 10:34:02 +0100952 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
953 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
954 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100955}
956
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100957#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100958static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100959{
960 va_list p;
961
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100962 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100963 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100964 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100965
966 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100967 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100968 va_end(p);
969
970 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100971 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100972 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
973
Denys Vlasenkoec603182018-12-17 10:34:02 +0100974 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100975 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100976 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100977}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100978#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100979#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100980
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100981// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
982// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100983// function must not have caller-cleaned parameters on stack.
984// Unfortunately, vararg function API does exactly that on most arches.
985// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100986static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100987{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100988 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100989}
990static ERRORFUNC int bc_error_bad_character(char c)
991{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100992 if (!c)
993 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100994 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100995}
996static ERRORFUNC int bc_error_bad_expression(void)
997{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100998 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100999}
1000static ERRORFUNC int bc_error_bad_token(void)
1001{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001002 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001003}
1004static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1005{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001006 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001007}
1008static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1009{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001010 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001011}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001012#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001013static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001014{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001015 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001016}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001017#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1018static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001019{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001020 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001021}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001022#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1023static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001024{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001025 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 +01001026}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001027#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)
1028static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001029{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001030 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001031}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001032#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 +01001033#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001034
Gavin Howard01055ba2018-11-03 11:00:21 -06001035static void bc_vec_grow(BcVec *v, size_t n)
1036{
1037 size_t cap = v->cap * 2;
1038 while (cap < v->len + n) cap *= 2;
1039 v->v = xrealloc(v->v, v->size * cap);
1040 v->cap = cap;
1041}
1042
1043static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1044{
1045 v->size = esize;
1046 v->cap = BC_VEC_START_CAP;
1047 v->len = 0;
1048 v->dtor = dtor;
1049 v->v = xmalloc(esize * BC_VEC_START_CAP);
1050}
1051
Denys Vlasenko7d628012018-12-04 21:46:47 +01001052static void bc_char_vec_init(BcVec *v)
1053{
1054 bc_vec_init(v, sizeof(char), NULL);
1055}
1056
Gavin Howard01055ba2018-11-03 11:00:21 -06001057static void bc_vec_expand(BcVec *v, size_t req)
1058{
1059 if (v->cap < req) {
1060 v->v = xrealloc(v->v, v->size * req);
1061 v->cap = req;
1062 }
1063}
1064
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001065static void bc_vec_pop(BcVec *v)
1066{
1067 v->len--;
1068 if (v->dtor)
1069 v->dtor(v->v + (v->size * v->len));
1070}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001071
Gavin Howard01055ba2018-11-03 11:00:21 -06001072static void bc_vec_npop(BcVec *v, size_t n)
1073{
1074 if (!v->dtor)
1075 v->len -= n;
1076 else {
1077 size_t len = v->len - n;
1078 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1079 }
1080}
1081
Denys Vlasenko7d628012018-12-04 21:46:47 +01001082static void bc_vec_pop_all(BcVec *v)
1083{
1084 bc_vec_npop(v, v->len);
1085}
1086
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001087static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001088{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001089 size_t len = v->len;
1090 if (len >= v->cap) bc_vec_grow(v, 1);
1091 memmove(v->v + (v->size * len), data, v->size);
1092 v->len++;
1093 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001094}
1095
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001096// G.prog.results often needs "pop old operand, push result" idiom.
1097// Can do this without a few extra ops
1098static size_t bc_result_pop_and_push(const void *data)
1099{
1100 BcVec *v = &G.prog.results;
1101 char *last;
1102 size_t len = v->len - 1;
1103
1104 last = v->v + (v->size * len);
1105 if (v->dtor)
1106 v->dtor(last);
1107 memmove(last, data, v->size);
1108 return len;
1109}
1110
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001111static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001112{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001113 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001114}
1115
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001116static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001117{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001118 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001119 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001121}
1122
Gavin Howard01055ba2018-11-03 11:00:21 -06001123static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1124{
1125 if (idx == v->len)
1126 bc_vec_push(v, data);
1127 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001128 char *ptr;
1129
1130 if (v->len == v->cap) bc_vec_grow(v, 1);
1131
1132 ptr = v->v + v->size * idx;
1133
1134 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1135 memmove(ptr, data, v->size);
1136 }
1137}
1138
1139static void bc_vec_string(BcVec *v, size_t len, const char *str)
1140{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001141 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001142 bc_vec_expand(v, len + 1);
1143 memcpy(v->v, str, len);
1144 v->len = len;
1145
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001146 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001147}
1148
Gavin Howard01055ba2018-11-03 11:00:21 -06001149static void *bc_vec_item(const BcVec *v, size_t idx)
1150{
1151 return v->v + v->size * idx;
1152}
1153
1154static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1155{
1156 return v->v + v->size * (v->len - idx - 1);
1157}
1158
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001159static void *bc_vec_top(const BcVec *v)
1160{
1161 return v->v + v->size * (v->len - 1);
1162}
1163
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001164static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001165{
1166 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001167 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168 free(v->v);
1169}
1170
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001171static BcFunc* bc_program_func(size_t idx)
1172{
1173 return bc_vec_item(&G.prog.fns, idx);
1174}
1175// BC_PROG_MAIN is zeroth element, so:
1176#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1177
1178#if ENABLE_BC
1179static BcFunc* bc_program_current_func(void)
1180{
1181 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1182 BcFunc *func = bc_program_func(ip->func);
1183 return func;
1184}
1185#endif
1186
1187static char** bc_program_str(size_t idx)
1188{
1189#if ENABLE_BC
1190 if (IS_BC) {
1191 BcFunc *func = bc_program_current_func();
1192 return bc_vec_item(&func->strs, idx);
1193 }
1194#endif
1195 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1196}
1197
1198static char** bc_program_const(size_t idx)
1199{
1200#if ENABLE_BC
1201 if (IS_BC) {
1202 BcFunc *func = bc_program_current_func();
1203 return bc_vec_item(&func->consts, idx);
1204 }
1205#endif
1206 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1207}
1208
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001209static int bc_id_cmp(const void *e1, const void *e2)
1210{
1211 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1212}
1213
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001214static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001215{
1216 free(((BcId *) id)->name);
1217}
1218
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001219static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001220{
1221 size_t low = 0, high = v->len;
1222
1223 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001224 size_t mid = (low + high) / 2;
1225 BcId *id = bc_vec_item(v, mid);
1226 int result = bc_id_cmp(ptr, id);
1227
1228 if (result == 0)
1229 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001230 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001231 high = mid;
1232 else
1233 low = mid + 1;
1234 }
1235
1236 return low;
1237}
1238
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001239static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001240{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001241 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001242
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001243 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001244 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001245 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1246 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001247 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001248 bc_vec_pushAt(v, ptr, n);
1249 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001250}
1251
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001252#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001253static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001254{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001255 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001256 if (i >= v->len) return BC_VEC_INVALID_IDX;
1257 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1258}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001259#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001260
Gavin Howard01055ba2018-11-03 11:00:21 -06001261static void bc_num_setToZero(BcNum *n, size_t scale)
1262{
1263 n->len = 0;
1264 n->neg = false;
1265 n->rdx = scale;
1266}
1267
1268static void bc_num_zero(BcNum *n)
1269{
1270 bc_num_setToZero(n, 0);
1271}
1272
1273static void bc_num_one(BcNum *n)
1274{
1275 bc_num_setToZero(n, 0);
1276 n->len = 1;
1277 n->num[0] = 1;
1278}
1279
Denys Vlasenko3129f702018-12-09 12:04:44 +01001280// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001281static void bc_num_init(BcNum *n, size_t req)
1282{
1283 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001284 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001285 n->num = xmalloc(req);
1286 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001287 n->rdx = 0;
1288 n->len = 0;
1289 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001290}
1291
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001292static void bc_num_init_DEF_SIZE(BcNum *n)
1293{
1294 bc_num_init(n, BC_NUM_DEF_SIZE);
1295}
1296
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001297static void bc_num_expand(BcNum *n, size_t req)
1298{
1299 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1300 if (req > n->cap) {
1301 n->num = xrealloc(n->num, req);
1302 n->cap = req;
1303 }
1304}
1305
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001306static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001307{
1308 free(((BcNum *) num)->num);
1309}
1310
1311static void bc_num_copy(BcNum *d, BcNum *s)
1312{
1313 if (d != s) {
1314 bc_num_expand(d, s->cap);
1315 d->len = s->len;
1316 d->neg = s->neg;
1317 d->rdx = s->rdx;
1318 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1319 }
1320}
1321
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001322static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001323{
1324 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001325 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001326
Denys Vlasenko1557b762018-12-22 21:37:46 +01001327 result = 0;
1328 i = n->len;
1329 while (i > n->rdx) {
1330 unsigned long prev = result;
1331 result = result * 10 + n->num[--i];
1332 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1333 // 0x1ff00000 * 10 is 0x13f600000,
1334 // or 0x3f600000 truncated to 32 bits. Which is larger.
1335 // However, (N*10)/8 < N check is always correct.
1336 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001337 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001338 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001339 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001340
Denys Vlasenko29301232018-12-11 15:29:32 +01001341 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001342}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001343#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1344
1345static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1346{
1347 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1348
1349 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1350}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001351#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001352
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001353#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1354# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1355#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1356# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1357#endif
1358// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1359// would not hit realloc() code path - not good if num[] is not malloced
1360
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001361static void bc_num_ulong2num(BcNum *n, unsigned long val)
1362{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001363 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001364
1365 bc_num_zero(n);
1366
1367 if (val == 0) return;
1368
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001369 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001370
1371 ptr = n->num;
1372 for (;;) {
1373 n->len++;
1374 *ptr++ = val % 10;
1375 val /= 10;
1376 if (val == 0) break;
1377 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001378}
1379
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001380static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001381{
1382 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001383 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001384 a[i] -= b[i];
1385 for (j = i; a[j] < 0;) {
1386 a[j++] += 10;
1387 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001388 }
1389 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001390}
1391
1392static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1393{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001394 size_t i = len;
1395 for (;;) {
1396 int c;
1397 if (i == 0)
1398 return 0;
1399 i--;
1400 c = a[i] - b[i];
1401 if (c != 0) {
1402 i++;
1403 if (c < 0)
1404 return -i;
1405 return i;
1406 }
1407 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001408}
1409
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001410#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1411#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1412#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1413//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1414static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1415{
1416 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1417}
1418//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1419static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1420{
1421 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1422}
1423
Gavin Howard01055ba2018-11-03 11:00:21 -06001424static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1425{
1426 size_t i, min, a_int, b_int, diff;
1427 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001428 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001429 ssize_t cmp;
1430
1431 if (a == b) return 0;
1432 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1433 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001434
1435 if (a->neg != b->neg) // signs of a and b differ
1436 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1437 return (int)b->neg - (int)a->neg;
1438 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001439
1440 a_int = BC_NUM_INT(a);
1441 b_int = BC_NUM_INT(b);
1442 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001443
1444 if (a_int != 0) return (ssize_t) a_int;
1445
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001446 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001447 if (a_max) {
1448 min = b->rdx;
1449 diff = a->rdx - b->rdx;
1450 max_num = a->num + diff;
1451 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001452 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1453 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001454 min = a->rdx;
1455 diff = b->rdx - a->rdx;
1456 max_num = b->num + diff;
1457 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001458 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001459 }
1460
1461 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001462 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001463
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001464 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001465 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001466 }
1467
1468 return 0;
1469}
1470
1471static void bc_num_truncate(BcNum *n, size_t places)
1472{
1473 if (places == 0) return;
1474
1475 n->rdx -= places;
1476
1477 if (n->len != 0) {
1478 n->len -= places;
1479 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1480 }
1481}
1482
1483static void bc_num_extend(BcNum *n, size_t places)
1484{
1485 size_t len = n->len + places;
1486
1487 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001488 if (n->cap < len) bc_num_expand(n, len);
1489
1490 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1491 memset(n->num, 0, sizeof(BcDig) * places);
1492
1493 n->len += places;
1494 n->rdx += places;
1495 }
1496}
1497
1498static void bc_num_clean(BcNum *n)
1499{
1500 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1501 if (n->len == 0)
1502 n->neg = false;
1503 else if (n->len < n->rdx)
1504 n->len = n->rdx;
1505}
1506
1507static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1508{
1509 if (n->rdx < scale)
1510 bc_num_extend(n, scale - n->rdx);
1511 else
1512 bc_num_truncate(n, n->rdx - scale);
1513
1514 bc_num_clean(n);
1515 if (n->len != 0) n->neg = !neg1 != !neg2;
1516}
1517
1518static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1519 BcNum *restrict b)
1520{
1521 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001522 b->len = n->len - idx;
1523 a->len = idx;
1524 a->rdx = b->rdx = 0;
1525
1526 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1527 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001528 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001529 bc_num_zero(b);
1530 bc_num_copy(a, n);
1531 }
1532
1533 bc_num_clean(a);
1534 bc_num_clean(b);
1535}
1536
Denys Vlasenko29301232018-12-11 15:29:32 +01001537static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001538{
Denys Vlasenko29301232018-12-11 15:29:32 +01001539 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001540
1541 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1542 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1543 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001544 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001545 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001546
1547 if (n->rdx >= places)
1548 n->rdx -= places;
1549 else {
1550 bc_num_extend(n, places - n->rdx);
1551 n->rdx = 0;
1552 }
1553
1554 bc_num_clean(n);
1555
Denys Vlasenko29301232018-12-11 15:29:32 +01001556 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001557}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001558#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001559
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001560typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1561
1562static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1563 BcNumBinaryOp op, size_t req)
1564{
1565 BcStatus s;
1566 BcNum num2, *ptr_a, *ptr_b;
1567 bool init = false;
1568
1569 if (c == a) {
1570 ptr_a = &num2;
1571 memcpy(ptr_a, c, sizeof(BcNum));
1572 init = true;
1573 } else
1574 ptr_a = a;
1575
1576 if (c == b) {
1577 ptr_b = &num2;
1578 if (c != a) {
1579 memcpy(ptr_b, c, sizeof(BcNum));
1580 init = true;
1581 }
1582 } else
1583 ptr_b = b;
1584
1585 if (init)
1586 bc_num_init(c, req);
1587 else
1588 bc_num_expand(c, req);
1589
1590 s = BC_STATUS_SUCCESS;
1591 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1592
1593 if (init) bc_num_free(&num2);
1594
1595 RETURN_STATUS(s);
1596}
1597#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1598
1599static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1600static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1601static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1602static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1603static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1604static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1605
1606static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1607{
1608 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1609 (void) scale;
1610 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1611}
1612
1613static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1614{
1615 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1616 (void) scale;
1617 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1618}
1619
1620static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1621{
1622 size_t req = BC_NUM_MREQ(a, b, scale);
1623 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1624}
1625
1626static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1627{
1628 size_t req = BC_NUM_MREQ(a, b, scale);
1629 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1630}
1631
1632static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1633{
1634 size_t req = BC_NUM_MREQ(a, b, scale);
1635 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1636}
1637
1638static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1639{
1640 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1641}
1642
1643static const BcNumBinaryOp zbc_program_ops[] = {
1644 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1645};
1646#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1647#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1648#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1649#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1650#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1651#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1652
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001653static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001654{
1655 BcNum one;
1656 BcDig num[2];
1657
1658 one.cap = 2;
1659 one.num = num;
1660 bc_num_one(&one);
1661
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001662 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001663}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001664#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001665
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001666static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001667{
1668 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1669 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001670 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001671
1672 // Because this function doesn't need to use scale (per the bc spec),
1673 // I am hijacking it to say whether it's doing an add or a subtract.
1674
1675 if (a->len == 0) {
1676 bc_num_copy(c, b);
1677 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001678 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001679 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001680 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001681 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001682 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001683 }
1684
1685 c->neg = a->neg;
1686 c->rdx = BC_MAX(a->rdx, b->rdx);
1687 min_rdx = BC_MIN(a->rdx, b->rdx);
1688 c->len = 0;
1689
1690 if (a->rdx > b->rdx) {
1691 diff = a->rdx - b->rdx;
1692 ptr = a->num;
1693 ptr_a = a->num + diff;
1694 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001695 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001696 diff = b->rdx - a->rdx;
1697 ptr = b->num;
1698 ptr_a = a->num;
1699 ptr_b = b->num + diff;
1700 }
1701
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001702 ptr_c = c->num;
1703 for (i = 0; i < diff; ++i, ++c->len)
1704 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001705
1706 ptr_c += diff;
1707 a_int = BC_NUM_INT(a);
1708 b_int = BC_NUM_INT(b);
1709
1710 if (a_int > b_int) {
1711 min_int = b_int;
1712 max = a_int;
1713 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001714 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001715 min_int = a_int;
1716 max = b_int;
1717 ptr = ptr_b;
1718 }
1719
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001720 carry = 0;
1721 for (i = 0; i < min_rdx + min_int; ++i) {
1722 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001723 carry = in / 10;
1724 ptr_c[i] = (BcDig)(in % 10);
1725 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001726 for (; i < max + min_rdx; ++i) {
1727 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001728 carry = in / 10;
1729 ptr_c[i] = (BcDig)(in % 10);
1730 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001731 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001732
1733 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1734
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001735 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001736}
1737
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001738static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001739{
Gavin Howard01055ba2018-11-03 11:00:21 -06001740 ssize_t cmp;
1741 BcNum *minuend, *subtrahend;
1742 size_t start;
1743 bool aneg, bneg, neg;
1744
1745 // Because this function doesn't need to use scale (per the bc spec),
1746 // I am hijacking it to say whether it's doing an add or a subtract.
1747
1748 if (a->len == 0) {
1749 bc_num_copy(c, b);
1750 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001751 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001752 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001753 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001754 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001755 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001756 }
1757
1758 aneg = a->neg;
1759 bneg = b->neg;
1760 a->neg = b->neg = false;
1761
1762 cmp = bc_num_cmp(a, b);
1763
1764 a->neg = aneg;
1765 b->neg = bneg;
1766
1767 if (cmp == 0) {
1768 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001769 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001770 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001771 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001772 neg = a->neg;
1773 minuend = a;
1774 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001775 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001776 neg = b->neg;
1777 if (sub) neg = !neg;
1778 minuend = b;
1779 subtrahend = a;
1780 }
1781
1782 bc_num_copy(c, minuend);
1783 c->neg = neg;
1784
1785 if (c->rdx < subtrahend->rdx) {
1786 bc_num_extend(c, subtrahend->rdx - c->rdx);
1787 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001788 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001789 start = c->rdx - subtrahend->rdx;
1790
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001791 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001792
1793 bc_num_clean(c);
1794
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001795 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001796}
1797
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001798static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001799 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001800#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001801{
1802 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001803 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001804 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001805 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001806
Gavin Howard01055ba2018-11-03 11:00:21 -06001807 if (a->len == 0 || b->len == 0) {
1808 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001809 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001810 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001811 aone = BC_NUM_ONE(a);
1812 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001814 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001815 }
1816
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001817 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1818 || a->len < BC_NUM_KARATSUBA_LEN
1819 || b->len < BC_NUM_KARATSUBA_LEN
1820 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001821 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001822
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 bc_num_expand(c, a->len + b->len + 1);
1824
1825 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001826 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001827
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001828 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001829 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001830 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001831 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001832 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001833 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 carry = in / 10;
1835 c->num[i + j] = (BcDig)(in % 10);
1836 }
1837
1838 c->num[i + j] += (BcDig) carry;
1839 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001840
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001842 // a=2^1000000
1843 // a*a <- without check below, this will not be interruptible
1844 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001845#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001846 }
1847
1848 c->len = len;
1849
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001850 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001851 }
1852
1853 bc_num_init(&l1, max);
1854 bc_num_init(&h1, max);
1855 bc_num_init(&l2, max);
1856 bc_num_init(&h2, max);
1857 bc_num_init(&m1, max);
1858 bc_num_init(&m2, max);
1859 bc_num_init(&z0, max);
1860 bc_num_init(&z1, max);
1861 bc_num_init(&z2, max);
1862 bc_num_init(&temp, max + max);
1863
1864 bc_num_split(a, max2, &l1, &h1);
1865 bc_num_split(b, max2, &l2, &h2);
1866
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001867 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001868 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001869 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 if (s) goto err;
1871
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001872 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001874 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001876 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 if (s) goto err;
1878
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001879 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001880 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001881 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001882 if (s) goto err;
1883
Denys Vlasenko29301232018-12-11 15:29:32 +01001884 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001885 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001886 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001887 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001888 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001889 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001890 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001891 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 bc_num_free(&temp);
1893 bc_num_free(&z2);
1894 bc_num_free(&z1);
1895 bc_num_free(&z0);
1896 bc_num_free(&m2);
1897 bc_num_free(&m1);
1898 bc_num_free(&h2);
1899 bc_num_free(&l2);
1900 bc_num_free(&h1);
1901 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001902 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001903}
1904
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001905static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001906{
1907 BcStatus s;
1908 BcNum cpa, cpb;
1909 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1910
1911 scale = BC_MAX(scale, a->rdx);
1912 scale = BC_MAX(scale, b->rdx);
1913 scale = BC_MIN(a->rdx + b->rdx, scale);
1914 maxrdx = BC_MAX(maxrdx, scale);
1915
1916 bc_num_init(&cpa, a->len);
1917 bc_num_init(&cpb, b->len);
1918
1919 bc_num_copy(&cpa, a);
1920 bc_num_copy(&cpb, b);
1921 cpa.neg = cpb.neg = false;
1922
Denys Vlasenko29301232018-12-11 15:29:32 +01001923 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001924 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001925 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001926 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001927 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001928 if (s) goto err;
1929
1930 maxrdx += scale;
1931 bc_num_expand(c, c->len + maxrdx);
1932
1933 if (c->len < maxrdx) {
1934 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1935 c->len += maxrdx;
1936 }
1937
1938 c->rdx = maxrdx;
1939 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001940 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001941 bc_num_free(&cpb);
1942 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001943 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001944}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001945#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001946
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001947static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001948{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001949 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 size_t len, end, i;
1951 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001952
1953 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001954 RETURN_STATUS(bc_error("divide by zero"));
1955 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001957 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001958 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001959 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001960 bc_num_copy(c, a);
1961 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001962 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001963 }
1964
1965 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1966 bc_num_copy(&cp, a);
1967 len = b->len;
1968
1969 if (len > cp.len) {
1970 bc_num_expand(&cp, len + 2);
1971 bc_num_extend(&cp, len - cp.len);
1972 }
1973
1974 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1975 cp.rdx -= b->rdx;
1976 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1977
1978 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001979 for (;;) {
1980 if (len == 0) break;
1981 len--;
1982 if (b->num[len] != 0)
1983 break;
1984 }
1985 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001986 }
1987
1988 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1989
1990 // We want an extra zero in front to make things simpler.
1991 cp.num[cp.len++] = 0;
1992 end = cp.len - len;
1993
1994 bc_num_expand(c, cp.len);
1995
1996 bc_num_zero(c);
1997 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1998 c->rdx = cp.rdx;
1999 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002000
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002001 s = BC_STATUS_SUCCESS;
2002 for (i = end - 1; i < end; --i) {
2003 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002004 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002005 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2006 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002007 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002008#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002009 // a=2^100000
2010 // scale=40000
2011 // 1/a <- without check below, this will not be interruptible
2012 if (G_interrupt) {
2013 s = BC_STATUS_FAILURE;
2014 break;
2015 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002016#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002017 }
2018
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002019 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002020 bc_num_free(&cp);
2021
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002022 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002023}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002024#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002025
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002026static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002027 BcNum *restrict d, size_t scale, size_t ts)
2028{
2029 BcStatus s;
2030 BcNum temp;
2031 bool neg;
2032
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002033 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002034 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002035
2036 if (a->len == 0) {
2037 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002038 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002039 }
2040
2041 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002042 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002043 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002044
2045 if (scale != 0) scale = ts;
2046
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002047 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002048 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002049 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002050 if (s) goto err;
2051
2052 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2053
2054 neg = d->neg;
2055 bc_num_retireMul(d, ts, a->neg, b->neg);
2056 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002057 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002058 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002059 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002060}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002061#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002062
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002063static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002064{
2065 BcStatus s;
2066 BcNum c1;
2067 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2068
2069 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002070 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002071 bc_num_free(&c1);
2072
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002073 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002074}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002075#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002076
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002077static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002078{
2079 BcStatus s = BC_STATUS_SUCCESS;
2080 BcNum copy;
2081 unsigned long pow;
2082 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002083 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002084
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002085 // GNU bc does not allow 2^2.0 - we do
2086 for (i = 0; i < b->rdx; i++)
2087 if (b->num[i] != 0)
2088 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002089
2090 if (b->len == 0) {
2091 bc_num_one(c);
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 (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002095 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002096 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002097 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002098 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002099 if (!b->neg)
2100 bc_num_copy(c, a);
2101 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002102 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002103 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002104 }
2105
2106 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002107 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002108 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002109 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002110
2111 bc_num_init(&copy, a->len);
2112 bc_num_copy(&copy, a);
2113
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002114 if (!neg) {
2115 if (a->rdx > scale)
2116 scale = a->rdx;
2117 if (a->rdx * pow < scale)
2118 scale = a->rdx * pow;
2119 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002120
Gavin Howard01055ba2018-11-03 11:00:21 -06002121
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002122 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002124 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002125 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002126 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002127 //if (G_interrupt) {
2128 // s = BC_STATUS_FAILURE;
2129 // goto err;
2130 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002131 }
2132
Gavin Howard01055ba2018-11-03 11:00:21 -06002133 bc_num_copy(c, &copy);
2134
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002135 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002136 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002137 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002138 if (s) goto err;
2139
2140 if (pow & 1) {
2141 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002142 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002143 if (s) goto err;
2144 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002145 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002146 //if (G_interrupt) {
2147 // s = BC_STATUS_FAILURE;
2148 // goto err;
2149 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002150 }
2151
2152 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002153 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002154 if (s) goto err;
2155 }
2156
Gavin Howard01055ba2018-11-03 11:00:21 -06002157 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2158
2159 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002160 for (i = 0; i < c->len; ++i)
2161 if (c->num[i] != 0)
2162 goto skip;
2163 bc_num_setToZero(c, scale);
2164 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002165
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002166 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002167 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002168 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002169}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002170#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002171
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002172static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002173{
2174 BcStatus s;
2175 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2176 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2177 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2178
2179 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2180 bc_num_expand(b, req);
2181
2182 if (a->len == 0) {
2183 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002184 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002185 }
2186 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002187 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002188 }
2189 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002190 bc_num_one(b);
2191 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002193 }
2194
2195 scale = BC_MAX(scale, a->rdx) + 1;
2196 len = a->len + scale;
2197
2198 bc_num_init(&num1, len);
2199 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002200 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002201
2202 bc_num_one(&half);
2203 half.num[0] = 5;
2204 half.rdx = 1;
2205
2206 bc_num_init(&f, len);
2207 bc_num_init(&fprime, len);
2208
2209 x0 = &num1;
2210 x1 = &num2;
2211
2212 bc_num_one(x0);
2213 pow = BC_NUM_INT(a);
2214
2215 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002216 if (pow & 1)
2217 x0->num[0] = 2;
2218 else
2219 x0->num[0] = 6;
2220
2221 pow -= 2 - (pow & 1);
2222
2223 bc_num_extend(x0, pow);
2224
2225 // Make sure to move the radix back.
2226 x0->rdx -= pow;
2227 }
2228
2229 x0->rdx = digs = digs1 = 0;
2230 resrdx = scale + 2;
2231 len = BC_NUM_INT(x0) + resrdx - 1;
2232
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002233 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002234 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002235 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002236 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002237 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002238 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002239 if (s) goto err;
2240
2241 cmp = bc_num_cmp(x1, x0);
2242 digs = x1->len - (unsigned long long) llabs(cmp);
2243
2244 if (cmp == cmp2 && digs == digs1)
2245 times += 1;
2246 else
2247 times = 0;
2248
2249 resrdx += times > 4;
2250
2251 cmp2 = cmp1;
2252 cmp1 = cmp;
2253 digs1 = digs;
2254
2255 temp = x0;
2256 x0 = x1;
2257 x1 = temp;
2258 }
2259
Gavin Howard01055ba2018-11-03 11:00:21 -06002260 bc_num_copy(b, x0);
2261 scale -= 1;
2262 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002263 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002264 bc_num_free(&fprime);
2265 bc_num_free(&f);
2266 bc_num_free(&half);
2267 bc_num_free(&num2);
2268 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002269 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002270}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002271#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002272
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002273static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002274 size_t scale)
2275{
2276 BcStatus s;
2277 BcNum num2, *ptr_a;
2278 bool init = false;
2279 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2280
2281 if (c == a) {
2282 memcpy(&num2, c, sizeof(BcNum));
2283 ptr_a = &num2;
2284 bc_num_init(c, len);
2285 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002286 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002287 ptr_a = a;
2288 bc_num_expand(c, len);
2289 }
2290
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002291 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002292
2293 if (init) bc_num_free(&num2);
2294
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002295 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002296}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002297#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002298
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002299#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002300static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002301{
2302 BcStatus s;
2303 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002304 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002305
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002306 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002307 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002308 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002309 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002310 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002311 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002312
2313 bc_num_expand(d, c->len);
2314 bc_num_init(&base, c->len);
2315 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002316 bc_num_init(&temp, b->len);
2317
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002318 two.cap = ARRAY_SIZE(two_digs);
2319 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002320 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002321 two_digs[0] = 2;
2322
Gavin Howard01055ba2018-11-03 11:00:21 -06002323 bc_num_one(d);
2324
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002325 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002326 if (s) goto err;
2327 bc_num_copy(&exp, b);
2328
2329 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002330 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002331 if (s) goto err;
2332
2333 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002334 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002335 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002336 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002337 if (s) goto err;
2338 }
2339
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002340 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002341 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002342 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002343 if (s) goto err;
2344 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002345 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002346 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002347 bc_num_free(&exp);
2348 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002349 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002350}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002351#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002352#endif // ENABLE_DC
2353
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002354static FAST_FUNC void bc_string_free(void *string)
2355{
2356 free(*(char**)string);
2357}
2358
Gavin Howard01055ba2018-11-03 11:00:21 -06002359static void bc_func_init(BcFunc *f)
2360{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002361 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002362 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2363 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002364 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2365 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002366 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002367}
2368
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002369static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002370{
2371 BcFunc *f = (BcFunc *) func;
2372 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002373 IF_BC(bc_vec_free(&f->labels);)
2374 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002375 IF_BC(bc_vec_free(&f->strs);)
2376 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002377}
2378
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002379static void bc_array_expand(BcVec *a, size_t len);
2380
Gavin Howard01055ba2018-11-03 11:00:21 -06002381static void bc_array_init(BcVec *a, bool nums)
2382{
2383 if (nums)
2384 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2385 else
2386 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2387 bc_array_expand(a, 1);
2388}
2389
Gavin Howard01055ba2018-11-03 11:00:21 -06002390static void bc_array_expand(BcVec *a, size_t len)
2391{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002392 if (a->dtor == bc_num_free
2393 // && a->size == sizeof(BcNum) - always true
2394 ) {
2395 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002396 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002397 bc_num_init_DEF_SIZE(&n);
2398 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002399 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002400 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002401 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002402 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002403 bc_array_init(&v, true);
2404 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002405 }
2406 }
2407}
2408
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002409static void bc_array_copy(BcVec *d, const BcVec *s)
2410{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002411 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002412 size_t i;
2413
2414 bc_vec_pop_all(d);
2415 bc_vec_expand(d, s->cap);
2416 d->len = s->len;
2417
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002418 dnum = (void*)d->v;
2419 snum = (void*)s->v;
2420 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002421 bc_num_init(dnum, snum->len);
2422 bc_num_copy(dnum, snum);
2423 }
2424}
2425
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002426#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002427static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002428{
2429 d->t = src->t;
2430
2431 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002432 case XC_RESULT_TEMP:
2433 case XC_RESULT_IBASE:
2434 case XC_RESULT_SCALE:
2435 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002436 bc_num_init(&d->d.n, src->d.n.len);
2437 bc_num_copy(&d->d.n, &src->d.n);
2438 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002439 case XC_RESULT_VAR:
2440 case XC_RESULT_ARRAY:
2441 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002442 d->d.id.name = xstrdup(src->d.id.name);
2443 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002444 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002445 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002446 IF_BC(case BC_RESULT_ONE:)
2447 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002448 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2449 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002450 }
2451}
2452#endif // ENABLE_DC
2453
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002454static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002455{
2456 BcResult *r = (BcResult *) result;
2457
2458 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002459 case XC_RESULT_TEMP:
2460 case XC_RESULT_IBASE:
2461 case XC_RESULT_SCALE:
2462 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002463 bc_num_free(&r->d.n);
2464 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002465 case XC_RESULT_VAR:
2466 case XC_RESULT_ARRAY:
2467 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 free(r->d.id.name);
2469 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002470 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002471 // Do nothing.
2472 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002473 }
2474}
2475
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002476#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
2477static void bc_vec_concat(BcVec *v, const char *str)
2478{
2479 size_t len, slen;
2480
2481 slen = strlen(str);
2482 len = v->len + slen + 1;
2483
2484 if (v->cap < len) bc_vec_grow(v, slen);
2485 strcpy(v->v + v->len, str);
2486
2487 v->len = len;
2488}
2489#endif
2490
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002491static int bad_input_byte(char c)
2492{
2493 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2494 || c > 0x7e
2495 ) {
2496 bc_error_fmt("illegal character 0x%02x", c);
2497 return 1;
2498 }
2499 return 0;
2500}
2501
2502// Note: it _appends_ data from fp to vec.
2503static void bc_read_line(BcVec *vec, FILE *fp)
2504{
2505 again:
2506 fflush_and_check();
2507
2508#if ENABLE_FEATURE_BC_SIGNALS
2509 if (G_interrupt) { // ^C was pressed
2510 intr:
2511 if (fp != stdin) {
2512 // ^C while running a script (bc SCRIPT): die.
2513 // We do not return to interactive prompt:
2514 // user might be running us from a shell,
2515 // and SCRIPT might be intended to terminate
2516 // (e.g. contain a "halt" stmt).
2517 // ^C dropping user into a bc prompt instead of
2518 // the shell would be unexpected.
2519 xfunc_die();
2520 }
2521 // ^C while interactive input
2522 G_interrupt = 0;
2523 // GNU bc says "interrupted execution."
2524 // GNU dc says "Interrupt!"
2525 fputs("\ninterrupted execution\n", stderr);
2526 }
2527
2528# if ENABLE_FEATURE_EDITING
2529 if (G_ttyin && fp == stdin) {
2530 int n, i;
2531# define line_buf bb_common_bufsiz1
2532 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2533 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2534 if (n == 0) // ^C
2535 goto intr;
2536 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2537 return;
2538 }
2539 i = 0;
2540 for (;;) {
2541 char c = line_buf[i++];
2542 if (!c) break;
2543 if (bad_input_byte(c)) goto again;
2544 }
2545 bc_vec_concat(vec, line_buf);
2546# undef line_buf
2547 } else
2548# endif
2549#endif
2550 {
2551 int c;
2552 bool bad_chars = 0;
2553 size_t len = vec->len;
2554
2555 do {
2556#if ENABLE_FEATURE_BC_SIGNALS
2557 if (G_interrupt) {
2558 // ^C was pressed: ignore entire line, get another one
2559 vec->len = len;
2560 goto intr;
2561 }
2562#endif
2563 do c = fgetc(fp); while (c == '\0');
2564 if (c == EOF) {
2565 if (ferror(fp))
2566 bb_perror_msg_and_die("input error");
2567 // Note: EOF does not append '\n'
2568 break;
2569 }
2570 bad_chars |= bad_input_byte(c);
2571 bc_vec_pushByte(vec, (char)c);
2572 } while (c != '\n');
2573
2574 if (bad_chars) {
2575 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002576 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002577 // ignore entire line, get another one
2578 vec->len = len;
2579 goto again;
2580 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002581 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002582 }
2583 bc_vec_pushZeroByte(vec);
2584 }
2585}
2586
2587//
2588// Parsing routines
2589//
2590
2591static bool bc_num_strValid(const char *val, size_t base)
2592{
2593 BcDig b;
2594 bool radix;
2595
2596 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2597 radix = false;
2598 for (;;) {
2599 BcDig c = *val++;
2600 if (c == '\0')
2601 break;
2602 if (c == '.') {
2603 if (radix) return false;
2604 radix = true;
2605 continue;
2606 }
2607 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2608 return false;
2609 }
2610 return true;
2611}
2612
2613// Note: n is already "bc_num_zero()"ed,
2614// leading zeroes in "val" are removed
2615static void bc_num_parseDecimal(BcNum *n, const char *val)
2616{
2617 size_t len, i;
2618 const char *ptr;
2619
2620 len = strlen(val);
2621 if (len == 0)
2622 return;
2623
2624 bc_num_expand(n, len);
2625
2626 ptr = strchr(val, '.');
2627
2628 n->rdx = 0;
2629 if (ptr != NULL)
2630 n->rdx = (size_t)((val + len) - (ptr + 1));
2631
2632 for (i = 0; val[i]; ++i) {
2633 if (val[i] != '0' && val[i] != '.') {
2634 // Not entirely zero value - convert it, and exit
2635 i = len - 1;
2636 for (;;) {
2637 n->num[n->len] = val[i] - '0';
2638 ++n->len;
2639 skip_dot:
2640 if (i == 0) break;
2641 if (val[--i] == '.') goto skip_dot;
2642 }
2643 break;
2644 }
2645 }
2646 // if for() exits without hitting if(), the value is entirely zero
2647}
2648
2649// Note: n is already "bc_num_zero()"ed,
2650// leading zeroes in "val" are removed
2651static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2652{
2653 BcStatus s;
2654 BcNum temp, mult, result;
2655 BcNum base;
2656 BcDig base_digs[ULONG_NUM_BUFSIZE];
2657 BcDig c = '\0';
2658 unsigned long v;
2659 size_t i, digits;
2660
2661 for (i = 0; ; ++i) {
2662 if (val[i] == '\0')
2663 return;
2664 if (val[i] != '.' && val[i] != '0')
2665 break;
2666 }
2667
2668 bc_num_init_DEF_SIZE(&temp);
2669 bc_num_init_DEF_SIZE(&mult);
2670 base.cap = ARRAY_SIZE(base_digs);
2671 base.num = base_digs;
2672 bc_num_ulong2num(&base, base_t);
2673
2674 for (;;) {
2675 c = *val++;
2676 if (c == '\0') goto int_err;
2677 if (c == '.') break;
2678
2679 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2680
2681 s = zbc_num_mul(n, &base, &mult, 0);
2682 if (s) goto int_err;
2683 bc_num_ulong2num(&temp, v);
2684 s = zbc_num_add(&mult, &temp, n, 0);
2685 if (s) goto int_err;
2686 }
2687
2688 bc_num_init(&result, base.len);
2689 //bc_num_zero(&result); - already is
2690 bc_num_one(&mult);
2691
2692 digits = 0;
2693 for (;;) {
2694 c = *val++;
2695 if (c == '\0') break;
2696 digits++;
2697
2698 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2699
2700 s = zbc_num_mul(&result, &base, &result, 0);
2701 if (s) goto err;
2702 bc_num_ulong2num(&temp, v);
2703 s = zbc_num_add(&result, &temp, &result, 0);
2704 if (s) goto err;
2705 s = zbc_num_mul(&mult, &base, &mult, 0);
2706 if (s) goto err;
2707 }
2708
2709 s = zbc_num_div(&result, &mult, &result, digits);
2710 if (s) goto err;
2711 s = zbc_num_add(n, &result, n, digits);
2712 if (s) goto err;
2713
2714 if (n->len != 0) {
2715 if (n->rdx < digits)
2716 bc_num_extend(n, digits - n->rdx);
2717 } else
2718 bc_num_zero(n);
2719 err:
2720 bc_num_free(&result);
2721 int_err:
2722 bc_num_free(&mult);
2723 bc_num_free(&temp);
2724}
2725
2726static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2727{
2728 if (!bc_num_strValid(val, base_t))
2729 RETURN_STATUS(bc_error("bad number string"));
2730
2731 bc_num_zero(n);
2732 while (*val == '0') val++;
2733
2734 if (base_t == 10)
2735 bc_num_parseDecimal(n, val);
2736 else
2737 bc_num_parseBase(n, val, base_t);
2738
2739 RETURN_STATUS(BC_STATUS_SUCCESS);
2740}
2741#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2742
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002743static bool bc_lex_more_input(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002744{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002745 BcParse *p = &G.prs;
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002746 unsigned str; // bool for bc, string nest count for dc
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002747 bool comment;
2748
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002749 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002750
2751 // This loop is complex because the vm tries not to send any lines that end
2752 // with a backslash to the parser. The reason for that is because the parser
2753 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2754 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002755 //
2756 // bc cases to test interactively:
2757 // 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2758 // 1 #comment/* - prints "1<newline>" at once
2759 // 1 #comment" - prints "1<newline>" at once
2760 // 1\#comment - error at once (\ is not a line continuation)
2761 // 1 + /*"*/2 - prints "3<newline>" at once
2762 // 1 + /*#*/2 - prints "3<newline>" at once
2763 // "str\" - prints "str\" at once
2764 // "str#" - prints "str#" at once
2765 // "str/*" - prints "str/*" at once
2766 // "str#\ - waits for second line
2767 // end" - ...prints "str#\<newline>end"
2768//This is way too complex, we duplicate comment/string logic of lexer
2769//TODO: switch to char-by-char input like hush does it
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002770 str = 0;
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002771 comment = false; // stays always false for dc
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002772 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002773 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002774 char *string;
2775
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002776 bc_read_line(&G.input_buffer, G.prs.lex_input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002777 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002778 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002779 break;
2780
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002781 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002782 while (*string) {
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002783 char c = *string++;
2784#if ENABLE_BC
2785 if (comment) {
2786 // We are in /**/ comment, exit only on "*/"
2787 if (c == '*' && *string == '/') {
2788 comment = false;
2789 string++;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002790 }
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002791 continue;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002792 }
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002793#endif
2794 // We are not in /**/ comment
2795 if (str) {
2796 // We are in "string" (bc) or [string] (dc)
2797 if (IS_BC) {
2798 // bc strings have no escapes: \\ is not special,
2799 // \n is not, \" is not, \<newline> is not.
2800 str = (c != '"'); // clear flag when " is seen
2801 } else {
2802 // dc strings have no escapes as well, can nest
2803 if (c == ']')
2804 str--;
2805 if (c == '[')
2806 str++;
2807 }
2808 continue;
2809 }
2810 // We are not in a string or /**/ comment
2811
2812 // Is it a #comment? Return the string (can't have continuation)
2813 if (c == '#')
2814 goto return_string;
2815#if ENABLE_BC
2816 if (IS_BC) {
2817 // bc: is it a start of /**/ comment or string?
Denys Vlasenko94576d22018-12-25 23:45:57 +01002818 if (c == '/' && *string == '*') {
2819 comment = true;
2820 string++;
2821 continue;
2822 }
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002823 str = (c == '"'); // set flag if " is seen
2824 continue;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002825 }
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002826#endif
2827 // dc: is it a start of string?
2828 str = (c == '[');
2829 } // end of "check all chars in string" loop
2830
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002831 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002832 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002833 continue;
2834 }
2835
2836 // Check for backslash+newline.
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002837 // We do not check that last char is '\n' -
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002838 // if it is not, then it's EOF, and looping back
2839 // to bc_read_line() will detect it:
2840 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002841 if (string >= G.input_buffer.v && *string == '\\') {
2842 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002843 continue;
2844 }
2845
2846 break;
2847 }
Denys Vlasenko63ad7992018-12-26 12:23:05 +01002848 return_string:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002849
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002850 p->lex_inbuf = G.input_buffer.v;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002851// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002852 p->lex_end = p->lex_inbuf + G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002853
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002854 return G.input_buffer.len > 1;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002855}
2856
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002857// p->lex_inbuf points to the current string to be parsed.
2858// if p->lex_inbuf points to '\0', it's either EOF or it points after
2859// last processed line's terminating '\n' (and more reading needs to be done
2860// to get next character).
2861//
2862// If you are in a situation where that is a possibility, call peek_inbuf().
2863// If necessary, it performs more reading and changes p->lex_inbuf,
2864// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2865// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2866// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2867//
2868// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2869// it returns current char and advances the pointer (if not EOF).
2870// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2871//
2872// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2873// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2874// on real EOF, not end-of-buffer.
2875static char peek_inbuf(void)
2876{
2877 if (G.prs.lex_inbuf == G.prs.lex_end) {
2878 if (G.prs.lex_input_fp)
2879 if (!bc_lex_more_input())
2880 G.prs.lex_input_fp = NULL;
2881 }
2882 return *G.prs.lex_inbuf;
2883}
2884static char eat_inbuf(void)
2885{
2886 char c = peek_inbuf();
2887 if (c) G.prs.lex_inbuf++;
2888 return c;
2889}
2890
2891static void bc_lex_lineComment(void)
2892{
2893 BcParse *p = &G.prs;
2894 char c;
2895
2896 // Try: echo -n '#foo' | bc
2897 p->lex = XC_LEX_WHITESPACE;
2898
2899 // We depend here on input being done in whole lines:
2900 // '\0' which isn't the EOF can only be seen after '\n'.
2901 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2902 p->lex_inbuf++;
2903}
2904
2905static void bc_lex_whitespace(void)
2906{
2907 BcParse *p = &G.prs;
2908
2909 p->lex = XC_LEX_WHITESPACE;
2910 for (;;) {
2911 // We depend here on input being done in whole lines:
2912 // '\0' which isn't the EOF can only be seen after '\n'.
2913 char c = *p->lex_inbuf;
2914 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2915 break;
2916 if (!isspace(c))
2917 break;
2918 p->lex_inbuf++;
2919 }
2920}
2921
2922static BC_STATUS zbc_lex_number(char last)
2923{
2924 BcParse *p = &G.prs;
2925 bool pt;
2926
2927 bc_vec_pop_all(&p->lex_strnumbuf);
2928 bc_vec_pushByte(&p->lex_strnumbuf, last);
2929
2930 pt = (last == '.');
2931 p->lex = XC_LEX_NUMBER;
2932 for (;;) {
2933 // We depend here on input being done in whole lines:
2934 // '\0' which isn't the EOF can only be seen after '\n'.
2935 char c = *p->lex_inbuf;
2936 check_c:
2937 if (c == '\0')
2938 break;
2939 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2940 p->lex_inbuf += 2;
2941 p->lex_line++;
2942 c = peek_inbuf(); // force next line to be read
2943 goto check_c;
2944 }
2945 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2946 if (c != '.') break;
2947 // if '.' was already seen, stop on second one:
2948 if (pt) break;
2949 pt = true;
2950 }
2951 // c is one of "0-9A-F."
2952 last = c;
2953 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2954 p->lex_inbuf++;
2955 }
2956 if (last == '.') // remove trailing '.' if any
2957 bc_vec_pop(&p->lex_strnumbuf);
2958 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2959
2960 G.err_line = G.prs.lex_line;
2961 RETURN_STATUS(BC_STATUS_SUCCESS);
2962}
2963#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
2964
2965static void bc_lex_name(void)
2966{
2967 BcParse *p = &G.prs;
2968 size_t i;
2969 const char *buf;
2970
2971 p->lex = XC_LEX_NAME;
2972
2973 // Since names can't cross lines with \<newline>,
2974 // we depend on the fact that whole line is in the buffer
2975 i = 0;
2976 buf = p->lex_inbuf - 1;
2977 for (;;) {
2978 char c = buf[i];
2979 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2980 i++;
2981 }
2982
2983#if 0 // We do not protect against people with gigabyte-long names
2984 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2985 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2986 if (i > BC_MAX_STRING)
2987 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2988 }
2989#endif
2990 bc_vec_string(&p->lex_strnumbuf, i, buf);
2991
2992 // Increment the index. We minus 1 because it has already been incremented.
2993 p->lex_inbuf += i - 1;
2994
2995 //return BC_STATUS_SUCCESS;
2996}
2997
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002998IF_BC(static BC_STATUS zbc_lex_token(void);)
2999IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003000#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
3001#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003002
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003003static BC_STATUS zbc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003004{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003005 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003006 BcStatus s;
3007
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003008 p->lex_last = p->lex;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003009 if (p->lex_last == XC_LEX_EOF)
3010 RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003011
3012 // Loop until failure or we don't have whitespace. This
3013 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003014 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003015 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003016 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003017 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003018 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003019 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01003020 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01003021 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003022 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003023 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003024 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
3025 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01003026 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003027 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003028 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003029 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003030 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003031 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003032 } while (!s && p->lex == XC_LEX_WHITESPACE);
3033 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06003034
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003035 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003036}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003037#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003038
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003039#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003040static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003041{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003042 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003043 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01003044 RETURN_STATUS(BC_STATUS_SUCCESS);
3045}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003046#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003047
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003048static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003049{
3050 BcStatus s;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003051 s = zbc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003052 if (s) RETURN_STATUS(s);
3053 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003054 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003055 RETURN_STATUS(s);
3056}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003057#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003058#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003059
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003060static BC_STATUS zbc_lex_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003061{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003062 G.prs.lex_inbuf = text;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003063 G.prs.lex_end = text + strlen(text);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003064 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003065 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003066}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003067#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003068
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003069#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003070static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003071{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003072 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003073 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003074 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003075 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003076
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003077 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3078 const char *keyword8 = bc_lex_kws[i].name8;
3079 unsigned j = 0;
3080 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3081 j++;
3082 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003083 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003084 if (keyword8[j] != '\0')
3085 continue;
3086 match:
3087 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003088 if (isalnum(buf[j]) || buf[j]=='_')
3089 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003090 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003091 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003092 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3093 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003094 }
3095
3096 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003097 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003098 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003099 }
3100
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003101 bc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003102 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003103
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003104 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003105 // Prevent this:
3106 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003107 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003108 // '
3109 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003110 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003111 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003112
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003113 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003114}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003115#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003116
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003117static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003118{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003119 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003120
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003121 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003122 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003123 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003124 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003125 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003126 RETURN_STATUS(bc_error("unterminated string1"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003127 }
3128 if (c == '"')
3129 break;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003130 if (c == '\n')
3131 p->lex_line++;
3132 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3133 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003134 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003135 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3136 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003137
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003138 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003139 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003140}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003141#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003142
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003143static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003144{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003145 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003146 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003147 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003148 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003149 with_and_without >>= 8; // store "with" value
3150 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003151 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003152}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003153#define parse_lex_by_checking_eq_sign(with, without) \
3154 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003155
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003156static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003157{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003158 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003159
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003160 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003161 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003162 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003163 char c;
3164
3165 p->lex_inbuf++;
3166 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003167 check_star:
3168 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003169 p->lex_inbuf++;
3170 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003171 if (c == '/')
3172 break;
3173 goto check_star;
3174 }
3175 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003176 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003177 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003178 if (c == '\n')
3179 p->lex_line++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003181 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003182
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003183 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003184 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003185}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003186#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003187
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003188#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003189static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003190{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003191 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003192 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003193 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003194 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003195
3196 // This is the workhorse of the lexer.
3197 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003198// case '\0': // probably never reached
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003199// p->lex_inbuf--;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003200// p->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003201// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003202 case '\n':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003203 p->lex_line++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003204 p->lex = XC_LEX_NLINE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 case '\t':
3207 case '\v':
3208 case '\f':
3209 case '\r':
3210 case ' ':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003211 bc_lex_whitespace();
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 case '!':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003214 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003215 if (p->lex == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003216 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
3217 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 case '"':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003221 s = zbc_lex_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 case '#':
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003224 s = zbc_POSIX_does_not_allow("'#' script comments");
3225 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003226 bc_lex_lineComment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 case '%':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003229 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 case '&':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003232 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 if (c2 == '&') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003234 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3235 if (s) RETURN_STATUS(s);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003236 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003237 p->lex = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003238 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003239 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003240 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003241 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003242 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003243 case '(':
3244 case ')':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003245 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003247 case '*':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003248 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 case '+':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003251 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 if (c2 == '+') {
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003253 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003254 p->lex = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003255 } else
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003256 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003257 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 case ',':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003259 p->lex = BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003261 case '-':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003262 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003263 if (c2 == '-') {
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003264 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003265 p->lex = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003266 } else
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003267 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003268 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003269 case '.':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003270 if (isdigit(*p->lex_inbuf))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003271 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003272 else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003273 p->lex = BC_LEX_KEY_LAST;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003274 s = zbc_POSIX_does_not_allow("'.' as 'last'");
Gavin Howard01055ba2018-11-03 11:00:21 -06003275 }
3276 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003277 case '/':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003278 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 if (c2 == '*')
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003280 s = zbc_lex_comment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 else
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003282 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003283 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 case '0':
3285 case '1':
3286 case '2':
3287 case '3':
3288 case '4':
3289 case '5':
3290 case '6':
3291 case '7':
3292 case '8':
3293 case '9':
3294 case 'A':
3295 case 'B':
3296 case 'C':
3297 case 'D':
3298 case 'E':
3299 case 'F':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003300 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003302 case ';':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003303 p->lex = BC_LEX_SCOLON;
Gavin Howard01055ba2018-11-03 11:00:21 -06003304 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003305 case '<':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003306 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003307 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003308 case '=':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003309 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003310 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003311 case '>':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003312 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003313 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003314 case '[':
3315 case ']':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003316 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06003317 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003318 case '\\':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003319 if (*p->lex_inbuf == '\n') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003320 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003321 p->lex_inbuf++;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003322 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003323 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003324 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003325 case '^':
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003326 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003327 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003328 case 'a':
3329 case 'b':
3330 case 'c':
3331 case 'd':
3332 case 'e':
3333 case 'f':
3334 case 'g':
3335 case 'h':
3336 case 'i':
3337 case 'j':
3338 case 'k':
3339 case 'l':
3340 case 'm':
3341 case 'n':
3342 case 'o':
3343 case 'p':
3344 case 'q':
3345 case 'r':
3346 case 's':
3347 case 't':
3348 case 'u':
3349 case 'v':
3350 case 'w':
3351 case 'x':
3352 case 'y':
3353 case 'z':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003354 s = zbc_lex_identifier();
Gavin Howard01055ba2018-11-03 11:00:21 -06003355 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003356 case '{':
3357 case '}':
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003358 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003359 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003360 case '|':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003361 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003362 if (c2 == '|') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003363 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3364 if (s) RETURN_STATUS(s);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003365 p->lex_inbuf++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003366 p->lex = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003367 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003368 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003369 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003370 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003371 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003372 default:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003373 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003374 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003375 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003376 }
3377
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003378 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003379}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003380#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003381#endif // ENABLE_BC
3382
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003383#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003384static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003385{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003386 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003387 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003388 bc_lex_whitespace(); // eats whitespace (but not newline)
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003389 p->lex_inbuf++; // bc_lex_name() expects this
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003390 bc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003391 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003392 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003393 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003394 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3395 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003396 }
3397
Denys Vlasenko29301232018-12-11 15:29:32 +01003398 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003399}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003400#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003401
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003402static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003403{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003404 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003405 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003406
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003407 p->lex = XC_LEX_STR;
3408 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003409
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003410 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003411 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003412 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003413 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003414 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003415 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003416 if (c == '[') depth++;
3417 if (c == ']')
3418 if (--depth == 0)
3419 break;
3420 if (c == '\n')
3421 p->lex_line++;
3422 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3423 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003424 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003425 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003426 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003427
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003428 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003429 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003430}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003431#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003432
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003433#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003434static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003435{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003436 BcParse *p = &G.prs;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003437 static const //BcLexType - should be this type, but narrower type saves size:
3438 uint8_t
3439 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003440 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 +01003441 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3442 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003443 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003444 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003445
Denys Vlasenko81293c82018-12-24 01:53:55 +01003446 BcStatus s;
3447 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003448 size_t i;
3449
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003450 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003451 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003452 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003453 }
3454
Denys Vlasenko81293c82018-12-24 01:53:55 +01003455 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003456 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003457 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003458 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003459 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003460 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003461 }
3462
3463 // This is the workhorse of the lexer.
3464 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003465// case '\0': // probably never reached
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003466// p->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003467// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003468 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003469 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003470 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003471 // only to allow interactive dc have a way to exit
3472 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003473 // on <enter>, not on _next_ token (which would mean
3474 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003475 // IOW: typing "1p<enter>" should print "1" _at once_,
3476 // not after some more input.
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003477 p->lex_line++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003478 p->lex = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003479 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 case '\t':
3481 case '\v':
3482 case '\f':
3483 case '\r':
3484 case ' ':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003485 bc_lex_whitespace();
Gavin Howard01055ba2018-11-03 11:00:21 -06003486 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 case '!':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003488 c2 = *p->lex_inbuf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003489 if (c2 == '=')
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003490 p->lex = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003491 else if (c2 == '<')
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003492 p->lex = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003493 else if (c2 == '>')
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003494 p->lex = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003496 RETURN_STATUS(bc_error_bad_character(c));
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003497 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003498 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003499 case '#':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003500 bc_lex_lineComment();
Gavin Howard01055ba2018-11-03 11:00:21 -06003501 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003502 case '.':
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003503 if (isdigit(*p->lex_inbuf))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003504 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003505 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003506 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003507 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003508 case '0':
3509 case '1':
3510 case '2':
3511 case '3':
3512 case '4':
3513 case '5':
3514 case '6':
3515 case '7':
3516 case '8':
3517 case '9':
3518 case 'A':
3519 case 'B':
3520 case 'C':
3521 case 'D':
3522 case 'E':
3523 case 'F':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003524 s = zbc_lex_number(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003525 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003526 case '[':
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003527 s = zdc_lex_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06003528 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003529 default:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003530 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003531 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003532 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003533 }
3534
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003535 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003536}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003537#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003538#endif // ENABLE_DC
3539
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003540static void bc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003541{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003542 BcParse *p = &G.prs;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003543 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003544 bc_vec_pushByte(&p->func->code, i);
3545}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003546
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003547static void bc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003548{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003549 while (*name)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003550 bc_parse_push(*name++);
3551 bc_parse_push(BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003552}
3553
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003554static void bc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003555{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003556 size_t mask;
3557 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003558
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003559 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003560 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3561 amt = sizeof(idx);
3562 do {
3563 if (idx & mask) break;
3564 mask >>= 8;
3565 amt--;
3566 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003567
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003568 bc_parse_push(amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003569
3570 while (idx != 0) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003571 bc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003572 idx >>= 8;
3573 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003574}
3575
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003576#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003577static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003578{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003579 bc_parse_push(BC_INST_JUMP);
3580 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003581}
3582
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003583static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003584{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003585 bc_parse_push(BC_INST_JUMP_ZERO);
3586 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003587}
3588
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003589static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003590{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003591 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003592 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003593
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003594 bc_parse_push(XC_INST_STR);
3595 bc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003596 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003597
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003598 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003599}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003600#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003601#endif
3602
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003603static void bc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003604{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003605 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003606 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003607#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003608 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003609#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003610 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003611#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003612 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003613#endif
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003614 bc_parse_push(XC_INST_NUM);
3615 bc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003616}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003617
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003618static BC_STATUS zbc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003619{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003620 BcParse *p = &G.prs;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003621 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003622
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003623 RETURN_STATUS(zbc_lex_text_init(text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003624}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003625#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003626
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003627// Called when parsing or execution detects a failure,
3628// resets execution structures.
3629static void bc_program_reset(void)
3630{
3631 BcFunc *f;
3632 BcInstPtr *ip;
3633
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003634 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003635 bc_vec_pop_all(&G.prog.results);
3636
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003637 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003638 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003639 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003640}
3641
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003642// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003643// resets parsing structures.
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003644static void bc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003645{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003646 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003647 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003648 bc_func_free(p->func);
3649 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003650
Denys Vlasenko65e10462018-12-19 15:13:14 +01003651 p->fidx = BC_PROG_MAIN;
3652 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003653 }
3654
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003655 p->lex_inbuf = p->lex_end;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003656 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003657
Denys Vlasenko503faf92018-12-20 16:24:18 +01003658 IF_BC(bc_vec_pop_all(&p->exits);)
3659 IF_BC(bc_vec_pop_all(&p->conds);)
3660 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003661
Denys Vlasenkod38af482018-12-04 19:11:02 +01003662 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003663}
3664
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003665static void bc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003666{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003667 BcParse *p = &G.prs;
Denys Vlasenko503faf92018-12-20 16:24:18 +01003668 IF_BC(bc_vec_free(&p->exits);)
3669 IF_BC(bc_vec_free(&p->conds);)
3670 IF_BC(bc_vec_free(&p->ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003671 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003672}
3673
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003674static void bc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003675{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003676 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003677 memset(p, 0, sizeof(BcParse));
3678
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003679 bc_char_vec_init(&G.prs.lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003680 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3681 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3682 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003683
Denys Vlasenkofa210792018-12-19 19:35:40 +01003684 p->fidx = fidx;
3685 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003686}
3687
Denys Vlasenko04715442018-12-21 00:10:26 +01003688static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003689{
Denys Vlasenko04715442018-12-21 00:10:26 +01003690 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003691 BcFunc f;
3692 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003693 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003694 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003695 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003696}
3697
3698#if ENABLE_BC
3699
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003700// Note: takes ownership of 'name' (must be malloced)
3701static size_t bc_program_addFunc(char *name)
3702{
3703 size_t idx;
3704 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003705 int inserted;
3706
3707 entry.name = name;
3708 entry.idx = G.prog.fns.len;
3709
3710 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3711 if (!inserted) free(name);
3712
3713 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3714 idx = entry_ptr->idx;
3715
3716 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003717 // There is already a function with this name.
3718 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003719 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003720 bc_func_free(func);
3721 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003722 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003723 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003724 }
3725
3726 return idx;
3727}
3728
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003729#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003730// We can calculate the conversion between tokens and exprs by subtracting the
3731// position of the first operator in the lex enum and adding the position of the
3732// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003733#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003734
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003735static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003736
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003737static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003738{
3739 BcStatus s;
3740
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003741 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003742 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3743 RETURN_STATUS(bc_error("empty expression"));
3744 RETURN_STATUS(s);
3745}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003746#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003747
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003748static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003749#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003750
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003751static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003752{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003753 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003754}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003755#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003756
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003757static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003758{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003759 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003760 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3761 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003762 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003763 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003764 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003765 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003766
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003767 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003768}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003769#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003770
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003771static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003772{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003773 BcParse *p = &G.prs;
Denys Vlasenko69560f42018-12-24 14:14:23 +01003774 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3775 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003776
3777 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003778 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003779 if (t == BC_LEX_LPAREN) break;
3780
Denys Vlasenko69560f42018-12-24 14:14:23 +01003781 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003782 if (l >= r && (l != r || !left)) break;
3783
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003784 bc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003785 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003786 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003787 }
3788
3789 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003790}
3791
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003792static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003793{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003794 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003795 BcLexType top;
3796
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003797 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003798 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 top = BC_PARSE_TOP_OP(p);
3800
3801 while (top != BC_LEX_LPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003802 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003803
3804 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003805 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003806
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003807 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003808 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 top = BC_PARSE_TOP_OP(p);
3810 }
3811
3812 bc_vec_pop(&p->ops);
3813
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003814 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003815}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003816#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003817
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003818static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003819{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003820 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003821 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003822 size_t nparams;
3823
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003824 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003825 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3826
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003827 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003828 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003829
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003830 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003831 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003832 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003833 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003834 if (s) RETURN_STATUS(s);
3835 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003836 if (p->lex != BC_LEX_COMMA) {
3837 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003838 break;
3839 RETURN_STATUS(bc_error_bad_token());
3840 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003841 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003842 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 }
3844 }
3845
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003846 bc_parse_push(BC_INST_CALL);
3847 bc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003848
Denys Vlasenko26819db2018-12-12 16:08:46 +01003849 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003850}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003851#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003852
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003853// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003854static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003855{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003856 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003857 BcStatus s;
3858 BcId entry, *entry_ptr;
3859 size_t idx;
3860
3861 entry.name = name;
3862
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003863 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003864 if (s) goto err;
3865
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003866 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003867 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 goto err;
3869 }
3870
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003871 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003872
3873 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003874 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003875 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003876 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003877 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 free(name);
3879
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003880 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003881 bc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003882
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003883 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003884 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003886 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003887}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003888#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003889
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003890static BC_STATUS zbc_parse_name(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 char *name;
3895
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003896 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003897 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003898 if (s) goto err;
3899
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003900 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003901 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003902 if (s) goto err;
3903
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003904 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003906 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 goto err;
3908 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003909 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003910 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003911 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003912 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003913 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003914 if (s) goto err;
3915 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003916 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003917 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003918 bc_parse_push(*type);
3919 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003920 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003921 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003922 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003923 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003924 goto err;
3925 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003926 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003927 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003928 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003929 *type = XC_INST_VAR;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003930 bc_parse_push(XC_INST_VAR);
3931 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003932 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003933 }
3934
Denys Vlasenko26819db2018-12-12 16:08:46 +01003935 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003936 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003937 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003938 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003939}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003940#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003941
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003942static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003943{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003944 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003945 BcStatus s;
3946
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003947 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003948 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003949 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003951 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003952 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003953 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003954
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003955 bc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003956
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003957 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003958}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003959#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003960
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003961static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003962{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003963 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003964 BcStatus s;
3965
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003966 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003967 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003968 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003969
3970 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3971
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003972 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003973 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003974
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003975 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003976 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003977
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003978 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003979
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003980 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003981 bc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003982
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003983 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003984}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003985#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003986
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003987static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003988{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003989 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003990 BcStatus s;
3991
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003992 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003993 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003994
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003995 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003996 *type = XC_INST_SCALE;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003997 bc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003998 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003999 }
4000
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004001 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06004002 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4003
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004004 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004005 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004007 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004008 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004009 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01004010 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004011 bc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004012
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004013 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004014}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004015#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004016
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004017static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
4018 size_t *nexprs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004019{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004020 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004021 BcStatus s;
4022 BcLexType type;
4023 char inst;
4024 BcInst etype = *prev;
4025
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004026 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4027 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4028 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004029 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004030 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004031 bc_parse_push(inst);
4032 s = zbc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004033 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004034 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004035 *paren_expr = true;
4036
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004037 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004038 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004039 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004040
4041 // Because we parse the next part of the expression
4042 // right here, we need to increment this.
4043 *nexprs = *nexprs + 1;
4044
4045 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004046 case XC_LEX_NAME:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004047 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004048 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004049 case BC_LEX_KEY_IBASE:
4050 case BC_LEX_KEY_LAST:
4051 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004052 bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4053 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004054 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004055 case BC_LEX_KEY_SCALE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004056 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004057 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004058 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004059 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004060 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004061 bc_parse_push(XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004062 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004064 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004065 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004066 }
4067
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004068 if (!s) bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004069 }
4070
Denys Vlasenko26819db2018-12-12 16:08:46 +01004071 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004072}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004073#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004074
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004075#if 0
4076#define BC_PARSE_LEAF(p, rparen) \
4077 ((rparen) \
4078 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
4079 || (p) == BC_INST_INC_POST \
4080 || (p) == BC_INST_DEC_POST \
4081 )
4082#else
4083static int ok_in_expr(BcInst p)
4084{
4085 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4086 || p == BC_INST_INC_POST
4087 || p == BC_INST_DEC_POST
4088 ;
4089}
4090#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
4091#endif
4092
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004093static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
4094 bool rparen, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004095{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004096 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004097 BcStatus s;
4098 BcLexType type;
4099 BcInst etype = *prev;
4100
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004101 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004102 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004103
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004104 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004105 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004106
4107 // We can just push onto the op stack because this is the largest
4108 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004109 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004110 bc_vec_push(&p->ops, &type);
4111 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004112 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004113
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004114 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004115}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004116#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004117
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004118static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004119{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004120 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004121 BcStatus s;
4122 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004123
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004124 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004125 s = zbc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004126 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004127 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004128 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004129 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004130 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004131 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004132 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004133 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004134 bc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004135 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004136 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004137 }
4138
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004139 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004140}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004141#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004142
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004143static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004144{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004145 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004146 BcStatus s;
4147 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004148
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004149 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004150 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004151 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004152
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004153 t = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004154 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004155 bc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004157 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004158 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004159 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004160 bc_parse_push(BC_INST_RET0);
4161 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004162 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004163 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004164
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004165 if (!paren || p->lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004166 s = zbc_POSIX_requires("parentheses around return expressions");
4167 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004168 }
4169
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004170 bc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004171 }
4172
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004173 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004174 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004175}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004176#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004177
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004178static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004179{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004180 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004181 size_t *label = bc_vec_item(&p->func->labels, idx);
4182 *label = p->func->code.len;
4183}
4184
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004185static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004186{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004187 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004188 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004189 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004190
4191 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004192 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004193 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004194 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004195
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004196 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004197 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004198 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004199 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004200 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004201
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004202 // Encode "if zero, jump to ..."
4203 // Pushed value (destination of the jump) is uninitialized,
4204 // will be rewritten to be address of "end of if()" or of "else".
4205 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004206 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004207
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004208 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004209 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004210
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004211 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4212 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004213 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004214
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004215 // Encode "after then_stmt, jump to end of if()"
4216 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4217 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004218 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004219
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004220 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 +01004221 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004222
Denys Vlasenko15850832018-12-16 21:40:54 +01004223 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004224
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004225 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004226 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004227 }
4228
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004229 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 +01004230 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004231
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004232 dbg_lex_done("%s:%d done", __func__, __LINE__);
4233 RETURN_STATUS(s);
4234}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004235#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004236
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004237static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004238{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004239 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004240 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004241 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004242 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004243
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004244 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004245 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004246 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004247 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004248 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004249
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004250 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004251 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004252 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004253
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004254 bc_vec_push(&p->exits, &ip_idx);
4255 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004256
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004257 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004258 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004259 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004260
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004261 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004262
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004263 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004264 if (s) RETURN_STATUS(s);
4265
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004266 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004267 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004268
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004269 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004270 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004271
4272 bc_vec_pop(&p->exits);
4273 bc_vec_pop(&p->conds);
4274
4275 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004276}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004277#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004278
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004279static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004280{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004281 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004282 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004283 size_t cond_idx, exit_idx, body_idx, update_idx;
4284
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004285 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004286 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004287 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004288 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004289 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004290 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004291
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004292 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004293 s = zbc_parse_expr(0);
4294 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004295 if (s) RETURN_STATUS(s);
4296 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004297 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4298 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004299 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004300
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004301 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004302 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004303 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004305 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004306 update_idx = cond_idx + 1;
4307 body_idx = update_idx + 1;
4308 exit_idx = body_idx + 1;
4309
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004310 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004311 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004312 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004313 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004314 // This is safe to set because the current token is a semicolon,
4315 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004316 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004317 bc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004318 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004319 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004320 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004321
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004322 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004323
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004324 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004325 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004326
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004327 bc_parse_pushJUMP_ZERO(exit_idx);
4328 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004329
Gavin Howard01055ba2018-11-03 11:00:21 -06004330 bc_vec_push(&p->conds, &update_idx);
4331 bc_vec_push(&p->func->labels, &p->func->code.len);
4332
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004333 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004334 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004335 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004336 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004337 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004338 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004339 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4340 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004341 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004342
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004343 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004344 bc_vec_push(&p->func->labels, &p->func->code.len);
4345
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004346 bc_vec_push(&p->exits, &exit_idx);
4347 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004348
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004349 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004350 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004351
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004352 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004353 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004354
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004355 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004356 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004357
4358 bc_vec_pop(&p->exits);
4359 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004360
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004361 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004362}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004363#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004364
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004365static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004366{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004367 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004368 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004369
Gavin Howard01055ba2018-11-03 11:00:21 -06004370 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004371 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4372 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004373 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004374 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004375 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004376 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004377 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004378
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004379 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004380}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004381#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004382
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004383static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4384{
4385 BcId *autoid;
4386 BcId a;
4387 size_t i;
4388
4389 autoid = (void*)f->autos.v;
4390 for (i = 0; i < f->autos.len; i++, autoid++) {
4391 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004392 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004393 }
4394
4395 a.idx = var;
4396 a.name = name;
4397
4398 bc_vec_push(&f->autos, &a);
4399
4400 RETURN_STATUS(BC_STATUS_SUCCESS);
4401}
4402#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4403
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004404static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004405{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004406 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004407 BcStatus s;
4408 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004409 char *name;
4410
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004411 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004412 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004413 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004414 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004415 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004416
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004417 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004418 p->fidx = bc_program_addFunc(name);
4419 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004420
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004421 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004422 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004423 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004424 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004425 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004426 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004427
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004428 while (p->lex != BC_LEX_RPAREN) {
4429 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004430 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004431
4432 ++p->func->nparams;
4433
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004434 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004435 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004436 if (s) goto err;
4437
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004438 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004439
4440 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004441 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004442 if (s) goto err;
4443
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004444 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004445 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004446 goto err;
4447 }
4448
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004449 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004450 if (s) goto err;
4451 }
4452
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004453 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 if (comma) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004455 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 if (s) goto err;
4457 }
4458
Denys Vlasenko29301232018-12-11 15:29:32 +01004459 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 if (s) goto err;
4461 }
4462
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004463 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004464
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004465 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004466 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004467
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004468 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004469 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004470 if (s) RETURN_STATUS(s);
4471 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004472
Denys Vlasenko202dd192018-12-16 17:30:35 +01004473 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004474 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004475 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004476 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004477 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004478 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004479
4480 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004481 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004482 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004483 if (s) RETURN_STATUS(s);
4484
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004485 bc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004486
4487 // Subsequent code generation is into main program
4488 p->fidx = BC_PROG_MAIN;
4489 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004490
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004491 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004492 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004493 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004494 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004495 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004496 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004497}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004498#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004499
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004500static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004501{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004502 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004503 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004504 char *name;
4505
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004506 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004507 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004508 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004509
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004510 for (;;) {
4511 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004512
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004513 if (p->lex != XC_LEX_NAME)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004514 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4515
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004516 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004517 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004518 if (s) goto err;
4519
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004520 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004521 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004522 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004523 if (s) goto err;
4524
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004525 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004526 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004527 goto err;
4528 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004529 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004530 if (s) goto err;
4531 }
4532
Denys Vlasenko29301232018-12-11 15:29:32 +01004533 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004535
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004536 if (p->lex == XC_LEX_NLINE
4537 || p->lex == BC_LEX_SCOLON
4538 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004539 ) {
4540 break;
4541 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004542 if (p->lex != BC_LEX_COMMA)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004543 RETURN_STATUS(bc_error("bad 'auto' syntax"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004544 s = zbc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004545 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004546 }
4547
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004548 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004549 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004550 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004552 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004553 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004554}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004555#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004556
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004557#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004558static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004559{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004560 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004561 BcStatus s = BC_STATUS_SUCCESS;
4562
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004563 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004564
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004565 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004566 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004567 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004568 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004569 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004570 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004571 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004572 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004573
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004574 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004575 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4576 do {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004577 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004578 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004579 } while (p->lex == XC_LEX_NLINE);
4580 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004581 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004582 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004583 if (s) RETURN_STATUS(s);
4584 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004585 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004586 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004587 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004588 if (s) RETURN_STATUS(s);
4589 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004590 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004591 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4592 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 }
4594
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004595 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4596 switch (p->lex) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004597 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004598 case BC_LEX_OP_INC:
4599 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004600 case BC_LEX_OP_BOOL_NOT:
4601 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004602 case XC_LEX_NAME:
4603 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 case BC_LEX_KEY_IBASE:
4605 case BC_LEX_KEY_LAST:
4606 case BC_LEX_KEY_LENGTH:
4607 case BC_LEX_KEY_OBASE:
4608 case BC_LEX_KEY_READ:
4609 case BC_LEX_KEY_SCALE:
4610 case BC_LEX_KEY_SQRT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004611 s = zbc_parse_expr(BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004613 case XC_LEX_STR:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004614 s = zbc_parse_pushSTR();
4615 bc_parse_push(XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 case BC_LEX_KEY_BREAK:
4618 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004619 s = zbc_parse_break_or_continue(p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 case BC_LEX_KEY_FOR:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004622 s = zbc_parse_for();
Gavin Howard01055ba2018-11-03 11:00:21 -06004623 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 case BC_LEX_KEY_HALT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004625 bc_parse_push(BC_INST_HALT);
4626 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004627 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 case BC_LEX_KEY_IF:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004629 s = zbc_parse_if();
Gavin Howard01055ba2018-11-03 11:00:21 -06004630 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004631 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004632 // "limits" is a compile-time command,
4633 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004634 printf(
4635 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4636 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4637 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4638 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4639 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4640 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4641 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4642 "Number of vars = "BC_MAX_VARS_STR "\n"
4643 );
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004644 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004645 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004646 case BC_LEX_KEY_PRINT:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004647 s = zbc_parse_print();
Gavin Howard01055ba2018-11-03 11:00:21 -06004648 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004649 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004650 // "quit" is a compile-time command. For example,
4651 // "if (0 == 1) quit" terminates when parsing the statement,
4652 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004653 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004654 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004655 if (!p->in_funcdef)
4656 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004657 s = zbc_parse_return();
Gavin Howard01055ba2018-11-03 11:00:21 -06004658 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004659 case BC_LEX_KEY_WHILE:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004660 s = zbc_parse_while();
Gavin Howard01055ba2018-11-03 11:00:21 -06004661 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004662 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004663 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004664 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 }
4666
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004667 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004668 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004669}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004670#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004671
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004672static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004673{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004674 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004675 BcStatus s;
4676
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004677 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004678 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004679 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004680 else if (p->lex == BC_LEX_KEY_DEFINE) {
4681 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004682 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004683 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004684 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004685 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004686 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004687
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004688 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004689 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004690}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004691#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004692
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004693// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004694static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004695{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004696 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004697 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004698 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004699 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004700 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004701
Denys Vlasenko17df8822018-12-14 23:00:24 +01004702 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004703 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004704 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004705 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004706 bin_last = true;
4707
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004708 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004709 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004710 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004711 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004712
4713 if (!lex_allowed_in_bc_expr(t))
4714 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004715
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004716 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004717 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004718 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004719 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 case BC_LEX_OP_INC:
4721 case BC_LEX_OP_DEC:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004722 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004723 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004724 rprn = bin_last = false;
4725 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004726 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004727 case XC_LEX_OP_MINUS:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004728 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004729 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004730 rprn = false;
4731 //get_token = false; - already is
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004732 bin_last = (prev == XC_INST_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004733 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004734 case BC_LEX_OP_ASSIGN_POWER:
4735 case BC_LEX_OP_ASSIGN_MULTIPLY:
4736 case BC_LEX_OP_ASSIGN_DIVIDE:
4737 case BC_LEX_OP_ASSIGN_MODULUS:
4738 case BC_LEX_OP_ASSIGN_PLUS:
4739 case BC_LEX_OP_ASSIGN_MINUS:
4740 case BC_LEX_OP_ASSIGN:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004741 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004742 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4743 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4744 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004745 ) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004746 return bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004747 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004748 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004749 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004750 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004751 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004752 case XC_LEX_OP_POWER:
4753 case XC_LEX_OP_MULTIPLY:
4754 case XC_LEX_OP_DIVIDE:
4755 case XC_LEX_OP_MODULUS:
4756 case XC_LEX_OP_PLUS:
4757 case XC_LEX_OP_REL_EQ:
4758 case XC_LEX_OP_REL_LE:
4759 case XC_LEX_OP_REL_GE:
4760 case XC_LEX_OP_REL_NE:
4761 case XC_LEX_OP_REL_LT:
4762 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004763 case BC_LEX_OP_BOOL_NOT:
4764 case BC_LEX_OP_BOOL_OR:
4765 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004766 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004767 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004768 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004769 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004770 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004771 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004772 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004773 prev = BC_TOKEN_2_INST(t);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004774 bc_parse_operator(t, ops_bgn, &nexprs);
4775 s = zbc_lex_next();
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004776 rprn = false;
4777 //get_token = false; - already is
Denys Vlasenko69560f42018-12-24 14:14:23 +01004778 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004780 case BC_LEX_LPAREN:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004781 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004782 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004783 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004784 bc_vec_push(&p->ops, &t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004785 nparens++;
4786 get_token = true;
4787 paren_expr = false;
4788 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004789 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004790 case BC_LEX_RPAREN:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004791 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004792 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004793 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004794 if (nparens == 0) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004795 goto exit_loop;
Gavin Howard01055ba2018-11-03 11:00:21 -06004796 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004797 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004798 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004799 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004800 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004801 s = zbc_parse_rightParen(ops_bgn, &nexprs);
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004802 nparens--;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004803 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004804 paren_expr = rprn = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004805 bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004806 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004807 case XC_LEX_NAME:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004808 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004809 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004810 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004811 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004812 paren_expr = true;
4813 rprn = bin_last = false;
4814 //get_token = false; - already is
Denys Vlasenkobb116032018-12-25 01:16:37 +01004815 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004816 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004817 case XC_LEX_NUMBER:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004818 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004819 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004820 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004821 bc_parse_pushNUM();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004822 prev = XC_INST_NUM;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004823 get_token = true;
4824 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004825 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004826 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004827 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004828 case BC_LEX_KEY_IBASE:
4829 case BC_LEX_KEY_LAST:
4830 case BC_LEX_KEY_OBASE:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004831 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004832 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004833 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004834 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004835 bc_parse_push((char) prev);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004836 get_token = true;
4837 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004838 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004839 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004840 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004841 case BC_LEX_KEY_LENGTH:
4842 case BC_LEX_KEY_SQRT:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004843 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004844 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004845 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004846 s = zbc_parse_builtin(t, flags, &prev);
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004847 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004848 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004849 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004850 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004851 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004852 case BC_LEX_KEY_READ:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004853 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004854 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004855 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004856 s = zbc_parse_read();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004857 prev = XC_INST_READ;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004858 get_token = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004859 paren_expr = true;
4860 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004861 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004862 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004863 case BC_LEX_KEY_SCALE:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01004864 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004865 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004866 return bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004867 s = zbc_parse_scale(&prev, flags);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004868 prev = XC_INST_SCALE;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004869 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004870 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004871 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004872 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004873 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004874 default:
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004875 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004876 }
4877
Denys Vlasenkobb116032018-12-25 01:16:37 +01004878 if (s || G_interrupt) // error, or ^C: stop parsing
4879 return BC_STATUS_FAILURE;
4880 if (get_token) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004881 s = zbc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004882 if (s) return s;
4883 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004884 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004885 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004886
4887 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004888 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004889 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004890
4891 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004892 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004893
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004894 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004895
Denys Vlasenko69560f42018-12-24 14:14:23 +01004896 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 bc_vec_pop(&p->ops);
4898 }
4899
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004900 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004901 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004902
Gavin Howard01055ba2018-11-03 11:00:21 -06004903 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004904 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004905 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4906 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004907 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004908 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004909 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4910 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004911 }
4912
4913 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004914 if (paren_first || !assign)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004915 bc_parse_push(XC_INST_PRINT);
4916 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004917 }
4918
Denys Vlasenko17df8822018-12-14 23:00:24 +01004919 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004920 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004921}
4922
Gavin Howard01055ba2018-11-03 11:00:21 -06004923#endif // ENABLE_BC
4924
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004925#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004926
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004927static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004928{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004929 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004931
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004932 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004933 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004934 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004935
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004936 bc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004937
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004938 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004939}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004940#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004941
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004942static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004943{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004944 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004945 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004946 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004947
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004948 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004949
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004950 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004951 bc_parse_push(XC_INST_STR);
4952 bc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004953 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004954
4955 // Explanation needed here
4956 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004957 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004958
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004959 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004960}
4961
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004962static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004963{
4964 BcStatus s;
4965
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004966 bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004967 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004968 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004969 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004970 }
4971
4972 if (store) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004973 bc_parse_push(DC_INST_SWAP);
4974 bc_parse_push(XC_INST_ASSIGN);
4975 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004976 }
4977
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004978 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004979}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004980#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004982static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004983{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004984 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004985 BcStatus s;
4986
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004987 bc_parse_push(inst);
4988 bc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004989
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004990 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004991 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004992
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004993 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004994 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004995
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004996 // Note that 'else' part can not be on the next line:
4997 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4998 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004999 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005000 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005001 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005002 s = zbc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005003 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005004 bc_parse_push(BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005005 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005006
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005007 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005008}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005009#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005010
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005011static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005012{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005013 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005014 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005015 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005016
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005017 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005018 s = BC_STATUS_SUCCESS;
5019 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005020 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01005021 case XC_LEX_OP_REL_EQ:
5022 case XC_LEX_OP_REL_LE:
5023 case XC_LEX_OP_REL_GE:
5024 case XC_LEX_OP_REL_NE:
5025 case XC_LEX_OP_REL_LT:
5026 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005027 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005028 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005029 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06005030 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005031 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005032 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005033 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005034 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005035 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005036 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005037 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005038 dc_parse_string();
Gavin Howard01055ba2018-11-03 11:00:21 -06005039 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01005040 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005041 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005042 s = zbc_lex_next();
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005043 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005044 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005045 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005046 bc_parse_pushNUM();
5047 bc_parse_push(XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005048 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005049 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005050 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005051 bc_parse_pushNUM();
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005053 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005054 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005055 bc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005056 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005057 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005058 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005059 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005060 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005061 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005062 s = zdc_parse_mem(inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005063 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005064 case DC_LEX_LOAD:
5065 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005066 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005067 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005068 s = zdc_parse_mem(inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005069 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005070 case DC_LEX_STORE_IBASE:
5071 case DC_LEX_STORE_SCALE:
5072 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005073 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005074 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005075 s = zdc_parse_mem(inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005077 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005078 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5079 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005080 }
5081
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005082 if (!s && get_token) s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005083
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005084 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005085 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005086}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005087#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005088
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005089static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005090{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005091 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005092 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005093
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005094 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005095 if (i >= 0) {
5096 BcInst inst = dc_LEX_to_INST[i];
5097 if (inst != DC_INST_INVALID) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005098 bc_parse_push(inst);
5099 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005100 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005101 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005102 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005103}
5104#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5105
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005106static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005107{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005108 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005109 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5110 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005111 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005112 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005113 }
5114
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005115 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005116 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005117}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005118#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005119
Gavin Howard01055ba2018-11-03 11:00:21 -06005120#endif // ENABLE_DC
5121
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005122//
5123// Execution engine
5124//
5125
5126#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5127#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5128
Denys Vlasenkodf515392018-12-02 19:27:48 +01005129static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005130{
Gavin Howard01055ba2018-11-03 11:00:21 -06005131 BcId e, *ptr;
5132 BcVec *v, *map;
5133 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005134 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005135
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005136 v = var ? &G.prog.vars : &G.prog.arrs;
5137 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005138
5139 e.name = id;
5140 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005141 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005142
5143 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005144 BcVec v2;
5145 bc_array_init(&v2, var);
5146 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005147 }
5148
5149 ptr = bc_vec_item(map, i);
5150 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005151 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005152}
5153
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005154// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005155static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005156{
Gavin Howard01055ba2018-11-03 11:00:21 -06005157 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005158 case XC_RESULT_STR:
5159 case XC_RESULT_TEMP:
5160 case XC_RESULT_IBASE:
5161 case XC_RESULT_SCALE:
5162 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005163 *num = &r->d.n;
5164 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005165 case XC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005166 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005167 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005168 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005169 size_t len;
5170
5171 str = *bc_program_const(r->d.id.idx);
5172 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005173
5174 bc_num_init(&r->d.n, len);
5175
5176 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005177 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005178 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005179 if (s) {
5180 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005181 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005182 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005183 *num = &r->d.n;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005184 r->t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005185 break;
5186 }
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005187 case XC_RESULT_VAR:
5188 case XC_RESULT_ARRAY:
5189 case XC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005190 BcVec *v;
5191
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005192 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005193
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005194 if (r->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005195 v = bc_vec_top(v);
5196 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5197 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005198 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005199 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005200 break;
5201 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005202#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005203 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005204 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005205 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005206 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005207 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005208 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005209#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005210#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005211 default:
5212 // Testing the theory that dc does not reach LAST/ONE
5213 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005214#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005215 }
5216
Denys Vlasenko29301232018-12-11 15:29:32 +01005217 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005218}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005219#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005220
Denys Vlasenko29301232018-12-11 15:29:32 +01005221static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005222 BcResult **r, BcNum **rn, bool assign)
5223{
5224 BcStatus s;
5225 bool hex;
5226 BcResultType lt, rt;
5227
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005228 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005229 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005230
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005231 *r = bc_vec_item_rev(&G.prog.results, 0);
5232 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
5234 lt = (*l)->t;
5235 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005236 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005237
Denys Vlasenko29301232018-12-11 15:29:32 +01005238 s = zbc_program_num(*l, ln, false);
5239 if (s) RETURN_STATUS(s);
5240 s = zbc_program_num(*r, rn, hex);
5241 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005242
5243 // We run this again under these conditions in case any vector has been
5244 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005245 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005246 s = zbc_program_num(*l, ln, false);
5247 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005248 }
5249
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005250 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005251 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005252 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005253 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005254
Denys Vlasenko29301232018-12-11 15:29:32 +01005255 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005256}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005257#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005258
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005259static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005260{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005261 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005262 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005263 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005264}
5265
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005266// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005267static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005268{
5269 BcStatus s;
5270
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005271 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005272 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005273 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005274
Denys Vlasenko29301232018-12-11 15:29:32 +01005275 s = zbc_program_num(*r, n, false);
5276 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005277
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005278 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005279 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005280
Denys Vlasenko29301232018-12-11 15:29:32 +01005281 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005282}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005283#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005284
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005285static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005286{
5287 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005288 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005289}
5290
Denys Vlasenko259137d2018-12-11 19:42:05 +01005291static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005292{
5293 BcStatus s;
5294 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005295 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005296
Denys Vlasenko29301232018-12-11 15:29:32 +01005297 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005298 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005299 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005300
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005301 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005302 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005303 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005304 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005305
Denys Vlasenko259137d2018-12-11 19:42:05 +01005306 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005307 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005308 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005309 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005310}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005311#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005312
Denys Vlasenko26819db2018-12-12 16:08:46 +01005313static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005314{
5315 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005316 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005317 BcVec buf;
5318 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005319 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005320
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005321 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005322 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005323
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005324 f = bc_program_func(BC_PROG_READ);
5325 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005326
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005327 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005328 bc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005329 //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 +01005330
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005331 s = zbc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005332 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005333 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005334 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005335 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005336 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005337 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005338 if (s) goto exec_err;
5339
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005340 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005341 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005342 goto exec_err;
5343 }
5344
5345 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005346 ip.inst_idx = 0;
5347 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005348
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005349 bc_parse_push(XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005350 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005351
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005352 exec_err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005353 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005354 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005355 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005356 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005357}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005358#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005359
5360static size_t bc_program_index(char *code, size_t *bgn)
5361{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005362 unsigned char *bytes = (void*)(code + *bgn);
5363 unsigned amt;
5364 unsigned i;
5365 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005366
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005367 amt = *bytes++;
5368 *bgn += amt + 1;
5369
5370 amt *= 8;
5371 res = 0;
5372 for (i = 0; i < amt; i += 8)
5373 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005374
5375 return res;
5376}
5377
5378static char *bc_program_name(char *code, size_t *bgn)
5379{
5380 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005381 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005382
Denys Vlasenko694d2982018-12-18 19:17:11 +01005383 code += *bgn;
5384 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005385 i = 0;
5386 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005387 char c = *code++;
5388 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005389 break;
5390 s[i++] = c;
5391 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005392 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005393 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005394
5395 return s;
5396}
5397
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005398static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005399{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005400#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005401 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005402 // Example: echo '[]ap' | dc
5403 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005404 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005405 return;
5406 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005407#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005408 while (*str) {
5409 int c = *str++;
5410 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005411 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005412 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005413 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005414 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005415 case 'a':
5416 bb_putchar('\a');
5417 break;
5418 case 'b':
5419 bb_putchar('\b');
5420 break;
5421 case '\\':
5422 case 'e':
5423 bb_putchar('\\');
5424 break;
5425 case 'f':
5426 bb_putchar('\f');
5427 break;
5428 case 'n':
5429 bb_putchar('\n');
5430 G.prog.nchars = SIZE_MAX;
5431 break;
5432 case 'r':
5433 bb_putchar('\r');
5434 break;
5435 case 'q':
5436 bb_putchar('"');
5437 break;
5438 case 't':
5439 bb_putchar('\t');
5440 break;
5441 default:
5442 // Just print the backslash and following character.
5443 bb_putchar('\\');
5444 ++G.prog.nchars;
5445 bb_putchar(c);
5446 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005447 }
5448 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005449 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005450 }
5451}
5452
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005453static void bc_num_printNewline(void)
5454{
5455 if (G.prog.nchars == G.prog.len - 1) {
5456 bb_putchar('\\');
5457 bb_putchar('\n');
5458 G.prog.nchars = 0;
5459 }
5460}
5461
5462#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005463static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005464{
5465 (void) radix;
5466 bb_putchar((char) num);
5467 G.prog.nchars += width;
5468}
5469#endif
5470
5471static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5472{
5473 size_t exp, pow;
5474
5475 bc_num_printNewline();
5476 bb_putchar(radix ? '.' : ' ');
5477 ++G.prog.nchars;
5478
5479 bc_num_printNewline();
5480 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5481 continue;
5482
5483 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5484 size_t dig;
5485 bc_num_printNewline();
5486 dig = num / pow;
5487 num -= dig * pow;
5488 bb_putchar(((char) dig) + '0');
5489 }
5490}
5491
5492static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5493{
5494 if (radix) {
5495 bc_num_printNewline();
5496 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005497 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005498 }
5499
5500 bc_num_printNewline();
5501 bb_putchar(bb_hexdigits_upcase[num]);
5502 G.prog.nchars += width;
5503}
5504
5505static void bc_num_printDecimal(BcNum *n)
5506{
5507 size_t i, rdx = n->rdx - 1;
5508
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005509 if (n->neg) {
5510 bb_putchar('-');
5511 G.prog.nchars++;
5512 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005513
5514 for (i = n->len - 1; i < n->len; --i)
5515 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5516}
5517
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005518typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5519
Denys Vlasenkod3401432018-12-18 17:00:35 +01005520static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005521{
5522 BcStatus s;
5523 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005524 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005525 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005526 BcNum intp, fracp, digit, frac_len;
5527 unsigned long dig, *ptr;
5528 size_t i;
5529 bool radix;
5530
5531 if (n->len == 0) {
5532 print(0, width, false);
5533 RETURN_STATUS(BC_STATUS_SUCCESS);
5534 }
5535
5536 bc_vec_init(&stack, sizeof(long), NULL);
5537 bc_num_init(&intp, n->len);
5538 bc_num_init(&fracp, n->rdx);
5539 bc_num_init(&digit, width);
5540 bc_num_init(&frac_len, BC_NUM_INT(n));
5541 bc_num_copy(&intp, n);
5542 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005543 base.cap = ARRAY_SIZE(base_digs);
5544 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005545 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005546
5547 bc_num_truncate(&intp, intp.rdx);
5548 s = zbc_num_sub(n, &intp, &fracp, 0);
5549 if (s) goto err;
5550
5551 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005552 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005553 if (s) goto err;
5554 s = zbc_num_ulong(&digit, &dig);
5555 if (s) goto err;
5556 bc_vec_push(&stack, &dig);
5557 }
5558
5559 for (i = 0; i < stack.len; ++i) {
5560 ptr = bc_vec_item_rev(&stack, i);
5561 print(*ptr, width, false);
5562 }
5563
5564 if (!n->rdx) goto err;
5565
5566 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005567 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005568 if (s) goto err;
5569 s = zbc_num_ulong(&fracp, &dig);
5570 if (s) goto err;
5571 bc_num_ulong2num(&intp, dig);
5572 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5573 if (s) goto err;
5574 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005575 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005576 if (s) goto err;
5577 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005578 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005579 bc_num_free(&frac_len);
5580 bc_num_free(&digit);
5581 bc_num_free(&fracp);
5582 bc_num_free(&intp);
5583 bc_vec_free(&stack);
5584 RETURN_STATUS(s);
5585}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005586#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005587
5588static BC_STATUS zbc_num_printBase(BcNum *n)
5589{
5590 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005591 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005592 BcNumDigitOp print;
5593 bool neg = n->neg;
5594
5595 if (neg) {
5596 bb_putchar('-');
5597 G.prog.nchars++;
5598 }
5599
5600 n->neg = false;
5601
5602 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5603 width = 1;
5604 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005605 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005606 unsigned i = G.prog.ob_t - 1;
5607 width = 0;
5608 for (;;) {
5609 width++;
5610 i /= 10;
5611 if (i == 0)
5612 break;
5613 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005614 print = bc_num_printDigits;
5615 }
5616
Denys Vlasenkod3401432018-12-18 17:00:35 +01005617 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005618 n->neg = neg;
5619
5620 RETURN_STATUS(s);
5621}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005622#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005623
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005624static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5625{
5626 BcStatus s = BC_STATUS_SUCCESS;
5627
5628 bc_num_printNewline();
5629
5630 if (n->len == 0) {
5631 bb_putchar('0');
5632 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005633 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005634 bc_num_printDecimal(n);
5635 else
5636 s = zbc_num_printBase(n);
5637
5638 if (newline) {
5639 bb_putchar('\n');
5640 G.prog.nchars = 0;
5641 }
5642
5643 RETURN_STATUS(s);
5644}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005645#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005646
Denys Vlasenko29301232018-12-11 15:29:32 +01005647static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005648{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005649 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005650 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005651 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005652 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005653
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005654 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005655 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005656
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005657 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005658 s = zbc_program_num(r, &num, false);
5659 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005660
5661 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005662 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005663#if ENABLE_BC
5664 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5665#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005666 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005667 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005668
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005669 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005670 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005671
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005672 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005673 for (;;) {
5674 char c = *str++;
5675 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005676 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005677 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005678 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005679 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005680 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005681 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005682 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005683 }
5684 }
5685
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005686 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005687
Denys Vlasenko29301232018-12-11 15:29:32 +01005688 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005689}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005690#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005691
Denys Vlasenko29301232018-12-11 15:29:32 +01005692static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005693{
5694 BcStatus s;
5695 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005696 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005697
Denys Vlasenko29301232018-12-11 15:29:32 +01005698 s = zbc_program_prep(&ptr, &num);
5699 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005700
5701 bc_num_init(&res.d.n, num->len);
5702 bc_num_copy(&res.d.n, num);
5703 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5704
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005705 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005706
Denys Vlasenko29301232018-12-11 15:29:32 +01005707 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005708}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005709#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005710
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005711static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005712{
5713 BcStatus s;
5714 BcResult *opd1, *opd2, res;
5715 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005716 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005717
Denys Vlasenko29301232018-12-11 15:29:32 +01005718 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005719 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005720
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005721 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005722
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005723 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005724 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005725 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005726 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005727 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005728 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005729 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005730 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005731 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005732 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005733 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005734 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005735 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005736 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005737 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005738 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005739 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005740 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005741 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005742 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005743 cond = (cond > 0);
5744 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005745 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005746 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005747 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005748 }
5749 }
5750
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005751 if (cond) bc_num_one(&res.d.n);
5752 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005753
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005754 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005755
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005756 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005757}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005758#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005759
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005760#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005761static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005762{
5763 BcNum n2;
5764 BcResult res;
5765
5766 memset(&n2, 0, sizeof(BcNum));
5767 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005768 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005769
5770 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005771 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005772 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005773 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005774 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005775 }
5776
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005777 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005778 bc_vec_push(v, &n2);
5779
Denys Vlasenko29301232018-12-11 15:29:32 +01005780 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005781}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005782#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005783#endif // ENABLE_DC
5784
Denys Vlasenko29301232018-12-11 15:29:32 +01005785static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005786{
5787 BcStatus s;
5788 BcResult *ptr, r;
5789 BcVec *v;
5790 BcNum *n;
5791
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005792 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005793 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005794
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005795 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005796 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005797 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005798 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005799
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005800#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005801 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005802 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005803 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005804 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005805#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005806
Denys Vlasenko29301232018-12-11 15:29:32 +01005807 s = zbc_program_num(ptr, &n, false);
5808 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005809
5810 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005811 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005812
5813 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005814 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005815 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005816 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005817 bc_array_init(&r.d.v, true);
5818 bc_array_copy(&r.d.v, (BcVec *) n);
5819 }
5820
5821 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005822 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005823
Denys Vlasenko29301232018-12-11 15:29:32 +01005824 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005825}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005826#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005828static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005829{
5830 BcStatus s;
5831 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005832 BcNum *l, *r;
5833 bool assign = (inst == XC_INST_ASSIGN);
5834 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005835
Denys Vlasenko29301232018-12-11 15:29:32 +01005836 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005837 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005838
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005839 ib = left->t == XC_RESULT_IBASE;
5840 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005841
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005842#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005843 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005844 BcVec *v;
5845
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005846 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005847 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005848 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005849
Denys Vlasenkofa210792018-12-19 19:35:40 +01005850 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005851 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005852#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005853
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005854 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005855 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005856 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005857 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005858 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005859
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005860#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005861 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005862 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005863
5864 if (assign)
5865 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005866 else {
5867 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005868 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005869 }
5870 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005871#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005872 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005873#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005874
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005875 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005876 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005877 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5878 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5879 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005880 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005881 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005882 size_t max;
5883 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005884
Denys Vlasenko29301232018-12-11 15:29:32 +01005885 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005886 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005887 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005888 if (sc) {
5889 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005890 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005891 } else {
5892 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005893 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005894 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005895 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005896 }
5897
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005898 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005899 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
5901 *ptr = (size_t) val;
5902 s = BC_STATUS_SUCCESS;
5903 }
5904
5905 bc_num_init(&res.d.n, l->len);
5906 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005907 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005908
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005909 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005910}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005911#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenko416ce762018-12-02 20:57:17 +01005913#if !ENABLE_DC
5914#define bc_program_pushVar(code, bgn, pop, copy) \
5915 bc_program_pushVar(code, bgn)
5916// for bc, 'pop' and 'copy' are always false
5917#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005918static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005919 bool pop, bool copy)
5920{
Gavin Howard01055ba2018-11-03 11:00:21 -06005921 BcResult r;
5922 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005923
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005924 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 r.d.id.name = name;
5926
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005927#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005928 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005929 BcVec *v = bc_program_search(name, true);
5930 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005931
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005932 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005933 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005934 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005935 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005936
5937 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005938 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005939 bc_num_init_DEF_SIZE(&r.d.n);
5940 bc_num_copy(&r.d.n, num);
5941 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005942 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005943 r.d.id.idx = num->rdx;
5944 }
5945
5946 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 }
5948#endif // ENABLE_DC
5949
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005950 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005951
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005952 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005953}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005954#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005955
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005956static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005957{
5958 BcStatus s = BC_STATUS_SUCCESS;
5959 BcResult r;
5960 BcNum *num;
5961
5962 r.d.id.name = bc_program_name(code, bgn);
5963
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005964 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005965 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005966 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005967 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005968 BcResult *operand;
5969 unsigned long temp;
5970
Denys Vlasenko29301232018-12-11 15:29:32 +01005971 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005972 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005973 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005974 if (s) goto err;
5975
5976 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005977 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005978 goto err;
5979 }
5980
5981 r.d.id.idx = (size_t) temp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005982 bc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005983 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005984 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005985 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005986 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005987}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005988#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005989
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005990#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005991static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005992{
5993 BcStatus s;
5994 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005995 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005996 char inst2 = inst;
5997
Denys Vlasenko29301232018-12-11 15:29:32 +01005998 s = zbc_program_prep(&ptr, &num);
5999 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006000
6001 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006002 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006003 bc_num_init(&copy.d.n, num->len);
6004 bc_num_copy(&copy.d.n, num);
6005 }
6006
6007 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006008 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
6009 ? BC_INST_ASSIGN_PLUS
6010 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006012 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006013 s = zbc_program_assign(inst);
6014 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
6016 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006017 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006018 }
6019
Denys Vlasenko29301232018-12-11 15:29:32 +01006020 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006022#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006023
Denys Vlasenko29301232018-12-11 15:29:32 +01006024static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006025{
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006027 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006028 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006029 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006030 BcResult *arg;
6031
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006032 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006033 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006034 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006035 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006037 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006038 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006039 }
6040 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006041 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006042 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01006043 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
6045 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006046 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006047
6048 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006049 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006050
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006051 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006052 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006053
Denys Vlasenko29301232018-12-11 15:29:32 +01006054 s = zbc_program_copyToVar(a->name, a->idx);
6055 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006056 }
6057
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006058 a = bc_vec_item(&func->autos, i);
6059 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006060 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006061
Denys Vlasenkodf515392018-12-02 19:27:48 +01006062 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006063 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006064 BcNum n2;
6065 bc_num_init_DEF_SIZE(&n2);
6066 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006067 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006068 BcVec v2;
6069 bc_array_init(&v2, true);
6070 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006071 }
6072 }
6073
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006074 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006075
Denys Vlasenko29301232018-12-11 15:29:32 +01006076 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006077}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006078#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006079
Denys Vlasenko29301232018-12-11 15:29:32 +01006080static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006081{
Gavin Howard01055ba2018-11-03 11:00:21 -06006082 BcResult res;
6083 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006084 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006085 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006086 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006087
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006088 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 +01006089 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006090
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006091 f = bc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006092 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006093
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006094 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006095 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006097 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006098
Denys Vlasenko29301232018-12-11 15:29:32 +01006099 s = zbc_program_num(operand, &num, false);
6100 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 bc_num_init(&res.d.n, num->len);
6102 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006103 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006104 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006105 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006106 }
6107
6108 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006109 a = (void*)f->autos.v;
6110 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006111 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006112 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006113 bc_vec_pop(v);
6114 }
6115
Denys Vlasenko24e41942018-12-21 23:01:26 +01006116 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006117 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006118 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006119
Denys Vlasenko29301232018-12-11 15:29:32 +01006120 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006121}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006122#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006123#endif // ENABLE_BC
6124
6125static unsigned long bc_program_scale(BcNum *n)
6126{
6127 return (unsigned long) n->rdx;
6128}
6129
6130static unsigned long bc_program_len(BcNum *n)
6131{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006132 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006133
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006134 if (n->rdx != len) return len;
6135 for (;;) {
6136 if (len == 0) break;
6137 len--;
6138 if (n->num[len] != 0) break;
6139 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006140 return len;
6141}
6142
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006143static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006144{
6145 BcStatus s;
6146 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006147 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006148 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006149 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006151 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006152 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006153 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006154
Denys Vlasenko29301232018-12-11 15:29:32 +01006155 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006156 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006157
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006158#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006159 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006160 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006161#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006162
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006163 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006164
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006165 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006166 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006167#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006168 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006169 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006170 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006171#endif
6172#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006173 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006174 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006175 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006176
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006177 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006178 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006179 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006180#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006182 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006183 }
6184
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006185 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006186
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006187 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006188}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006189#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006190
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006191#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006192static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006193{
6194 BcStatus s;
6195 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006196 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006197
Denys Vlasenko29301232018-12-11 15:29:32 +01006198 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006199 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006201 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202 bc_num_init(&res2.d.n, n2->len);
6203
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006204 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006205 if (s) goto err;
6206
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006207 bc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006208 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006209 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006210
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006211 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006212 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006213 bc_num_free(&res2.d.n);
6214 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006215 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006216}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006217#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006218
Denys Vlasenkofa210792018-12-19 19:35:40 +01006219static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006220{
6221 BcStatus s;
6222 BcResult *r1, *r2, *r3, res;
6223 BcNum *n1, *n2, *n3;
6224
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006225 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006226 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006227 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006228 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006229
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006230 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006231 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006232 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006233 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006234 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006235
6236 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006237 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006238 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006239 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006240 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006241 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006242 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006243 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006244 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006245 }
6246 }
6247
6248 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006249 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006250 if (s) goto err;
6251
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006252 bc_vec_pop(&G.prog.results);
6253 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006254
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006255 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006256 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006257 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006258 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006259}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006260#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006261
Denys Vlasenkofa210792018-12-19 19:35:40 +01006262static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006263{
Gavin Howard01055ba2018-11-03 11:00:21 -06006264 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006265 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006266
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006267 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006268
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006269 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006270 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006271 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006272}
6273
Denys Vlasenkofa210792018-12-19 19:35:40 +01006274static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006275{
6276 BcStatus s;
6277 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006278 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006279 char **strs;
6280 char *str;
6281 char c;
6282 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006283
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006284 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006285 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006286 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006287
Denys Vlasenko29301232018-12-11 15:29:32 +01006288 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006289 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006290
6291 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006292 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006293 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006294 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006295
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006296 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006297 bc_num_copy(&n, num);
6298 bc_num_truncate(&n, n.rdx);
6299
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006300 strmb.cap = ARRAY_SIZE(strmb_digs);
6301 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006302 bc_num_ulong2num(&strmb, 0x100);
6303 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006304
Gavin Howard01055ba2018-11-03 11:00:21 -06006305 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006306 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307 if (s) goto num_err;
6308
6309 c = (char) val;
6310
6311 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006312 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006313 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006314 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006315 sp = *bc_program_str(idx);
6316 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006317 }
6318
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006319 strs = (void*)G.prog.strs.v;
6320 for (idx = 0; idx < G.prog.strs.len; idx++) {
6321 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6322 goto dup;
6323 }
6324 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006325 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006326 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006327 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006328 bc_vec_push(&G.prog.strs, &str);
6329 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006330 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006331 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006332 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006334 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006335 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006336 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006337 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006338}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006339#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006340
Denys Vlasenkofa210792018-12-19 19:35:40 +01006341static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006342{
6343 BcStatus s;
6344 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006345 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006346 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006347
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006348 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006349 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006350 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006351
Denys Vlasenko29301232018-12-11 15:29:32 +01006352 s = zbc_program_num(r, &n, false);
6353 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006354
Denys Vlasenko57734c92018-12-17 21:14:05 +01006355 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006356 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006357 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006358 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006359 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006360 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006361 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 }
6363
Denys Vlasenko29301232018-12-11 15:29:32 +01006364 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006365}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006366#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006367
Denys Vlasenkofa210792018-12-19 19:35:40 +01006368static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006369{
6370 BcStatus s;
6371 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006372 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006373 unsigned long val;
6374
Denys Vlasenko29301232018-12-11 15:29:32 +01006375 s = zbc_program_prep(&opnd, &num);
6376 if (s) RETURN_STATUS(s);
6377 s = zbc_num_ulong(num, &val);
6378 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006379
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006380 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006381
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006382 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006383 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006384 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006385 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006386 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006387
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006388 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006389
Denys Vlasenko29301232018-12-11 15:29:32 +01006390 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006391}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006392#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006393
Denys Vlasenkofa210792018-12-19 19:35:40 +01006394static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006395{
6396 BcStatus s = BC_STATUS_SUCCESS;
6397 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006398 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 BcInstPtr ip;
6400 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006401
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006402 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006403 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006404
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006406
6407 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006408 BcNum *n = n; // for compiler
6409 bool exec;
6410 char *name;
6411 char *then_name = bc_program_name(code, bgn);
6412 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006413
6414 if (code[*bgn] == BC_PARSE_STREND)
6415 (*bgn) += 1;
6416 else
6417 else_name = bc_program_name(code, bgn);
6418
6419 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006420 name = then_name;
6421 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006422 exec = true;
6423 name = else_name;
6424 }
6425
6426 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006427 BcVec *v;
6428 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006429 n = bc_vec_top(v);
6430 }
6431
6432 free(then_name);
6433 free(else_name);
6434
6435 if (!exec) goto exit;
6436 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006437 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006438 goto exit;
6439 }
6440
6441 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006442 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006443 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006444 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006445 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006446 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006447 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448 if (s || !BC_PROG_STR(n)) goto exit;
6449 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006450 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006451 goto exit;
6452 }
6453
6454 fidx = sidx + BC_PROG_REQ_FUNCS;
6455
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006456 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
6458 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006459 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006460 char *str;
6461
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006462 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006463 bc_parse_create(fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006464 str = *bc_program_str(sidx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006465 s = zbc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006466 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006467
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006468 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006469 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006470 bc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006471 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006472 s = bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006473 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006474 G.prs = sv_parse; // struct copy
6475 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006476 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006477 bc_vec_pop_all(&f->code);
6478 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006479 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 }
6481
Denys Vlasenko24e41942018-12-21 23:01:26 +01006482 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006483 ip.func = fidx;
6484
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006485 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006486 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006487
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006488 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006489 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006490 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006491 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006492}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006493#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006494#endif // ENABLE_DC
6495
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006496static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006497{
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 BcResult res;
6499 unsigned long val;
6500
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006501 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006502 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006503 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006504 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006505 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006506 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006507 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006508
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006509 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006510 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006511 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006512}
6513
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006514static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006515{
Gavin Howard01055ba2018-11-03 11:00:21 -06006516 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006517 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006518 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006520
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006521 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006522 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6523 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006524 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006525 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006526
Denys Vlasenko24e41942018-12-21 23:01:26 +01006527 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 -06006528 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006529#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006530 case BC_INST_JUMP_ZERO: {
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006531 BcNum *num;
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006532 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006533 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006534 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006535 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006536 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006537 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006538 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006539 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006540 break;
6541 }
6542 // else: fall through
6543 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006544 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006545 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006546 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006547 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006548 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006549 break;
6550 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006551 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006552 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006553 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006554 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006555 case BC_INST_INC_PRE:
6556 case BC_INST_DEC_PRE:
6557 case BC_INST_INC_POST:
6558 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006559 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006560 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006561 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006562 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006563 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006564 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006565 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006566 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006567 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006568 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006569 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006570 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006571 case XC_INST_BOOL_OR:
6572 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006573#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006574 case XC_INST_REL_EQ:
6575 case XC_INST_REL_LE:
6576 case XC_INST_REL_GE:
6577 case XC_INST_REL_NE:
6578 case XC_INST_REL_LT:
6579 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006580 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006581 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006582 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006583 case XC_INST_READ:
6584 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006585 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006586 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006587 case XC_INST_VAR:
6588 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006589 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006591 case XC_INST_ARRAY_ELEM:
6592 case XC_INST_ARRAY:
6593 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006594 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006596#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006598 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006599 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006600 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006601 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006602#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006603 case XC_INST_IBASE:
6604 case XC_INST_OBASE:
6605 case XC_INST_SCALE:
6606 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006607 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006608 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006609 case XC_INST_SCALE_FUNC:
6610 case XC_INST_LENGTH:
6611 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006612 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006613 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006614 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006615 case XC_INST_NUM:
6616 dbg_exec("XC_INST_NUM:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006617 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006618 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006619 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006621 case XC_INST_POP:
6622 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006623 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006624 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006626 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006627 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006628 case XC_INST_PRINT:
6629 case XC_INST_PRINT_POP:
6630 case XC_INST_PRINT_STR:
6631 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006632 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006634 case XC_INST_STR:
6635 dbg_exec("XC_INST_STR:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006636 r.t = XC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006637 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006638 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006639 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006640 case XC_INST_POWER:
6641 case XC_INST_MULTIPLY:
6642 case XC_INST_DIVIDE:
6643 case XC_INST_MODULUS:
6644 case XC_INST_PLUS:
6645 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006646 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006647 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006648 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006649 case XC_INST_BOOL_NOT: {
6650 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006651 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006652 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006653 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006654 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006655 if (bc_num_cmp(num, &G.prog.zero) == 0)
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006656 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006657 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006658 bc_program_retire(&r, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006659 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006660 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006661 case XC_INST_NEG:
6662 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006663 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006664 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006665#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006666 case BC_INST_ASSIGN_POWER:
6667 case BC_INST_ASSIGN_MULTIPLY:
6668 case BC_INST_ASSIGN_DIVIDE:
6669 case BC_INST_ASSIGN_MODULUS:
6670 case BC_INST_ASSIGN_PLUS:
6671 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006672#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006673 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006674 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006675 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006677#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006678 case DC_INST_POP_EXEC:
6679 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006680 bc_vec_pop(&G.prog.exestack);
6681 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006682 case DC_INST_MODEXP:
6683 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006684 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006685 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006686 case DC_INST_DIVMOD:
6687 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006688 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006689 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006690 case DC_INST_EXECUTE:
6691 case DC_INST_EXEC_COND:
6692 dbg_exec("DC_INST_EXEC[_COND]:");
6693 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006694 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006695 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006696 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006697 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006698 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006699 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006700 if (s) break;
6701 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006702 break;
6703 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006704 case DC_INST_CLEAR_STACK:
6705 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006706 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006707 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006708 case DC_INST_STACK_LEN:
6709 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006710 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006711 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006712 case DC_INST_DUPLICATE:
6713 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006714 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006715 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006716 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006717 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006718 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006719 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006720 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006721 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006722 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006723 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006724 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006725 ptr = bc_vec_item_rev(&G.prog.results, 0);
6726 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006727 memcpy(&r, ptr, sizeof(BcResult));
6728 memcpy(ptr, ptr2, sizeof(BcResult));
6729 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 break;
6731 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006732 case DC_INST_ASCIIFY:
6733 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006734 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006735 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006736 case DC_INST_PRINT_STREAM:
6737 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006738 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006739 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006740 case DC_INST_LOAD:
6741 case DC_INST_PUSH_VAR: {
6742 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006743 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006744 break;
6745 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006746 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006747 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006748 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 free(name);
6750 break;
6751 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006752 case DC_INST_QUIT:
6753 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006754 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006755 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006756 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006757 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006758 case DC_INST_NQUIT:
6759 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006760 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006761 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006762#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006763 read_updated_ip:
6764 // Instruction stack has changed, read new pointers
6765 ip = bc_vec_top(&G.prog.exestack);
6766 func = bc_program_func(ip->func);
6767 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006768 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 }
6770
Denys Vlasenkod38af482018-12-04 19:11:02 +01006771 if (s || G_interrupt) {
6772 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006773 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006774 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006775
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006776 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006777 }
6778
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006779 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006780}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006781#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006782
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006783static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006784{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006785 char *lenv;
6786 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006787
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006788 lenv = getenv(var);
6789 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006790 if (!lenv) return len;
6791
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006792 len = bb_strtou(lenv, NULL, 10) - 1;
6793 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006794 len = BC_NUM_PRINT_WIDTH;
6795
6796 return len;
6797}
6798
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006799static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006800{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006801 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006802
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006803 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006804 s = zbc_parse_text_init(text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006805 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006806
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006807 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006808 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006809 BcInstPtr *ip;
6810 BcFunc *f;
6811
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006812 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006813 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006814#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006815 if (G.prs.lex == BC_LEX_SCOLON
6816 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006817 ) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006818 s = zbc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006819 if (s) goto err;
6820 goto check_eof;
6821 }
6822
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006823 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006824 if (s) goto err;
6825
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006826 // Check that next token is a correct stmt delimiter -
6827 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006828 if (G.prs.lex != BC_LEX_SCOLON
6829 && G.prs.lex != XC_LEX_NLINE
6830 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006831 ) {
6832 const char *err_at;
6833//TODO: commonalize for other parse errors:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006834 err_at = G.prs.lex_next_at ? G.prs.lex_next_at : "UNKNOWN";
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006835 bc_error_fmt("bad statement terminator at '%.*s'",
6836 (int)(strchrnul(err_at, '\n') - err_at),
6837 err_at
6838 );
6839 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006840 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006841 // The above logic is fragile. Check these examples:
6842 // - interative read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006843#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006844 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006845#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006846 // Most of dc parsing assumes all whitespace,
6847 // including '\n', is eaten.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006848 while (G.prs.lex == XC_LEX_NLINE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006849 s = zbc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006850 if (s) goto err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006851 if (G.prs.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006852 goto done;
6853 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006854 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006855#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006856 }
6857 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006858 err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006859 bc_parse_reset(); // includes bc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006860 RETURN_STATUS(BC_STATUS_FAILURE);
6861 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006862
Denys Vlasenko39287e02018-12-22 02:23:08 +01006863 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006864 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006865 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006866 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006867 break;
6868 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006869
6870 ip = (void*)G.prog.exestack.v;
6871#if SANITY_CHECKS
6872 if (G.prog.exestack.len != 1) // should have only main's IP
6873 bb_error_msg_and_die("BUG:call stack");
6874 if (ip->func != BC_PROG_MAIN)
6875 bb_error_msg_and_die("BUG:not MAIN");
6876#endif
6877 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006878 // bc discards strings, constants and code after each
6879 // top-level statement in the "main program".
6880 // This prevents "yes 1 | bc" from growing its memory
6881 // without bound. This can be done because data stack
6882 // is empty and thus can't hold any references to
6883 // strings or constants, there is no generated code
6884 // which can hold references (after we discard one
6885 // we just executed). Code of functions can have references,
6886 // but bc stores function strings/constants in per-function
6887 // storage.
6888 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006889#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006890 if (G.prog.results.len != 0) // should be empty
6891 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006892#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006893 IF_BC(bc_vec_pop_all(&f->strs);)
6894 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006895 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006896 if (G.prog.results.len == 0
6897 && G.prog.vars.len == 0
6898 ) {
6899 // If stack is empty and no registers exist (TODO: or they are all empty),
6900 // we can get rid of accumulated strings and constants.
6901 // In this example dc process should not grow
6902 // its memory consumption with time:
6903 // yes 1pc | dc
6904 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6905 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6906 }
6907 // The code is discarded always (below), thus this example
6908 // should also not grow its memory consumption with time,
6909 // even though its data stack is not empty:
6910 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006911 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006912 // We drop generated and executed code for both bc and dc:
6913 bc_vec_pop_all(&f->code);
6914 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006915 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006916 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006917 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006918 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006919}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006920#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006921
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006922static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006923{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006924 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006925 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006926 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006927 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006928
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006929 G.prs.lex_filename = filename;
6930 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006931 G.err_line = G.prs.lex_line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06006932
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006933 do {
6934 s = zbc_vm_process("");
6935 // We do not stop looping on errors here if reading stdin.
6936 // Example: start interactive bc and enter "return".
6937 // It should say "'return' not in a function"
6938 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006939 } while (G.prs.lex_input_fp == stdin);
6940 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006941 RETURN_STATUS(s);
6942}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006943#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006944
6945static BC_STATUS zbc_vm_file(const char *file)
6946{
6947 BcStatus s;
6948 FILE *fp;
6949
6950 fp = xfopen_for_read(file);
6951 s = zbc_vm_execute_FILE(fp, file);
6952 fclose(fp);
6953
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006954 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006955}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006956#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006957
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006958#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006959static void bc_vm_info(void)
6960{
6961 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006962 "Adapted from https://github.com/gavinhoward/bc\n"
6963 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006964 , applet_name);
6965}
6966
6967static void bc_args(char **argv)
6968{
6969 unsigned opts;
6970 int i;
6971
6972 GETOPT_RESET();
6973#if ENABLE_FEATURE_BC_LONG_OPTIONS
6974 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6975 "warn\0" No_argument "w"
6976 "version\0" No_argument "v"
6977 "standard\0" No_argument "s"
6978 "quiet\0" No_argument "q"
6979 "mathlib\0" No_argument "l"
6980 "interactive\0" No_argument "i"
6981 );
6982#else
6983 opts = option_mask32 |= getopt32(argv, "wvsqli");
6984#endif
6985 if (getenv("POSIXLY_CORRECT"))
6986 option_mask32 |= BC_FLAG_S;
6987
6988 if (opts & BC_FLAG_V) {
6989 bc_vm_info();
6990 exit(0);
6991 }
6992
6993 for (i = optind; argv[i]; ++i)
6994 bc_vec_push(&G.files, argv + i);
6995}
6996
6997static void bc_vm_envArgs(void)
6998{
6999 BcVec v;
7000 char *buf;
7001 char *env_args = getenv("BC_ENV_ARGS");
7002
7003 if (!env_args) return;
7004
7005 G.env_args = xstrdup(env_args);
7006 buf = G.env_args;
7007
7008 bc_vec_init(&v, sizeof(char *), NULL);
7009
7010 while (*(buf = skip_whitespace(buf)) != '\0') {
7011 bc_vec_push(&v, &buf);
7012 buf = skip_non_whitespace(buf);
7013 if (!*buf)
7014 break;
7015 *buf++ = '\0';
7016 }
7017
7018 // NULL terminate, and pass argv[] so that first arg is argv[1]
7019 if (sizeof(int) == sizeof(char*)) {
7020 bc_vec_push(&v, &const_int_0);
7021 } else {
7022 static char *const nullptr = NULL;
7023 bc_vec_push(&v, &nullptr);
7024 }
7025 bc_args(((char **)v.v) - 1);
7026
7027 bc_vec_free(&v);
7028}
7029
7030static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007031 "scale=20"
7032"\n" "define e(x){"
7033"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007034////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7035//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7036//e(-.998896): GNU:.36828580434569428695
7037// above code:.36828580434569428696
7038// actual value:.3682858043456942869594...
7039// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007040"\n" "b=ibase"
7041"\n" "ibase=A"
7042"\n" "if(x<0){"
7043"\n" "n=1"
7044"\n" "x=-x"
7045"\n" "}"
7046"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007047"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007048"\n" "scale=scale(x)+1"
7049"\n" "while(x>1){"
7050"\n" "d+=1"
7051"\n" "x/=2"
7052"\n" "scale+=1"
7053"\n" "}"
7054"\n" "scale=r"
7055"\n" "r=x+1"
7056"\n" "p=x"
7057"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007058"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007059"\n" "p*=x"
7060"\n" "f*=i"
7061"\n" "v=p/f"
7062"\n" "r+=v"
7063"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007064"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007065"\n" "scale=s"
7066"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007067"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007068"\n" "return(r/1)"
7069"\n" "}"
7070"\n" "define l(x){"
7071"\n" "auto b,s,r,p,a,q,i,v"
7072"\n" "b=ibase"
7073"\n" "ibase=A"
7074"\n" "if(x<=0){"
7075"\n" "r=(1-10^scale)/1"
7076"\n" "ibase=b"
7077"\n" "return(r)"
7078"\n" "}"
7079"\n" "s=scale"
7080"\n" "scale+=6"
7081"\n" "p=2"
7082"\n" "while(x>=2){"
7083"\n" "p*=2"
7084"\n" "x=sqrt(x)"
7085"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007086"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007087"\n" "p*=2"
7088"\n" "x=sqrt(x)"
7089"\n" "}"
7090"\n" "r=a=(x-1)/(x+1)"
7091"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007092"\n" "v=1"
7093"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007094"\n" "a*=q"
7095"\n" "v=a/i"
7096"\n" "r+=v"
7097"\n" "}"
7098"\n" "r*=p"
7099"\n" "scale=s"
7100"\n" "ibase=b"
7101"\n" "return(r/1)"
7102"\n" "}"
7103"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007104"\n" "auto b,s,r,a,q,i"
7105"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007106"\n" "b=ibase"
7107"\n" "ibase=A"
7108"\n" "s=scale"
7109"\n" "scale=1.1*s+2"
7110"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007111"\n" "scale=0"
7112"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007113"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007114"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007115"\n" "scale=s+2"
7116"\n" "r=a=x"
7117"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007118"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007119"\n" "a*=q/(i*(i-1))"
7120"\n" "r+=a"
7121"\n" "}"
7122"\n" "scale=s"
7123"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007124"\n" "return(r/1)"
7125"\n" "}"
7126"\n" "define c(x){"
7127"\n" "auto b,s"
7128"\n" "b=ibase"
7129"\n" "ibase=A"
7130"\n" "s=scale"
7131"\n" "scale*=1.2"
7132"\n" "x=s(2*a(1)+x)"
7133"\n" "scale=s"
7134"\n" "ibase=b"
7135"\n" "return(x/1)"
7136"\n" "}"
7137"\n" "define a(x){"
7138"\n" "auto b,s,r,n,a,m,t,f,i,u"
7139"\n" "b=ibase"
7140"\n" "ibase=A"
7141"\n" "n=1"
7142"\n" "if(x<0){"
7143"\n" "n=-1"
7144"\n" "x=-x"
7145"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007146"\n" "if(scale<65){"
7147"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7148"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007149"\n" "}"
7150"\n" "s=scale"
7151"\n" "if(x>.2){"
7152"\n" "scale+=5"
7153"\n" "a=a(.2)"
7154"\n" "}"
7155"\n" "scale=s+3"
7156"\n" "while(x>.2){"
7157"\n" "m+=1"
7158"\n" "x=(x-.2)/(1+.2*x)"
7159"\n" "}"
7160"\n" "r=u=x"
7161"\n" "f=-x*x"
7162"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007163"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007164"\n" "u*=f"
7165"\n" "t=u/i"
7166"\n" "r+=t"
7167"\n" "}"
7168"\n" "scale=s"
7169"\n" "ibase=b"
7170"\n" "return((m*a+r)/n)"
7171"\n" "}"
7172"\n" "define j(n,x){"
7173"\n" "auto b,s,o,a,i,v,f"
7174"\n" "b=ibase"
7175"\n" "ibase=A"
7176"\n" "s=scale"
7177"\n" "scale=0"
7178"\n" "n/=1"
7179"\n" "if(n<0){"
7180"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007181"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007182"\n" "}"
7183"\n" "a=1"
7184"\n" "for(i=2;i<=n;++i)a*=i"
7185"\n" "scale=1.5*s"
7186"\n" "a=(x^n)/2^n/a"
7187"\n" "r=v=1"
7188"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007189"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007190"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007191"\n" "v=v*f/i/(n+i)"
7192"\n" "r+=v"
7193"\n" "}"
7194"\n" "scale=s"
7195"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007196"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007197"\n" "return(a*r/1)"
7198"\n" "}"
7199};
7200#endif // ENABLE_BC
7201
Denys Vlasenko19f11072018-12-12 22:48:19 +01007202static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007203{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007204 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007205 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007206 size_t i;
7207
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007208#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007209 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007210 // We know that internal library is not buggy,
7211 // thus error checking is normally disabled.
7212# define DEBUG_LIB 0
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007213 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007214 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007215 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007216#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007217
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007218 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007219 fname = (void*)G.files.v;
7220 for (i = 0; i < G.files.len; i++) {
7221 s = zbc_vm_file(*fname++);
7222 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7223 // Debug config, non-interactive mode:
7224 // return all the way back to main.
7225 // Non-debug builds do not come here
7226 // in non-interactive mode, they exit.
7227 RETURN_STATUS(s);
7228 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007229 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007230
Denys Vlasenko91cde952018-12-10 20:56:08 +01007231 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007232 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007233
Denys Vlasenko19f11072018-12-12 22:48:19 +01007234 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007235}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007236#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007237
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007238#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007239static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007240{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007241 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007242 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007243 bc_vec_free(&G.prog.vars);
7244 bc_vec_free(&G.prog.var_map);
7245 bc_vec_free(&G.prog.arrs);
7246 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007247 IF_DC(bc_vec_free(&G.prog.strs);)
7248 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007249 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007250 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007251 IF_BC(bc_num_free(&G.prog.last);)
7252 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007253 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007254 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007255}
7256
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007257static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007258{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007259 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007260 bc_program_free();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007261 bc_parse_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007262 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007263}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007264#endif
7265
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007266static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007267{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007268 BcInstPtr ip;
7269
Denys Vlasenko82269122018-12-14 16:30:56 +01007270 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007271 memset(&ip, 0, sizeof(BcInstPtr));
7272
Denys Vlasenko82269122018-12-14 16:30:56 +01007273 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007274 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007275 G.prog.ob_t = 10;
7276
Denys Vlasenko503faf92018-12-20 16:24:18 +01007277 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7278 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007279
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007280 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007281 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007282
Denys Vlasenko503faf92018-12-20 16:24:18 +01007283 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7284 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007285
7286 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007287 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007288
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007289 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007290 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007291 // a valid function name (and be short)
7292 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007293 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007294 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007295 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007296 bc_program_add_fn();
7297 bc_program_add_fn();
7298 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007299
7300 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007301 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007302
7303 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007304 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007305
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007306 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7307 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007308 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007309 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7310 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007311
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007312 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007313}
Gavin Howard01055ba2018-11-03 11:00:21 -06007314
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007315static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007316{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007317#if ENABLE_FEATURE_EDITING
7318 G.line_input_state = new_line_input_t(DO_HISTORY);
7319#endif
7320 G.prog.len = bc_vm_envLen(env_len);
7321
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007322 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007323 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007324 bc_program_init();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007325 bc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007326
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007327//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7328//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007329 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007330#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007331 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007332 // With SA_RESTART, most system calls will restart
7333 // (IOW: they won't fail with EINTR).
7334 // In particular, this means ^C won't cause
7335 // stdout to get into "error state" if SIGINT hits
7336 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007337 //
7338 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007339 // will only be handled after [Enter] since read()
7340 // from stdin is not interrupted by ^C either,
7341 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007342 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007343 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7344
7345 // Without SA_RESTART, this exhibits a bug:
7346 // "while (1) print 1" and try ^C-ing it.
7347 // Intermittently, instead of returning to input line,
7348 // you'll get "output error: Interrupted system call"
7349 // and exit.
7350 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007351#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007352 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007353 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007354 return 0; // "not a tty"
7355}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007356
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007357static BcStatus bc_vm_run(void)
7358{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007359 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007360#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007361 if (G_exiting) // it was actually "halt" or "quit"
7362 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007363 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007364# if ENABLE_FEATURE_EDITING
7365 free_line_input_t(G.line_input_state);
7366# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007367 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007368#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007369 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007370 return st;
7371}
7372
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007373#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007374int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007375int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007376{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007377 int is_tty;
7378
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007379 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007380
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007381 is_tty = bc_vm_init("BC_LINE_LENGTH");
7382
7383 bc_args(argv);
7384
7385 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7386 bc_vm_info();
7387
7388 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007389}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007390#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007391
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007392#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007393int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007394int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007395{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007396 int noscript;
7397
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007398 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007399
7400 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7401 // 1 char wider than bc from the same package.
7402 // Both default width, and xC_LINE_LENGTH=N are wider:
7403 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007404 // |1234\ |
7405 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007406 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007407 // |123\ |
7408 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007409 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007410 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007411
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007412 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7413 noscript = BC_FLAG_I;
7414 for (;;) {
7415 int n = getopt(argc, argv, "e:f:x");
7416 if (n <= 0)
7417 break;
7418 switch (n) {
7419 case 'e':
7420 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007421 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007422 if (n) return n;
7423 break;
7424 case 'f':
7425 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007426 n = zbc_vm_file(optarg);
7427 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007428 break;
7429 case 'x':
7430 option_mask32 |= DC_FLAG_X;
7431 break;
7432 default:
7433 bb_show_usage();
7434 }
7435 }
7436 argv += optind;
7437
7438 while (*argv) {
7439 noscript = 0;
7440 bc_vec_push(&G.files, argv++);
7441 }
7442
7443 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7444
7445 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007446}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007447#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007448
7449#endif // not DC_SMALL