blob: 6463817bb16f67a31bbdeb3475dcdd0af2285ca1 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Gavin Howardfa495ce2018-12-18 10:03:14 -07004 * Adapted from https://github.com/gavinhoward/bc
5 * Original code copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06006 */
7//config:config BC
Denys Vlasenkob097a842018-12-28 03:20:17 +01008//config: bool "bc (45 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -06009//config: default y
10//config: help
11//config: bc is a command-line, arbitrary-precision calculator with a
12//config: Turing-complete language. See the GNU bc manual
13//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010014//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060015//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010016//config: This bc has five differences to the GNU bc:
17//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060018//config: 2) Arrays are copied before being passed as arguments to
19//config: functions. This behavior is required by the bc spec.
20//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010021//config: the number of elements in the array. This prints "1":
22//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060023//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010024//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060025//config: allows POSIX-compliant scripts to work while somewhat
26//config: preserving expected behavior (versus C) and making parsing
27//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010028//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060029//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config:config DC
Denys Vlasenkob097a842018-12-28 03:20:17 +010031//config: bool "dc (36 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060032//config: default y
33//config: help
34//config: dc is a reverse-polish notation command-line calculator which
35//config: supports unlimited precision arithmetic. See the FreeBSD man page
36//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010037//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060038//config:
39//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010040//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010042//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060043//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010044//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060045//config: 4) Like the FreeBSD dc, this dc supports extended registers.
46//config: However, they are implemented differently. When it encounters
47//config: whitespace where a register should be, it skips the whitespace.
48//config: If the character following is not a lowercase letter, an error
49//config: is issued. Otherwise, the register name is parsed by the
Denys Vlasenko14767602018-12-27 22:52:13 +010050//config: following regex: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060051//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: whitespace. Examples:
53//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060054//config: Also note that, like the FreeBSD dc, extended registers are not
55//config: allowed unless the "-x" option is given.
56//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010057//config:if BC || DC # for menuconfig indenting
58//config:
59//config:config FEATURE_DC_BIG
60//config: bool "Use bc code base for dc (larger, more features)"
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010061//config: depends on DC && !BC
Denys Vlasenko14767602018-12-27 22:52:13 +010062//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010063//config:
64//config:config FEATURE_DC_LIBM
65//config: bool "Enable power and exp functions (requires libm)"
66//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010067//config: depends on DC && !BC && !FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010068//config: help
69//config: Enable power and exp functions.
70//config: NOTE: This will require libm to be present for linking.
71//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010072//config:config FEATURE_BC_INTERACTIVE
Denys Vlasenko89e785a2018-12-13 16:35:52 +010073//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060074//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010075//config: depends on BC || (DC && FEATURE_DC_BIG)
Gavin Howard01055ba2018-11-03 11:00:21 -060076//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010077//config: Enable interactive mode: when started on a tty,
78//config: ^C interrupts execution and returns to command line,
79//config: errors also return to command line instead of exiting,
80//config: line editing with history is available.
81//config:
82//config: With this option off, input can still be taken from tty,
83//config: but all errors are fatal, ^C is fatal,
84//config: tty is treated exactly the same as any other
85//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060086//config:
87//config:config FEATURE_BC_LONG_OPTIONS
88//config: bool "Enable bc/dc long options"
89//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010090//config: depends on BC || (DC && FEATURE_DC_BIG)
91//config:
92//config:endif
Gavin Howard01055ba2018-11-03 11:00:21 -060093
94//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
95//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
96
97//kbuild:lib-$(CONFIG_BC) += bc.o
98//kbuild:lib-$(CONFIG_DC) += bc.o
99
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100100//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600101//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100102//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600103//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100104//usage:#define bc_full_usage "\n"
105//usage: "\nArbitrary precision calculator"
106//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100107///////: "\n -i Interactive" - has no effect for now
108//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100109//usage: "\n -l Load standard math library"
110//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//usage: "\n -w Warn if extensions are used"
112///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100113//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100114//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600115//usage:
116//usage:#define bc_example_usage
117//usage: "3 + 4.129\n"
118//usage: "1903 - 2893\n"
119//usage: "-129 * 213.28935\n"
120//usage: "12 / -1932\n"
121//usage: "12 % 12\n"
122//usage: "34 ^ 189\n"
123//usage: "scale = 13\n"
124//usage: "ibase = 2\n"
125//usage: "obase = A\n"
126//usage:
127//usage:#define dc_trivial_usage
Denys Vlasenko14767602018-12-27 22:52:13 +0100128//usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600129//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100130//usage:#define dc_full_usage "\n"
131//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko14767602018-12-27 22:52:13 +0100132//usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100133//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100134//usage: "\nf - print entire stack"
135//usage: "\nk - pop the value and set the precision"
136//usage: "\ni - pop the value and set input radix"
137//usage: "\no - pop the value and set output radix"
138//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600139//usage:
140//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100141//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600142//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100143//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600144//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100145//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600146//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100147//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600148//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100149//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage: "64\n"
151
152#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100153#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600154
Denys Vlasenko14767602018-12-27 22:52:13 +0100155#if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100156# include "dc.c"
157#else
158
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100159#define DEBUG_LEXER 0
160#define DEBUG_COMPILE 0
161#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100162// This can be left enabled for production as well:
163#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100164
165#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100166static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100167#define dbg_lex(...) \
168 do { \
169 fprintf(stderr, "%*s", lex_indent, ""); \
170 bb_error_msg(__VA_ARGS__); \
171 } while (0)
172#define dbg_lex_enter(...) \
173 do { \
174 dbg_lex(__VA_ARGS__); \
175 lex_indent++; \
176 } while (0)
177#define dbg_lex_done(...) \
178 do { \
179 lex_indent--; \
180 dbg_lex(__VA_ARGS__); \
181 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100182#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100183# define dbg_lex(...) ((void)0)
184# define dbg_lex_enter(...) ((void)0)
185# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100186#endif
187
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100188#if DEBUG_COMPILE
189# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
190#else
191# define dbg_compile(...) ((void)0)
192#endif
193
Denys Vlasenko99b37622018-12-15 20:06:59 +0100194#if DEBUG_EXEC
195# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
196#else
197# define dbg_exec(...) ((void)0)
198#endif
199
Gavin Howard01055ba2018-11-03 11:00:21 -0600200typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100201 BC_STATUS_SUCCESS = 0,
202 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100203 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600204} BcStatus;
205
Gavin Howard01055ba2018-11-03 11:00:21 -0600206#define BC_VEC_INVALID_IDX ((size_t) -1)
207#define BC_VEC_START_CAP (1 << 5)
208
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100209typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600210
211typedef struct BcVec {
212 char *v;
213 size_t len;
214 size_t cap;
215 size_t size;
216 BcVecFree dtor;
217} BcVec;
218
Gavin Howard01055ba2018-11-03 11:00:21 -0600219typedef signed char BcDig;
220
221typedef struct BcNum {
222 BcDig *restrict num;
223 size_t rdx;
224 size_t len;
225 size_t cap;
226 bool neg;
227} BcNum;
228
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100229#define BC_NUM_MAX_IBASE ((unsigned long) 16)
230// larger value might speed up BIGNUM calculations a bit:
231#define BC_NUM_DEF_SIZE (16)
232#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600233
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100234#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600235
Gavin Howard01055ba2018-11-03 11:00:21 -0600236typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100237#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600238 BC_INST_INC_PRE,
239 BC_INST_DEC_PRE,
240 BC_INST_INC_POST,
241 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100242#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100243 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100245 XC_INST_REL_EQ, // should
246 XC_INST_REL_LE, // match
247 XC_INST_REL_GE, // LEX
248 XC_INST_REL_NE, // constants
249 XC_INST_REL_LT, // for
250 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600251
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100252 XC_INST_POWER, // operations
253 XC_INST_MULTIPLY, // |
254 XC_INST_DIVIDE, // |
255 XC_INST_MODULUS, // |
256 XC_INST_PLUS, // |
257 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600258
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100259 XC_INST_BOOL_NOT, // |
260 XC_INST_BOOL_OR, // |
261 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100262#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100263 BC_INST_ASSIGN_POWER, // |
264 BC_INST_ASSIGN_MULTIPLY,// |
265 BC_INST_ASSIGN_DIVIDE, // |
266 BC_INST_ASSIGN_MODULUS, // |
267 BC_INST_ASSIGN_PLUS, // |
268 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100269#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100270 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600271
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100272 XC_INST_NUM,
273 XC_INST_VAR,
274 XC_INST_ARRAY_ELEM,
275 XC_INST_ARRAY,
276 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100277
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100278 XC_INST_IBASE, // order of these constans should match other enums
279 XC_INST_OBASE, // order of these constans should match other enums
280 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100281 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100282 XC_INST_LENGTH,
283 XC_INST_READ,
284 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600285
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100286 XC_INST_PRINT,
287 XC_INST_PRINT_POP,
288 XC_INST_STR,
289 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600290
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100291#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100292 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600293 BC_INST_JUMP,
294 BC_INST_JUMP_ZERO,
295
296 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600297 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100298#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100299 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600300
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100301 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100302#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100303 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600304
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100305 DC_INST_MODEXP,
306 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600307
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100308 DC_INST_EXECUTE,
309 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600310
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100311 DC_INST_ASCIIFY,
312 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600313
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100314 DC_INST_PRINT_STACK,
315 DC_INST_CLEAR_STACK,
316 DC_INST_STACK_LEN,
317 DC_INST_DUPLICATE,
318 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600319
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100320 DC_INST_LOAD,
321 DC_INST_PUSH_VAR,
322 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600323
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100324 DC_INST_QUIT,
325 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600326
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100327 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100328#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600329} BcInst;
330
331typedef struct BcId {
332 char *name;
333 size_t idx;
334} BcId;
335
336typedef struct BcFunc {
337 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100338 IF_BC(BcVec labels;)
339 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100340 IF_BC(BcVec strs;)
341 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100342 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600343} BcFunc;
344
345typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100346 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600347
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100348 XC_RESULT_VAR,
349 XC_RESULT_ARRAY_ELEM,
350 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600351
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100352 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600353
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100354 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
355 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
356 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
357 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100358 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100359 XC_RESULT_CONSTANT,
360 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600361} BcResultType;
362
363typedef union BcResultData {
364 BcNum n;
365 BcVec v;
366 BcId id;
367} BcResultData;
368
369typedef struct BcResult {
370 BcResultType t;
371 BcResultData d;
372} BcResult;
373
374typedef struct BcInstPtr {
375 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100376 size_t inst_idx;
377 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600378} BcInstPtr;
379
Gavin Howard01055ba2018-11-03 11:00:21 -0600380typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100381 XC_LEX_EOF,
382 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600383
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100384 XC_LEX_NLINE,
385 XC_LEX_WHITESPACE,
386 XC_LEX_STR,
387 XC_LEX_NAME,
388 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100389
Denys Vlasenko69560f42018-12-24 14:14:23 +0100390 XC_LEX_1st_op,
391 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100392
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100393 XC_LEX_OP_REL_EQ, // should
394 XC_LEX_OP_REL_LE, // match
395 XC_LEX_OP_REL_GE, // INST
396 XC_LEX_OP_REL_NE, // constants
397 XC_LEX_OP_REL_LT, // for
398 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100399
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100400 XC_LEX_OP_POWER, // operations
401 XC_LEX_OP_MULTIPLY, // |
402 XC_LEX_OP_DIVIDE, // |
403 XC_LEX_OP_MODULUS, // |
404 XC_LEX_OP_PLUS, // |
405 XC_LEX_OP_MINUS, // |
406 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100407#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100408 BC_LEX_OP_BOOL_NOT, // |
409 BC_LEX_OP_BOOL_OR, // |
410 BC_LEX_OP_BOOL_AND, // |
411
412 BC_LEX_OP_ASSIGN_POWER, // |
413 BC_LEX_OP_ASSIGN_MULTIPLY, // |
414 BC_LEX_OP_ASSIGN_DIVIDE, // |
415 BC_LEX_OP_ASSIGN_MODULUS, // |
416 BC_LEX_OP_ASSIGN_PLUS, // |
417 BC_LEX_OP_ASSIGN_MINUS, // |
418
419 BC_LEX_OP_ASSIGN, // V
420
Gavin Howard01055ba2018-11-03 11:00:21 -0600421 BC_LEX_OP_INC,
422 BC_LEX_OP_DEC,
423
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100424 BC_LEX_LPAREN, // () are 0x28 and 0x29
425 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600426
Denys Vlasenko10bde142018-12-27 18:23:58 +0100427 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600428 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100429 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600430
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100431 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100433 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600434
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100435 BC_LEX_KEY_1st_keyword,
436 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600437 BC_LEX_KEY_BREAK,
438 BC_LEX_KEY_CONTINUE,
439 BC_LEX_KEY_DEFINE,
440 BC_LEX_KEY_ELSE,
441 BC_LEX_KEY_FOR,
442 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100443 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
444 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
445 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600446 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100447 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600448 BC_LEX_KEY_LENGTH,
449 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600450 BC_LEX_KEY_PRINT,
451 BC_LEX_KEY_QUIT,
452 BC_LEX_KEY_READ,
453 BC_LEX_KEY_RETURN,
454 BC_LEX_KEY_SCALE,
455 BC_LEX_KEY_SQRT,
456 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100457#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600458
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100459#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100460 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
461 DC_LEX_OP_ASSIGN,
462
463 DC_LEX_LPAREN,
464 DC_LEX_SCOLON,
465 DC_LEX_READ,
466 DC_LEX_IBASE,
467 DC_LEX_SCALE,
468 DC_LEX_OBASE,
469 DC_LEX_LENGTH,
470 DC_LEX_PRINT,
471 DC_LEX_QUIT,
472 DC_LEX_SQRT,
473 DC_LEX_LBRACE,
474
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100475 DC_LEX_EQ_NO_REG,
476 DC_LEX_OP_MODEXP,
477 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600478
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100479 DC_LEX_COLON,
480 DC_LEX_ELSE,
481 DC_LEX_EXECUTE,
482 DC_LEX_PRINT_STACK,
483 DC_LEX_CLEAR_STACK,
484 DC_LEX_STACK_LEVEL,
485 DC_LEX_DUPLICATE,
486 DC_LEX_SWAP,
487 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600488
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100489 DC_LEX_ASCIIFY,
490 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600491
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100492 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
493 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
494 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
495 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
496 DC_LEX_LOAD,
497 DC_LEX_LOAD_POP,
498 DC_LEX_STORE_PUSH,
499 DC_LEX_PRINT_POP,
500 DC_LEX_NQUIT,
501 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100502#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600503} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100504// must match order of BC_LEX_KEY_foo etc above
505#if ENABLE_BC
506struct BcLexKeyword {
507 char name8[8];
508};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100509#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100510 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100511static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100512 LEX_KW_ENTRY("auto" , 1), // 0
513 LEX_KW_ENTRY("break" , 1), // 1
514 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
515 LEX_KW_ENTRY("define" , 1), // 3
516 LEX_KW_ENTRY("else" , 0), // 4
517 LEX_KW_ENTRY("for" , 1), // 5
518 LEX_KW_ENTRY("halt" , 0), // 6
519 LEX_KW_ENTRY("ibase" , 1), // 7
520 LEX_KW_ENTRY("obase" , 1), // 8
521 LEX_KW_ENTRY("if" , 1), // 9
522 LEX_KW_ENTRY("last" , 0), // 10
523 LEX_KW_ENTRY("length" , 1), // 11
524 LEX_KW_ENTRY("limits" , 0), // 12
525 LEX_KW_ENTRY("print" , 0), // 13
526 LEX_KW_ENTRY("quit" , 1), // 14
527 LEX_KW_ENTRY("read" , 0), // 15
528 LEX_KW_ENTRY("return" , 1), // 16
529 LEX_KW_ENTRY("scale" , 1), // 17
530 LEX_KW_ENTRY("sqrt" , 1), // 18
531 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100532};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100533#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100534#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100535#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100536#define STRING_if (bc_lex_kws[9].name8)
537#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100538enum {
539 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100540 | (1 << 0) // 0
541 | (1 << 1) // 1
542 | (0 << 2) // 2
543 | (1 << 3) // 3
544 | (0 << 4) // 4
545 | (1 << 5) // 5
546 | (0 << 6) // 6
547 | (1 << 7) // 7
548 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100549 | (1 << 9) // 9
550 | (0 << 10) // 10
551 | (1 << 11) // 11
552 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100553 | (0 << 13) // 13
554 | (1 << 14) // 14
555 | (0 << 15) // 15
556 | (1 << 16) // 16
557 | (1 << 17) // 17
558 | (1 << 18) // 18
559 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100560};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100561#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100562
563// This is a bit array that corresponds to token types. An entry is
564// true if the token is valid in an expression, false otherwise.
565// Used to figure out when expr parsing should stop *without error message*
566// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100567// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100568// by later processing.
569enum {
570#define EXBITS(a,b,c,d,e,f,g,h) \
571 ((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 +0100572 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100573 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100574 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
575 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100576 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
577 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100578 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
579 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
580 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100581#undef EXBITS
582};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100583static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100584{
585#if ULONG_MAX > 0xffffffff
586 // 64-bit version (will not work correctly for 32-bit longs!)
587 return BC_PARSE_EXPRS_BITS & (1UL << i);
588#else
589 // 32-bit version
590 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
591 if (i >= 32) {
592 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
593 i &= 31;
594 }
595 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100596#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100597}
598
599// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100600// [XC_LEX_1st_op...] token types.
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100601static const uint8_t bc_parse_ops[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100602#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100603 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100604 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100605 OP(2, false), // pow
606 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
607 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100608 OP(1, false), // not
609 OP(7, true ), OP( 7, true ), // or and
610 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
611 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100612 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100613#undef OP
614};
615#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
616#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
617#endif // ENABLE_BC
618
619#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100620static const //BcLexType - should be this type
621uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100622dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100623 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100624 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100625 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100626 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100627 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100628 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100629 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100630 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
631 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
632 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100633 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100634 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
635 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100636 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100637 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
638 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100639 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100640 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
641 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100642 // QRSTUVWXY
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100643 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
644 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100645 // Z[\]
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100646 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100647 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100648 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100649 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100650 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
651 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100652 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100653 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100654 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100655 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100656 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
657 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100658 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100659 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100660 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100661 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100662};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100663static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100664int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100665dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
666 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
667 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
668 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100669 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
670 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
671 XC_INST_REL_GT, // DC_LEX_LPAREN
672 DC_INST_INVALID, // DC_LEX_SCOLON
673 DC_INST_INVALID, // DC_LEX_READ
674 XC_INST_IBASE, // DC_LEX_IBASE
675 XC_INST_SCALE, // DC_LEX_SCALE
676 XC_INST_OBASE, // DC_LEX_OBASE
677 XC_INST_LENGTH, // DC_LEX_LENGTH
678 XC_INST_PRINT, // DC_LEX_PRINT
679 DC_INST_QUIT, // DC_LEX_QUIT
680 XC_INST_SQRT, // DC_LEX_SQRT
681 XC_INST_REL_GE, // DC_LEX_LBRACE
682 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100683 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
684 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
685 DC_INST_EXECUTE, // DC_LEX_EXECUTE
686 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
687 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
688 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
689 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
690 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
691 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
692 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
693 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
694 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100695 // DC_INST_INVALID in this table either means that corresponding LEX
696 // is not possible for dc, or that it does not compile one-to-one
697 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100698};
699#endif // ENABLE_DC
700
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100701typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100702 smallint lex; // was BcLexType // first member is most used
703 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100704 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100705 const char *lex_inbuf;
706 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100707 const char *lex_filename;
708 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100709 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100710
Gavin Howard01055ba2018-11-03 11:00:21 -0600711 BcFunc *func;
712 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100713 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100714 IF_BC(BcVec exits;)
715 IF_BC(BcVec conds;)
716 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600717} BcParse;
718
Gavin Howard01055ba2018-11-03 11:00:21 -0600719typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600720 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100721 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600722
Denys Vlasenko503faf92018-12-20 16:24:18 +0100723 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600724 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600725 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600726
Gavin Howard01055ba2018-11-03 11:00:21 -0600727 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100728 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600729
730 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100731 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600732
733 BcVec vars;
734 BcVec var_map;
735
736 BcVec arrs;
737 BcVec arr_map;
738
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100739 IF_DC(BcVec strs;)
740 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600741
Gavin Howard01055ba2018-11-03 11:00:21 -0600742 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100743 IF_BC(BcNum one;)
744 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600745} BcProgram;
746
Denys Vlasenko6e618232018-12-25 22:20:14 +0100747struct globals {
748 BcParse prs; // first member is most used
749
750 // For error messages. Can be set to current parsed line,
751 // or [TODO] to current executing line (can be before last parsed one)
752 unsigned err_line;
753
754 BcVec input_buffer;
755
Denys Vlasenko14767602018-12-27 22:52:13 +0100756 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100757 IF_FEATURE_CLEAN_UP(smallint exiting;)
758
759 BcProgram prog;
760
761 BcVec files;
762
763 char *env_args;
764
765#if ENABLE_FEATURE_EDITING
766 line_input_t *line_input_state;
767#endif
768} FIX_ALIASING;
769#define G (*ptr_to_globals)
770#define INIT_G() do { \
771 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
772} while (0)
773#define FREE_G() do { \
774 FREE_PTR_TO_GLOBALS(); \
775} while (0)
776#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
777#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
778#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100779#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100780# define G_interrupt bb_got_signal
781# define G_ttyin G.ttyin
782#else
783# define G_interrupt 0
784# define G_ttyin 0
785#endif
786#if ENABLE_FEATURE_CLEAN_UP
787# define G_exiting G.exiting
788#else
789# define G_exiting 0
790#endif
791#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
792#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
793
Denys Vlasenko6e618232018-12-25 22:20:14 +0100794#if ENABLE_BC
795# define BC_PARSE_REL (1 << 0)
796# define BC_PARSE_PRINT (1 << 1)
797# define BC_PARSE_ARRAY (1 << 2)
798# define BC_PARSE_NOCALL (1 << 3)
799#endif
800
Gavin Howard01055ba2018-11-03 11:00:21 -0600801#define BC_PROG_MAIN (0)
802#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100803#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600804#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100805#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600806
807#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
808#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100809 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600810
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100811#define BC_FLAG_W (1 << 0)
812#define BC_FLAG_V (1 << 1)
813#define BC_FLAG_S (1 << 2)
814#define BC_FLAG_Q (1 << 3)
815#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100816#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
817#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600818
819#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
820#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
821
Denys Vlasenko64074a12018-12-07 15:50:14 +0100822#define BC_MAX_OBASE ((unsigned) 999)
823#define BC_MAX_DIM ((unsigned) INT_MAX)
824#define BC_MAX_SCALE ((unsigned) UINT_MAX)
825#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
826#define BC_MAX_NUM BC_MAX_STRING
827// Unused apart from "limits" message. Just show a "biggish number" there.
828//#define BC_MAX_NAME BC_MAX_STRING
829//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
830//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
831#define BC_MAX_NAME_STR "999999999"
832#define BC_MAX_EXP_STR "999999999"
833#define BC_MAX_VARS_STR "999999999"
834
835#define BC_MAX_OBASE_STR "999"
836
837#if INT_MAX == 2147483647
838# define BC_MAX_DIM_STR "2147483647"
839#elif INT_MAX == 9223372036854775807
840# define BC_MAX_DIM_STR "9223372036854775807"
841#else
842# error Strange INT_MAX
843#endif
844
845#if UINT_MAX == 4294967295
846# define BC_MAX_SCALE_STR "4294967295"
847# define BC_MAX_STRING_STR "4294967294"
848#elif UINT_MAX == 18446744073709551615
849# define BC_MAX_SCALE_STR "18446744073709551615"
850# define BC_MAX_STRING_STR "18446744073709551614"
851#else
852# error Strange UINT_MAX
853#endif
854#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600855
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100856// In configurations where errors abort instead of propagating error
857// return code up the call chain, functions returning BC_STATUS
858// actually don't return anything, they always succeed and return "void".
859// A macro wrapper is provided, which makes this statement work:
860// s = zbc_func(...)
861// and makes it visible to the compiler that s is always zero,
862// allowing compiler to optimize dead code after the statement.
863//
864// To make code more readable, each such function has a "z"
865// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
866//
Denys Vlasenko14767602018-12-27 22:52:13 +0100867#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100868# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100869# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100870# define IF_ERROR_RETURN_POSSIBLE(a) a
871# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100872# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100873# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100874#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100875# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100876# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100877# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
878# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100879# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100880# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100881#endif
882
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100883//
884// Utility routines
885//
Gavin Howard01055ba2018-11-03 11:00:21 -0600886
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100887static void fflush_and_check(void)
888{
889 fflush_all();
890 if (ferror(stdout) || ferror(stderr))
891 bb_perror_msg_and_die("output error");
892}
893
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100894#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100895#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100896do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100897 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100898 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100899 return BC_STATUS_FAILURE; \
900} while (0)
901#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100902static void quit(void) NORETURN;
903static void quit(void)
904{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100905 if (ferror(stdin))
906 bb_perror_msg_and_die("input error");
907 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100908 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100909 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100910}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100911#define QUIT_OR_RETURN_TO_MAIN quit()
912#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100913
Denys Vlasenko5318f812018-12-05 17:48:01 +0100914static void bc_verror_msg(const char *fmt, va_list p)
915{
Denys Vlasenko82269122018-12-14 16:30:56 +0100916 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100917 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100918 sv = applet_name;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100919 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100920 }
921 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100922 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100923 free((char*)applet_name);
924 applet_name = sv;
925 }
926}
927
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100928static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100929{
930 va_list p;
931
932 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100933 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100934 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100935
Denys Vlasenkoec603182018-12-17 10:34:02 +0100936 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
937 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
938 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100939}
940
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100941#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100942static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100943{
944 va_list p;
945
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100946 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100947 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100948 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100949
950 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100951 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100952 va_end(p);
953
954 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100955 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100956 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
957
Denys Vlasenkoec603182018-12-17 10:34:02 +0100958 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100959 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100960 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100961}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100962#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100963#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100964
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100965// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
966// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100967// function must not have caller-cleaned parameters on stack.
968// Unfortunately, vararg function API does exactly that on most arches.
969// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100970static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100971{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100972 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100973}
974static ERRORFUNC int bc_error_bad_character(char c)
975{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100976 if (!c)
977 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100978 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100979}
980static ERRORFUNC int bc_error_bad_expression(void)
981{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100982 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100983}
984static ERRORFUNC int bc_error_bad_token(void)
985{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100986 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100987}
988static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
989{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100990 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100991}
992static ERRORFUNC int bc_error_variable_is_wrong_type(void)
993{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100994 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100995}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100996#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100997static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100998{
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100999 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001000}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001001#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1002static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001003{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001004 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001005}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001006#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1007static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001008{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001009 RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; this is bad:", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001010}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001011#define zbc_POSIX_does_not_allow_bool_ops_this_is_bad(...) (zbc_POSIX_does_not_allow_bool_ops_this_is_bad(__VA_ARGS__) COMMA_SUCCESS)
1012static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001013{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001014 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001015}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001016#define zbc_POSIX_does_not_allow_empty_X_expression_in_for(...) (zbc_POSIX_does_not_allow_empty_X_expression_in_for(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001017#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001018
Gavin Howard01055ba2018-11-03 11:00:21 -06001019static void bc_vec_grow(BcVec *v, size_t n)
1020{
1021 size_t cap = v->cap * 2;
1022 while (cap < v->len + n) cap *= 2;
1023 v->v = xrealloc(v->v, v->size * cap);
1024 v->cap = cap;
1025}
1026
1027static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1028{
1029 v->size = esize;
1030 v->cap = BC_VEC_START_CAP;
1031 v->len = 0;
1032 v->dtor = dtor;
1033 v->v = xmalloc(esize * BC_VEC_START_CAP);
1034}
1035
Denys Vlasenko7d628012018-12-04 21:46:47 +01001036static void bc_char_vec_init(BcVec *v)
1037{
1038 bc_vec_init(v, sizeof(char), NULL);
1039}
1040
Gavin Howard01055ba2018-11-03 11:00:21 -06001041static void bc_vec_expand(BcVec *v, size_t req)
1042{
1043 if (v->cap < req) {
1044 v->v = xrealloc(v->v, v->size * req);
1045 v->cap = req;
1046 }
1047}
1048
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001049static void bc_vec_pop(BcVec *v)
1050{
1051 v->len--;
1052 if (v->dtor)
1053 v->dtor(v->v + (v->size * v->len));
1054}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001055
Gavin Howard01055ba2018-11-03 11:00:21 -06001056static void bc_vec_npop(BcVec *v, size_t n)
1057{
1058 if (!v->dtor)
1059 v->len -= n;
1060 else {
1061 size_t len = v->len - n;
1062 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1063 }
1064}
1065
Denys Vlasenko7d628012018-12-04 21:46:47 +01001066static void bc_vec_pop_all(BcVec *v)
1067{
1068 bc_vec_npop(v, v->len);
1069}
1070
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001071static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001072{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001073 size_t len = v->len;
1074 if (len >= v->cap) bc_vec_grow(v, 1);
1075 memmove(v->v + (v->size * len), data, v->size);
1076 v->len++;
1077 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001078}
1079
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001080// G.prog.results often needs "pop old operand, push result" idiom.
1081// Can do this without a few extra ops
1082static size_t bc_result_pop_and_push(const void *data)
1083{
1084 BcVec *v = &G.prog.results;
1085 char *last;
1086 size_t len = v->len - 1;
1087
1088 last = v->v + (v->size * len);
1089 if (v->dtor)
1090 v->dtor(last);
1091 memmove(last, data, v->size);
1092 return len;
1093}
1094
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001095static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001096{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001097 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001098}
1099
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001100static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001101{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001102 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001103 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001104 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001105}
1106
Gavin Howard01055ba2018-11-03 11:00:21 -06001107static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1108{
1109 if (idx == v->len)
1110 bc_vec_push(v, data);
1111 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001112 char *ptr;
1113
1114 if (v->len == v->cap) bc_vec_grow(v, 1);
1115
1116 ptr = v->v + v->size * idx;
1117
1118 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1119 memmove(ptr, data, v->size);
1120 }
1121}
1122
1123static void bc_vec_string(BcVec *v, size_t len, const char *str)
1124{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001125 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001126 bc_vec_expand(v, len + 1);
1127 memcpy(v->v, str, len);
1128 v->len = len;
1129
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001130 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001131}
1132
Gavin Howard01055ba2018-11-03 11:00:21 -06001133static void *bc_vec_item(const BcVec *v, size_t idx)
1134{
1135 return v->v + v->size * idx;
1136}
1137
1138static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1139{
1140 return v->v + v->size * (v->len - idx - 1);
1141}
1142
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001143static void *bc_vec_top(const BcVec *v)
1144{
1145 return v->v + v->size * (v->len - 1);
1146}
1147
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001148static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001149{
1150 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001151 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001152 free(v->v);
1153}
1154
Denys Vlasenko10bde142018-12-27 18:23:58 +01001155static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001156{
1157 return bc_vec_item(&G.prog.fns, idx);
1158}
1159// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001160#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001161
1162#if ENABLE_BC
1163static BcFunc* bc_program_current_func(void)
1164{
1165 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001166 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001167 return func;
1168}
1169#endif
1170
Denys Vlasenko10bde142018-12-27 18:23:58 +01001171static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001172{
1173#if ENABLE_BC
1174 if (IS_BC) {
1175 BcFunc *func = bc_program_current_func();
1176 return bc_vec_item(&func->strs, idx);
1177 }
1178#endif
1179 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1180}
1181
Denys Vlasenko10bde142018-12-27 18:23:58 +01001182static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001183{
1184#if ENABLE_BC
1185 if (IS_BC) {
1186 BcFunc *func = bc_program_current_func();
1187 return bc_vec_item(&func->consts, idx);
1188 }
1189#endif
1190 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1191}
1192
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001193static int bc_id_cmp(const void *e1, const void *e2)
1194{
1195 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1196}
1197
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001198static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001199{
1200 free(((BcId *) id)->name);
1201}
1202
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001203static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001204{
1205 size_t low = 0, high = v->len;
1206
1207 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001208 size_t mid = (low + high) / 2;
1209 BcId *id = bc_vec_item(v, mid);
1210 int result = bc_id_cmp(ptr, id);
1211
1212 if (result == 0)
1213 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001214 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001215 high = mid;
1216 else
1217 low = mid + 1;
1218 }
1219
1220 return low;
1221}
1222
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001223static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001224{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001225 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001226
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001227 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001228 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001229 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1230 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001231 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001232 bc_vec_pushAt(v, ptr, n);
1233 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001234}
1235
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001236#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001237static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001238{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001239 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001240 if (i >= v->len) return BC_VEC_INVALID_IDX;
1241 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1242}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001243#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001244
Gavin Howard01055ba2018-11-03 11:00:21 -06001245static void bc_num_setToZero(BcNum *n, size_t scale)
1246{
1247 n->len = 0;
1248 n->neg = false;
1249 n->rdx = scale;
1250}
1251
1252static void bc_num_zero(BcNum *n)
1253{
1254 bc_num_setToZero(n, 0);
1255}
1256
1257static void bc_num_one(BcNum *n)
1258{
1259 bc_num_setToZero(n, 0);
1260 n->len = 1;
1261 n->num[0] = 1;
1262}
1263
Denys Vlasenko3129f702018-12-09 12:04:44 +01001264// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001265static void bc_num_init(BcNum *n, size_t req)
1266{
1267 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001268 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001269 n->num = xmalloc(req);
1270 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001271 n->rdx = 0;
1272 n->len = 0;
1273 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001274}
1275
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001276static void bc_num_init_DEF_SIZE(BcNum *n)
1277{
1278 bc_num_init(n, BC_NUM_DEF_SIZE);
1279}
1280
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001281static void bc_num_expand(BcNum *n, size_t req)
1282{
1283 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1284 if (req > n->cap) {
1285 n->num = xrealloc(n->num, req);
1286 n->cap = req;
1287 }
1288}
1289
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001290static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001291{
1292 free(((BcNum *) num)->num);
1293}
1294
1295static void bc_num_copy(BcNum *d, BcNum *s)
1296{
1297 if (d != s) {
1298 bc_num_expand(d, s->cap);
1299 d->len = s->len;
1300 d->neg = s->neg;
1301 d->rdx = s->rdx;
1302 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1303 }
1304}
1305
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001306static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001307{
1308 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001309 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001310
Denys Vlasenko1557b762018-12-22 21:37:46 +01001311 result = 0;
1312 i = n->len;
1313 while (i > n->rdx) {
1314 unsigned long prev = result;
1315 result = result * 10 + n->num[--i];
1316 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1317 // 0x1ff00000 * 10 is 0x13f600000,
1318 // or 0x3f600000 truncated to 32 bits. Which is larger.
1319 // However, (N*10)/8 < N check is always correct.
1320 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001321 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001322 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001323 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001324
Denys Vlasenko29301232018-12-11 15:29:32 +01001325 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001326}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001327#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1328
1329static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1330{
1331 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1332
1333 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1334}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001335#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001336
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001337#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1338# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1339#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1340# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1341#endif
1342// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1343// would not hit realloc() code path - not good if num[] is not malloced
1344
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001345static void bc_num_ulong2num(BcNum *n, unsigned long val)
1346{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001348
1349 bc_num_zero(n);
1350
1351 if (val == 0) return;
1352
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001353 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001354
1355 ptr = n->num;
1356 for (;;) {
1357 n->len++;
1358 *ptr++ = val % 10;
1359 val /= 10;
1360 if (val == 0) break;
1361 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362}
1363
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001364static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001365{
1366 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001367 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001368 a[i] -= b[i];
1369 for (j = i; a[j] < 0;) {
1370 a[j++] += 10;
1371 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001372 }
1373 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001374}
1375
1376static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1377{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001378 size_t i = len;
1379 for (;;) {
1380 int c;
1381 if (i == 0)
1382 return 0;
1383 i--;
1384 c = a[i] - b[i];
1385 if (c != 0) {
1386 i++;
1387 if (c < 0)
1388 return -i;
1389 return i;
1390 }
1391 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001392}
1393
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001394#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1395#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1396#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1397//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1398static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1399{
1400 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1401}
1402//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1403static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1404{
1405 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1406}
1407
Gavin Howard01055ba2018-11-03 11:00:21 -06001408static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1409{
1410 size_t i, min, a_int, b_int, diff;
1411 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001412 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001413 ssize_t cmp;
1414
1415 if (a == b) return 0;
1416 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1417 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001418
1419 if (a->neg != b->neg) // signs of a and b differ
1420 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1421 return (int)b->neg - (int)a->neg;
1422 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001423
1424 a_int = BC_NUM_INT(a);
1425 b_int = BC_NUM_INT(b);
1426 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001427
1428 if (a_int != 0) return (ssize_t) a_int;
1429
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001430 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001431 if (a_max) {
1432 min = b->rdx;
1433 diff = a->rdx - b->rdx;
1434 max_num = a->num + diff;
1435 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001436 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1437 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001438 min = a->rdx;
1439 diff = b->rdx - a->rdx;
1440 max_num = b->num + diff;
1441 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001442 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001443 }
1444
1445 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001446 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001447
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001448 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001449 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001450 }
1451
1452 return 0;
1453}
1454
1455static void bc_num_truncate(BcNum *n, size_t places)
1456{
1457 if (places == 0) return;
1458
1459 n->rdx -= places;
1460
1461 if (n->len != 0) {
1462 n->len -= places;
1463 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1464 }
1465}
1466
1467static void bc_num_extend(BcNum *n, size_t places)
1468{
1469 size_t len = n->len + places;
1470
1471 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001472 if (n->cap < len) bc_num_expand(n, len);
1473
1474 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1475 memset(n->num, 0, sizeof(BcDig) * places);
1476
1477 n->len += places;
1478 n->rdx += places;
1479 }
1480}
1481
1482static void bc_num_clean(BcNum *n)
1483{
1484 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1485 if (n->len == 0)
1486 n->neg = false;
1487 else if (n->len < n->rdx)
1488 n->len = n->rdx;
1489}
1490
1491static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1492{
1493 if (n->rdx < scale)
1494 bc_num_extend(n, scale - n->rdx);
1495 else
1496 bc_num_truncate(n, n->rdx - scale);
1497
1498 bc_num_clean(n);
1499 if (n->len != 0) n->neg = !neg1 != !neg2;
1500}
1501
1502static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1503 BcNum *restrict b)
1504{
1505 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001506 b->len = n->len - idx;
1507 a->len = idx;
1508 a->rdx = b->rdx = 0;
1509
1510 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1511 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001512 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001513 bc_num_zero(b);
1514 bc_num_copy(a, n);
1515 }
1516
1517 bc_num_clean(a);
1518 bc_num_clean(b);
1519}
1520
Denys Vlasenko29301232018-12-11 15:29:32 +01001521static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001522{
Denys Vlasenko29301232018-12-11 15:29:32 +01001523 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001524
1525 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1526 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1527 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001528 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001529 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001530
1531 if (n->rdx >= places)
1532 n->rdx -= places;
1533 else {
1534 bc_num_extend(n, places - n->rdx);
1535 n->rdx = 0;
1536 }
1537
1538 bc_num_clean(n);
1539
Denys Vlasenko29301232018-12-11 15:29:32 +01001540 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001541}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001542#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001543
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001544typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1545
1546static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1547 BcNumBinaryOp op, size_t req)
1548{
1549 BcStatus s;
1550 BcNum num2, *ptr_a, *ptr_b;
1551 bool init = false;
1552
1553 if (c == a) {
1554 ptr_a = &num2;
1555 memcpy(ptr_a, c, sizeof(BcNum));
1556 init = true;
1557 } else
1558 ptr_a = a;
1559
1560 if (c == b) {
1561 ptr_b = &num2;
1562 if (c != a) {
1563 memcpy(ptr_b, c, sizeof(BcNum));
1564 init = true;
1565 }
1566 } else
1567 ptr_b = b;
1568
1569 if (init)
1570 bc_num_init(c, req);
1571 else
1572 bc_num_expand(c, req);
1573
1574 s = BC_STATUS_SUCCESS;
1575 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1576
1577 if (init) bc_num_free(&num2);
1578
1579 RETURN_STATUS(s);
1580}
1581#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1582
1583static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1584static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1585static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1586static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1587static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1588static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1589
1590static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1591{
1592 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1593 (void) scale;
1594 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1595}
1596
1597static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1598{
1599 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1600 (void) scale;
1601 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1602}
1603
1604static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1605{
1606 size_t req = BC_NUM_MREQ(a, b, scale);
1607 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1608}
1609
1610static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1611{
1612 size_t req = BC_NUM_MREQ(a, b, scale);
1613 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1614}
1615
1616static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1617{
1618 size_t req = BC_NUM_MREQ(a, b, scale);
1619 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1620}
1621
1622static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1623{
1624 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1625}
1626
Denys Vlasenko10bde142018-12-27 18:23:58 +01001627static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001628 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1629};
1630#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1631#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1632#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1633#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1634#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1635#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1636
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001637static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001638{
1639 BcNum one;
1640 BcDig num[2];
1641
1642 one.cap = 2;
1643 one.num = num;
1644 bc_num_one(&one);
1645
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001646 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001647}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001648#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001649
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001650static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001651{
1652 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1653 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001654 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001655
1656 // Because this function doesn't need to use scale (per the bc spec),
1657 // I am hijacking it to say whether it's doing an add or a subtract.
1658
1659 if (a->len == 0) {
1660 bc_num_copy(c, b);
1661 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001662 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001663 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001664 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001665 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001666 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001667 }
1668
1669 c->neg = a->neg;
1670 c->rdx = BC_MAX(a->rdx, b->rdx);
1671 min_rdx = BC_MIN(a->rdx, b->rdx);
1672 c->len = 0;
1673
1674 if (a->rdx > b->rdx) {
1675 diff = a->rdx - b->rdx;
1676 ptr = a->num;
1677 ptr_a = a->num + diff;
1678 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001679 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001680 diff = b->rdx - a->rdx;
1681 ptr = b->num;
1682 ptr_a = a->num;
1683 ptr_b = b->num + diff;
1684 }
1685
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001686 ptr_c = c->num;
1687 for (i = 0; i < diff; ++i, ++c->len)
1688 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001689
1690 ptr_c += diff;
1691 a_int = BC_NUM_INT(a);
1692 b_int = BC_NUM_INT(b);
1693
1694 if (a_int > b_int) {
1695 min_int = b_int;
1696 max = a_int;
1697 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001698 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001699 min_int = a_int;
1700 max = b_int;
1701 ptr = ptr_b;
1702 }
1703
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001704 carry = 0;
1705 for (i = 0; i < min_rdx + min_int; ++i) {
1706 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001707 carry = in / 10;
1708 ptr_c[i] = (BcDig)(in % 10);
1709 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001710 for (; i < max + min_rdx; ++i) {
1711 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001712 carry = in / 10;
1713 ptr_c[i] = (BcDig)(in % 10);
1714 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001715 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001716
1717 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1718
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001719 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001720}
1721
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001722static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001723{
Gavin Howard01055ba2018-11-03 11:00:21 -06001724 ssize_t cmp;
1725 BcNum *minuend, *subtrahend;
1726 size_t start;
1727 bool aneg, bneg, neg;
1728
1729 // Because this function doesn't need to use scale (per the bc spec),
1730 // I am hijacking it to say whether it's doing an add or a subtract.
1731
1732 if (a->len == 0) {
1733 bc_num_copy(c, b);
1734 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001735 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001736 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001737 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001738 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001739 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001740 }
1741
1742 aneg = a->neg;
1743 bneg = b->neg;
1744 a->neg = b->neg = false;
1745
1746 cmp = bc_num_cmp(a, b);
1747
1748 a->neg = aneg;
1749 b->neg = bneg;
1750
1751 if (cmp == 0) {
1752 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001753 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001754 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001755 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001756 neg = a->neg;
1757 minuend = a;
1758 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001759 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001760 neg = b->neg;
1761 if (sub) neg = !neg;
1762 minuend = b;
1763 subtrahend = a;
1764 }
1765
1766 bc_num_copy(c, minuend);
1767 c->neg = neg;
1768
1769 if (c->rdx < subtrahend->rdx) {
1770 bc_num_extend(c, subtrahend->rdx - c->rdx);
1771 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001772 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001773 start = c->rdx - subtrahend->rdx;
1774
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001775 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001776
1777 bc_num_clean(c);
1778
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001779 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001780}
1781
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001782static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001783 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001784#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001785{
1786 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001787 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001788 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001789 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001790
Gavin Howard01055ba2018-11-03 11:00:21 -06001791 if (a->len == 0 || b->len == 0) {
1792 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001793 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001795 aone = BC_NUM_ONE(a);
1796 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001798 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001799 }
1800
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001801 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1802 || a->len < BC_NUM_KARATSUBA_LEN
1803 || b->len < BC_NUM_KARATSUBA_LEN
1804 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001805 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001806
Gavin Howard01055ba2018-11-03 11:00:21 -06001807 bc_num_expand(c, a->len + b->len + 1);
1808
1809 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001810 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001811
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001812 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001813 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001814 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001815 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001816 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001817 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001818 carry = in / 10;
1819 c->num[i + j] = (BcDig)(in % 10);
1820 }
1821
1822 c->num[i + j] += (BcDig) carry;
1823 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001824
Denys Vlasenko14767602018-12-27 22:52:13 +01001825#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001826 // a=2^1000000
1827 // a*a <- without check below, this will not be interruptible
1828 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001829#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001830 }
1831
1832 c->len = len;
1833
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001834 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001835 }
1836
1837 bc_num_init(&l1, max);
1838 bc_num_init(&h1, max);
1839 bc_num_init(&l2, max);
1840 bc_num_init(&h2, max);
1841 bc_num_init(&m1, max);
1842 bc_num_init(&m2, max);
1843 bc_num_init(&z0, max);
1844 bc_num_init(&z1, max);
1845 bc_num_init(&z2, max);
1846 bc_num_init(&temp, max + max);
1847
1848 bc_num_split(a, max2, &l1, &h1);
1849 bc_num_split(b, max2, &l2, &h2);
1850
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001851 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001852 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001853 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001854 if (s) goto err;
1855
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001856 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001857 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001858 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001859 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001860 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001861 if (s) goto err;
1862
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001863 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001864 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001865 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001866 if (s) goto err;
1867
Denys Vlasenko29301232018-12-11 15:29:32 +01001868 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001869 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001870 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001871 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001872 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001874 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001875 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 bc_num_free(&temp);
1877 bc_num_free(&z2);
1878 bc_num_free(&z1);
1879 bc_num_free(&z0);
1880 bc_num_free(&m2);
1881 bc_num_free(&m1);
1882 bc_num_free(&h2);
1883 bc_num_free(&l2);
1884 bc_num_free(&h1);
1885 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001886 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001887}
1888
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001889static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001890{
1891 BcStatus s;
1892 BcNum cpa, cpb;
1893 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1894
1895 scale = BC_MAX(scale, a->rdx);
1896 scale = BC_MAX(scale, b->rdx);
1897 scale = BC_MIN(a->rdx + b->rdx, scale);
1898 maxrdx = BC_MAX(maxrdx, scale);
1899
1900 bc_num_init(&cpa, a->len);
1901 bc_num_init(&cpb, b->len);
1902
1903 bc_num_copy(&cpa, a);
1904 bc_num_copy(&cpb, b);
1905 cpa.neg = cpb.neg = false;
1906
Denys Vlasenko29301232018-12-11 15:29:32 +01001907 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001908 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001909 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001910 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001911 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001912 if (s) goto err;
1913
1914 maxrdx += scale;
1915 bc_num_expand(c, c->len + maxrdx);
1916
1917 if (c->len < maxrdx) {
1918 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1919 c->len += maxrdx;
1920 }
1921
1922 c->rdx = maxrdx;
1923 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001924 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001925 bc_num_free(&cpb);
1926 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001927 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001928}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001929#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001930
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001931static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001932{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001933 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001934 size_t len, end, i;
1935 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001936
1937 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001938 RETURN_STATUS(bc_error("divide by zero"));
1939 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001940 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001941 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001942 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001943 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001944 bc_num_copy(c, a);
1945 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001946 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001947 }
1948
1949 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1950 bc_num_copy(&cp, a);
1951 len = b->len;
1952
1953 if (len > cp.len) {
1954 bc_num_expand(&cp, len + 2);
1955 bc_num_extend(&cp, len - cp.len);
1956 }
1957
1958 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1959 cp.rdx -= b->rdx;
1960 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1961
1962 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001963 for (;;) {
1964 if (len == 0) break;
1965 len--;
1966 if (b->num[len] != 0)
1967 break;
1968 }
1969 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001970 }
1971
1972 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1973
1974 // We want an extra zero in front to make things simpler.
1975 cp.num[cp.len++] = 0;
1976 end = cp.len - len;
1977
1978 bc_num_expand(c, cp.len);
1979
1980 bc_num_zero(c);
1981 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1982 c->rdx = cp.rdx;
1983 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001984
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001985 s = BC_STATUS_SUCCESS;
1986 for (i = end - 1; i < end; --i) {
1987 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06001988 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001989 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
1990 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001991 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01001992#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01001993 // a=2^100000
1994 // scale=40000
1995 // 1/a <- without check below, this will not be interruptible
1996 if (G_interrupt) {
1997 s = BC_STATUS_FAILURE;
1998 break;
1999 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002000#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002001 }
2002
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002003 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002004 bc_num_free(&cp);
2005
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002006 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002007}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002008#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002009
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002010static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002011 BcNum *restrict d, size_t scale, size_t ts)
2012{
2013 BcStatus s;
2014 BcNum temp;
2015 bool neg;
2016
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002017 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002018 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002019
2020 if (a->len == 0) {
2021 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002022 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002023 }
2024
2025 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002026 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002027 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002028
2029 if (scale != 0) scale = ts;
2030
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002031 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002032 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002033 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002034 if (s) goto err;
2035
2036 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2037
2038 neg = d->neg;
2039 bc_num_retireMul(d, ts, a->neg, b->neg);
2040 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002041 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002042 bc_num_free(&temp);
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_r(...) (zbc_num_r(__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_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002048{
2049 BcStatus s;
2050 BcNum c1;
2051 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2052
2053 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002054 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002055 bc_num_free(&c1);
2056
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002057 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002058}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002059#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002060
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002061static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002062{
2063 BcStatus s = BC_STATUS_SUCCESS;
2064 BcNum copy;
2065 unsigned long pow;
2066 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002067 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002068
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002069 // GNU bc does not allow 2^2.0 - we do
2070 for (i = 0; i < b->rdx; i++)
2071 if (b->num[i] != 0)
2072 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002073
2074 if (b->len == 0) {
2075 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002076 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002077 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002078 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002079 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002080 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002081 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002082 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002083 if (!b->neg)
2084 bc_num_copy(c, a);
2085 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002086 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002087 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002088 }
2089
2090 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002091 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002092 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002093 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002094
2095 bc_num_init(&copy, a->len);
2096 bc_num_copy(&copy, a);
2097
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002098 if (!neg) {
2099 if (a->rdx > scale)
2100 scale = a->rdx;
2101 if (a->rdx * pow < scale)
2102 scale = a->rdx * pow;
2103 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002104
Gavin Howard01055ba2018-11-03 11:00:21 -06002105
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002106 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002107 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002108 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002109 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002110 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002111 //if (G_interrupt) {
2112 // s = BC_STATUS_FAILURE;
2113 // goto err;
2114 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 }
2116
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 bc_num_copy(c, &copy);
2118
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002119 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002121 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 if (s) goto err;
2123
2124 if (pow & 1) {
2125 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002126 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002127 if (s) goto err;
2128 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002129 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002130 //if (G_interrupt) {
2131 // s = BC_STATUS_FAILURE;
2132 // goto err;
2133 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002134 }
2135
2136 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002137 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002138 if (s) goto err;
2139 }
2140
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2142
2143 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002144 for (i = 0; i < c->len; ++i)
2145 if (c->num[i] != 0)
2146 goto skip;
2147 bc_num_setToZero(c, scale);
2148 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002149
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002150 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002151 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002152 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002153}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002154#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002155
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002156static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002157{
2158 BcStatus s;
2159 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2160 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2161 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2162
2163 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2164 bc_num_expand(b, req);
2165
2166 if (a->len == 0) {
2167 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002168 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002169 }
2170 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002171 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002172 }
2173 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002174 bc_num_one(b);
2175 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002176 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002177 }
2178
2179 scale = BC_MAX(scale, a->rdx) + 1;
2180 len = a->len + scale;
2181
2182 bc_num_init(&num1, len);
2183 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002184 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002185
2186 bc_num_one(&half);
2187 half.num[0] = 5;
2188 half.rdx = 1;
2189
2190 bc_num_init(&f, len);
2191 bc_num_init(&fprime, len);
2192
2193 x0 = &num1;
2194 x1 = &num2;
2195
2196 bc_num_one(x0);
2197 pow = BC_NUM_INT(a);
2198
2199 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002200 if (pow & 1)
2201 x0->num[0] = 2;
2202 else
2203 x0->num[0] = 6;
2204
2205 pow -= 2 - (pow & 1);
2206
2207 bc_num_extend(x0, pow);
2208
2209 // Make sure to move the radix back.
2210 x0->rdx -= pow;
2211 }
2212
2213 x0->rdx = digs = digs1 = 0;
2214 resrdx = scale + 2;
2215 len = BC_NUM_INT(x0) + resrdx - 1;
2216
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002217 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002218 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002219 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002220 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002221 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002222 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002223 if (s) goto err;
2224
2225 cmp = bc_num_cmp(x1, x0);
2226 digs = x1->len - (unsigned long long) llabs(cmp);
2227
2228 if (cmp == cmp2 && digs == digs1)
2229 times += 1;
2230 else
2231 times = 0;
2232
2233 resrdx += times > 4;
2234
2235 cmp2 = cmp1;
2236 cmp1 = cmp;
2237 digs1 = digs;
2238
2239 temp = x0;
2240 x0 = x1;
2241 x1 = temp;
2242 }
2243
Gavin Howard01055ba2018-11-03 11:00:21 -06002244 bc_num_copy(b, x0);
2245 scale -= 1;
2246 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002247 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002248 bc_num_free(&fprime);
2249 bc_num_free(&f);
2250 bc_num_free(&half);
2251 bc_num_free(&num2);
2252 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002253 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002254}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002255#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002256
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002257static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002258 size_t scale)
2259{
2260 BcStatus s;
2261 BcNum num2, *ptr_a;
2262 bool init = false;
2263 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2264
2265 if (c == a) {
2266 memcpy(&num2, c, sizeof(BcNum));
2267 ptr_a = &num2;
2268 bc_num_init(c, len);
2269 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002270 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002271 ptr_a = a;
2272 bc_num_expand(c, len);
2273 }
2274
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002275 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002276
2277 if (init) bc_num_free(&num2);
2278
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002279 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002280}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002281#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002282
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002283#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002284static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002285{
2286 BcStatus s;
2287 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002288 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002289
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002290 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002291 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002292 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002293 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002294 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002295 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002296
2297 bc_num_expand(d, c->len);
2298 bc_num_init(&base, c->len);
2299 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002300 bc_num_init(&temp, b->len);
2301
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002302 two.cap = ARRAY_SIZE(two_digs);
2303 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002304 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002305 two_digs[0] = 2;
2306
Gavin Howard01055ba2018-11-03 11:00:21 -06002307 bc_num_one(d);
2308
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002309 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002310 if (s) goto err;
2311 bc_num_copy(&exp, b);
2312
2313 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002314 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002315 if (s) goto err;
2316
2317 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002318 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002319 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002320 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002321 if (s) goto err;
2322 }
2323
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002324 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002325 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002326 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002327 if (s) goto err;
2328 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002329 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002330 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002331 bc_num_free(&exp);
2332 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002333 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002334}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002335#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002336#endif // ENABLE_DC
2337
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002338static FAST_FUNC void bc_string_free(void *string)
2339{
2340 free(*(char**)string);
2341}
2342
Gavin Howard01055ba2018-11-03 11:00:21 -06002343static void bc_func_init(BcFunc *f)
2344{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002345 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002346 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2347 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002348 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2349 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002350 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002351}
2352
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002353static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002354{
2355 BcFunc *f = (BcFunc *) func;
2356 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002357 IF_BC(bc_vec_free(&f->labels);)
2358 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002359 IF_BC(bc_vec_free(&f->strs);)
2360 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002361}
2362
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002363static void bc_array_expand(BcVec *a, size_t len);
2364
Gavin Howard01055ba2018-11-03 11:00:21 -06002365static void bc_array_init(BcVec *a, bool nums)
2366{
2367 if (nums)
2368 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2369 else
2370 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2371 bc_array_expand(a, 1);
2372}
2373
Gavin Howard01055ba2018-11-03 11:00:21 -06002374static void bc_array_expand(BcVec *a, size_t len)
2375{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002376 if (a->dtor == bc_num_free
2377 // && a->size == sizeof(BcNum) - always true
2378 ) {
2379 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002380 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002381 bc_num_init_DEF_SIZE(&n);
2382 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002383 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002384 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002385 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002386 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002387 bc_array_init(&v, true);
2388 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002389 }
2390 }
2391}
2392
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002393static void bc_array_copy(BcVec *d, const BcVec *s)
2394{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002395 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002396 size_t i;
2397
2398 bc_vec_pop_all(d);
2399 bc_vec_expand(d, s->cap);
2400 d->len = s->len;
2401
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002402 dnum = (void*)d->v;
2403 snum = (void*)s->v;
2404 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002405 bc_num_init(dnum, snum->len);
2406 bc_num_copy(dnum, snum);
2407 }
2408}
2409
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002410#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002411static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002412{
2413 d->t = src->t;
2414
2415 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002416 case XC_RESULT_TEMP:
2417 case XC_RESULT_IBASE:
2418 case XC_RESULT_SCALE:
2419 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 bc_num_init(&d->d.n, src->d.n.len);
2421 bc_num_copy(&d->d.n, &src->d.n);
2422 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002423 case XC_RESULT_VAR:
2424 case XC_RESULT_ARRAY:
2425 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002426 d->d.id.name = xstrdup(src->d.id.name);
2427 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002428 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002429 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002430 IF_BC(case BC_RESULT_ONE:)
2431 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002432 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2433 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002434 }
2435}
2436#endif // ENABLE_DC
2437
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002438static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002439{
2440 BcResult *r = (BcResult *) result;
2441
2442 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002443 case XC_RESULT_TEMP:
2444 case XC_RESULT_IBASE:
2445 case XC_RESULT_SCALE:
2446 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002447 bc_num_free(&r->d.n);
2448 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002449 case XC_RESULT_VAR:
2450 case XC_RESULT_ARRAY:
2451 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002452 free(r->d.id.name);
2453 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002454 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002455 // Do nothing.
2456 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002457 }
2458}
2459
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002460static int bad_input_byte(char c)
2461{
2462 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2463 || c > 0x7e
2464 ) {
2465 bc_error_fmt("illegal character 0x%02x", c);
2466 return 1;
2467 }
2468 return 0;
2469}
2470
Denys Vlasenko10bde142018-12-27 18:23:58 +01002471static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002472{
2473 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002474 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002475 fflush_and_check();
2476
Denys Vlasenko14767602018-12-27 22:52:13 +01002477#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002478 if (G_interrupt) { // ^C was pressed
2479 intr:
2480 if (fp != stdin) {
2481 // ^C while running a script (bc SCRIPT): die.
2482 // We do not return to interactive prompt:
2483 // user might be running us from a shell,
2484 // and SCRIPT might be intended to terminate
2485 // (e.g. contain a "halt" stmt).
2486 // ^C dropping user into a bc prompt instead of
2487 // the shell would be unexpected.
2488 xfunc_die();
2489 }
2490 // ^C while interactive input
2491 G_interrupt = 0;
2492 // GNU bc says "interrupted execution."
2493 // GNU dc says "Interrupt!"
2494 fputs("\ninterrupted execution\n", stderr);
2495 }
2496
2497# if ENABLE_FEATURE_EDITING
2498 if (G_ttyin && fp == stdin) {
2499 int n, i;
2500# define line_buf bb_common_bufsiz1
2501 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2502 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2503 if (n == 0) // ^C
2504 goto intr;
2505 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2506 return;
2507 }
2508 i = 0;
2509 for (;;) {
2510 char c = line_buf[i++];
2511 if (!c) break;
2512 if (bad_input_byte(c)) goto again;
2513 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002514 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002515# undef line_buf
2516 } else
2517# endif
2518#endif
2519 {
2520 int c;
2521 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002522
2523 do {
Denys Vlasenko14767602018-12-27 22:52:13 +01002524#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002525 if (G_interrupt) {
2526 // ^C was pressed: ignore entire line, get another one
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002527 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002528 goto intr;
2529 }
2530#endif
2531 do c = fgetc(fp); while (c == '\0');
2532 if (c == EOF) {
2533 if (ferror(fp))
2534 bb_perror_msg_and_die("input error");
2535 // Note: EOF does not append '\n'
2536 break;
2537 }
2538 bad_chars |= bad_input_byte(c);
2539 bc_vec_pushByte(vec, (char)c);
2540 } while (c != '\n');
2541
2542 if (bad_chars) {
2543 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002544 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002545 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002546 goto again;
2547 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002548 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002549 }
2550 bc_vec_pushZeroByte(vec);
2551 }
2552}
2553
2554//
2555// Parsing routines
2556//
2557
Denys Vlasenko10bde142018-12-27 18:23:58 +01002558static bool xc_num_strValid(const char *val, size_t base)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002559{
2560 BcDig b;
2561 bool radix;
2562
2563 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2564 radix = false;
2565 for (;;) {
2566 BcDig c = *val++;
2567 if (c == '\0')
2568 break;
2569 if (c == '.') {
2570 if (radix) return false;
2571 radix = true;
2572 continue;
2573 }
2574 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2575 return false;
2576 }
2577 return true;
2578}
2579
2580// Note: n is already "bc_num_zero()"ed,
2581// leading zeroes in "val" are removed
2582static void bc_num_parseDecimal(BcNum *n, const char *val)
2583{
2584 size_t len, i;
2585 const char *ptr;
2586
2587 len = strlen(val);
2588 if (len == 0)
2589 return;
2590
2591 bc_num_expand(n, len);
2592
2593 ptr = strchr(val, '.');
2594
2595 n->rdx = 0;
2596 if (ptr != NULL)
2597 n->rdx = (size_t)((val + len) - (ptr + 1));
2598
2599 for (i = 0; val[i]; ++i) {
2600 if (val[i] != '0' && val[i] != '.') {
2601 // Not entirely zero value - convert it, and exit
2602 i = len - 1;
2603 for (;;) {
2604 n->num[n->len] = val[i] - '0';
2605 ++n->len;
2606 skip_dot:
2607 if (i == 0) break;
2608 if (val[--i] == '.') goto skip_dot;
2609 }
2610 break;
2611 }
2612 }
2613 // if for() exits without hitting if(), the value is entirely zero
2614}
2615
2616// Note: n is already "bc_num_zero()"ed,
2617// leading zeroes in "val" are removed
2618static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2619{
2620 BcStatus s;
2621 BcNum temp, mult, result;
2622 BcNum base;
2623 BcDig base_digs[ULONG_NUM_BUFSIZE];
2624 BcDig c = '\0';
2625 unsigned long v;
2626 size_t i, digits;
2627
2628 for (i = 0; ; ++i) {
2629 if (val[i] == '\0')
2630 return;
2631 if (val[i] != '.' && val[i] != '0')
2632 break;
2633 }
2634
2635 bc_num_init_DEF_SIZE(&temp);
2636 bc_num_init_DEF_SIZE(&mult);
2637 base.cap = ARRAY_SIZE(base_digs);
2638 base.num = base_digs;
2639 bc_num_ulong2num(&base, base_t);
2640
2641 for (;;) {
2642 c = *val++;
2643 if (c == '\0') goto int_err;
2644 if (c == '.') break;
2645
2646 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2647
2648 s = zbc_num_mul(n, &base, &mult, 0);
2649 if (s) goto int_err;
2650 bc_num_ulong2num(&temp, v);
2651 s = zbc_num_add(&mult, &temp, n, 0);
2652 if (s) goto int_err;
2653 }
2654
2655 bc_num_init(&result, base.len);
2656 //bc_num_zero(&result); - already is
2657 bc_num_one(&mult);
2658
2659 digits = 0;
2660 for (;;) {
2661 c = *val++;
2662 if (c == '\0') break;
2663 digits++;
2664
2665 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2666
2667 s = zbc_num_mul(&result, &base, &result, 0);
2668 if (s) goto err;
2669 bc_num_ulong2num(&temp, v);
2670 s = zbc_num_add(&result, &temp, &result, 0);
2671 if (s) goto err;
2672 s = zbc_num_mul(&mult, &base, &mult, 0);
2673 if (s) goto err;
2674 }
2675
2676 s = zbc_num_div(&result, &mult, &result, digits);
2677 if (s) goto err;
2678 s = zbc_num_add(n, &result, n, digits);
2679 if (s) goto err;
2680
2681 if (n->len != 0) {
2682 if (n->rdx < digits)
2683 bc_num_extend(n, digits - n->rdx);
2684 } else
2685 bc_num_zero(n);
2686 err:
2687 bc_num_free(&result);
2688 int_err:
2689 bc_num_free(&mult);
2690 bc_num_free(&temp);
2691}
2692
Denys Vlasenko10bde142018-12-27 18:23:58 +01002693static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002694{
Denys Vlasenko10bde142018-12-27 18:23:58 +01002695 if (!xc_num_strValid(val, base_t))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002696 RETURN_STATUS(bc_error("bad number string"));
2697
2698 bc_num_zero(n);
2699 while (*val == '0') val++;
2700
2701 if (base_t == 10)
2702 bc_num_parseDecimal(n, val);
2703 else
2704 bc_num_parseBase(n, val, base_t);
2705
2706 RETURN_STATUS(BC_STATUS_SUCCESS);
2707}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002708#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002709
Denys Vlasenko10bde142018-12-27 18:23:58 +01002710static bool xc_lex_more_input(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002711{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002712 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002713
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002714 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002715
Denys Vlasenko10bde142018-12-27 18:23:58 +01002716 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002717
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002718 p->lex_inbuf = G.input_buffer.v;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002719// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002720
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002721 return G.input_buffer.len > 1;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002722}
2723
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002724// p->lex_inbuf points to the current string to be parsed.
2725// if p->lex_inbuf points to '\0', it's either EOF or it points after
2726// last processed line's terminating '\n' (and more reading needs to be done
2727// to get next character).
2728//
2729// If you are in a situation where that is a possibility, call peek_inbuf().
2730// If necessary, it performs more reading and changes p->lex_inbuf,
2731// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2732// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2733// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2734//
2735// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2736// it returns current char and advances the pointer (if not EOF).
2737// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2738//
2739// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2740// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2741// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002742//
2743// bc cases to test interactively:
2744// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2745// 1 #comment/* - prints "1<newline>" at once
2746// 1 #comment" - prints "1<newline>" at once
2747// 1\#comment - error at once (\ is not a line continuation)
2748// 1 + /*"*/2 - prints "3<newline>" at once
2749// 1 + /*#*/2 - prints "3<newline>" at once
2750// "str\" - prints "str\" at once
2751// "str#" - prints "str#" at once
2752// "str/*" - prints "str/*" at once
2753// "str#\ - waits for second line
2754// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002755static char peek_inbuf(void)
2756{
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002757 if (*G.prs.lex_inbuf == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002758 if (G.prs.lex_input_fp)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002759 if (!xc_lex_more_input())
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002760 G.prs.lex_input_fp = NULL;
2761 }
2762 return *G.prs.lex_inbuf;
2763}
2764static char eat_inbuf(void)
2765{
2766 char c = peek_inbuf();
2767 if (c) G.prs.lex_inbuf++;
2768 return c;
2769}
2770
Denys Vlasenko10bde142018-12-27 18:23:58 +01002771static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002772{
2773 BcParse *p = &G.prs;
2774 char c;
2775
2776 // Try: echo -n '#foo' | bc
2777 p->lex = XC_LEX_WHITESPACE;
2778
2779 // We depend here on input being done in whole lines:
2780 // '\0' which isn't the EOF can only be seen after '\n'.
2781 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2782 p->lex_inbuf++;
2783}
2784
Denys Vlasenko10bde142018-12-27 18:23:58 +01002785static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002786{
2787 BcParse *p = &G.prs;
2788
2789 p->lex = XC_LEX_WHITESPACE;
2790 for (;;) {
2791 // We depend here on input being done in whole lines:
2792 // '\0' which isn't the EOF can only be seen after '\n'.
2793 char c = *p->lex_inbuf;
2794 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2795 break;
2796 if (!isspace(c))
2797 break;
2798 p->lex_inbuf++;
2799 }
2800}
2801
Denys Vlasenko10bde142018-12-27 18:23:58 +01002802static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002803{
2804 BcParse *p = &G.prs;
2805 bool pt;
2806
2807 bc_vec_pop_all(&p->lex_strnumbuf);
2808 bc_vec_pushByte(&p->lex_strnumbuf, last);
2809
2810 pt = (last == '.');
2811 p->lex = XC_LEX_NUMBER;
2812 for (;;) {
2813 // We depend here on input being done in whole lines:
2814 // '\0' which isn't the EOF can only be seen after '\n'.
2815 char c = *p->lex_inbuf;
2816 check_c:
2817 if (c == '\0')
2818 break;
2819 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2820 p->lex_inbuf += 2;
2821 p->lex_line++;
2822 c = peek_inbuf(); // force next line to be read
2823 goto check_c;
2824 }
2825 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2826 if (c != '.') break;
2827 // if '.' was already seen, stop on second one:
2828 if (pt) break;
2829 pt = true;
2830 }
2831 // c is one of "0-9A-F."
2832 last = c;
2833 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2834 p->lex_inbuf++;
2835 }
2836 if (last == '.') // remove trailing '.' if any
2837 bc_vec_pop(&p->lex_strnumbuf);
2838 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2839
2840 G.err_line = G.prs.lex_line;
2841 RETURN_STATUS(BC_STATUS_SUCCESS);
2842}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002843#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002844
Denys Vlasenko10bde142018-12-27 18:23:58 +01002845static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002846{
2847 BcParse *p = &G.prs;
2848 size_t i;
2849 const char *buf;
2850
2851 p->lex = XC_LEX_NAME;
2852
2853 // Since names can't cross lines with \<newline>,
2854 // we depend on the fact that whole line is in the buffer
2855 i = 0;
2856 buf = p->lex_inbuf - 1;
2857 for (;;) {
2858 char c = buf[i];
2859 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2860 i++;
2861 }
2862
2863#if 0 // We do not protect against people with gigabyte-long names
2864 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2865 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2866 if (i > BC_MAX_STRING)
2867 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2868 }
2869#endif
2870 bc_vec_string(&p->lex_strnumbuf, i, buf);
2871
2872 // Increment the index. We minus 1 because it has already been incremented.
2873 p->lex_inbuf += i - 1;
2874
2875 //return BC_STATUS_SUCCESS;
2876}
2877
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002878IF_BC(static BC_STATUS zbc_lex_token(void);)
2879IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002880#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2881#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002882
Denys Vlasenko10bde142018-12-27 18:23:58 +01002883static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002884{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002885 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002886 BcStatus s;
2887
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002888 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002889//why?
2890// if (p->lex_last == XC_LEX_EOF)
2891// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002892
2893 // Loop until failure or we don't have whitespace. This
2894 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002895 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002896 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002897 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002898 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002899 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002900 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002901 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002902 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002903 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002904 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002905 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2906 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002907 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002908 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002909 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002910 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002911 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002912 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002913 } while (!s && p->lex == XC_LEX_WHITESPACE);
2914 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002915
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002916 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002917}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002918#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002919
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002920#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002921static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002922{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002923 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002924 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002925 RETURN_STATUS(BC_STATUS_SUCCESS);
2926}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002927#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002928
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002929static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002930{
2931 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01002932 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002933 if (s) RETURN_STATUS(s);
2934 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002935 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002936 RETURN_STATUS(s);
2937}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002938#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002939
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002940static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002941{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002942 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002943 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002944 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002945 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002946
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002947 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2948 const char *keyword8 = bc_lex_kws[i].name8;
2949 unsigned j = 0;
2950 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2951 j++;
2952 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002953 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002954 if (keyword8[j] != '\0')
2955 continue;
2956 match:
2957 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002958 if (isalnum(buf[j]) || buf[j]=='_')
2959 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002960 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01002961 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01002962 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
2963 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002964 }
2965
2966 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002967 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002968 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002969 }
2970
Denys Vlasenko10bde142018-12-27 18:23:58 +01002971 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002972 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002973
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002974 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002975 // Prevent this:
2976 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01002977 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002978 // '
2979 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01002980 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002981 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002982
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002983 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002984}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002985#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002986
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002987static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002988{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002989 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002990
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002991 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002992 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01002993 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002994 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01002995 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01002996 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01002997 }
2998 if (c == '"')
2999 break;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003000 if (c == '\n')
3001 p->lex_line++;
3002 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3003 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003004 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003005 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3006 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003007
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003008 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003009 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003010}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003011#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003012
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003013static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003014{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003015 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003016 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003017 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003018 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003019 with_and_without >>= 8; // store "with" value
3020 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003021 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003022}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003023#define parse_lex_by_checking_eq_sign(with, without) \
3024 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003025
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003026static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003027{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003028 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003029
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003030 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003031 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003032 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003033 char c;
3034
3035 p->lex_inbuf++;
3036 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003037 check_star:
3038 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003039 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003040 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003041 if (c == '/')
3042 break;
3043 goto check_star;
3044 }
3045 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003046 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003047 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003048 if (c == '\n')
3049 p->lex_line++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003050 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003051 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003052
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003053 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003054 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003055}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003056#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003057
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003058#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003059static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003060{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003061 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003062 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003063 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003064 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003065
3066 // This is the workhorse of the lexer.
3067 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003068// case '\0': // probably never reached
3069// p->lex_inbuf--;
3070// p->lex = XC_LEX_EOF;
3071// break;
3072 case '\n':
3073 p->lex_line++;
3074 p->lex = XC_LEX_NLINE;
3075 break;
3076 case '\t':
3077 case '\v':
3078 case '\f':
3079 case '\r':
3080 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003081 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003082 break;
3083 case '!':
3084 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3085 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3086 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003087 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003088 }
3089 break;
3090 case '"':
3091 s = zbc_lex_string();
3092 break;
3093 case '#':
3094 s = zbc_POSIX_does_not_allow("'#' script comments");
3095 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003096 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003097 break;
3098 case '%':
3099 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3100 break;
3101 case '&':
3102 c2 = *p->lex_inbuf;
3103 if (c2 == '&') {
3104 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3105 if (s) RETURN_STATUS(s);
3106 p->lex_inbuf++;
3107 p->lex = BC_LEX_OP_BOOL_AND;
3108 } else {
3109 p->lex = XC_LEX_INVALID;
3110 s = bc_error_bad_character('&');
3111 }
3112 break;
3113 case '(':
3114 case ')':
3115 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3116 break;
3117 case '*':
3118 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3119 break;
3120 case '+':
3121 c2 = *p->lex_inbuf;
3122 if (c2 == '+') {
3123 p->lex_inbuf++;
3124 p->lex = BC_LEX_OP_INC;
3125 } else
3126 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3127 break;
3128 case ',':
3129 p->lex = BC_LEX_COMMA;
3130 break;
3131 case '-':
3132 c2 = *p->lex_inbuf;
3133 if (c2 == '-') {
3134 p->lex_inbuf++;
3135 p->lex = BC_LEX_OP_DEC;
3136 } else
3137 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3138 break;
3139 case '.':
3140 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003141 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003142 else {
3143 p->lex = BC_LEX_KEY_LAST;
3144 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3145 }
3146 break;
3147 case '/':
3148 c2 = *p->lex_inbuf;
3149 if (c2 == '*')
3150 s = zbc_lex_comment();
3151 else
3152 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3153 break;
3154 case '0':
3155 case '1':
3156 case '2':
3157 case '3':
3158 case '4':
3159 case '5':
3160 case '6':
3161 case '7':
3162 case '8':
3163 case '9':
3164 case 'A':
3165 case 'B':
3166 case 'C':
3167 case 'D':
3168 case 'E':
3169 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003170 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003171 break;
3172 case ';':
3173 p->lex = BC_LEX_SCOLON;
3174 break;
3175 case '<':
3176 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3177 break;
3178 case '=':
3179 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3180 break;
3181 case '>':
3182 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3183 break;
3184 case '[':
3185 case ']':
3186 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3187 break;
3188 case '\\':
3189 if (*p->lex_inbuf == '\n') {
3190 p->lex = XC_LEX_WHITESPACE;
3191 p->lex_inbuf++;
3192 } else
3193 s = bc_error_bad_character(c);
3194 break;
3195 case '^':
3196 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3197 break;
3198 case 'a':
3199 case 'b':
3200 case 'c':
3201 case 'd':
3202 case 'e':
3203 case 'f':
3204 case 'g':
3205 case 'h':
3206 case 'i':
3207 case 'j':
3208 case 'k':
3209 case 'l':
3210 case 'm':
3211 case 'n':
3212 case 'o':
3213 case 'p':
3214 case 'q':
3215 case 'r':
3216 case 's':
3217 case 't':
3218 case 'u':
3219 case 'v':
3220 case 'w':
3221 case 'x':
3222 case 'y':
3223 case 'z':
3224 s = zbc_lex_identifier();
3225 break;
3226 case '{':
3227 case '}':
3228 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3229 break;
3230 case '|':
3231 c2 = *p->lex_inbuf;
3232 if (c2 == '|') {
3233 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3234 if (s) RETURN_STATUS(s);
3235 p->lex_inbuf++;
3236 p->lex = BC_LEX_OP_BOOL_OR;
3237 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003238 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003239 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003240 }
3241 break;
3242 default:
3243 p->lex = XC_LEX_INVALID;
3244 s = bc_error_bad_character(c);
3245 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 }
3247
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003248 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003249}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003250#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003251#endif // ENABLE_BC
3252
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003253#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003254static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003255{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003256 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003257 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003258 xc_lex_whitespace(); // eats whitespace (but not newline)
3259 p->lex_inbuf++; // xc_lex_name() expects this
3260 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003261 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003262 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003263 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003264 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3265 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 }
3267
Denys Vlasenko29301232018-12-11 15:29:32 +01003268 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003269}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003270#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003271
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003272static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003273{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003274 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003275 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003276
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003277 p->lex = XC_LEX_STR;
3278 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003279
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003280 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003281 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003282 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003283 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003284 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003285 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003286 if (c == '[') depth++;
3287 if (c == ']')
3288 if (--depth == 0)
3289 break;
3290 if (c == '\n')
3291 p->lex_line++;
3292 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3293 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003295 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003296 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003297
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003298 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003299 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003300}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003301#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003302
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003303#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003304static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003305{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003306 static const //BcLexType - should be this type, but narrower type saves size:
3307 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003308 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003309 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 +01003310 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3311 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003312 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003313 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003314
Denys Vlasenko10bde142018-12-27 18:23:58 +01003315 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003316 BcStatus s;
3317 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003318 size_t i;
3319
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003320 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003321 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003322 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003323 }
3324
Denys Vlasenko81293c82018-12-24 01:53:55 +01003325 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003326 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003327 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003328 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003329 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003330 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003331 }
3332
3333 // This is the workhorse of the lexer.
3334 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003335// case '\0': // probably never reached
3336// p->lex = XC_LEX_EOF;
3337// break;
3338 case '\n':
3339 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3340 // (and "case '\n':" is not just empty here)
3341 // only to allow interactive dc have a way to exit
3342 // "parse" stage of "parse,execute" loop
3343 // on <enter>, not on _next_ token (which would mean
3344 // commands are not executed on pressing <enter>).
3345 // IOW: typing "1p<enter>" should print "1" _at once_,
3346 // not after some more input.
3347 p->lex_line++;
3348 p->lex = XC_LEX_NLINE;
3349 break;
3350 case '\t':
3351 case '\v':
3352 case '\f':
3353 case '\r':
3354 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003355 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003356 break;
3357 case '!':
3358 c2 = *p->lex_inbuf;
3359 if (c2 == '=')
3360 p->lex = XC_LEX_OP_REL_NE;
3361 else if (c2 == '<')
3362 p->lex = XC_LEX_OP_REL_LE;
3363 else if (c2 == '>')
3364 p->lex = XC_LEX_OP_REL_GE;
3365 else
3366 RETURN_STATUS(bc_error_bad_character(c));
3367 p->lex_inbuf++;
3368 break;
3369 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003370 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003371 break;
3372 case '.':
3373 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003374 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003375 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003376 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003377 break;
3378 case '0':
3379 case '1':
3380 case '2':
3381 case '3':
3382 case '4':
3383 case '5':
3384 case '6':
3385 case '7':
3386 case '8':
3387 case '9':
3388 case 'A':
3389 case 'B':
3390 case 'C':
3391 case 'D':
3392 case 'E':
3393 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003394 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003395 break;
3396 case '[':
3397 s = zdc_lex_string();
3398 break;
3399 default:
3400 p->lex = XC_LEX_INVALID;
3401 s = bc_error_bad_character(c);
3402 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003403 }
3404
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003405 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003406}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003407#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003408#endif // ENABLE_DC
3409
Denys Vlasenko10bde142018-12-27 18:23:58 +01003410static void xc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003411{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003412 BcVec *code = &G.prs.func->code;
3413 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
3414 bc_vec_pushByte(code, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003415}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003416
Denys Vlasenko10bde142018-12-27 18:23:58 +01003417static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003418{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003419#if 1
3420 BcVec *code = &G.prs.func->code;
3421 size_t pos = code->len;
3422 size_t len = strlen(name) + 1;
3423
3424 bc_vec_expand(code, pos + len);
3425 strcpy(code->v + pos, name);
3426 code->len = pos + len;
3427#else
3428 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003429 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003430 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003431 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003432#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003433}
3434
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003435// Indexes < 0xfc are encoded verbatim, else first byte is
3436// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3437// followed by that many bytes, lsb first.
3438// (The above describes 32-bit case).
3439#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3440
Denys Vlasenko10bde142018-12-27 18:23:58 +01003441static void xc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003442{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003443 size_t mask;
3444 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003445
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003446 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003447 if (idx < SMALL_INDEX_LIMIT) {
3448 goto push_idx;
3449 }
3450
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003451 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3452 amt = sizeof(idx);
3453 do {
3454 if (idx & mask) break;
3455 mask >>= 8;
3456 amt--;
3457 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003458
Denys Vlasenko10bde142018-12-27 18:23:58 +01003459 xc_parse_push(SMALL_INDEX_LIMIT + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003460
3461 while (idx != 0) {
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003462 push_idx:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003463 xc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003464 idx >>= 8;
3465 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003466}
3467
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003468#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003469static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003470{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003471 xc_parse_push(BC_INST_JUMP);
3472 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003473}
3474
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003475static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003476{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003477 xc_parse_push(BC_INST_JUMP_ZERO);
3478 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003479}
3480
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003481static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003482{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003483 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003484 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003485
Denys Vlasenko10bde142018-12-27 18:23:58 +01003486 xc_parse_push(XC_INST_STR);
3487 xc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003488 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003489
Denys Vlasenko10bde142018-12-27 18:23:58 +01003490 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003491}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003492#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003493#endif
3494
Denys Vlasenko10bde142018-12-27 18:23:58 +01003495static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003496{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003497 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003498 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003499#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003500 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003501#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003502 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003503#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003504 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003505#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01003506 xc_parse_push(XC_INST_NUM);
3507 xc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003508}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003509
Denys Vlasenko10bde142018-12-27 18:23:58 +01003510static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003511{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003512 G.prs.func = xc_program_func(G.prs.fidx);
3513 G.prs.lex_inbuf = text;
3514 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3515 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003516}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003517#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003518
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003519// Called when parsing or execution detects a failure,
3520// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003521static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003522{
3523 BcFunc *f;
3524 BcInstPtr *ip;
3525
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003526 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003527 bc_vec_pop_all(&G.prog.results);
3528
Denys Vlasenko10bde142018-12-27 18:23:58 +01003529 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003530 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003531 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003532}
3533
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003534// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003535// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003536static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003537{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003538 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003539 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003540 bc_func_free(p->func);
3541 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003542
Denys Vlasenko65e10462018-12-19 15:13:14 +01003543 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003544 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003545 }
3546
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003547 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003548 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003549
Denys Vlasenko503faf92018-12-20 16:24:18 +01003550 IF_BC(bc_vec_pop_all(&p->exits);)
3551 IF_BC(bc_vec_pop_all(&p->conds);)
3552 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003553
Denys Vlasenko10bde142018-12-27 18:23:58 +01003554 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003555}
3556
Denys Vlasenko10bde142018-12-27 18:23:58 +01003557static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003558{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003559 IF_BC(bc_vec_free(&G.prs.exits);)
3560 IF_BC(bc_vec_free(&G.prs.conds);)
3561 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003562 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003563}
3564
Denys Vlasenko10bde142018-12-27 18:23:58 +01003565static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003566{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003567 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003568 memset(p, 0, sizeof(BcParse));
3569
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003570 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003571 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3572 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3573 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003574
Denys Vlasenkofa210792018-12-19 19:35:40 +01003575 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003576 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003577}
3578
Denys Vlasenko10bde142018-12-27 18:23:58 +01003579static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003580{
Denys Vlasenko04715442018-12-21 00:10:26 +01003581 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003582 BcFunc f;
3583 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003584 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003585 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003586 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003587}
3588
3589#if ENABLE_BC
3590
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003591// Note: takes ownership of 'name' (must be malloced)
3592static size_t bc_program_addFunc(char *name)
3593{
3594 size_t idx;
3595 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003596 int inserted;
3597
3598 entry.name = name;
3599 entry.idx = G.prog.fns.len;
3600
3601 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3602 if (!inserted) free(name);
3603
3604 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3605 idx = entry_ptr->idx;
3606
3607 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003608 // There is already a function with this name.
3609 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003610 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003611 bc_func_free(func);
3612 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003613 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003614 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003615 }
3616
3617 return idx;
3618}
3619
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003620#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003621// We can calculate the conversion between tokens and exprs by subtracting the
3622// position of the first operator in the lex enum and adding the position of the
3623// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003624#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003625
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003626static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003627
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003628static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003629{
3630 BcStatus s;
3631
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003632 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003633 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3634 RETURN_STATUS(bc_error("empty expression"));
3635 RETURN_STATUS(s);
3636}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003637#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003638
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003639static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003640#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003641
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003642static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003643{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003644 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003645}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003646#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003647
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003648static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003649{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003650 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003651 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3652 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003653 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003654 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003655 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003656 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003657
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003658 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003659}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003660#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003661
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003662static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003663{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003664 BcParse *p = &G.prs;
Denys Vlasenko69560f42018-12-24 14:14:23 +01003665 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3666 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003667
3668 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003669 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003670 if (t == BC_LEX_LPAREN) break;
3671
Denys Vlasenko69560f42018-12-24 14:14:23 +01003672 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003673 if (l >= r && (l != r || !left)) break;
3674
Denys Vlasenko10bde142018-12-27 18:23:58 +01003675 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003676 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003677 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003678 }
3679
3680 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003681}
3682
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003683static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003684{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003685 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003686 BcLexType top;
3687
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003688 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003689 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003690 top = BC_PARSE_TOP_OP(p);
3691
3692 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003693 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003694
3695 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003696 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003697
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003698 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003699 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003700 top = BC_PARSE_TOP_OP(p);
3701 }
3702
3703 bc_vec_pop(&p->ops);
3704
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003705 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003706}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003707#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003708
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003709static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003710{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003711 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003712 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003713 size_t nparams;
3714
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003715 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003716 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3717
Denys Vlasenko10bde142018-12-27 18:23:58 +01003718 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003719 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003720
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003721 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003722 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003723 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003724 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003725 if (s) RETURN_STATUS(s);
3726 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003727 if (p->lex != BC_LEX_COMMA) {
3728 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003729 break;
3730 RETURN_STATUS(bc_error_bad_token());
3731 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003732 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003733 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003734 }
3735 }
3736
Denys Vlasenko10bde142018-12-27 18:23:58 +01003737 xc_parse_push(BC_INST_CALL);
3738 xc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003739
Denys Vlasenko26819db2018-12-12 16:08:46 +01003740 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003741}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003742#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003743
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003744// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003745static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003746{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003747 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003748 BcStatus s;
3749 BcId entry, *entry_ptr;
3750 size_t idx;
3751
3752 entry.name = name;
3753
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003754 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003755 if (s) goto err;
3756
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003757 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003758 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003759 goto err;
3760 }
3761
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003762 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003763
3764 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003765 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003766 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003767 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003768 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003769 free(name);
3770
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003771 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003772 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003773
Denys Vlasenko10bde142018-12-27 18:23:58 +01003774 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003775 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003776 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003777 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003778}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003779#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003780
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003781static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003782{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003783 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003784 BcStatus s;
3785 char *name;
3786
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003787 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003788 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003789 if (s) goto err;
3790
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003791 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003792 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003793 if (s) goto err;
3794
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003795 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003796 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003797 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003798 goto err;
3799 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003800 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003801 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003802 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003803 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003804 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003805 if (s) goto err;
3806 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003807 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003808 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003809 xc_parse_push(*type);
3810 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003811 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003812 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003813 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003814 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003815 goto err;
3816 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003818 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003819 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003820 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003821 xc_parse_push(XC_INST_VAR);
3822 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003823 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003824 }
3825
Denys Vlasenko26819db2018-12-12 16:08:46 +01003826 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003827 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003828 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003829 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003830}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003831#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003832
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003833static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003834{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003835 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003836 BcStatus s;
3837
Denys Vlasenko10bde142018-12-27 18:23:58 +01003838 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003839 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003840 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003841
Denys Vlasenko10bde142018-12-27 18:23:58 +01003842 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003843 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003844 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003845
Denys Vlasenko10bde142018-12-27 18:23:58 +01003846 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003847
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003848 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003849}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003850#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003851
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003852static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003853{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003854 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003855 BcStatus s;
3856
Denys Vlasenko10bde142018-12-27 18:23:58 +01003857 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003858 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003859 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003860
3861 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3862
Denys Vlasenko10bde142018-12-27 18:23:58 +01003863 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003864 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003865
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003866 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003867 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003868
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003869 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003870
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003871 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003872 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003873
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003874 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003875}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003876#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003877
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003878static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003879{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003880 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003881 BcStatus s;
3882
Denys Vlasenko10bde142018-12-27 18:23:58 +01003883 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003884 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003885
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003886 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003887 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003888 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003889 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003890 }
3891
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003892 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003893 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3894
Denys Vlasenko10bde142018-12-27 18:23:58 +01003895 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003896 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003897
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003898 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003899 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003900 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003901 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01003902 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003903
Denys Vlasenko10bde142018-12-27 18:23:58 +01003904 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003905}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003906#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003907
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003908static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
3909 size_t *nexprs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003910{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003911 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003912 BcStatus s;
3913 BcLexType type;
3914 char inst;
3915 BcInst etype = *prev;
3916
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003917 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3918 || etype == XC_INST_SCALE || etype == BC_INST_LAST
3919 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003920 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003921 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003922 xc_parse_push(inst);
3923 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003924 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003925 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003926 *paren_expr = true;
3927
Denys Vlasenko10bde142018-12-27 18:23:58 +01003928 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003929 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003930 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06003931
3932 // Because we parse the next part of the expression
3933 // right here, we need to increment this.
3934 *nexprs = *nexprs + 1;
3935
3936 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003937 case XC_LEX_NAME:
3938 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
3939 break;
3940 case BC_LEX_KEY_IBASE:
3941 case BC_LEX_KEY_LAST:
3942 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003943 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
3944 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003945 break;
3946 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003947 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003948 if (s) RETURN_STATUS(s);
3949 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003950 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003951 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01003952 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003953 break;
3954 default:
3955 s = bc_error_bad_token();
3956 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003957 }
3958
Denys Vlasenko10bde142018-12-27 18:23:58 +01003959 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06003960 }
3961
Denys Vlasenko26819db2018-12-12 16:08:46 +01003962 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003963}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003964#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003966#if 0
3967#define BC_PARSE_LEAF(p, rparen) \
3968 ((rparen) \
3969 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
3970 || (p) == BC_INST_INC_POST \
3971 || (p) == BC_INST_DEC_POST \
3972 )
3973#else
3974static int ok_in_expr(BcInst p)
3975{
3976 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
3977 || p == BC_INST_INC_POST
3978 || p == BC_INST_DEC_POST
3979 ;
3980}
3981#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
3982#endif
3983
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003984static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
3985 bool rparen, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003986{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003987 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003988 BcStatus s;
3989 BcLexType type;
3990 BcInst etype = *prev;
3991
Denys Vlasenko10bde142018-12-27 18:23:58 +01003992 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003993 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003994
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003995 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01003996 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003997
3998 // We can just push onto the op stack because this is the largest
3999 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004000 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004001 bc_vec_push(&p->ops, &type);
4002 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004003 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004004
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004005 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004007#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004008
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004009static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004010{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004011 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004012 BcStatus s;
4013 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004014
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004015 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004016 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004017 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004018 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004019 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004020 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004021 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004022 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004024 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004025 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004026 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004027 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004028 }
4029
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004030 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004031}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004032#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004033
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004034static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004035{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004036 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004037 BcStatus s;
4038 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004039
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004040 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004041 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004042 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004043
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004044 t = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004045 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004046 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004047 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004048 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004049 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004050 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004051 xc_parse_push(BC_INST_RET0);
4052 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004053 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004054 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004056 if (!paren || p->lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004057 s = zbc_POSIX_requires("parentheses around return expressions");
4058 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004059 }
4060
Denys Vlasenko10bde142018-12-27 18:23:58 +01004061 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004062 }
4063
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004064 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004065 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004066}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004067#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004068
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004069static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004070{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004071 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004072 size_t *label = bc_vec_item(&p->func->labels, idx);
4073 *label = p->func->code.len;
4074}
4075
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004076static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004077{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004078 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004079 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004080 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004081
4082 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004083 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004084 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004085 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004086
Denys Vlasenko10bde142018-12-27 18:23:58 +01004087 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004088 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004089 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004090 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004091 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004092
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004093 // Encode "if zero, jump to ..."
4094 // Pushed value (destination of the jump) is uninitialized,
4095 // will be rewritten to be address of "end of if()" or of "else".
4096 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004097 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004098
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004099 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004100 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004101
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004102 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4103 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004104 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004105
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004106 // Encode "after then_stmt, jump to end of if()"
4107 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4108 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004109 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004110
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004111 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004112 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004113
Denys Vlasenko15850832018-12-16 21:40:54 +01004114 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004115
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004116 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004117 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004118 }
4119
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004120 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004121 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004122
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004123 dbg_lex_done("%s:%d done", __func__, __LINE__);
4124 RETURN_STATUS(s);
4125}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004126#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004127
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004128static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004129{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004130 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004131 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004132 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004133 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004134
Denys Vlasenko10bde142018-12-27 18:23:58 +01004135 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004136 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004137 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004138 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004139 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004140
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004141 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004142 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004143 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004144
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004145 bc_vec_push(&p->exits, &ip_idx);
4146 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004147
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004148 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004149 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004150 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004151
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004152 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004153
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004154 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004155 if (s) RETURN_STATUS(s);
4156
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004157 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004158 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004160 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004161 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004162
4163 bc_vec_pop(&p->exits);
4164 bc_vec_pop(&p->conds);
4165
4166 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004167}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004168#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004169
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004170static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004171{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004172 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004173 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004174 size_t cond_idx, exit_idx, body_idx, update_idx;
4175
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004176 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004177 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004178 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004179 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004180 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004181 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004182
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004183 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004184 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004185 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004186 if (s) RETURN_STATUS(s);
4187 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004188 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4189 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004190 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004191
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004192 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004193 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004194 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004195
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004196 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004197 update_idx = cond_idx + 1;
4198 body_idx = update_idx + 1;
4199 exit_idx = body_idx + 1;
4200
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004201 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004202 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004203 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004204 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004205 // This is safe to set because the current token is a semicolon,
4206 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004207 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004208 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004209 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004210 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004211 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004212
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004213 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004214
Denys Vlasenko10bde142018-12-27 18:23:58 +01004215 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004216 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004217
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004218 bc_parse_pushJUMP_ZERO(exit_idx);
4219 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004220
Gavin Howard01055ba2018-11-03 11:00:21 -06004221 bc_vec_push(&p->conds, &update_idx);
4222 bc_vec_push(&p->func->labels, &p->func->code.len);
4223
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004224 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004225 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004226 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004227 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004228 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004229 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004230 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4231 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004232 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004233
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004234 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004235 bc_vec_push(&p->func->labels, &p->func->code.len);
4236
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004237 bc_vec_push(&p->exits, &exit_idx);
4238 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004239
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004240 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004241 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004242
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004243 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004244 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004245
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004246 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004247 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004248
4249 bc_vec_pop(&p->exits);
4250 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004251
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004252 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004253}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004254#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004255
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004256static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004257{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004258 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004259 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004260
Gavin Howard01055ba2018-11-03 11:00:21 -06004261 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004262 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4263 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004264 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004265 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004266 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004267 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004268 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004269
Denys Vlasenko10bde142018-12-27 18:23:58 +01004270 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004271}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004272#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004273
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004274static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4275{
4276 BcId *autoid;
4277 BcId a;
4278 size_t i;
4279
4280 autoid = (void*)f->autos.v;
4281 for (i = 0; i < f->autos.len; i++, autoid++) {
4282 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004283 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004284 }
4285
4286 a.idx = var;
4287 a.name = name;
4288
4289 bc_vec_push(&f->autos, &a);
4290
4291 RETURN_STATUS(BC_STATUS_SUCCESS);
4292}
4293#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4294
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004295static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004296{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004297 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004298 BcStatus s;
4299 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004300 char *name;
4301
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004302 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004303 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004304 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004305 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004306 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004307
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004308 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004309 p->fidx = bc_program_addFunc(name);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004310 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004311
Denys Vlasenko10bde142018-12-27 18:23:58 +01004312 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004313 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004314 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004315 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004316 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004317 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004318
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004319 while (p->lex != BC_LEX_RPAREN) {
4320 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004321 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004322
4323 ++p->func->nparams;
4324
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004325 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004326 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004327 if (s) goto err;
4328
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004329 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004330
4331 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004332 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004333 if (s) goto err;
4334
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004335 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004336 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004337 goto err;
4338 }
4339
Denys Vlasenko10bde142018-12-27 18:23:58 +01004340 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004341 if (s) goto err;
4342 }
4343
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004344 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004345 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004346 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004347 if (s) goto err;
4348 }
4349
Denys Vlasenko29301232018-12-11 15:29:32 +01004350 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004351 if (s) goto err;
4352 }
4353
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004354 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004355
Denys Vlasenko10bde142018-12-27 18:23:58 +01004356 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004357 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004358
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004359 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004360 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004361 if (s) RETURN_STATUS(s);
4362 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004363
Denys Vlasenko202dd192018-12-16 17:30:35 +01004364 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004365 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004366 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004367 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004368 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004369 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004370
4371 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004372 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004373 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004374 if (s) RETURN_STATUS(s);
4375
Denys Vlasenko10bde142018-12-27 18:23:58 +01004376 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004377
4378 // Subsequent code generation is into main program
4379 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004380 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004381
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004382 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004383 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004384 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004385 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004386 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004387 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004388}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004389#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004390
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004391static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004392{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004393 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004394 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004395 char *name;
4396
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004397 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004398 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004399 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004400
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004401 for (;;) {
4402 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004403
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004404 if (p->lex != XC_LEX_NAME)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004405 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4406
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004407 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004408 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004409 if (s) goto err;
4410
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004411 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004412 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004413 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004414 if (s) goto err;
4415
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004416 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004417 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004418 goto err;
4419 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004420 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004421 if (s) goto err;
4422 }
4423
Denys Vlasenko29301232018-12-11 15:29:32 +01004424 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004425 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004426
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004427 if (p->lex == XC_LEX_NLINE
4428 || p->lex == BC_LEX_SCOLON
4429 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004430 ) {
4431 break;
4432 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004433 if (p->lex != BC_LEX_COMMA)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004434 RETURN_STATUS(bc_error("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004435 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004436 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004437 }
4438
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004439 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004440 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004441 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004442 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004443 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004444 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004445}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004446#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004447
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004448#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004449static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004450{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004451 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 BcStatus s = BC_STATUS_SUCCESS;
4453
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004454 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004455
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004456 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004457 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004458 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004459 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004460 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004461 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004462 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004463 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004464
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004465 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004466 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4467 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004468 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004469 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004470 } while (p->lex == XC_LEX_NLINE);
4471 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004472 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004473 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004474 if (s) RETURN_STATUS(s);
4475 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004476 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004477 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004478 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004479 if (s) RETURN_STATUS(s);
4480 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004481 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004482 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4483 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004484 }
4485
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004486 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4487 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004488 case XC_LEX_OP_MINUS:
4489 case BC_LEX_OP_INC:
4490 case BC_LEX_OP_DEC:
4491 case BC_LEX_OP_BOOL_NOT:
4492 case BC_LEX_LPAREN:
4493 case XC_LEX_NAME:
4494 case XC_LEX_NUMBER:
4495 case BC_LEX_KEY_IBASE:
4496 case BC_LEX_KEY_LAST:
4497 case BC_LEX_KEY_LENGTH:
4498 case BC_LEX_KEY_OBASE:
4499 case BC_LEX_KEY_READ:
4500 case BC_LEX_KEY_SCALE:
4501 case BC_LEX_KEY_SQRT:
4502 s = zbc_parse_expr(BC_PARSE_PRINT);
4503 break;
4504 case XC_LEX_STR:
4505 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004506 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004507 break;
4508 case BC_LEX_KEY_BREAK:
4509 case BC_LEX_KEY_CONTINUE:
4510 s = zbc_parse_break_or_continue(p->lex);
4511 break;
4512 case BC_LEX_KEY_FOR:
4513 s = zbc_parse_for();
4514 break;
4515 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004516 xc_parse_push(BC_INST_HALT);
4517 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004518 break;
4519 case BC_LEX_KEY_IF:
4520 s = zbc_parse_if();
4521 break;
4522 case BC_LEX_KEY_LIMITS:
4523 // "limits" is a compile-time command,
4524 // the output is produced at _parse time_.
4525 printf(
4526 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4527 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4528 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4529 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4530 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4531 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4532 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4533 "Number of vars = "BC_MAX_VARS_STR "\n"
4534 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004535 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004536 break;
4537 case BC_LEX_KEY_PRINT:
4538 s = zbc_parse_print();
4539 break;
4540 case BC_LEX_KEY_QUIT:
4541 // "quit" is a compile-time command. For example,
4542 // "if (0 == 1) quit" terminates when parsing the statement,
4543 // not when it is executed
4544 QUIT_OR_RETURN_TO_MAIN;
4545 case BC_LEX_KEY_RETURN:
4546 if (!p->in_funcdef)
4547 RETURN_STATUS(bc_error("'return' not in a function"));
4548 s = zbc_parse_return();
4549 break;
4550 case BC_LEX_KEY_WHILE:
4551 s = zbc_parse_while();
4552 break;
4553 default:
4554 s = bc_error_bad_token();
4555 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004556 }
4557
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004558 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004559 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004560}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004561#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004562
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004563static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004564{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004565 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004566 BcStatus s;
4567
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004568 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004569 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004570 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004571 else if (p->lex == BC_LEX_KEY_DEFINE) {
4572 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004573 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004574 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004575 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004576 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004577 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004578
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004579 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004580 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004581}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004582#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004583
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004584// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004585static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004586{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004587 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004588 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004589 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004590 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004591 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004592
Denys Vlasenko17df8822018-12-14 23:00:24 +01004593 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004594 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004595 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004596 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004597 bin_last = true;
4598
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004599 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004600 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004601 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004602 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004603
4604 if (!lex_allowed_in_bc_expr(t))
4605 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004606
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004607 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004608 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004609 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004610 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004611 case BC_LEX_OP_INC:
4612 case BC_LEX_OP_DEC:
4613 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
4614 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
4615 rprn = bin_last = false;
4616 //get_token = false; - already is
4617 break;
4618 case XC_LEX_OP_MINUS:
4619 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
4620 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
4621 rprn = false;
4622 //get_token = false; - already is
4623 bin_last = (prev == XC_INST_MINUS);
4624 break;
4625 case BC_LEX_OP_ASSIGN_POWER:
4626 case BC_LEX_OP_ASSIGN_MULTIPLY:
4627 case BC_LEX_OP_ASSIGN_DIVIDE:
4628 case BC_LEX_OP_ASSIGN_MODULUS:
4629 case BC_LEX_OP_ASSIGN_PLUS:
4630 case BC_LEX_OP_ASSIGN_MINUS:
4631 case BC_LEX_OP_ASSIGN:
4632 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4633 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4634 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4635 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4636 ) {
4637 return bc_error("bad assignment:"
4638 " left side must be variable"
4639 " or array element"
4640 ); // note: shared string
4641 }
4642 // Fallthrough.
4643 case XC_LEX_OP_POWER:
4644 case XC_LEX_OP_MULTIPLY:
4645 case XC_LEX_OP_DIVIDE:
4646 case XC_LEX_OP_MODULUS:
4647 case XC_LEX_OP_PLUS:
4648 case XC_LEX_OP_REL_EQ:
4649 case XC_LEX_OP_REL_LE:
4650 case XC_LEX_OP_REL_GE:
4651 case XC_LEX_OP_REL_NE:
4652 case XC_LEX_OP_REL_LT:
4653 case XC_LEX_OP_REL_GT:
4654 case BC_LEX_OP_BOOL_NOT:
4655 case BC_LEX_OP_BOOL_OR:
4656 case BC_LEX_OP_BOOL_AND:
4657 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
4658 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4659 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
4660 ) {
4661 return bc_error_bad_expression();
4662 }
4663 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4664 prev = BC_TOKEN_2_INST(t);
4665 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004666 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004667 rprn = false;
4668 //get_token = false; - already is
4669 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4670 break;
4671 case BC_LEX_LPAREN:
4672 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
4673 if (BC_PARSE_LEAF(prev, rprn))
4674 return bc_error_bad_expression();
4675 bc_vec_push(&p->ops, &t);
4676 nparens++;
4677 get_token = true;
4678 paren_expr = false;
4679 rprn = bin_last = false;
4680 break;
4681 case BC_LEX_RPAREN:
4682 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
4683 if (bin_last || prev == XC_INST_BOOL_NOT)
4684 return bc_error_bad_expression();
4685 if (nparens == 0) {
4686 goto exit_loop;
4687 }
4688 if (!paren_expr) {
4689 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
4690 return BC_STATUS_PARSE_EMPTY_EXP;
4691 }
4692 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4693 nparens--;
4694 get_token = true;
4695 paren_expr = rprn = true;
4696 bin_last = false;
4697 break;
4698 case XC_LEX_NAME:
4699 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
4700 if (BC_PARSE_LEAF(prev, rprn))
4701 return bc_error_bad_expression();
4702 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
4703 paren_expr = true;
4704 rprn = bin_last = false;
4705 //get_token = false; - already is
4706 nexprs++;
4707 break;
4708 case XC_LEX_NUMBER:
4709 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4710 if (BC_PARSE_LEAF(prev, rprn))
4711 return bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004712 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004713 prev = XC_INST_NUM;
4714 get_token = true;
4715 paren_expr = true;
4716 rprn = bin_last = false;
4717 nexprs++;
4718 break;
4719 case BC_LEX_KEY_IBASE:
4720 case BC_LEX_KEY_LAST:
4721 case BC_LEX_KEY_OBASE:
4722 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
4723 if (BC_PARSE_LEAF(prev, rprn))
4724 return bc_error_bad_expression();
4725 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004726 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004727 get_token = true;
4728 paren_expr = true;
4729 rprn = bin_last = false;
4730 nexprs++;
4731 break;
4732 case BC_LEX_KEY_LENGTH:
4733 case BC_LEX_KEY_SQRT:
4734 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
4735 if (BC_PARSE_LEAF(prev, rprn))
4736 return bc_error_bad_expression();
4737 s = zbc_parse_builtin(t, flags, &prev);
4738 get_token = true;
4739 paren_expr = true;
4740 rprn = bin_last = false;
4741 nexprs++;
4742 break;
4743 case BC_LEX_KEY_READ:
4744 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
4745 if (BC_PARSE_LEAF(prev, rprn))
4746 return bc_error_bad_expression();
4747 s = zbc_parse_read();
4748 prev = XC_INST_READ;
4749 get_token = true;
4750 paren_expr = true;
4751 rprn = bin_last = false;
4752 nexprs++;
4753 break;
4754 case BC_LEX_KEY_SCALE:
4755 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
4756 if (BC_PARSE_LEAF(prev, rprn))
4757 return bc_error_bad_expression();
4758 s = zbc_parse_scale(&prev, flags);
4759 prev = XC_INST_SCALE;
4760 //get_token = false; - already is
4761 paren_expr = true;
4762 rprn = bin_last = false;
4763 nexprs++;
4764 break;
4765 default:
4766 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004767 }
4768
Denys Vlasenkobb116032018-12-25 01:16:37 +01004769 if (s || G_interrupt) // error, or ^C: stop parsing
4770 return BC_STATUS_FAILURE;
4771 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004772 s = zxc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004773 if (s) return s;
4774 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004775 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004776 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004777
4778 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004779 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004780 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004781
4782 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004783 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004784
Denys Vlasenko10bde142018-12-27 18:23:58 +01004785 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004786
Denys Vlasenko69560f42018-12-24 14:14:23 +01004787 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004788 bc_vec_pop(&p->ops);
4789 }
4790
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004791 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004792 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004793
Gavin Howard01055ba2018-11-03 11:00:21 -06004794 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004795 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004796 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4797 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004798 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004799 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004800 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4801 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004802 }
4803
4804 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004805 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004806 xc_parse_push(XC_INST_PRINT);
4807 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004808 }
4809
Denys Vlasenko17df8822018-12-14 23:00:24 +01004810 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004811 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004812}
4813
Gavin Howard01055ba2018-11-03 11:00:21 -06004814#endif // ENABLE_BC
4815
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004816#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004817
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004818static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004819{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004820 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004821 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004822
Denys Vlasenko10bde142018-12-27 18:23:58 +01004823 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004824 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004825 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004826
Denys Vlasenko10bde142018-12-27 18:23:58 +01004827 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004828
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004829 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004830}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004831#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004832
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004833static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004834{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004835 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004836 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004837 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004838
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004839 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004840
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004841 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004842 xc_parse_push(XC_INST_STR);
4843 xc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004844 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004845
4846 // Explanation needed here
Denys Vlasenko10bde142018-12-27 18:23:58 +01004847 xc_program_add_fn();
4848 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004849
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004850 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004851}
4852
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004853static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004854{
4855 BcStatus s;
4856
Denys Vlasenko10bde142018-12-27 18:23:58 +01004857 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004858 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004859 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004860 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004861 }
4862
4863 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004864 xc_parse_push(DC_INST_SWAP);
4865 xc_parse_push(XC_INST_ASSIGN);
4866 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004867 }
4868
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004869 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004870}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004871#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004872
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004873static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004874{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004875 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004876 BcStatus s;
4877
Denys Vlasenko10bde142018-12-27 18:23:58 +01004878 xc_parse_push(inst);
4879 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004880
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004881 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004882 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004883
Denys Vlasenko10bde142018-12-27 18:23:58 +01004884 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004885 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004886
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004887 // Note that 'else' part can not be on the next line:
4888 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4889 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004890 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004891 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004892 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004893 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004894 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004895 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004896 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004897
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004898 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004899}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004900#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004901
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004902static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004903{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004904 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004905 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004906 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004907
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004908 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004909 s = BC_STATUS_SUCCESS;
4910 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004911 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004912 case XC_LEX_OP_REL_EQ:
4913 case XC_LEX_OP_REL_LE:
4914 case XC_LEX_OP_REL_GE:
4915 case XC_LEX_OP_REL_NE:
4916 case XC_LEX_OP_REL_LT:
4917 case XC_LEX_OP_REL_GT:
4918 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
4919 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
4920 get_token = false;
4921 break;
4922 case DC_LEX_SCOLON:
4923 case DC_LEX_COLON:
4924 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
4925 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
4926 break;
4927 case XC_LEX_STR:
4928 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
4929 dc_parse_string();
4930 break;
4931 case XC_LEX_NEG:
4932 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004933 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004934 if (s) RETURN_STATUS(s);
4935 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004936 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004937 xc_parse_pushNUM();
4938 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004939 break;
4940 case XC_LEX_NUMBER:
4941 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004942 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004943 break;
4944 case DC_LEX_READ:
4945 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004946 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004947 break;
4948 case DC_LEX_OP_ASSIGN:
4949 case DC_LEX_STORE_PUSH:
4950 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
4951 assign = (t == DC_LEX_OP_ASSIGN);
4952 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
4953 s = zdc_parse_mem(inst, true, assign);
4954 break;
4955 case DC_LEX_LOAD:
4956 case DC_LEX_LOAD_POP:
4957 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
4958 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
4959 s = zdc_parse_mem(inst, true, false);
4960 break;
4961 case DC_LEX_STORE_IBASE:
4962 case DC_LEX_STORE_SCALE:
4963 case DC_LEX_STORE_OBASE:
4964 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
4965 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
4966 s = zdc_parse_mem(inst, false, true);
4967 break;
4968 default:
4969 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4970 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004971 }
4972
Denys Vlasenko10bde142018-12-27 18:23:58 +01004973 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004974
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004975 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004976 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004977}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004978#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004979
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004980static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004981{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004982 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004983 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004984
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004985 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004986 if (i >= 0) {
4987 BcInst inst = dc_LEX_to_INST[i];
4988 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004989 xc_parse_push(inst);
4990 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004991 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004992 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004993 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004994}
4995#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
4996
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004997static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004998{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004999 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005000 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5001 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005002 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005003 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005004 }
5005
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005006 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005007 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005008}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005009#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005010
Gavin Howard01055ba2018-11-03 11:00:21 -06005011#endif // ENABLE_DC
5012
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005013//
5014// Execution engine
5015//
5016
5017#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5018#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5019
Denys Vlasenko10bde142018-12-27 18:23:58 +01005020static BcVec* xc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005021{
Gavin Howard01055ba2018-11-03 11:00:21 -06005022 BcId e, *ptr;
5023 BcVec *v, *map;
5024 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005025 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005026
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005027 v = var ? &G.prog.vars : &G.prog.arrs;
5028 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005029
5030 e.name = id;
5031 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005032 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005033
5034 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005035 BcVec v2;
5036 bc_array_init(&v2, var);
5037 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005038 }
5039
5040 ptr = bc_vec_item(map, i);
5041 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005042 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005043}
5044
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005045// 'num' need not be initialized on entry
Denys Vlasenko10bde142018-12-27 18:23:58 +01005046static BC_STATUS zxc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005047{
Gavin Howard01055ba2018-11-03 11:00:21 -06005048 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005049 case XC_RESULT_STR:
5050 case XC_RESULT_TEMP:
5051 case XC_RESULT_IBASE:
5052 case XC_RESULT_SCALE:
5053 case XC_RESULT_OBASE:
5054 *num = &r->d.n;
5055 break;
5056 case XC_RESULT_CONSTANT: {
5057 BcStatus s;
5058 char *str;
5059 unsigned base_t;
5060 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005061
Denys Vlasenko10bde142018-12-27 18:23:58 +01005062 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005063 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005064
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005065 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005066
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005067 hex = hex && len == 1;
5068 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005069 s = zxc_num_parse(&r->d.n, str, base_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005070 if (s) {
5071 bc_num_free(&r->d.n);
5072 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005073 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005074 *num = &r->d.n;
5075 r->t = XC_RESULT_TEMP;
5076 break;
5077 }
5078 case XC_RESULT_VAR:
5079 case XC_RESULT_ARRAY:
5080 case XC_RESULT_ARRAY_ELEM: {
5081 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005082
Denys Vlasenko10bde142018-12-27 18:23:58 +01005083 v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005084
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005085 if (r->t == XC_RESULT_ARRAY_ELEM) {
5086 v = bc_vec_top(v);
5087 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5088 *num = bc_vec_item(v, r->d.id.idx);
5089 } else
5090 *num = bc_vec_top(v);
5091 break;
5092 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005093#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005094 case BC_RESULT_LAST:
5095 *num = &G.prog.last;
5096 break;
5097 case BC_RESULT_ONE:
5098 *num = &G.prog.one;
5099 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005100#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005101#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005102 default:
5103 // Testing the theory that dc does not reach LAST/ONE
5104 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005105#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005106 }
5107
Denys Vlasenko29301232018-12-11 15:29:32 +01005108 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005109}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005110#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005111
Denys Vlasenko10bde142018-12-27 18:23:58 +01005112static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005113 BcResult **r, BcNum **rn, bool assign)
5114{
5115 BcStatus s;
5116 bool hex;
5117 BcResultType lt, rt;
5118
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005119 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005120 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005121
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005122 *r = bc_vec_item_rev(&G.prog.results, 0);
5123 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005124
5125 lt = (*l)->t;
5126 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005127 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005128
Denys Vlasenko10bde142018-12-27 18:23:58 +01005129 s = zxc_program_num(*l, ln, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005130 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005131 s = zxc_program_num(*r, rn, hex);
Denys Vlasenko29301232018-12-11 15:29:32 +01005132 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005133
5134 // We run this again under these conditions in case any vector has been
5135 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005136 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005137 s = zxc_program_num(*l, ln, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005138 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005139 }
5140
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005141 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005142 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005143 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005144 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005145
Denys Vlasenko29301232018-12-11 15:29:32 +01005146 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005147}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005148#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005149
Denys Vlasenko10bde142018-12-27 18:23:58 +01005150static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005151{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005152 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005153 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005154 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005155}
5156
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005157// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005158static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005159{
5160 BcStatus s;
5161
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005162 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005163 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005164 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005165
Denys Vlasenko10bde142018-12-27 18:23:58 +01005166 s = zxc_program_num(*r, n, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005167 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005168
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005169 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005170 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005171
Denys Vlasenko29301232018-12-11 15:29:32 +01005172 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005173}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005174#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005175
Denys Vlasenko10bde142018-12-27 18:23:58 +01005176static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005177{
5178 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005179 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005180}
5181
Denys Vlasenko10bde142018-12-27 18:23:58 +01005182static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005183{
5184 BcStatus s;
5185 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005186 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005187
Denys Vlasenko10bde142018-12-27 18:23:58 +01005188 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005189 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005190 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005191
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005192 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005193 IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005194 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005195 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005196
Denys Vlasenko259137d2018-12-11 19:42:05 +01005197 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005198 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005199 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005200 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005201}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005202#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005203
Denys Vlasenko10bde142018-12-27 18:23:58 +01005204static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005205{
5206 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005207 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005208 BcVec buf;
5209 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005210 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005211
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005212 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005213 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005214
Denys Vlasenko10bde142018-12-27 18:23:58 +01005215 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005216 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005218 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005219 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005220 //G.err_line = G.prs.lex_line = 1; - not needed, error line info is not printed for read()
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005221
Denys Vlasenko10bde142018-12-27 18:23:58 +01005222 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005223 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005224 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005225 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005226 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005227 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005228 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005229 if (s) goto exec_err;
5230
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005231 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005232 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005233 goto exec_err;
5234 }
5235
5236 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005237 ip.inst_idx = 0;
5238 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005239
Denys Vlasenko10bde142018-12-27 18:23:58 +01005240 xc_parse_push(XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005241 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005242
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005243 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005244 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005245 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005246 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005247 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005248}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005249#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005250
Denys Vlasenko10bde142018-12-27 18:23:58 +01005251static size_t xc_program_index(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005252{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005253 unsigned char *bytes = (void*)(code + *bgn);
5254 unsigned amt;
5255 unsigned i;
5256 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005257
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005258 amt = *bytes++;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01005259 if (amt < SMALL_INDEX_LIMIT) {
5260 *bgn += 1;
5261 return amt;
5262 }
5263 amt -= SMALL_INDEX_LIMIT;
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005264 *bgn += amt + 1;
5265
5266 amt *= 8;
5267 res = 0;
5268 for (i = 0; i < amt; i += 8)
5269 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005270
5271 return res;
5272}
5273
Denys Vlasenko10bde142018-12-27 18:23:58 +01005274static char *xc_program_name(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005275{
Denys Vlasenko694d2982018-12-18 19:17:11 +01005276 code += *bgn;
Denys Vlasenkof706a182018-12-26 20:02:09 +01005277 *bgn += strlen(code) + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005278
Denys Vlasenkof706a182018-12-26 20:02:09 +01005279 return xstrdup(code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005280}
5281
Denys Vlasenko10bde142018-12-27 18:23:58 +01005282static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005283{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005284#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005285 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005286 // Example: echo '[]ap' | dc
5287 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005288 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005289 return;
5290 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005291#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005292 while (*str) {
5293 int c = *str++;
5294 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005295 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005296 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005297 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005298 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005299 case 'a':
5300 bb_putchar('\a');
5301 break;
5302 case 'b':
5303 bb_putchar('\b');
5304 break;
5305 case '\\':
5306 case 'e':
5307 bb_putchar('\\');
5308 break;
5309 case 'f':
5310 bb_putchar('\f');
5311 break;
5312 case 'n':
5313 bb_putchar('\n');
5314 G.prog.nchars = SIZE_MAX;
5315 break;
5316 case 'r':
5317 bb_putchar('\r');
5318 break;
5319 case 'q':
5320 bb_putchar('"');
5321 break;
5322 case 't':
5323 bb_putchar('\t');
5324 break;
5325 default:
5326 // Just print the backslash and following character.
5327 bb_putchar('\\');
5328 ++G.prog.nchars;
5329 bb_putchar(c);
5330 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005331 }
5332 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005333 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005334 }
5335}
5336
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005337static void bc_num_printNewline(void)
5338{
5339 if (G.prog.nchars == G.prog.len - 1) {
5340 bb_putchar('\\');
5341 bb_putchar('\n');
5342 G.prog.nchars = 0;
5343 }
5344}
5345
5346#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005347static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005348{
5349 (void) radix;
5350 bb_putchar((char) num);
5351 G.prog.nchars += width;
5352}
5353#endif
5354
5355static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5356{
5357 size_t exp, pow;
5358
5359 bc_num_printNewline();
5360 bb_putchar(radix ? '.' : ' ');
5361 ++G.prog.nchars;
5362
5363 bc_num_printNewline();
5364 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5365 continue;
5366
5367 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5368 size_t dig;
5369 bc_num_printNewline();
5370 dig = num / pow;
5371 num -= dig * pow;
5372 bb_putchar(((char) dig) + '0');
5373 }
5374}
5375
5376static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5377{
5378 if (radix) {
5379 bc_num_printNewline();
5380 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005381 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005382 }
5383
5384 bc_num_printNewline();
5385 bb_putchar(bb_hexdigits_upcase[num]);
5386 G.prog.nchars += width;
5387}
5388
5389static void bc_num_printDecimal(BcNum *n)
5390{
5391 size_t i, rdx = n->rdx - 1;
5392
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005393 if (n->neg) {
5394 bb_putchar('-');
5395 G.prog.nchars++;
5396 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005397
5398 for (i = n->len - 1; i < n->len; --i)
5399 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5400}
5401
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005402typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5403
Denys Vlasenko10bde142018-12-27 18:23:58 +01005404static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005405{
5406 BcStatus s;
5407 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005408 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005409 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005410 BcNum intp, fracp, digit, frac_len;
5411 unsigned long dig, *ptr;
5412 size_t i;
5413 bool radix;
5414
5415 if (n->len == 0) {
5416 print(0, width, false);
5417 RETURN_STATUS(BC_STATUS_SUCCESS);
5418 }
5419
5420 bc_vec_init(&stack, sizeof(long), NULL);
5421 bc_num_init(&intp, n->len);
5422 bc_num_init(&fracp, n->rdx);
5423 bc_num_init(&digit, width);
5424 bc_num_init(&frac_len, BC_NUM_INT(n));
5425 bc_num_copy(&intp, n);
5426 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005427 base.cap = ARRAY_SIZE(base_digs);
5428 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005429 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005430
5431 bc_num_truncate(&intp, intp.rdx);
5432 s = zbc_num_sub(n, &intp, &fracp, 0);
5433 if (s) goto err;
5434
5435 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005436 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005437 if (s) goto err;
5438 s = zbc_num_ulong(&digit, &dig);
5439 if (s) goto err;
5440 bc_vec_push(&stack, &dig);
5441 }
5442
5443 for (i = 0; i < stack.len; ++i) {
5444 ptr = bc_vec_item_rev(&stack, i);
5445 print(*ptr, width, false);
5446 }
5447
5448 if (!n->rdx) goto err;
5449
5450 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005451 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005452 if (s) goto err;
5453 s = zbc_num_ulong(&fracp, &dig);
5454 if (s) goto err;
5455 bc_num_ulong2num(&intp, dig);
5456 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5457 if (s) goto err;
5458 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005459 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005460 if (s) goto err;
5461 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005462 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005463 bc_num_free(&frac_len);
5464 bc_num_free(&digit);
5465 bc_num_free(&fracp);
5466 bc_num_free(&intp);
5467 bc_vec_free(&stack);
5468 RETURN_STATUS(s);
5469}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005470#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005471
Denys Vlasenko10bde142018-12-27 18:23:58 +01005472static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005473{
5474 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005475 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005476 BcNumDigitOp print;
5477 bool neg = n->neg;
5478
5479 if (neg) {
5480 bb_putchar('-');
5481 G.prog.nchars++;
5482 }
5483
5484 n->neg = false;
5485
5486 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5487 width = 1;
5488 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005489 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005490 unsigned i = G.prog.ob_t - 1;
5491 width = 0;
5492 for (;;) {
5493 width++;
5494 i /= 10;
5495 if (i == 0)
5496 break;
5497 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005498 print = bc_num_printDigits;
5499 }
5500
Denys Vlasenko10bde142018-12-27 18:23:58 +01005501 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005502 n->neg = neg;
5503
5504 RETURN_STATUS(s);
5505}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005506#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005507
Denys Vlasenko10bde142018-12-27 18:23:58 +01005508static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005509{
5510 BcStatus s = BC_STATUS_SUCCESS;
5511
5512 bc_num_printNewline();
5513
5514 if (n->len == 0) {
5515 bb_putchar('0');
5516 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005517 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005518 bc_num_printDecimal(n);
5519 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005520 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005521
5522 if (newline) {
5523 bb_putchar('\n');
5524 G.prog.nchars = 0;
5525 }
5526
5527 RETURN_STATUS(s);
5528}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005529#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005530
Denys Vlasenko10bde142018-12-27 18:23:58 +01005531static BC_STATUS zxc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005532{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005533 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005534 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005535 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005536 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005537
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005538 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005539 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005540
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005541 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005542 s = zxc_program_num(r, &num, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005543 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005544
5545 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005546 s = zxc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005547#if ENABLE_BC
5548 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5549#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005550 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005551 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005552
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005553 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005554 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005555
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005556 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005557 for (;;) {
5558 char c = *str++;
5559 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005560 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005561 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005562 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005563 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005564 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005565 xc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005566 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005567 }
5568 }
5569
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005570 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005571
Denys Vlasenko29301232018-12-11 15:29:32 +01005572 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005573}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005574#define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005575
Denys Vlasenko10bde142018-12-27 18:23:58 +01005576static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005577{
5578 BcStatus s;
5579 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005580 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005581
Denys Vlasenko10bde142018-12-27 18:23:58 +01005582 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005583 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005584
5585 bc_num_init(&res.d.n, num->len);
5586 bc_num_copy(&res.d.n, num);
5587 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5588
Denys Vlasenko10bde142018-12-27 18:23:58 +01005589 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005590
Denys Vlasenko29301232018-12-11 15:29:32 +01005591 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005592}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005593#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005594
Denys Vlasenko10bde142018-12-27 18:23:58 +01005595static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005596{
5597 BcStatus s;
5598 BcResult *opd1, *opd2, res;
5599 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005600 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005601
Denys Vlasenko10bde142018-12-27 18:23:58 +01005602 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005603 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005604
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005605 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005606
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005607 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005608 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005609 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005610 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005611 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005612 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005613 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005614 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005615 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005616 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005617 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005618 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005619 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005620 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005621 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005622 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005623 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005624 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005625 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005626 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005627 cond = (cond > 0);
5628 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005629 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005630 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005631 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005632 }
5633 }
5634
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005635 if (cond) bc_num_one(&res.d.n);
5636 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005637
Denys Vlasenko10bde142018-12-27 18:23:58 +01005638 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005639
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005640 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005641}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005642#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005643
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005644#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005645static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005646{
5647 BcNum n2;
5648 BcResult res;
5649
5650 memset(&n2, 0, sizeof(BcNum));
5651 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005652 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005653
5654 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005655 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005656 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005657 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005658 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005659 }
5660
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005661 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005662 bc_vec_push(v, &n2);
5663
Denys Vlasenko29301232018-12-11 15:29:32 +01005664 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005665}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005666#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005667#endif // ENABLE_DC
5668
Denys Vlasenko10bde142018-12-27 18:23:58 +01005669static BC_STATUS zxc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005670{
5671 BcStatus s;
5672 BcResult *ptr, r;
5673 BcVec *v;
5674 BcNum *n;
5675
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005676 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005677 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005678
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005679 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005680 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005681 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005682 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005683
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005684#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005685 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005686 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005687 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005688 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005689#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005690
Denys Vlasenko10bde142018-12-27 18:23:58 +01005691 s = zxc_program_num(ptr, &n, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005692 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005693
5694 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko10bde142018-12-27 18:23:58 +01005695 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005696
5697 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005698 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005699 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005700 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005701 bc_array_init(&r.d.v, true);
5702 bc_array_copy(&r.d.v, (BcVec *) n);
5703 }
5704
5705 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005706 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005707
Denys Vlasenko29301232018-12-11 15:29:32 +01005708 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005709}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005710#define zxc_program_copyToVar(...) (zxc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005711
Denys Vlasenko10bde142018-12-27 18:23:58 +01005712static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005713{
5714 BcStatus s;
5715 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005716 BcNum *l, *r;
5717 bool assign = (inst == XC_INST_ASSIGN);
5718 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005719
Denys Vlasenko10bde142018-12-27 18:23:58 +01005720 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005721 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005722
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005723 ib = left->t == XC_RESULT_IBASE;
5724 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005725
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005726#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005727 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005728 BcVec *v;
5729
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005730 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005731 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005732 v = xc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005733
Denys Vlasenkofa210792018-12-19 19:35:40 +01005734 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005735 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005736#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005737
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005738 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005739 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005740 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005741 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005742 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005743
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005744#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005745 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005746 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005747
5748 if (assign)
5749 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005750 else {
5751 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005752 IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005753 }
5754 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005755#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005756 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005757#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005758
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005759 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005760 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005761 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5762 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5763 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005764 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005765 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005766 size_t max;
5767 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005768
Denys Vlasenko29301232018-12-11 15:29:32 +01005769 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005770 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005771 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005772 if (sc) {
5773 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005774 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005775 } else {
5776 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005777 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005778 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005779 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005780 }
5781
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005782 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005783 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005784
5785 *ptr = (size_t) val;
5786 s = BC_STATUS_SUCCESS;
5787 }
5788
5789 bc_num_init(&res.d.n, l->len);
5790 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005791 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005792
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005793 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005794}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005795#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005796
Denys Vlasenko416ce762018-12-02 20:57:17 +01005797#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005798#define xc_program_pushVar(code, bgn, pop, copy) \
5799 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005800// for bc, 'pop' and 'copy' are always false
5801#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005802static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005803 bool pop, bool copy)
5804{
Gavin Howard01055ba2018-11-03 11:00:21 -06005805 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005806 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005807
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005808 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005809 r.d.id.name = name;
5810
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005811#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005812 if (pop || copy) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005813 BcVec *v = xc_program_search(name, true);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005814 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005815
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005816 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005817 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005818 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005819 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005820
5821 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005822 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005823 bc_num_init_DEF_SIZE(&r.d.n);
5824 bc_num_copy(&r.d.n, num);
5825 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005826 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005827 r.d.id.idx = num->rdx;
5828 }
5829
5830 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005831 }
5832#endif // ENABLE_DC
5833
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005834 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005835
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005836 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005837}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005838#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005839
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005840static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005841{
5842 BcStatus s = BC_STATUS_SUCCESS;
5843 BcResult r;
5844 BcNum *num;
5845
Denys Vlasenko10bde142018-12-27 18:23:58 +01005846 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005847
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005848 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005849 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005850 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005851 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005852 BcResult *operand;
5853 unsigned long temp;
5854
Denys Vlasenko10bde142018-12-27 18:23:58 +01005855 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005856 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005857 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005858 if (s) goto err;
5859
5860 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005861 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005862 goto err;
5863 }
5864
5865 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005866 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005867 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005868 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005869 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005870 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005871}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005872#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005873
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005874#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005875static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005876{
5877 BcStatus s;
5878 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005879 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005880 char inst2 = inst;
5881
Denys Vlasenko10bde142018-12-27 18:23:58 +01005882 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005883 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884
5885 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005886 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005887 bc_num_init(&copy.d.n, num->len);
5888 bc_num_copy(&copy.d.n, num);
5889 }
5890
5891 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005892 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5893 ? BC_INST_ASSIGN_PLUS
5894 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005895
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005896 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005897 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005898 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005899
5900 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005901 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902 }
5903
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_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005907
Denys Vlasenko29301232018-12-11 15:29:32 +01005908static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005909{
Gavin Howard01055ba2018-11-03 11:00:21 -06005910 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005911 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005912 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005913 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005914 BcResult *arg;
5915
Denys Vlasenko10bde142018-12-27 18:23:58 +01005916 nparams = xc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005917 ip.inst_idx = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005918 ip.func = xc_program_index(code, idx);
5919 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005920
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005921 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005922 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005923 }
5924 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005925 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005926 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005927 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005928
5929 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005930 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005931
5932 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005933 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005934
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005935 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005936 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005937
Denys Vlasenko10bde142018-12-27 18:23:58 +01005938 s = zxc_program_copyToVar(a->name, a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005939 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005940 }
5941
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005942 a = bc_vec_item(&func->autos, i);
5943 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005944 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005945
Denys Vlasenko10bde142018-12-27 18:23:58 +01005946 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005948 BcNum n2;
5949 bc_num_init_DEF_SIZE(&n2);
5950 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005951 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005952 BcVec v2;
5953 bc_array_init(&v2, true);
5954 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005955 }
5956 }
5957
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005958 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005959
Denys Vlasenko29301232018-12-11 15:29:32 +01005960 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005961}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005962#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005963
Denys Vlasenko29301232018-12-11 15:29:32 +01005964static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005965{
Gavin Howard01055ba2018-11-03 11:00:21 -06005966 BcResult res;
5967 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005968 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005969 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005970 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005971
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005972 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 +01005973 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005974
Denys Vlasenko10bde142018-12-27 18:23:58 +01005975 f = xc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005976 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005977
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005978 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005979 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005980 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005981 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenko10bde142018-12-27 18:23:58 +01005983 s = zxc_program_num(operand, &num, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01005984 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005985 bc_num_init(&res.d.n, num->len);
5986 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005987 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005988 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01005989 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005990 }
5991
5992 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005993 a = (void*)f->autos.v;
5994 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005996 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005997 bc_vec_pop(v);
5998 }
5999
Denys Vlasenko24e41942018-12-21 23:01:26 +01006000 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006001 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006002 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006003
Denys Vlasenko29301232018-12-11 15:29:32 +01006004 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006005}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006006#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006007#endif // ENABLE_BC
6008
Denys Vlasenko10bde142018-12-27 18:23:58 +01006009static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006010{
6011 return (unsigned long) n->rdx;
6012}
6013
Denys Vlasenko10bde142018-12-27 18:23:58 +01006014static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006015{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006016 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006017
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006018 if (n->rdx != len) return len;
6019 for (;;) {
6020 if (len == 0) break;
6021 len--;
6022 if (n->num[len] != 0) break;
6023 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 return len;
6025}
6026
Denys Vlasenko10bde142018-12-27 18:23:58 +01006027static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006028{
6029 BcStatus s;
6030 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006031 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006032 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006033 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006034
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006035 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006036 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006037 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006038
Denys Vlasenko10bde142018-12-27 18:23:58 +01006039 s = zxc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006040 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006041
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006042#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006043 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006044 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006045#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006046
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006047 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006048
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006049 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006050 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006051#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006052 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006053 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006054 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006055#endif
6056#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006058 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006059 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006060
Denys Vlasenko10bde142018-12-27 18:23:58 +01006061 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006062 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006063 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006064#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006065 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006066 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006067 }
6068
Denys Vlasenko10bde142018-12-27 18:23:58 +01006069 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006070
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006071 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006072}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006073#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006074
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006075#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006076static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006077{
6078 BcStatus s;
6079 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006080 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006081
Denys Vlasenko10bde142018-12-27 18:23:58 +01006082 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006083 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006085 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006086 bc_num_init(&res2.d.n, n2->len);
6087
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006088 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 if (s) goto err;
6090
Denys Vlasenko10bde142018-12-27 18:23:58 +01006091 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006092 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006093 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006094
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006095 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006096 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 bc_num_free(&res2.d.n);
6098 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006100}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006101#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006102
Denys Vlasenkofa210792018-12-19 19:35:40 +01006103static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006104{
6105 BcStatus s;
6106 BcResult *r1, *r2, *r3, res;
6107 BcNum *n1, *n2, *n3;
6108
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006109 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006110 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006111 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006112 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006114 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006115 s = zxc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006116 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006117 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006118 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006119
6120 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006121 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006122 if (r1->t == r2->t) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006123 s = zxc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006124 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006126 if (r1->t == r3->t) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006127 s = zxc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006128 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006129 }
6130 }
6131
6132 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006133 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006134 if (s) goto err;
6135
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006136 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006137 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006138
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006139 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006140 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006141 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006142 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006143}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006144#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006145
Denys Vlasenkofa210792018-12-19 19:35:40 +01006146static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006147{
Gavin Howard01055ba2018-11-03 11:00:21 -06006148 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006149 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006151 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006152
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006153 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006154 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006155 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006156}
6157
Denys Vlasenkofa210792018-12-19 19:35:40 +01006158static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006159{
6160 BcStatus s;
6161 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006162 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006163 char **strs;
6164 char *str;
6165 char c;
6166 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006167
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006168 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006169 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006170 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006171
Denys Vlasenko10bde142018-12-27 18:23:58 +01006172 s = zxc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006173 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174
6175 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006176 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006177 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006178 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006179
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006180 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 bc_num_copy(&n, num);
6182 bc_num_truncate(&n, n.rdx);
6183
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006184 strmb.cap = ARRAY_SIZE(strmb_digs);
6185 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006186 bc_num_ulong2num(&strmb, 0x100);
6187 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006188
Gavin Howard01055ba2018-11-03 11:00:21 -06006189 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006190 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 if (s) goto num_err;
6192
6193 c = (char) val;
6194
6195 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006196 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006197 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006198 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006199 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006200 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006201 }
6202
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006203 strs = (void*)G.prog.strs.v;
6204 for (idx = 0; idx < G.prog.strs.len; idx++) {
6205 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6206 goto dup;
6207 }
6208 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006209 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006211 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006212 bc_vec_push(&G.prog.strs, &str);
6213 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006214 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006215 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006216 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006217
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006218 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006219 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006220 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006221 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006222}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006223#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006224
Denys Vlasenkofa210792018-12-19 19:35:40 +01006225static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006226{
6227 BcStatus s;
6228 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006229 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006230 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006231
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006232 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006233 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006234 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006235
Denys Vlasenko10bde142018-12-27 18:23:58 +01006236 s = zxc_program_num(r, &n, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01006237 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006238
Denys Vlasenko57734c92018-12-17 21:14:05 +01006239 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006240 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006241 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006242 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006243 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006244 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006245 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006246 }
6247
Denys Vlasenko29301232018-12-11 15:29:32 +01006248 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006249}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006250#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006251
Denys Vlasenkofa210792018-12-19 19:35:40 +01006252static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006253{
6254 BcStatus s;
6255 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006256 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006257 unsigned long val;
6258
Denys Vlasenko10bde142018-12-27 18:23:58 +01006259 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006260 if (s) RETURN_STATUS(s);
6261 s = zbc_num_ulong(num, &val);
6262 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006264 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006266 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006267 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006268 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006269 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006270 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006271
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006272 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006273
Denys Vlasenko29301232018-12-11 15:29:32 +01006274 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006275}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006276#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenkofa210792018-12-19 19:35:40 +01006278static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006279{
6280 BcStatus s = BC_STATUS_SUCCESS;
6281 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006282 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006283 BcInstPtr ip;
6284 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006285
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006286 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006287 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006288
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006289 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006290
6291 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006292 BcNum *n = n; // for compiler
6293 bool exec;
6294 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006295 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006296 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006297
Denys Vlasenkof706a182018-12-26 20:02:09 +01006298 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 (*bgn) += 1;
6300 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006301 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006302
6303 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006304 name = then_name;
6305 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006306 exec = true;
6307 name = else_name;
6308 }
6309
6310 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006311 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006312 v = xc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006313 n = bc_vec_top(v);
6314 }
6315
6316 free(then_name);
6317 free(else_name);
6318
6319 if (!exec) goto exit;
6320 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006321 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006322 goto exit;
6323 }
6324
6325 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006326 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006327 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006328 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006329 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006330 BcNum *n;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006331 s = zxc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006332 if (s || !BC_PROG_STR(n)) goto exit;
6333 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006334 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006335 goto exit;
6336 }
6337
6338 fidx = sidx + BC_PROG_REQ_FUNCS;
6339
Denys Vlasenko10bde142018-12-27 18:23:58 +01006340 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341
6342 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006343 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006344 char *str;
6345
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006346 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006347 xc_parse_create(fidx);
6348 str = *xc_program_str(sidx);
6349 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006350 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006351
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006352 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006353 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006354 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006355 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006356 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006357 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006358 G.prs = sv_parse; // struct copy
6359 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006360 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006361 bc_vec_pop_all(&f->code);
6362 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006363 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006364 }
6365
Denys Vlasenko24e41942018-12-21 23:01:26 +01006366 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006367 ip.func = fidx;
6368
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006369 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006370 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006371
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006372 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006373 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006374 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006375 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006376}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006377#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006378#endif // ENABLE_DC
6379
Denys Vlasenko10bde142018-12-27 18:23:58 +01006380static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006381{
Gavin Howard01055ba2018-11-03 11:00:21 -06006382 BcResult res;
6383 unsigned long val;
6384
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006385 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006386 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006387 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006388 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006389 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006390 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006391 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006392
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006393 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006394 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006395 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006396}
6397
Denys Vlasenko10bde142018-12-27 18:23:58 +01006398static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006399{
Gavin Howard01055ba2018-11-03 11:00:21 -06006400 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006401 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006402 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006404
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006405 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006406 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6407 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006408 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006409 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006410
Denys Vlasenko24e41942018-12-21 23:01:26 +01006411 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 -06006412 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006413#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006414 case BC_INST_JUMP_ZERO: {
6415 BcNum *num;
6416 bool zero;
6417 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006418 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006419 if (s) RETURN_STATUS(s);
6420 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6421 bc_vec_pop(&G.prog.results);
6422 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006423 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006424 break;
6425 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006426 // else: fall through
6427 }
6428 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006429 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006430 size_t *addr = bc_vec_item(&func->labels, idx);
6431 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6432 ip->inst_idx = *addr;
6433 break;
6434 }
6435 case BC_INST_CALL:
6436 dbg_exec("BC_INST_CALL:");
6437 s = zbc_program_call(code, &ip->inst_idx);
6438 goto read_updated_ip;
6439 case BC_INST_INC_PRE:
6440 case BC_INST_DEC_PRE:
6441 case BC_INST_INC_POST:
6442 case BC_INST_DEC_POST:
6443 dbg_exec("BC_INST_INCDEC:");
6444 s = zbc_program_incdec(inst);
6445 break;
6446 case BC_INST_HALT:
6447 dbg_exec("BC_INST_HALT:");
6448 QUIT_OR_RETURN_TO_MAIN;
6449 break;
6450 case XC_INST_RET:
6451 case BC_INST_RET0:
6452 dbg_exec("BC_INST_RET[0]:");
6453 s = zbc_program_return(inst);
6454 goto read_updated_ip;
6455 case XC_INST_BOOL_OR:
6456 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006457#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006458 case XC_INST_REL_EQ:
6459 case XC_INST_REL_LE:
6460 case XC_INST_REL_GE:
6461 case XC_INST_REL_NE:
6462 case XC_INST_REL_LT:
6463 case XC_INST_REL_GT:
6464 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006465 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006466 break;
6467 case XC_INST_READ:
6468 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006469 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006470 goto read_updated_ip;
6471 case XC_INST_VAR:
6472 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006473 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006474 break;
6475 case XC_INST_ARRAY_ELEM:
6476 case XC_INST_ARRAY:
6477 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6478 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6479 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006480#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006481 case BC_INST_LAST:
6482 dbg_exec("BC_INST_LAST:");
6483 r.t = BC_RESULT_LAST;
6484 bc_vec_push(&G.prog.results, &r);
6485 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006486#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006487 case XC_INST_IBASE:
6488 case XC_INST_OBASE:
6489 case XC_INST_SCALE:
6490 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006491 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006492 break;
6493 case XC_INST_SCALE_FUNC:
6494 case XC_INST_LENGTH:
6495 case XC_INST_SQRT:
6496 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006497 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006498 break;
6499 case XC_INST_NUM:
6500 dbg_exec("XC_INST_NUM:");
6501 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006502 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006503 bc_vec_push(&G.prog.results, &r);
6504 break;
6505 case XC_INST_POP:
6506 dbg_exec("XC_INST_POP:");
6507 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6508 s = bc_error_stack_has_too_few_elements();
6509 else
6510 bc_vec_pop(&G.prog.results);
6511 break;
6512 case XC_INST_PRINT:
6513 case XC_INST_PRINT_POP:
6514 case XC_INST_PRINT_STR:
6515 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006516 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006517 break;
6518 case XC_INST_STR:
6519 dbg_exec("XC_INST_STR:");
6520 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006521 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006522 bc_vec_push(&G.prog.results, &r);
6523 break;
6524 case XC_INST_POWER:
6525 case XC_INST_MULTIPLY:
6526 case XC_INST_DIVIDE:
6527 case XC_INST_MODULUS:
6528 case XC_INST_PLUS:
6529 case XC_INST_MINUS:
6530 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006531 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006532 break;
6533 case XC_INST_BOOL_NOT: {
6534 BcNum *num;
6535 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006536 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006537 if (s) RETURN_STATUS(s);
6538 bc_num_init_DEF_SIZE(&r.d.n);
6539 if (bc_num_cmp(num, &G.prog.zero) == 0)
6540 bc_num_one(&r.d.n);
6541 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006542 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006543 break;
6544 }
6545 case XC_INST_NEG:
6546 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006547 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006548 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006549#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006550 case BC_INST_ASSIGN_POWER:
6551 case BC_INST_ASSIGN_MULTIPLY:
6552 case BC_INST_ASSIGN_DIVIDE:
6553 case BC_INST_ASSIGN_MODULUS:
6554 case BC_INST_ASSIGN_PLUS:
6555 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006556#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006557 case XC_INST_ASSIGN:
6558 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006559 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006560 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006561#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006562 case DC_INST_POP_EXEC:
6563 dbg_exec("DC_INST_POP_EXEC:");
6564 bc_vec_pop(&G.prog.exestack);
6565 goto read_updated_ip;
6566 case DC_INST_MODEXP:
6567 dbg_exec("DC_INST_MODEXP:");
6568 s = zdc_program_modexp();
6569 break;
6570 case DC_INST_DIVMOD:
6571 dbg_exec("DC_INST_DIVMOD:");
6572 s = zdc_program_divmod();
6573 break;
6574 case DC_INST_EXECUTE:
6575 case DC_INST_EXEC_COND:
6576 dbg_exec("DC_INST_EXEC[_COND]:");
6577 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6578 goto read_updated_ip;
6579 case DC_INST_PRINT_STACK: {
6580 size_t idx;
6581 dbg_exec("DC_INST_PRINT_STACK:");
6582 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006583 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006584 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006585 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006586 break;
6587 }
6588 case DC_INST_CLEAR_STACK:
6589 dbg_exec("DC_INST_CLEAR_STACK:");
6590 bc_vec_pop_all(&G.prog.results);
6591 break;
6592 case DC_INST_STACK_LEN:
6593 dbg_exec("DC_INST_STACK_LEN:");
6594 dc_program_stackLen();
6595 break;
6596 case DC_INST_DUPLICATE:
6597 dbg_exec("DC_INST_DUPLICATE:");
6598 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6599 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6600 ptr = bc_vec_top(&G.prog.results);
6601 dc_result_copy(&r, ptr);
6602 bc_vec_push(&G.prog.results, &r);
6603 break;
6604 case DC_INST_SWAP: {
6605 BcResult *ptr2;
6606 dbg_exec("DC_INST_SWAP:");
6607 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6608 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6609 ptr = bc_vec_item_rev(&G.prog.results, 0);
6610 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6611 memcpy(&r, ptr, sizeof(BcResult));
6612 memcpy(ptr, ptr2, sizeof(BcResult));
6613 memcpy(ptr2, &r, sizeof(BcResult));
6614 break;
6615 }
6616 case DC_INST_ASCIIFY:
6617 dbg_exec("DC_INST_ASCIIFY:");
6618 s = zdc_program_asciify();
6619 break;
6620 case DC_INST_PRINT_STREAM:
6621 dbg_exec("DC_INST_PRINT_STREAM:");
6622 s = zdc_program_printStream();
6623 break;
6624 case DC_INST_LOAD:
6625 case DC_INST_PUSH_VAR: {
6626 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006627 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006628 break;
6629 }
6630 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006631 char *name = xc_program_name(code, &ip->inst_idx);
6632 s = zxc_program_copyToVar(name, true);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006633 free(name);
6634 break;
6635 }
6636 case DC_INST_QUIT:
6637 dbg_exec("DC_INST_QUIT:");
6638 if (G.prog.exestack.len <= 2)
6639 QUIT_OR_RETURN_TO_MAIN;
6640 bc_vec_npop(&G.prog.exestack, 2);
6641 goto read_updated_ip;
6642 case DC_INST_NQUIT:
6643 dbg_exec("DC_INST_NQUIT:");
6644 s = zdc_program_nquit();
6645 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006646#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006647 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006648 // Instruction stack has changed, read new pointers
6649 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006650 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006651 code = func->code.v;
6652 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 }
6654
Denys Vlasenkod38af482018-12-04 19:11:02 +01006655 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006656 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006657 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006658 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006659
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006660 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006661 }
6662
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006663 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006664}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006665#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006666
Denys Vlasenko10bde142018-12-27 18:23:58 +01006667static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006668{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006669 char *lenv;
6670 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006671
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006672 lenv = getenv(var);
6673 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006674 if (!lenv) return len;
6675
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006676 len = bb_strtou(lenv, NULL, 10) - 1;
6677 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006678 len = BC_NUM_PRINT_WIDTH;
6679
6680 return len;
6681}
6682
Denys Vlasenko10bde142018-12-27 18:23:58 +01006683static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006684{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006685 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006686
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006687 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006688 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006689 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006690
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006691 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006692 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006693 BcInstPtr *ip;
6694 BcFunc *f;
6695
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006696 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006697 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006698#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006699 if (G.prs.lex == BC_LEX_SCOLON
6700 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006701 ) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006702 s = zxc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006703 if (s) goto err;
6704 goto check_eof;
6705 }
6706
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006707 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006708 if (s) goto err;
6709
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006710 // Check that next token is a correct stmt delimiter -
6711 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006712 if (G.prs.lex != BC_LEX_SCOLON
6713 && G.prs.lex != XC_LEX_NLINE
6714 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006715 ) {
6716 const char *err_at;
6717//TODO: commonalize for other parse errors:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006718 err_at = G.prs.lex_next_at ? G.prs.lex_next_at : "UNKNOWN";
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006719 bc_error_fmt("bad statement terminator at '%.*s'",
6720 (int)(strchrnul(err_at, '\n') - err_at),
6721 err_at
6722 );
6723 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006724 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006725 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006726 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006727#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006728 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006729#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006730 // Most of dc parsing assumes all whitespace,
6731 // including '\n', is eaten.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006732 while (G.prs.lex == XC_LEX_NLINE) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006733 s = zxc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006734 if (s) goto err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006735 if (G.prs.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006736 goto done;
6737 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006738 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006739#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006740 }
6741 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006742 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006743 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006744 RETURN_STATUS(BC_STATUS_FAILURE);
6745 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006746
Denys Vlasenko39287e02018-12-22 02:23:08 +01006747 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006748 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006749 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006750 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006751 break;
6752 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006753
6754 ip = (void*)G.prog.exestack.v;
6755#if SANITY_CHECKS
6756 if (G.prog.exestack.len != 1) // should have only main's IP
6757 bb_error_msg_and_die("BUG:call stack");
6758 if (ip->func != BC_PROG_MAIN)
6759 bb_error_msg_and_die("BUG:not MAIN");
6760#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006761 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006762 // bc discards strings, constants and code after each
6763 // top-level statement in the "main program".
6764 // This prevents "yes 1 | bc" from growing its memory
6765 // without bound. This can be done because data stack
6766 // is empty and thus can't hold any references to
6767 // strings or constants, there is no generated code
6768 // which can hold references (after we discard one
6769 // we just executed). Code of functions can have references,
6770 // but bc stores function strings/constants in per-function
6771 // storage.
6772 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006773#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006774 if (G.prog.results.len != 0) // should be empty
6775 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006776#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006777 IF_BC(bc_vec_pop_all(&f->strs);)
6778 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006779 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006780 if (G.prog.results.len == 0
6781 && G.prog.vars.len == 0
6782 ) {
6783 // If stack is empty and no registers exist (TODO: or they are all empty),
6784 // we can get rid of accumulated strings and constants.
6785 // In this example dc process should not grow
6786 // its memory consumption with time:
6787 // yes 1pc | dc
6788 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6789 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6790 }
6791 // The code is discarded always (below), thus this example
6792 // should also not grow its memory consumption with time,
6793 // even though its data stack is not empty:
6794 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006795 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006796 // We drop generated and executed code for both bc and dc:
6797 bc_vec_pop_all(&f->code);
6798 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006799 }
Denys Vlasenko8a56e362018-12-26 19:09:23 +01006800 IF_DC(done:)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006801 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006802 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006803}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006804#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006805
Denys Vlasenko10bde142018-12-27 18:23:58 +01006806static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006807{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006808 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006809 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006810 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006811 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006812
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006813 G.prs.lex_filename = filename;
6814 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006815 G.err_line = G.prs.lex_line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06006816
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006817 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006818 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006819 // We do not stop looping on errors here if reading stdin.
6820 // Example: start interactive bc and enter "return".
6821 // It should say "'return' not in a function"
6822 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006823 } while (G.prs.lex_input_fp == stdin);
6824 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006825 RETURN_STATUS(s);
6826}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006827#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006828
Denys Vlasenko10bde142018-12-27 18:23:58 +01006829static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006830{
6831 BcStatus s;
6832 FILE *fp;
6833
6834 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006835 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006836 fclose(fp);
6837
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006838 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006839}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006840#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006841
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006842#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006843static void bc_vm_info(void)
6844{
6845 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006846 "Adapted from https://github.com/gavinhoward/bc\n"
6847 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006848 , applet_name);
6849}
6850
6851static void bc_args(char **argv)
6852{
6853 unsigned opts;
6854 int i;
6855
6856 GETOPT_RESET();
6857#if ENABLE_FEATURE_BC_LONG_OPTIONS
6858 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6859 "warn\0" No_argument "w"
6860 "version\0" No_argument "v"
6861 "standard\0" No_argument "s"
6862 "quiet\0" No_argument "q"
6863 "mathlib\0" No_argument "l"
6864 "interactive\0" No_argument "i"
6865 );
6866#else
6867 opts = option_mask32 |= getopt32(argv, "wvsqli");
6868#endif
6869 if (getenv("POSIXLY_CORRECT"))
6870 option_mask32 |= BC_FLAG_S;
6871
6872 if (opts & BC_FLAG_V) {
6873 bc_vm_info();
6874 exit(0);
6875 }
6876
6877 for (i = optind; argv[i]; ++i)
6878 bc_vec_push(&G.files, argv + i);
6879}
6880
6881static void bc_vm_envArgs(void)
6882{
6883 BcVec v;
6884 char *buf;
6885 char *env_args = getenv("BC_ENV_ARGS");
6886
6887 if (!env_args) return;
6888
6889 G.env_args = xstrdup(env_args);
6890 buf = G.env_args;
6891
6892 bc_vec_init(&v, sizeof(char *), NULL);
6893
6894 while (*(buf = skip_whitespace(buf)) != '\0') {
6895 bc_vec_push(&v, &buf);
6896 buf = skip_non_whitespace(buf);
6897 if (!*buf)
6898 break;
6899 *buf++ = '\0';
6900 }
6901
6902 // NULL terminate, and pass argv[] so that first arg is argv[1]
6903 if (sizeof(int) == sizeof(char*)) {
6904 bc_vec_push(&v, &const_int_0);
6905 } else {
6906 static char *const nullptr = NULL;
6907 bc_vec_push(&v, &nullptr);
6908 }
6909 bc_args(((char **)v.v) - 1);
6910
6911 bc_vec_free(&v);
6912}
6913
6914static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006915 "scale=20"
6916"\n" "define e(x){"
6917"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006918////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6919//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6920//e(-.998896): GNU:.36828580434569428695
6921// above code:.36828580434569428696
6922// actual value:.3682858043456942869594...
6923// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006924"\n" "b=ibase"
6925"\n" "ibase=A"
6926"\n" "if(x<0){"
6927"\n" "n=1"
6928"\n" "x=-x"
6929"\n" "}"
6930"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006931"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006932"\n" "scale=scale(x)+1"
6933"\n" "while(x>1){"
6934"\n" "d+=1"
6935"\n" "x/=2"
6936"\n" "scale+=1"
6937"\n" "}"
6938"\n" "scale=r"
6939"\n" "r=x+1"
6940"\n" "p=x"
6941"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006942"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006943"\n" "p*=x"
6944"\n" "f*=i"
6945"\n" "v=p/f"
6946"\n" "r+=v"
6947"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006948"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006949"\n" "scale=s"
6950"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006951"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006952"\n" "return(r/1)"
6953"\n" "}"
6954"\n" "define l(x){"
6955"\n" "auto b,s,r,p,a,q,i,v"
6956"\n" "b=ibase"
6957"\n" "ibase=A"
6958"\n" "if(x<=0){"
6959"\n" "r=(1-10^scale)/1"
6960"\n" "ibase=b"
6961"\n" "return(r)"
6962"\n" "}"
6963"\n" "s=scale"
6964"\n" "scale+=6"
6965"\n" "p=2"
6966"\n" "while(x>=2){"
6967"\n" "p*=2"
6968"\n" "x=sqrt(x)"
6969"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006970"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006971"\n" "p*=2"
6972"\n" "x=sqrt(x)"
6973"\n" "}"
6974"\n" "r=a=(x-1)/(x+1)"
6975"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006976"\n" "v=1"
6977"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006978"\n" "a*=q"
6979"\n" "v=a/i"
6980"\n" "r+=v"
6981"\n" "}"
6982"\n" "r*=p"
6983"\n" "scale=s"
6984"\n" "ibase=b"
6985"\n" "return(r/1)"
6986"\n" "}"
6987"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006988"\n" "auto b,s,r,a,q,i"
6989"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006990"\n" "b=ibase"
6991"\n" "ibase=A"
6992"\n" "s=scale"
6993"\n" "scale=1.1*s+2"
6994"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006995"\n" "scale=0"
6996"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006997"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006998"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006999"\n" "scale=s+2"
7000"\n" "r=a=x"
7001"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007002"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007003"\n" "a*=q/(i*(i-1))"
7004"\n" "r+=a"
7005"\n" "}"
7006"\n" "scale=s"
7007"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007008"\n" "return(r/1)"
7009"\n" "}"
7010"\n" "define c(x){"
7011"\n" "auto b,s"
7012"\n" "b=ibase"
7013"\n" "ibase=A"
7014"\n" "s=scale"
7015"\n" "scale*=1.2"
7016"\n" "x=s(2*a(1)+x)"
7017"\n" "scale=s"
7018"\n" "ibase=b"
7019"\n" "return(x/1)"
7020"\n" "}"
7021"\n" "define a(x){"
7022"\n" "auto b,s,r,n,a,m,t,f,i,u"
7023"\n" "b=ibase"
7024"\n" "ibase=A"
7025"\n" "n=1"
7026"\n" "if(x<0){"
7027"\n" "n=-1"
7028"\n" "x=-x"
7029"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007030"\n" "if(scale<65){"
7031"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7032"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007033"\n" "}"
7034"\n" "s=scale"
7035"\n" "if(x>.2){"
7036"\n" "scale+=5"
7037"\n" "a=a(.2)"
7038"\n" "}"
7039"\n" "scale=s+3"
7040"\n" "while(x>.2){"
7041"\n" "m+=1"
7042"\n" "x=(x-.2)/(1+.2*x)"
7043"\n" "}"
7044"\n" "r=u=x"
7045"\n" "f=-x*x"
7046"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007047"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007048"\n" "u*=f"
7049"\n" "t=u/i"
7050"\n" "r+=t"
7051"\n" "}"
7052"\n" "scale=s"
7053"\n" "ibase=b"
7054"\n" "return((m*a+r)/n)"
7055"\n" "}"
7056"\n" "define j(n,x){"
7057"\n" "auto b,s,o,a,i,v,f"
7058"\n" "b=ibase"
7059"\n" "ibase=A"
7060"\n" "s=scale"
7061"\n" "scale=0"
7062"\n" "n/=1"
7063"\n" "if(n<0){"
7064"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007065"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007066"\n" "}"
7067"\n" "a=1"
7068"\n" "for(i=2;i<=n;++i)a*=i"
7069"\n" "scale=1.5*s"
7070"\n" "a=(x^n)/2^n/a"
7071"\n" "r=v=1"
7072"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007073"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007074"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007075"\n" "v=v*f/i/(n+i)"
7076"\n" "r+=v"
7077"\n" "}"
7078"\n" "scale=s"
7079"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007080"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007081"\n" "return(a*r/1)"
7082"\n" "}"
7083};
7084#endif // ENABLE_BC
7085
Denys Vlasenko10bde142018-12-27 18:23:58 +01007086static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007087{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007088 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007089 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007090 size_t i;
7091
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007092#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007093 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007094 // We know that internal library is not buggy,
7095 // thus error checking is normally disabled.
7096# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007097 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007098 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007099 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007100#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007101
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007102 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007103 fname = (void*)G.files.v;
7104 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007105 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007106 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7107 // Debug config, non-interactive mode:
7108 // return all the way back to main.
7109 // Non-debug builds do not come here
7110 // in non-interactive mode, they exit.
7111 RETURN_STATUS(s);
7112 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007113 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007114
Denys Vlasenko91cde952018-12-10 20:56:08 +01007115 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007116 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007117
Denys Vlasenko19f11072018-12-12 22:48:19 +01007118 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007119}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007120#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007121
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007122#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007123static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007124{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007125 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007126 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007127 bc_vec_free(&G.prog.vars);
7128 bc_vec_free(&G.prog.var_map);
7129 bc_vec_free(&G.prog.arrs);
7130 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007131 IF_DC(bc_vec_free(&G.prog.strs);)
7132 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007133 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007134 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007135 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007136 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007137 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007138 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007139}
7140
Denys Vlasenko10bde142018-12-27 18:23:58 +01007141static void xc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007142{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007143 bc_vec_free(&G.files);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007144 xc_program_free();
7145 xc_parse_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007146 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007147}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007148#endif
7149
Denys Vlasenko10bde142018-12-27 18:23:58 +01007150static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007151{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007152 BcInstPtr ip;
7153
Denys Vlasenko82269122018-12-14 16:30:56 +01007154 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007155 memset(&ip, 0, sizeof(BcInstPtr));
7156
Denys Vlasenko82269122018-12-14 16:30:56 +01007157 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007158 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007159 G.prog.ob_t = 10;
7160
Denys Vlasenko503faf92018-12-20 16:24:18 +01007161 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7162 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007163
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007164 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007165 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007166
Denys Vlasenko503faf92018-12-20 16:24:18 +01007167 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7168 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007169
7170 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007171 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007172
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007173 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007174 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007175 // a valid function name (and be short)
7176 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007177 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007178 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007179 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007180 xc_program_add_fn();
7181 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007182 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007183
7184 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007185 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007186
7187 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007188 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007189
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007190 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7191 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007192 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007193 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7194 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007195
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007196 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007197}
Gavin Howard01055ba2018-11-03 11:00:21 -06007198
Denys Vlasenko10bde142018-12-27 18:23:58 +01007199static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007200{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007201#if ENABLE_FEATURE_EDITING
7202 G.line_input_state = new_line_input_t(DO_HISTORY);
7203#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01007204 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007205
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007206 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007207 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007208 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007209 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007210
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007211//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7212//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007213 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007214#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007215 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007216 // With SA_RESTART, most system calls will restart
7217 // (IOW: they won't fail with EINTR).
7218 // In particular, this means ^C won't cause
7219 // stdout to get into "error state" if SIGINT hits
7220 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007221 //
7222 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007223 // will only be handled after [Enter] since read()
7224 // from stdin is not interrupted by ^C either,
7225 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007226 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007227 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7228
7229 // Without SA_RESTART, this exhibits a bug:
7230 // "while (1) print 1" and try ^C-ing it.
7231 // Intermittently, instead of returning to input line,
7232 // you'll get "output error: Interrupted system call"
7233 // and exit.
7234 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007235#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007236 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007237 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007238 return 0; // "not a tty"
7239}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007240
Denys Vlasenko10bde142018-12-27 18:23:58 +01007241static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007242{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007243 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007244#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007245 if (G_exiting) // it was actually "halt" or "quit"
7246 st = EXIT_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007247 xc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007248# if ENABLE_FEATURE_EDITING
7249 free_line_input_t(G.line_input_state);
7250# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007251 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007252#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007253 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007254 return st;
7255}
7256
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007257#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007258int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007259int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007260{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007261 int is_tty;
7262
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007263 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007264
Denys Vlasenko10bde142018-12-27 18:23:58 +01007265 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007266
7267 bc_args(argv);
7268
7269 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7270 bc_vm_info();
7271
Denys Vlasenko10bde142018-12-27 18:23:58 +01007272 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007273}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007274#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007275
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007276#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007277int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007278int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007279{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007280 int noscript;
7281
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007282 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007283
7284 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7285 // 1 char wider than bc from the same package.
7286 // Both default width, and xC_LINE_LENGTH=N are wider:
7287 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007288 // |1234\ |
7289 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007290 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007291 // |123\ |
7292 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007293 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007294 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007295
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007296 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7297 noscript = BC_FLAG_I;
7298 for (;;) {
7299 int n = getopt(argc, argv, "e:f:x");
7300 if (n <= 0)
7301 break;
7302 switch (n) {
7303 case 'e':
7304 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007305 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007306 if (n) return n;
7307 break;
7308 case 'f':
7309 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007310 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007311 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007312 break;
7313 case 'x':
7314 option_mask32 |= DC_FLAG_X;
7315 break;
7316 default:
7317 bb_show_usage();
7318 }
7319 }
7320 argv += optind;
7321
7322 while (*argv) {
7323 noscript = 0;
7324 bc_vec_push(&G.files, argv++);
7325 }
7326
7327 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7328
Denys Vlasenko10bde142018-12-27 18:23:58 +01007329 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007330}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007331#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007332
Denys Vlasenko14767602018-12-27 22:52:13 +01007333#endif // DC_BIG
7334