blob: 8556a0e73372b41c5cbc1b964e221fbb6ce2f226 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010013//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060014//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010015//config: This bc has five differences to the GNU bc:
16//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config: 2) Arrays are copied before being passed as arguments to
18//config: functions. This behavior is required by the bc spec.
19//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010020//config: the number of elements in the array. This prints "1":
21//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010023//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060024//config: allows POSIX-compliant scripts to work while somewhat
25//config: preserving expected behavior (versus C) and making parsing
26//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config:
29//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: -q --quiet don't print version and copyright
32//config: -s --standard error if any non-POSIX extensions are used
33//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: s(expr) sine in radians
36//config: c(expr) cosine in radians
37//config: a(expr) arctangent, returning radians
38//config: l(expr) natural log
39//config: e(expr) raises e to the power of expr
40//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
42//config:config DC
43//config: bool "dc (38 kb; 49 kb when combined with bc)"
44//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
62//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010065//config: whitespace. Examples:
66//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060067//config: Also note that, like the FreeBSD dc, extended registers are not
68//config: allowed unless the "-x" option is given.
69//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010070//config:config FEATURE_DC_SMALL
71//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
72//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010073//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
78//config: depends on FEATURE_DC_SMALL
79//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010086//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100101//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600102//config: help
103//config: Enable long options for bc and dc.
104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100113//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100139//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100144//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166#if ENABLE_FEATURE_DC_SMALL
167# include "dc.c"
168#else
169
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100170#define DEBUG_LEXER 0
171#define DEBUG_COMPILE 0
172#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100173// This can be left enabled for production as well:
174#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100175
176#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100177static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100178#define dbg_lex(...) \
179 do { \
180 fprintf(stderr, "%*s", lex_indent, ""); \
181 bb_error_msg(__VA_ARGS__); \
182 } while (0)
183#define dbg_lex_enter(...) \
184 do { \
185 dbg_lex(__VA_ARGS__); \
186 lex_indent++; \
187 } while (0)
188#define dbg_lex_done(...) \
189 do { \
190 lex_indent--; \
191 dbg_lex(__VA_ARGS__); \
192 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100193#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100194# define dbg_lex(...) ((void)0)
195# define dbg_lex_enter(...) ((void)0)
196# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100197#endif
198
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100199#if DEBUG_COMPILE
200# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
201#else
202# define dbg_compile(...) ((void)0)
203#endif
204
Denys Vlasenko99b37622018-12-15 20:06:59 +0100205#if DEBUG_EXEC
206# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
207#else
208# define dbg_exec(...) ((void)0)
209#endif
210
Gavin Howard01055ba2018-11-03 11:00:21 -0600211typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100212 BC_STATUS_SUCCESS = 0,
213 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100214 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600215} BcStatus;
216
Gavin Howard01055ba2018-11-03 11:00:21 -0600217#define BC_VEC_INVALID_IDX ((size_t) -1)
218#define BC_VEC_START_CAP (1 << 5)
219
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100220typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
222typedef struct BcVec {
223 char *v;
224 size_t len;
225 size_t cap;
226 size_t size;
227 BcVecFree dtor;
228} BcVec;
229
Gavin Howard01055ba2018-11-03 11:00:21 -0600230typedef signed char BcDig;
231
232typedef struct BcNum {
233 BcDig *restrict num;
234 size_t rdx;
235 size_t len;
236 size_t cap;
237 bool neg;
238} BcNum;
239
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100240#define BC_NUM_MAX_IBASE ((unsigned long) 16)
241// larger value might speed up BIGNUM calculations a bit:
242#define BC_NUM_DEF_SIZE (16)
243#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100245#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Gavin Howard01055ba2018-11-03 11:00:21 -0600247typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100248#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600249 BC_INST_INC_PRE,
250 BC_INST_DEC_PRE,
251 BC_INST_INC_POST,
252 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100254 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600255
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100256 XC_INST_POWER, // should
257 XC_INST_MULTIPLY, // match
258 XC_INST_DIVIDE, // LEX
259 XC_INST_MODULUS, // constants
260 XC_INST_PLUS, // for
261 XC_INST_MINUS, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600262
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100263 XC_INST_REL_EQ, // operations
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100264 XC_INST_REL_LE, // |
265 XC_INST_REL_GE, // |
266 XC_INST_REL_NE, // |
267 XC_INST_REL_LT, // |
268 XC_INST_REL_GT, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600269
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100270 XC_INST_BOOL_NOT, // |
271 XC_INST_BOOL_OR, // |
272 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100273#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100274 BC_INST_ASSIGN_POWER, // |
275 BC_INST_ASSIGN_MULTIPLY,// |
276 BC_INST_ASSIGN_DIVIDE, // |
277 BC_INST_ASSIGN_MODULUS, // |
278 BC_INST_ASSIGN_PLUS, // |
279 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100281 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600282
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100283 XC_INST_NUM,
284 XC_INST_VAR,
285 XC_INST_ARRAY_ELEM,
286 XC_INST_ARRAY,
287 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100288
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100289 XC_INST_IBASE, // order of these constans should match other enums
290 XC_INST_OBASE, // order of these constans should match other enums
291 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100292 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100293 XC_INST_LENGTH,
294 XC_INST_READ,
295 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600296
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100297 XC_INST_PRINT,
298 XC_INST_PRINT_POP,
299 XC_INST_STR,
300 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600301
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100302#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100303 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600304 BC_INST_JUMP,
305 BC_INST_JUMP_ZERO,
306
307 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600308 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100309#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600311
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100312 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100313#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100314 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_MODEXP,
317 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600318
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100319 DC_INST_EXECUTE,
320 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600321
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100322 DC_INST_ASCIIFY,
323 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600324
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100325 DC_INST_PRINT_STACK,
326 DC_INST_CLEAR_STACK,
327 DC_INST_STACK_LEN,
328 DC_INST_DUPLICATE,
329 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100331 DC_INST_LOAD,
332 DC_INST_PUSH_VAR,
333 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600334
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100335 DC_INST_QUIT,
336 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600337
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100338 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100339#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600340} BcInst;
341
342typedef struct BcId {
343 char *name;
344 size_t idx;
345} BcId;
346
347typedef struct BcFunc {
348 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100349 IF_BC(BcVec labels;)
350 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100351 IF_BC(BcVec strs;)
352 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100353 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600354} BcFunc;
355
356typedef enum BcResultType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600357 BC_RESULT_TEMP,
358
359 BC_RESULT_VAR,
360 BC_RESULT_ARRAY_ELEM,
361 BC_RESULT_ARRAY,
362
363 BC_RESULT_STR,
364
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100365 //code uses "inst - XC_INST_IBASE + BC_RESULT_IBASE" construct,
366 BC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
367 BC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
368 BC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100369 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600370 BC_RESULT_CONSTANT,
371 BC_RESULT_ONE,
Gavin Howard01055ba2018-11-03 11:00:21 -0600372} BcResultType;
373
374typedef union BcResultData {
375 BcNum n;
376 BcVec v;
377 BcId id;
378} BcResultData;
379
380typedef struct BcResult {
381 BcResultType t;
382 BcResultData d;
383} BcResult;
384
385typedef struct BcInstPtr {
386 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100387 size_t inst_idx;
388 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600389} BcInstPtr;
390
Gavin Howard01055ba2018-11-03 11:00:21 -0600391typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100392 XC_LEX_EOF,
393 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600394
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100395 XC_LEX_NLINE,
396 XC_LEX_WHITESPACE,
397 XC_LEX_STR,
398 XC_LEX_NAME,
399 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100400
Denys Vlasenko69560f42018-12-24 14:14:23 +0100401 XC_LEX_1st_op,
402 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100403
Denys Vlasenko69560f42018-12-24 14:14:23 +0100404 XC_LEX_OP_POWER, // should
405 XC_LEX_OP_MULTIPLY, // match
406 XC_LEX_OP_DIVIDE, // INST
407 XC_LEX_OP_MODULUS, // constants
408 XC_LEX_OP_PLUS, // for
409 XC_LEX_OP_MINUS, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100410
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100411 XC_LEX_OP_REL_EQ, // operations
Denys Vlasenko69560f42018-12-24 14:14:23 +0100412 XC_LEX_OP_REL_LE, // |
413 XC_LEX_OP_REL_GE, // |
414 XC_LEX_OP_REL_NE, // |
415 XC_LEX_OP_REL_LT, // |
416 XC_LEX_OP_REL_GT, // |
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100417 XC_LEX_OP_last = XC_LEX_OP_REL_GT,
418#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100419 BC_LEX_OP_BOOL_NOT, // |
420 BC_LEX_OP_BOOL_OR, // |
421 BC_LEX_OP_BOOL_AND, // |
422
423 BC_LEX_OP_ASSIGN_POWER, // |
424 BC_LEX_OP_ASSIGN_MULTIPLY, // |
425 BC_LEX_OP_ASSIGN_DIVIDE, // |
426 BC_LEX_OP_ASSIGN_MODULUS, // |
427 BC_LEX_OP_ASSIGN_PLUS, // |
428 BC_LEX_OP_ASSIGN_MINUS, // |
429
430 BC_LEX_OP_ASSIGN, // V
431
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_OP_INC,
433 BC_LEX_OP_DEC,
434
Gavin Howard01055ba2018-11-03 11:00:21 -0600435 BC_LEX_LPAREN,
436 BC_LEX_RPAREN,
437
438 BC_LEX_LBRACKET,
439 BC_LEX_COMMA,
440 BC_LEX_RBRACKET,
441
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100442 BC_LEX_LBRACE, // '{' is 0x7B, '}' is 0x7D,
Gavin Howard01055ba2018-11-03 11:00:21 -0600443 BC_LEX_SCOLON,
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100444 BC_LEX_RBRACE, // should be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600445
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100446 BC_LEX_KEY_1st_keyword,
447 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600448 BC_LEX_KEY_BREAK,
449 BC_LEX_KEY_CONTINUE,
450 BC_LEX_KEY_DEFINE,
451 BC_LEX_KEY_ELSE,
452 BC_LEX_KEY_FOR,
453 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100454 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
455 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
456 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100458 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_LENGTH,
460 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600461 BC_LEX_KEY_PRINT,
462 BC_LEX_KEY_QUIT,
463 BC_LEX_KEY_READ,
464 BC_LEX_KEY_RETURN,
465 BC_LEX_KEY_SCALE,
466 BC_LEX_KEY_SQRT,
467 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100468#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600469
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100470#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100471 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
472 DC_LEX_OP_ASSIGN,
473
474 DC_LEX_LPAREN,
475 DC_LEX_SCOLON,
476 DC_LEX_READ,
477 DC_LEX_IBASE,
478 DC_LEX_SCALE,
479 DC_LEX_OBASE,
480 DC_LEX_LENGTH,
481 DC_LEX_PRINT,
482 DC_LEX_QUIT,
483 DC_LEX_SQRT,
484 DC_LEX_LBRACE,
485
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100486 DC_LEX_EQ_NO_REG,
487 DC_LEX_OP_MODEXP,
488 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600489
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100490 DC_LEX_COLON,
491 DC_LEX_ELSE,
492 DC_LEX_EXECUTE,
493 DC_LEX_PRINT_STACK,
494 DC_LEX_CLEAR_STACK,
495 DC_LEX_STACK_LEVEL,
496 DC_LEX_DUPLICATE,
497 DC_LEX_SWAP,
498 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600499
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100500 DC_LEX_ASCIIFY,
501 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600502
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100503 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
504 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
505 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
506 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
507 DC_LEX_LOAD,
508 DC_LEX_LOAD_POP,
509 DC_LEX_STORE_PUSH,
510 DC_LEX_PRINT_POP,
511 DC_LEX_NQUIT,
512 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600514} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515// must match order of BC_LEX_KEY_foo etc above
516#if ENABLE_BC
517struct BcLexKeyword {
518 char name8[8];
519};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520#define BC_LEX_KW_ENTRY(a, b) \
521 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100522static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100523 BC_LEX_KW_ENTRY("auto" , 1), // 0
524 BC_LEX_KW_ENTRY("break" , 1), // 1
525 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
526 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527 BC_LEX_KW_ENTRY("else" , 0), // 4
528 BC_LEX_KW_ENTRY("for" , 1), // 5
529 BC_LEX_KW_ENTRY("halt" , 0), // 6
530 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100531 BC_LEX_KW_ENTRY("obase" , 1), // 8
532 BC_LEX_KW_ENTRY("if" , 1), // 9
533 BC_LEX_KW_ENTRY("last" , 0), // 10
534 BC_LEX_KW_ENTRY("length" , 1), // 11
535 BC_LEX_KW_ENTRY("limits" , 0), // 12
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100536 BC_LEX_KW_ENTRY("print" , 0), // 13
537 BC_LEX_KW_ENTRY("quit" , 1), // 14
538 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100539 BC_LEX_KW_ENTRY("return" , 1), // 16
540 BC_LEX_KW_ENTRY("scale" , 1), // 17
541 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
542 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100545#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100547#define STRING_if (bc_lex_kws[9].name8)
548#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100549enum {
550 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100551 | (1 << 0) // 0
552 | (1 << 1) // 1
553 | (0 << 2) // 2
554 | (1 << 3) // 3
555 | (0 << 4) // 4
556 | (1 << 5) // 5
557 | (0 << 6) // 6
558 | (1 << 7) // 7
559 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100560 | (1 << 9) // 9
561 | (0 << 10) // 10
562 | (1 << 11) // 11
563 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100564 | (0 << 13) // 13
565 | (1 << 14) // 14
566 | (0 << 15) // 15
567 | (1 << 16) // 16
568 | (1 << 17) // 17
569 | (1 << 18) // 18
570 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100571};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100572#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100573
574// This is a bit array that corresponds to token types. An entry is
575// true if the token is valid in an expression, false otherwise.
576// Used to figure out when expr parsing should stop *without error message*
577// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100578// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100579// by later processing.
580enum {
581#define EXBITS(a,b,c,d,e,f,g,h) \
582 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100583 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100584 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
585 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: ^ * / % + - == <=
586 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: >= != < > ! || && ^=
587 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
588 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100589 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
590 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
591 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100592#undef EXBITS
593};
594static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
595{
596#if ULONG_MAX > 0xffffffff
597 // 64-bit version (will not work correctly for 32-bit longs!)
598 return BC_PARSE_EXPRS_BITS & (1UL << i);
599#else
600 // 32-bit version
601 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
602 if (i >= 32) {
603 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
604 i &= 31;
605 }
606 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100607#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100608}
609
610// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100611// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612static const uint8_t bc_parse_ops[] = {
613#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614 OP(1, false), // neg
615 OP(2, false), // pow
616 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
617 OP(4, true ), OP( 4, true ), // + -
618 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
619 OP(1, false), // not
620 OP(7, true ), OP( 7, true ), // or and
621 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
622 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100623 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100624#undef OP
625};
626#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
627#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
628#endif // ENABLE_BC
629
630#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100631static const //BcLexType - should be this type
632uint8_t
633dc_char_to_LEX[] = {
634 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100635 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100636 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100637 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100638 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100639 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100640 /* 0123456789 */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100641 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100644 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100645 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
646 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100647 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100648 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
649 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100650 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100651 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
652 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100653 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100654 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
655 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100656 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100657 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100658 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100659 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100660 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100661 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
662 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100663 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100666 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100667 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
668 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100669 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100670 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100671 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100672 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100673};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100674static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100675int8_t
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100676dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
677 XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY
678 XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS
679 XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS
680 DC_INST_INVALID, DC_INST_INVALID, // OP_REL_EQ OP_REL_LE
681 DC_INST_INVALID, DC_INST_INVALID, // OP_REL_GE OP_REL_NE
682 DC_INST_INVALID, DC_INST_INVALID, // OP_REL_LT OP_REL_GT
683 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
684 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
685 XC_INST_REL_GT, // DC_LEX_LPAREN
686 DC_INST_INVALID, // DC_LEX_SCOLON
687 DC_INST_INVALID, // DC_LEX_READ
688 XC_INST_IBASE, // DC_LEX_IBASE
689 XC_INST_SCALE, // DC_LEX_SCALE
690 XC_INST_OBASE, // DC_LEX_OBASE
691 XC_INST_LENGTH, // DC_LEX_LENGTH
692 XC_INST_PRINT, // DC_LEX_PRINT
693 DC_INST_QUIT, // DC_LEX_QUIT
694 XC_INST_SQRT, // DC_LEX_SQRT
695 XC_INST_REL_GE, // DC_LEX_LBRACE
696 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
697 DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD
698 DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE
699 DC_INST_EXECUTE, // EXECUTE
700 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK
701 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE
702 DC_INST_SWAP, XC_INST_POP, // SWAP POP
703 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM
704 DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE
705 DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD
706 DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH
707 XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT
708 XC_INST_SCALE_FUNC, // SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100709 // DC_INST_INVALID in this table either means that corresponding LEX
710 // is not possible for dc, or that it does not compile one-to-one
711 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100712};
713#endif // ENABLE_DC
714
Gavin Howard01055ba2018-11-03 11:00:21 -0600715typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600716 const char *buf;
717 size_t i;
718 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600719 size_t len;
720 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600721 struct {
722 BcLexType t;
723 BcLexType last;
724 BcVec v;
725 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600726} BcLex;
727
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100728#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600729
Denys Vlasenko39287e02018-12-22 02:23:08 +0100730#if ENABLE_BC
731# define BC_PARSE_REL (1 << 0)
732# define BC_PARSE_PRINT (1 << 1)
733# define BC_PARSE_ARRAY (1 << 2)
734# define BC_PARSE_NOCALL (1 << 3)
735#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600736
Gavin Howard01055ba2018-11-03 11:00:21 -0600737typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600738 BcLex l;
739
Denys Vlasenko503faf92018-12-20 16:24:18 +0100740 IF_BC(BcVec exits;)
741 IF_BC(BcVec conds;)
742 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600743
Gavin Howard01055ba2018-11-03 11:00:21 -0600744 BcFunc *func;
745 size_t fidx;
746
Denys Vlasenko503faf92018-12-20 16:24:18 +0100747 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600748} BcParse;
749
Gavin Howard01055ba2018-11-03 11:00:21 -0600750typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600751 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100752 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600753
Denys Vlasenko503faf92018-12-20 16:24:18 +0100754 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600755 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600756 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600757
Gavin Howard01055ba2018-11-03 11:00:21 -0600758 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100759 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600760
761 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100762 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600763
764 BcVec vars;
765 BcVec var_map;
766
767 BcVec arrs;
768 BcVec arr_map;
769
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100770 IF_DC(BcVec strs;)
771 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600772
773 const char *file;
774
Gavin Howard01055ba2018-11-03 11:00:21 -0600775 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100776 IF_BC(BcNum one;)
777 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600778} BcProgram;
779
Gavin Howard01055ba2018-11-03 11:00:21 -0600780#define BC_PROG_MAIN (0)
781#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100782#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600783#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100784#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600785
786#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
787#define BC_PROG_NUM(r, n) \
788 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
789
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100790#define BC_FLAG_W (1 << 0)
791#define BC_FLAG_V (1 << 1)
792#define BC_FLAG_S (1 << 2)
793#define BC_FLAG_Q (1 << 3)
794#define BC_FLAG_L (1 << 4)
795#define BC_FLAG_I (1 << 5)
796#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600797
798#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
799#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
800
Denys Vlasenko64074a12018-12-07 15:50:14 +0100801#define BC_MAX_OBASE ((unsigned) 999)
802#define BC_MAX_DIM ((unsigned) INT_MAX)
803#define BC_MAX_SCALE ((unsigned) UINT_MAX)
804#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
805#define BC_MAX_NUM BC_MAX_STRING
806// Unused apart from "limits" message. Just show a "biggish number" there.
807//#define BC_MAX_NAME BC_MAX_STRING
808//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
809//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
810#define BC_MAX_NAME_STR "999999999"
811#define BC_MAX_EXP_STR "999999999"
812#define BC_MAX_VARS_STR "999999999"
813
814#define BC_MAX_OBASE_STR "999"
815
816#if INT_MAX == 2147483647
817# define BC_MAX_DIM_STR "2147483647"
818#elif INT_MAX == 9223372036854775807
819# define BC_MAX_DIM_STR "9223372036854775807"
820#else
821# error Strange INT_MAX
822#endif
823
824#if UINT_MAX == 4294967295
825# define BC_MAX_SCALE_STR "4294967295"
826# define BC_MAX_STRING_STR "4294967294"
827#elif UINT_MAX == 18446744073709551615
828# define BC_MAX_SCALE_STR "18446744073709551615"
829# define BC_MAX_STRING_STR "18446744073709551614"
830#else
831# error Strange UINT_MAX
832#endif
833#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600834
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100835struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100836 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100837 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600838
839 BcParse prs;
840 BcProgram prog;
841
Denys Vlasenko5318f812018-12-05 17:48:01 +0100842 // For error messages. Can be set to current parsed line,
843 // or [TODO] to current executing line (can be before last parsed one)
844 unsigned err_line;
845
Gavin Howard01055ba2018-11-03 11:00:21 -0600846 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100847 BcVec input_buffer;
848 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600849
850 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100851
852#if ENABLE_FEATURE_EDITING
853 line_input_t *line_input_state;
854#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100855} FIX_ALIASING;
856#define G (*ptr_to_globals)
857#define INIT_G() do { \
858 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
859} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100860#define FREE_G() do { \
861 FREE_PTR_TO_GLOBALS(); \
862} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100863#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
864#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100865#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100866#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100867# define G_interrupt bb_got_signal
868# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100869#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100870# define G_interrupt 0
871# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100872#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100873#if ENABLE_FEATURE_CLEAN_UP
874# define G_exiting G.exiting
875#else
876# define G_exiting 0
877#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100878#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100879#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100880
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100881// In configurations where errors abort instead of propagating error
882// return code up the call chain, functions returning BC_STATUS
883// actually don't return anything, they always succeed and return "void".
884// A macro wrapper is provided, which makes this statement work:
885// s = zbc_func(...)
886// and makes it visible to the compiler that s is always zero,
887// allowing compiler to optimize dead code after the statement.
888//
889// To make code more readable, each such function has a "z"
890// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
891//
892#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100893# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100894# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100895# define IF_ERROR_RETURN_POSSIBLE(a) a
896# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100897# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100898# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100899#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100900# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100902# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
903# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100904# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100905# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100906#endif
907
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100908//
909// Utility routines
910//
Gavin Howard01055ba2018-11-03 11:00:21 -0600911
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100912static void fflush_and_check(void)
913{
914 fflush_all();
915 if (ferror(stdout) || ferror(stderr))
916 bb_perror_msg_and_die("output error");
917}
918
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100919#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100920#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100921do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100922 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100923 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100924 return BC_STATUS_FAILURE; \
925} while (0)
926#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100927#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100928#endif
929
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100930static void quit(void) NORETURN;
931static void quit(void)
932{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100933 if (ferror(stdin))
934 bb_perror_msg_and_die("input error");
935 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100936 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100937 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100938}
939
Denys Vlasenko5318f812018-12-05 17:48:01 +0100940static void bc_verror_msg(const char *fmt, va_list p)
941{
Denys Vlasenko82269122018-12-14 16:30:56 +0100942 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100943 if (G.prog.file) {
944 sv = applet_name;
945 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
946 }
947 bb_verror_msg(fmt, p, NULL);
948 if (G.prog.file) {
949 free((char*)applet_name);
950 applet_name = sv;
951 }
952}
953
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100954static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100955{
956 va_list p;
957
958 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100959 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100960 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100961
Denys Vlasenkoec603182018-12-17 10:34:02 +0100962 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
963 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
964 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100965}
966
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100967#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100968static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100969{
970 va_list p;
971
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100972 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100973 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100974 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100975
976 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100977 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100978 va_end(p);
979
980 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100981 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100982 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenkoec603182018-12-17 10:34:02 +0100983 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
984 return BC_STATUS_FAILURE;
985 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100986}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100987#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100988
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100989// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
990// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100991// function must not have caller-cleaned parameters on stack.
992// Unfortunately, vararg function API does exactly that on most arches.
993// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100994static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100995{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100996 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100997}
998static ERRORFUNC int bc_error_bad_character(char c)
999{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001000 if (!c)
1001 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001002 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001003}
1004static ERRORFUNC int bc_error_bad_expression(void)
1005{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001006 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001007}
1008static ERRORFUNC int bc_error_bad_token(void)
1009{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001010 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001011}
1012static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1013{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001014 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001015}
1016static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1017{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001018 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001019}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001020#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001021static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001022{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001023 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001024}
Denys Vlasenko00646792018-12-05 18:12:27 +01001025static int bc_POSIX_does_not_allow(const char *msg)
1026{
1027 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1028}
1029static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1030{
1031 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1032}
1033static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1034{
1035 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1036}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001037#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001038
Gavin Howard01055ba2018-11-03 11:00:21 -06001039static void bc_vec_grow(BcVec *v, size_t n)
1040{
1041 size_t cap = v->cap * 2;
1042 while (cap < v->len + n) cap *= 2;
1043 v->v = xrealloc(v->v, v->size * cap);
1044 v->cap = cap;
1045}
1046
1047static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1048{
1049 v->size = esize;
1050 v->cap = BC_VEC_START_CAP;
1051 v->len = 0;
1052 v->dtor = dtor;
1053 v->v = xmalloc(esize * BC_VEC_START_CAP);
1054}
1055
Denys Vlasenko7d628012018-12-04 21:46:47 +01001056static void bc_char_vec_init(BcVec *v)
1057{
1058 bc_vec_init(v, sizeof(char), NULL);
1059}
1060
Gavin Howard01055ba2018-11-03 11:00:21 -06001061static void bc_vec_expand(BcVec *v, size_t req)
1062{
1063 if (v->cap < req) {
1064 v->v = xrealloc(v->v, v->size * req);
1065 v->cap = req;
1066 }
1067}
1068
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001069static void bc_vec_pop(BcVec *v)
1070{
1071 v->len--;
1072 if (v->dtor)
1073 v->dtor(v->v + (v->size * v->len));
1074}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001075
Gavin Howard01055ba2018-11-03 11:00:21 -06001076static void bc_vec_npop(BcVec *v, size_t n)
1077{
1078 if (!v->dtor)
1079 v->len -= n;
1080 else {
1081 size_t len = v->len - n;
1082 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1083 }
1084}
1085
Denys Vlasenko7d628012018-12-04 21:46:47 +01001086static void bc_vec_pop_all(BcVec *v)
1087{
1088 bc_vec_npop(v, v->len);
1089}
1090
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001091static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001092{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001093 size_t len = v->len;
1094 if (len >= v->cap) bc_vec_grow(v, 1);
1095 memmove(v->v + (v->size * len), data, v->size);
1096 v->len++;
1097 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001098}
1099
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001100// G.prog.results often needs "pop old operand, push result" idiom.
1101// Can do this without a few extra ops
1102static size_t bc_result_pop_and_push(const void *data)
1103{
1104 BcVec *v = &G.prog.results;
1105 char *last;
1106 size_t len = v->len - 1;
1107
1108 last = v->v + (v->size * len);
1109 if (v->dtor)
1110 v->dtor(last);
1111 memmove(last, data, v->size);
1112 return len;
1113}
1114
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001115static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001116{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001117 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001118}
1119
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001121{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001122 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001123 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001124 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001125}
1126
Gavin Howard01055ba2018-11-03 11:00:21 -06001127static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1128{
1129 if (idx == v->len)
1130 bc_vec_push(v, data);
1131 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001132 char *ptr;
1133
1134 if (v->len == v->cap) bc_vec_grow(v, 1);
1135
1136 ptr = v->v + v->size * idx;
1137
1138 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1139 memmove(ptr, data, v->size);
1140 }
1141}
1142
1143static void bc_vec_string(BcVec *v, size_t len, const char *str)
1144{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001145 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001146 bc_vec_expand(v, len + 1);
1147 memcpy(v->v, str, len);
1148 v->len = len;
1149
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001150 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001151}
1152
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001153#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001154static void bc_vec_concat(BcVec *v, const char *str)
1155{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001156 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001157
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001158 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001159
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001160 slen = strlen(str);
1161 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001162
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001163 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001164 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001165
1166 v->len = len;
1167}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001168#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001169
1170static void *bc_vec_item(const BcVec *v, size_t idx)
1171{
1172 return v->v + v->size * idx;
1173}
1174
1175static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1176{
1177 return v->v + v->size * (v->len - idx - 1);
1178}
1179
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001180static void *bc_vec_top(const BcVec *v)
1181{
1182 return v->v + v->size * (v->len - 1);
1183}
1184
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001185static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001186{
1187 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001188 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001189 free(v->v);
1190}
1191
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001192static BcFunc* bc_program_func(size_t idx)
1193{
1194 return bc_vec_item(&G.prog.fns, idx);
1195}
1196// BC_PROG_MAIN is zeroth element, so:
1197#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1198
1199#if ENABLE_BC
1200static BcFunc* bc_program_current_func(void)
1201{
1202 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1203 BcFunc *func = bc_program_func(ip->func);
1204 return func;
1205}
1206#endif
1207
1208static char** bc_program_str(size_t idx)
1209{
1210#if ENABLE_BC
1211 if (IS_BC) {
1212 BcFunc *func = bc_program_current_func();
1213 return bc_vec_item(&func->strs, idx);
1214 }
1215#endif
1216 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1217}
1218
1219static char** bc_program_const(size_t idx)
1220{
1221#if ENABLE_BC
1222 if (IS_BC) {
1223 BcFunc *func = bc_program_current_func();
1224 return bc_vec_item(&func->consts, idx);
1225 }
1226#endif
1227 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1228}
1229
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001230static int bc_id_cmp(const void *e1, const void *e2)
1231{
1232 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1233}
1234
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001235static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001236{
1237 free(((BcId *) id)->name);
1238}
1239
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001240static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001241{
1242 size_t low = 0, high = v->len;
1243
1244 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001245 size_t mid = (low + high) / 2;
1246 BcId *id = bc_vec_item(v, mid);
1247 int result = bc_id_cmp(ptr, id);
1248
1249 if (result == 0)
1250 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001251 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001252 high = mid;
1253 else
1254 low = mid + 1;
1255 }
1256
1257 return low;
1258}
1259
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001260static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001261{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001262 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001263
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001264 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001265 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001266 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1267 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001268 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 bc_vec_pushAt(v, ptr, n);
1270 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001271}
1272
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001273#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001274static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001275{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001276 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001277 if (i >= v->len) return BC_VEC_INVALID_IDX;
1278 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1279}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001280#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001281
Gavin Howard01055ba2018-11-03 11:00:21 -06001282static void bc_num_setToZero(BcNum *n, size_t scale)
1283{
1284 n->len = 0;
1285 n->neg = false;
1286 n->rdx = scale;
1287}
1288
1289static void bc_num_zero(BcNum *n)
1290{
1291 bc_num_setToZero(n, 0);
1292}
1293
1294static void bc_num_one(BcNum *n)
1295{
1296 bc_num_setToZero(n, 0);
1297 n->len = 1;
1298 n->num[0] = 1;
1299}
1300
Denys Vlasenko3129f702018-12-09 12:04:44 +01001301// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001302static void bc_num_init(BcNum *n, size_t req)
1303{
1304 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001305 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001306 n->num = xmalloc(req);
1307 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001308 n->rdx = 0;
1309 n->len = 0;
1310 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001311}
1312
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001313static void bc_num_init_DEF_SIZE(BcNum *n)
1314{
1315 bc_num_init(n, BC_NUM_DEF_SIZE);
1316}
1317
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001318static void bc_num_expand(BcNum *n, size_t req)
1319{
1320 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1321 if (req > n->cap) {
1322 n->num = xrealloc(n->num, req);
1323 n->cap = req;
1324 }
1325}
1326
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001327static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001328{
1329 free(((BcNum *) num)->num);
1330}
1331
1332static void bc_num_copy(BcNum *d, BcNum *s)
1333{
1334 if (d != s) {
1335 bc_num_expand(d, s->cap);
1336 d->len = s->len;
1337 d->neg = s->neg;
1338 d->rdx = s->rdx;
1339 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1340 }
1341}
1342
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001343static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001344{
1345 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001346 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347
Denys Vlasenko1557b762018-12-22 21:37:46 +01001348 result = 0;
1349 i = n->len;
1350 while (i > n->rdx) {
1351 unsigned long prev = result;
1352 result = result * 10 + n->num[--i];
1353 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1354 // 0x1ff00000 * 10 is 0x13f600000,
1355 // or 0x3f600000 truncated to 32 bits. Which is larger.
1356 // However, (N*10)/8 < N check is always correct.
1357 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001358 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001359 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001360 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001361
Denys Vlasenko29301232018-12-11 15:29:32 +01001362 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001363}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001364#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1365
1366static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1367{
1368 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1369
1370 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1371}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001372#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001373
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001374#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1375# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1376#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1377# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1378#endif
1379// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1380// would not hit realloc() code path - not good if num[] is not malloced
1381
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001382static void bc_num_ulong2num(BcNum *n, unsigned long val)
1383{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001384 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001385
1386 bc_num_zero(n);
1387
1388 if (val == 0) return;
1389
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001390 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001391
1392 ptr = n->num;
1393 for (;;) {
1394 n->len++;
1395 *ptr++ = val % 10;
1396 val /= 10;
1397 if (val == 0) break;
1398 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001399}
1400
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001401static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001402{
1403 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001404 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001405 a[i] -= b[i];
1406 for (j = i; a[j] < 0;) {
1407 a[j++] += 10;
1408 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001409 }
1410 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001411}
1412
1413static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1414{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001415 size_t i = len;
1416 for (;;) {
1417 int c;
1418 if (i == 0)
1419 return 0;
1420 i--;
1421 c = a[i] - b[i];
1422 if (c != 0) {
1423 i++;
1424 if (c < 0)
1425 return -i;
1426 return i;
1427 }
1428 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001429}
1430
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001431#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1432#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1433#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1434//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1435static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1436{
1437 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1438}
1439//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1440static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1441{
1442 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1443}
1444
Gavin Howard01055ba2018-11-03 11:00:21 -06001445static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1446{
1447 size_t i, min, a_int, b_int, diff;
1448 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001449 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001450 ssize_t cmp;
1451
1452 if (a == b) return 0;
1453 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1454 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001455
1456 if (a->neg != b->neg) // signs of a and b differ
1457 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1458 return (int)b->neg - (int)a->neg;
1459 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001460
1461 a_int = BC_NUM_INT(a);
1462 b_int = BC_NUM_INT(b);
1463 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001464
1465 if (a_int != 0) return (ssize_t) a_int;
1466
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001467 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001468 if (a_max) {
1469 min = b->rdx;
1470 diff = a->rdx - b->rdx;
1471 max_num = a->num + diff;
1472 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001473 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1474 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001475 min = a->rdx;
1476 diff = b->rdx - a->rdx;
1477 max_num = b->num + diff;
1478 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001479 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001480 }
1481
1482 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001483 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001484
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001485 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001486 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001487 }
1488
1489 return 0;
1490}
1491
1492static void bc_num_truncate(BcNum *n, size_t places)
1493{
1494 if (places == 0) return;
1495
1496 n->rdx -= places;
1497
1498 if (n->len != 0) {
1499 n->len -= places;
1500 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1501 }
1502}
1503
1504static void bc_num_extend(BcNum *n, size_t places)
1505{
1506 size_t len = n->len + places;
1507
1508 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001509 if (n->cap < len) bc_num_expand(n, len);
1510
1511 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1512 memset(n->num, 0, sizeof(BcDig) * places);
1513
1514 n->len += places;
1515 n->rdx += places;
1516 }
1517}
1518
1519static void bc_num_clean(BcNum *n)
1520{
1521 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1522 if (n->len == 0)
1523 n->neg = false;
1524 else if (n->len < n->rdx)
1525 n->len = n->rdx;
1526}
1527
1528static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1529{
1530 if (n->rdx < scale)
1531 bc_num_extend(n, scale - n->rdx);
1532 else
1533 bc_num_truncate(n, n->rdx - scale);
1534
1535 bc_num_clean(n);
1536 if (n->len != 0) n->neg = !neg1 != !neg2;
1537}
1538
1539static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1540 BcNum *restrict b)
1541{
1542 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001543 b->len = n->len - idx;
1544 a->len = idx;
1545 a->rdx = b->rdx = 0;
1546
1547 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1548 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001549 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001550 bc_num_zero(b);
1551 bc_num_copy(a, n);
1552 }
1553
1554 bc_num_clean(a);
1555 bc_num_clean(b);
1556}
1557
Denys Vlasenko29301232018-12-11 15:29:32 +01001558static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001559{
Denys Vlasenko29301232018-12-11 15:29:32 +01001560 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001561
1562 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1563 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1564 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001565 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001566 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001567
1568 if (n->rdx >= places)
1569 n->rdx -= places;
1570 else {
1571 bc_num_extend(n, places - n->rdx);
1572 n->rdx = 0;
1573 }
1574
1575 bc_num_clean(n);
1576
Denys Vlasenko29301232018-12-11 15:29:32 +01001577 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001578}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001579#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001580
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001581typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1582
1583static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1584 BcNumBinaryOp op, size_t req)
1585{
1586 BcStatus s;
1587 BcNum num2, *ptr_a, *ptr_b;
1588 bool init = false;
1589
1590 if (c == a) {
1591 ptr_a = &num2;
1592 memcpy(ptr_a, c, sizeof(BcNum));
1593 init = true;
1594 } else
1595 ptr_a = a;
1596
1597 if (c == b) {
1598 ptr_b = &num2;
1599 if (c != a) {
1600 memcpy(ptr_b, c, sizeof(BcNum));
1601 init = true;
1602 }
1603 } else
1604 ptr_b = b;
1605
1606 if (init)
1607 bc_num_init(c, req);
1608 else
1609 bc_num_expand(c, req);
1610
1611 s = BC_STATUS_SUCCESS;
1612 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1613
1614 if (init) bc_num_free(&num2);
1615
1616 RETURN_STATUS(s);
1617}
1618#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1619
1620static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1621static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1622static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1623static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1624static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1625static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1626
1627static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1628{
1629 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1630 (void) scale;
1631 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1632}
1633
1634static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1635{
1636 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1637 (void) scale;
1638 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1639}
1640
1641static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1642{
1643 size_t req = BC_NUM_MREQ(a, b, scale);
1644 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1645}
1646
1647static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1648{
1649 size_t req = BC_NUM_MREQ(a, b, scale);
1650 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1651}
1652
1653static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1654{
1655 size_t req = BC_NUM_MREQ(a, b, scale);
1656 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1657}
1658
1659static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1660{
1661 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1662}
1663
1664static const BcNumBinaryOp zbc_program_ops[] = {
1665 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1666};
1667#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1668#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1669#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1670#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1671#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1672#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1673
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001674static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001675{
1676 BcNum one;
1677 BcDig num[2];
1678
1679 one.cap = 2;
1680 one.num = num;
1681 bc_num_one(&one);
1682
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001683 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001684}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001685#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001686
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001687static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001688{
1689 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1690 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001691 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001692
1693 // Because this function doesn't need to use scale (per the bc spec),
1694 // I am hijacking it to say whether it's doing an add or a subtract.
1695
1696 if (a->len == 0) {
1697 bc_num_copy(c, b);
1698 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001699 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001700 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001701 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001702 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001703 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001704 }
1705
1706 c->neg = a->neg;
1707 c->rdx = BC_MAX(a->rdx, b->rdx);
1708 min_rdx = BC_MIN(a->rdx, b->rdx);
1709 c->len = 0;
1710
1711 if (a->rdx > b->rdx) {
1712 diff = a->rdx - b->rdx;
1713 ptr = a->num;
1714 ptr_a = a->num + diff;
1715 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001716 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001717 diff = b->rdx - a->rdx;
1718 ptr = b->num;
1719 ptr_a = a->num;
1720 ptr_b = b->num + diff;
1721 }
1722
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001723 ptr_c = c->num;
1724 for (i = 0; i < diff; ++i, ++c->len)
1725 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001726
1727 ptr_c += diff;
1728 a_int = BC_NUM_INT(a);
1729 b_int = BC_NUM_INT(b);
1730
1731 if (a_int > b_int) {
1732 min_int = b_int;
1733 max = a_int;
1734 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001735 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001736 min_int = a_int;
1737 max = b_int;
1738 ptr = ptr_b;
1739 }
1740
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001741 carry = 0;
1742 for (i = 0; i < min_rdx + min_int; ++i) {
1743 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001744 carry = in / 10;
1745 ptr_c[i] = (BcDig)(in % 10);
1746 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001747 for (; i < max + min_rdx; ++i) {
1748 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001749 carry = in / 10;
1750 ptr_c[i] = (BcDig)(in % 10);
1751 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001752 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001753
1754 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1755
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001756 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001757}
1758
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001759static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001760{
Gavin Howard01055ba2018-11-03 11:00:21 -06001761 ssize_t cmp;
1762 BcNum *minuend, *subtrahend;
1763 size_t start;
1764 bool aneg, bneg, neg;
1765
1766 // Because this function doesn't need to use scale (per the bc spec),
1767 // I am hijacking it to say whether it's doing an add or a subtract.
1768
1769 if (a->len == 0) {
1770 bc_num_copy(c, b);
1771 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001772 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001773 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001774 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001775 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001776 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001777 }
1778
1779 aneg = a->neg;
1780 bneg = b->neg;
1781 a->neg = b->neg = false;
1782
1783 cmp = bc_num_cmp(a, b);
1784
1785 a->neg = aneg;
1786 b->neg = bneg;
1787
1788 if (cmp == 0) {
1789 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001790 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001791 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001792 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001793 neg = a->neg;
1794 minuend = a;
1795 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001796 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 neg = b->neg;
1798 if (sub) neg = !neg;
1799 minuend = b;
1800 subtrahend = a;
1801 }
1802
1803 bc_num_copy(c, minuend);
1804 c->neg = neg;
1805
1806 if (c->rdx < subtrahend->rdx) {
1807 bc_num_extend(c, subtrahend->rdx - c->rdx);
1808 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001809 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001810 start = c->rdx - subtrahend->rdx;
1811
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001812 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001813
1814 bc_num_clean(c);
1815
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001816 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001817}
1818
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001819static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001820 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001821#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001822{
1823 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001824 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001825 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001826 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001827
Gavin Howard01055ba2018-11-03 11:00:21 -06001828 if (a->len == 0 || b->len == 0) {
1829 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001830 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001832 aone = BC_NUM_ONE(a);
1833 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001835 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001836 }
1837
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001838 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1839 || a->len < BC_NUM_KARATSUBA_LEN
1840 || b->len < BC_NUM_KARATSUBA_LEN
1841 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001842 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001843
Gavin Howard01055ba2018-11-03 11:00:21 -06001844 bc_num_expand(c, a->len + b->len + 1);
1845
1846 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001847 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001848
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001849 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001850 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001851 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001852 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001853 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001854 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001855 carry = in / 10;
1856 c->num[i + j] = (BcDig)(in % 10);
1857 }
1858
1859 c->num[i + j] += (BcDig) carry;
1860 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001861
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001862#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001863 // a=2^1000000
1864 // a*a <- without check below, this will not be interruptible
1865 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001866#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 }
1868
1869 c->len = len;
1870
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001871 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001872 }
1873
1874 bc_num_init(&l1, max);
1875 bc_num_init(&h1, max);
1876 bc_num_init(&l2, max);
1877 bc_num_init(&h2, max);
1878 bc_num_init(&m1, max);
1879 bc_num_init(&m2, max);
1880 bc_num_init(&z0, max);
1881 bc_num_init(&z1, max);
1882 bc_num_init(&z2, max);
1883 bc_num_init(&temp, max + max);
1884
1885 bc_num_split(a, max2, &l1, &h1);
1886 bc_num_split(b, max2, &l2, &h2);
1887
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001888 s = zbc_num_add(&h1, &l1, &m1, 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(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 if (s) goto err;
1892
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001893 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001894 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001895 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001896 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001897 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 if (s) goto err;
1899
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001900 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001901 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001902 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001903 if (s) goto err;
1904
Denys Vlasenko29301232018-12-11 15:29:32 +01001905 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001906 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001907 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001908 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001909 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001910 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001911 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001912 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001913 bc_num_free(&temp);
1914 bc_num_free(&z2);
1915 bc_num_free(&z1);
1916 bc_num_free(&z0);
1917 bc_num_free(&m2);
1918 bc_num_free(&m1);
1919 bc_num_free(&h2);
1920 bc_num_free(&l2);
1921 bc_num_free(&h1);
1922 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001923 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001924}
1925
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001926static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001927{
1928 BcStatus s;
1929 BcNum cpa, cpb;
1930 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1931
1932 scale = BC_MAX(scale, a->rdx);
1933 scale = BC_MAX(scale, b->rdx);
1934 scale = BC_MIN(a->rdx + b->rdx, scale);
1935 maxrdx = BC_MAX(maxrdx, scale);
1936
1937 bc_num_init(&cpa, a->len);
1938 bc_num_init(&cpb, b->len);
1939
1940 bc_num_copy(&cpa, a);
1941 bc_num_copy(&cpb, b);
1942 cpa.neg = cpb.neg = false;
1943
Denys Vlasenko29301232018-12-11 15:29:32 +01001944 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001945 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001946 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001947 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001948 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 if (s) goto err;
1950
1951 maxrdx += scale;
1952 bc_num_expand(c, c->len + maxrdx);
1953
1954 if (c->len < maxrdx) {
1955 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1956 c->len += maxrdx;
1957 }
1958
1959 c->rdx = maxrdx;
1960 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001961 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001962 bc_num_free(&cpb);
1963 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001964 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001965}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001966#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001967
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001968static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001969{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001970 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001971 size_t len, end, i;
1972 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001973
1974 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001975 RETURN_STATUS(bc_error("divide by zero"));
1976 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001977 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001978 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001979 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001980 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001981 bc_num_copy(c, a);
1982 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001983 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001984 }
1985
1986 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1987 bc_num_copy(&cp, a);
1988 len = b->len;
1989
1990 if (len > cp.len) {
1991 bc_num_expand(&cp, len + 2);
1992 bc_num_extend(&cp, len - cp.len);
1993 }
1994
1995 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1996 cp.rdx -= b->rdx;
1997 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1998
1999 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002000 for (;;) {
2001 if (len == 0) break;
2002 len--;
2003 if (b->num[len] != 0)
2004 break;
2005 }
2006 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002007 }
2008
2009 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2010
2011 // We want an extra zero in front to make things simpler.
2012 cp.num[cp.len++] = 0;
2013 end = cp.len - len;
2014
2015 bc_num_expand(c, cp.len);
2016
2017 bc_num_zero(c);
2018 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2019 c->rdx = cp.rdx;
2020 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002021
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002022 s = BC_STATUS_SUCCESS;
2023 for (i = end - 1; i < end; --i) {
2024 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002025 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002026 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2027 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002028 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002029#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002030 // a=2^100000
2031 // scale=40000
2032 // 1/a <- without check below, this will not be interruptible
2033 if (G_interrupt) {
2034 s = BC_STATUS_FAILURE;
2035 break;
2036 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002037#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002038 }
2039
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002040 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002041 bc_num_free(&cp);
2042
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002043 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002044}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002045#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002046
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002047static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002048 BcNum *restrict d, size_t scale, size_t ts)
2049{
2050 BcStatus s;
2051 BcNum temp;
2052 bool neg;
2053
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002054 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002055 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002056
2057 if (a->len == 0) {
2058 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002059 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002060 }
2061
2062 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002063 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002064 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002065
2066 if (scale != 0) scale = ts;
2067
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002068 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002069 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002070 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002071 if (s) goto err;
2072
2073 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2074
2075 neg = d->neg;
2076 bc_num_retireMul(d, ts, a->neg, b->neg);
2077 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002078 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002079 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002080 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002081}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002082#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002083
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002084static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002085{
2086 BcStatus s;
2087 BcNum c1;
2088 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2089
2090 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002091 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002092 bc_num_free(&c1);
2093
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002094 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002095}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002096#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002097
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002098static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002099{
2100 BcStatus s = BC_STATUS_SUCCESS;
2101 BcNum copy;
2102 unsigned long pow;
2103 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002104 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002105
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002106 // GNU bc does not allow 2^2.0 - we do
2107 for (i = 0; i < b->rdx; i++)
2108 if (b->num[i] != 0)
2109 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002110
2111 if (b->len == 0) {
2112 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002113 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002114 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002115 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002116 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002117 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002118 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002119 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 if (!b->neg)
2121 bc_num_copy(c, a);
2122 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002123 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002124 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002125 }
2126
2127 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002128 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002129 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002130 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002131
2132 bc_num_init(&copy, a->len);
2133 bc_num_copy(&copy, a);
2134
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002135 if (!neg) {
2136 if (a->rdx > scale)
2137 scale = a->rdx;
2138 if (a->rdx * pow < scale)
2139 scale = a->rdx * pow;
2140 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002141
Gavin Howard01055ba2018-11-03 11:00:21 -06002142
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002143 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002144 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002145 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002147 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002148 //if (G_interrupt) {
2149 // s = BC_STATUS_FAILURE;
2150 // goto err;
2151 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 }
2153
Gavin Howard01055ba2018-11-03 11:00:21 -06002154 bc_num_copy(c, &copy);
2155
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002156 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002157 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002158 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 if (s) goto err;
2160
2161 if (pow & 1) {
2162 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002163 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002164 if (s) goto err;
2165 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002166 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002167 //if (G_interrupt) {
2168 // s = BC_STATUS_FAILURE;
2169 // goto err;
2170 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002171 }
2172
2173 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002174 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002175 if (s) goto err;
2176 }
2177
Gavin Howard01055ba2018-11-03 11:00:21 -06002178 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2179
2180 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002181 for (i = 0; i < c->len; ++i)
2182 if (c->num[i] != 0)
2183 goto skip;
2184 bc_num_setToZero(c, scale);
2185 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002186
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002187 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002188 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002189 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002190}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002191#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002192
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002193static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002194{
2195 BcStatus s;
2196 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2197 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2198 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2199
2200 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2201 bc_num_expand(b, req);
2202
2203 if (a->len == 0) {
2204 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002205 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002206 }
2207 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002208 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002209 }
2210 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002211 bc_num_one(b);
2212 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002213 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002214 }
2215
2216 scale = BC_MAX(scale, a->rdx) + 1;
2217 len = a->len + scale;
2218
2219 bc_num_init(&num1, len);
2220 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002221 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002222
2223 bc_num_one(&half);
2224 half.num[0] = 5;
2225 half.rdx = 1;
2226
2227 bc_num_init(&f, len);
2228 bc_num_init(&fprime, len);
2229
2230 x0 = &num1;
2231 x1 = &num2;
2232
2233 bc_num_one(x0);
2234 pow = BC_NUM_INT(a);
2235
2236 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002237 if (pow & 1)
2238 x0->num[0] = 2;
2239 else
2240 x0->num[0] = 6;
2241
2242 pow -= 2 - (pow & 1);
2243
2244 bc_num_extend(x0, pow);
2245
2246 // Make sure to move the radix back.
2247 x0->rdx -= pow;
2248 }
2249
2250 x0->rdx = digs = digs1 = 0;
2251 resrdx = scale + 2;
2252 len = BC_NUM_INT(x0) + resrdx - 1;
2253
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002254 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002255 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002256 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002257 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002258 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002259 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002260 if (s) goto err;
2261
2262 cmp = bc_num_cmp(x1, x0);
2263 digs = x1->len - (unsigned long long) llabs(cmp);
2264
2265 if (cmp == cmp2 && digs == digs1)
2266 times += 1;
2267 else
2268 times = 0;
2269
2270 resrdx += times > 4;
2271
2272 cmp2 = cmp1;
2273 cmp1 = cmp;
2274 digs1 = digs;
2275
2276 temp = x0;
2277 x0 = x1;
2278 x1 = temp;
2279 }
2280
Gavin Howard01055ba2018-11-03 11:00:21 -06002281 bc_num_copy(b, x0);
2282 scale -= 1;
2283 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002284 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002285 bc_num_free(&fprime);
2286 bc_num_free(&f);
2287 bc_num_free(&half);
2288 bc_num_free(&num2);
2289 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002290 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002291}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002292#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002293
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002294static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002295 size_t scale)
2296{
2297 BcStatus s;
2298 BcNum num2, *ptr_a;
2299 bool init = false;
2300 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2301
2302 if (c == a) {
2303 memcpy(&num2, c, sizeof(BcNum));
2304 ptr_a = &num2;
2305 bc_num_init(c, len);
2306 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002307 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002308 ptr_a = a;
2309 bc_num_expand(c, len);
2310 }
2311
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002312 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002313
2314 if (init) bc_num_free(&num2);
2315
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002316 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002317}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002318#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002319
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002320#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002321static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002322{
2323 BcStatus s;
2324 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002325 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002326
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002327 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002328 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002329 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002330 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002331 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002332 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002333
2334 bc_num_expand(d, c->len);
2335 bc_num_init(&base, c->len);
2336 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002337 bc_num_init(&temp, b->len);
2338
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002339 two.cap = ARRAY_SIZE(two_digs);
2340 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002341 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002342 two_digs[0] = 2;
2343
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 bc_num_one(d);
2345
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002346 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002347 if (s) goto err;
2348 bc_num_copy(&exp, b);
2349
2350 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002351 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002352 if (s) goto err;
2353
2354 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002355 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002356 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002357 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002358 if (s) goto err;
2359 }
2360
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002361 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002362 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002363 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002364 if (s) goto err;
2365 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002366 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002367 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002368 bc_num_free(&exp);
2369 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002370 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002371}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002372#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002373#endif // ENABLE_DC
2374
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002375static FAST_FUNC void bc_string_free(void *string)
2376{
2377 free(*(char**)string);
2378}
2379
Gavin Howard01055ba2018-11-03 11:00:21 -06002380static void bc_func_init(BcFunc *f)
2381{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002382 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002383 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2384 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002385 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2386 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002387 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002388}
2389
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002390static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002391{
2392 BcFunc *f = (BcFunc *) func;
2393 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002394 IF_BC(bc_vec_free(&f->labels);)
2395 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002396 IF_BC(bc_vec_free(&f->strs);)
2397 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002398}
2399
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002400static void bc_array_expand(BcVec *a, size_t len);
2401
Gavin Howard01055ba2018-11-03 11:00:21 -06002402static void bc_array_init(BcVec *a, bool nums)
2403{
2404 if (nums)
2405 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2406 else
2407 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2408 bc_array_expand(a, 1);
2409}
2410
Gavin Howard01055ba2018-11-03 11:00:21 -06002411static void bc_array_expand(BcVec *a, size_t len)
2412{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002413 if (a->dtor == bc_num_free
2414 // && a->size == sizeof(BcNum) - always true
2415 ) {
2416 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002417 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002418 bc_num_init_DEF_SIZE(&n);
2419 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002421 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002422 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002423 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002424 bc_array_init(&v, true);
2425 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002426 }
2427 }
2428}
2429
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002430static void bc_array_copy(BcVec *d, const BcVec *s)
2431{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002432 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002433 size_t i;
2434
2435 bc_vec_pop_all(d);
2436 bc_vec_expand(d, s->cap);
2437 d->len = s->len;
2438
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002439 dnum = (void*)d->v;
2440 snum = (void*)s->v;
2441 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002442 bc_num_init(dnum, snum->len);
2443 bc_num_copy(dnum, snum);
2444 }
2445}
2446
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002447#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002448static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002449{
2450 d->t = src->t;
2451
2452 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002453 case BC_RESULT_TEMP:
2454 case BC_RESULT_IBASE:
2455 case BC_RESULT_SCALE:
2456 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002457 bc_num_init(&d->d.n, src->d.n.len);
2458 bc_num_copy(&d->d.n, &src->d.n);
2459 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002460 case BC_RESULT_VAR:
2461 case BC_RESULT_ARRAY:
2462 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002463 d->d.id.name = xstrdup(src->d.id.name);
2464 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002465 case BC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002466 IF_BC(case BC_RESULT_LAST:)
Gavin Howard01055ba2018-11-03 11:00:21 -06002467 case BC_RESULT_ONE:
2468 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002469 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2470 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002471 }
2472}
2473#endif // ENABLE_DC
2474
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002475static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002476{
2477 BcResult *r = (BcResult *) result;
2478
2479 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002480 case BC_RESULT_TEMP:
2481 case BC_RESULT_IBASE:
2482 case BC_RESULT_SCALE:
2483 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002484 bc_num_free(&r->d.n);
2485 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002486 case BC_RESULT_VAR:
2487 case BC_RESULT_ARRAY:
2488 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002489 free(r->d.id.name);
2490 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002491 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002492 // Do nothing.
2493 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002494 }
2495}
2496
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002497static int bad_input_byte(char c)
2498{
2499 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2500 || c > 0x7e
2501 ) {
2502 bc_error_fmt("illegal character 0x%02x", c);
2503 return 1;
2504 }
2505 return 0;
2506}
2507
2508// Note: it _appends_ data from fp to vec.
2509static void bc_read_line(BcVec *vec, FILE *fp)
2510{
2511 again:
2512 fflush_and_check();
2513
2514#if ENABLE_FEATURE_BC_SIGNALS
2515 if (G_interrupt) { // ^C was pressed
2516 intr:
2517 if (fp != stdin) {
2518 // ^C while running a script (bc SCRIPT): die.
2519 // We do not return to interactive prompt:
2520 // user might be running us from a shell,
2521 // and SCRIPT might be intended to terminate
2522 // (e.g. contain a "halt" stmt).
2523 // ^C dropping user into a bc prompt instead of
2524 // the shell would be unexpected.
2525 xfunc_die();
2526 }
2527 // ^C while interactive input
2528 G_interrupt = 0;
2529 // GNU bc says "interrupted execution."
2530 // GNU dc says "Interrupt!"
2531 fputs("\ninterrupted execution\n", stderr);
2532 }
2533
2534# if ENABLE_FEATURE_EDITING
2535 if (G_ttyin && fp == stdin) {
2536 int n, i;
2537# define line_buf bb_common_bufsiz1
2538 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2539 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2540 if (n == 0) // ^C
2541 goto intr;
2542 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2543 return;
2544 }
2545 i = 0;
2546 for (;;) {
2547 char c = line_buf[i++];
2548 if (!c) break;
2549 if (bad_input_byte(c)) goto again;
2550 }
2551 bc_vec_concat(vec, line_buf);
2552# undef line_buf
2553 } else
2554# endif
2555#endif
2556 {
2557 int c;
2558 bool bad_chars = 0;
2559 size_t len = vec->len;
2560
2561 do {
2562#if ENABLE_FEATURE_BC_SIGNALS
2563 if (G_interrupt) {
2564 // ^C was pressed: ignore entire line, get another one
2565 vec->len = len;
2566 goto intr;
2567 }
2568#endif
2569 do c = fgetc(fp); while (c == '\0');
2570 if (c == EOF) {
2571 if (ferror(fp))
2572 bb_perror_msg_and_die("input error");
2573 // Note: EOF does not append '\n'
2574 break;
2575 }
2576 bad_chars |= bad_input_byte(c);
2577 bc_vec_pushByte(vec, (char)c);
2578 } while (c != '\n');
2579
2580 if (bad_chars) {
2581 // Bad chars on this line
2582 if (!G.prog.file) { // stdin
2583 // ignore entire line, get another one
2584 vec->len = len;
2585 goto again;
2586 }
2587 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
2588 }
2589 bc_vec_pushZeroByte(vec);
2590 }
2591}
2592
2593//
2594// Parsing routines
2595//
2596
2597static bool bc_num_strValid(const char *val, size_t base)
2598{
2599 BcDig b;
2600 bool radix;
2601
2602 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2603 radix = false;
2604 for (;;) {
2605 BcDig c = *val++;
2606 if (c == '\0')
2607 break;
2608 if (c == '.') {
2609 if (radix) return false;
2610 radix = true;
2611 continue;
2612 }
2613 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2614 return false;
2615 }
2616 return true;
2617}
2618
2619// Note: n is already "bc_num_zero()"ed,
2620// leading zeroes in "val" are removed
2621static void bc_num_parseDecimal(BcNum *n, const char *val)
2622{
2623 size_t len, i;
2624 const char *ptr;
2625
2626 len = strlen(val);
2627 if (len == 0)
2628 return;
2629
2630 bc_num_expand(n, len);
2631
2632 ptr = strchr(val, '.');
2633
2634 n->rdx = 0;
2635 if (ptr != NULL)
2636 n->rdx = (size_t)((val + len) - (ptr + 1));
2637
2638 for (i = 0; val[i]; ++i) {
2639 if (val[i] != '0' && val[i] != '.') {
2640 // Not entirely zero value - convert it, and exit
2641 i = len - 1;
2642 for (;;) {
2643 n->num[n->len] = val[i] - '0';
2644 ++n->len;
2645 skip_dot:
2646 if (i == 0) break;
2647 if (val[--i] == '.') goto skip_dot;
2648 }
2649 break;
2650 }
2651 }
2652 // if for() exits without hitting if(), the value is entirely zero
2653}
2654
2655// Note: n is already "bc_num_zero()"ed,
2656// leading zeroes in "val" are removed
2657static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2658{
2659 BcStatus s;
2660 BcNum temp, mult, result;
2661 BcNum base;
2662 BcDig base_digs[ULONG_NUM_BUFSIZE];
2663 BcDig c = '\0';
2664 unsigned long v;
2665 size_t i, digits;
2666
2667 for (i = 0; ; ++i) {
2668 if (val[i] == '\0')
2669 return;
2670 if (val[i] != '.' && val[i] != '0')
2671 break;
2672 }
2673
2674 bc_num_init_DEF_SIZE(&temp);
2675 bc_num_init_DEF_SIZE(&mult);
2676 base.cap = ARRAY_SIZE(base_digs);
2677 base.num = base_digs;
2678 bc_num_ulong2num(&base, base_t);
2679
2680 for (;;) {
2681 c = *val++;
2682 if (c == '\0') goto int_err;
2683 if (c == '.') break;
2684
2685 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2686
2687 s = zbc_num_mul(n, &base, &mult, 0);
2688 if (s) goto int_err;
2689 bc_num_ulong2num(&temp, v);
2690 s = zbc_num_add(&mult, &temp, n, 0);
2691 if (s) goto int_err;
2692 }
2693
2694 bc_num_init(&result, base.len);
2695 //bc_num_zero(&result); - already is
2696 bc_num_one(&mult);
2697
2698 digits = 0;
2699 for (;;) {
2700 c = *val++;
2701 if (c == '\0') break;
2702 digits++;
2703
2704 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2705
2706 s = zbc_num_mul(&result, &base, &result, 0);
2707 if (s) goto err;
2708 bc_num_ulong2num(&temp, v);
2709 s = zbc_num_add(&result, &temp, &result, 0);
2710 if (s) goto err;
2711 s = zbc_num_mul(&mult, &base, &mult, 0);
2712 if (s) goto err;
2713 }
2714
2715 s = zbc_num_div(&result, &mult, &result, digits);
2716 if (s) goto err;
2717 s = zbc_num_add(n, &result, n, digits);
2718 if (s) goto err;
2719
2720 if (n->len != 0) {
2721 if (n->rdx < digits)
2722 bc_num_extend(n, digits - n->rdx);
2723 } else
2724 bc_num_zero(n);
2725 err:
2726 bc_num_free(&result);
2727 int_err:
2728 bc_num_free(&mult);
2729 bc_num_free(&temp);
2730}
2731
2732static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2733{
2734 if (!bc_num_strValid(val, base_t))
2735 RETURN_STATUS(bc_error("bad number string"));
2736
2737 bc_num_zero(n);
2738 while (*val == '0') val++;
2739
2740 if (base_t == 10)
2741 bc_num_parseDecimal(n, val);
2742 else
2743 bc_num_parseBase(n, val, base_t);
2744
2745 RETURN_STATUS(BC_STATUS_SUCCESS);
2746}
2747#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2748
Gavin Howard01055ba2018-11-03 11:00:21 -06002749static void bc_lex_lineComment(BcLex *l)
2750{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002751 // Try: echo -n '#foo' | bc
2752 size_t i;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002753 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002754 i = l->i;
2755 while (i < l->len && l->buf[i] != '\n')
2756 i++;
2757 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002758}
2759
2760static void bc_lex_whitespace(BcLex *l)
2761{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002762 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002763 for (;;) {
2764 char c = l->buf[l->i];
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002765 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002766 break;
2767 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002768 break;
2769 l->i++;
2770 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002771}
2772
Denys Vlasenko29301232018-12-11 15:29:32 +01002773static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002774{
2775 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002776 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002777 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002778
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002779 pt = (start == '.');
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002780 l->t.t = XC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002781 ccnt = i = 0;
2782 for (;;) {
2783 char c = buf[i];
2784 if (c == '\0')
2785 break;
2786 if (c == '\\' && buf[i + 1] == '\n') {
2787 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002788 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002789 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002790 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002791 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2792 if (c != '.') break;
2793 // if '.' was already seen, stop on second one:
2794 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002795 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002796 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002797 // buf[i] is one of "0-9A-F."
2798 i++;
2799 if (c != '.')
2800 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002801 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002802 //ccnt is the number of chars in the number string, excluding possible
2803 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2804 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002805 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002806
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002807 // This might overestimate the size, if there are "\<NL>"'s
2808 // in the number. Subtracting number_of_backslashes*2 correctly
2809 // is not that easy: consider that in the case of "NNN.\<NL>"
2810 // loop above will count "\<NL>" before it realizes it is not
2811 // in fact *inside* the number:
2812 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002813
Denys Vlasenko64074a12018-12-07 15:50:14 +01002814 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2815 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2816 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002817 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002818 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002819
Denys Vlasenko7d628012018-12-04 21:46:47 +01002820 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002821 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002822 bc_vec_push(&l->t.v, &start);
2823
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002824 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002825 // If we have hit a backslash, skip it. We don't have
2826 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002827 if (*buf == '\\') {
2828 buf += 2;
2829 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002830 continue;
2831 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002832 bc_vec_push(&l->t.v, buf);
2833 buf++;
2834 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002835 }
2836
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002837 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002838
Denys Vlasenko29301232018-12-11 15:29:32 +01002839 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002840}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002841#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002842
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002843static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002844{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002845 size_t i;
2846 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002847
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002848 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06002849
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002850 i = 0;
2851 buf = l->buf + l->i - 1;
2852 for (;;) {
2853 char c = buf[i];
2854 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2855 i++;
2856 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002857
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002858#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002859 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2860 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2861 if (i > BC_MAX_STRING)
2862 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2863 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002864#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002865 bc_vec_string(&l->t.v, i, buf);
2866
2867 // Increment the index. We minus 1 because it has already been incremented.
2868 l->i += i - 1;
2869
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002870 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002871}
2872
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002873static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002874{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002875 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002876}
2877
2878static void bc_lex_free(BcLex *l)
2879{
2880 bc_vec_free(&l->t.v);
2881}
2882
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002883static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002884{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002885 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002886 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002887}
2888
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002889static bool bc_lex_more_input(BcLex *l)
2890{
2891 size_t str;
2892 bool comment;
2893
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002894 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002895
2896 // This loop is complex because the vm tries not to send any lines that end
2897 // with a backslash to the parser. The reason for that is because the parser
2898 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2899 // case, and for strings and comments, the parser will expect more stuff.
2900 comment = false;
2901 str = 0;
2902 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002903 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002904 char *string;
2905
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002906 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002907 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002908 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002909 break;
2910
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002911 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002912 while (*string) {
2913 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002914 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002915 if (IS_BC)
2916 str ^= (c == '"');
2917 else {
2918 if (c == ']')
2919 str -= 1;
2920 else if (c == '[')
2921 str += 1;
2922 }
2923 }
2924 string++;
2925 if (c == '/' && *string == '*') {
2926 comment = true;
2927 string++;
2928 continue;
2929 }
2930 if (c == '*' && *string == '/') {
2931 comment = false;
2932 string++;
2933 }
2934 }
2935 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002936 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002937 continue;
2938 }
2939
2940 // Check for backslash+newline.
2941 // we do not check that last char is '\n' -
2942 // if it is not, then it's EOF, and looping back
2943 // to bc_read_line() will detect it:
2944 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002945 if (string >= G.input_buffer.v && *string == '\\') {
2946 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002947 continue;
2948 }
2949
2950 break;
2951 }
2952
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002953 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002954 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002955// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2956 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002957
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002958 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002959}
2960
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002961IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2962IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002963#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2964#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002965
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002966static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002967{
2968 BcStatus s;
2969
2970 l->t.last = l->t.t;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002971 if (l->t.last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002972
2973 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002974 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002975 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002976
2977 // Loop until failure or we don't have whitespace. This
2978 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002979 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002980 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002981 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002982 if (l->i == l->len) {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002983 l->t.t = XC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002984 if (!G.input_fp)
2985 RETURN_STATUS(BC_STATUS_SUCCESS);
2986 if (!bc_lex_more_input(l)) {
2987 G.input_fp = NULL;
2988 RETURN_STATUS(BC_STATUS_SUCCESS);
2989 }
2990 // here it's guaranteed that l->i is below l->len
2991 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002992 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002993 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2994 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002995 if (IS_BC) {
2996 IF_BC(s = zbc_lex_token(l));
2997 } else {
2998 IF_DC(s = zdc_lex_token(l));
2999 }
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003000 } while (!s && l->t.t == XC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01003001 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06003002
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003003 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003004}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003005#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003006
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003007#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01003008static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
3009{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003010 if (l->t.t == XC_LEX_NLINE)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003011 RETURN_STATUS(zbc_lex_next(l));
3012 RETURN_STATUS(BC_STATUS_SUCCESS);
3013}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003014#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003015
3016static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
3017{
3018 BcStatus s;
3019 s = zbc_lex_next(l);
3020 if (s) RETURN_STATUS(s);
3021 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
3022 s = zbc_lex_skip_if_at_NLINE(l);
3023 RETURN_STATUS(s);
3024}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003025#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003026#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003027
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003028static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003029{
3030 l->buf = text;
3031 l->i = 0;
3032 l->len = strlen(text);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003033 l->t.t = l->t.last = XC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003034 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003035}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003036#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003038#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003039static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003040{
3041 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003042 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003043 const char *buf = l->buf + l->i - 1;
3044
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003045 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3046 const char *keyword8 = bc_lex_kws[i].name8;
3047 unsigned j = 0;
3048 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3049 j++;
3050 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003051 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003052 if (keyword8[j] != '\0')
3053 continue;
3054 match:
3055 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003056 if (isalnum(buf[j]) || buf[j]=='_')
3057 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003058 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003059 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003060 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003061 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003062 }
3063
3064 // We minus 1 because the index has already been incremented.
3065 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003066 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003067 }
3068
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003069 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003070 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003071
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003072 if (l->t.v.len > 2) {
3073 // Prevent this:
3074 // >>> qwe=1
3075 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3076 // '
3077 unsigned len = strchrnul(buf, '\n') - buf;
3078 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3079 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003080
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003081 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003082}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003083#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003084
Denys Vlasenko26819db2018-12-12 16:08:46 +01003085static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003086{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003087 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003088
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003089 l->t.t = XC_LEX_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003091 nls = 0;
3092 i = l->i;
3093 for (;;) {
3094 char c = l->buf[i];
3095 if (c == '\0') {
3096 l->i = i;
3097 RETURN_STATUS(bc_error("string end could not be found"));
3098 }
3099 if (c == '"')
3100 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003101 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003102 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003103 }
3104
3105 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003106 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3107 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3108 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003109 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003110 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003111 bc_vec_string(&l->t.v, len, l->buf + l->i);
3112
3113 l->i = i + 1;
3114 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003115 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003116
Denys Vlasenko26819db2018-12-12 16:08:46 +01003117 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003118}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003119#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003120
Denys Vlasenko0a238142018-12-14 16:48:34 +01003121static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003122{
3123 if (l->buf[l->i] == '=') {
3124 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003125 with_and_without >>= 8; // store "with" value
3126 } // else store "without" value
3127 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003128}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003129#define bc_lex_assign(l, with, without) \
3130 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003131
Denys Vlasenko29301232018-12-11 15:29:32 +01003132static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003133{
3134 size_t i, nls = 0;
3135 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003136
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003137 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003138 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003139 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003140 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003141 check_star:
3142 if (c == '*') {
3143 c = buf[++i];
3144 if (c == '/')
3145 break;
3146 goto check_star;
3147 }
3148 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003149 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003150 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003151 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003152 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 }
3154
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003155 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003157 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003158
Denys Vlasenko29301232018-12-11 15:29:32 +01003159 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003160}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003161#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003162
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003163#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003164static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003165{
3166 BcStatus s = BC_STATUS_SUCCESS;
3167 char c = l->buf[l->i++], c2;
3168
3169 // This is the workhorse of the lexer.
3170 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003171// case '\0': // probably never reached
3172// l->i--;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003173// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003174// l->newline = true;
3175// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003176 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003177 l->t.t = XC_LEX_NLINE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003178 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003179 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 case '\t':
3181 case '\v':
3182 case '\f':
3183 case '\r':
3184 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003185 bc_lex_whitespace(l);
3186 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003187 case '!':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003188 bc_lex_assign(l, XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003189 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003190 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003191 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003192 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003193 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003194 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003195 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003198 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003199 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 case '%':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003203 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003204 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 c2 = l->buf[l->i];
3207 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003208 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003209 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 ++l->i;
3211 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003212 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003213 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003214 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003215 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 case '(':
3218 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3220 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 case '*':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003222 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003225 c2 = l->buf[l->i];
3226 if (c2 == '+') {
3227 ++l->i;
3228 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003229 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003230 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 l->t.t = BC_LEX_COMMA;
3234 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003235 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003236 c2 = l->buf[l->i];
3237 if (c2 == '-') {
3238 ++l->i;
3239 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003240 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003241 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003242 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003243 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003244 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003245 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 else {
3247 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003248 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 }
3250 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003251 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 c2 = l->buf[l->i];
3253 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003254 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003256 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003257 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 case '0':
3259 case '1':
3260 case '2':
3261 case '3':
3262 case '4':
3263 case '5':
3264 case '6':
3265 case '7':
3266 case '8':
3267 case '9':
3268 case 'A':
3269 case 'B':
3270 case 'C':
3271 case 'D':
3272 case 'E':
3273 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003274 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003275 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003277 l->t.t = BC_LEX_SCOLON;
3278 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 case '<':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003280 bc_lex_assign(l, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 case '=':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003283 bc_lex_assign(l, XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 case '>':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003286 bc_lex_assign(l, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003287 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 case '[':
3289 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3291 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003292 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003293 if (l->buf[l->i] == '\n') {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003294 l->t.t = XC_LEX_WHITESPACE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003295 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003296 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003297 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003298 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003299 case '^':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003300 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003302 case 'a':
3303 case 'b':
3304 case 'c':
3305 case 'd':
3306 case 'e':
3307 case 'f':
3308 case 'g':
3309 case 'h':
3310 case 'i':
3311 case 'j':
3312 case 'k':
3313 case 'l':
3314 case 'm':
3315 case 'n':
3316 case 'o':
3317 case 'p':
3318 case 'q':
3319 case 'r':
3320 case 's':
3321 case 't':
3322 case 'u':
3323 case 'v':
3324 case 'w':
3325 case 'x':
3326 case 'y':
3327 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003328 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003329 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003330 case '{':
3331 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003332 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3333 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003334 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003335 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003336 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003337 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003338 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003339 ++l->i;
3340 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003341 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003342 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003343 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003344 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003345 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003346 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003347 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003348 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003349 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003350 }
3351
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003352 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003353}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003354#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003355#endif // ENABLE_BC
3356
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003357#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003358static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003359{
Denys Vlasenko81293c82018-12-24 01:53:55 +01003360 if (G_exreg && isspace(l->buf[l->i])) {
3361 bc_lex_whitespace(l); // eats whitespace (but not newline)
3362 l->i++; // bc_lex_name() expects this
Denys Vlasenko29301232018-12-11 15:29:32 +01003363 bc_lex_name(l);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003364 } else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003365 bc_vec_pop_all(&l->t.v);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003366 bc_vec_push(&l->t.v, &l->buf[l->i++]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003367 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003368 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003369 }
3370
Denys Vlasenko29301232018-12-11 15:29:32 +01003371 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003372}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003373#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003374
Denys Vlasenko29301232018-12-11 15:29:32 +01003375static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003376{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003377 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003378
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003379 l->t.t = XC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003380 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003381
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003382 nls = 0;
3383 depth = 1;
3384 i = l->i;
3385 for (;;) {
3386 char c = l->buf[i];
3387 if (c == '\0') {
3388 l->i = i;
3389 RETURN_STATUS(bc_error("string end could not be found"));
3390 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003391 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003392 if (i == l->i || l->buf[i - 1] != '\\') {
3393 if (c == '[') depth++;
3394 if (c == ']')
3395 if (--depth == 0)
3396 break;
3397 }
3398 bc_vec_push(&l->t.v, &l->buf[i]);
3399 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003400 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003401 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003402
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003403 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003404 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3405 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3406 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003407 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003408 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003409
3410 l->i = i;
3411 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003412 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003413
Denys Vlasenko29301232018-12-11 15:29:32 +01003414 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003415}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003416#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003417
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003418#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003419static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003420{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003421 static const //BcLexType - should be this type, but narrower type saves size:
3422 uint8_t
3423 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003424 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 +01003425 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3426 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003427 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003428 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003429
Denys Vlasenko81293c82018-12-24 01:53:55 +01003430 BcStatus s;
3431 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003432 size_t i;
3433
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003434 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3435 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003436 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003437 }
3438
Denys Vlasenko81293c82018-12-24 01:53:55 +01003439 s = BC_STATUS_SUCCESS;
3440 c = l->buf[l->i++];
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003441 if (c >= '%' && c <= '~'
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003442 && (l->t.t = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003443 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003444 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003445 }
3446
3447 // This is the workhorse of the lexer.
3448 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003449// case '\0': // probably never reached
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003450// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003451// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003452 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003453 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003454 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003455 // only to allow interactive dc have a way to exit
3456 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003457 // on <enter>, not on _next_ token (which would mean
3458 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003459 // IOW: typing "1p<enter>" should print "1" _at once_,
3460 // not after some more input.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003461 l->t.t = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003462 l->newline = true;
3463 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003464 case '\t':
3465 case '\v':
3466 case '\f':
3467 case '\r':
3468 case ' ':
Denys Vlasenko81293c82018-12-24 01:53:55 +01003469 l->newline = 0; // was (c == '\n')
Gavin Howard01055ba2018-11-03 11:00:21 -06003470 bc_lex_whitespace(l);
3471 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003472 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003473 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003474 if (c2 == '=')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003475 l->t.t = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003476 else if (c2 == '<')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003477 l->t.t = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003478 else if (c2 == '>')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003479 l->t.t = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003481 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003482 ++l->i;
3483 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003484 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003485 bc_lex_lineComment(l);
3486 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003488 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003489 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003490 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003491 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003492 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003493 case '0':
3494 case '1':
3495 case '2':
3496 case '3':
3497 case '4':
3498 case '5':
3499 case '6':
3500 case '7':
3501 case '8':
3502 case '9':
3503 case 'A':
3504 case 'B':
3505 case 'C':
3506 case 'D':
3507 case 'E':
3508 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003509 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003510 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003511 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003512 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003513 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003515 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003516 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003517 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003518 }
3519
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003520 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003521}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003522#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003523#endif // ENABLE_DC
3524
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003525static void bc_parse_push(BcParse *p, char i)
3526{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003527 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003528 bc_vec_pushByte(&p->func->code, i);
3529}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003530
Gavin Howard01055ba2018-11-03 11:00:21 -06003531static void bc_parse_pushName(BcParse *p, char *name)
3532{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003533 while (*name)
3534 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003535 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003536}
3537
3538static void bc_parse_pushIndex(BcParse *p, size_t idx)
3539{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003540 size_t mask;
3541 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003542
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003543 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003544 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3545 amt = sizeof(idx);
3546 do {
3547 if (idx & mask) break;
3548 mask >>= 8;
3549 amt--;
3550 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003551
3552 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003553
3554 while (idx != 0) {
3555 bc_parse_push(p, (unsigned char)idx);
3556 idx >>= 8;
3557 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003558}
3559
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003560#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003561static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3562{
3563 bc_parse_push(p, BC_INST_JUMP);
3564 bc_parse_pushIndex(p, idx);
3565}
3566
3567static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3568{
3569 bc_parse_push(p, BC_INST_JUMP_ZERO);
3570 bc_parse_pushIndex(p, idx);
3571}
3572
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003573static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003574{
3575 char *str = xstrdup(p->l.t.v.v);
3576
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003577 bc_parse_push(p, XC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003578 bc_parse_pushIndex(p, p->func->strs.len);
3579 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003580
3581 RETURN_STATUS(zbc_lex_next(&p->l));
3582}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003583#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003584#endif
3585
3586static void bc_parse_pushNUM(BcParse *p)
3587{
3588 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003589#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003590 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003591#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003592 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003593#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003594 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003595#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003596 bc_parse_push(p, XC_INST_NUM);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003597 bc_parse_pushIndex(p, idx);
3598}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003599
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003600static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003601{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003602 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003603
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003604 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003605}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003606#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003607
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003608// Called when parsing or execution detects a failure,
3609// resets execution structures.
3610static void bc_program_reset(void)
3611{
3612 BcFunc *f;
3613 BcInstPtr *ip;
3614
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003615 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003616 bc_vec_pop_all(&G.prog.results);
3617
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003618 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003619 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003620 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003621}
3622
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003623// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003624// resets parsing structures.
3625static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003626{
3627 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003628 bc_func_free(p->func);
3629 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003630
Denys Vlasenko65e10462018-12-19 15:13:14 +01003631 p->fidx = BC_PROG_MAIN;
3632 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003633 }
3634
3635 p->l.i = p->l.len;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003636 p->l.t.t = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003637
Denys Vlasenko503faf92018-12-20 16:24:18 +01003638 IF_BC(bc_vec_pop_all(&p->exits);)
3639 IF_BC(bc_vec_pop_all(&p->conds);)
3640 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003641
Denys Vlasenkod38af482018-12-04 19:11:02 +01003642 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003643}
3644
3645static void bc_parse_free(BcParse *p)
3646{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003647 IF_BC(bc_vec_free(&p->exits);)
3648 IF_BC(bc_vec_free(&p->conds);)
3649 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003650 bc_lex_free(&p->l);
3651}
3652
Denys Vlasenkofa210792018-12-19 19:35:40 +01003653static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003654{
3655 memset(p, 0, sizeof(BcParse));
3656
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003657 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003658 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3659 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3660 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003661
Denys Vlasenkofa210792018-12-19 19:35:40 +01003662 p->fidx = fidx;
3663 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003664}
3665
Denys Vlasenko04715442018-12-21 00:10:26 +01003666static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003667{
Denys Vlasenko04715442018-12-21 00:10:26 +01003668 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003669 BcFunc f;
3670 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003671 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003672 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003673 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003674}
3675
3676#if ENABLE_BC
3677
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003678// Note: takes ownership of 'name' (must be malloced)
3679static size_t bc_program_addFunc(char *name)
3680{
3681 size_t idx;
3682 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003683 int inserted;
3684
3685 entry.name = name;
3686 entry.idx = G.prog.fns.len;
3687
3688 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3689 if (!inserted) free(name);
3690
3691 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3692 idx = entry_ptr->idx;
3693
3694 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003695 // There is already a function with this name.
3696 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003697 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003698 bc_func_free(func);
3699 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003700 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003701 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003702 }
3703
3704 return idx;
3705}
3706
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003707#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3708#define BC_PARSE_LEAF(p, rparen) \
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003709 (((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) || (rparen) || \
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003710 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3711
3712// We can calculate the conversion between tokens and exprs by subtracting the
3713// position of the first operator in the lex enum and adding the position of the
3714// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003715#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003716
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003717static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3718
3719static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3720{
3721 BcStatus s;
3722
3723 s = bc_parse_expr_empty_ok(p, flags);
3724 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3725 RETURN_STATUS(bc_error("empty expression"));
3726 RETURN_STATUS(s);
3727}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003728#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003729
3730static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003731#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003732
3733static BC_STATUS zbc_parse_stmt(BcParse *p)
3734{
3735 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3736}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003737#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003738
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003739static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003740{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003741 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3742 // Same for "else", "while()", "for()".
3743 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3744 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003745 if (p->l.t.t == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003746 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003747
3748 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003749}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003750#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003751
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003752static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3753 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003754{
Denys Vlasenko69560f42018-12-24 14:14:23 +01003755 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3756 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003757
3758 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003759 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 if (t == BC_LEX_LPAREN) break;
3761
Denys Vlasenko69560f42018-12-24 14:14:23 +01003762 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 if (l >= r && (l != r || !left)) break;
3764
Denys Vlasenko0154d782018-12-14 23:32:51 +01003765 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003766 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003767 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003768 }
3769
3770 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003771}
3772
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003773static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003774{
3775 BcLexType top;
3776
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003777 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003778 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003779 top = BC_PARSE_TOP_OP(p);
3780
3781 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003782 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003783
3784 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003785 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003786
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003787 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003788 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003789 top = BC_PARSE_TOP_OP(p);
3790 }
3791
3792 bc_vec_pop(&p->ops);
3793
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003794 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003795}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003796#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003797
Denys Vlasenko26819db2018-12-12 16:08:46 +01003798static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003799{
3800 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003801 size_t nparams;
3802
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003803 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003804 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3805
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003806 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003807 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003808
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003809 nparams = 0;
3810 if (p->l.t.t != BC_LEX_RPAREN) {
3811 for (;;) {
3812 s = zbc_parse_expr(p, flags);
3813 if (s) RETURN_STATUS(s);
3814 nparams++;
3815 if (p->l.t.t != BC_LEX_COMMA) {
3816 if (p->l.t.t == BC_LEX_RPAREN)
3817 break;
3818 RETURN_STATUS(bc_error_bad_token());
3819 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003820 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003821 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003822 }
3823 }
3824
Gavin Howard01055ba2018-11-03 11:00:21 -06003825 bc_parse_push(p, BC_INST_CALL);
3826 bc_parse_pushIndex(p, nparams);
3827
Denys Vlasenko26819db2018-12-12 16:08:46 +01003828 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003829}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003830#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003831
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003832// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003833static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003834{
3835 BcStatus s;
3836 BcId entry, *entry_ptr;
3837 size_t idx;
3838
3839 entry.name = name;
3840
Denys Vlasenko26819db2018-12-12 16:08:46 +01003841 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003842 if (s) goto err;
3843
3844 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003845 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003846 goto err;
3847 }
3848
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003849 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003850
3851 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003852 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003853 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003854 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003855 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003856 free(name);
3857
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003858 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003859 bc_parse_pushIndex(p, entry_ptr->idx);
3860
Denys Vlasenko26819db2018-12-12 16:08:46 +01003861 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003862 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003863 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003864 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003865}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003866#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003867
Denys Vlasenko26819db2018-12-12 16:08:46 +01003868static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003869{
3870 BcStatus s;
3871 char *name;
3872
3873 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003874 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003875 if (s) goto err;
3876
3877 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003878 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003879 if (s) goto err;
3880
3881 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003883 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 goto err;
3885 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003886 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003887 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003888 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003889 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003890 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003891 if (s) goto err;
3892 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003893 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 if (s) goto err;
3895 bc_parse_push(p, *type);
3896 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003897 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003898 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003899 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003900 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003901 goto err;
3902 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003903 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003904 s = zbc_parse_call(p, name, flags);
3905 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003906 *type = XC_INST_VAR;
3907 bc_parse_push(p, XC_INST_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003909 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003910 }
3911
Denys Vlasenko26819db2018-12-12 16:08:46 +01003912 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003913 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003914 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003915 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003916}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003917#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003918
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003919static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003920{
3921 BcStatus s;
3922
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003923 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003924 if (s) RETURN_STATUS(s);
3925 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003926
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003927 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003928 if (s) RETURN_STATUS(s);
3929 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003930
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003931 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003932
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003933 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003934}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003935#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003936
Denys Vlasenko26819db2018-12-12 16:08:46 +01003937static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003938 BcInst *prev)
3939{
3940 BcStatus s;
3941
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003942 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003943 if (s) RETURN_STATUS(s);
3944 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003945
3946 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3947
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003948 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003949 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003951 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
Denys Vlasenko26819db2018-12-12 16:08:46 +01003954 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003955
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003956 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Gavin Howard01055ba2018-11-03 11:00:21 -06003957 bc_parse_push(p, *prev);
3958
Denys Vlasenko26819db2018-12-12 16:08:46 +01003959 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003960}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003961#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003962
Denys Vlasenko26819db2018-12-12 16:08:46 +01003963static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003964{
3965 BcStatus s;
3966
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003967 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003968 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003969
3970 if (p->l.t.t != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003971 *type = XC_INST_SCALE;
3972 bc_parse_push(p, XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003973 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003974 }
3975
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003976 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3978
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003979 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003980 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003981
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003982 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003983 if (s) RETURN_STATUS(s);
3984 if (p->l.t.t != BC_LEX_RPAREN)
3985 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003986 bc_parse_push(p, XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003987
Denys Vlasenko26819db2018-12-12 16:08:46 +01003988 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003989}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003990#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003991
Denys Vlasenko26819db2018-12-12 16:08:46 +01003992static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003993 size_t *nexprs, uint8_t flags)
3994{
3995 BcStatus s;
3996 BcLexType type;
3997 char inst;
3998 BcInst etype = *prev;
3999
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004000 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4001 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4002 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004003 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004004 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4005 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004006 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004007 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004008 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4009 *paren_expr = true;
4010
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004011 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004012 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004013 type = p->l.t.t;
4014
4015 // Because we parse the next part of the expression
4016 // right here, we need to increment this.
4017 *nexprs = *nexprs + 1;
4018
4019 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004020 case XC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004021 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004022 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 case BC_LEX_KEY_IBASE:
4024 case BC_LEX_KEY_LAST:
4025 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004026 bc_parse_push(p, type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004027 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004028 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004029 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004030 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004031 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004032 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004033 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004034 else
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004035 bc_parse_push(p, XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004036 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004037 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004038 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004039 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004040 }
4041
4042 if (!s) bc_parse_push(p, inst);
4043 }
4044
Denys Vlasenko26819db2018-12-12 16:08:46 +01004045 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004046}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004047#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004048
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004049static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004050 bool rparen, size_t *nexprs)
4051{
4052 BcStatus s;
4053 BcLexType type;
4054 BcInst etype = *prev;
4055
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004056 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004057 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004058
4059 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004060 (etype >= XC_INST_NUM && etype <= XC_INST_SQRT) ?
Denys Vlasenko69560f42018-12-24 14:14:23 +01004061 XC_LEX_OP_MINUS :
4062 XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004063 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004064
4065 // We can just push onto the op stack because this is the largest
4066 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004067 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004068 bc_vec_push(&p->ops, &type);
4069 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004070 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004071
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004072 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004073}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004074#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004075
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004076static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004077{
4078 BcStatus s;
4079 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004080
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004081 for (;;) {
4082 s = zbc_lex_next(&p->l);
4083 if (s) RETURN_STATUS(s);
4084 type = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004085 if (type == XC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004086 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004087 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004088 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004089 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004090 if (s) RETURN_STATUS(s);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004091 bc_parse_push(p, XC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004092 if (p->l.t.t != BC_LEX_COMMA)
4093 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004094 }
4095
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004096 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004097}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004098#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004099
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004100static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004101{
4102 BcStatus s;
4103 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004104
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004105 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004106 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004107 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004108
4109 t = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004110 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Gavin Howard01055ba2018-11-03 11:00:21 -06004111 bc_parse_push(p, BC_INST_RET0);
4112 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004113 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004114 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004115 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004116 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004117 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004118 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004119 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004120
4121 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004122 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004123 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004124 }
4125
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004126 bc_parse_push(p, XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004127 }
4128
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004129 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004130 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004131}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004132#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004133
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004134static void rewrite_label_to_current(BcParse *p, size_t idx)
4135{
4136 size_t *label = bc_vec_item(&p->func->labels, idx);
4137 *label = p->func->code.len;
4138}
4139
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004140static BC_STATUS zbc_parse_if(BcParse *p)
4141{
4142 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004143 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004144
4145 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4146 s = zbc_lex_next(&p->l);
4147 if (s) RETURN_STATUS(s);
4148 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4149
4150 s = zbc_lex_next(&p->l);
4151 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004152 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004153 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004154 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004155
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004156 // Encode "if zero, jump to ..."
4157 // Pushed value (destination of the jump) is uninitialized,
4158 // will be rewritten to be address of "end of if()" or of "else".
4159 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004160 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004161
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004162 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004163 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004164
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004165 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4166 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004167 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004168
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004169 // Encode "after then_stmt, jump to end of if()"
4170 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4171 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004172 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004173
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004174 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004175 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004176
Denys Vlasenko15850832018-12-16 21:40:54 +01004177 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004178
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004179 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004180 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004181 }
4182
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004183 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004184 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004185
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004186 dbg_lex_done("%s:%d done", __func__, __LINE__);
4187 RETURN_STATUS(s);
4188}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004189#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004190
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004191static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004192{
4193 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004194 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004195 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004196
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004197 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004198 if (s) RETURN_STATUS(s);
4199 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004200 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004201 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004202
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004203 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004204 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004205 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004206
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004207 bc_vec_push(&p->exits, &ip_idx);
4208 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004209
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004210 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004211 if (s) RETURN_STATUS(s);
4212 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004213
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004214 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004215
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004216 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004217 if (s) RETURN_STATUS(s);
4218
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004219 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004220 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004221
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004222 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004223 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004224
4225 bc_vec_pop(&p->exits);
4226 bc_vec_pop(&p->conds);
4227
4228 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004229}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004230#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004231
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004232static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004233{
4234 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004235 size_t cond_idx, exit_idx, body_idx, update_idx;
4236
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004237 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004238 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004239 if (s) RETURN_STATUS(s);
4240 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004241 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004242 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004243
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004244 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004245 s = zbc_parse_expr(p, 0);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004246 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004247 if (s) RETURN_STATUS(s);
4248 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004249 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004250 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4251 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004252
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004253 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004254 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004255 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004256
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004257 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004258 update_idx = cond_idx + 1;
4259 body_idx = update_idx + 1;
4260 exit_idx = body_idx + 1;
4261
Gavin Howard01055ba2018-11-03 11:00:21 -06004262 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004263 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004264 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004265 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004266 // This is safe to set because the current token is a semicolon,
4267 // which has no string requirement.
4268 bc_vec_string(&p->l.t.v, 1, "1");
4269 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004270 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004271 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004272 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004273
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004274 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004275
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004276 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004277 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004278
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004279 bc_parse_pushJUMP_ZERO(p, exit_idx);
4280 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Gavin Howard01055ba2018-11-03 11:00:21 -06004282 bc_vec_push(&p->conds, &update_idx);
4283 bc_vec_push(&p->func->labels, &p->func->code.len);
4284
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004285 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004286 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004287 if (s) RETURN_STATUS(s);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004288 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004289 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004290 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004291 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004292 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4293 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004294
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004295 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004296 bc_vec_push(&p->func->labels, &p->func->code.len);
4297
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004298 bc_vec_push(&p->exits, &exit_idx);
4299 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004300
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004301 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004302 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004303
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004304 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004305 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004306
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004307 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004308 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004309
4310 bc_vec_pop(&p->exits);
4311 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004312
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004313 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004314}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004315#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004316
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004317static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004318{
4319 BcStatus s;
4320 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004321
Gavin Howard01055ba2018-11-03 11:00:21 -06004322 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004323 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4324 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004325 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004326 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004327 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004328 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004329
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004330 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004331
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004332 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004333 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004334
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004335 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != XC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004336 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004337
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004338 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004339}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004340#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004341
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004342static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4343{
4344 BcId *autoid;
4345 BcId a;
4346 size_t i;
4347
4348 autoid = (void*)f->autos.v;
4349 for (i = 0; i < f->autos.len; i++, autoid++) {
4350 if (strcmp(name, autoid->name) == 0)
4351 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
4352 }
4353
4354 a.idx = var;
4355 a.name = name;
4356
4357 bc_vec_push(&f->autos, &a);
4358
4359 RETURN_STATUS(BC_STATUS_SUCCESS);
4360}
4361#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4362
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004363static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004364{
4365 BcStatus s;
4366 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004367 char *name;
4368
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004369 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004370 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004371 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004372 if (p->l.t.t != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004373 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004374
4375 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004376 p->fidx = bc_program_addFunc(name);
4377 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004378
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004379 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004380 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004381 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004382 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004383 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004384 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004385
4386 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004387 if (p->l.t.t != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004388 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004389
4390 ++p->func->nparams;
4391
4392 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004393 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004394 if (s) goto err;
4395
4396 var = p->l.t.t != BC_LEX_LBRACKET;
4397
4398 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004399 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004400 if (s) goto err;
4401
4402 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004403 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004404 goto err;
4405 }
4406
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004407 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004408 if (s) goto err;
4409 }
4410
4411 comma = p->l.t.t == BC_LEX_COMMA;
4412 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004413 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004414 if (s) goto err;
4415 }
4416
Denys Vlasenko29301232018-12-11 15:29:32 +01004417 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004418 if (s) goto err;
4419 }
4420
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004421 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004422
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004423 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004424 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004425
4426 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004427 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004428
Denys Vlasenko202dd192018-12-16 17:30:35 +01004429 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4430 s = zbc_lex_skip_if_at_NLINE(&p->l);
4431 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004432 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4433 if (p->l.t.t != BC_LEX_LBRACE)
4434 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004435
4436 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004437 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004438 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004439 if (s) RETURN_STATUS(s);
4440
4441 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004442
4443 // Subsequent code generation is into main program
4444 p->fidx = BC_PROG_MAIN;
4445 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004446
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004447 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004448 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004449 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004450 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004451 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004452 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004453}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004454#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004455
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004456static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004457{
4458 BcStatus s;
4459 bool comma, var, one;
4460 char *name;
4461
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004462 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004463 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004464 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004465
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004466 comma = false;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004467 one = p->l.t.t == XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06004468
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004469 while (p->l.t.t == XC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004470 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004471 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004472 if (s) goto err;
4473
4474 var = p->l.t.t != BC_LEX_LBRACKET;
4475 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004476 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004477 if (s) goto err;
4478
4479 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004480 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004481 goto err;
4482 }
4483
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004484 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004485 if (s) goto err;
4486 }
4487
4488 comma = p->l.t.t == BC_LEX_COMMA;
4489 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004490 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004491 if (s) goto err;
4492 }
4493
Denys Vlasenko29301232018-12-11 15:29:32 +01004494 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004495 if (s) goto err;
4496 }
4497
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004498 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4499 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004500
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004501 if (p->l.t.t != XC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004502 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004503
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004504 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004505 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004506 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004507 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004508 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004509 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004510}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004511#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004512
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004513#undef zbc_parse_stmt_possibly_auto
4514static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004515{
4516 BcStatus s = BC_STATUS_SUCCESS;
4517
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004518 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004519
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004520 if (p->l.t.t == XC_LEX_NLINE) {
4521 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004522 RETURN_STATUS(zbc_lex_next(&p->l));
4523 }
4524 if (p->l.t.t == BC_LEX_SCOLON) {
4525 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4526 RETURN_STATUS(zbc_lex_next(&p->l));
4527 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004528
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004529 if (p->l.t.t == BC_LEX_LBRACE) {
4530 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4531 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004532 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004533 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004534 } while (p->l.t.t == XC_LEX_NLINE);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004535 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4536 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4537 s = zbc_parse_auto(p);
4538 if (s) RETURN_STATUS(s);
4539 }
4540 while (p->l.t.t != BC_LEX_RBRACE) {
4541 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4542 s = zbc_parse_stmt(p);
4543 if (s) RETURN_STATUS(s);
4544 }
4545 s = zbc_lex_next(&p->l);
4546 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4547 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004548 }
4549
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004550 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 switch (p->l.t.t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004552 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004553 case BC_LEX_OP_INC:
4554 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004555 case BC_LEX_OP_BOOL_NOT:
4556 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004557 case XC_LEX_NAME:
4558 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004559 case BC_LEX_KEY_IBASE:
4560 case BC_LEX_KEY_LAST:
4561 case BC_LEX_KEY_LENGTH:
4562 case BC_LEX_KEY_OBASE:
4563 case BC_LEX_KEY_READ:
4564 case BC_LEX_KEY_SCALE:
4565 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004566 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004568 case XC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004569 s = zbc_parse_pushSTR(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004570 bc_parse_push(p, XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004572 case BC_LEX_KEY_BREAK:
4573 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004574 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004577 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004581 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004584 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004587 // "limits" is a compile-time command,
4588 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004589 printf(
4590 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4591 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4592 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4593 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4594 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4595 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4596 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4597 "Number of vars = "BC_MAX_VARS_STR "\n"
4598 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004599 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004600 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004601 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004602 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004603 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004605 // "quit" is a compile-time command. For example,
4606 // "if (0 == 1) quit" terminates when parsing the statement,
4607 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004608 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004610 if (!p->in_funcdef)
4611 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004612 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004613 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004615 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004618 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004619 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 }
4621
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004622 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004623 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004624}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004625#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004626
Denys Vlasenko99b37622018-12-15 20:06:59 +01004627static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004628{
4629 BcStatus s;
4630
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004631 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004632 if (p->l.t.t == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004633 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004634 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004635 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004636 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004637 } else {
4638 dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004639 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004640 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004641
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004642 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004643 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004644}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004645#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004646
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004647// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004648static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004649{
4650 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004651 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004652 BcLexType top, t = p->l.t.t;
4653 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004654 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004655 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4656
Denys Vlasenko17df8822018-12-14 23:00:24 +01004657 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004658 paren_first = p->l.t.t == BC_LEX_LPAREN;
4659 nparens = nrelops = 0;
4660 paren_expr = rprn = done = get_token = assign = false;
4661 bin_last = true;
4662
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004663 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004664 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 case BC_LEX_OP_INC:
4667 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004668 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004669 rprn = get_token = bin_last = false;
4670 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004671 case XC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004672 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004673 rprn = get_token = false;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004674 bin_last = prev == XC_INST_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004675 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004676 case BC_LEX_OP_ASSIGN_POWER:
4677 case BC_LEX_OP_ASSIGN_MULTIPLY:
4678 case BC_LEX_OP_ASSIGN_DIVIDE:
4679 case BC_LEX_OP_ASSIGN_MODULUS:
4680 case BC_LEX_OP_ASSIGN_PLUS:
4681 case BC_LEX_OP_ASSIGN_MINUS:
4682 case BC_LEX_OP_ASSIGN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004683 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4684 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4685 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004686 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004687 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004688 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004689 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004690 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004691 break;
4692 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004693 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004694 case XC_LEX_OP_POWER:
4695 case XC_LEX_OP_MULTIPLY:
4696 case XC_LEX_OP_DIVIDE:
4697 case XC_LEX_OP_MODULUS:
4698 case XC_LEX_OP_PLUS:
4699 case XC_LEX_OP_REL_EQ:
4700 case XC_LEX_OP_REL_LE:
4701 case XC_LEX_OP_REL_GE:
4702 case XC_LEX_OP_REL_NE:
4703 case XC_LEX_OP_REL_LT:
4704 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004705 case BC_LEX_OP_BOOL_NOT:
4706 case BC_LEX_OP_BOOL_OR:
4707 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004708 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004709 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004710 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004711 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004712 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004713 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004714 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004715 bc_parse_operator(p, t, ops_bgn, &nexprs);
4716 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004717 rprn = get_token = false;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004718 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004719 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004721 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004722 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004723 ++nparens;
4724 paren_expr = rprn = bin_last = false;
4725 get_token = true;
4726 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004727 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004728 case BC_LEX_RPAREN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004729 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004730 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004731 if (nparens == 0) {
4732 s = BC_STATUS_SUCCESS;
4733 done = true;
4734 get_token = false;
4735 break;
4736 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004737 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004738 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004739 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004740 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004741 --nparens;
4742 paren_expr = rprn = true;
4743 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004744 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004745 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004746 case XC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004747 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004748 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004749 paren_expr = true;
4750 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004751 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004752 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004753 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004754 case XC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004755 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004756 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004757 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004758 nexprs++;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004759 prev = XC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004760 paren_expr = get_token = true;
4761 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004762 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004763 case BC_LEX_KEY_IBASE:
4764 case BC_LEX_KEY_LAST:
4765 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004766 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004767 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004768 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004769 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004770 paren_expr = get_token = true;
4771 rprn = bin_last = false;
4772 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004773 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004774 case BC_LEX_KEY_LENGTH:
4775 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004776 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004777 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004778 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 paren_expr = true;
4780 rprn = get_token = bin_last = false;
4781 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004782 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004784 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004785 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004786 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004787 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004788 paren_expr = true;
4789 rprn = get_token = bin_last = false;
4790 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004791 prev = XC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004792 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004793 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004794 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004795 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004796 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004797 paren_expr = true;
4798 rprn = get_token = bin_last = false;
4799 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004800 prev = XC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004801 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004802 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004803 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004804 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004805 }
4806
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004807 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004808 }
4809
4810 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004811 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004812
4813 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004814 top = BC_PARSE_TOP_OP(p);
4815 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4816
4817 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004818 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004819
Denys Vlasenko0154d782018-12-14 23:32:51 +01004820 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004821
Denys Vlasenko69560f42018-12-24 14:14:23 +01004822 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004823 bc_vec_pop(&p->ops);
4824 }
4825
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004826 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004827 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004828
Gavin Howard01055ba2018-11-03 11:00:21 -06004829 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004830 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004831 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004832 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004833 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004834 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004835 }
4836
4837 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004838 if (paren_first || !assign) bc_parse_push(p, XC_INST_PRINT);
4839 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004840 }
4841
Denys Vlasenko17df8822018-12-14 23:00:24 +01004842 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004843 return s;
4844}
4845
Gavin Howard01055ba2018-11-03 11:00:21 -06004846#endif // ENABLE_BC
4847
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004848#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004849
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004850static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004851{
4852 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004853
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004854 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004855 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004856 if (p->l.t.t != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004857
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004858 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004859
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004860 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004861}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004862#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004863
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004864static void dc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004865{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004866 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004867 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004868
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004869 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004870
4871 str = xstrdup(p->l.t.v.v);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004872 bc_parse_push(p, XC_INST_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004873 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004874 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004875
4876 // Explanation needed here
4877 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004878 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004879
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004880 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004881}
4882
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004883static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004884{
4885 BcStatus s;
4886
4887 bc_parse_push(p, inst);
4888 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004889 s = zdc_parse_register(p);
4890 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004891 }
4892
4893 if (store) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004894 bc_parse_push(p, DC_INST_SWAP);
4895 bc_parse_push(p, XC_INST_ASSIGN);
4896 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 }
4898
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004899 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004900}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004901#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004902
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004903static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004904{
4905 BcStatus s;
4906
4907 bc_parse_push(p, inst);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004908 bc_parse_push(p, DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004909
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004910 s = zdc_parse_register(p);
4911 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004912
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004913 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004914 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004915
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004916 // Note that 'else' part can not be on the next line:
4917 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4918 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004919 if (p->l.t.t == DC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004920 s = zdc_parse_register(p);
4921 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004922 s = zbc_lex_next(&p->l);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004923 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004924 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004925 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004926
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004927 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004928}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004929#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004930
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004931static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004932{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004933 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004934 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004935 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004936
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004937 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004938 s = BC_STATUS_SUCCESS;
4939 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004940 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004941 case XC_LEX_OP_REL_EQ:
4942 case XC_LEX_OP_REL_LE:
4943 case XC_LEX_OP_REL_GE:
4944 case XC_LEX_OP_REL_NE:
4945 case XC_LEX_OP_REL_LT:
4946 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004947 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenko69560f42018-12-24 14:14:23 +01004948 s = zdc_parse_cond(p, t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004949 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004950 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004951 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004952 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004953 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004954 s = zdc_parse_mem(p, XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004955 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004956 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004957 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004958 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004959 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004960 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004961 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4962 s = zbc_lex_next(&p->l);
4963 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004964 if (p->l.t.t != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004965 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004966 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004967 bc_parse_push(p, XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004968 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004969 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004970 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4971 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004972 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004973 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004974 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004975 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004976 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004977 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004978 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004979 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004980 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004981 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004982 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004983 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004984 case DC_LEX_LOAD:
4985 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004986 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004987 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004988 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004989 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004990 case DC_LEX_STORE_IBASE:
4991 case DC_LEX_STORE_SCALE:
4992 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004993 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004994 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004995 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004996 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004997 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004998 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4999 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005000 }
5001
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005002 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005003
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005004 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005005 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005006}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005007#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005008
Denys Vlasenko39287e02018-12-22 02:23:08 +01005009static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005010{
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005011 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005012
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005013 i = (int)p->l.t.t - (int)XC_LEX_OP_POWER;
5014 if (i >= 0) {
5015 BcInst inst = dc_LEX_to_INST[i];
5016 if (inst != DC_INST_INVALID) {
5017 bc_parse_push(p, inst);
5018 RETURN_STATUS(zbc_lex_next(&p->l));
5019 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005020 }
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005021 RETURN_STATUS(zdc_parse_token(p, p->l.t.t));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005022}
5023#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5024
5025static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
5026{
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01005027 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005028 while (p->l.t.t != XC_LEX_EOF) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005029 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005030 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005031 }
5032
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005033 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005034 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005035}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005036#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005037
Gavin Howard01055ba2018-11-03 11:00:21 -06005038#endif // ENABLE_DC
5039
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005040//
5041// Execution engine
5042//
5043
5044#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5045#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5046
Denys Vlasenkodf515392018-12-02 19:27:48 +01005047static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005048{
Gavin Howard01055ba2018-11-03 11:00:21 -06005049 BcId e, *ptr;
5050 BcVec *v, *map;
5051 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005052 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005053
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005054 v = var ? &G.prog.vars : &G.prog.arrs;
5055 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005056
5057 e.name = id;
5058 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005059 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005060
5061 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005062 BcVec v2;
5063 bc_array_init(&v2, var);
5064 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005065 }
5066
5067 ptr = bc_vec_item(map, i);
5068 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005069 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005070}
5071
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005072// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005073static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005074{
Gavin Howard01055ba2018-11-03 11:00:21 -06005075 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 case BC_RESULT_STR:
5077 case BC_RESULT_TEMP:
5078 case BC_RESULT_IBASE:
5079 case BC_RESULT_SCALE:
5080 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005081 *num = &r->d.n;
5082 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005083 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005084 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005085 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005086 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005087 size_t len;
5088
5089 str = *bc_program_const(r->d.id.idx);
5090 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005091
5092 bc_num_init(&r->d.n, len);
5093
5094 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005095 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005096 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005097 if (s) {
5098 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005100 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005101 *num = &r->d.n;
5102 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005103 break;
5104 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005105 case BC_RESULT_VAR:
5106 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005107 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005108 BcVec *v;
5109
Denys Vlasenkodf515392018-12-02 19:27:48 +01005110 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005111
5112 if (r->t == BC_RESULT_ARRAY_ELEM) {
5113 v = bc_vec_top(v);
5114 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5115 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005116 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005117 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005118 break;
5119 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005120#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005121 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005122 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005123 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005124 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005125 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005126 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005127#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005128#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005129 default:
5130 // Testing the theory that dc does not reach LAST/ONE
5131 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005132#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005133 }
5134
Denys Vlasenko29301232018-12-11 15:29:32 +01005135 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005136}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005137#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005138
Denys Vlasenko29301232018-12-11 15:29:32 +01005139static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005140 BcResult **r, BcNum **rn, bool assign)
5141{
5142 BcStatus s;
5143 bool hex;
5144 BcResultType lt, rt;
5145
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005146 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005147 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005148
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005149 *r = bc_vec_item_rev(&G.prog.results, 0);
5150 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005151
5152 lt = (*l)->t;
5153 rt = (*r)->t;
5154 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5155
Denys Vlasenko29301232018-12-11 15:29:32 +01005156 s = zbc_program_num(*l, ln, false);
5157 if (s) RETURN_STATUS(s);
5158 s = zbc_program_num(*r, rn, hex);
5159 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005160
5161 // We run this again under these conditions in case any vector has been
5162 // reallocated out from under the BcNums or arrays we had.
5163 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005164 s = zbc_program_num(*l, ln, false);
5165 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005166 }
5167
5168 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005169 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005170 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005171 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005172
Denys Vlasenko29301232018-12-11 15:29:32 +01005173 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005174}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005175#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005176
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005177static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005178{
5179 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005180 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005181 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005182}
5183
Denys Vlasenko29301232018-12-11 15:29:32 +01005184static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005185{
5186 BcStatus s;
5187
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005188 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005189 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005190 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005191
Denys Vlasenko29301232018-12-11 15:29:32 +01005192 s = zbc_program_num(*r, n, false);
5193 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005194
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005195 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005196 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005197
Denys Vlasenko29301232018-12-11 15:29:32 +01005198 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005199}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005200#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005201
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005202static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005203{
5204 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005205 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005206}
5207
Denys Vlasenko259137d2018-12-11 19:42:05 +01005208static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005209{
5210 BcStatus s;
5211 BcResult *opd1, *opd2, res;
5212 BcNum *n1, *n2 = NULL;
5213
Denys Vlasenko29301232018-12-11 15:29:32 +01005214 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005215 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005216 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005218 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005219 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 -06005220 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005221 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005222
Denys Vlasenko259137d2018-12-11 19:42:05 +01005223 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005224 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005225 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005226 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005227}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005228#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005229
Denys Vlasenko26819db2018-12-12 16:08:46 +01005230static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005231{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005232 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005233 BcStatus s;
5234 BcParse parse;
5235 BcVec buf;
5236 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005237 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005238
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005239 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005240 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005241
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005242 sv_file = G.prog.file;
5243 G.prog.file = NULL;
5244
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005245 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005246 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005247
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005248 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005249 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005250
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005251 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005252 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005253 if (IS_BC) {
5254 IF_BC(s = zbc_parse_expr(&parse, 0));
5255 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005256 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005257 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005258 if (s) goto exec_err;
5259
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005260 if (parse.l.t.t != XC_LEX_NLINE && parse.l.t.t != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005261 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005262 goto exec_err;
5263 }
5264
5265 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005266 ip.inst_idx = 0;
5267 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005268
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005269 bc_parse_push(&parse, XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005270 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005271 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005272 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005273 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005274 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005275 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005276}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005277#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005278
5279static size_t bc_program_index(char *code, size_t *bgn)
5280{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005281 unsigned char *bytes = (void*)(code + *bgn);
5282 unsigned amt;
5283 unsigned i;
5284 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005285
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005286 amt = *bytes++;
5287 *bgn += amt + 1;
5288
5289 amt *= 8;
5290 res = 0;
5291 for (i = 0; i < amt; i += 8)
5292 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005293
5294 return res;
5295}
5296
5297static char *bc_program_name(char *code, size_t *bgn)
5298{
5299 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005300 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005301
Denys Vlasenko694d2982018-12-18 19:17:11 +01005302 code += *bgn;
5303 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005304 i = 0;
5305 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005306 char c = *code++;
5307 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005308 break;
5309 s[i++] = c;
5310 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005311 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005312 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005313
5314 return s;
5315}
5316
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005317static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005318{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005319#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005320 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005321 // Example: echo '[]ap' | dc
5322 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005323 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005324 return;
5325 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005326#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005327 while (*str) {
5328 int c = *str++;
5329 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005330 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005331 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005332 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005333 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005334 case 'a':
5335 bb_putchar('\a');
5336 break;
5337 case 'b':
5338 bb_putchar('\b');
5339 break;
5340 case '\\':
5341 case 'e':
5342 bb_putchar('\\');
5343 break;
5344 case 'f':
5345 bb_putchar('\f');
5346 break;
5347 case 'n':
5348 bb_putchar('\n');
5349 G.prog.nchars = SIZE_MAX;
5350 break;
5351 case 'r':
5352 bb_putchar('\r');
5353 break;
5354 case 'q':
5355 bb_putchar('"');
5356 break;
5357 case 't':
5358 bb_putchar('\t');
5359 break;
5360 default:
5361 // Just print the backslash and following character.
5362 bb_putchar('\\');
5363 ++G.prog.nchars;
5364 bb_putchar(c);
5365 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005366 }
5367 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005368 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005369 }
5370}
5371
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005372static void bc_num_printNewline(void)
5373{
5374 if (G.prog.nchars == G.prog.len - 1) {
5375 bb_putchar('\\');
5376 bb_putchar('\n');
5377 G.prog.nchars = 0;
5378 }
5379}
5380
5381#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005382static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005383{
5384 (void) radix;
5385 bb_putchar((char) num);
5386 G.prog.nchars += width;
5387}
5388#endif
5389
5390static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5391{
5392 size_t exp, pow;
5393
5394 bc_num_printNewline();
5395 bb_putchar(radix ? '.' : ' ');
5396 ++G.prog.nchars;
5397
5398 bc_num_printNewline();
5399 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5400 continue;
5401
5402 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5403 size_t dig;
5404 bc_num_printNewline();
5405 dig = num / pow;
5406 num -= dig * pow;
5407 bb_putchar(((char) dig) + '0');
5408 }
5409}
5410
5411static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5412{
5413 if (radix) {
5414 bc_num_printNewline();
5415 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005416 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005417 }
5418
5419 bc_num_printNewline();
5420 bb_putchar(bb_hexdigits_upcase[num]);
5421 G.prog.nchars += width;
5422}
5423
5424static void bc_num_printDecimal(BcNum *n)
5425{
5426 size_t i, rdx = n->rdx - 1;
5427
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005428 if (n->neg) {
5429 bb_putchar('-');
5430 G.prog.nchars++;
5431 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005432
5433 for (i = n->len - 1; i < n->len; --i)
5434 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5435}
5436
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005437typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5438
Denys Vlasenkod3401432018-12-18 17:00:35 +01005439static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005440{
5441 BcStatus s;
5442 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005443 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005444 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005445 BcNum intp, fracp, digit, frac_len;
5446 unsigned long dig, *ptr;
5447 size_t i;
5448 bool radix;
5449
5450 if (n->len == 0) {
5451 print(0, width, false);
5452 RETURN_STATUS(BC_STATUS_SUCCESS);
5453 }
5454
5455 bc_vec_init(&stack, sizeof(long), NULL);
5456 bc_num_init(&intp, n->len);
5457 bc_num_init(&fracp, n->rdx);
5458 bc_num_init(&digit, width);
5459 bc_num_init(&frac_len, BC_NUM_INT(n));
5460 bc_num_copy(&intp, n);
5461 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005462 base.cap = ARRAY_SIZE(base_digs);
5463 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005464 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005465
5466 bc_num_truncate(&intp, intp.rdx);
5467 s = zbc_num_sub(n, &intp, &fracp, 0);
5468 if (s) goto err;
5469
5470 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005471 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005472 if (s) goto err;
5473 s = zbc_num_ulong(&digit, &dig);
5474 if (s) goto err;
5475 bc_vec_push(&stack, &dig);
5476 }
5477
5478 for (i = 0; i < stack.len; ++i) {
5479 ptr = bc_vec_item_rev(&stack, i);
5480 print(*ptr, width, false);
5481 }
5482
5483 if (!n->rdx) goto err;
5484
5485 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005486 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005487 if (s) goto err;
5488 s = zbc_num_ulong(&fracp, &dig);
5489 if (s) goto err;
5490 bc_num_ulong2num(&intp, dig);
5491 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5492 if (s) goto err;
5493 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005494 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005495 if (s) goto err;
5496 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005497 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005498 bc_num_free(&frac_len);
5499 bc_num_free(&digit);
5500 bc_num_free(&fracp);
5501 bc_num_free(&intp);
5502 bc_vec_free(&stack);
5503 RETURN_STATUS(s);
5504}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005505#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005506
5507static BC_STATUS zbc_num_printBase(BcNum *n)
5508{
5509 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005510 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005511 BcNumDigitOp print;
5512 bool neg = n->neg;
5513
5514 if (neg) {
5515 bb_putchar('-');
5516 G.prog.nchars++;
5517 }
5518
5519 n->neg = false;
5520
5521 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5522 width = 1;
5523 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005524 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005525 unsigned i = G.prog.ob_t - 1;
5526 width = 0;
5527 for (;;) {
5528 width++;
5529 i /= 10;
5530 if (i == 0)
5531 break;
5532 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005533 print = bc_num_printDigits;
5534 }
5535
Denys Vlasenkod3401432018-12-18 17:00:35 +01005536 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005537 n->neg = neg;
5538
5539 RETURN_STATUS(s);
5540}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005541#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005542
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005543static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5544{
5545 BcStatus s = BC_STATUS_SUCCESS;
5546
5547 bc_num_printNewline();
5548
5549 if (n->len == 0) {
5550 bb_putchar('0');
5551 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005552 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005553 bc_num_printDecimal(n);
5554 else
5555 s = zbc_num_printBase(n);
5556
5557 if (newline) {
5558 bb_putchar('\n');
5559 G.prog.nchars = 0;
5560 }
5561
5562 RETURN_STATUS(s);
5563}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005564#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005565
Denys Vlasenko29301232018-12-11 15:29:32 +01005566static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005567{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005568 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005569 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005570 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005571 bool pop = inst != XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06005572
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005573 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005574 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005575
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005576 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005577 s = zbc_program_num(r, &num, false);
5578 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005579
5580 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005581 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005582#if ENABLE_BC
5583 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5584#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005585 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005586 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005587
5588 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005589 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005590
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005591 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005592 for (;;) {
5593 char c = *str++;
5594 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005595 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005596 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005597 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005598 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005599 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005600 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005601 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005602 }
5603 }
5604
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005605 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005606
Denys Vlasenko29301232018-12-11 15:29:32 +01005607 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005608}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005609#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005610
Denys Vlasenko29301232018-12-11 15:29:32 +01005611static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005612{
5613 BcStatus s;
5614 BcResult res, *ptr;
5615 BcNum *num = NULL;
5616
Denys Vlasenko29301232018-12-11 15:29:32 +01005617 s = zbc_program_prep(&ptr, &num);
5618 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005619
5620 bc_num_init(&res.d.n, num->len);
5621 bc_num_copy(&res.d.n, num);
5622 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5623
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005624 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005625
Denys Vlasenko29301232018-12-11 15:29:32 +01005626 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005627}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005628#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005629
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005630static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005631{
5632 BcStatus s;
5633 BcResult *opd1, *opd2, res;
5634 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005635 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005636
Denys Vlasenko29301232018-12-11 15:29:32 +01005637 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005638 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005639
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005640 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005641
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005642 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005643 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005644 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005645 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005646 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005647 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005648 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005649 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005650 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005651 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005652 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005653 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005654 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005655 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005656 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005657 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005658 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005659 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005660 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005661 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005662 cond = (cond > 0);
5663 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005664 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005665 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005666 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005667 }
5668 }
5669
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005670 if (cond) bc_num_one(&res.d.n);
5671 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005672
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005673 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005674
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005675 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005676}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005677#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005678
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005679#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005680static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005681{
5682 BcNum n2;
5683 BcResult res;
5684
5685 memset(&n2, 0, sizeof(BcNum));
5686 n2.rdx = res.d.id.idx = r->d.id.idx;
5687 res.t = BC_RESULT_STR;
5688
5689 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005690 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005691 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005692 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005693 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005694 }
5695
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005696 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005697 bc_vec_push(v, &n2);
5698
Denys Vlasenko29301232018-12-11 15:29:32 +01005699 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005700}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005701#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005702#endif // ENABLE_DC
5703
Denys Vlasenko29301232018-12-11 15:29:32 +01005704static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005705{
5706 BcStatus s;
5707 BcResult *ptr, r;
5708 BcVec *v;
5709 BcNum *n;
5710
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005711 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005712 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005713
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005714 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005715 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005716 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005717 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005718
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005719#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005720 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005721 RETURN_STATUS(bc_error_variable_is_wrong_type());
5722 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005723 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005724#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005725
Denys Vlasenko29301232018-12-11 15:29:32 +01005726 s = zbc_program_num(ptr, &n, false);
5727 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005728
5729 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005730 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005731
5732 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005733 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005734 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005735 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005736 bc_array_init(&r.d.v, true);
5737 bc_array_copy(&r.d.v, (BcVec *) n);
5738 }
5739
5740 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005741 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005742
Denys Vlasenko29301232018-12-11 15:29:32 +01005743 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005744}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005745#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005746
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005747static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005748{
5749 BcStatus s;
5750 BcResult *left, *right, res;
5751 BcNum *l = NULL, *r = NULL;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005752 bool assign = inst == XC_INST_ASSIGN, ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005753
Denys Vlasenko29301232018-12-11 15:29:32 +01005754 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005755 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005756
5757 ib = left->t == BC_RESULT_IBASE;
5758 sc = left->t == BC_RESULT_SCALE;
5759
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005760#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005761 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005762 BcVec *v;
5763
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005764 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005765 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005766 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005767
Denys Vlasenkofa210792018-12-19 19:35:40 +01005768 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005769 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005770#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005771
5772 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005773 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005774 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005775 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005776 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005777
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005778#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005779 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005780 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005781
5782 if (assign)
5783 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005784 else {
5785 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005786 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 +01005787 }
5788 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005789#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005791#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005792
5793 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005794 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005795 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
Denys Vlasenko64074a12018-12-07 15:50:14 +01005796 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005797 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005798 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005799 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005800 size_t max;
5801 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005802
Denys Vlasenko29301232018-12-11 15:29:32 +01005803 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005804 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005805 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005806 if (sc) {
5807 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005808 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005809 } else {
5810 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005811 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005812 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005813 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005814 }
5815
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005816 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005817 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005818
5819 *ptr = (size_t) val;
5820 s = BC_STATUS_SUCCESS;
5821 }
5822
5823 bc_num_init(&res.d.n, l->len);
5824 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005825 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005826
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005827 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005828}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005829#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005830
Denys Vlasenko416ce762018-12-02 20:57:17 +01005831#if !ENABLE_DC
5832#define bc_program_pushVar(code, bgn, pop, copy) \
5833 bc_program_pushVar(code, bgn)
5834// for bc, 'pop' and 'copy' are always false
5835#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005836static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005837 bool pop, bool copy)
5838{
Gavin Howard01055ba2018-11-03 11:00:21 -06005839 BcResult r;
5840 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005841
5842 r.t = BC_RESULT_VAR;
5843 r.d.id.name = name;
5844
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005845#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005846 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005847 BcVec *v = bc_program_search(name, true);
5848 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005849
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005850 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005851 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005852 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005853 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005854
5855 if (!BC_PROG_STR(num)) {
5856 r.t = BC_RESULT_TEMP;
5857 bc_num_init_DEF_SIZE(&r.d.n);
5858 bc_num_copy(&r.d.n, num);
5859 } else {
5860 r.t = BC_RESULT_STR;
5861 r.d.id.idx = num->rdx;
5862 }
5863
5864 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005865 }
5866#endif // ENABLE_DC
5867
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005868 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005869
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005870 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005871}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005872#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005873
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005874static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005875{
5876 BcStatus s = BC_STATUS_SUCCESS;
5877 BcResult r;
5878 BcNum *num;
5879
5880 r.d.id.name = bc_program_name(code, bgn);
5881
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005882 if (inst == XC_INST_ARRAY) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005883 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005884 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005885 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005886 BcResult *operand;
5887 unsigned long temp;
5888
Denys Vlasenko29301232018-12-11 15:29:32 +01005889 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005890 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005891 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005892 if (s) goto err;
5893
5894 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005895 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005896 goto err;
5897 }
5898
5899 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005900 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005901 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005902 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005903 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005904 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005905}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005906#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005907
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005908#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005909static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005910{
5911 BcStatus s;
5912 BcResult *ptr, res, copy;
5913 BcNum *num = NULL;
5914 char inst2 = inst;
5915
Denys Vlasenko29301232018-12-11 15:29:32 +01005916 s = zbc_program_prep(&ptr, &num);
5917 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005918
5919 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5920 copy.t = BC_RESULT_TEMP;
5921 bc_num_init(&copy.d.n, num->len);
5922 bc_num_copy(&copy.d.n, num);
5923 }
5924
5925 res.t = BC_RESULT_ONE;
5926 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5927 BC_INST_ASSIGN_PLUS :
5928 BC_INST_ASSIGN_MINUS;
5929
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005930 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005931 s = zbc_program_assign(inst);
5932 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005933
5934 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005935 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005936 }
5937
Denys Vlasenko29301232018-12-11 15:29:32 +01005938 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005939}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005940#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005941
Denys Vlasenko29301232018-12-11 15:29:32 +01005942static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005943{
Gavin Howard01055ba2018-11-03 11:00:21 -06005944 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005945 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005946 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005948 BcResult *arg;
5949
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005950 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005951 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005952 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005953 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005954
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005955 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005956 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005957 }
5958 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005959 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005960 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005961 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005962
5963 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005964 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005965
5966 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005967 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005968
5969 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005970 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005971
Denys Vlasenko29301232018-12-11 15:29:32 +01005972 s = zbc_program_copyToVar(a->name, a->idx);
5973 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005974 }
5975
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005976 a = bc_vec_item(&func->autos, i);
5977 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005978 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005979
Denys Vlasenkodf515392018-12-02 19:27:48 +01005980 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005981 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005982 BcNum n2;
5983 bc_num_init_DEF_SIZE(&n2);
5984 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005985 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005986 BcVec v2;
5987 bc_array_init(&v2, true);
5988 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005989 }
5990 }
5991
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005992 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005993
Denys Vlasenko29301232018-12-11 15:29:32 +01005994 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005995}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005996#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005997
Denys Vlasenko29301232018-12-11 15:29:32 +01005998static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005999{
Gavin Howard01055ba2018-11-03 11:00:21 -06006000 BcResult res;
6001 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006002 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006003 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006004 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006005
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006006 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 +01006007 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006008
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006009 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006010 res.t = BC_RESULT_TEMP;
6011
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006012 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006013 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006014 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006015 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006016
Denys Vlasenko29301232018-12-11 15:29:32 +01006017 s = zbc_program_num(operand, &num, false);
6018 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006019 bc_num_init(&res.d.n, num->len);
6020 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006021 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006022 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006023 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 }
6025
6026 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006027 a = (void*)f->autos.v;
6028 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006029 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006030 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006031 bc_vec_pop(v);
6032 }
6033
Denys Vlasenko24e41942018-12-21 23:01:26 +01006034 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006035 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006036 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006037
Denys Vlasenko29301232018-12-11 15:29:32 +01006038 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006039}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006040#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006041#endif // ENABLE_BC
6042
6043static unsigned long bc_program_scale(BcNum *n)
6044{
6045 return (unsigned long) n->rdx;
6046}
6047
6048static unsigned long bc_program_len(BcNum *n)
6049{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006050 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006051
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006052 if (n->rdx != len) return len;
6053 for (;;) {
6054 if (len == 0) break;
6055 len--;
6056 if (n->num[len] != 0) break;
6057 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006058 return len;
6059}
6060
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006061static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006062{
6063 BcStatus s;
6064 BcResult *opnd;
6065 BcNum *num = NULL;
6066 BcResult res;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006067 bool len = inst == XC_INST_LENGTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006068
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006069 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006070 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006071 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006072
Denys Vlasenko29301232018-12-11 15:29:32 +01006073 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006074 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006075
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006076#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006077 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006078 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006079#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006080
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006081 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006082
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006083 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006084 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006085#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006086 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006087 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006088 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006089#endif
6090#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006092 char **str;
6093 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6094
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006095 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006096 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006098#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006099 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006100 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 }
6102
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006103 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006104
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006105 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006106}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006107#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006108
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006109#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006110static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006111{
6112 BcStatus s;
6113 BcResult *opd1, *opd2, res, res2;
6114 BcNum *n1, *n2 = NULL;
6115
Denys Vlasenko29301232018-12-11 15:29:32 +01006116 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006117 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006118
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006119 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006120 bc_num_init(&res2.d.n, n2->len);
6121
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006122 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123 if (s) goto err;
6124
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006125 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006126 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006127 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006129 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006130 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006131 bc_num_free(&res2.d.n);
6132 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006133 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006134}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006135#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006136
Denys Vlasenkofa210792018-12-19 19:35:40 +01006137static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006138{
6139 BcStatus s;
6140 BcResult *r1, *r2, *r3, res;
6141 BcNum *n1, *n2, *n3;
6142
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006143 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006144 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006145 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006146 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006147
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006148 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006149 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006150 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006151 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006152 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006153
6154 // Make sure that the values have their pointers updated, if necessary.
6155 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006156 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006157 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006158 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006159 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006161 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006162 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006163 }
6164 }
6165
6166 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006167 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006168 if (s) goto err;
6169
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006170 bc_vec_pop(&G.prog.results);
6171 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006172
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006173 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006174 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006175 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006176 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006177}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006178#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006179
Denys Vlasenkofa210792018-12-19 19:35:40 +01006180static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006181{
Gavin Howard01055ba2018-11-03 11:00:21 -06006182 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006183 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006184
6185 res.t = BC_RESULT_TEMP;
6186
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006187 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006188 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006189 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006190}
6191
Denys Vlasenkofa210792018-12-19 19:35:40 +01006192static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006193{
6194 BcStatus s;
6195 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006196 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006197 char **strs;
6198 char *str;
6199 char c;
6200 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006201
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006202 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006203 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006204 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006205
Denys Vlasenko29301232018-12-11 15:29:32 +01006206 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006207 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006208
6209 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006210 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006211 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006212 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006213
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006214 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006215 bc_num_copy(&n, num);
6216 bc_num_truncate(&n, n.rdx);
6217
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006218 strmb.cap = ARRAY_SIZE(strmb_digs);
6219 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006220 bc_num_ulong2num(&strmb, 0x100);
6221 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006222
Gavin Howard01055ba2018-11-03 11:00:21 -06006223 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006224 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006225 if (s) goto num_err;
6226
6227 c = (char) val;
6228
6229 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006230 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006231 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006232 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006233 sp = *bc_program_str(idx);
6234 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006235 }
6236
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006237 strs = (void*)G.prog.strs.v;
6238 for (idx = 0; idx < G.prog.strs.len; idx++) {
6239 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6240 goto dup;
6241 }
6242 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006243 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006244 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006245 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006246 bc_vec_push(&G.prog.strs, &str);
6247 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006248 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006249 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006250 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006251
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006252 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006253 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006254 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006255 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006256}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006257#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006258
Denys Vlasenkofa210792018-12-19 19:35:40 +01006259static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006260{
6261 BcStatus s;
6262 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006263 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006264 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006265
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006266 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006267 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006268 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006269
Denys Vlasenko29301232018-12-11 15:29:32 +01006270 s = zbc_program_num(r, &n, false);
6271 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006272
Denys Vlasenko57734c92018-12-17 21:14:05 +01006273 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006274 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006275 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006276 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006277 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006278 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006279 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006280 }
6281
Denys Vlasenko29301232018-12-11 15:29:32 +01006282 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006283}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006284#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006285
Denys Vlasenkofa210792018-12-19 19:35:40 +01006286static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006287{
6288 BcStatus s;
6289 BcResult *opnd;
6290 BcNum *num = NULL;
6291 unsigned long val;
6292
Denys Vlasenko29301232018-12-11 15:29:32 +01006293 s = zbc_program_prep(&opnd, &num);
6294 if (s) RETURN_STATUS(s);
6295 s = zbc_num_ulong(num, &val);
6296 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006297
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006298 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006299
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006300 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006301 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006302 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006303 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006304 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006305
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006306 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307
Denys Vlasenko29301232018-12-11 15:29:32 +01006308 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006309}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006310#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006311
Denys Vlasenkofa210792018-12-19 19:35:40 +01006312static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006313{
6314 BcStatus s = BC_STATUS_SUCCESS;
6315 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006316 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006317 BcInstPtr ip;
6318 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006319
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006320 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006321 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006322
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006323 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324
6325 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006326 BcNum *n = n; // for compiler
6327 bool exec;
6328 char *name;
6329 char *then_name = bc_program_name(code, bgn);
6330 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
6332 if (code[*bgn] == BC_PARSE_STREND)
6333 (*bgn) += 1;
6334 else
6335 else_name = bc_program_name(code, bgn);
6336
6337 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006338 name = then_name;
6339 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006340 exec = true;
6341 name = else_name;
6342 }
6343
6344 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006345 BcVec *v;
6346 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006347 n = bc_vec_top(v);
6348 }
6349
6350 free(then_name);
6351 free(else_name);
6352
6353 if (!exec) goto exit;
6354 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006355 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 goto exit;
6357 }
6358
6359 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006360 } else {
6361 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006363 } else if (r->t == BC_RESULT_VAR) {
6364 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006365 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006366 if (s || !BC_PROG_STR(n)) goto exit;
6367 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006368 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006369 goto exit;
6370 }
6371
6372 fidx = sidx + BC_PROG_REQ_FUNCS;
6373
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006374 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006375
6376 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006377 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006378 BcParse prs;
6379 char *str;
6380
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006381 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006382 str = *bc_program_str(sidx);
6383 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006384 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006385
6386 sv_input_fp = G.input_fp;
6387 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6388 s = zdc_parse_exprs_until_eof(&prs);
6389 G.input_fp = sv_input_fp;
6390
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006392 if (prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006393 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006394 err:
6395 bc_parse_free(&prs);
6396 bc_vec_pop_all(&f->code);
6397 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006398 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006399 bc_parse_push(&prs, DC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006400 bc_parse_free(&prs);
6401 }
6402
Denys Vlasenko24e41942018-12-21 23:01:26 +01006403 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006404 ip.func = fidx;
6405
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006406 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006407 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006408
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006409 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006410 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006411 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006412 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006413}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006414#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006415#endif // ENABLE_DC
6416
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006417static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006418{
Gavin Howard01055ba2018-11-03 11:00:21 -06006419 BcResult res;
6420 unsigned long val;
6421
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006422 res.t = inst - XC_INST_IBASE + BC_RESULT_IBASE;
6423 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006424 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006425 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006426 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006427 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006428 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006429
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006430 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006431 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006432 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006433}
6434
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006435static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006436{
Gavin Howard01055ba2018-11-03 11:00:21 -06006437 BcResult r, *ptr;
6438 BcNum *num;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006439 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006440 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006441 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006442
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006443 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006444 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6445 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006446 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006447 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006448
Denys Vlasenko24e41942018-12-21 23:01:26 +01006449 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 -06006450 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006451#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006452 case BC_INST_JUMP_ZERO: {
6453 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006454 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006455 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006456 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006457 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006458 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006459 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006460 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006461 break;
6462 }
6463 // else: fall through
6464 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006465 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006466 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006467 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006468 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006469 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006470 break;
6471 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006472 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006473 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006474 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006475 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006476 case BC_INST_INC_PRE:
6477 case BC_INST_DEC_PRE:
6478 case BC_INST_INC_POST:
6479 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006480 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006481 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006482 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006483 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006484 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006485 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006486 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006487 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006488 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006489 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006490 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006491 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006492 case XC_INST_BOOL_OR:
6493 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006494#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006495 case XC_INST_REL_EQ:
6496 case XC_INST_REL_LE:
6497 case XC_INST_REL_GE:
6498 case XC_INST_REL_NE:
6499 case XC_INST_REL_LT:
6500 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006501 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006502 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006504 case XC_INST_READ:
6505 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006506 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006507 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006508 case XC_INST_VAR:
6509 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006510 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006512 case XC_INST_ARRAY_ELEM:
6513 case XC_INST_ARRAY:
6514 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006515 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006516 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006517#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006518 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006519 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006520 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006521 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006522 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006523#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006524 case XC_INST_IBASE:
6525 case XC_INST_OBASE:
6526 case XC_INST_SCALE:
6527 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006528 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006529 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006530 case XC_INST_SCALE_FUNC:
6531 case XC_INST_LENGTH:
6532 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006533 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006534 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006535 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006536 case XC_INST_NUM:
6537 dbg_exec("XC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006538 r.t = BC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006539 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006540 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006541 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006542 case XC_INST_POP:
6543 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006544 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006545 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006546 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006547 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006548 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006549 case XC_INST_PRINT:
6550 case XC_INST_PRINT_POP:
6551 case XC_INST_PRINT_STR:
6552 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006553 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006555 case XC_INST_STR:
6556 dbg_exec("XC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006557 r.t = BC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006558 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006559 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006560 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006561 case XC_INST_POWER:
6562 case XC_INST_MULTIPLY:
6563 case XC_INST_DIVIDE:
6564 case XC_INST_MODULUS:
6565 case XC_INST_PLUS:
6566 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006567 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006568 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006570 case XC_INST_BOOL_NOT:
6571 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006572 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006573 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006574 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006575 if (!bc_num_cmp(num, &G.prog.zero))
6576 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006577 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006578 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006579 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006580 case XC_INST_NEG:
6581 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006582 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006583 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006584#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006585 case BC_INST_ASSIGN_POWER:
6586 case BC_INST_ASSIGN_MULTIPLY:
6587 case BC_INST_ASSIGN_DIVIDE:
6588 case BC_INST_ASSIGN_MODULUS:
6589 case BC_INST_ASSIGN_PLUS:
6590 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006591#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006592 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006593 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006594 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006596#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006597 case DC_INST_POP_EXEC:
6598 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006599 bc_vec_pop(&G.prog.exestack);
6600 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006601 case DC_INST_MODEXP:
6602 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006603 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006604 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006605 case DC_INST_DIVMOD:
6606 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006607 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006608 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006609 case DC_INST_EXECUTE:
6610 case DC_INST_EXEC_COND:
6611 dbg_exec("DC_INST_EXEC[_COND]:");
6612 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006613 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006614 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006615 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006616 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006617 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006618 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006619 if (s) break;
6620 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006621 break;
6622 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006623 case DC_INST_CLEAR_STACK:
6624 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006625 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006626 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006627 case DC_INST_STACK_LEN:
6628 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006629 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006630 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006631 case DC_INST_DUPLICATE:
6632 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006633 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006634 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006635 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006636 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006637 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006638 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006639 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006640 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006641 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006642 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006643 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006644 ptr = bc_vec_item_rev(&G.prog.results, 0);
6645 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006646 memcpy(&r, ptr, sizeof(BcResult));
6647 memcpy(ptr, ptr2, sizeof(BcResult));
6648 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006649 break;
6650 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006651 case DC_INST_ASCIIFY:
6652 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006653 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006654 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006655 case DC_INST_PRINT_STREAM:
6656 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006657 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006658 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006659 case DC_INST_LOAD:
6660 case DC_INST_PUSH_VAR: {
6661 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006662 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006663 break;
6664 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006665 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006666 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006667 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006668 free(name);
6669 break;
6670 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006671 case DC_INST_QUIT:
6672 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006673 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006674 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006675 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006676 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006677 case DC_INST_NQUIT:
6678 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006679 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006680 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006681#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006682 read_updated_ip:
6683 // Instruction stack has changed, read new pointers
6684 ip = bc_vec_top(&G.prog.exestack);
6685 func = bc_program_func(ip->func);
6686 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006687 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006688 }
6689
Denys Vlasenkod38af482018-12-04 19:11:02 +01006690 if (s || G_interrupt) {
6691 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006692 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006693 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006694
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006695 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 }
6697
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006698 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006699}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006700#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006701
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006702static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006703{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006704 char *lenv;
6705 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006706
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006707 lenv = getenv(var);
6708 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 if (!lenv) return len;
6710
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006711 len = bb_strtou(lenv, NULL, 10) - 1;
6712 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006713 len = BC_NUM_PRINT_WIDTH;
6714
6715 return len;
6716}
6717
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006718static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006719{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006720 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006721
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006722 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006723 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006724 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006725
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006726 while (G.prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006727 BcInstPtr *ip;
6728 BcFunc *f;
6729
Denys Vlasenko39287e02018-12-22 02:23:08 +01006730 dbg_lex("%s:%d G.prs.l.t.t:%d, parsing...", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006731 if (IS_BC) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006732// FIXME: "eating" of stmt delimiters is coded inconsistently
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006733// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6734// which causes bugs such as "print 1 print 2" erroneously accepted,
6735// or "print 1 else 2" detecting parse error only after executing
6736// "print 1" part.
6737 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6738 } else {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006739 // Most of dc parsing assumes all whitespace,
6740 // including '\n', is eaten.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01006741 while (G.prs.l.t.t == XC_LEX_NLINE) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006742 s = zbc_lex_next(&G.prs.l);
6743 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006744 if (G.prs.l.t.t == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006745 goto done;
6746 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006747 IF_DC(s = zdc_parse_expr(&G.prs));
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006748 }
6749 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006750 err:
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006751 bc_parse_reset(&G.prs); // includes bc_program_reset()
6752 RETURN_STATUS(BC_STATUS_FAILURE);
6753 }
6754
Denys Vlasenko39287e02018-12-22 02:23:08 +01006755 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006756 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006757 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006758 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006759 break;
6760 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006761
6762 ip = (void*)G.prog.exestack.v;
6763#if SANITY_CHECKS
6764 if (G.prog.exestack.len != 1) // should have only main's IP
6765 bb_error_msg_and_die("BUG:call stack");
6766 if (ip->func != BC_PROG_MAIN)
6767 bb_error_msg_and_die("BUG:not MAIN");
6768#endif
6769 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006770 // bc discards strings, constants and code after each
6771 // top-level statement in the "main program".
6772 // This prevents "yes 1 | bc" from growing its memory
6773 // without bound. This can be done because data stack
6774 // is empty and thus can't hold any references to
6775 // strings or constants, there is no generated code
6776 // which can hold references (after we discard one
6777 // we just executed). Code of functions can have references,
6778 // but bc stores function strings/constants in per-function
6779 // storage.
6780 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006781#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006782 if (G.prog.results.len != 0) // should be empty
6783 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006784#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006785 IF_BC(bc_vec_pop_all(&f->strs);)
6786 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006787 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006788 if (G.prog.results.len == 0
6789 && G.prog.vars.len == 0
6790 ) {
6791 // If stack is empty and no registers exist (TODO: or they are all empty),
6792 // we can get rid of accumulated strings and constants.
6793 // In this example dc process should not grow
6794 // its memory consumption with time:
6795 // yes 1pc | dc
6796 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6797 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6798 }
6799 // The code is discarded always (below), thus this example
6800 // should also not grow its memory consumption with time,
6801 // even though its data stack is not empty:
6802 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006803 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006804 // We drop generated and executed code for both bc and dc:
6805 bc_vec_pop_all(&f->code);
6806 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006808 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006809 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006810 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006811}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006812#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006813
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006814static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006815{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006816 // So far bc/dc have no way to include a file from another file,
6817 // therefore we know G.prog.file == NULL on entry
6818 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006819 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006820
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006821 G.prog.file = filename;
6822 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006823 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006824
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006825 do {
6826 s = zbc_vm_process("");
6827 // We do not stop looping on errors here if reading stdin.
6828 // Example: start interactive bc and enter "return".
6829 // It should say "'return' not in a function"
6830 // but should not exit.
6831 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006832 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006833 RETURN_STATUS(s);
6834}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006835#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006836
6837static BC_STATUS zbc_vm_file(const char *file)
6838{
6839 BcStatus s;
6840 FILE *fp;
6841
6842 fp = xfopen_for_read(file);
6843 s = zbc_vm_execute_FILE(fp, file);
6844 fclose(fp);
6845
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006846 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006847}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006848#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006849
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006850#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006851static void bc_vm_info(void)
6852{
6853 printf("%s "BB_VER"\n"
6854 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6855 , applet_name);
6856}
6857
6858static void bc_args(char **argv)
6859{
6860 unsigned opts;
6861 int i;
6862
6863 GETOPT_RESET();
6864#if ENABLE_FEATURE_BC_LONG_OPTIONS
6865 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6866 "warn\0" No_argument "w"
6867 "version\0" No_argument "v"
6868 "standard\0" No_argument "s"
6869 "quiet\0" No_argument "q"
6870 "mathlib\0" No_argument "l"
6871 "interactive\0" No_argument "i"
6872 );
6873#else
6874 opts = option_mask32 |= getopt32(argv, "wvsqli");
6875#endif
6876 if (getenv("POSIXLY_CORRECT"))
6877 option_mask32 |= BC_FLAG_S;
6878
6879 if (opts & BC_FLAG_V) {
6880 bc_vm_info();
6881 exit(0);
6882 }
6883
6884 for (i = optind; argv[i]; ++i)
6885 bc_vec_push(&G.files, argv + i);
6886}
6887
6888static void bc_vm_envArgs(void)
6889{
6890 BcVec v;
6891 char *buf;
6892 char *env_args = getenv("BC_ENV_ARGS");
6893
6894 if (!env_args) return;
6895
6896 G.env_args = xstrdup(env_args);
6897 buf = G.env_args;
6898
6899 bc_vec_init(&v, sizeof(char *), NULL);
6900
6901 while (*(buf = skip_whitespace(buf)) != '\0') {
6902 bc_vec_push(&v, &buf);
6903 buf = skip_non_whitespace(buf);
6904 if (!*buf)
6905 break;
6906 *buf++ = '\0';
6907 }
6908
6909 // NULL terminate, and pass argv[] so that first arg is argv[1]
6910 if (sizeof(int) == sizeof(char*)) {
6911 bc_vec_push(&v, &const_int_0);
6912 } else {
6913 static char *const nullptr = NULL;
6914 bc_vec_push(&v, &nullptr);
6915 }
6916 bc_args(((char **)v.v) - 1);
6917
6918 bc_vec_free(&v);
6919}
6920
6921static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006922 "scale=20"
6923"\n" "define e(x){"
6924"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006925////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6926//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6927//e(-.998896): GNU:.36828580434569428695
6928// above code:.36828580434569428696
6929// actual value:.3682858043456942869594...
6930// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006931"\n" "b=ibase"
6932"\n" "ibase=A"
6933"\n" "if(x<0){"
6934"\n" "n=1"
6935"\n" "x=-x"
6936"\n" "}"
6937"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006938"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006939"\n" "scale=scale(x)+1"
6940"\n" "while(x>1){"
6941"\n" "d+=1"
6942"\n" "x/=2"
6943"\n" "scale+=1"
6944"\n" "}"
6945"\n" "scale=r"
6946"\n" "r=x+1"
6947"\n" "p=x"
6948"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006949"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006950"\n" "p*=x"
6951"\n" "f*=i"
6952"\n" "v=p/f"
6953"\n" "r+=v"
6954"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006955"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006956"\n" "scale=s"
6957"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006958"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006959"\n" "return(r/1)"
6960"\n" "}"
6961"\n" "define l(x){"
6962"\n" "auto b,s,r,p,a,q,i,v"
6963"\n" "b=ibase"
6964"\n" "ibase=A"
6965"\n" "if(x<=0){"
6966"\n" "r=(1-10^scale)/1"
6967"\n" "ibase=b"
6968"\n" "return(r)"
6969"\n" "}"
6970"\n" "s=scale"
6971"\n" "scale+=6"
6972"\n" "p=2"
6973"\n" "while(x>=2){"
6974"\n" "p*=2"
6975"\n" "x=sqrt(x)"
6976"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006977"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006978"\n" "p*=2"
6979"\n" "x=sqrt(x)"
6980"\n" "}"
6981"\n" "r=a=(x-1)/(x+1)"
6982"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006983"\n" "v=1"
6984"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006985"\n" "a*=q"
6986"\n" "v=a/i"
6987"\n" "r+=v"
6988"\n" "}"
6989"\n" "r*=p"
6990"\n" "scale=s"
6991"\n" "ibase=b"
6992"\n" "return(r/1)"
6993"\n" "}"
6994"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006995"\n" "auto b,s,r,a,q,i"
6996"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006997"\n" "b=ibase"
6998"\n" "ibase=A"
6999"\n" "s=scale"
7000"\n" "scale=1.1*s+2"
7001"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007002"\n" "scale=0"
7003"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007004"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007005"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007006"\n" "scale=s+2"
7007"\n" "r=a=x"
7008"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007009"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007010"\n" "a*=q/(i*(i-1))"
7011"\n" "r+=a"
7012"\n" "}"
7013"\n" "scale=s"
7014"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007015"\n" "return(r/1)"
7016"\n" "}"
7017"\n" "define c(x){"
7018"\n" "auto b,s"
7019"\n" "b=ibase"
7020"\n" "ibase=A"
7021"\n" "s=scale"
7022"\n" "scale*=1.2"
7023"\n" "x=s(2*a(1)+x)"
7024"\n" "scale=s"
7025"\n" "ibase=b"
7026"\n" "return(x/1)"
7027"\n" "}"
7028"\n" "define a(x){"
7029"\n" "auto b,s,r,n,a,m,t,f,i,u"
7030"\n" "b=ibase"
7031"\n" "ibase=A"
7032"\n" "n=1"
7033"\n" "if(x<0){"
7034"\n" "n=-1"
7035"\n" "x=-x"
7036"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007037"\n" "if(scale<65){"
7038"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7039"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007040"\n" "}"
7041"\n" "s=scale"
7042"\n" "if(x>.2){"
7043"\n" "scale+=5"
7044"\n" "a=a(.2)"
7045"\n" "}"
7046"\n" "scale=s+3"
7047"\n" "while(x>.2){"
7048"\n" "m+=1"
7049"\n" "x=(x-.2)/(1+.2*x)"
7050"\n" "}"
7051"\n" "r=u=x"
7052"\n" "f=-x*x"
7053"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007054"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007055"\n" "u*=f"
7056"\n" "t=u/i"
7057"\n" "r+=t"
7058"\n" "}"
7059"\n" "scale=s"
7060"\n" "ibase=b"
7061"\n" "return((m*a+r)/n)"
7062"\n" "}"
7063"\n" "define j(n,x){"
7064"\n" "auto b,s,o,a,i,v,f"
7065"\n" "b=ibase"
7066"\n" "ibase=A"
7067"\n" "s=scale"
7068"\n" "scale=0"
7069"\n" "n/=1"
7070"\n" "if(n<0){"
7071"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007072"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007073"\n" "}"
7074"\n" "a=1"
7075"\n" "for(i=2;i<=n;++i)a*=i"
7076"\n" "scale=1.5*s"
7077"\n" "a=(x^n)/2^n/a"
7078"\n" "r=v=1"
7079"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007080"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007081"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007082"\n" "v=v*f/i/(n+i)"
7083"\n" "r+=v"
7084"\n" "}"
7085"\n" "scale=s"
7086"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007087"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007088"\n" "return(a*r/1)"
7089"\n" "}"
7090};
7091#endif // ENABLE_BC
7092
Denys Vlasenko19f11072018-12-12 22:48:19 +01007093static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007094{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007095 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007096 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007097 size_t i;
7098
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007099#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007100 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007101 // We know that internal library is not buggy,
7102 // thus error checking is normally disabled.
7103# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007104 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007105 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007106 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007107 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007108#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007109
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007110 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007111 fname = (void*)G.files.v;
7112 for (i = 0; i < G.files.len; i++) {
7113 s = zbc_vm_file(*fname++);
7114 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7115 // Debug config, non-interactive mode:
7116 // return all the way back to main.
7117 // Non-debug builds do not come here
7118 // in non-interactive mode, they exit.
7119 RETURN_STATUS(s);
7120 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007121 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007122
Denys Vlasenko91cde952018-12-10 20:56:08 +01007123 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007124 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007125
Denys Vlasenko19f11072018-12-12 22:48:19 +01007126 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007127}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007128#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007129
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007130#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007131static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007132{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007133 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007134 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007135 bc_vec_free(&G.prog.vars);
7136 bc_vec_free(&G.prog.var_map);
7137 bc_vec_free(&G.prog.arrs);
7138 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007139 IF_DC(bc_vec_free(&G.prog.strs);)
7140 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007141 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007142 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007143 IF_BC(bc_num_free(&G.prog.last);)
7144 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007145 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007146 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007147}
7148
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007149static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007150{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007151 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007152 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007153 bc_parse_free(&G.prs);
7154 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007155}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007156#endif
7157
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007158static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007159{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007160 BcInstPtr ip;
7161
Denys Vlasenko82269122018-12-14 16:30:56 +01007162 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007163 memset(&ip, 0, sizeof(BcInstPtr));
7164
Denys Vlasenko82269122018-12-14 16:30:56 +01007165 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007166 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007167 G.prog.ob_t = 10;
7168
Denys Vlasenko503faf92018-12-20 16:24:18 +01007169 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7170 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007171
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007172 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007173 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007174
Denys Vlasenko503faf92018-12-20 16:24:18 +01007175 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7176 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007177
7178 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007179 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007180
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007181 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007182 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007183 // a valid function name (and be short)
7184 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007185 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007186 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007187 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007188 bc_program_add_fn();
7189 bc_program_add_fn();
7190 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007191
7192 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007193 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007194
7195 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007196 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007197
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007198 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7199 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007200 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007201 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7202 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007203
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007204 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007205}
Gavin Howard01055ba2018-11-03 11:00:21 -06007206
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007207static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007208{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007209#if ENABLE_FEATURE_EDITING
7210 G.line_input_state = new_line_input_t(DO_HISTORY);
7211#endif
7212 G.prog.len = bc_vm_envLen(env_len);
7213
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007214 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007215 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007216 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007217 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007218
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007219//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7220//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007221 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007222#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007223 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007224 // With SA_RESTART, most system calls will restart
7225 // (IOW: they won't fail with EINTR).
7226 // In particular, this means ^C won't cause
7227 // stdout to get into "error state" if SIGINT hits
7228 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007229 //
7230 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007231 // will only be handled after [Enter] since read()
7232 // from stdin is not interrupted by ^C either,
7233 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007234 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007235 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7236
7237 // Without SA_RESTART, this exhibits a bug:
7238 // "while (1) print 1" and try ^C-ing it.
7239 // Intermittently, instead of returning to input line,
7240 // you'll get "output error: Interrupted system call"
7241 // and exit.
7242 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007243#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007244 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007245 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007246 return 0; // "not a tty"
7247}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007248
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007249static BcStatus bc_vm_run(void)
7250{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007251 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007252#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007253 if (G_exiting) // it was actually "halt" or "quit"
7254 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007255 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007256# if ENABLE_FEATURE_EDITING
7257 free_line_input_t(G.line_input_state);
7258# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007259 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007260#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007261 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007262 return st;
7263}
7264
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007265#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007266int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007267int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007268{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007269 int is_tty;
7270
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007271 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007272
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007273 is_tty = bc_vm_init("BC_LINE_LENGTH");
7274
7275 bc_args(argv);
7276
7277 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7278 bc_vm_info();
7279
7280 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007281}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007282#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007283
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007284#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007285int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007286int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007287{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007288 int noscript;
7289
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007290 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007291
7292 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7293 // 1 char wider than bc from the same package.
7294 // Both default width, and xC_LINE_LENGTH=N are wider:
7295 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007296 // |1234\ |
7297 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007298 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007299 // |123\ |
7300 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007301 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007302 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007303
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007304 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7305 noscript = BC_FLAG_I;
7306 for (;;) {
7307 int n = getopt(argc, argv, "e:f:x");
7308 if (n <= 0)
7309 break;
7310 switch (n) {
7311 case 'e':
7312 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007313 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007314 if (n) return n;
7315 break;
7316 case 'f':
7317 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007318 n = zbc_vm_file(optarg);
7319 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007320 break;
7321 case 'x':
7322 option_mask32 |= DC_FLAG_X;
7323 break;
7324 default:
7325 bb_show_usage();
7326 }
7327 }
7328 argv += optind;
7329
7330 while (*argv) {
7331 noscript = 0;
7332 bc_vec_push(&G.files, argv++);
7333 }
7334
7335 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7336
7337 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007338}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007339#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007340
7341#endif // not DC_SMALL