blob: 72c23542c48f5cad81d78571a7f3283fc6d31f0b [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 */
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007//TODO: GNU extensions:
8// support ibase up to 36
9// support "define void f()..."
10// support "define f(*param[])" - "pass array by reference" syntax
11
12#define DEBUG_LEXER 0
13#define DEBUG_COMPILE 0
14#define DEBUG_EXEC 0
15// This can be left enabled for production as well:
16#define SANITY_CHECKS 1
17
Gavin Howard01055ba2018-11-03 11:00:21 -060018//config:config BC
Denys Vlasenkob097a842018-12-28 03:20:17 +010019//config: bool "bc (45 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060020//config: default y
Denys Vlasenkocdadad52018-12-28 15:13:23 +010021//config: select FEATURE_DC_BIG
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: help
23//config: bc is a command-line, arbitrary-precision calculator with a
24//config: Turing-complete language. See the GNU bc manual
25//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010026//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060027//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010028//config: This bc has five differences to the GNU bc:
29//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: 2) Arrays are copied before being passed as arguments to
31//config: functions. This behavior is required by the bc spec.
32//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010033//config: the number of elements in the array. This prints "1":
34//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060035//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010036//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060037//config: allows POSIX-compliant scripts to work while somewhat
38//config: preserving expected behavior (versus C) and making parsing
39//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010040//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060042//config:config DC
Denys Vlasenkob097a842018-12-28 03:20:17 +010043//config: bool "dc (36 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060044//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
Denys Vlasenko14767602018-12-27 22:52:13 +010062//config: following regex: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060063//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010064//config: whitespace. Examples:
65//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060066//config: Also note that, like the FreeBSD dc, extended registers are not
67//config: allowed unless the "-x" option is given.
68//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010069//config:if BC || DC # for menuconfig indenting
70//config:
71//config:config FEATURE_DC_BIG
72//config: bool "Use bc code base for dc (larger, more features)"
Denys Vlasenko14767602018-12-27 22:52:13 +010073//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010078//config: depends on DC && !BC && !FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010079//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010083//config:config FEATURE_BC_INTERACTIVE
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010086//config: depends on BC || (DC && FEATURE_DC_BIG)
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +0100101//config: depends on BC || (DC && FEATURE_DC_BIG)
102//config:
103//config:endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100113//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko14767602018-12-27 22:52:13 +0100139//usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko14767602018-12-27 22:52:13 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100144//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko14767602018-12-27 22:52:13 +0100166#if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100167# include "dc.c"
168#else
169
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100170#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100171static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100172#define dbg_lex(...) \
173 do { \
174 fprintf(stderr, "%*s", lex_indent, ""); \
175 bb_error_msg(__VA_ARGS__); \
176 } while (0)
177#define dbg_lex_enter(...) \
178 do { \
179 dbg_lex(__VA_ARGS__); \
180 lex_indent++; \
181 } while (0)
182#define dbg_lex_done(...) \
183 do { \
184 lex_indent--; \
185 dbg_lex(__VA_ARGS__); \
186 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100187#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100188# define dbg_lex(...) ((void)0)
189# define dbg_lex_enter(...) ((void)0)
190# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100191#endif
192
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100193#if DEBUG_COMPILE
194# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
195#else
196# define dbg_compile(...) ((void)0)
197#endif
198
Denys Vlasenko99b37622018-12-15 20:06:59 +0100199#if DEBUG_EXEC
200# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
201#else
202# define dbg_exec(...) ((void)0)
203#endif
204
Gavin Howard01055ba2018-11-03 11:00:21 -0600205typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100206 BC_STATUS_SUCCESS = 0,
207 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100208 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600209} BcStatus;
210
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100211#define BC_VEC_INVALID_IDX ((size_t) -1)
212#define BC_VEC_START_CAP (1 << 5)
Gavin Howard01055ba2018-11-03 11:00:21 -0600213
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100214typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600215
216typedef struct BcVec {
217 char *v;
218 size_t len;
219 size_t cap;
220 size_t size;
221 BcVecFree dtor;
222} BcVec;
223
Gavin Howard01055ba2018-11-03 11:00:21 -0600224typedef signed char BcDig;
225
226typedef struct BcNum {
227 BcDig *restrict num;
228 size_t rdx;
229 size_t len;
230 size_t cap;
231 bool neg;
232} BcNum;
233
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100234#define BC_NUM_MAX_IBASE ((unsigned long) 16)
235// larger value might speed up BIGNUM calculations a bit:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100236#define BC_NUM_DEF_SIZE 16
237#define BC_NUM_PRINT_WIDTH 69
Gavin Howard01055ba2018-11-03 11:00:21 -0600238
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100239#define BC_NUM_KARATSUBA_LEN 32
Gavin Howard01055ba2018-11-03 11:00:21 -0600240
Gavin Howard01055ba2018-11-03 11:00:21 -0600241typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100242#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600243 BC_INST_INC_PRE,
244 BC_INST_DEC_PRE,
245 BC_INST_INC_POST,
246 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100247#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100248 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600249
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100250 XC_INST_REL_EQ, // should
251 XC_INST_REL_LE, // match
252 XC_INST_REL_GE, // LEX
253 XC_INST_REL_NE, // constants
254 XC_INST_REL_LT, // for
255 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600256
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100257 XC_INST_POWER, // operations
258 XC_INST_MULTIPLY, // |
259 XC_INST_DIVIDE, // |
260 XC_INST_MODULUS, // |
261 XC_INST_PLUS, // |
262 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600263
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100264 XC_INST_BOOL_NOT, // |
265 XC_INST_BOOL_OR, // |
266 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100267#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100268 BC_INST_ASSIGN_POWER, // |
269 BC_INST_ASSIGN_MULTIPLY,// |
270 BC_INST_ASSIGN_DIVIDE, // |
271 BC_INST_ASSIGN_MODULUS, // |
272 BC_INST_ASSIGN_PLUS, // |
273 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100274#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100275 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600276
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100277 XC_INST_NUM,
278 XC_INST_VAR,
279 XC_INST_ARRAY_ELEM,
280 XC_INST_ARRAY,
281 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100282
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100283 XC_INST_IBASE, // order of these constans should match other enums
284 XC_INST_OBASE, // order of these constans should match other enums
285 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100286 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100287 XC_INST_LENGTH,
288 XC_INST_READ,
289 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600290
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100291 XC_INST_PRINT,
292 XC_INST_PRINT_POP,
293 XC_INST_STR,
294 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600295
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100296#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100297 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600298 BC_INST_JUMP,
299 BC_INST_JUMP_ZERO,
300
301 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600302 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100303#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100304 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600305
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100306 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100307#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100308 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600309
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 DC_INST_MODEXP,
311 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600312
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100313 DC_INST_EXECUTE,
314 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_ASCIIFY,
317 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600318
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100319 DC_INST_PRINT_STACK,
320 DC_INST_CLEAR_STACK,
321 DC_INST_STACK_LEN,
322 DC_INST_DUPLICATE,
323 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600324
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100325 DC_INST_LOAD,
326 DC_INST_PUSH_VAR,
327 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600328
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100329 DC_INST_QUIT,
330 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600331
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100332 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100333#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600334} BcInst;
335
336typedef struct BcId {
337 char *name;
338 size_t idx;
339} BcId;
340
341typedef struct BcFunc {
342 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100343 IF_BC(BcVec labels;)
344 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100345 IF_BC(BcVec strs;)
346 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100347 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600348} BcFunc;
349
350typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100351 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600352
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100353 XC_RESULT_VAR,
354 XC_RESULT_ARRAY_ELEM,
355 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600356
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100357 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600358
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100359 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
360 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
361 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
362 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100363 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100364 XC_RESULT_CONSTANT,
365 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600366} BcResultType;
367
368typedef union BcResultData {
369 BcNum n;
370 BcVec v;
371 BcId id;
372} BcResultData;
373
374typedef struct BcResult {
375 BcResultType t;
376 BcResultData d;
377} BcResult;
378
379typedef struct BcInstPtr {
380 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100381 size_t inst_idx;
382 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600383} BcInstPtr;
384
Gavin Howard01055ba2018-11-03 11:00:21 -0600385typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100386 XC_LEX_EOF,
387 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600388
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100389 XC_LEX_NLINE,
390 XC_LEX_WHITESPACE,
391 XC_LEX_STR,
392 XC_LEX_NAME,
393 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100394
Denys Vlasenko69560f42018-12-24 14:14:23 +0100395 XC_LEX_1st_op,
396 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100397
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100398 XC_LEX_OP_REL_EQ, // should
399 XC_LEX_OP_REL_LE, // match
400 XC_LEX_OP_REL_GE, // INST
401 XC_LEX_OP_REL_NE, // constants
402 XC_LEX_OP_REL_LT, // for
403 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100404
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100405 XC_LEX_OP_POWER, // operations
406 XC_LEX_OP_MULTIPLY, // |
407 XC_LEX_OP_DIVIDE, // |
408 XC_LEX_OP_MODULUS, // |
409 XC_LEX_OP_PLUS, // |
410 XC_LEX_OP_MINUS, // |
411 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100412#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100413 BC_LEX_OP_BOOL_NOT, // |
414 BC_LEX_OP_BOOL_OR, // |
415 BC_LEX_OP_BOOL_AND, // |
416
417 BC_LEX_OP_ASSIGN_POWER, // |
418 BC_LEX_OP_ASSIGN_MULTIPLY, // |
419 BC_LEX_OP_ASSIGN_DIVIDE, // |
420 BC_LEX_OP_ASSIGN_MODULUS, // |
421 BC_LEX_OP_ASSIGN_PLUS, // |
422 BC_LEX_OP_ASSIGN_MINUS, // |
423
424 BC_LEX_OP_ASSIGN, // V
425
Gavin Howard01055ba2018-11-03 11:00:21 -0600426 BC_LEX_OP_INC,
427 BC_LEX_OP_DEC,
428
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100429 BC_LEX_LPAREN, // () are 0x28 and 0x29
430 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600431
Denys Vlasenko10bde142018-12-27 18:23:58 +0100432 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600433 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100434 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600435
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100436 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600437 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100438 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600439
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100440 BC_LEX_KEY_1st_keyword,
441 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600442 BC_LEX_KEY_BREAK,
443 BC_LEX_KEY_CONTINUE,
444 BC_LEX_KEY_DEFINE,
445 BC_LEX_KEY_ELSE,
446 BC_LEX_KEY_FOR,
447 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100448 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100449 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
450 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600451 BC_LEX_KEY_IF,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100452 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600453 BC_LEX_KEY_LENGTH,
454 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600455 BC_LEX_KEY_PRINT,
456 BC_LEX_KEY_QUIT,
457 BC_LEX_KEY_READ,
458 BC_LEX_KEY_RETURN,
459 BC_LEX_KEY_SCALE,
460 BC_LEX_KEY_SQRT,
461 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100462#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600463
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100464#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100465 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
466 DC_LEX_OP_ASSIGN,
467
468 DC_LEX_LPAREN,
469 DC_LEX_SCOLON,
470 DC_LEX_READ,
471 DC_LEX_IBASE,
472 DC_LEX_SCALE,
473 DC_LEX_OBASE,
474 DC_LEX_LENGTH,
475 DC_LEX_PRINT,
476 DC_LEX_QUIT,
477 DC_LEX_SQRT,
478 DC_LEX_LBRACE,
479
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100480 DC_LEX_EQ_NO_REG,
481 DC_LEX_OP_MODEXP,
482 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600483
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100484 DC_LEX_COLON,
485 DC_LEX_ELSE,
486 DC_LEX_EXECUTE,
487 DC_LEX_PRINT_STACK,
488 DC_LEX_CLEAR_STACK,
489 DC_LEX_STACK_LEVEL,
490 DC_LEX_DUPLICATE,
491 DC_LEX_SWAP,
492 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600493
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100494 DC_LEX_ASCIIFY,
495 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600496
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100497 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
498 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
499 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
500 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
501 DC_LEX_LOAD,
502 DC_LEX_LOAD_POP,
503 DC_LEX_STORE_PUSH,
504 DC_LEX_PRINT_POP,
505 DC_LEX_NQUIT,
506 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100507#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600508} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100509// must match order of BC_LEX_KEY_foo etc above
510#if ENABLE_BC
511struct BcLexKeyword {
512 char name8[8];
513};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100514#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100515 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100516static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100517 LEX_KW_ENTRY("auto" , 1), // 0
518 LEX_KW_ENTRY("break" , 1), // 1
519 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
520 LEX_KW_ENTRY("define" , 1), // 3
521 LEX_KW_ENTRY("else" , 0), // 4
522 LEX_KW_ENTRY("for" , 1), // 5
523 LEX_KW_ENTRY("halt" , 0), // 6
524 LEX_KW_ENTRY("ibase" , 1), // 7
525 LEX_KW_ENTRY("obase" , 1), // 8
526 LEX_KW_ENTRY("if" , 1), // 9
527 LEX_KW_ENTRY("last" , 0), // 10
528 LEX_KW_ENTRY("length" , 1), // 11
529 LEX_KW_ENTRY("limits" , 0), // 12
530 LEX_KW_ENTRY("print" , 0), // 13
531 LEX_KW_ENTRY("quit" , 1), // 14
532 LEX_KW_ENTRY("read" , 0), // 15
533 LEX_KW_ENTRY("return" , 1), // 16
534 LEX_KW_ENTRY("scale" , 1), // 17
535 LEX_KW_ENTRY("sqrt" , 1), // 18
536 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100537};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100538#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100539#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100540#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100541#define STRING_if (bc_lex_kws[9].name8)
542#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100543enum {
544 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100545 | (1 << 0) // 0
546 | (1 << 1) // 1
547 | (0 << 2) // 2
548 | (1 << 3) // 3
549 | (0 << 4) // 4
550 | (1 << 5) // 5
551 | (0 << 6) // 6
552 | (1 << 7) // 7
553 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100554 | (1 << 9) // 9
555 | (0 << 10) // 10
556 | (1 << 11) // 11
557 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100558 | (0 << 13) // 13
559 | (1 << 14) // 14
560 | (0 << 15) // 15
561 | (1 << 16) // 16
562 | (1 << 17) // 17
563 | (1 << 18) // 18
564 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100565};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100566#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100567
568// This is a bit array that corresponds to token types. An entry is
569// true if the token is valid in an expression, false otherwise.
570// Used to figure out when expr parsing should stop *without error message*
571// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100572// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100573// by later processing.
574enum {
575#define EXBITS(a,b,c,d,e,f,g,h) \
576 ((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 +0100577 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100578 + (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 +0100579 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
580 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100581 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
582 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100583 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
584 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
585 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100586#undef EXBITS
587};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100588static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100589{
590#if ULONG_MAX > 0xffffffff
591 // 64-bit version (will not work correctly for 32-bit longs!)
592 return BC_PARSE_EXPRS_BITS & (1UL << i);
593#else
594 // 32-bit version
595 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
596 if (i >= 32) {
597 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
598 i &= 31;
599 }
600 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100601#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100602}
603
604// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100605// [XC_LEX_1st_op...] token types.
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100606static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100607#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100608 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100609 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 +0100610 OP(2, false), // pow
611 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
612 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100613 OP(1, false), // not
614 OP(7, true ), OP( 7, true ), // or and
615 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
616 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100617 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100618#undef OP
619};
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100620#define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f)
621#define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100622#endif // ENABLE_BC
623
624#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100625static const //BcLexType - should be this type
626uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100627dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100628 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100629 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100630 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100631 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100632 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100633 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100634 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100635 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
636 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
637 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100638 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100639 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
640 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100641 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100644 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100645 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
646 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100647 // QRSTUVWXY
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100648 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
649 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100650 // Z[\]
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100651 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100652 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100653 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100654 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100655 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
656 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100657 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100658 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100659 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100660 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100661 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
662 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100663 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100665 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100666 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100667};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100668static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100669int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100670dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
671 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
672 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
673 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100674 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
675 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
676 XC_INST_REL_GT, // DC_LEX_LPAREN
677 DC_INST_INVALID, // DC_LEX_SCOLON
678 DC_INST_INVALID, // DC_LEX_READ
679 XC_INST_IBASE, // DC_LEX_IBASE
680 XC_INST_SCALE, // DC_LEX_SCALE
681 XC_INST_OBASE, // DC_LEX_OBASE
682 XC_INST_LENGTH, // DC_LEX_LENGTH
683 XC_INST_PRINT, // DC_LEX_PRINT
684 DC_INST_QUIT, // DC_LEX_QUIT
685 XC_INST_SQRT, // DC_LEX_SQRT
686 XC_INST_REL_GE, // DC_LEX_LBRACE
687 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100688 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
689 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
690 DC_INST_EXECUTE, // DC_LEX_EXECUTE
691 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
692 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
693 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
694 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
695 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
696 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
697 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
698 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
699 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100700 // DC_INST_INVALID in this table either means that corresponding LEX
701 // is not possible for dc, or that it does not compile one-to-one
702 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100703};
704#endif // ENABLE_DC
705
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100706typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100707 smallint lex; // was BcLexType // first member is most used
708 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100709 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100710 const char *lex_inbuf;
711 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100712 const char *lex_filename;
713 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100714 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100715
Gavin Howard01055ba2018-11-03 11:00:21 -0600716 BcFunc *func;
717 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100718 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100719 IF_BC(BcVec exits;)
720 IF_BC(BcVec conds;)
721 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600722} BcParse;
723
Gavin Howard01055ba2018-11-03 11:00:21 -0600724typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600725 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100726 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600727
Denys Vlasenko503faf92018-12-20 16:24:18 +0100728 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600729 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600730 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600731
Gavin Howard01055ba2018-11-03 11:00:21 -0600732 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100733 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600734
735 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100736 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600737
738 BcVec vars;
739 BcVec var_map;
740
741 BcVec arrs;
742 BcVec arr_map;
743
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100744 IF_DC(BcVec strs;)
745 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600746
Gavin Howard01055ba2018-11-03 11:00:21 -0600747 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100748 IF_BC(BcNum one;)
749 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600750} BcProgram;
751
Denys Vlasenko6e618232018-12-25 22:20:14 +0100752struct globals {
753 BcParse prs; // first member is most used
754
755 // For error messages. Can be set to current parsed line,
756 // or [TODO] to current executing line (can be before last parsed one)
757 unsigned err_line;
758
759 BcVec input_buffer;
760
Denys Vlasenko14767602018-12-27 22:52:13 +0100761 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100762 IF_FEATURE_CLEAN_UP(smallint exiting;)
763
764 BcProgram prog;
765
766 BcVec files;
767
768 char *env_args;
769
770#if ENABLE_FEATURE_EDITING
771 line_input_t *line_input_state;
772#endif
773} FIX_ALIASING;
774#define G (*ptr_to_globals)
775#define INIT_G() do { \
776 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
777} while (0)
778#define FREE_G() do { \
779 FREE_PTR_TO_GLOBALS(); \
780} while (0)
781#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
782#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
783#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100784#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100785# define G_interrupt bb_got_signal
786# define G_ttyin G.ttyin
787#else
788# define G_interrupt 0
789# define G_ttyin 0
790#endif
791#if ENABLE_FEATURE_CLEAN_UP
792# define G_exiting G.exiting
793#else
794# define G_exiting 0
795#endif
796#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
797#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
798
Denys Vlasenko6e618232018-12-25 22:20:14 +0100799#if ENABLE_BC
800# define BC_PARSE_REL (1 << 0)
801# define BC_PARSE_PRINT (1 << 1)
802# define BC_PARSE_ARRAY (1 << 2)
803# define BC_PARSE_NOCALL (1 << 3)
804#endif
805
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100806#define BC_PROG_MAIN 0
807#define BC_PROG_READ 1
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100808#if ENABLE_DC
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100809#define BC_PROG_REQ_FUNCS 2
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100810#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600811
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100812#define BC_FLAG_W (1 << 0)
813#define BC_FLAG_V (1 << 1)
814#define BC_FLAG_S (1 << 2)
815#define BC_FLAG_Q (1 << 3)
816#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100817#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
818#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600819
Denys Vlasenko64074a12018-12-07 15:50:14 +0100820#define BC_MAX_OBASE ((unsigned) 999)
821#define BC_MAX_DIM ((unsigned) INT_MAX)
822#define BC_MAX_SCALE ((unsigned) UINT_MAX)
823#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
824#define BC_MAX_NUM BC_MAX_STRING
825// Unused apart from "limits" message. Just show a "biggish number" there.
826//#define BC_MAX_NAME BC_MAX_STRING
827//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
828//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
829#define BC_MAX_NAME_STR "999999999"
830#define BC_MAX_EXP_STR "999999999"
831#define BC_MAX_VARS_STR "999999999"
832
833#define BC_MAX_OBASE_STR "999"
834
835#if INT_MAX == 2147483647
836# define BC_MAX_DIM_STR "2147483647"
837#elif INT_MAX == 9223372036854775807
838# define BC_MAX_DIM_STR "9223372036854775807"
839#else
840# error Strange INT_MAX
841#endif
842
843#if UINT_MAX == 4294967295
844# define BC_MAX_SCALE_STR "4294967295"
845# define BC_MAX_STRING_STR "4294967294"
846#elif UINT_MAX == 18446744073709551615
847# define BC_MAX_SCALE_STR "18446744073709551615"
848# define BC_MAX_STRING_STR "18446744073709551614"
849#else
850# error Strange UINT_MAX
851#endif
852#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600853
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100854// In configurations where errors abort instead of propagating error
855// return code up the call chain, functions returning BC_STATUS
856// actually don't return anything, they always succeed and return "void".
857// A macro wrapper is provided, which makes this statement work:
858// s = zbc_func(...)
859// and makes it visible to the compiler that s is always zero,
860// allowing compiler to optimize dead code after the statement.
861//
862// To make code more readable, each such function has a "z"
863// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
864//
Denys Vlasenko14767602018-12-27 22:52:13 +0100865#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100866# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100867# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100868# define IF_ERROR_RETURN_POSSIBLE(a) a
869# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100870# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100871# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100872#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100873# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100874# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100875# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
876# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100877# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100878# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100879#endif
880
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100881//
882// Utility routines
883//
Gavin Howard01055ba2018-11-03 11:00:21 -0600884
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100885#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
886#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
887
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100888static void fflush_and_check(void)
889{
890 fflush_all();
891 if (ferror(stdout) || ferror(stderr))
892 bb_perror_msg_and_die("output error");
893}
894
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100895#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100896#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100897do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100898 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100899 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100900 return BC_STATUS_FAILURE; \
901} while (0)
902#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100903static void quit(void) NORETURN;
904static void quit(void)
905{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100906 if (ferror(stdin))
907 bb_perror_msg_and_die("input error");
908 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100909 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100910 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100911}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100912#define QUIT_OR_RETURN_TO_MAIN quit()
913#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100914
Denys Vlasenko5318f812018-12-05 17:48:01 +0100915static void bc_verror_msg(const char *fmt, va_list p)
916{
Denys Vlasenko82269122018-12-14 16:30:56 +0100917 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100918 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100919 sv = applet_name;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100920 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100921 }
922 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100923 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100924 free((char*)applet_name);
925 applet_name = sv;
926 }
927}
928
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100929static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100930{
931 va_list p;
932
933 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100934 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100935 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100936
Denys Vlasenkoec603182018-12-17 10:34:02 +0100937 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
938 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
939 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100940}
941
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100942#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100943static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100944{
945 va_list p;
946
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100947 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100948 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100949 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100950
951 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100952 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100953 va_end(p);
954
955 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100956 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100957 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
958
Denys Vlasenkoec603182018-12-17 10:34:02 +0100959 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100960 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100961 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100962}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100963#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100964#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100965
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100966// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
967// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100968// function must not have caller-cleaned parameters on stack.
969// Unfortunately, vararg function API does exactly that on most arches.
970// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100971static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100972{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100973 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100974}
975static ERRORFUNC int bc_error_bad_character(char c)
976{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100977 if (!c)
978 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100979 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100980}
981static ERRORFUNC int bc_error_bad_expression(void)
982{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100983 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100984}
985static ERRORFUNC int bc_error_bad_token(void)
986{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100987 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100988}
989static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
990{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100991 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100992}
993static ERRORFUNC int bc_error_variable_is_wrong_type(void)
994{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100995 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100996}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100997#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100998static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100999{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001000 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001001}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001002#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1003static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001004{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001005 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001006}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001007#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1008static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001009{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001010 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 +01001011}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001012#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)
1013static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001014{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001015 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001016}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001017#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 +01001018#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001019
Gavin Howard01055ba2018-11-03 11:00:21 -06001020static void bc_vec_grow(BcVec *v, size_t n)
1021{
1022 size_t cap = v->cap * 2;
1023 while (cap < v->len + n) cap *= 2;
1024 v->v = xrealloc(v->v, v->size * cap);
1025 v->cap = cap;
1026}
1027
1028static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1029{
1030 v->size = esize;
1031 v->cap = BC_VEC_START_CAP;
1032 v->len = 0;
1033 v->dtor = dtor;
1034 v->v = xmalloc(esize * BC_VEC_START_CAP);
1035}
1036
Denys Vlasenko7d628012018-12-04 21:46:47 +01001037static void bc_char_vec_init(BcVec *v)
1038{
1039 bc_vec_init(v, sizeof(char), NULL);
1040}
1041
Gavin Howard01055ba2018-11-03 11:00:21 -06001042static void bc_vec_expand(BcVec *v, size_t req)
1043{
1044 if (v->cap < req) {
1045 v->v = xrealloc(v->v, v->size * req);
1046 v->cap = req;
1047 }
1048}
1049
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001050static void bc_vec_pop(BcVec *v)
1051{
1052 v->len--;
1053 if (v->dtor)
1054 v->dtor(v->v + (v->size * v->len));
1055}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001056
Gavin Howard01055ba2018-11-03 11:00:21 -06001057static void bc_vec_npop(BcVec *v, size_t n)
1058{
1059 if (!v->dtor)
1060 v->len -= n;
1061 else {
1062 size_t len = v->len - n;
1063 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1064 }
1065}
1066
Denys Vlasenko7d628012018-12-04 21:46:47 +01001067static void bc_vec_pop_all(BcVec *v)
1068{
1069 bc_vec_npop(v, v->len);
1070}
1071
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001072static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001073{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001074 size_t len = v->len;
1075 if (len >= v->cap) bc_vec_grow(v, 1);
1076 memmove(v->v + (v->size * len), data, v->size);
1077 v->len++;
1078 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001079}
1080
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001081// G.prog.results often needs "pop old operand, push result" idiom.
1082// Can do this without a few extra ops
1083static size_t bc_result_pop_and_push(const void *data)
1084{
1085 BcVec *v = &G.prog.results;
1086 char *last;
1087 size_t len = v->len - 1;
1088
1089 last = v->v + (v->size * len);
1090 if (v->dtor)
1091 v->dtor(last);
1092 memmove(last, data, v->size);
1093 return len;
1094}
1095
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001096static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001097{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001098 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001099}
1100
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001101static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001102{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001103 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001104 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001105 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001106}
1107
Gavin Howard01055ba2018-11-03 11:00:21 -06001108static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1109{
1110 if (idx == v->len)
1111 bc_vec_push(v, data);
1112 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001113 char *ptr;
1114
1115 if (v->len == v->cap) bc_vec_grow(v, 1);
1116
1117 ptr = v->v + v->size * idx;
1118
1119 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1120 memmove(ptr, data, v->size);
1121 }
1122}
1123
1124static void bc_vec_string(BcVec *v, size_t len, const char *str)
1125{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001126 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001127 bc_vec_expand(v, len + 1);
1128 memcpy(v->v, str, len);
1129 v->len = len;
1130
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001131 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001132}
1133
Gavin Howard01055ba2018-11-03 11:00:21 -06001134static void *bc_vec_item(const BcVec *v, size_t idx)
1135{
1136 return v->v + v->size * idx;
1137}
1138
1139static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1140{
1141 return v->v + v->size * (v->len - idx - 1);
1142}
1143
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001144static void *bc_vec_top(const BcVec *v)
1145{
1146 return v->v + v->size * (v->len - 1);
1147}
1148
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001149static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001150{
1151 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001152 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001153 free(v->v);
1154}
1155
Denys Vlasenko10bde142018-12-27 18:23:58 +01001156static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001157{
1158 return bc_vec_item(&G.prog.fns, idx);
1159}
1160// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001161#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001162
1163#if ENABLE_BC
1164static BcFunc* bc_program_current_func(void)
1165{
1166 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001167 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001168 return func;
1169}
1170#endif
1171
Denys Vlasenko10bde142018-12-27 18:23:58 +01001172static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001173{
1174#if ENABLE_BC
1175 if (IS_BC) {
1176 BcFunc *func = bc_program_current_func();
1177 return bc_vec_item(&func->strs, idx);
1178 }
1179#endif
1180 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1181}
1182
Denys Vlasenko10bde142018-12-27 18:23:58 +01001183static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001184{
1185#if ENABLE_BC
1186 if (IS_BC) {
1187 BcFunc *func = bc_program_current_func();
1188 return bc_vec_item(&func->consts, idx);
1189 }
1190#endif
1191 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1192}
1193
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001194static int bc_id_cmp(const void *e1, const void *e2)
1195{
1196 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1197}
1198
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001199static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001200{
1201 free(((BcId *) id)->name);
1202}
1203
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001204static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001205{
1206 size_t low = 0, high = v->len;
1207
1208 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001209 size_t mid = (low + high) / 2;
1210 BcId *id = bc_vec_item(v, mid);
1211 int result = bc_id_cmp(ptr, id);
1212
1213 if (result == 0)
1214 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001215 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001216 high = mid;
1217 else
1218 low = mid + 1;
1219 }
1220
1221 return low;
1222}
1223
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001224static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001225{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001226 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001227
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001228 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001229 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001230 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1231 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001232 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001233 bc_vec_pushAt(v, ptr, n);
1234 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001235}
1236
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001237#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001238static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001239{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001240 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001241 if (i >= v->len) return BC_VEC_INVALID_IDX;
1242 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1243}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001244#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001245
Gavin Howard01055ba2018-11-03 11:00:21 -06001246static void bc_num_setToZero(BcNum *n, size_t scale)
1247{
1248 n->len = 0;
1249 n->neg = false;
1250 n->rdx = scale;
1251}
1252
1253static void bc_num_zero(BcNum *n)
1254{
1255 bc_num_setToZero(n, 0);
1256}
1257
1258static void bc_num_one(BcNum *n)
1259{
1260 bc_num_setToZero(n, 0);
1261 n->len = 1;
1262 n->num[0] = 1;
1263}
1264
Denys Vlasenko3129f702018-12-09 12:04:44 +01001265// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001266static void bc_num_init(BcNum *n, size_t req)
1267{
1268 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001269 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001270 n->num = xmalloc(req);
1271 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001272 n->rdx = 0;
1273 n->len = 0;
1274 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001275}
1276
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001277static void bc_num_init_DEF_SIZE(BcNum *n)
1278{
1279 bc_num_init(n, BC_NUM_DEF_SIZE);
1280}
1281
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001282static void bc_num_expand(BcNum *n, size_t req)
1283{
1284 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1285 if (req > n->cap) {
1286 n->num = xrealloc(n->num, req);
1287 n->cap = req;
1288 }
1289}
1290
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001291static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001292{
1293 free(((BcNum *) num)->num);
1294}
1295
1296static void bc_num_copy(BcNum *d, BcNum *s)
1297{
1298 if (d != s) {
1299 bc_num_expand(d, s->cap);
1300 d->len = s->len;
1301 d->neg = s->neg;
1302 d->rdx = s->rdx;
1303 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1304 }
1305}
1306
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001307static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001308{
1309 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001310 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001311
Denys Vlasenko1557b762018-12-22 21:37:46 +01001312 result = 0;
1313 i = n->len;
1314 while (i > n->rdx) {
1315 unsigned long prev = result;
1316 result = result * 10 + n->num[--i];
1317 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1318 // 0x1ff00000 * 10 is 0x13f600000,
1319 // or 0x3f600000 truncated to 32 bits. Which is larger.
1320 // However, (N*10)/8 < N check is always correct.
1321 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001322 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001323 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001324 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001325
Denys Vlasenko29301232018-12-11 15:29:32 +01001326 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001327}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001328#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1329
1330static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1331{
1332 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1333
1334 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1335}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001336#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001337
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001338#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1339# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1340#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1341# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1342#endif
1343// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1344// would not hit realloc() code path - not good if num[] is not malloced
1345
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001346static void bc_num_ulong2num(BcNum *n, unsigned long val)
1347{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001348 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001349
1350 bc_num_zero(n);
1351
1352 if (val == 0) return;
1353
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001354 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001355
1356 ptr = n->num;
1357 for (;;) {
1358 n->len++;
1359 *ptr++ = val % 10;
1360 val /= 10;
1361 if (val == 0) break;
1362 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001363}
1364
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001365static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001366{
1367 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001368 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001369 a[i] -= b[i];
1370 for (j = i; a[j] < 0;) {
1371 a[j++] += 10;
1372 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001373 }
1374 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001375}
1376
1377static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1378{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001379 size_t i = len;
1380 for (;;) {
1381 int c;
1382 if (i == 0)
1383 return 0;
1384 i--;
1385 c = a[i] - b[i];
1386 if (c != 0) {
1387 i++;
1388 if (c < 0)
1389 return -i;
1390 return i;
1391 }
1392 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001393}
1394
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001395#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1396#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1397#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1398//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1399static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1400{
1401 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1402}
1403//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1404static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1405{
1406 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1407}
1408
Gavin Howard01055ba2018-11-03 11:00:21 -06001409static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1410{
1411 size_t i, min, a_int, b_int, diff;
1412 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001413 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001414 ssize_t cmp;
1415
1416 if (a == b) return 0;
1417 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1418 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001419
1420 if (a->neg != b->neg) // signs of a and b differ
1421 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1422 return (int)b->neg - (int)a->neg;
1423 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001424
1425 a_int = BC_NUM_INT(a);
1426 b_int = BC_NUM_INT(b);
1427 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001428
1429 if (a_int != 0) return (ssize_t) a_int;
1430
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001431 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001432 if (a_max) {
1433 min = b->rdx;
1434 diff = a->rdx - b->rdx;
1435 max_num = a->num + diff;
1436 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001437 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1438 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001439 min = a->rdx;
1440 diff = b->rdx - a->rdx;
1441 max_num = b->num + diff;
1442 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001443 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001444 }
1445
1446 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001447 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001448
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001449 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001450 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001451 }
1452
1453 return 0;
1454}
1455
1456static void bc_num_truncate(BcNum *n, size_t places)
1457{
1458 if (places == 0) return;
1459
1460 n->rdx -= places;
1461
1462 if (n->len != 0) {
1463 n->len -= places;
1464 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1465 }
1466}
1467
1468static void bc_num_extend(BcNum *n, size_t places)
1469{
1470 size_t len = n->len + places;
1471
1472 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001473 if (n->cap < len) bc_num_expand(n, len);
1474
1475 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1476 memset(n->num, 0, sizeof(BcDig) * places);
1477
1478 n->len += places;
1479 n->rdx += places;
1480 }
1481}
1482
1483static void bc_num_clean(BcNum *n)
1484{
1485 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1486 if (n->len == 0)
1487 n->neg = false;
1488 else if (n->len < n->rdx)
1489 n->len = n->rdx;
1490}
1491
1492static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1493{
1494 if (n->rdx < scale)
1495 bc_num_extend(n, scale - n->rdx);
1496 else
1497 bc_num_truncate(n, n->rdx - scale);
1498
1499 bc_num_clean(n);
1500 if (n->len != 0) n->neg = !neg1 != !neg2;
1501}
1502
1503static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1504 BcNum *restrict b)
1505{
1506 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001507 b->len = n->len - idx;
1508 a->len = idx;
1509 a->rdx = b->rdx = 0;
1510
1511 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1512 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001513 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001514 bc_num_zero(b);
1515 bc_num_copy(a, n);
1516 }
1517
1518 bc_num_clean(a);
1519 bc_num_clean(b);
1520}
1521
Denys Vlasenko29301232018-12-11 15:29:32 +01001522static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001523{
Denys Vlasenko29301232018-12-11 15:29:32 +01001524 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001525
1526 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1527 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1528 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001529 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001530 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001531
1532 if (n->rdx >= places)
1533 n->rdx -= places;
1534 else {
1535 bc_num_extend(n, places - n->rdx);
1536 n->rdx = 0;
1537 }
1538
1539 bc_num_clean(n);
1540
Denys Vlasenko29301232018-12-11 15:29:32 +01001541 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001542}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001543#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001544
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001545typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1546
1547static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1548 BcNumBinaryOp op, size_t req)
1549{
1550 BcStatus s;
1551 BcNum num2, *ptr_a, *ptr_b;
1552 bool init = false;
1553
1554 if (c == a) {
1555 ptr_a = &num2;
1556 memcpy(ptr_a, c, sizeof(BcNum));
1557 init = true;
1558 } else
1559 ptr_a = a;
1560
1561 if (c == b) {
1562 ptr_b = &num2;
1563 if (c != a) {
1564 memcpy(ptr_b, c, sizeof(BcNum));
1565 init = true;
1566 }
1567 } else
1568 ptr_b = b;
1569
1570 if (init)
1571 bc_num_init(c, req);
1572 else
1573 bc_num_expand(c, req);
1574
1575 s = BC_STATUS_SUCCESS;
1576 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1577
1578 if (init) bc_num_free(&num2);
1579
1580 RETURN_STATUS(s);
1581}
1582#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1583
1584static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1585static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1586static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1587static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1588static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1589static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1590
1591static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1592{
1593 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1594 (void) scale;
1595 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1596}
1597
1598static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1599{
1600 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1601 (void) scale;
1602 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1603}
1604
1605static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1606{
1607 size_t req = BC_NUM_MREQ(a, b, scale);
1608 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1609}
1610
1611static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1612{
1613 size_t req = BC_NUM_MREQ(a, b, scale);
1614 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1615}
1616
1617static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1618{
1619 size_t req = BC_NUM_MREQ(a, b, scale);
1620 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1621}
1622
1623static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1624{
1625 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1626}
1627
Denys Vlasenko10bde142018-12-27 18:23:58 +01001628static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001629 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1630};
1631#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1632#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1633#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1634#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1635#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1636#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1637
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001638static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001639{
1640 BcNum one;
1641 BcDig num[2];
1642
1643 one.cap = 2;
1644 one.num = num;
1645 bc_num_one(&one);
1646
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001647 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001648}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001649#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001650
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001651static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001652{
1653 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1654 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001655 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001656
1657 // Because this function doesn't need to use scale (per the bc spec),
1658 // I am hijacking it to say whether it's doing an add or a subtract.
1659
1660 if (a->len == 0) {
1661 bc_num_copy(c, b);
1662 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001663 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001664 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001665 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001666 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001667 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001668 }
1669
1670 c->neg = a->neg;
1671 c->rdx = BC_MAX(a->rdx, b->rdx);
1672 min_rdx = BC_MIN(a->rdx, b->rdx);
1673 c->len = 0;
1674
1675 if (a->rdx > b->rdx) {
1676 diff = a->rdx - b->rdx;
1677 ptr = a->num;
1678 ptr_a = a->num + diff;
1679 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001680 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001681 diff = b->rdx - a->rdx;
1682 ptr = b->num;
1683 ptr_a = a->num;
1684 ptr_b = b->num + diff;
1685 }
1686
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001687 ptr_c = c->num;
1688 for (i = 0; i < diff; ++i, ++c->len)
1689 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001690
1691 ptr_c += diff;
1692 a_int = BC_NUM_INT(a);
1693 b_int = BC_NUM_INT(b);
1694
1695 if (a_int > b_int) {
1696 min_int = b_int;
1697 max = a_int;
1698 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001699 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001700 min_int = a_int;
1701 max = b_int;
1702 ptr = ptr_b;
1703 }
1704
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001705 carry = 0;
1706 for (i = 0; i < min_rdx + min_int; ++i) {
1707 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001708 carry = in / 10;
1709 ptr_c[i] = (BcDig)(in % 10);
1710 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001711 for (; i < max + min_rdx; ++i) {
1712 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001713 carry = in / 10;
1714 ptr_c[i] = (BcDig)(in % 10);
1715 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001716 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001717
1718 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1719
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001720 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001721}
1722
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001723static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001724{
Gavin Howard01055ba2018-11-03 11:00:21 -06001725 ssize_t cmp;
1726 BcNum *minuend, *subtrahend;
1727 size_t start;
1728 bool aneg, bneg, neg;
1729
1730 // Because this function doesn't need to use scale (per the bc spec),
1731 // I am hijacking it to say whether it's doing an add or a subtract.
1732
1733 if (a->len == 0) {
1734 bc_num_copy(c, b);
1735 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001736 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001737 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001738 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001739 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001740 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001741 }
1742
1743 aneg = a->neg;
1744 bneg = b->neg;
1745 a->neg = b->neg = false;
1746
1747 cmp = bc_num_cmp(a, b);
1748
1749 a->neg = aneg;
1750 b->neg = bneg;
1751
1752 if (cmp == 0) {
1753 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001754 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001755 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001756 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001757 neg = a->neg;
1758 minuend = a;
1759 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001760 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001761 neg = b->neg;
1762 if (sub) neg = !neg;
1763 minuend = b;
1764 subtrahend = a;
1765 }
1766
1767 bc_num_copy(c, minuend);
1768 c->neg = neg;
1769
1770 if (c->rdx < subtrahend->rdx) {
1771 bc_num_extend(c, subtrahend->rdx - c->rdx);
1772 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001773 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001774 start = c->rdx - subtrahend->rdx;
1775
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001776 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001777
1778 bc_num_clean(c);
1779
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001780 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001781}
1782
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001783static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001784 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001785#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001786{
1787 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001788 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001789 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001790 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001791
Gavin Howard01055ba2018-11-03 11:00:21 -06001792 if (a->len == 0 || b->len == 0) {
1793 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001794 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001795 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001796 aone = BC_NUM_ONE(a);
1797 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001798 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001799 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 }
1801
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001802 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1803 || a->len < BC_NUM_KARATSUBA_LEN
1804 || b->len < BC_NUM_KARATSUBA_LEN
1805 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001806 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001807
Gavin Howard01055ba2018-11-03 11:00:21 -06001808 bc_num_expand(c, a->len + b->len + 1);
1809
1810 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001811 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001812
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001813 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001814 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001815 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001816 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001817 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001818 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001819 carry = in / 10;
1820 c->num[i + j] = (BcDig)(in % 10);
1821 }
1822
1823 c->num[i + j] += (BcDig) carry;
1824 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001825
Denys Vlasenko14767602018-12-27 22:52:13 +01001826#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001827 // a=2^1000000
1828 // a*a <- without check below, this will not be interruptible
1829 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001830#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 }
1832
1833 c->len = len;
1834
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001835 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001836 }
1837
1838 bc_num_init(&l1, max);
1839 bc_num_init(&h1, max);
1840 bc_num_init(&l2, max);
1841 bc_num_init(&h2, max);
1842 bc_num_init(&m1, max);
1843 bc_num_init(&m2, max);
1844 bc_num_init(&z0, max);
1845 bc_num_init(&z1, max);
1846 bc_num_init(&z2, max);
1847 bc_num_init(&temp, max + max);
1848
1849 bc_num_split(a, max2, &l1, &h1);
1850 bc_num_split(b, max2, &l2, &h2);
1851
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001852 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001853 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001854 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001855 if (s) goto err;
1856
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001857 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001858 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001859 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001860 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001861 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001862 if (s) goto err;
1863
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001864 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001865 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001866 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 if (s) goto err;
1868
Denys Vlasenko29301232018-12-11 15:29:32 +01001869 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001871 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001872 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001873 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001875 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001876 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 bc_num_free(&temp);
1878 bc_num_free(&z2);
1879 bc_num_free(&z1);
1880 bc_num_free(&z0);
1881 bc_num_free(&m2);
1882 bc_num_free(&m1);
1883 bc_num_free(&h2);
1884 bc_num_free(&l2);
1885 bc_num_free(&h1);
1886 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001887 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001888}
1889
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001890static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001891{
1892 BcStatus s;
1893 BcNum cpa, cpb;
1894 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1895
1896 scale = BC_MAX(scale, a->rdx);
1897 scale = BC_MAX(scale, b->rdx);
1898 scale = BC_MIN(a->rdx + b->rdx, scale);
1899 maxrdx = BC_MAX(maxrdx, scale);
1900
1901 bc_num_init(&cpa, a->len);
1902 bc_num_init(&cpb, b->len);
1903
1904 bc_num_copy(&cpa, a);
1905 bc_num_copy(&cpb, b);
1906 cpa.neg = cpb.neg = false;
1907
Denys Vlasenko29301232018-12-11 15:29:32 +01001908 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001910 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001911 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001912 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001913 if (s) goto err;
1914
1915 maxrdx += scale;
1916 bc_num_expand(c, c->len + maxrdx);
1917
1918 if (c->len < maxrdx) {
1919 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1920 c->len += maxrdx;
1921 }
1922
1923 c->rdx = maxrdx;
1924 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001925 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001926 bc_num_free(&cpb);
1927 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001928 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001929}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001930#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001931
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001932static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001933{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001934 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001935 size_t len, end, i;
1936 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001937
1938 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001939 RETURN_STATUS(bc_error("divide by zero"));
1940 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001941 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001942 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001943 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001944 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001945 bc_num_copy(c, a);
1946 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001947 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001948 }
1949
1950 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1951 bc_num_copy(&cp, a);
1952 len = b->len;
1953
1954 if (len > cp.len) {
1955 bc_num_expand(&cp, len + 2);
1956 bc_num_extend(&cp, len - cp.len);
1957 }
1958
1959 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1960 cp.rdx -= b->rdx;
1961 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1962
1963 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001964 for (;;) {
1965 if (len == 0) break;
1966 len--;
1967 if (b->num[len] != 0)
1968 break;
1969 }
1970 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001971 }
1972
1973 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1974
1975 // We want an extra zero in front to make things simpler.
1976 cp.num[cp.len++] = 0;
1977 end = cp.len - len;
1978
1979 bc_num_expand(c, cp.len);
1980
1981 bc_num_zero(c);
1982 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1983 c->rdx = cp.rdx;
1984 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001985
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001986 s = BC_STATUS_SUCCESS;
1987 for (i = end - 1; i < end; --i) {
1988 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06001989 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001990 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
1991 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001992 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01001993#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01001994 // a=2^100000
1995 // scale=40000
1996 // 1/a <- without check below, this will not be interruptible
1997 if (G_interrupt) {
1998 s = BC_STATUS_FAILURE;
1999 break;
2000 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002001#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002002 }
2003
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002004 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002005 bc_num_free(&cp);
2006
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002007 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002008}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002009#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002010
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002011static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002012 BcNum *restrict d, size_t scale, size_t ts)
2013{
2014 BcStatus s;
2015 BcNum temp;
2016 bool neg;
2017
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002018 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002019 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002020
2021 if (a->len == 0) {
2022 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002023 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002024 }
2025
2026 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002027 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002028 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002029
2030 if (scale != 0) scale = ts;
2031
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002032 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002033 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002034 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002035 if (s) goto err;
2036
2037 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2038
2039 neg = d->neg;
2040 bc_num_retireMul(d, ts, a->neg, b->neg);
2041 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002042 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002043 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002044 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002045}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002046#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002047
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002048static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002049{
2050 BcStatus s;
2051 BcNum c1;
2052 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2053
2054 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002055 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002056 bc_num_free(&c1);
2057
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002059}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002060#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002061
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002062static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002063{
2064 BcStatus s = BC_STATUS_SUCCESS;
2065 BcNum copy;
2066 unsigned long pow;
2067 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002068 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002069
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002070 // GNU bc does not allow 2^2.0 - we do
2071 for (i = 0; i < b->rdx; i++)
2072 if (b->num[i] != 0)
2073 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002074
2075 if (b->len == 0) {
2076 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002077 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002078 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002079 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002080 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002081 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002082 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002083 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002084 if (!b->neg)
2085 bc_num_copy(c, a);
2086 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002087 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002088 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002089 }
2090
2091 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002092 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002094 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002095
2096 bc_num_init(&copy, a->len);
2097 bc_num_copy(&copy, a);
2098
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002099 if (!neg) {
2100 if (a->rdx > scale)
2101 scale = a->rdx;
2102 if (a->rdx * pow < scale)
2103 scale = a->rdx * pow;
2104 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002105
Gavin Howard01055ba2018-11-03 11:00:21 -06002106
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002107 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002108 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002109 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002110 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002111 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002112 //if (G_interrupt) {
2113 // s = BC_STATUS_FAILURE;
2114 // goto err;
2115 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002116 }
2117
Gavin Howard01055ba2018-11-03 11:00:21 -06002118 bc_num_copy(c, &copy);
2119
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002120 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002122 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 if (s) goto err;
2124
2125 if (pow & 1) {
2126 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002127 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 if (s) goto err;
2129 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002130 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002131 //if (G_interrupt) {
2132 // s = BC_STATUS_FAILURE;
2133 // goto err;
2134 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002135 }
2136
2137 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002138 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002139 if (s) goto err;
2140 }
2141
Gavin Howard01055ba2018-11-03 11:00:21 -06002142 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2143
2144 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002145 for (i = 0; i < c->len; ++i)
2146 if (c->num[i] != 0)
2147 goto skip;
2148 bc_num_setToZero(c, scale);
2149 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002150
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002151 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002153 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002154}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002155#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002156
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002157static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002158{
2159 BcStatus s;
2160 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002161 BcDig half_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2163 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2164
2165 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2166 bc_num_expand(b, req);
2167
2168 if (a->len == 0) {
2169 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002170 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002171 }
2172 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002173 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002174 }
2175 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002176 bc_num_one(b);
2177 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002178 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002179 }
2180
2181 scale = BC_MAX(scale, a->rdx) + 1;
2182 len = a->len + scale;
2183
2184 bc_num_init(&num1, len);
2185 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002186
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002187 half.cap = ARRAY_SIZE(half_digs);
2188 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002189 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002190 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002191 half.rdx = 1;
2192
2193 bc_num_init(&f, len);
2194 bc_num_init(&fprime, len);
2195
2196 x0 = &num1;
2197 x1 = &num2;
2198
2199 bc_num_one(x0);
2200 pow = BC_NUM_INT(a);
2201
2202 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002203 if (pow & 1)
2204 x0->num[0] = 2;
2205 else
2206 x0->num[0] = 6;
2207
2208 pow -= 2 - (pow & 1);
2209
2210 bc_num_extend(x0, pow);
2211
2212 // Make sure to move the radix back.
2213 x0->rdx -= pow;
2214 }
2215
2216 x0->rdx = digs = digs1 = 0;
2217 resrdx = scale + 2;
2218 len = BC_NUM_INT(x0) + resrdx - 1;
2219
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002220 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002221 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002222 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002223 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002224 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002225 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002226 if (s) goto err;
2227
2228 cmp = bc_num_cmp(x1, x0);
2229 digs = x1->len - (unsigned long long) llabs(cmp);
2230
2231 if (cmp == cmp2 && digs == digs1)
2232 times += 1;
2233 else
2234 times = 0;
2235
2236 resrdx += times > 4;
2237
2238 cmp2 = cmp1;
2239 cmp1 = cmp;
2240 digs1 = digs;
2241
2242 temp = x0;
2243 x0 = x1;
2244 x1 = temp;
2245 }
2246
Gavin Howard01055ba2018-11-03 11:00:21 -06002247 bc_num_copy(b, x0);
2248 scale -= 1;
2249 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002250 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002251 bc_num_free(&fprime);
2252 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002253 bc_num_free(&num2);
2254 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002255 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002256}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002257#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002258
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002259static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002260 size_t scale)
2261{
2262 BcStatus s;
2263 BcNum num2, *ptr_a;
2264 bool init = false;
2265 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2266
2267 if (c == a) {
2268 memcpy(&num2, c, sizeof(BcNum));
2269 ptr_a = &num2;
2270 bc_num_init(c, len);
2271 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002272 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002273 ptr_a = a;
2274 bc_num_expand(c, len);
2275 }
2276
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002277 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002278
2279 if (init) bc_num_free(&num2);
2280
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002281 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002282}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002283#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002284
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002285#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002286static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002287{
2288 BcStatus s;
2289 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002290 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002291
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002292 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002293 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002294 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002295 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002296 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002297 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002298
2299 bc_num_expand(d, c->len);
2300 bc_num_init(&base, c->len);
2301 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002302 bc_num_init(&temp, b->len);
2303
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002304 two.cap = ARRAY_SIZE(two_digs);
2305 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002306 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002307 two_digs[0] = 2;
2308
Gavin Howard01055ba2018-11-03 11:00:21 -06002309 bc_num_one(d);
2310
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002311 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002312 if (s) goto err;
2313 bc_num_copy(&exp, b);
2314
2315 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002316 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002317 if (s) goto err;
2318
2319 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002320 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002321 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002322 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002323 if (s) goto err;
2324 }
2325
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002326 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002327 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002328 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002329 if (s) goto err;
2330 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002331 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002332 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002333 bc_num_free(&exp);
2334 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002335 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002336}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002337#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002338#endif // ENABLE_DC
2339
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002340static FAST_FUNC void bc_string_free(void *string)
2341{
2342 free(*(char**)string);
2343}
2344
Gavin Howard01055ba2018-11-03 11:00:21 -06002345static void bc_func_init(BcFunc *f)
2346{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002347 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002348 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2349 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002350 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2351 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002352 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002353}
2354
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002355static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002356{
2357 BcFunc *f = (BcFunc *) func;
2358 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002359 IF_BC(bc_vec_free(&f->labels);)
2360 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002361 IF_BC(bc_vec_free(&f->strs);)
2362 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002363}
2364
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002365static void bc_array_expand(BcVec *a, size_t len);
2366
Gavin Howard01055ba2018-11-03 11:00:21 -06002367static void bc_array_init(BcVec *a, bool nums)
2368{
2369 if (nums)
2370 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2371 else
2372 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2373 bc_array_expand(a, 1);
2374}
2375
Gavin Howard01055ba2018-11-03 11:00:21 -06002376static void bc_array_expand(BcVec *a, size_t len)
2377{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002378 if (a->dtor == bc_num_free
2379 // && a->size == sizeof(BcNum) - always true
2380 ) {
2381 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002382 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002383 bc_num_init_DEF_SIZE(&n);
2384 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002385 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002386 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002387 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002388 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002389 bc_array_init(&v, true);
2390 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002391 }
2392 }
2393}
2394
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002395static void bc_array_copy(BcVec *d, const BcVec *s)
2396{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002397 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002398 size_t i;
2399
2400 bc_vec_pop_all(d);
2401 bc_vec_expand(d, s->cap);
2402 d->len = s->len;
2403
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002404 dnum = (void*)d->v;
2405 snum = (void*)s->v;
2406 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002407 bc_num_init(dnum, snum->len);
2408 bc_num_copy(dnum, snum);
2409 }
2410}
2411
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002412#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002413static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002414{
2415 d->t = src->t;
2416
2417 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002418 case XC_RESULT_TEMP:
2419 case XC_RESULT_IBASE:
2420 case XC_RESULT_SCALE:
2421 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002422 bc_num_init(&d->d.n, src->d.n.len);
2423 bc_num_copy(&d->d.n, &src->d.n);
2424 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002425 case XC_RESULT_VAR:
2426 case XC_RESULT_ARRAY:
2427 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002428 d->d.id.name = xstrdup(src->d.id.name);
2429 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002430 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002431 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002432 IF_BC(case BC_RESULT_ONE:)
2433 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002434 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2435 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002436 }
2437}
2438#endif // ENABLE_DC
2439
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002440static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002441{
2442 BcResult *r = (BcResult *) result;
2443
2444 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002445 case XC_RESULT_TEMP:
2446 case XC_RESULT_IBASE:
2447 case XC_RESULT_SCALE:
2448 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002449 bc_num_free(&r->d.n);
2450 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002451 case XC_RESULT_VAR:
2452 case XC_RESULT_ARRAY:
2453 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002454 free(r->d.id.name);
2455 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002456 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002457 // Do nothing.
2458 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002459 }
2460}
2461
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002462static int bad_input_byte(char c)
2463{
2464 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2465 || c > 0x7e
2466 ) {
2467 bc_error_fmt("illegal character 0x%02x", c);
2468 return 1;
2469 }
2470 return 0;
2471}
2472
Denys Vlasenko10bde142018-12-27 18:23:58 +01002473static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002474{
2475 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002476 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002477 fflush_and_check();
2478
Denys Vlasenko14767602018-12-27 22:52:13 +01002479#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002480 if (G_interrupt) { // ^C was pressed
2481 intr:
2482 if (fp != stdin) {
2483 // ^C while running a script (bc SCRIPT): die.
2484 // We do not return to interactive prompt:
2485 // user might be running us from a shell,
2486 // and SCRIPT might be intended to terminate
2487 // (e.g. contain a "halt" stmt).
2488 // ^C dropping user into a bc prompt instead of
2489 // the shell would be unexpected.
2490 xfunc_die();
2491 }
2492 // ^C while interactive input
2493 G_interrupt = 0;
2494 // GNU bc says "interrupted execution."
2495 // GNU dc says "Interrupt!"
2496 fputs("\ninterrupted execution\n", stderr);
2497 }
2498
2499# if ENABLE_FEATURE_EDITING
2500 if (G_ttyin && fp == stdin) {
2501 int n, i;
2502# define line_buf bb_common_bufsiz1
2503 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2504 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2505 if (n == 0) // ^C
2506 goto intr;
2507 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2508 return;
2509 }
2510 i = 0;
2511 for (;;) {
2512 char c = line_buf[i++];
2513 if (!c) break;
2514 if (bad_input_byte(c)) goto again;
2515 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002516 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002517# undef line_buf
2518 } else
2519# endif
2520#endif
2521 {
2522 int c;
2523 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002524
2525 do {
Denys Vlasenko14767602018-12-27 22:52:13 +01002526#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002527 if (G_interrupt) {
2528 // ^C was pressed: ignore entire line, get another one
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002529 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002530 goto intr;
2531 }
2532#endif
2533 do c = fgetc(fp); while (c == '\0');
2534 if (c == EOF) {
2535 if (ferror(fp))
2536 bb_perror_msg_and_die("input error");
2537 // Note: EOF does not append '\n'
2538 break;
2539 }
2540 bad_chars |= bad_input_byte(c);
2541 bc_vec_pushByte(vec, (char)c);
2542 } while (c != '\n');
2543
2544 if (bad_chars) {
2545 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002546 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002547 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002548 goto again;
2549 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002550 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002551 }
2552 bc_vec_pushZeroByte(vec);
2553 }
2554}
2555
2556//
2557// Parsing routines
2558//
2559
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002560// "Input numbers may contain the characters 0-9 and A-Z.
2561// (Note: They must be capitals. Lower case letters are variable names.)
2562// Single digit numbers always have the value of the digit regardless of
2563// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2564// all input digits greater or equal to ibase to the value of ibase-1.
2565// This makes the number ZZZ always be the largest 3 digit number of the
2566// input base."
2567static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002568{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002569 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002570 for (;;) {
2571 BcDig c = *val++;
2572 if (c == '\0')
2573 break;
2574 if (c == '.') {
2575 if (radix) return false;
2576 radix = true;
2577 continue;
2578 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002579 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002580 return false;
2581 }
2582 return true;
2583}
2584
2585// Note: n is already "bc_num_zero()"ed,
2586// leading zeroes in "val" are removed
2587static void bc_num_parseDecimal(BcNum *n, const char *val)
2588{
2589 size_t len, i;
2590 const char *ptr;
2591
2592 len = strlen(val);
2593 if (len == 0)
2594 return;
2595
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002596 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002597
2598 ptr = strchr(val, '.');
2599
2600 n->rdx = 0;
2601 if (ptr != NULL)
2602 n->rdx = (size_t)((val + len) - (ptr + 1));
2603
2604 for (i = 0; val[i]; ++i) {
2605 if (val[i] != '0' && val[i] != '.') {
2606 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002607 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002608 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002609 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002610 if (c > 9) { // A-Z => 10-36
2611 n->len = 2;
2612 c -= ('A' - '9' - 1);
2613 n->num[1] = c/10;
2614 c = c%10;
2615 }
2616 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002617 break;
2618 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002619 i = len - 1;
2620 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002621 char c = val[i] - '0';
2622 if (c > 9) // A-Z => 9
2623 c = 9;
2624 n->num[n->len] = c;
2625 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002626 skip_dot:
2627 if (i == 0) break;
2628 if (val[--i] == '.') goto skip_dot;
2629 }
2630 break;
2631 }
2632 }
2633 // if for() exits without hitting if(), the value is entirely zero
2634}
2635
2636// Note: n is already "bc_num_zero()"ed,
2637// leading zeroes in "val" are removed
2638static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2639{
2640 BcStatus s;
2641 BcNum temp, mult, result;
2642 BcNum base;
2643 BcDig base_digs[ULONG_NUM_BUFSIZE];
2644 BcDig c = '\0';
2645 unsigned long v;
2646 size_t i, digits;
2647
2648 for (i = 0; ; ++i) {
2649 if (val[i] == '\0')
2650 return;
2651 if (val[i] != '.' && val[i] != '0')
2652 break;
2653 }
2654
2655 bc_num_init_DEF_SIZE(&temp);
2656 bc_num_init_DEF_SIZE(&mult);
2657 base.cap = ARRAY_SIZE(base_digs);
2658 base.num = base_digs;
2659 bc_num_ulong2num(&base, base_t);
2660
2661 for (;;) {
2662 c = *val++;
2663 if (c == '\0') goto int_err;
2664 if (c == '.') break;
2665
2666 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2667
2668 s = zbc_num_mul(n, &base, &mult, 0);
2669 if (s) goto int_err;
2670 bc_num_ulong2num(&temp, v);
2671 s = zbc_num_add(&mult, &temp, n, 0);
2672 if (s) goto int_err;
2673 }
2674
2675 bc_num_init(&result, base.len);
2676 //bc_num_zero(&result); - already is
2677 bc_num_one(&mult);
2678
2679 digits = 0;
2680 for (;;) {
2681 c = *val++;
2682 if (c == '\0') break;
2683 digits++;
2684
2685 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2686
2687 s = zbc_num_mul(&result, &base, &result, 0);
2688 if (s) goto err;
2689 bc_num_ulong2num(&temp, v);
2690 s = zbc_num_add(&result, &temp, &result, 0);
2691 if (s) goto err;
2692 s = zbc_num_mul(&mult, &base, &mult, 0);
2693 if (s) goto err;
2694 }
2695
2696 s = zbc_num_div(&result, &mult, &result, digits);
2697 if (s) goto err;
2698 s = zbc_num_add(n, &result, n, digits);
2699 if (s) goto err;
2700
2701 if (n->len != 0) {
2702 if (n->rdx < digits)
2703 bc_num_extend(n, digits - n->rdx);
2704 } else
2705 bc_num_zero(n);
2706 err:
2707 bc_num_free(&result);
2708 int_err:
2709 bc_num_free(&mult);
2710 bc_num_free(&temp);
2711}
2712
Denys Vlasenko10bde142018-12-27 18:23:58 +01002713static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002714{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002715 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002716 RETURN_STATUS(bc_error("bad number string"));
2717
2718 bc_num_zero(n);
2719 while (*val == '0') val++;
2720
2721 if (base_t == 10)
2722 bc_num_parseDecimal(n, val);
2723 else
2724 bc_num_parseBase(n, val, base_t);
2725
2726 RETURN_STATUS(BC_STATUS_SUCCESS);
2727}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002728#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002729
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002730// p->lex_inbuf points to the current string to be parsed.
2731// if p->lex_inbuf points to '\0', it's either EOF or it points after
2732// last processed line's terminating '\n' (and more reading needs to be done
2733// to get next character).
2734//
2735// If you are in a situation where that is a possibility, call peek_inbuf().
2736// If necessary, it performs more reading and changes p->lex_inbuf,
2737// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2738// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2739// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2740//
2741// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2742// it returns current char and advances the pointer (if not EOF).
2743// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2744//
2745// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2746// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2747// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002748//
2749// bc cases to test interactively:
2750// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2751// 1 #comment/* - prints "1<newline>" at once
2752// 1 #comment" - prints "1<newline>" at once
2753// 1\#comment - error at once (\ is not a line continuation)
2754// 1 + /*"*/2 - prints "3<newline>" at once
2755// 1 + /*#*/2 - prints "3<newline>" at once
2756// "str\" - prints "str\" at once
2757// "str#" - prints "str#" at once
2758// "str/*" - prints "str/*" at once
2759// "str#\ - waits for second line
2760// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002761static char peek_inbuf(void)
2762{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002763 if (*G.prs.lex_inbuf == '\0'
2764 && G.prs.lex_input_fp
2765 ) {
2766 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2767 G.prs.lex_inbuf = G.input_buffer.v;
2768 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2769 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002770 }
2771 return *G.prs.lex_inbuf;
2772}
2773static char eat_inbuf(void)
2774{
2775 char c = peek_inbuf();
2776 if (c) G.prs.lex_inbuf++;
2777 return c;
2778}
2779
Denys Vlasenko10bde142018-12-27 18:23:58 +01002780static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002781{
2782 BcParse *p = &G.prs;
2783 char c;
2784
2785 // Try: echo -n '#foo' | bc
2786 p->lex = XC_LEX_WHITESPACE;
2787
Denys Vlasenko2747f612018-12-31 18:48:10 +01002788 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002789 // '\0' which isn't the EOF can only be seen after '\n'.
2790 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2791 p->lex_inbuf++;
2792}
2793
Denys Vlasenko10bde142018-12-27 18:23:58 +01002794static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002795{
2796 BcParse *p = &G.prs;
2797
2798 p->lex = XC_LEX_WHITESPACE;
2799 for (;;) {
2800 // We depend here on input being done in whole lines:
2801 // '\0' which isn't the EOF can only be seen after '\n'.
2802 char c = *p->lex_inbuf;
2803 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2804 break;
2805 if (!isspace(c))
2806 break;
2807 p->lex_inbuf++;
2808 }
2809}
2810
Denys Vlasenko10bde142018-12-27 18:23:58 +01002811static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002812{
2813 BcParse *p = &G.prs;
2814 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002815 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002816
2817 bc_vec_pop_all(&p->lex_strnumbuf);
2818 bc_vec_pushByte(&p->lex_strnumbuf, last);
2819
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002820// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002821// (Note: They must be capitals. Lower case letters are variable names.)
2822// Single digit numbers always have the value of the digit regardless of
2823// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2824// all input digits greater or equal to ibase to the value of ibase-1.
2825// This makes the number ZZZ always be the largest 3 digit number of the
2826// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002827// dc only allows A-F, the rules about single-char and multi-char are the same.
2828 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002829 pt = (last == '.');
2830 p->lex = XC_LEX_NUMBER;
2831 for (;;) {
2832 // We depend here on input being done in whole lines:
2833 // '\0' which isn't the EOF can only be seen after '\n'.
2834 char c = *p->lex_inbuf;
2835 check_c:
2836 if (c == '\0')
2837 break;
2838 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2839 p->lex_inbuf += 2;
2840 p->lex_line++;
2841 c = peek_inbuf(); // force next line to be read
2842 goto check_c;
2843 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002844 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002845 if (c != '.') break;
2846 // if '.' was already seen, stop on second one:
2847 if (pt) break;
2848 pt = true;
2849 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002850 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002851 last = c;
2852 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2853 p->lex_inbuf++;
2854 }
2855 if (last == '.') // remove trailing '.' if any
2856 bc_vec_pop(&p->lex_strnumbuf);
2857 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2858
2859 G.err_line = G.prs.lex_line;
2860 RETURN_STATUS(BC_STATUS_SUCCESS);
2861}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002862#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002863
Denys Vlasenko10bde142018-12-27 18:23:58 +01002864static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002865{
2866 BcParse *p = &G.prs;
2867 size_t i;
2868 const char *buf;
2869
2870 p->lex = XC_LEX_NAME;
2871
2872 // Since names can't cross lines with \<newline>,
2873 // we depend on the fact that whole line is in the buffer
2874 i = 0;
2875 buf = p->lex_inbuf - 1;
2876 for (;;) {
2877 char c = buf[i];
2878 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2879 i++;
2880 }
2881
2882#if 0 // We do not protect against people with gigabyte-long names
2883 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2884 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2885 if (i > BC_MAX_STRING)
2886 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2887 }
2888#endif
2889 bc_vec_string(&p->lex_strnumbuf, i, buf);
2890
2891 // Increment the index. We minus 1 because it has already been incremented.
2892 p->lex_inbuf += i - 1;
2893
2894 //return BC_STATUS_SUCCESS;
2895}
2896
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002897IF_BC(static BC_STATUS zbc_lex_token(void);)
2898IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002899#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2900#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002901
Denys Vlasenko10bde142018-12-27 18:23:58 +01002902static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002903{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002904 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002905 BcStatus s;
2906
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002907 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002908//why?
2909// if (p->lex_last == XC_LEX_EOF)
2910// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002911
2912 // Loop until failure or we don't have whitespace. This
2913 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002914 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002915 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002916 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002917 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002918 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002919 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002920 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002921 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002922 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002923 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002924 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2925 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002926 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002927 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002928 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002929 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002930 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002931 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002932 } while (!s && p->lex == XC_LEX_WHITESPACE);
2933 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002934
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002935 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002936}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002937#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002938
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002939#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002940static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002941{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002942 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002943 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002944 RETURN_STATUS(BC_STATUS_SUCCESS);
2945}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002946#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002947
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002948static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002949{
2950 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01002951 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002952 if (s) RETURN_STATUS(s);
2953 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002954 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002955 RETURN_STATUS(s);
2956}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002957#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002958
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002959static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002960{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002961 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002962 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002963 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002964 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002965
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002966 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2967 const char *keyword8 = bc_lex_kws[i].name8;
2968 unsigned j = 0;
2969 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2970 j++;
2971 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002972 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002973 if (keyword8[j] != '\0')
2974 continue;
2975 match:
2976 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002977 if (isalnum(buf[j]) || buf[j]=='_')
2978 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002979 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01002980 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01002981 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
2982 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002983 }
2984
2985 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002986 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002987 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002988 }
2989
Denys Vlasenko10bde142018-12-27 18:23:58 +01002990 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002991 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002992
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002993 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002994 // Prevent this:
2995 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01002996 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002997 // '
2998 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01002999 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003000 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003001
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003002 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003003}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003004#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003005
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003006static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003007{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003008 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003009
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003010 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003011 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003012 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003013 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003014 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003015 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003016 }
3017 if (c == '"')
3018 break;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003019 if (c == '\n')
3020 p->lex_line++;
3021 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3022 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003023 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003024 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3025 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003026
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003027 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003028 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003029}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003030#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003031
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003032static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003033{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003034 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003035 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003036 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003037 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003038 with_and_without >>= 8; // store "with" value
3039 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003040 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003041}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003042#define parse_lex_by_checking_eq_sign(with, without) \
3043 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003044
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003045static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003046{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003047 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003048
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003049 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003050 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003051 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003052 char c;
3053
3054 p->lex_inbuf++;
3055 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003056 check_star:
3057 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003058 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003059 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003060 if (c == '/')
3061 break;
3062 goto check_star;
3063 }
3064 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003065 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003066 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003067 if (c == '\n')
3068 p->lex_line++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003069 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003070 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003071
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003072 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003073 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003074}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003075#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003076
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003077#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003078static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003079{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003080 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003081 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003082 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003083 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003084
3085 // This is the workhorse of the lexer.
3086 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003087// case '\0': // probably never reached
3088// p->lex_inbuf--;
3089// p->lex = XC_LEX_EOF;
3090// break;
3091 case '\n':
3092 p->lex_line++;
3093 p->lex = XC_LEX_NLINE;
3094 break;
3095 case '\t':
3096 case '\v':
3097 case '\f':
3098 case '\r':
3099 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003100 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003101 break;
3102 case '!':
3103 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3104 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3105 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003106 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003107 }
3108 break;
3109 case '"':
3110 s = zbc_lex_string();
3111 break;
3112 case '#':
3113 s = zbc_POSIX_does_not_allow("'#' script comments");
3114 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003115 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003116 break;
3117 case '%':
3118 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3119 break;
3120 case '&':
3121 c2 = *p->lex_inbuf;
3122 if (c2 == '&') {
3123 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3124 if (s) RETURN_STATUS(s);
3125 p->lex_inbuf++;
3126 p->lex = BC_LEX_OP_BOOL_AND;
3127 } else {
3128 p->lex = XC_LEX_INVALID;
3129 s = bc_error_bad_character('&');
3130 }
3131 break;
3132 case '(':
3133 case ')':
3134 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3135 break;
3136 case '*':
3137 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3138 break;
3139 case '+':
3140 c2 = *p->lex_inbuf;
3141 if (c2 == '+') {
3142 p->lex_inbuf++;
3143 p->lex = BC_LEX_OP_INC;
3144 } else
3145 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3146 break;
3147 case ',':
3148 p->lex = BC_LEX_COMMA;
3149 break;
3150 case '-':
3151 c2 = *p->lex_inbuf;
3152 if (c2 == '-') {
3153 p->lex_inbuf++;
3154 p->lex = BC_LEX_OP_DEC;
3155 } else
3156 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3157 break;
3158 case '.':
3159 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003160 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003161 else {
3162 p->lex = BC_LEX_KEY_LAST;
3163 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3164 }
3165 break;
3166 case '/':
3167 c2 = *p->lex_inbuf;
3168 if (c2 == '*')
3169 s = zbc_lex_comment();
3170 else
3171 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3172 break;
3173 case '0':
3174 case '1':
3175 case '2':
3176 case '3':
3177 case '4':
3178 case '5':
3179 case '6':
3180 case '7':
3181 case '8':
3182 case '9':
3183 case 'A':
3184 case 'B':
3185 case 'C':
3186 case 'D':
3187 case 'E':
3188 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003189 case 'G':
3190 case 'H':
3191 case 'I':
3192 case 'J':
3193 case 'K':
3194 case 'L':
3195 case 'M':
3196 case 'N':
3197 case 'O':
3198 case 'P':
3199 case 'Q':
3200 case 'R':
3201 case 'S':
3202 case 'T':
3203 case 'U':
3204 case 'V':
3205 case 'W':
3206 case 'X':
3207 case 'Y':
3208 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003209 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003210 break;
3211 case ';':
3212 p->lex = BC_LEX_SCOLON;
3213 break;
3214 case '<':
3215 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3216 break;
3217 case '=':
3218 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3219 break;
3220 case '>':
3221 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3222 break;
3223 case '[':
3224 case ']':
3225 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3226 break;
3227 case '\\':
3228 if (*p->lex_inbuf == '\n') {
3229 p->lex = XC_LEX_WHITESPACE;
3230 p->lex_inbuf++;
3231 } else
3232 s = bc_error_bad_character(c);
3233 break;
3234 case '^':
3235 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3236 break;
3237 case 'a':
3238 case 'b':
3239 case 'c':
3240 case 'd':
3241 case 'e':
3242 case 'f':
3243 case 'g':
3244 case 'h':
3245 case 'i':
3246 case 'j':
3247 case 'k':
3248 case 'l':
3249 case 'm':
3250 case 'n':
3251 case 'o':
3252 case 'p':
3253 case 'q':
3254 case 'r':
3255 case 's':
3256 case 't':
3257 case 'u':
3258 case 'v':
3259 case 'w':
3260 case 'x':
3261 case 'y':
3262 case 'z':
3263 s = zbc_lex_identifier();
3264 break;
3265 case '{':
3266 case '}':
3267 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3268 break;
3269 case '|':
3270 c2 = *p->lex_inbuf;
3271 if (c2 == '|') {
3272 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3273 if (s) RETURN_STATUS(s);
3274 p->lex_inbuf++;
3275 p->lex = BC_LEX_OP_BOOL_OR;
3276 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003277 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003278 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003279 }
3280 break;
3281 default:
3282 p->lex = XC_LEX_INVALID;
3283 s = bc_error_bad_character(c);
3284 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 }
3286
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003287 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003288}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003289#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003290#endif // ENABLE_BC
3291
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003292#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003293static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003294{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003295 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003296 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003297 xc_lex_whitespace(); // eats whitespace (but not newline)
3298 p->lex_inbuf++; // xc_lex_name() expects this
3299 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003300 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003301 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003302 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003303 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3304 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003305 }
3306
Denys Vlasenko29301232018-12-11 15:29:32 +01003307 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003308}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003309#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003310
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003311static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003312{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003313 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003314 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003315
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003316 p->lex = XC_LEX_STR;
3317 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003318
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003319 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003320 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003321 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003322 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003323 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003324 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003325 if (c == '[') depth++;
3326 if (c == ']')
3327 if (--depth == 0)
3328 break;
3329 if (c == '\n')
3330 p->lex_line++;
3331 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3332 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003333 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003334 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003335 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003336
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003337 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003338 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003339}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003340#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003341
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003342#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003343static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003344{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003345 static const //BcLexType - should be this type, but narrower type saves size:
3346 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003347 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003348 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 +01003349 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3350 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003351 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003352 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003353
Denys Vlasenko10bde142018-12-27 18:23:58 +01003354 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003355 BcStatus s;
3356 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003357 size_t i;
3358
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003359 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003360 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003361 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003362 }
3363
Denys Vlasenko81293c82018-12-24 01:53:55 +01003364 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003365 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003366 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003367 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003368 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003369 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003370 }
3371
3372 // This is the workhorse of the lexer.
3373 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003374// case '\0': // probably never reached
3375// p->lex = XC_LEX_EOF;
3376// break;
3377 case '\n':
3378 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3379 // (and "case '\n':" is not just empty here)
3380 // only to allow interactive dc have a way to exit
3381 // "parse" stage of "parse,execute" loop
3382 // on <enter>, not on _next_ token (which would mean
3383 // commands are not executed on pressing <enter>).
3384 // IOW: typing "1p<enter>" should print "1" _at once_,
3385 // not after some more input.
3386 p->lex_line++;
3387 p->lex = XC_LEX_NLINE;
3388 break;
3389 case '\t':
3390 case '\v':
3391 case '\f':
3392 case '\r':
3393 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003394 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003395 break;
3396 case '!':
3397 c2 = *p->lex_inbuf;
3398 if (c2 == '=')
3399 p->lex = XC_LEX_OP_REL_NE;
3400 else if (c2 == '<')
3401 p->lex = XC_LEX_OP_REL_LE;
3402 else if (c2 == '>')
3403 p->lex = XC_LEX_OP_REL_GE;
3404 else
3405 RETURN_STATUS(bc_error_bad_character(c));
3406 p->lex_inbuf++;
3407 break;
3408 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003409 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003410 break;
3411 case '.':
3412 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003413 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003414 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003415 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003416 break;
3417 case '0':
3418 case '1':
3419 case '2':
3420 case '3':
3421 case '4':
3422 case '5':
3423 case '6':
3424 case '7':
3425 case '8':
3426 case '9':
3427 case 'A':
3428 case 'B':
3429 case 'C':
3430 case 'D':
3431 case 'E':
3432 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003433 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003434 break;
3435 case '[':
3436 s = zdc_lex_string();
3437 break;
3438 default:
3439 p->lex = XC_LEX_INVALID;
3440 s = bc_error_bad_character(c);
3441 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003442 }
3443
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003444 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003445}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003446#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003447#endif // ENABLE_DC
3448
Denys Vlasenko10bde142018-12-27 18:23:58 +01003449static void xc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003450{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003451 BcVec *code = &G.prs.func->code;
3452 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
3453 bc_vec_pushByte(code, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003454}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003455
Denys Vlasenko10bde142018-12-27 18:23:58 +01003456static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003457{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003458#if 1
3459 BcVec *code = &G.prs.func->code;
3460 size_t pos = code->len;
3461 size_t len = strlen(name) + 1;
3462
3463 bc_vec_expand(code, pos + len);
3464 strcpy(code->v + pos, name);
3465 code->len = pos + len;
3466#else
3467 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003468 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003469 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003470 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003471#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003472}
3473
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003474// Indexes < 0xfc are encoded verbatim, else first byte is
3475// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3476// followed by that many bytes, lsb first.
3477// (The above describes 32-bit case).
3478#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3479
Denys Vlasenko10bde142018-12-27 18:23:58 +01003480static void xc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003481{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003482 size_t mask;
3483 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003484
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003485 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003486 if (idx < SMALL_INDEX_LIMIT) {
3487 goto push_idx;
3488 }
3489
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003490 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3491 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003492 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003493 if (idx & mask) break;
3494 mask >>= 8;
3495 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003496 }
3497 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003498
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003499 xc_parse_push((SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003500
3501 while (idx != 0) {
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003502 push_idx:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003503 xc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003504 idx >>= 8;
3505 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003506}
3507
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003508#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003509static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003510{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003511 xc_parse_push(BC_INST_JUMP);
3512 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003513}
3514
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003515static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003516{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003517 xc_parse_push(BC_INST_JUMP_ZERO);
3518 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003519}
3520
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003521static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003522{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003523 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003524 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003525
Denys Vlasenko10bde142018-12-27 18:23:58 +01003526 xc_parse_push(XC_INST_STR);
3527 xc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003528 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003529
Denys Vlasenko10bde142018-12-27 18:23:58 +01003530 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003531}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003532#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003533#endif
3534
Denys Vlasenko10bde142018-12-27 18:23:58 +01003535static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003536{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003537 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003538 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003539#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003540 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003541#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003542 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003543#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003544 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003545#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01003546 xc_parse_push(XC_INST_NUM);
3547 xc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003548}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003549
Denys Vlasenko10bde142018-12-27 18:23:58 +01003550static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003551{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003552 G.prs.func = xc_program_func(G.prs.fidx);
3553 G.prs.lex_inbuf = text;
3554 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3555 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003556}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003557#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003558
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003559// Called when parsing or execution detects a failure,
3560// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003561static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003562{
3563 BcFunc *f;
3564 BcInstPtr *ip;
3565
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003566 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003567 bc_vec_pop_all(&G.prog.results);
3568
Denys Vlasenko10bde142018-12-27 18:23:58 +01003569 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003570 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003571 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003572}
3573
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003574// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003575// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003576static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003577{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003578 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003579 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003580 bc_func_free(p->func);
3581 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003582
Denys Vlasenko65e10462018-12-19 15:13:14 +01003583 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003584 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003585 }
3586
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003587 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003588 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003589
Denys Vlasenko503faf92018-12-20 16:24:18 +01003590 IF_BC(bc_vec_pop_all(&p->exits);)
3591 IF_BC(bc_vec_pop_all(&p->conds);)
3592 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003593
Denys Vlasenko10bde142018-12-27 18:23:58 +01003594 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003595}
3596
Denys Vlasenko10bde142018-12-27 18:23:58 +01003597static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003598{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003599 IF_BC(bc_vec_free(&G.prs.exits);)
3600 IF_BC(bc_vec_free(&G.prs.conds);)
3601 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003602 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003603}
3604
Denys Vlasenko10bde142018-12-27 18:23:58 +01003605static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003606{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003607 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003608 memset(p, 0, sizeof(BcParse));
3609
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003610 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003611 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3612 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3613 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003614
Denys Vlasenkofa210792018-12-19 19:35:40 +01003615 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003616 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003617}
3618
Denys Vlasenko10bde142018-12-27 18:23:58 +01003619static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003620{
Denys Vlasenko04715442018-12-21 00:10:26 +01003621 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003622 BcFunc f;
3623 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003624 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003625 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003626 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003627}
3628
3629#if ENABLE_BC
3630
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003631// Note: takes ownership of 'name' (must be malloced)
3632static size_t bc_program_addFunc(char *name)
3633{
3634 size_t idx;
3635 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003636 int inserted;
3637
3638 entry.name = name;
3639 entry.idx = G.prog.fns.len;
3640
3641 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3642 if (!inserted) free(name);
3643
3644 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3645 idx = entry_ptr->idx;
3646
3647 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003648 // There is already a function with this name.
3649 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003650 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003651 bc_func_free(func);
3652 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003653 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003654 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003655 }
3656
3657 return idx;
3658}
3659
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003660#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003661// We can calculate the conversion between tokens and exprs by subtracting the
3662// position of the first operator in the lex enum and adding the position of the
3663// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003664#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003665
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003666static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003667
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003668static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003669{
3670 BcStatus s;
3671
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003672 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003673 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3674 RETURN_STATUS(bc_error("empty expression"));
3675 RETURN_STATUS(s);
3676}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003677#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003678
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003679static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003680#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003681
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003682static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003683{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003684 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003685}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003686#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003687
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003688static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003689{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003690 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003691 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3692 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003693 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003694 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003695 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003696 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003697
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003698 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003699}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003700#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003701
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003702static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003703{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003704 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003705 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3706 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003707
3708 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003709 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003710 if (t == BC_LEX_LPAREN) break;
3711
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003712 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003713 if (l >= r && (l != r || !left)) break;
3714
Denys Vlasenko10bde142018-12-27 18:23:58 +01003715 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003716 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003717 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003718 }
3719
3720 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003721}
3722
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003723static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003724{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003725 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003726 BcLexType top;
3727
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003728 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003729 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003730 top = BC_PARSE_TOP_OP(p);
3731
3732 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003733 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003734
3735 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003736 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003737
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003738 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003739 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003740 top = BC_PARSE_TOP_OP(p);
3741 }
3742
3743 bc_vec_pop(&p->ops);
3744
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003745 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003746}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003747#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003748
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003749static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003750{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003751 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003752 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003753 size_t nparams;
3754
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003755 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003756 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3757
Denys Vlasenko10bde142018-12-27 18:23:58 +01003758 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003759 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003760
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003761 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003762 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003763 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003764 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003765 if (s) RETURN_STATUS(s);
3766 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003767 if (p->lex != BC_LEX_COMMA) {
3768 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003769 break;
3770 RETURN_STATUS(bc_error_bad_token());
3771 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003772 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003773 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003774 }
3775 }
3776
Denys Vlasenko10bde142018-12-27 18:23:58 +01003777 xc_parse_push(BC_INST_CALL);
3778 xc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003779
Denys Vlasenko26819db2018-12-12 16:08:46 +01003780 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003781}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003782#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003783
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003784// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003785static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003786{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003787 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003788 BcStatus s;
3789 BcId entry, *entry_ptr;
3790 size_t idx;
3791
3792 entry.name = name;
3793
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003794 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003795 if (s) goto err;
3796
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003797 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003798 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 goto err;
3800 }
3801
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003802 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003803
3804 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003805 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003806 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003807 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003808 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 free(name);
3810
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003811 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003812 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003813
Denys Vlasenko10bde142018-12-27 18:23:58 +01003814 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003815 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003816 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003817 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003818}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003819#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003820
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003821static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003822{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003823 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003824 BcStatus s;
3825 char *name;
3826
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003827 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003828 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003829 if (s) goto err;
3830
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003831 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003832 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003833 if (s) goto err;
3834
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003835 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003836 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003837 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003838 goto err;
3839 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003840 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003841 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003842 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003844 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003845 if (s) goto err;
3846 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003847 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003848 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003849 xc_parse_push(*type);
3850 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003851 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003852 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003853 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003854 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003855 goto err;
3856 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003857 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003858 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003859 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003860 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003861 xc_parse_push(XC_INST_VAR);
3862 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003863 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003864 }
3865
Denys Vlasenko26819db2018-12-12 16:08:46 +01003866 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003867 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003869 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003870}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003871#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003872
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003873static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003874{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003875 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003876 BcStatus s;
3877
Denys Vlasenko10bde142018-12-27 18:23:58 +01003878 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003879 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003880 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003881
Denys Vlasenko10bde142018-12-27 18:23:58 +01003882 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003883 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003884 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003885
Denys Vlasenko10bde142018-12-27 18:23:58 +01003886 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003887
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003888 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003889}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003890#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003891
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003892static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003893{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003894 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003895 BcStatus s;
3896
Denys Vlasenko10bde142018-12-27 18:23:58 +01003897 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003898 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003899 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003900
3901 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3902
Denys Vlasenko10bde142018-12-27 18:23:58 +01003903 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003904 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003905
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003906 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003907 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003908
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003909 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003910
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003911 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003912 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003913
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003914 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003915}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003916#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003917
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003918static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003919{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003920 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003921 BcStatus s;
3922
Denys Vlasenko10bde142018-12-27 18:23:58 +01003923 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003924 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003925
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003926 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003927 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003928 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003929 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003930 }
3931
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003932 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003933 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3934
Denys Vlasenko10bde142018-12-27 18:23:58 +01003935 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003936 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003937
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003938 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003939 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003940 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003941 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01003942 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003943
Denys Vlasenko10bde142018-12-27 18:23:58 +01003944 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003945}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003946#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003947
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003948static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
3949 size_t *nexprs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003950{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003951 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003952 BcStatus s;
3953 BcLexType type;
3954 char inst;
3955 BcInst etype = *prev;
3956
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003957 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3958 || etype == XC_INST_SCALE || etype == BC_INST_LAST
3959 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003960 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003961 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003962 xc_parse_push(inst);
3963 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003964 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003965 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003966 *paren_expr = true;
3967
Denys Vlasenko10bde142018-12-27 18:23:58 +01003968 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003969 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003970 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06003971
3972 // Because we parse the next part of the expression
3973 // right here, we need to increment this.
3974 *nexprs = *nexprs + 1;
3975
3976 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003977 case XC_LEX_NAME:
3978 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
3979 break;
3980 case BC_LEX_KEY_IBASE:
3981 case BC_LEX_KEY_LAST:
3982 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003983 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
3984 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003985 break;
3986 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003987 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003988 if (s) RETURN_STATUS(s);
3989 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003990 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003991 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01003992 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003993 break;
3994 default:
3995 s = bc_error_bad_token();
3996 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003997 }
3998
Denys Vlasenko10bde142018-12-27 18:23:58 +01003999 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004000 }
4001
Denys Vlasenko26819db2018-12-12 16:08:46 +01004002 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004003}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004004#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004005
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004006#if 0
4007#define BC_PARSE_LEAF(p, rparen) \
4008 ((rparen) \
4009 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
4010 || (p) == BC_INST_INC_POST \
4011 || (p) == BC_INST_DEC_POST \
4012 )
4013#else
4014static int ok_in_expr(BcInst p)
4015{
4016 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4017 || p == BC_INST_INC_POST
4018 || p == BC_INST_DEC_POST
4019 ;
4020}
4021#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
4022#endif
4023
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004024static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
4025 bool rparen, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004026{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004027 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004028 BcStatus s;
4029 BcLexType type;
4030 BcInst etype = *prev;
4031
Denys Vlasenko10bde142018-12-27 18:23:58 +01004032 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004033 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004034
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004035 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004036 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004037
4038 // We can just push onto the op stack because this is the largest
4039 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004040 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004041 bc_vec_push(&p->ops, &type);
4042 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004043 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004044
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004045 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004046}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004047#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004048
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004049static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004050{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004051 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004052 BcStatus s;
4053 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004054
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004055 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004056 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004057 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004058 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004059 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004060 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004061 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004062 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004064 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004065 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004066 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004067 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004068 }
4069
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004070 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004071}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004072#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004073
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004074static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004075{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004076 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004077 BcStatus s;
4078 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004079
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004080 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004081 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004082 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004083
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004084 t = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004085 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004086 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004087 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004088 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004089 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004090 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004091 xc_parse_push(BC_INST_RET0);
4092 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004093 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004094 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004095
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004096 if (!paren || p->lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004097 s = zbc_POSIX_requires("parentheses around return expressions");
4098 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004099 }
4100
Denys Vlasenko10bde142018-12-27 18:23:58 +01004101 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004102 }
4103
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004104 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004105 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004106}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004107#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004108
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004109static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004110{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004111 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004112 size_t *label = bc_vec_item(&p->func->labels, idx);
4113 *label = p->func->code.len;
4114}
4115
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004116static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004117{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004118 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004119 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004120 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004121
4122 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004123 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004124 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004125 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004126
Denys Vlasenko10bde142018-12-27 18:23:58 +01004127 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004128 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004129 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004130 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004131 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004132
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004133 // Encode "if zero, jump to ..."
4134 // Pushed value (destination of the jump) is uninitialized,
4135 // will be rewritten to be address of "end of if()" or of "else".
4136 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004137 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004138
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004139 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004140 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004141
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004142 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4143 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004144 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004145
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004146 // Encode "after then_stmt, jump to end of if()"
4147 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4148 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004149 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004150
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004151 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 +01004152 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004153
Denys Vlasenko15850832018-12-16 21:40:54 +01004154 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004155
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004156 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004157 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004158 }
4159
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004160 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 +01004161 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004162
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004163 dbg_lex_done("%s:%d done", __func__, __LINE__);
4164 RETURN_STATUS(s);
4165}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004166#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004167
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004168static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004169{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004170 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004171 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004172 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004173 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004174
Denys Vlasenko10bde142018-12-27 18:23:58 +01004175 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004176 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004177 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004178 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004179 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004180
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004181 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004182 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004183 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004184
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004185 bc_vec_push(&p->exits, &ip_idx);
4186 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004187
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004188 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004189 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004190 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004191
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004192 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004193
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004194 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004195 if (s) RETURN_STATUS(s);
4196
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004197 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004198 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004199
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004200 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004201 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004202
4203 bc_vec_pop(&p->exits);
4204 bc_vec_pop(&p->conds);
4205
4206 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004207}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004208#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004209
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004210static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004211{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004212 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004213 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004214 size_t cond_idx, exit_idx, body_idx, update_idx;
4215
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004216 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004217 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004218 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004219 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004220 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004221 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004222
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004223 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004224 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004225 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004226 if (s) RETURN_STATUS(s);
4227 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004228 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4229 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004230 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004231
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004232 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004233 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004234 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004235
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004236 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004237 update_idx = cond_idx + 1;
4238 body_idx = update_idx + 1;
4239 exit_idx = body_idx + 1;
4240
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004241 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004242 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004243 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004244 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004245 // This is safe to set because the current token is a semicolon,
4246 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004247 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004248 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004249 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004250 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004251 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004252
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004253 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004254
Denys Vlasenko10bde142018-12-27 18:23:58 +01004255 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004256 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004257
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004258 bc_parse_pushJUMP_ZERO(exit_idx);
4259 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004260
Gavin Howard01055ba2018-11-03 11:00:21 -06004261 bc_vec_push(&p->conds, &update_idx);
4262 bc_vec_push(&p->func->labels, &p->func->code.len);
4263
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004264 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004265 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004266 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004267 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004268 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004269 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004270 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4271 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004272 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004273
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004274 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004275 bc_vec_push(&p->func->labels, &p->func->code.len);
4276
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004277 bc_vec_push(&p->exits, &exit_idx);
4278 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004279
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004280 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004281 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004282
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004283 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004284 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004285
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004286 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004287 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004288
4289 bc_vec_pop(&p->exits);
4290 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004291
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004292 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004293}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004294#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004295
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004296static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004297{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004298 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004299 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004300
Gavin Howard01055ba2018-11-03 11:00:21 -06004301 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004302 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4303 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004304 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004305 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004306 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004307 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004308 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004309
Denys Vlasenko10bde142018-12-27 18:23:58 +01004310 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004311}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004312#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004313
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004314static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4315{
4316 BcId *autoid;
4317 BcId a;
4318 size_t i;
4319
4320 autoid = (void*)f->autos.v;
4321 for (i = 0; i < f->autos.len; i++, autoid++) {
4322 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004323 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004324 }
4325
4326 a.idx = var;
4327 a.name = name;
4328
4329 bc_vec_push(&f->autos, &a);
4330
4331 RETURN_STATUS(BC_STATUS_SUCCESS);
4332}
4333#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4334
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004335static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004336{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004337 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004338 BcStatus s;
4339 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004340 char *name;
4341
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004342 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004343 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004344 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004345 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004346 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004347
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004348 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004349 p->fidx = bc_program_addFunc(name);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004350 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004351
Denys Vlasenko10bde142018-12-27 18:23:58 +01004352 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004353 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004354 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004355 RETURN_STATUS(bc_error("bad function definition"));
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 while (p->lex != BC_LEX_RPAREN) {
4360 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004361 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004362
4363 ++p->func->nparams;
4364
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004365 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004366 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004367 if (s) goto err;
4368
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004369 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004370
4371 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004372 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004373 if (s) goto err;
4374
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004375 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004376 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004377 goto err;
4378 }
4379
Denys Vlasenko10bde142018-12-27 18:23:58 +01004380 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004381 if (s) goto err;
4382 }
4383
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004384 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004385 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004386 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004387 if (s) goto err;
4388 }
4389
Denys Vlasenko29301232018-12-11 15:29:32 +01004390 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004391 if (s) goto err;
4392 }
4393
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004394 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004395
Denys Vlasenko10bde142018-12-27 18:23:58 +01004396 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004397 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004398
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004399 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004400 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004401 if (s) RETURN_STATUS(s);
4402 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004403
Denys Vlasenko202dd192018-12-16 17:30:35 +01004404 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004405 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004406 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004407 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004408 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004409 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004410
4411 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004412 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004413 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004414 if (s) RETURN_STATUS(s);
4415
Denys Vlasenko10bde142018-12-27 18:23:58 +01004416 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004417
4418 // Subsequent code generation is into main program
4419 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004420 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004421
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004422 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004423 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004424 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004425 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004426 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004427 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004428}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004429#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004430
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004431static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004432{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004433 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004434 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004435 char *name;
4436
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004437 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004438 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004439 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004440
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004441 for (;;) {
4442 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004443
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004444 if (p->lex != XC_LEX_NAME)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004445 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4446
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004447 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004448 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004449 if (s) goto err;
4450
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004451 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004453 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 if (s) goto err;
4455
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004456 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004457 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004458 goto err;
4459 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004460 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004461 if (s) goto err;
4462 }
4463
Denys Vlasenko29301232018-12-11 15:29:32 +01004464 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004465 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004466
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004467 if (p->lex == XC_LEX_NLINE
4468 || p->lex == BC_LEX_SCOLON
4469 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004470 ) {
4471 break;
4472 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004473 if (p->lex != BC_LEX_COMMA)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004474 RETURN_STATUS(bc_error("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004475 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004476 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004477 }
4478
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004479 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004480 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004481 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004482 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004483 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004484 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004485}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004486#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004487
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004488#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004489static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004490{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004491 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004492 BcStatus s = BC_STATUS_SUCCESS;
4493
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004494 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004495
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004496 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004497 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004498 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004499 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004500 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004501 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004502 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004503 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004504
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004505 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004506 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4507 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004508 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004509 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004510 } while (p->lex == XC_LEX_NLINE);
4511 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004512 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004513 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004514 if (s) RETURN_STATUS(s);
4515 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004516 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004517 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004518 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004519 if (s) RETURN_STATUS(s);
4520 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004521 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004522 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4523 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004524 }
4525
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004526 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4527 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004528 case XC_LEX_OP_MINUS:
4529 case BC_LEX_OP_INC:
4530 case BC_LEX_OP_DEC:
4531 case BC_LEX_OP_BOOL_NOT:
4532 case BC_LEX_LPAREN:
4533 case XC_LEX_NAME:
4534 case XC_LEX_NUMBER:
4535 case BC_LEX_KEY_IBASE:
4536 case BC_LEX_KEY_LAST:
4537 case BC_LEX_KEY_LENGTH:
4538 case BC_LEX_KEY_OBASE:
4539 case BC_LEX_KEY_READ:
4540 case BC_LEX_KEY_SCALE:
4541 case BC_LEX_KEY_SQRT:
4542 s = zbc_parse_expr(BC_PARSE_PRINT);
4543 break;
4544 case XC_LEX_STR:
4545 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004546 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004547 break;
4548 case BC_LEX_KEY_BREAK:
4549 case BC_LEX_KEY_CONTINUE:
4550 s = zbc_parse_break_or_continue(p->lex);
4551 break;
4552 case BC_LEX_KEY_FOR:
4553 s = zbc_parse_for();
4554 break;
4555 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004556 xc_parse_push(BC_INST_HALT);
4557 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004558 break;
4559 case BC_LEX_KEY_IF:
4560 s = zbc_parse_if();
4561 break;
4562 case BC_LEX_KEY_LIMITS:
4563 // "limits" is a compile-time command,
4564 // the output is produced at _parse time_.
4565 printf(
4566 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4567 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4568 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4569 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4570 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4571 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4572 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4573 "Number of vars = "BC_MAX_VARS_STR "\n"
4574 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004575 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004576 break;
4577 case BC_LEX_KEY_PRINT:
4578 s = zbc_parse_print();
4579 break;
4580 case BC_LEX_KEY_QUIT:
4581 // "quit" is a compile-time command. For example,
4582 // "if (0 == 1) quit" terminates when parsing the statement,
4583 // not when it is executed
4584 QUIT_OR_RETURN_TO_MAIN;
4585 case BC_LEX_KEY_RETURN:
4586 if (!p->in_funcdef)
4587 RETURN_STATUS(bc_error("'return' not in a function"));
4588 s = zbc_parse_return();
4589 break;
4590 case BC_LEX_KEY_WHILE:
4591 s = zbc_parse_while();
4592 break;
4593 default:
4594 s = bc_error_bad_token();
4595 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004596 }
4597
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004598 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004599 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004600}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004601#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004602
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004603static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004604{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004605 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004606 BcStatus s;
4607
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004608 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004609 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004610 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004611 else if (p->lex == BC_LEX_KEY_DEFINE) {
4612 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004613 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004614 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004615 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004616 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004617 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004618
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004619 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004620 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004621}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004622#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004623
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004624// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004625static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004626{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004627 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004628 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004629 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004630 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004631 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004632
Denys Vlasenko17df8822018-12-14 23:00:24 +01004633 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004634 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004635 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004636 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004637 bin_last = true;
4638
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004639 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004640 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004641 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004642 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004643
4644 if (!lex_allowed_in_bc_expr(t))
4645 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004646
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004647 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004648 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004649 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004650 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004651 case BC_LEX_OP_INC:
4652 case BC_LEX_OP_DEC:
4653 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
4654 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
4655 rprn = bin_last = false;
4656 //get_token = false; - already is
4657 break;
4658 case XC_LEX_OP_MINUS:
4659 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
4660 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
4661 rprn = false;
4662 //get_token = false; - already is
4663 bin_last = (prev == XC_INST_MINUS);
4664 break;
4665 case BC_LEX_OP_ASSIGN_POWER:
4666 case BC_LEX_OP_ASSIGN_MULTIPLY:
4667 case BC_LEX_OP_ASSIGN_DIVIDE:
4668 case BC_LEX_OP_ASSIGN_MODULUS:
4669 case BC_LEX_OP_ASSIGN_PLUS:
4670 case BC_LEX_OP_ASSIGN_MINUS:
4671 case BC_LEX_OP_ASSIGN:
4672 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4673 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4674 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4675 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4676 ) {
4677 return bc_error("bad assignment:"
4678 " left side must be variable"
4679 " or array element"
4680 ); // note: shared string
4681 }
4682 // Fallthrough.
4683 case XC_LEX_OP_POWER:
4684 case XC_LEX_OP_MULTIPLY:
4685 case XC_LEX_OP_DIVIDE:
4686 case XC_LEX_OP_MODULUS:
4687 case XC_LEX_OP_PLUS:
4688 case XC_LEX_OP_REL_EQ:
4689 case XC_LEX_OP_REL_LE:
4690 case XC_LEX_OP_REL_GE:
4691 case XC_LEX_OP_REL_NE:
4692 case XC_LEX_OP_REL_LT:
4693 case XC_LEX_OP_REL_GT:
4694 case BC_LEX_OP_BOOL_NOT:
4695 case BC_LEX_OP_BOOL_OR:
4696 case BC_LEX_OP_BOOL_AND:
4697 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
4698 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4699 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
4700 ) {
4701 return bc_error_bad_expression();
4702 }
4703 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4704 prev = BC_TOKEN_2_INST(t);
4705 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004706 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004707 rprn = false;
4708 //get_token = false; - already is
4709 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4710 break;
4711 case BC_LEX_LPAREN:
4712 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
4713 if (BC_PARSE_LEAF(prev, rprn))
4714 return bc_error_bad_expression();
4715 bc_vec_push(&p->ops, &t);
4716 nparens++;
4717 get_token = true;
4718 paren_expr = false;
4719 rprn = bin_last = false;
4720 break;
4721 case BC_LEX_RPAREN:
4722 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
4723 if (bin_last || prev == XC_INST_BOOL_NOT)
4724 return bc_error_bad_expression();
4725 if (nparens == 0) {
4726 goto exit_loop;
4727 }
4728 if (!paren_expr) {
4729 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
4730 return BC_STATUS_PARSE_EMPTY_EXP;
4731 }
4732 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4733 nparens--;
4734 get_token = true;
4735 paren_expr = rprn = true;
4736 bin_last = false;
4737 break;
4738 case XC_LEX_NAME:
4739 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
4740 if (BC_PARSE_LEAF(prev, rprn))
4741 return bc_error_bad_expression();
4742 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
4743 paren_expr = true;
4744 rprn = bin_last = false;
4745 //get_token = false; - already is
4746 nexprs++;
4747 break;
4748 case XC_LEX_NUMBER:
4749 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4750 if (BC_PARSE_LEAF(prev, rprn))
4751 return bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004752 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004753 prev = XC_INST_NUM;
4754 get_token = true;
4755 paren_expr = true;
4756 rprn = bin_last = false;
4757 nexprs++;
4758 break;
4759 case BC_LEX_KEY_IBASE:
4760 case BC_LEX_KEY_LAST:
4761 case BC_LEX_KEY_OBASE:
4762 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
4763 if (BC_PARSE_LEAF(prev, rprn))
4764 return bc_error_bad_expression();
4765 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004766 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004767 get_token = true;
4768 paren_expr = true;
4769 rprn = bin_last = false;
4770 nexprs++;
4771 break;
4772 case BC_LEX_KEY_LENGTH:
4773 case BC_LEX_KEY_SQRT:
4774 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
4775 if (BC_PARSE_LEAF(prev, rprn))
4776 return bc_error_bad_expression();
4777 s = zbc_parse_builtin(t, flags, &prev);
4778 get_token = true;
4779 paren_expr = true;
4780 rprn = bin_last = false;
4781 nexprs++;
4782 break;
4783 case BC_LEX_KEY_READ:
4784 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
4785 if (BC_PARSE_LEAF(prev, rprn))
4786 return bc_error_bad_expression();
4787 s = zbc_parse_read();
4788 prev = XC_INST_READ;
4789 get_token = true;
4790 paren_expr = true;
4791 rprn = bin_last = false;
4792 nexprs++;
4793 break;
4794 case BC_LEX_KEY_SCALE:
4795 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
4796 if (BC_PARSE_LEAF(prev, rprn))
4797 return bc_error_bad_expression();
4798 s = zbc_parse_scale(&prev, flags);
4799 prev = XC_INST_SCALE;
4800 //get_token = false; - already is
4801 paren_expr = true;
4802 rprn = bin_last = false;
4803 nexprs++;
4804 break;
4805 default:
4806 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004807 }
4808
Denys Vlasenkobb116032018-12-25 01:16:37 +01004809 if (s || G_interrupt) // error, or ^C: stop parsing
4810 return BC_STATUS_FAILURE;
4811 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004812 s = zxc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004813 if (s) return s;
4814 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004815 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004816 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004817
4818 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004819 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004820 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004821
4822 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004823 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004824
Denys Vlasenko10bde142018-12-27 18:23:58 +01004825 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004826
Denys Vlasenko69560f42018-12-24 14:14:23 +01004827 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004828 bc_vec_pop(&p->ops);
4829 }
4830
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004831 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004832 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004833
Gavin Howard01055ba2018-11-03 11:00:21 -06004834 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004835 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004836 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4837 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004838 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004839 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004840 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4841 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004842 }
4843
4844 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004845 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004846 xc_parse_push(XC_INST_PRINT);
4847 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004848 }
4849
Denys Vlasenko17df8822018-12-14 23:00:24 +01004850 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004851 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004852}
4853
Gavin Howard01055ba2018-11-03 11:00:21 -06004854#endif // ENABLE_BC
4855
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004856#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004857
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004858static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004859{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004860 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004861 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004862
Denys Vlasenko10bde142018-12-27 18:23:58 +01004863 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004864 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004865 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004866
Denys Vlasenko10bde142018-12-27 18:23:58 +01004867 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004868
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004869 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004870}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004871#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004872
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004873static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004874{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004875 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004876 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004877 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004878
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004879 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004880
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004881 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004882 xc_parse_push(XC_INST_STR);
4883 xc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004884 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004885
4886 // Explanation needed here
Denys Vlasenko10bde142018-12-27 18:23:58 +01004887 xc_program_add_fn();
4888 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004889
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004890 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004891}
4892
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004893static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004894{
4895 BcStatus s;
4896
Denys Vlasenko10bde142018-12-27 18:23:58 +01004897 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004898 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004899 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004900 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004901 }
4902
4903 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004904 xc_parse_push(DC_INST_SWAP);
4905 xc_parse_push(XC_INST_ASSIGN);
4906 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004907 }
4908
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004909 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004910}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004911#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004912
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004913static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004914{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004915 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004916 BcStatus s;
4917
Denys Vlasenko10bde142018-12-27 18:23:58 +01004918 xc_parse_push(inst);
4919 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004920
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004921 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004922 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004923
Denys Vlasenko10bde142018-12-27 18:23:58 +01004924 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004925 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004926
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004927 // Note that 'else' part can not be on the next line:
4928 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4929 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004930 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004931 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004932 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004933 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004934 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004935 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004936 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004937
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004938 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004939}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004940#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004941
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004942static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004943{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004944 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004945 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004946 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004947
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004948 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004949 s = BC_STATUS_SUCCESS;
4950 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004951 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004952 case XC_LEX_OP_REL_EQ:
4953 case XC_LEX_OP_REL_LE:
4954 case XC_LEX_OP_REL_GE:
4955 case XC_LEX_OP_REL_NE:
4956 case XC_LEX_OP_REL_LT:
4957 case XC_LEX_OP_REL_GT:
4958 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
4959 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
4960 get_token = false;
4961 break;
4962 case DC_LEX_SCOLON:
4963 case DC_LEX_COLON:
4964 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
4965 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
4966 break;
4967 case XC_LEX_STR:
4968 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
4969 dc_parse_string();
4970 break;
4971 case XC_LEX_NEG:
4972 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004973 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004974 if (s) RETURN_STATUS(s);
4975 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004976 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004977 xc_parse_pushNUM();
4978 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004979 break;
4980 case XC_LEX_NUMBER:
4981 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004982 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004983 break;
4984 case DC_LEX_READ:
4985 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004986 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004987 break;
4988 case DC_LEX_OP_ASSIGN:
4989 case DC_LEX_STORE_PUSH:
4990 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
4991 assign = (t == DC_LEX_OP_ASSIGN);
4992 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
4993 s = zdc_parse_mem(inst, true, assign);
4994 break;
4995 case DC_LEX_LOAD:
4996 case DC_LEX_LOAD_POP:
4997 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
4998 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
4999 s = zdc_parse_mem(inst, true, false);
5000 break;
5001 case DC_LEX_STORE_IBASE:
5002 case DC_LEX_STORE_SCALE:
5003 case DC_LEX_STORE_OBASE:
5004 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5005 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5006 s = zdc_parse_mem(inst, false, true);
5007 break;
5008 default:
5009 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5010 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005011 }
5012
Denys Vlasenko10bde142018-12-27 18:23:58 +01005013 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005014
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005015 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005016 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005017}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005018#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005019
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005020static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005021{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005022 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005023 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005024
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005025 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005026 if (i >= 0) {
5027 BcInst inst = dc_LEX_to_INST[i];
5028 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005029 xc_parse_push(inst);
5030 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005031 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005032 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005033 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005034}
5035#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5036
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005037static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005038{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005039 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005040 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5041 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005042 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005043 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005044 }
5045
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005046 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005047 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005048}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005049#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005050
Gavin Howard01055ba2018-11-03 11:00:21 -06005051#endif // ENABLE_DC
5052
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005053//
5054// Execution engine
5055//
5056
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005057#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5058#define BC_PROG_NUM(r, n) \
5059 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5060
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005061#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5062#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5063
Denys Vlasenko10bde142018-12-27 18:23:58 +01005064static BcVec* xc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005065{
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 BcId e, *ptr;
5067 BcVec *v, *map;
5068 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005069 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005070
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005071 v = var ? &G.prog.vars : &G.prog.arrs;
5072 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005073
5074 e.name = id;
5075 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005076 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005077
5078 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005079 BcVec v2;
5080 bc_array_init(&v2, var);
5081 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 }
5083
5084 ptr = bc_vec_item(map, i);
5085 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005086 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005087}
5088
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005089// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005090static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005091{
Gavin Howard01055ba2018-11-03 11:00:21 -06005092 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005093 case XC_RESULT_STR:
5094 case XC_RESULT_TEMP:
5095 case XC_RESULT_IBASE:
5096 case XC_RESULT_SCALE:
5097 case XC_RESULT_OBASE:
5098 *num = &r->d.n;
5099 break;
5100 case XC_RESULT_CONSTANT: {
5101 BcStatus s;
5102 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005103 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005104
Denys Vlasenko10bde142018-12-27 18:23:58 +01005105 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005106 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005107
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005108 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005109
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005110 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005111 if (s) {
5112 bc_num_free(&r->d.n);
5113 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005114 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005115 *num = &r->d.n;
5116 r->t = XC_RESULT_TEMP;
5117 break;
5118 }
5119 case XC_RESULT_VAR:
5120 case XC_RESULT_ARRAY:
5121 case XC_RESULT_ARRAY_ELEM: {
5122 BcVec *v;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005123 void *p;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005124 v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005125// dc variables are all stacks, so here we have this:
5126 p = bc_vec_top(v);
5127// TODO: eliminate these stacks for bc-only config?
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005128 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005129 v = p;
5130 if (v->len <= r->d.id.idx)
5131 bc_array_expand(v, r->d.id.idx + 1);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005132 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005133 } else {
5134 *num = p;
5135 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005136 break;
5137 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005138#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005139 case BC_RESULT_LAST:
5140 *num = &G.prog.last;
5141 break;
5142 case BC_RESULT_ONE:
5143 *num = &G.prog.one;
5144 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005145#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005146#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005147 default:
5148 // Testing the theory that dc does not reach LAST/ONE
5149 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005150#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005151 }
5152
Denys Vlasenko29301232018-12-11 15:29:32 +01005153 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005154}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005155#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005156
Denys Vlasenko10bde142018-12-27 18:23:58 +01005157static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005158 BcResult **r, BcNum **rn, bool assign)
5159{
5160 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005161 BcResultType lt, rt;
5162
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005163 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005164 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005165
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005166 *r = bc_vec_item_rev(&G.prog.results, 0);
5167 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005168
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005169 s = zxc_program_num(*l, ln);
5170 if (s) RETURN_STATUS(s);
5171 s = zxc_program_num(*r, rn);
5172 if (s) RETURN_STATUS(s);
5173
Gavin Howard01055ba2018-11-03 11:00:21 -06005174 lt = (*l)->t;
5175 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005176
5177 // We run this again under these conditions in case any vector has been
5178 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005179 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005180 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005181 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005182 }
5183
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005184 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005185 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005186 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005187 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005188
Denys Vlasenko29301232018-12-11 15:29:32 +01005189 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005190}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005191#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005192
Denys Vlasenko10bde142018-12-27 18:23:58 +01005193static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005194{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005195 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005196 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005197 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005198}
5199
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005200// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005201static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005202{
5203 BcStatus s;
5204
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005205 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005206 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005207 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005208
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005209 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005210 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005211
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005212 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005213 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005214
Denys Vlasenko29301232018-12-11 15:29:32 +01005215 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005216}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005217#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005218
Denys Vlasenko10bde142018-12-27 18:23:58 +01005219static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005220{
5221 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005222 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005223}
5224
Denys Vlasenko10bde142018-12-27 18:23:58 +01005225static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005226{
5227 BcStatus s;
5228 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005229 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005230
Denys Vlasenko10bde142018-12-27 18:23:58 +01005231 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005232 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005233 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005234
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005235 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005236 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 -06005237 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005238 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005239
Denys Vlasenko259137d2018-12-11 19:42:05 +01005240 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005241 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005242 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005243 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005244}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005245#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005246
Denys Vlasenko10bde142018-12-27 18:23:58 +01005247static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005248{
5249 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005250 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005251 BcVec buf;
5252 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005253 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005254
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005255 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005256 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005257
Denys Vlasenko10bde142018-12-27 18:23:58 +01005258 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005259 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005260
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005261 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005262 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005263 //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 +01005264
Denys Vlasenko10bde142018-12-27 18:23:58 +01005265 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005266 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005267 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005268 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005269 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005270 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005271 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005272 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005273 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005274 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005275 goto exec_err;
5276 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005277 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005278
5279 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005280 ip.inst_idx = 0;
5281 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005282 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005283
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005284 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005285 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005286 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005287 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005288 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005289}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005290#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005291
Denys Vlasenko10bde142018-12-27 18:23:58 +01005292static size_t xc_program_index(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005293{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005294 unsigned char *bytes = (void*)(code + *bgn);
5295 unsigned amt;
5296 unsigned i;
5297 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005298
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005299 amt = *bytes++;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01005300 if (amt < SMALL_INDEX_LIMIT) {
5301 *bgn += 1;
5302 return amt;
5303 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005304 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005305 *bgn += amt + 1;
5306
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005307 res = 0;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005308 i = 0;
5309 do {
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005310 res |= (size_t)(*bytes++) << i;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005311 i += 8;
5312 } while (--amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005313
5314 return res;
5315}
5316
Denys Vlasenko10bde142018-12-27 18:23:58 +01005317static char *xc_program_name(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005318{
Denys Vlasenko694d2982018-12-18 19:17:11 +01005319 code += *bgn;
Denys Vlasenkof706a182018-12-26 20:02:09 +01005320 *bgn += strlen(code) + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005321
Denys Vlasenkof706a182018-12-26 20:02:09 +01005322 return xstrdup(code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005323}
5324
Denys Vlasenko10bde142018-12-27 18:23:58 +01005325static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005326{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005327#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005328 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005329 // Example: echo '[]ap' | dc
5330 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005331 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005332 return;
5333 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005334#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005335 while (*str) {
5336 int c = *str++;
5337 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005338 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005339 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005340 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005341 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005342 case 'a':
5343 bb_putchar('\a');
5344 break;
5345 case 'b':
5346 bb_putchar('\b');
5347 break;
5348 case '\\':
5349 case 'e':
5350 bb_putchar('\\');
5351 break;
5352 case 'f':
5353 bb_putchar('\f');
5354 break;
5355 case 'n':
5356 bb_putchar('\n');
5357 G.prog.nchars = SIZE_MAX;
5358 break;
5359 case 'r':
5360 bb_putchar('\r');
5361 break;
5362 case 'q':
5363 bb_putchar('"');
5364 break;
5365 case 't':
5366 bb_putchar('\t');
5367 break;
5368 default:
5369 // Just print the backslash and following character.
5370 bb_putchar('\\');
5371 ++G.prog.nchars;
5372 bb_putchar(c);
5373 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005374 }
5375 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005376 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005377 }
5378}
5379
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005380static void bc_num_printNewline(void)
5381{
5382 if (G.prog.nchars == G.prog.len - 1) {
5383 bb_putchar('\\');
5384 bb_putchar('\n');
5385 G.prog.nchars = 0;
5386 }
5387}
5388
5389#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005390static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005391{
5392 (void) radix;
5393 bb_putchar((char) num);
5394 G.prog.nchars += width;
5395}
5396#endif
5397
5398static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5399{
5400 size_t exp, pow;
5401
5402 bc_num_printNewline();
5403 bb_putchar(radix ? '.' : ' ');
5404 ++G.prog.nchars;
5405
5406 bc_num_printNewline();
5407 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5408 continue;
5409
5410 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5411 size_t dig;
5412 bc_num_printNewline();
5413 dig = num / pow;
5414 num -= dig * pow;
5415 bb_putchar(((char) dig) + '0');
5416 }
5417}
5418
5419static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5420{
5421 if (radix) {
5422 bc_num_printNewline();
5423 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005424 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005425 }
5426
5427 bc_num_printNewline();
5428 bb_putchar(bb_hexdigits_upcase[num]);
5429 G.prog.nchars += width;
5430}
5431
5432static void bc_num_printDecimal(BcNum *n)
5433{
5434 size_t i, rdx = n->rdx - 1;
5435
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005436 if (n->neg) {
5437 bb_putchar('-');
5438 G.prog.nchars++;
5439 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005440
5441 for (i = n->len - 1; i < n->len; --i)
5442 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5443}
5444
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005445typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5446
Denys Vlasenko10bde142018-12-27 18:23:58 +01005447static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005448{
5449 BcStatus s;
5450 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005451 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005452 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005453 BcNum intp, fracp, digit, frac_len;
5454 unsigned long dig, *ptr;
5455 size_t i;
5456 bool radix;
5457
5458 if (n->len == 0) {
5459 print(0, width, false);
5460 RETURN_STATUS(BC_STATUS_SUCCESS);
5461 }
5462
5463 bc_vec_init(&stack, sizeof(long), NULL);
5464 bc_num_init(&intp, n->len);
5465 bc_num_init(&fracp, n->rdx);
5466 bc_num_init(&digit, width);
5467 bc_num_init(&frac_len, BC_NUM_INT(n));
5468 bc_num_copy(&intp, n);
5469 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005470 base.cap = ARRAY_SIZE(base_digs);
5471 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005472 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005473
5474 bc_num_truncate(&intp, intp.rdx);
5475 s = zbc_num_sub(n, &intp, &fracp, 0);
5476 if (s) goto err;
5477
5478 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005479 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005480 if (s) goto err;
5481 s = zbc_num_ulong(&digit, &dig);
5482 if (s) goto err;
5483 bc_vec_push(&stack, &dig);
5484 }
5485
5486 for (i = 0; i < stack.len; ++i) {
5487 ptr = bc_vec_item_rev(&stack, i);
5488 print(*ptr, width, false);
5489 }
5490
5491 if (!n->rdx) goto err;
5492
5493 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005494 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005495 if (s) goto err;
5496 s = zbc_num_ulong(&fracp, &dig);
5497 if (s) goto err;
5498 bc_num_ulong2num(&intp, dig);
5499 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5500 if (s) goto err;
5501 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005502 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005503 if (s) goto err;
5504 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005505 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005506 bc_num_free(&frac_len);
5507 bc_num_free(&digit);
5508 bc_num_free(&fracp);
5509 bc_num_free(&intp);
5510 bc_vec_free(&stack);
5511 RETURN_STATUS(s);
5512}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005513#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005514
Denys Vlasenko10bde142018-12-27 18:23:58 +01005515static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005516{
5517 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005518 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005519 BcNumDigitOp print;
5520 bool neg = n->neg;
5521
5522 if (neg) {
5523 bb_putchar('-');
5524 G.prog.nchars++;
5525 }
5526
5527 n->neg = false;
5528
5529 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5530 width = 1;
5531 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005532 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005533 unsigned i = G.prog.ob_t - 1;
5534 width = 0;
5535 for (;;) {
5536 width++;
5537 i /= 10;
5538 if (i == 0)
5539 break;
5540 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005541 print = bc_num_printDigits;
5542 }
5543
Denys Vlasenko10bde142018-12-27 18:23:58 +01005544 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005545 n->neg = neg;
5546
5547 RETURN_STATUS(s);
5548}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005549#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005550
Denys Vlasenko10bde142018-12-27 18:23:58 +01005551static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005552{
5553 BcStatus s = BC_STATUS_SUCCESS;
5554
5555 bc_num_printNewline();
5556
5557 if (n->len == 0) {
5558 bb_putchar('0');
5559 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005560 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005561 bc_num_printDecimal(n);
5562 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005563 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005564
5565 if (newline) {
5566 bb_putchar('\n');
5567 G.prog.nchars = 0;
5568 }
5569
5570 RETURN_STATUS(s);
5571}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005572#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005573
Denys Vlasenko10bde142018-12-27 18:23:58 +01005574static BC_STATUS zxc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005575{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005576 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005577 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005578 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005579 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005580
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005581 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005582 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005583
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005584 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005585 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005586 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005587
5588 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005589 s = zxc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005590#if ENABLE_BC
5591 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5592#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005593 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005594 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005595
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005596 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005597 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005598
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005599 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005600 for (;;) {
5601 char c = *str++;
5602 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005603 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005604 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005605 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005606 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005607 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005608 xc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005609 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005610 }
5611 }
5612
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005613 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005614
Denys Vlasenko29301232018-12-11 15:29:32 +01005615 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005616}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005617#define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005618
Denys Vlasenko10bde142018-12-27 18:23:58 +01005619static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005620{
5621 BcStatus s;
5622 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005623 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005624
Denys Vlasenko10bde142018-12-27 18:23:58 +01005625 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005626 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005627
5628 bc_num_init(&res.d.n, num->len);
5629 bc_num_copy(&res.d.n, num);
5630 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5631
Denys Vlasenko10bde142018-12-27 18:23:58 +01005632 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005633
Denys Vlasenko29301232018-12-11 15:29:32 +01005634 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005635}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005636#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005637
Denys Vlasenko10bde142018-12-27 18:23:58 +01005638static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005639{
5640 BcStatus s;
5641 BcResult *opd1, *opd2, res;
5642 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005643 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005644
Denys Vlasenko10bde142018-12-27 18:23:58 +01005645 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005646 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005647
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005648 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005649
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005650 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005651 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005652 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005653 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005654 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005655 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005656 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005657 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005658 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005659 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005660 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005661 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005662 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005663 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005664 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005665 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005666 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005667 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005668 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005669 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005670 cond = (cond > 0);
5671 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005672 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005673 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005674 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005675 }
5676 }
5677
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005678 if (cond) bc_num_one(&res.d.n);
5679 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005680
Denys Vlasenko10bde142018-12-27 18:23:58 +01005681 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005682
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005683 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005684}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005685#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005686
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005687#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005688static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005689{
5690 BcNum n2;
5691 BcResult res;
5692
5693 memset(&n2, 0, sizeof(BcNum));
5694 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005695 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005696
5697 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005698 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005699 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005700 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005701 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005702 }
5703
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005704 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005705 bc_vec_push(v, &n2);
5706
Denys Vlasenko29301232018-12-11 15:29:32 +01005707 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005708}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005709#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005710#endif // ENABLE_DC
5711
Denys Vlasenko10bde142018-12-27 18:23:58 +01005712static BC_STATUS zxc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005713{
5714 BcStatus s;
5715 BcResult *ptr, r;
5716 BcVec *v;
5717 BcNum *n;
5718
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005719 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005720 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005721
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005722 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005723 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005724 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005725 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005727#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005728 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005729 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005730 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005731 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005732#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005733
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005734 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005735 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005736
5737 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko10bde142018-12-27 18:23:58 +01005738 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005739
5740 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005741 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005742 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005743 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005744 bc_array_init(&r.d.v, true);
5745 bc_array_copy(&r.d.v, (BcVec *) n);
5746 }
5747
5748 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005749 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005750
Denys Vlasenko29301232018-12-11 15:29:32 +01005751 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005752}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005753#define zxc_program_copyToVar(...) (zxc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005754
Denys Vlasenko10bde142018-12-27 18:23:58 +01005755static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005756{
5757 BcStatus s;
5758 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005759 BcNum *l, *r;
5760 bool assign = (inst == XC_INST_ASSIGN);
5761 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005762
Denys Vlasenko10bde142018-12-27 18:23:58 +01005763 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005764 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005765
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005766 ib = left->t == XC_RESULT_IBASE;
5767 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005768
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005769#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005770 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005771 BcVec *v;
5772
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005773 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005774 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005775 v = xc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005776
Denys Vlasenkofa210792018-12-19 19:35:40 +01005777 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005778 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005779#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005780
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005781 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005782 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005783 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005784 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005785 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005786
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005787#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005788 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005789 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005790
5791 if (assign)
5792 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005793 else {
5794 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005795 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 +01005796 }
5797 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005798#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005799 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005800#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005801
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005802 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005803 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005804 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5805 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5806 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005807 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005808 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005809 size_t max;
5810 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005811
Denys Vlasenko29301232018-12-11 15:29:32 +01005812 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005813 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005814 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005815 if (sc) {
5816 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005817 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005818 } else {
5819 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005820 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005821 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005822 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005823 }
5824
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005825 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005826 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
5828 *ptr = (size_t) val;
5829 s = BC_STATUS_SUCCESS;
5830 }
5831
5832 bc_num_init(&res.d.n, l->len);
5833 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005834 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005835
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005836 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005837}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005838#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005839
Denys Vlasenko416ce762018-12-02 20:57:17 +01005840#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005841#define xc_program_pushVar(code, bgn, pop, copy) \
5842 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005843// for bc, 'pop' and 'copy' are always false
5844#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005845static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005846 bool pop, bool copy)
5847{
Gavin Howard01055ba2018-11-03 11:00:21 -06005848 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005849 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005850
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005851 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005852 r.d.id.name = name;
5853
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005854#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005855 if (pop || copy) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005856 BcVec *v = xc_program_search(name, true);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005857 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005858
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005859 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005860 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005861 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005862 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005863
5864 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005865 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005866 bc_num_init_DEF_SIZE(&r.d.n);
5867 bc_num_copy(&r.d.n, num);
5868 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005869 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005870 r.d.id.idx = num->rdx;
5871 }
5872
5873 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005874 }
5875#endif // ENABLE_DC
5876
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005877 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005878
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005879 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005880}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005881#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005882
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005883static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005884{
5885 BcStatus s = BC_STATUS_SUCCESS;
5886 BcResult r;
5887 BcNum *num;
5888
Denys Vlasenko10bde142018-12-27 18:23:58 +01005889 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005890
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005891 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005892 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005893 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005894 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005895 BcResult *operand;
5896 unsigned long temp;
5897
Denys Vlasenko10bde142018-12-27 18:23:58 +01005898 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005899 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005900 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005901 if (s) goto err;
5902
5903 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005904 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005905 goto err;
5906 }
5907
5908 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005909 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005910 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005911 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005912 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005913 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005915#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005916
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005917#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005918static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005919{
5920 BcStatus s;
5921 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005922 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005923 char inst2 = inst;
5924
Denys Vlasenko10bde142018-12-27 18:23:58 +01005925 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005926 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005927
5928 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005929 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005930 bc_num_init(&copy.d.n, num->len);
5931 bc_num_copy(&copy.d.n, num);
5932 }
5933
5934 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005935 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5936 ? BC_INST_ASSIGN_PLUS
5937 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005938
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005939 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005940 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005941 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005942
5943 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005944 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005945 }
5946
Denys Vlasenko29301232018-12-11 15:29:32 +01005947 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005948}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005949#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005950
Denys Vlasenko29301232018-12-11 15:29:32 +01005951static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005952{
Gavin Howard01055ba2018-11-03 11:00:21 -06005953 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005954 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005955 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005956 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005957 BcResult *arg;
5958
Denys Vlasenko10bde142018-12-27 18:23:58 +01005959 nparams = xc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005960 ip.inst_idx = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005961 ip.func = xc_program_index(code, idx);
5962 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005963
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005964 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005965 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005966 }
5967 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005968 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005969 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005970 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005971
5972 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005973 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005974
5975 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005976 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005977
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005978 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005979 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005980
Denys Vlasenko10bde142018-12-27 18:23:58 +01005981 s = zxc_program_copyToVar(a->name, a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005982 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005983 }
5984
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005985 a = bc_vec_item(&func->autos, i);
5986 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005987 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005988
Denys Vlasenko10bde142018-12-27 18:23:58 +01005989 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005990 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005991 BcNum n2;
5992 bc_num_init_DEF_SIZE(&n2);
5993 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005994 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005995 BcVec v2;
5996 bc_array_init(&v2, true);
5997 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005998 }
5999 }
6000
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006001 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006002
Denys Vlasenko29301232018-12-11 15:29:32 +01006003 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006004}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006005#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006006
Denys Vlasenko29301232018-12-11 15:29:32 +01006007static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006008{
Gavin Howard01055ba2018-11-03 11:00:21 -06006009 BcResult res;
6010 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006011 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006012 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006013 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006014
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006015 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 +01006016 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006017
Denys Vlasenko10bde142018-12-27 18:23:58 +01006018 f = xc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006019 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006020
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006021 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006022 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006023 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006024 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006025
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006026 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006027 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006028 bc_num_init(&res.d.n, num->len);
6029 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006030 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006031 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006032 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006033 }
6034
6035 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006036 a = (void*)f->autos.v;
6037 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006038 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006039 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006040 bc_vec_pop(v);
6041 }
6042
Denys Vlasenko24e41942018-12-21 23:01:26 +01006043 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006044 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006045 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006046
Denys Vlasenko29301232018-12-11 15:29:32 +01006047 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006048}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006049#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006050#endif // ENABLE_BC
6051
Denys Vlasenko10bde142018-12-27 18:23:58 +01006052static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006053{
6054 return (unsigned long) n->rdx;
6055}
6056
Denys Vlasenko10bde142018-12-27 18:23:58 +01006057static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006058{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006059 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006060
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006061 if (n->rdx != len) return len;
6062 for (;;) {
6063 if (len == 0) break;
6064 len--;
6065 if (n->num[len] != 0) break;
6066 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006067 return len;
6068}
6069
Denys Vlasenko10bde142018-12-27 18:23:58 +01006070static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006071{
6072 BcStatus s;
6073 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006074 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006075 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006076 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006077
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006078 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006079 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006080 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006081
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006082 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006083 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006085#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006086 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006087 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006088#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006089
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006090 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006091
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006092 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006093 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006094#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006095 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006096 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006098#endif
6099#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006100 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006102 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006103
Denys Vlasenko10bde142018-12-27 18:23:58 +01006104 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006105 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006106 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006107#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006108 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006109 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006110 }
6111
Denys Vlasenko10bde142018-12-27 18:23:58 +01006112 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006114 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006115}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006116#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006117
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006118#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006119static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006120{
6121 BcStatus s;
6122 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006123 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006124
Denys Vlasenko10bde142018-12-27 18:23:58 +01006125 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006126 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006127
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006128 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006129 bc_num_init(&res2.d.n, n2->len);
6130
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006131 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 if (s) goto err;
6133
Denys Vlasenko10bde142018-12-27 18:23:58 +01006134 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006135 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006136 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006137
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006138 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006139 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006140 bc_num_free(&res2.d.n);
6141 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_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006145
Denys Vlasenkofa210792018-12-19 19:35:40 +01006146static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006147{
6148 BcStatus s;
6149 BcResult *r1, *r2, *r3, res;
6150 BcNum *n1, *n2, *n3;
6151
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006152 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006153 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006154 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006155 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006156
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006157 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006158 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006159 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006160 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006161 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006162
6163 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006164 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006166 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006167 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006168 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006169 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006170 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006171 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006172 }
6173 }
6174
6175 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006176 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006177 if (s) goto err;
6178
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006179 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006180 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006182 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006183 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006184 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006185 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006186}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006187#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006188
Denys Vlasenkofa210792018-12-19 19:35:40 +01006189static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006190{
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006192 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006193
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006194 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006195
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006196 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006197 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006198 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006199}
6200
Denys Vlasenkofa210792018-12-19 19:35:40 +01006201static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006202{
6203 BcStatus s;
6204 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006205 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006206 char **strs;
6207 char *str;
6208 char c;
6209 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006210
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006211 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006212 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006213
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006214 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006215 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006216 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006217
6218 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006219 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006220 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006221 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006222
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006223 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006224 bc_num_copy(&n, num);
6225 bc_num_truncate(&n, n.rdx);
6226
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006227 strmb.cap = ARRAY_SIZE(strmb_digs);
6228 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006229 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006230
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006231 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006232 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006233 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006234 if (s) goto num_err;
6235
6236 c = (char) val;
6237
6238 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006239 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006240 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006241 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006242 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006243 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006244 }
6245
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006246 strs = (void*)G.prog.strs.v;
6247 for (idx = 0; idx < G.prog.strs.len; idx++) {
6248 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6249 goto dup;
6250 }
6251 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006252 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006253 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006254 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006255 bc_vec_push(&G.prog.strs, &str);
6256 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006257 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006258 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006259 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006260
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006261 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006262 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006263 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006264 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006266#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006267
Denys Vlasenkofa210792018-12-19 19:35:40 +01006268static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006269{
6270 BcStatus s;
6271 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006272 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006273 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006274
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006275 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006276 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006277 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006278
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006279 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006280 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006281
Denys Vlasenko57734c92018-12-17 21:14:05 +01006282 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006283 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006284 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006285 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006286 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006287 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006288 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 }
6290
Denys Vlasenko29301232018-12-11 15:29:32 +01006291 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006292}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006293#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
Denys Vlasenkofa210792018-12-19 19:35:40 +01006295static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006296{
6297 BcStatus s;
6298 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006299 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006300 unsigned long val;
6301
Denys Vlasenko10bde142018-12-27 18:23:58 +01006302 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006303 if (s) RETURN_STATUS(s);
6304 s = zbc_num_ulong(num, &val);
6305 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006306
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006307 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006308
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006309 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006310 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006311 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006312 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006313 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006314
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006315 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006316
Denys Vlasenko29301232018-12-11 15:29:32 +01006317 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006319#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006320
Denys Vlasenkofa210792018-12-19 19:35:40 +01006321static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006322{
6323 BcStatus s = BC_STATUS_SUCCESS;
6324 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006325 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006326 BcInstPtr ip;
6327 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006328
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006329 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006330 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006332 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
6334 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006335 BcNum *n = n; // for compiler
6336 bool exec;
6337 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006338 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006339 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006340
Denys Vlasenkof706a182018-12-26 20:02:09 +01006341 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006342 (*bgn) += 1;
6343 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006344 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006345
6346 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006347 name = then_name;
6348 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006349 exec = true;
6350 name = else_name;
6351 }
6352
6353 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006354 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006355 v = xc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 n = bc_vec_top(v);
6357 }
6358
6359 free(then_name);
6360 free(else_name);
6361
6362 if (!exec) goto exit;
6363 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006364 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006365 goto exit;
6366 }
6367
6368 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006369 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006370 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006371 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006372 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006373 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006374 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006375 if (s || !BC_PROG_STR(n)) goto exit;
6376 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006377 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006378 goto exit;
6379 }
6380
6381 fidx = sidx + BC_PROG_REQ_FUNCS;
6382
Denys Vlasenko10bde142018-12-27 18:23:58 +01006383 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006384
6385 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006386 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006387 char *str;
6388
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006389 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006390 xc_parse_create(fidx);
6391 str = *xc_program_str(sidx);
6392 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006393 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006394
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006395 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006396 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006397 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006398 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006399 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006400 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006401 G.prs = sv_parse; // struct copy
6402 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006403 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006404 bc_vec_pop_all(&f->code);
6405 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006406 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006407 }
6408
Denys Vlasenko24e41942018-12-21 23:01:26 +01006409 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006410 ip.func = fidx;
6411
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006412 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006413 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006414
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006415 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006416 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006417 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006418 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006419}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006420#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006421#endif // ENABLE_DC
6422
Denys Vlasenko10bde142018-12-27 18:23:58 +01006423static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006424{
Gavin Howard01055ba2018-11-03 11:00:21 -06006425 BcResult res;
6426 unsigned long val;
6427
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006428 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006429 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006430 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006431 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006432 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006433 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006434 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006435
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006436 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006437 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006438 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006439}
6440
Denys Vlasenko10bde142018-12-27 18:23:58 +01006441static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006442{
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006444 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006445 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006446 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006447
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006448 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006449 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6450 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006451 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006452 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006453
Denys Vlasenko24e41942018-12-21 23:01:26 +01006454 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 -06006455 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006456#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006457 case BC_INST_JUMP_ZERO: {
6458 BcNum *num;
6459 bool zero;
6460 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006461 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006462 if (s) RETURN_STATUS(s);
6463 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6464 bc_vec_pop(&G.prog.results);
6465 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006466 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006467 break;
6468 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006469 // else: fall through
6470 }
6471 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006472 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006473 size_t *addr = bc_vec_item(&func->labels, idx);
6474 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6475 ip->inst_idx = *addr;
6476 break;
6477 }
6478 case BC_INST_CALL:
6479 dbg_exec("BC_INST_CALL:");
6480 s = zbc_program_call(code, &ip->inst_idx);
6481 goto read_updated_ip;
6482 case BC_INST_INC_PRE:
6483 case BC_INST_DEC_PRE:
6484 case BC_INST_INC_POST:
6485 case BC_INST_DEC_POST:
6486 dbg_exec("BC_INST_INCDEC:");
6487 s = zbc_program_incdec(inst);
6488 break;
6489 case BC_INST_HALT:
6490 dbg_exec("BC_INST_HALT:");
6491 QUIT_OR_RETURN_TO_MAIN;
6492 break;
6493 case XC_INST_RET:
6494 case BC_INST_RET0:
6495 dbg_exec("BC_INST_RET[0]:");
6496 s = zbc_program_return(inst);
6497 goto read_updated_ip;
6498 case XC_INST_BOOL_OR:
6499 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006500#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006501 case XC_INST_REL_EQ:
6502 case XC_INST_REL_LE:
6503 case XC_INST_REL_GE:
6504 case XC_INST_REL_NE:
6505 case XC_INST_REL_LT:
6506 case XC_INST_REL_GT:
6507 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006508 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006509 break;
6510 case XC_INST_READ:
6511 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006512 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006513 goto read_updated_ip;
6514 case XC_INST_VAR:
6515 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006516 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006517 break;
6518 case XC_INST_ARRAY_ELEM:
6519 case XC_INST_ARRAY:
6520 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6521 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6522 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006523#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006524 case BC_INST_LAST:
6525 dbg_exec("BC_INST_LAST:");
6526 r.t = BC_RESULT_LAST;
6527 bc_vec_push(&G.prog.results, &r);
6528 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006529#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006530 case XC_INST_IBASE:
6531 case XC_INST_OBASE:
6532 case XC_INST_SCALE:
6533 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006534 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006535 break;
6536 case XC_INST_SCALE_FUNC:
6537 case XC_INST_LENGTH:
6538 case XC_INST_SQRT:
6539 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006540 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006541 break;
6542 case XC_INST_NUM:
6543 dbg_exec("XC_INST_NUM:");
6544 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006545 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006546 bc_vec_push(&G.prog.results, &r);
6547 break;
6548 case XC_INST_POP:
6549 dbg_exec("XC_INST_POP:");
6550 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6551 s = bc_error_stack_has_too_few_elements();
6552 else
6553 bc_vec_pop(&G.prog.results);
6554 break;
6555 case XC_INST_PRINT:
6556 case XC_INST_PRINT_POP:
6557 case XC_INST_PRINT_STR:
6558 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006559 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006560 break;
6561 case XC_INST_STR:
6562 dbg_exec("XC_INST_STR:");
6563 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006564 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006565 bc_vec_push(&G.prog.results, &r);
6566 break;
6567 case XC_INST_POWER:
6568 case XC_INST_MULTIPLY:
6569 case XC_INST_DIVIDE:
6570 case XC_INST_MODULUS:
6571 case XC_INST_PLUS:
6572 case XC_INST_MINUS:
6573 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006574 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006575 break;
6576 case XC_INST_BOOL_NOT: {
6577 BcNum *num;
6578 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006579 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006580 if (s) RETURN_STATUS(s);
6581 bc_num_init_DEF_SIZE(&r.d.n);
6582 if (bc_num_cmp(num, &G.prog.zero) == 0)
6583 bc_num_one(&r.d.n);
6584 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006585 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006586 break;
6587 }
6588 case XC_INST_NEG:
6589 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006590 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006591 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006592#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006593 case BC_INST_ASSIGN_POWER:
6594 case BC_INST_ASSIGN_MULTIPLY:
6595 case BC_INST_ASSIGN_DIVIDE:
6596 case BC_INST_ASSIGN_MODULUS:
6597 case BC_INST_ASSIGN_PLUS:
6598 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006599#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006600 case XC_INST_ASSIGN:
6601 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006602 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006603 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006604#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006605 case DC_INST_POP_EXEC:
6606 dbg_exec("DC_INST_POP_EXEC:");
6607 bc_vec_pop(&G.prog.exestack);
6608 goto read_updated_ip;
6609 case DC_INST_MODEXP:
6610 dbg_exec("DC_INST_MODEXP:");
6611 s = zdc_program_modexp();
6612 break;
6613 case DC_INST_DIVMOD:
6614 dbg_exec("DC_INST_DIVMOD:");
6615 s = zdc_program_divmod();
6616 break;
6617 case DC_INST_EXECUTE:
6618 case DC_INST_EXEC_COND:
6619 dbg_exec("DC_INST_EXEC[_COND]:");
6620 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6621 goto read_updated_ip;
6622 case DC_INST_PRINT_STACK: {
6623 size_t idx;
6624 dbg_exec("DC_INST_PRINT_STACK:");
6625 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006626 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006627 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006629 break;
6630 }
6631 case DC_INST_CLEAR_STACK:
6632 dbg_exec("DC_INST_CLEAR_STACK:");
6633 bc_vec_pop_all(&G.prog.results);
6634 break;
6635 case DC_INST_STACK_LEN:
6636 dbg_exec("DC_INST_STACK_LEN:");
6637 dc_program_stackLen();
6638 break;
6639 case DC_INST_DUPLICATE:
6640 dbg_exec("DC_INST_DUPLICATE:");
6641 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6642 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6643 ptr = bc_vec_top(&G.prog.results);
6644 dc_result_copy(&r, ptr);
6645 bc_vec_push(&G.prog.results, &r);
6646 break;
6647 case DC_INST_SWAP: {
6648 BcResult *ptr2;
6649 dbg_exec("DC_INST_SWAP:");
6650 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6651 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6652 ptr = bc_vec_item_rev(&G.prog.results, 0);
6653 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6654 memcpy(&r, ptr, sizeof(BcResult));
6655 memcpy(ptr, ptr2, sizeof(BcResult));
6656 memcpy(ptr2, &r, sizeof(BcResult));
6657 break;
6658 }
6659 case DC_INST_ASCIIFY:
6660 dbg_exec("DC_INST_ASCIIFY:");
6661 s = zdc_program_asciify();
6662 break;
6663 case DC_INST_PRINT_STREAM:
6664 dbg_exec("DC_INST_PRINT_STREAM:");
6665 s = zdc_program_printStream();
6666 break;
6667 case DC_INST_LOAD:
6668 case DC_INST_PUSH_VAR: {
6669 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006670 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006671 break;
6672 }
6673 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006674 char *name = xc_program_name(code, &ip->inst_idx);
6675 s = zxc_program_copyToVar(name, true);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006676 free(name);
6677 break;
6678 }
6679 case DC_INST_QUIT:
6680 dbg_exec("DC_INST_QUIT:");
6681 if (G.prog.exestack.len <= 2)
6682 QUIT_OR_RETURN_TO_MAIN;
6683 bc_vec_npop(&G.prog.exestack, 2);
6684 goto read_updated_ip;
6685 case DC_INST_NQUIT:
6686 dbg_exec("DC_INST_NQUIT:");
6687 s = zdc_program_nquit();
6688 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006689#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006690 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006691 // Instruction stack has changed, read new pointers
6692 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006693 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006694 code = func->code.v;
6695 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 }
6697
Denys Vlasenkod38af482018-12-04 19:11:02 +01006698 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006699 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006700 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006701 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006702
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006703 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006704 }
6705
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006706 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006707}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006708#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006709
Denys Vlasenko10bde142018-12-27 18:23:58 +01006710static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006711{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006712 char *lenv;
6713 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006714
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006715 lenv = getenv(var);
6716 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006717 if (!lenv) return len;
6718
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006719 len = bb_strtou(lenv, NULL, 10) - 1;
6720 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006721 len = BC_NUM_PRINT_WIDTH;
6722
6723 return len;
6724}
6725
Denys Vlasenko10bde142018-12-27 18:23:58 +01006726static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006727{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006728 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006729
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006730 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006731 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006732 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006733
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006734 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006735 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006736 BcInstPtr *ip;
6737 BcFunc *f;
6738
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006739 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006740 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006741#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006742 if (G.prs.lex == BC_LEX_SCOLON
6743 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006744 ) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006745 s = zxc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006746 if (s) goto err;
6747 goto check_eof;
6748 }
6749
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006750 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006751 if (s) goto err;
6752
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006753 // Check that next token is a correct stmt delimiter -
6754 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006755 if (G.prs.lex != BC_LEX_SCOLON
6756 && G.prs.lex != XC_LEX_NLINE
6757 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006758 ) {
6759 const char *err_at;
6760//TODO: commonalize for other parse errors:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006761 err_at = G.prs.lex_next_at ? G.prs.lex_next_at : "UNKNOWN";
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006762 bc_error_fmt("bad statement terminator at '%.*s'",
6763 (int)(strchrnul(err_at, '\n') - err_at),
6764 err_at
6765 );
6766 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006767 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006768 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006769 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006770#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006771 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006772#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006773 // Most of dc parsing assumes all whitespace,
6774 // including '\n', is eaten.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006775 while (G.prs.lex == XC_LEX_NLINE) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006776 s = zxc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006777 if (s) goto err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006778 if (G.prs.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006779 goto done;
6780 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006781 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006782#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006783 }
6784 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006785 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006786 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006787 RETURN_STATUS(BC_STATUS_FAILURE);
6788 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006789
Denys Vlasenko39287e02018-12-22 02:23:08 +01006790 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006791 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006792 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006793 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006794 break;
6795 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006796
6797 ip = (void*)G.prog.exestack.v;
6798#if SANITY_CHECKS
6799 if (G.prog.exestack.len != 1) // should have only main's IP
6800 bb_error_msg_and_die("BUG:call stack");
6801 if (ip->func != BC_PROG_MAIN)
6802 bb_error_msg_and_die("BUG:not MAIN");
6803#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006804 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006805 // bc discards strings, constants and code after each
6806 // top-level statement in the "main program".
6807 // This prevents "yes 1 | bc" from growing its memory
6808 // without bound. This can be done because data stack
6809 // is empty and thus can't hold any references to
6810 // strings or constants, there is no generated code
6811 // which can hold references (after we discard one
6812 // we just executed). Code of functions can have references,
6813 // but bc stores function strings/constants in per-function
6814 // storage.
6815 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006816#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006817 if (G.prog.results.len != 0) // should be empty
6818 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006819#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006820 IF_BC(bc_vec_pop_all(&f->strs);)
6821 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006822 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006823 if (G.prog.results.len == 0
6824 && G.prog.vars.len == 0
6825 ) {
6826 // If stack is empty and no registers exist (TODO: or they are all empty),
6827 // we can get rid of accumulated strings and constants.
6828 // In this example dc process should not grow
6829 // its memory consumption with time:
6830 // yes 1pc | dc
6831 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6832 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6833 }
6834 // The code is discarded always (below), thus this example
6835 // should also not grow its memory consumption with time,
6836 // even though its data stack is not empty:
6837 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006838 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006839 // We drop generated and executed code for both bc and dc:
6840 bc_vec_pop_all(&f->code);
6841 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006842 }
Denys Vlasenko8a56e362018-12-26 19:09:23 +01006843 IF_DC(done:)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006844 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006845 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006846}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006847#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006848
Denys Vlasenko10bde142018-12-27 18:23:58 +01006849static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006850{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006851 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006852 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006853 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006854 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006855
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006856 G.prs.lex_filename = filename;
6857 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006858 G.err_line = G.prs.lex_line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06006859
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006860 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006861 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006862 // We do not stop looping on errors here if reading stdin.
6863 // Example: start interactive bc and enter "return".
6864 // It should say "'return' not in a function"
6865 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006866 } while (G.prs.lex_input_fp == stdin);
6867 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006868 RETURN_STATUS(s);
6869}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006870#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006871
Denys Vlasenko10bde142018-12-27 18:23:58 +01006872static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006873{
6874 BcStatus s;
6875 FILE *fp;
6876
6877 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006878 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006879 fclose(fp);
6880
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006881 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006882}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006883#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006884
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006885#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006886static void bc_vm_info(void)
6887{
6888 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006889 "Adapted from https://github.com/gavinhoward/bc\n"
6890 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006891 , applet_name);
6892}
6893
6894static void bc_args(char **argv)
6895{
6896 unsigned opts;
6897 int i;
6898
6899 GETOPT_RESET();
6900#if ENABLE_FEATURE_BC_LONG_OPTIONS
6901 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6902 "warn\0" No_argument "w"
6903 "version\0" No_argument "v"
6904 "standard\0" No_argument "s"
6905 "quiet\0" No_argument "q"
6906 "mathlib\0" No_argument "l"
6907 "interactive\0" No_argument "i"
6908 );
6909#else
6910 opts = option_mask32 |= getopt32(argv, "wvsqli");
6911#endif
6912 if (getenv("POSIXLY_CORRECT"))
6913 option_mask32 |= BC_FLAG_S;
6914
6915 if (opts & BC_FLAG_V) {
6916 bc_vm_info();
6917 exit(0);
6918 }
6919
6920 for (i = optind; argv[i]; ++i)
6921 bc_vec_push(&G.files, argv + i);
6922}
6923
6924static void bc_vm_envArgs(void)
6925{
6926 BcVec v;
6927 char *buf;
6928 char *env_args = getenv("BC_ENV_ARGS");
6929
6930 if (!env_args) return;
6931
6932 G.env_args = xstrdup(env_args);
6933 buf = G.env_args;
6934
6935 bc_vec_init(&v, sizeof(char *), NULL);
6936
6937 while (*(buf = skip_whitespace(buf)) != '\0') {
6938 bc_vec_push(&v, &buf);
6939 buf = skip_non_whitespace(buf);
6940 if (!*buf)
6941 break;
6942 *buf++ = '\0';
6943 }
6944
6945 // NULL terminate, and pass argv[] so that first arg is argv[1]
6946 if (sizeof(int) == sizeof(char*)) {
6947 bc_vec_push(&v, &const_int_0);
6948 } else {
6949 static char *const nullptr = NULL;
6950 bc_vec_push(&v, &nullptr);
6951 }
6952 bc_args(((char **)v.v) - 1);
6953
6954 bc_vec_free(&v);
6955}
6956
6957static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006958 "scale=20"
6959"\n" "define e(x){"
6960"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006961////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6962//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6963//e(-.998896): GNU:.36828580434569428695
6964// above code:.36828580434569428696
6965// actual value:.3682858043456942869594...
6966// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006967"\n" "b=ibase"
6968"\n" "ibase=A"
6969"\n" "if(x<0){"
6970"\n" "n=1"
6971"\n" "x=-x"
6972"\n" "}"
6973"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006974"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006975"\n" "scale=scale(x)+1"
6976"\n" "while(x>1){"
6977"\n" "d+=1"
6978"\n" "x/=2"
6979"\n" "scale+=1"
6980"\n" "}"
6981"\n" "scale=r"
6982"\n" "r=x+1"
6983"\n" "p=x"
6984"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006985"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006986"\n" "p*=x"
6987"\n" "f*=i"
6988"\n" "v=p/f"
6989"\n" "r+=v"
6990"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006991"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006992"\n" "scale=s"
6993"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006994"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006995"\n" "return(r/1)"
6996"\n" "}"
6997"\n" "define l(x){"
6998"\n" "auto b,s,r,p,a,q,i,v"
6999"\n" "b=ibase"
7000"\n" "ibase=A"
7001"\n" "if(x<=0){"
7002"\n" "r=(1-10^scale)/1"
7003"\n" "ibase=b"
7004"\n" "return(r)"
7005"\n" "}"
7006"\n" "s=scale"
7007"\n" "scale+=6"
7008"\n" "p=2"
7009"\n" "while(x>=2){"
7010"\n" "p*=2"
7011"\n" "x=sqrt(x)"
7012"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007013"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007014"\n" "p*=2"
7015"\n" "x=sqrt(x)"
7016"\n" "}"
7017"\n" "r=a=(x-1)/(x+1)"
7018"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007019"\n" "v=1"
7020"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007021"\n" "a*=q"
7022"\n" "v=a/i"
7023"\n" "r+=v"
7024"\n" "}"
7025"\n" "r*=p"
7026"\n" "scale=s"
7027"\n" "ibase=b"
7028"\n" "return(r/1)"
7029"\n" "}"
7030"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007031"\n" "auto b,s,r,a,q,i"
7032"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007033"\n" "b=ibase"
7034"\n" "ibase=A"
7035"\n" "s=scale"
7036"\n" "scale=1.1*s+2"
7037"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007038"\n" "scale=0"
7039"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007040"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007041"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007042"\n" "scale=s+2"
7043"\n" "r=a=x"
7044"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007045"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007046"\n" "a*=q/(i*(i-1))"
7047"\n" "r+=a"
7048"\n" "}"
7049"\n" "scale=s"
7050"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007051"\n" "return(r/1)"
7052"\n" "}"
7053"\n" "define c(x){"
7054"\n" "auto b,s"
7055"\n" "b=ibase"
7056"\n" "ibase=A"
7057"\n" "s=scale"
7058"\n" "scale*=1.2"
7059"\n" "x=s(2*a(1)+x)"
7060"\n" "scale=s"
7061"\n" "ibase=b"
7062"\n" "return(x/1)"
7063"\n" "}"
7064"\n" "define a(x){"
7065"\n" "auto b,s,r,n,a,m,t,f,i,u"
7066"\n" "b=ibase"
7067"\n" "ibase=A"
7068"\n" "n=1"
7069"\n" "if(x<0){"
7070"\n" "n=-1"
7071"\n" "x=-x"
7072"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007073"\n" "if(scale<65){"
7074"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7075"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007076"\n" "}"
7077"\n" "s=scale"
7078"\n" "if(x>.2){"
7079"\n" "scale+=5"
7080"\n" "a=a(.2)"
7081"\n" "}"
7082"\n" "scale=s+3"
7083"\n" "while(x>.2){"
7084"\n" "m+=1"
7085"\n" "x=(x-.2)/(1+.2*x)"
7086"\n" "}"
7087"\n" "r=u=x"
7088"\n" "f=-x*x"
7089"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007090"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007091"\n" "u*=f"
7092"\n" "t=u/i"
7093"\n" "r+=t"
7094"\n" "}"
7095"\n" "scale=s"
7096"\n" "ibase=b"
7097"\n" "return((m*a+r)/n)"
7098"\n" "}"
7099"\n" "define j(n,x){"
7100"\n" "auto b,s,o,a,i,v,f"
7101"\n" "b=ibase"
7102"\n" "ibase=A"
7103"\n" "s=scale"
7104"\n" "scale=0"
7105"\n" "n/=1"
7106"\n" "if(n<0){"
7107"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007108"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007109"\n" "}"
7110"\n" "a=1"
7111"\n" "for(i=2;i<=n;++i)a*=i"
7112"\n" "scale=1.5*s"
7113"\n" "a=(x^n)/2^n/a"
7114"\n" "r=v=1"
7115"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007116"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007117"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007118"\n" "v=v*f/i/(n+i)"
7119"\n" "r+=v"
7120"\n" "}"
7121"\n" "scale=s"
7122"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007123"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007124"\n" "return(a*r/1)"
7125"\n" "}"
7126};
7127#endif // ENABLE_BC
7128
Denys Vlasenko10bde142018-12-27 18:23:58 +01007129static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007130{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007131 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007132 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007133 size_t i;
7134
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007135#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007136 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007137 // We know that internal library is not buggy,
7138 // thus error checking is normally disabled.
7139# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007140 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007141 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007142 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007143#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007144
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007145 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007146 fname = (void*)G.files.v;
7147 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007148 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007149 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7150 // Debug config, non-interactive mode:
7151 // return all the way back to main.
7152 // Non-debug builds do not come here
7153 // in non-interactive mode, they exit.
7154 RETURN_STATUS(s);
7155 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007156 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007157
Denys Vlasenko91cde952018-12-10 20:56:08 +01007158 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007159 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007160
Denys Vlasenko19f11072018-12-12 22:48:19 +01007161 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007162}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007163#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007164
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007165#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007166static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007167{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007168 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007169 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007170 bc_vec_free(&G.prog.vars);
7171 bc_vec_free(&G.prog.var_map);
7172 bc_vec_free(&G.prog.arrs);
7173 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007174 IF_DC(bc_vec_free(&G.prog.strs);)
7175 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007176 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007177 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007178 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007179 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007180 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007181 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007182}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007183#endif
7184
Denys Vlasenko10bde142018-12-27 18:23:58 +01007185static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007186{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007187 BcInstPtr ip;
7188
Denys Vlasenko82269122018-12-14 16:30:56 +01007189 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007190 memset(&ip, 0, sizeof(BcInstPtr));
7191
Denys Vlasenko82269122018-12-14 16:30:56 +01007192 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007193 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007194 G.prog.ob_t = 10;
7195
Denys Vlasenko503faf92018-12-20 16:24:18 +01007196 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7197 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007198
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007199 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007200 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007201
Denys Vlasenko503faf92018-12-20 16:24:18 +01007202 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7203 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007204
7205 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007206 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007207
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007208 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007209 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007210 // a valid function name (and be short)
7211 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007212 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007213 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007214 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007215 xc_program_add_fn();
7216 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007217 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007218
7219 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007220 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007221
7222 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007223 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007224
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007225 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7226 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007227 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007228 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7229 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007230
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007231 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007232}
Gavin Howard01055ba2018-11-03 11:00:21 -06007233
Denys Vlasenko10bde142018-12-27 18:23:58 +01007234static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007235{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007236 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007237#if ENABLE_FEATURE_EDITING
7238 G.line_input_state = new_line_input_t(DO_HISTORY);
7239#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007240 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007241
Denys Vlasenko10bde142018-12-27 18:23:58 +01007242 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007243 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007244 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007245
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007246//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7247//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007248 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007249#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007250 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007251 // With SA_RESTART, most system calls will restart
7252 // (IOW: they won't fail with EINTR).
7253 // In particular, this means ^C won't cause
7254 // stdout to get into "error state" if SIGINT hits
7255 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007256 //
7257 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007258 // will only be handled after [Enter] since read()
7259 // from stdin is not interrupted by ^C either,
7260 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007261 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007262 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7263
7264 // Without SA_RESTART, this exhibits a bug:
7265 // "while (1) print 1" and try ^C-ing it.
7266 // Intermittently, instead of returning to input line,
7267 // you'll get "output error: Interrupted system call"
7268 // and exit.
7269 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007270#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007271 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007272 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007273 return 0; // "not a tty"
7274}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007275
Denys Vlasenko10bde142018-12-27 18:23:58 +01007276static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007277{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007278 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007279#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007280 if (G_exiting) // it was actually "halt" or "quit"
7281 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007282
7283 bc_vec_free(&G.files);
7284 xc_program_free();
7285 xc_parse_free();
7286 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007287# if ENABLE_FEATURE_EDITING
7288 free_line_input_t(G.line_input_state);
7289# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007290 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007291#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007292 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007293 return st;
7294}
7295
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007296#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007297int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007298int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007299{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007300 int is_tty;
7301
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007302 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007303
Denys Vlasenko10bde142018-12-27 18:23:58 +01007304 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007305
7306 bc_args(argv);
7307
7308 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7309 bc_vm_info();
7310
Denys Vlasenko10bde142018-12-27 18:23:58 +01007311 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007312}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007313#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007314
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007315#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007316int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007317int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007318{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007319 int noscript;
7320
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007321 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007322
7323 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7324 // 1 char wider than bc from the same package.
7325 // Both default width, and xC_LINE_LENGTH=N are wider:
7326 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007327 // |1234\ |
7328 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007329 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007330 // |123\ |
7331 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007332 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007333 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007334
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007335 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7336 noscript = BC_FLAG_I;
7337 for (;;) {
7338 int n = getopt(argc, argv, "e:f:x");
7339 if (n <= 0)
7340 break;
7341 switch (n) {
7342 case 'e':
7343 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007344 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007345 if (n) return n;
7346 break;
7347 case 'f':
7348 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007349 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007350 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007351 break;
7352 case 'x':
7353 option_mask32 |= DC_FLAG_X;
7354 break;
7355 default:
7356 bb_show_usage();
7357 }
7358 }
7359 argv += optind;
7360
7361 while (*argv) {
7362 noscript = 0;
7363 bc_vec_push(&G.files, argv++);
7364 }
7365
7366 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7367
Denys Vlasenko10bde142018-12-27 18:23:58 +01007368 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007369}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007370#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007371
Denys Vlasenko14767602018-12-27 22:52:13 +01007372#endif // DC_BIG
7373