blob: 02a61ac4917b4a90cf12a8be69a28aa5708345ff [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 Vlasenko53799502019-01-25 14:24:03 +01007//TODO:
8// maybe implement a^b for non-integer b?
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01009
10#define DEBUG_LEXER 0
11#define DEBUG_COMPILE 0
12#define DEBUG_EXEC 0
13// This can be left enabled for production as well:
14#define SANITY_CHECKS 1
15
Gavin Howard01055ba2018-11-03 11:00:21 -060016//config:config BC
Denys Vlasenkob097a842018-12-28 03:20:17 +010017//config: bool "bc (45 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060018//config: default y
Denys Vlasenkocdadad52018-12-28 15:13:23 +010019//config: select FEATURE_DC_BIG
Gavin Howard01055ba2018-11-03 11:00:21 -060020//config: help
21//config: bc is a command-line, arbitrary-precision calculator with a
22//config: Turing-complete language. See the GNU bc manual
23//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010024//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060025//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010026//config: This bc has five differences to the GNU bc:
27//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config: 2) Arrays are copied before being passed as arguments to
29//config: functions. This behavior is required by the bc spec.
30//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: the number of elements in the array. This prints "1":
32//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060033//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010034//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060035//config: allows POSIX-compliant scripts to work while somewhat
36//config: preserving expected behavior (versus C) and making parsing
37//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010038//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060039//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060040//config:config DC
Denys Vlasenkob097a842018-12-28 03:20:17 +010041//config: bool "dc (36 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060042//config: default y
43//config: help
44//config: dc is a reverse-polish notation command-line calculator which
45//config: supports unlimited precision arithmetic. See the FreeBSD man page
46//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010047//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060048//config:
49//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010050//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060051//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: 4) Like the FreeBSD dc, this dc supports extended registers.
56//config: However, they are implemented differently. When it encounters
57//config: whitespace where a register should be, it skips the whitespace.
58//config: If the character following is not a lowercase letter, an error
59//config: is issued. Otherwise, the register name is parsed by the
Denys Vlasenko14767602018-12-27 22:52:13 +010060//config: following regex: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060061//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010062//config: whitespace. Examples:
63//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: Also note that, like the FreeBSD dc, extended registers are not
65//config: allowed unless the "-x" option is given.
66//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010067//config:if BC || DC # for menuconfig indenting
68//config:
69//config:config FEATURE_DC_BIG
70//config: bool "Use bc code base for dc (larger, more features)"
Denys Vlasenko14767602018-12-27 22:52:13 +010071//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010072//config:
73//config:config FEATURE_DC_LIBM
74//config: bool "Enable power and exp functions (requires libm)"
75//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010076//config: depends on DC && !BC && !FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010077//config: help
78//config: Enable power and exp functions.
79//config: NOTE: This will require libm to be present for linking.
80//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010081//config:config FEATURE_BC_INTERACTIVE
Denys Vlasenko89e785a2018-12-13 16:35:52 +010082//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010084//config: depends on BC || (DC && FEATURE_DC_BIG)
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010086//config: Enable interactive mode: when started on a tty,
87//config: ^C interrupts execution and returns to command line,
88//config: errors also return to command line instead of exiting,
89//config: line editing with history is available.
90//config:
91//config: With this option off, input can still be taken from tty,
92//config: but all errors are fatal, ^C is fatal,
93//config: tty is treated exactly the same as any other
94//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060095//config:
96//config:config FEATURE_BC_LONG_OPTIONS
97//config: bool "Enable bc/dc long options"
98//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010099//config: depends on BC || (DC && FEATURE_DC_BIG)
100//config:
101//config:endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600102
103//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
104//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
105
106//kbuild:lib-$(CONFIG_BC) += bc.o
107//kbuild:lib-$(CONFIG_DC) += bc.o
108
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100109//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600110//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100111//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100113//usage:#define bc_full_usage "\n"
114//usage: "\nArbitrary precision calculator"
115//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100116///////: "\n -i Interactive" - has no effect for now
117//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100118//usage: "\n -l Load standard math library"
119//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -w Warn if extensions are used"
121///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100122//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100123//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600124//usage:
125//usage:#define bc_example_usage
126//usage: "3 + 4.129\n"
127//usage: "1903 - 2893\n"
128//usage: "-129 * 213.28935\n"
129//usage: "12 / -1932\n"
130//usage: "12 % 12\n"
131//usage: "34 ^ 189\n"
132//usage: "scale = 13\n"
133//usage: "ibase = 2\n"
134//usage: "obase = A\n"
135//usage:
136//usage:#define dc_trivial_usage
Denys Vlasenko14767602018-12-27 22:52:13 +0100137//usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600138//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100139//usage:#define dc_full_usage "\n"
140//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko14767602018-12-27 22:52:13 +0100141//usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100142//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100143//usage: "\nf - print entire stack"
144//usage: "\nk - pop the value and set the precision"
145//usage: "\ni - pop the value and set input radix"
146//usage: "\no - pop the value and set output radix"
147//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600148//usage:
149//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100150//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600151//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "0\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: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "64\n"
160
161#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100162#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600163
Denys Vlasenko14767602018-12-27 22:52:13 +0100164#if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100165# include "dc.c"
166#else
167
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100168#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100169static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100170#define dbg_lex(...) \
171 do { \
172 fprintf(stderr, "%*s", lex_indent, ""); \
173 bb_error_msg(__VA_ARGS__); \
174 } while (0)
175#define dbg_lex_enter(...) \
176 do { \
177 dbg_lex(__VA_ARGS__); \
178 lex_indent++; \
179 } while (0)
180#define dbg_lex_done(...) \
181 do { \
182 lex_indent--; \
183 dbg_lex(__VA_ARGS__); \
184 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100185#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100186# define dbg_lex(...) ((void)0)
187# define dbg_lex_enter(...) ((void)0)
188# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100189#endif
190
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100191#if DEBUG_COMPILE
192# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
193#else
194# define dbg_compile(...) ((void)0)
195#endif
196
Denys Vlasenko99b37622018-12-15 20:06:59 +0100197#if DEBUG_EXEC
198# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
199#else
200# define dbg_exec(...) ((void)0)
201#endif
202
Gavin Howard01055ba2018-11-03 11:00:21 -0600203typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100204 BC_STATUS_SUCCESS = 0,
205 BC_STATUS_FAILURE = 1,
Gavin Howard01055ba2018-11-03 11:00:21 -0600206} BcStatus;
207
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100208#define BC_VEC_INVALID_IDX ((size_t) -1)
209#define BC_VEC_START_CAP (1 << 5)
Gavin Howard01055ba2018-11-03 11:00:21 -0600210
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100211typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600212
213typedef struct BcVec {
214 char *v;
215 size_t len;
216 size_t cap;
217 size_t size;
218 BcVecFree dtor;
219} BcVec;
220
Gavin Howard01055ba2018-11-03 11:00:21 -0600221typedef signed char BcDig;
222
223typedef struct BcNum {
224 BcDig *restrict num;
225 size_t rdx;
226 size_t len;
227 size_t cap;
228 bool neg;
229} BcNum;
230
Denys Vlasenko680ccd32018-12-31 19:42:13 +0100231#define BC_NUM_MAX_IBASE 36
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100232// larger value might speed up BIGNUM calculations a bit:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100233#define BC_NUM_DEF_SIZE 16
Denys Vlasenko29a90432020-12-29 18:50:56 +0100234#define BC_NUM_PRINT_WIDTH 70
Gavin Howard01055ba2018-11-03 11:00:21 -0600235
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100236#define BC_NUM_KARATSUBA_LEN 32
Gavin Howard01055ba2018-11-03 11:00:21 -0600237
Gavin Howard01055ba2018-11-03 11:00:21 -0600238typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100239#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600240 BC_INST_INC_PRE,
241 BC_INST_DEC_PRE,
242 BC_INST_INC_POST,
243 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100244#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100245 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100247 XC_INST_REL_EQ, // should
248 XC_INST_REL_LE, // match
249 XC_INST_REL_GE, // LEX
250 XC_INST_REL_NE, // constants
251 XC_INST_REL_LT, // for
252 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600253
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100254 XC_INST_POWER, // operations
255 XC_INST_MULTIPLY, // |
256 XC_INST_DIVIDE, // |
257 XC_INST_MODULUS, // |
258 XC_INST_PLUS, // |
259 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600260
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100261 XC_INST_BOOL_NOT, // |
262 XC_INST_BOOL_OR, // |
263 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100264#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100265 BC_INST_ASSIGN_POWER, // |
266 BC_INST_ASSIGN_MULTIPLY,// |
267 BC_INST_ASSIGN_DIVIDE, // |
268 BC_INST_ASSIGN_MODULUS, // |
269 BC_INST_ASSIGN_PLUS, // |
270 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100271#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100272 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600273
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100274 XC_INST_NUM,
275 XC_INST_VAR,
276 XC_INST_ARRAY_ELEM,
277 XC_INST_ARRAY,
278 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100279
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100280 XC_INST_IBASE, // order of these constans should match other enums
281 XC_INST_OBASE, // order of these constans should match other enums
282 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100283 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100284 XC_INST_LENGTH,
285 XC_INST_READ,
286 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600287
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100288 XC_INST_PRINT,
289 XC_INST_PRINT_POP,
290 XC_INST_STR,
291 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600292
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100293#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100294 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600295 BC_INST_JUMP,
296 BC_INST_JUMP_ZERO,
297
298 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600299 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100300#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100301 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600302
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100303 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100304#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100305 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600306
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100307 DC_INST_MODEXP,
308 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600309
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 DC_INST_EXECUTE,
311 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600312
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100313 DC_INST_ASCIIFY,
314 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_PRINT_STACK,
317 DC_INST_CLEAR_STACK,
318 DC_INST_STACK_LEN,
319 DC_INST_DUPLICATE,
320 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600321
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100322 DC_INST_LOAD,
323 DC_INST_PUSH_VAR,
324 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600325
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100326 DC_INST_QUIT,
327 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600328
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100329 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100330#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600331} BcInst;
332
333typedef struct BcId {
334 char *name;
335 size_t idx;
336} BcId;
337
338typedef struct BcFunc {
339 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100340 IF_BC(BcVec labels;)
341 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100342 IF_BC(BcVec strs;)
343 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100344 IF_BC(size_t nparams;)
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100345 IF_BC(bool voidfunc;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600346} BcFunc;
347
348typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100349 XC_RESULT_TEMP,
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100350 IF_BC(BC_RESULT_VOID,) // same as TEMP, but INST_PRINT will ignore it
Gavin Howard01055ba2018-11-03 11:00:21 -0600351
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100352 XC_RESULT_VAR,
353 XC_RESULT_ARRAY_ELEM,
354 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600355
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100356 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600357
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100358 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
359 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
360 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
361 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100362 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100363 XC_RESULT_CONSTANT,
364 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600365} BcResultType;
366
367typedef union BcResultData {
368 BcNum n;
369 BcVec v;
370 BcId id;
371} BcResultData;
372
373typedef struct BcResult {
374 BcResultType t;
375 BcResultData d;
376} BcResult;
377
378typedef struct BcInstPtr {
379 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100380 size_t inst_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -0600381} BcInstPtr;
382
Denys Vlasenko53799502019-01-25 14:24:03 +0100383typedef enum BcType {
384 BC_TYPE_VAR,
385 BC_TYPE_ARRAY,
386 BC_TYPE_REF,
387} BcType;
388
Gavin Howard01055ba2018-11-03 11:00:21 -0600389typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100390 XC_LEX_EOF,
391 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600392
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100393 XC_LEX_NLINE,
394 XC_LEX_WHITESPACE,
395 XC_LEX_STR,
396 XC_LEX_NAME,
397 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100398
Denys Vlasenko69560f42018-12-24 14:14:23 +0100399 XC_LEX_1st_op,
400 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100401
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100402 XC_LEX_OP_REL_EQ, // should
403 XC_LEX_OP_REL_LE, // match
404 XC_LEX_OP_REL_GE, // INST
405 XC_LEX_OP_REL_NE, // constants
406 XC_LEX_OP_REL_LT, // for
407 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100408
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100409 XC_LEX_OP_POWER, // operations
410 XC_LEX_OP_MULTIPLY, // |
411 XC_LEX_OP_DIVIDE, // |
412 XC_LEX_OP_MODULUS, // |
413 XC_LEX_OP_PLUS, // |
414 XC_LEX_OP_MINUS, // |
415 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100416#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100417 BC_LEX_OP_BOOL_NOT, // |
418 BC_LEX_OP_BOOL_OR, // |
419 BC_LEX_OP_BOOL_AND, // |
420
421 BC_LEX_OP_ASSIGN_POWER, // |
422 BC_LEX_OP_ASSIGN_MULTIPLY, // |
423 BC_LEX_OP_ASSIGN_DIVIDE, // |
424 BC_LEX_OP_ASSIGN_MODULUS, // |
425 BC_LEX_OP_ASSIGN_PLUS, // |
426 BC_LEX_OP_ASSIGN_MINUS, // |
427
428 BC_LEX_OP_ASSIGN, // V
429
Gavin Howard01055ba2018-11-03 11:00:21 -0600430 BC_LEX_OP_INC,
431 BC_LEX_OP_DEC,
432
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100433 BC_LEX_LPAREN, // () are 0x28 and 0x29
434 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600435
Denys Vlasenko10bde142018-12-27 18:23:58 +0100436 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600437 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100438 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600439
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100440 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600441 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100442 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600443
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100444 BC_LEX_KEY_1st_keyword,
445 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600446 BC_LEX_KEY_BREAK,
447 BC_LEX_KEY_CONTINUE,
448 BC_LEX_KEY_DEFINE,
449 BC_LEX_KEY_ELSE,
450 BC_LEX_KEY_FOR,
451 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100452 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100453 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
454 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600455 BC_LEX_KEY_IF,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100456 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_LENGTH,
458 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_PRINT,
460 BC_LEX_KEY_QUIT,
461 BC_LEX_KEY_READ,
462 BC_LEX_KEY_RETURN,
463 BC_LEX_KEY_SCALE,
464 BC_LEX_KEY_SQRT,
465 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100466#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600467
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100468#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100469 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
470 DC_LEX_OP_ASSIGN,
471
472 DC_LEX_LPAREN,
473 DC_LEX_SCOLON,
474 DC_LEX_READ,
475 DC_LEX_IBASE,
476 DC_LEX_SCALE,
477 DC_LEX_OBASE,
478 DC_LEX_LENGTH,
479 DC_LEX_PRINT,
480 DC_LEX_QUIT,
481 DC_LEX_SQRT,
482 DC_LEX_LBRACE,
483
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100484 DC_LEX_EQ_NO_REG,
485 DC_LEX_OP_MODEXP,
486 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600487
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100488 DC_LEX_COLON,
489 DC_LEX_ELSE,
490 DC_LEX_EXECUTE,
491 DC_LEX_PRINT_STACK,
492 DC_LEX_CLEAR_STACK,
493 DC_LEX_STACK_LEVEL,
494 DC_LEX_DUPLICATE,
495 DC_LEX_SWAP,
496 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600497
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100498 DC_LEX_ASCIIFY,
499 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600500
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100501 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
502 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
503 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
504 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
505 DC_LEX_LOAD,
506 DC_LEX_LOAD_POP,
507 DC_LEX_STORE_PUSH,
508 DC_LEX_PRINT_POP,
509 DC_LEX_NQUIT,
510 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100511#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600512} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100513// must match order of BC_LEX_KEY_foo etc above
514#if ENABLE_BC
515struct BcLexKeyword {
516 char name8[8];
517};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100518#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100519 { .name8 = a /*, .posix = b */ }
Denys Vlasenko6cc49622020-11-30 14:58:02 +0100520static const struct BcLexKeyword bc_lex_kws[20] ALIGN8 = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100521 LEX_KW_ENTRY("auto" , 1), // 0
522 LEX_KW_ENTRY("break" , 1), // 1
523 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
524 LEX_KW_ENTRY("define" , 1), // 3
525 LEX_KW_ENTRY("else" , 0), // 4
526 LEX_KW_ENTRY("for" , 1), // 5
527 LEX_KW_ENTRY("halt" , 0), // 6
528 LEX_KW_ENTRY("ibase" , 1), // 7
529 LEX_KW_ENTRY("obase" , 1), // 8
530 LEX_KW_ENTRY("if" , 1), // 9
531 LEX_KW_ENTRY("last" , 0), // 10
532 LEX_KW_ENTRY("length" , 1), // 11
533 LEX_KW_ENTRY("limits" , 0), // 12
534 LEX_KW_ENTRY("print" , 0), // 13
535 LEX_KW_ENTRY("quit" , 1), // 14
536 LEX_KW_ENTRY("read" , 0), // 15
537 LEX_KW_ENTRY("return" , 1), // 16
538 LEX_KW_ENTRY("scale" , 1), // 17
539 LEX_KW_ENTRY("sqrt" , 1), // 18
540 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100541};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100542#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100543#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100544#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100545#define STRING_if (bc_lex_kws[9].name8)
546#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100547enum {
548 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100549 | (1 << 0) // 0
550 | (1 << 1) // 1
551 | (0 << 2) // 2
552 | (1 << 3) // 3
553 | (0 << 4) // 4
554 | (1 << 5) // 5
555 | (0 << 6) // 6
556 | (1 << 7) // 7
557 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100558 | (1 << 9) // 9
559 | (0 << 10) // 10
560 | (1 << 11) // 11
561 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100562 | (0 << 13) // 13
563 | (1 << 14) // 14
564 | (0 << 15) // 15
565 | (1 << 16) // 16
566 | (1 << 17) // 17
567 | (1 << 18) // 18
568 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100569};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100570#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100571
572// This is a bit array that corresponds to token types. An entry is
573// true if the token is valid in an expression, false otherwise.
574// Used to figure out when expr parsing should stop *without error message*
575// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100576// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100577// by later processing.
578enum {
579#define EXBITS(a,b,c,d,e,f,g,h) \
580 ((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 +0100581 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100582 + (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 +0100583 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
584 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100585 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
586 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100587 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
588 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
589 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100590#undef EXBITS
591};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100592static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100593{
594#if ULONG_MAX > 0xffffffff
595 // 64-bit version (will not work correctly for 32-bit longs!)
596 return BC_PARSE_EXPRS_BITS & (1UL << i);
597#else
598 // 32-bit version
599 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
600 if (i >= 32) {
601 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
602 i &= 31;
603 }
604 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100605#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100606}
607
608// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100609// [XC_LEX_1st_op...] token types.
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100610static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100611#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100613 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 +0100614 OP(2, false), // pow
615 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
616 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100617 OP(1, false), // not
618 OP(7, true ), OP( 7, true ), // or and
619 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
620 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100621 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100622#undef OP
623};
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100624#define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f)
625#define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100626#endif // ENABLE_BC
627
628#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100629static const //BcLexType - should be this type
630uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100631dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100632 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100633 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100634 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100635 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100636 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100637 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100638 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100639 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
640 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
641 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100642 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100643 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
644 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100645 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100646 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
647 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100648 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100649 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
650 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenkof11b5b92019-01-04 15:54:40 +0100651 // QRSTUVWX
652 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID,
653 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR,
654 // YZ
655 XC_LEX_INVALID, DC_LEX_LENGTH,
656 // [\]
657 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100658 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100659 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100660 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100661 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
662 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100663 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100666 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100667 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
668 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100669 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100670 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100671 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100672 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100673};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100674static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100675int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100676dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
677 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
678 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
679 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100680 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
681 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
682 XC_INST_REL_GT, // DC_LEX_LPAREN
683 DC_INST_INVALID, // DC_LEX_SCOLON
684 DC_INST_INVALID, // DC_LEX_READ
685 XC_INST_IBASE, // DC_LEX_IBASE
686 XC_INST_SCALE, // DC_LEX_SCALE
687 XC_INST_OBASE, // DC_LEX_OBASE
688 XC_INST_LENGTH, // DC_LEX_LENGTH
689 XC_INST_PRINT, // DC_LEX_PRINT
690 DC_INST_QUIT, // DC_LEX_QUIT
691 XC_INST_SQRT, // DC_LEX_SQRT
692 XC_INST_REL_GE, // DC_LEX_LBRACE
693 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100694 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
695 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
696 DC_INST_EXECUTE, // DC_LEX_EXECUTE
697 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
698 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
699 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
700 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
701 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
702 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
703 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
704 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
705 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100706 // DC_INST_INVALID in this table either means that corresponding LEX
707 // is not possible for dc, or that it does not compile one-to-one
708 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100709};
710#endif // ENABLE_DC
711
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100712typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100713 smallint lex; // was BcLexType // first member is most used
714 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100715 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100716 const char *lex_inbuf;
717 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100718 const char *lex_filename;
719 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100720 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100721
Gavin Howard01055ba2018-11-03 11:00:21 -0600722 BcFunc *func;
723 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100724 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100725 IF_BC(BcVec exits;)
726 IF_BC(BcVec conds;)
727 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600728} BcParse;
729
Gavin Howard01055ba2018-11-03 11:00:21 -0600730typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600731 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100732 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600733
Denys Vlasenko503faf92018-12-20 16:24:18 +0100734 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600735 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600736 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600737
Gavin Howard01055ba2018-11-03 11:00:21 -0600738 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100739 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600740
741 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100742 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600743
744 BcVec vars;
745 BcVec var_map;
746
747 BcVec arrs;
748 BcVec arr_map;
749
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100750 IF_DC(BcVec strs;)
751 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600752
Gavin Howard01055ba2018-11-03 11:00:21 -0600753 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100754 IF_BC(BcNum one;)
755 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600756} BcProgram;
757
Denys Vlasenko6e618232018-12-25 22:20:14 +0100758struct globals {
759 BcParse prs; // first member is most used
760
761 // For error messages. Can be set to current parsed line,
762 // or [TODO] to current executing line (can be before last parsed one)
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100763 size_t err_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100764
765 BcVec input_buffer;
766
Denys Vlasenko14767602018-12-27 22:52:13 +0100767 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100768 IF_FEATURE_CLEAN_UP(smallint exiting;)
769
770 BcProgram prog;
771
772 BcVec files;
773
774 char *env_args;
775
776#if ENABLE_FEATURE_EDITING
777 line_input_t *line_input_state;
778#endif
779} FIX_ALIASING;
780#define G (*ptr_to_globals)
781#define INIT_G() do { \
782 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
783} while (0)
784#define FREE_G() do { \
785 FREE_PTR_TO_GLOBALS(); \
786} while (0)
787#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
788#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
789#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100790#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100791# define G_interrupt bb_got_signal
792# define G_ttyin G.ttyin
793#else
794# define G_interrupt 0
795# define G_ttyin 0
796#endif
797#if ENABLE_FEATURE_CLEAN_UP
798# define G_exiting G.exiting
799#else
800# define G_exiting 0
801#endif
802#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
803#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
804
Denys Vlasenko6e618232018-12-25 22:20:14 +0100805#if ENABLE_BC
806# define BC_PARSE_REL (1 << 0)
807# define BC_PARSE_PRINT (1 << 1)
808# define BC_PARSE_ARRAY (1 << 2)
809# define BC_PARSE_NOCALL (1 << 3)
810#endif
811
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100812#define BC_PROG_MAIN 0
813#define BC_PROG_READ 1
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100814#if ENABLE_DC
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100815#define BC_PROG_REQ_FUNCS 2
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100816#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600817
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100818#define BC_FLAG_W (1 << 0)
819#define BC_FLAG_V (1 << 1)
820#define BC_FLAG_S (1 << 2)
821#define BC_FLAG_Q (1 << 3)
822#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100823#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
824#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600825
Denys Vlasenko64074a12018-12-07 15:50:14 +0100826#define BC_MAX_OBASE ((unsigned) 999)
827#define BC_MAX_DIM ((unsigned) INT_MAX)
828#define BC_MAX_SCALE ((unsigned) UINT_MAX)
829#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
830#define BC_MAX_NUM BC_MAX_STRING
831// Unused apart from "limits" message. Just show a "biggish number" there.
Denys Vlasenko64074a12018-12-07 15:50:14 +0100832//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
833//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
Denys Vlasenko64074a12018-12-07 15:50:14 +0100834#define BC_MAX_EXP_STR "999999999"
835#define BC_MAX_VARS_STR "999999999"
836
837#define BC_MAX_OBASE_STR "999"
838
839#if INT_MAX == 2147483647
840# define BC_MAX_DIM_STR "2147483647"
841#elif INT_MAX == 9223372036854775807
842# define BC_MAX_DIM_STR "9223372036854775807"
843#else
844# error Strange INT_MAX
845#endif
846
Kang-Che Sungf1593522019-09-05 23:40:38 +0800847#if UINT_MAX == 4294967295U
Denys Vlasenko64074a12018-12-07 15:50:14 +0100848# define BC_MAX_SCALE_STR "4294967295"
849# define BC_MAX_STRING_STR "4294967294"
Kang-Che Sungf1593522019-09-05 23:40:38 +0800850#elif UINT_MAX == 18446744073709551615U
Denys Vlasenko64074a12018-12-07 15:50:14 +0100851# define BC_MAX_SCALE_STR "18446744073709551615"
852# define BC_MAX_STRING_STR "18446744073709551614"
853#else
854# error Strange UINT_MAX
855#endif
856#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600857
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100858// In configurations where errors abort instead of propagating error
859// return code up the call chain, functions returning BC_STATUS
860// actually don't return anything, they always succeed and return "void".
861// A macro wrapper is provided, which makes this statement work:
862// s = zbc_func(...)
863// and makes it visible to the compiler that s is always zero,
864// allowing compiler to optimize dead code after the statement.
865//
866// To make code more readable, each such function has a "z"
867// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
868//
Denys Vlasenko14767602018-12-27 22:52:13 +0100869#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100870# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100871# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100872# define IF_ERROR_RETURN_POSSIBLE(a) a
873# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100874# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100875# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100876#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100877# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100878# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100879# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
880# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100881# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100882# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100883#endif
884
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100885//
886// Utility routines
887//
Gavin Howard01055ba2018-11-03 11:00:21 -0600888
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100889#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
890#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
891
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100892static void fflush_and_check(void)
893{
894 fflush_all();
895 if (ferror(stdout) || ferror(stderr))
James Byrne69374872019-07-02 11:35:03 +0200896 bb_simple_perror_msg_and_die("output error");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100897}
898
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100899#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100900#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100901do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100902 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100903 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100904 return BC_STATUS_FAILURE; \
905} while (0)
906#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100907static void quit(void) NORETURN;
908static void quit(void)
909{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100910 if (ferror(stdin))
James Byrne69374872019-07-02 11:35:03 +0200911 bb_simple_perror_msg_and_die("input error");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100912 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100913 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100914 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100915}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100916#define QUIT_OR_RETURN_TO_MAIN quit()
917#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100918
Denys Vlasenko5318f812018-12-05 17:48:01 +0100919static void bc_verror_msg(const char *fmt, va_list p)
920{
Denys Vlasenko82269122018-12-14 16:30:56 +0100921 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100922 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100923 sv = applet_name;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100924 applet_name = xasprintf("%s: %s:%lu", applet_name,
925 G.prs.lex_filename, (unsigned long)G.err_line
926 );
Denys Vlasenko5318f812018-12-05 17:48:01 +0100927 }
928 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100929 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100930 free((char*)applet_name);
931 applet_name = sv;
932 }
933}
934
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100935static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100936{
937 va_list p;
938
939 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100940 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100941 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100942
Denys Vlasenkoec603182018-12-17 10:34:02 +0100943 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
944 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
945 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100946}
947
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100948#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100949static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100950{
951 va_list p;
952
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100953 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100954 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100955 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100956
957 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100958 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100959 va_end(p);
960
961 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100962 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100963 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
964
Denys Vlasenkoec603182018-12-17 10:34:02 +0100965 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100966 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100967 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100968}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100969#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100970#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100971
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100972// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
973// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100974// function must not have caller-cleaned parameters on stack.
975// Unfortunately, vararg function API does exactly that on most arches.
976// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100977static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100978{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100979 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100980}
Denys Vlasenko22314682019-01-01 21:50:14 +0100981static ERRORFUNC int bc_error_at(const char *msg)
982{
983 const char *err_at = G.prs.lex_next_at;
984 if (err_at) {
985 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt(
986 "%s at '%.*s'",
987 msg,
988 (int)(strchrnul(err_at, '\n') - err_at),
989 err_at
990 );
991 }
992 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
993}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100994static ERRORFUNC int bc_error_bad_character(char c)
995{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100996 if (!c)
997 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100998 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100999}
Denys Vlasenko7a4e5542019-06-08 12:39:30 +02001000#if ENABLE_BC
Denys Vlasenko22314682019-01-01 21:50:14 +01001001static ERRORFUNC int bc_error_bad_function_definition(void)
1002{
1003 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition");
1004}
Denys Vlasenko7a4e5542019-06-08 12:39:30 +02001005#endif
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006static ERRORFUNC int bc_error_bad_expression(void)
1007{
Denys Vlasenko22314682019-01-01 21:50:14 +01001008 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression");
1009}
1010static ERRORFUNC int bc_error_bad_assignment(void)
1011{
1012 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at(
1013 "bad assignment: left side must be variable or array element"
1014 );
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001015}
1016static ERRORFUNC int bc_error_bad_token(void)
1017{
Denys Vlasenko22314682019-01-01 21:50:14 +01001018 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001019}
1020static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1021{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001022 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001023}
1024static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1025{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001026 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001027}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001028#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001029static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001030{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001031 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001032}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001033#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1034static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001035{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001036 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001037}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001038#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1039static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001040{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001041 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 +01001042}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001043#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)
1044static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001045{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001046 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001047}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001048#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 +01001049#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001050
Gavin Howard01055ba2018-11-03 11:00:21 -06001051static void bc_vec_grow(BcVec *v, size_t n)
1052{
1053 size_t cap = v->cap * 2;
1054 while (cap < v->len + n) cap *= 2;
1055 v->v = xrealloc(v->v, v->size * cap);
1056 v->cap = cap;
1057}
1058
1059static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1060{
1061 v->size = esize;
1062 v->cap = BC_VEC_START_CAP;
1063 v->len = 0;
1064 v->dtor = dtor;
1065 v->v = xmalloc(esize * BC_VEC_START_CAP);
1066}
1067
Denys Vlasenko7d628012018-12-04 21:46:47 +01001068static void bc_char_vec_init(BcVec *v)
1069{
1070 bc_vec_init(v, sizeof(char), NULL);
1071}
1072
Gavin Howard01055ba2018-11-03 11:00:21 -06001073static void bc_vec_expand(BcVec *v, size_t req)
1074{
1075 if (v->cap < req) {
1076 v->v = xrealloc(v->v, v->size * req);
1077 v->cap = req;
1078 }
1079}
1080
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001081static void bc_vec_pop(BcVec *v)
1082{
1083 v->len--;
1084 if (v->dtor)
1085 v->dtor(v->v + (v->size * v->len));
1086}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001087
Gavin Howard01055ba2018-11-03 11:00:21 -06001088static void bc_vec_npop(BcVec *v, size_t n)
1089{
1090 if (!v->dtor)
1091 v->len -= n;
1092 else {
1093 size_t len = v->len - n;
1094 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1095 }
1096}
1097
Denys Vlasenko7d628012018-12-04 21:46:47 +01001098static void bc_vec_pop_all(BcVec *v)
1099{
1100 bc_vec_npop(v, v->len);
1101}
1102
Denys Vlasenko53799502019-01-25 14:24:03 +01001103static size_t bc_vec_npush(BcVec *v, size_t n, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001104{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001105 size_t len = v->len;
Denys Vlasenko53799502019-01-25 14:24:03 +01001106 if (len + n > v->cap) bc_vec_grow(v, n);
1107 memmove(v->v + (v->size * len), data, v->size * n);
1108 v->len = len + n;
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001109 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001110}
1111
Denys Vlasenko53799502019-01-25 14:24:03 +01001112static size_t bc_vec_push(BcVec *v, const void *data)
1113{
1114 return bc_vec_npush(v, 1, data);
1115 //size_t len = v->len;
1116 //if (len >= v->cap) bc_vec_grow(v, 1);
1117 //memmove(v->v + (v->size * len), data, v->size);
1118 //v->len = len + 1;
1119 //return len;
1120}
1121
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001122// G.prog.results often needs "pop old operand, push result" idiom.
1123// Can do this without a few extra ops
1124static size_t bc_result_pop_and_push(const void *data)
1125{
1126 BcVec *v = &G.prog.results;
1127 char *last;
1128 size_t len = v->len - 1;
1129
1130 last = v->v + (v->size * len);
1131 if (v->dtor)
1132 v->dtor(last);
1133 memmove(last, data, v->size);
1134 return len;
1135}
1136
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001137static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001138{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001139 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001140}
1141
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001142static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001143{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001144 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001145 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001146 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001147}
1148
Gavin Howard01055ba2018-11-03 11:00:21 -06001149static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1150{
1151 if (idx == v->len)
1152 bc_vec_push(v, data);
1153 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001154 char *ptr;
1155
1156 if (v->len == v->cap) bc_vec_grow(v, 1);
1157
1158 ptr = v->v + v->size * idx;
1159
1160 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1161 memmove(ptr, data, v->size);
1162 }
1163}
1164
1165static void bc_vec_string(BcVec *v, size_t len, const char *str)
1166{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001167 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168 bc_vec_expand(v, len + 1);
1169 memcpy(v->v, str, len);
1170 v->len = len;
1171
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001172 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001173}
1174
Gavin Howard01055ba2018-11-03 11:00:21 -06001175static void *bc_vec_item(const BcVec *v, size_t idx)
1176{
1177 return v->v + v->size * idx;
1178}
1179
1180static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1181{
1182 return v->v + v->size * (v->len - idx - 1);
1183}
1184
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001185static void *bc_vec_top(const BcVec *v)
1186{
1187 return v->v + v->size * (v->len - 1);
1188}
1189
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001190static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001191{
1192 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001193 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001194 free(v->v);
1195}
1196
Denys Vlasenko10bde142018-12-27 18:23:58 +01001197static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001198{
1199 return bc_vec_item(&G.prog.fns, idx);
1200}
1201// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001202#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001203
1204#if ENABLE_BC
1205static BcFunc* bc_program_current_func(void)
1206{
1207 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001208 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001209 return func;
1210}
1211#endif
1212
Denys Vlasenko10bde142018-12-27 18:23:58 +01001213static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001214{
1215#if ENABLE_BC
1216 if (IS_BC) {
1217 BcFunc *func = bc_program_current_func();
1218 return bc_vec_item(&func->strs, idx);
1219 }
1220#endif
1221 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1222}
1223
Denys Vlasenko10bde142018-12-27 18:23:58 +01001224static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001225{
1226#if ENABLE_BC
1227 if (IS_BC) {
1228 BcFunc *func = bc_program_current_func();
1229 return bc_vec_item(&func->consts, idx);
1230 }
1231#endif
1232 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1233}
1234
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001235static int bc_id_cmp(const void *e1, const void *e2)
1236{
1237 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1238}
1239
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001240static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001241{
1242 free(((BcId *) id)->name);
1243}
1244
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001245static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001246{
1247 size_t low = 0, high = v->len;
1248
1249 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001250 size_t mid = (low + high) / 2;
1251 BcId *id = bc_vec_item(v, mid);
1252 int result = bc_id_cmp(ptr, id);
1253
1254 if (result == 0)
1255 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001256 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001257 high = mid;
1258 else
1259 low = mid + 1;
1260 }
1261
1262 return low;
1263}
1264
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001265static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001266{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001267 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001268
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001270 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001271 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1272 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001273 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001274 bc_vec_pushAt(v, ptr, n);
1275 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001276}
1277
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001278static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001279{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001280 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001281 if (i >= v->len) return BC_VEC_INVALID_IDX;
1282 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1283}
1284
Gavin Howard01055ba2018-11-03 11:00:21 -06001285static void bc_num_setToZero(BcNum *n, size_t scale)
1286{
1287 n->len = 0;
1288 n->neg = false;
1289 n->rdx = scale;
1290}
1291
1292static void bc_num_zero(BcNum *n)
1293{
1294 bc_num_setToZero(n, 0);
1295}
1296
1297static void bc_num_one(BcNum *n)
1298{
1299 bc_num_setToZero(n, 0);
1300 n->len = 1;
1301 n->num[0] = 1;
1302}
1303
Denys Vlasenko3129f702018-12-09 12:04:44 +01001304// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001305static void bc_num_init(BcNum *n, size_t req)
1306{
1307 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001308 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001309 n->num = xmalloc(req);
1310 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001311 n->rdx = 0;
1312 n->len = 0;
1313 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001314}
1315
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001316static void bc_num_init_DEF_SIZE(BcNum *n)
1317{
1318 bc_num_init(n, BC_NUM_DEF_SIZE);
1319}
1320
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001321static void bc_num_expand(BcNum *n, size_t req)
1322{
1323 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1324 if (req > n->cap) {
1325 n->num = xrealloc(n->num, req);
1326 n->cap = req;
1327 }
1328}
1329
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001330static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001331{
1332 free(((BcNum *) num)->num);
1333}
1334
1335static void bc_num_copy(BcNum *d, BcNum *s)
1336{
1337 if (d != s) {
1338 bc_num_expand(d, s->cap);
1339 d->len = s->len;
1340 d->neg = s->neg;
1341 d->rdx = s->rdx;
1342 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1343 }
1344}
1345
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001346static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347{
1348 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001349 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001350
Denys Vlasenko1557b762018-12-22 21:37:46 +01001351 result = 0;
1352 i = n->len;
1353 while (i > n->rdx) {
1354 unsigned long prev = result;
1355 result = result * 10 + n->num[--i];
1356 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1357 // 0x1ff00000 * 10 is 0x13f600000,
1358 // or 0x3f600000 truncated to 32 bits. Which is larger.
1359 // However, (N*10)/8 < N check is always correct.
1360 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001361 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001363 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001364
Denys Vlasenko29301232018-12-11 15:29:32 +01001365 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001366}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001367#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1368
1369static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1370{
1371 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1372
1373 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1374}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001375#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001376
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001377#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1378# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1379#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1380# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1381#endif
1382// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1383// would not hit realloc() code path - not good if num[] is not malloced
1384
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001385static void bc_num_ulong2num(BcNum *n, unsigned long val)
1386{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001387 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001388
1389 bc_num_zero(n);
1390
1391 if (val == 0) return;
1392
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001393 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001394
1395 ptr = n->num;
1396 for (;;) {
1397 n->len++;
1398 *ptr++ = val % 10;
1399 val /= 10;
1400 if (val == 0) break;
1401 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001402}
1403
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001404static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001405{
1406 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001407 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001408 a[i] -= b[i];
1409 for (j = i; a[j] < 0;) {
1410 a[j++] += 10;
1411 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001412 }
1413 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001414}
1415
1416static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1417{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001418 size_t i = len;
1419 for (;;) {
1420 int c;
1421 if (i == 0)
1422 return 0;
1423 i--;
1424 c = a[i] - b[i];
1425 if (c != 0) {
1426 i++;
1427 if (c < 0)
1428 return -i;
1429 return i;
1430 }
1431 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001432}
1433
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001434#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1435#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1436#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1437//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1438static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1439{
1440 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1441}
1442//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1443static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1444{
1445 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1446}
1447
Gavin Howard01055ba2018-11-03 11:00:21 -06001448static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1449{
1450 size_t i, min, a_int, b_int, diff;
1451 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001452 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001453 ssize_t cmp;
1454
1455 if (a == b) return 0;
1456 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1457 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001458
1459 if (a->neg != b->neg) // signs of a and b differ
1460 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1461 return (int)b->neg - (int)a->neg;
1462 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001463
1464 a_int = BC_NUM_INT(a);
1465 b_int = BC_NUM_INT(b);
1466 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001467
Denys Vlasenko00841372019-11-23 17:25:21 +01001468 if (a_int != 0) {
1469 if (neg) return - (ssize_t) a_int;
1470 return (ssize_t) a_int;
1471 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001472
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001473 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001474 if (a_max) {
1475 min = b->rdx;
1476 diff = a->rdx - b->rdx;
1477 max_num = a->num + diff;
1478 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001479 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1480 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001481 min = a->rdx;
1482 diff = b->rdx - a->rdx;
1483 max_num = b->num + diff;
1484 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001485 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001486 }
1487
1488 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001489 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001490
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001491 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001492 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001493 }
1494
1495 return 0;
1496}
1497
1498static void bc_num_truncate(BcNum *n, size_t places)
1499{
1500 if (places == 0) return;
1501
1502 n->rdx -= places;
1503
1504 if (n->len != 0) {
1505 n->len -= places;
1506 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1507 }
1508}
1509
1510static void bc_num_extend(BcNum *n, size_t places)
1511{
1512 size_t len = n->len + places;
1513
1514 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001515 if (n->cap < len) bc_num_expand(n, len);
1516
1517 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1518 memset(n->num, 0, sizeof(BcDig) * places);
1519
1520 n->len += places;
1521 n->rdx += places;
1522 }
1523}
1524
1525static void bc_num_clean(BcNum *n)
1526{
1527 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1528 if (n->len == 0)
1529 n->neg = false;
1530 else if (n->len < n->rdx)
1531 n->len = n->rdx;
1532}
1533
1534static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1535{
1536 if (n->rdx < scale)
1537 bc_num_extend(n, scale - n->rdx);
1538 else
1539 bc_num_truncate(n, n->rdx - scale);
1540
1541 bc_num_clean(n);
1542 if (n->len != 0) n->neg = !neg1 != !neg2;
1543}
1544
1545static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1546 BcNum *restrict b)
1547{
1548 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001549 b->len = n->len - idx;
1550 a->len = idx;
1551 a->rdx = b->rdx = 0;
1552
1553 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1554 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001555 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001556 bc_num_zero(b);
1557 bc_num_copy(a, n);
1558 }
1559
1560 bc_num_clean(a);
1561 bc_num_clean(b);
1562}
1563
Denys Vlasenko29301232018-12-11 15:29:32 +01001564static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001565{
Denys Vlasenko29301232018-12-11 15:29:32 +01001566 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001567
1568 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1569 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1570 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001571 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001572 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001573
1574 if (n->rdx >= places)
1575 n->rdx -= places;
1576 else {
1577 bc_num_extend(n, places - n->rdx);
1578 n->rdx = 0;
1579 }
1580
1581 bc_num_clean(n);
1582
Denys Vlasenko29301232018-12-11 15:29:32 +01001583 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001584}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001585#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001586
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001587typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1588
1589static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1590 BcNumBinaryOp op, size_t req)
1591{
1592 BcStatus s;
1593 BcNum num2, *ptr_a, *ptr_b;
1594 bool init = false;
1595
1596 if (c == a) {
1597 ptr_a = &num2;
1598 memcpy(ptr_a, c, sizeof(BcNum));
1599 init = true;
1600 } else
1601 ptr_a = a;
1602
1603 if (c == b) {
1604 ptr_b = &num2;
1605 if (c != a) {
1606 memcpy(ptr_b, c, sizeof(BcNum));
1607 init = true;
1608 }
1609 } else
1610 ptr_b = b;
1611
1612 if (init)
1613 bc_num_init(c, req);
1614 else
1615 bc_num_expand(c, req);
1616
1617 s = BC_STATUS_SUCCESS;
1618 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1619
1620 if (init) bc_num_free(&num2);
1621
1622 RETURN_STATUS(s);
1623}
1624#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1625
1626static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1627static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1628static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1629static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1630static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1631static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1632
1633static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1634{
1635 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1636 (void) scale;
1637 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1638}
1639
1640static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1641{
1642 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1643 (void) scale;
1644 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1645}
1646
1647static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1648{
1649 size_t req = BC_NUM_MREQ(a, b, scale);
1650 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1651}
1652
1653static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1654{
1655 size_t req = BC_NUM_MREQ(a, b, scale);
1656 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1657}
1658
1659static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1660{
1661 size_t req = BC_NUM_MREQ(a, b, scale);
1662 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1663}
1664
1665static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1666{
1667 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1668}
1669
Denys Vlasenko10bde142018-12-27 18:23:58 +01001670static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001671 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1672};
1673#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1674#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1675#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1676#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1677#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1678#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1679
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001680static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001681{
1682 BcNum one;
1683 BcDig num[2];
1684
1685 one.cap = 2;
1686 one.num = num;
1687 bc_num_one(&one);
1688
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001689 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001690}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001691#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001692
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001693static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001694{
1695 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1696 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001697 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001698
1699 // Because this function doesn't need to use scale (per the bc spec),
1700 // I am hijacking it to say whether it's doing an add or a subtract.
1701
1702 if (a->len == 0) {
1703 bc_num_copy(c, b);
1704 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001705 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001706 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001707 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001708 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001709 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001710 }
1711
1712 c->neg = a->neg;
1713 c->rdx = BC_MAX(a->rdx, b->rdx);
1714 min_rdx = BC_MIN(a->rdx, b->rdx);
1715 c->len = 0;
1716
1717 if (a->rdx > b->rdx) {
1718 diff = a->rdx - b->rdx;
1719 ptr = a->num;
1720 ptr_a = a->num + diff;
1721 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001722 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001723 diff = b->rdx - a->rdx;
1724 ptr = b->num;
1725 ptr_a = a->num;
1726 ptr_b = b->num + diff;
1727 }
1728
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001729 ptr_c = c->num;
1730 for (i = 0; i < diff; ++i, ++c->len)
1731 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001732
1733 ptr_c += diff;
1734 a_int = BC_NUM_INT(a);
1735 b_int = BC_NUM_INT(b);
1736
1737 if (a_int > b_int) {
1738 min_int = b_int;
1739 max = a_int;
1740 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001741 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001742 min_int = a_int;
1743 max = b_int;
1744 ptr = ptr_b;
1745 }
1746
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001747 carry = 0;
1748 for (i = 0; i < min_rdx + min_int; ++i) {
1749 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001750 carry = in / 10;
1751 ptr_c[i] = (BcDig)(in % 10);
1752 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001753 for (; i < max + min_rdx; ++i) {
1754 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001755 carry = in / 10;
1756 ptr_c[i] = (BcDig)(in % 10);
1757 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001758 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001759
1760 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1761
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001763}
1764
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001765static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001766{
Gavin Howard01055ba2018-11-03 11:00:21 -06001767 ssize_t cmp;
1768 BcNum *minuend, *subtrahend;
1769 size_t start;
1770 bool aneg, bneg, neg;
1771
1772 // Because this function doesn't need to use scale (per the bc spec),
1773 // I am hijacking it to say whether it's doing an add or a subtract.
1774
1775 if (a->len == 0) {
1776 bc_num_copy(c, b);
1777 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001778 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001779 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001780 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001781 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001782 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001783 }
1784
1785 aneg = a->neg;
1786 bneg = b->neg;
1787 a->neg = b->neg = false;
1788
1789 cmp = bc_num_cmp(a, b);
1790
1791 a->neg = aneg;
1792 b->neg = bneg;
1793
1794 if (cmp == 0) {
1795 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001796 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001798 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001799 neg = a->neg;
1800 minuend = a;
1801 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001802 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 neg = b->neg;
1804 if (sub) neg = !neg;
1805 minuend = b;
1806 subtrahend = a;
1807 }
1808
1809 bc_num_copy(c, minuend);
1810 c->neg = neg;
1811
1812 if (c->rdx < subtrahend->rdx) {
1813 bc_num_extend(c, subtrahend->rdx - c->rdx);
1814 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001815 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 start = c->rdx - subtrahend->rdx;
1817
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001818 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001819
1820 bc_num_clean(c);
1821
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001822 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001823}
1824
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001825static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001826 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001827#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001828{
1829 BcStatus s;
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01001830 size_t max, max2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001832 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001833
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 if (a->len == 0 || b->len == 0) {
1835 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001836 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001838 aone = BC_NUM_ONE(a);
1839 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001840 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 }
1843
Denys Vlasenkocc4303f2021-01-06 14:00:53 +01001844 if (a->len < BC_NUM_KARATSUBA_LEN
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001845 || b->len < BC_NUM_KARATSUBA_LEN
Denys Vlasenkocc4303f2021-01-06 14:00:53 +01001846 /* || a->len + b->len < BC_NUM_KARATSUBA_LEN - redundant check */
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001847 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001848 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001849
Gavin Howard01055ba2018-11-03 11:00:21 -06001850 bc_num_expand(c, a->len + b->len + 1);
1851
1852 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001853 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001854
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001855 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001856 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001857 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001858 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001859 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001860 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001861 carry = in / 10;
1862 c->num[i + j] = (BcDig)(in % 10);
1863 }
1864
1865 c->num[i + j] += (BcDig) carry;
1866 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001867
Denys Vlasenko14767602018-12-27 22:52:13 +01001868#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001869 // a=2^1000000
1870 // a*a <- without check below, this will not be interruptible
1871 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001872#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 }
1874
1875 c->len = len;
1876
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001877 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 }
1879
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01001880 max = BC_MAX(a->len, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 bc_num_init(&l1, max);
1882 bc_num_init(&h1, max);
1883 bc_num_init(&l2, max);
1884 bc_num_init(&h2, max);
1885 bc_num_init(&m1, max);
1886 bc_num_init(&m2, max);
1887 bc_num_init(&z0, max);
1888 bc_num_init(&z1, max);
1889 bc_num_init(&z2, max);
1890 bc_num_init(&temp, max + max);
1891
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01001892 max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 bc_num_split(a, max2, &l1, &h1);
1894 bc_num_split(b, max2, &l2, &h2);
1895
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001897 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001898 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 if (s) goto err;
1900
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001901 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001902 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001903 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001905 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001906 if (s) goto err;
1907
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001908 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001910 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001911 if (s) goto err;
1912
Denys Vlasenko29301232018-12-11 15:29:32 +01001913 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001914 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001915 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001917 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001918 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001919 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001920 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001921 bc_num_free(&temp);
1922 bc_num_free(&z2);
1923 bc_num_free(&z1);
1924 bc_num_free(&z0);
1925 bc_num_free(&m2);
1926 bc_num_free(&m1);
1927 bc_num_free(&h2);
1928 bc_num_free(&l2);
1929 bc_num_free(&h1);
1930 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001931 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001932}
1933
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001934static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001935{
1936 BcStatus s;
1937 BcNum cpa, cpb;
1938 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1939
1940 scale = BC_MAX(scale, a->rdx);
1941 scale = BC_MAX(scale, b->rdx);
1942 scale = BC_MIN(a->rdx + b->rdx, scale);
1943 maxrdx = BC_MAX(maxrdx, scale);
1944
1945 bc_num_init(&cpa, a->len);
1946 bc_num_init(&cpb, b->len);
1947
1948 bc_num_copy(&cpa, a);
1949 bc_num_copy(&cpb, b);
1950 cpa.neg = cpb.neg = false;
1951
Denys Vlasenko29301232018-12-11 15:29:32 +01001952 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001953 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001954 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001955 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001956 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001957 if (s) goto err;
1958
1959 maxrdx += scale;
1960 bc_num_expand(c, c->len + maxrdx);
1961
1962 if (c->len < maxrdx) {
1963 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1964 c->len += maxrdx;
1965 }
1966
1967 c->rdx = maxrdx;
1968 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001969 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001970 bc_num_free(&cpb);
1971 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001972 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001973}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001974#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001975
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001976static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001977{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001978 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001979 size_t len, end, i;
1980 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001981
1982 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001983 RETURN_STATUS(bc_error("divide by zero"));
1984 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001985 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001986 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001987 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001988 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001989 bc_num_copy(c, a);
1990 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001991 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001992 }
1993
1994 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1995 bc_num_copy(&cp, a);
1996 len = b->len;
1997
1998 if (len > cp.len) {
1999 bc_num_expand(&cp, len + 2);
2000 bc_num_extend(&cp, len - cp.len);
2001 }
2002
2003 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2004 cp.rdx -= b->rdx;
2005 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2006
2007 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002008 for (;;) {
2009 if (len == 0) break;
2010 len--;
2011 if (b->num[len] != 0)
2012 break;
2013 }
2014 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002015 }
2016
2017 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2018
2019 // We want an extra zero in front to make things simpler.
2020 cp.num[cp.len++] = 0;
2021 end = cp.len - len;
2022
2023 bc_num_expand(c, cp.len);
2024
2025 bc_num_zero(c);
2026 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2027 c->rdx = cp.rdx;
2028 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002029
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002030 s = BC_STATUS_SUCCESS;
2031 for (i = end - 1; i < end; --i) {
2032 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002033 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002034 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2035 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002036 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01002037#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01002038 // a=2^100000
2039 // scale=40000
2040 // 1/a <- without check below, this will not be interruptible
2041 if (G_interrupt) {
2042 s = BC_STATUS_FAILURE;
2043 break;
2044 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002045#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002046 }
2047
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002048 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002049 bc_num_free(&cp);
2050
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002051 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002052}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002053#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002054
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002055static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002056 BcNum *restrict d, size_t scale, size_t ts)
2057{
2058 BcStatus s;
2059 BcNum temp;
2060 bool neg;
2061
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002062 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002063 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002064
2065 if (a->len == 0) {
2066 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002067 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002068 }
2069
2070 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002071 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002072 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002073
2074 if (scale != 0) scale = ts;
2075
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002076 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002077 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002078 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002079 if (s) goto err;
2080
2081 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2082
2083 neg = d->neg;
2084 bc_num_retireMul(d, ts, a->neg, b->neg);
2085 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002086 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002087 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002088 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002089}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002090#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002091
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002092static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002093{
2094 BcStatus s;
2095 BcNum c1;
2096 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2097
2098 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002099 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002100 bc_num_free(&c1);
2101
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002102 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002103}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002104#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002105
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002106static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002107{
2108 BcStatus s = BC_STATUS_SUCCESS;
2109 BcNum copy;
2110 unsigned long pow;
2111 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002112 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002113
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002114 // GNU bc does not allow 2^2.0 - we do
2115 for (i = 0; i < b->rdx; i++)
2116 if (b->num[i] != 0)
2117 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002118
2119 if (b->len == 0) {
2120 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002121 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002123 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002124 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002125 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002126 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002127 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 if (!b->neg)
2129 bc_num_copy(c, a);
2130 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002131 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002132 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002133 }
2134
2135 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002136 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002137 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002138 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002139
2140 bc_num_init(&copy, a->len);
2141 bc_num_copy(&copy, a);
2142
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002143 if (!neg) {
2144 if (a->rdx > scale)
2145 scale = a->rdx;
2146 if (a->rdx * pow < scale)
2147 scale = a->rdx * pow;
2148 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002149
Gavin Howard01055ba2018-11-03 11:00:21 -06002150
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002151 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002153 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002154 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002155 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002156 //if (G_interrupt) {
2157 // s = BC_STATUS_FAILURE;
2158 // goto err;
2159 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002160 }
2161
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 bc_num_copy(c, &copy);
2163
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002164 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002165 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002166 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002167 if (s) goto err;
2168
2169 if (pow & 1) {
2170 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002171 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002172 if (s) goto err;
2173 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002174 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002175 //if (G_interrupt) {
2176 // s = BC_STATUS_FAILURE;
2177 // goto err;
2178 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002179 }
2180
2181 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002182 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002183 if (s) goto err;
2184 }
2185
Gavin Howard01055ba2018-11-03 11:00:21 -06002186 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2187
2188 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002189 for (i = 0; i < c->len; ++i)
2190 if (c->num[i] != 0)
2191 goto skip;
2192 bc_num_setToZero(c, scale);
2193 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002194
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002195 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002196 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002197 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002198}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002199#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002200
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002201static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002202{
2203 BcStatus s;
2204 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002205 BcDig half_digs[1];
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002206 size_t pow, len, digs, digs1, resrdx, req, times;
2207 ssize_t cmp, cmp1, cmp2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002208
2209 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2210 bc_num_expand(b, req);
2211
2212 if (a->len == 0) {
2213 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002214 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002215 }
2216 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002217 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002218 }
2219 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002220 bc_num_one(b);
2221 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002222 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002223 }
2224
2225 scale = BC_MAX(scale, a->rdx) + 1;
2226 len = a->len + scale;
2227
2228 bc_num_init(&num1, len);
2229 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002230
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002231 half.cap = ARRAY_SIZE(half_digs);
2232 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002233 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002234 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002235 half.rdx = 1;
2236
2237 bc_num_init(&f, len);
2238 bc_num_init(&fprime, len);
2239
2240 x0 = &num1;
2241 x1 = &num2;
2242
2243 bc_num_one(x0);
2244 pow = BC_NUM_INT(a);
2245
2246 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002247 if (pow & 1)
2248 x0->num[0] = 2;
2249 else
2250 x0->num[0] = 6;
2251
2252 pow -= 2 - (pow & 1);
2253
2254 bc_num_extend(x0, pow);
2255
2256 // Make sure to move the radix back.
2257 x0->rdx -= pow;
2258 }
2259
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002260 x0->rdx = digs = digs1 = times = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002261 resrdx = scale + 2;
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002262 len = x0->len + resrdx - 1;
2263 cmp = 1;
2264 cmp1 = cmp2 = SSIZE_MAX;
2265 do {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002266 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002267 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002268 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002269 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002270 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002271 if (s) goto err;
2272
2273 cmp = bc_num_cmp(x1, x0);
2274 digs = x1->len - (unsigned long long) llabs(cmp);
2275
2276 if (cmp == cmp2 && digs == digs1)
2277 times += 1;
2278 else
2279 times = 0;
2280
2281 resrdx += times > 4;
2282
2283 cmp2 = cmp1;
2284 cmp1 = cmp;
2285 digs1 = digs;
2286
2287 temp = x0;
2288 x0 = x1;
2289 x1 = temp;
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002290 } while (cmp != 0 || digs < len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002291
Gavin Howard01055ba2018-11-03 11:00:21 -06002292 bc_num_copy(b, x0);
2293 scale -= 1;
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002294 if (b->rdx > scale)
2295 bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002296 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002297 bc_num_free(&fprime);
2298 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002299 bc_num_free(&num2);
2300 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002301 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002302}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002303#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002304
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002305static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002306 size_t scale)
2307{
2308 BcStatus s;
2309 BcNum num2, *ptr_a;
2310 bool init = false;
2311 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2312
2313 if (c == a) {
2314 memcpy(&num2, c, sizeof(BcNum));
2315 ptr_a = &num2;
2316 bc_num_init(c, len);
2317 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002318 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002319 ptr_a = a;
2320 bc_num_expand(c, len);
2321 }
2322
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002323 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002324
2325 if (init) bc_num_free(&num2);
2326
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002327 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002328}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002329#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002330
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002331#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002332static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002333{
2334 BcStatus s;
2335 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002336 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002337
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002338 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002339 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002340 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002341 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002342 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002343 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002344
2345 bc_num_expand(d, c->len);
2346 bc_num_init(&base, c->len);
2347 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002348 bc_num_init(&temp, b->len);
2349
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002350 two.cap = ARRAY_SIZE(two_digs);
2351 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002352 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002353 two_digs[0] = 2;
2354
Gavin Howard01055ba2018-11-03 11:00:21 -06002355 bc_num_one(d);
2356
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002357 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002358 if (s) goto err;
2359 bc_num_copy(&exp, b);
2360
2361 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002362 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002363 if (s) goto err;
2364
2365 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002366 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002367 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002368 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002369 if (s) goto err;
2370 }
2371
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002372 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002373 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002374 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002375 if (s) goto err;
2376 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002377 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002378 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002379 bc_num_free(&exp);
2380 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002381 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002382}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002383#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002384#endif // ENABLE_DC
2385
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002386static FAST_FUNC void bc_string_free(void *string)
2387{
2388 free(*(char**)string);
2389}
2390
Gavin Howard01055ba2018-11-03 11:00:21 -06002391static void bc_func_init(BcFunc *f)
2392{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002393 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002394 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2395 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002396 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2397 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002398 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002399}
2400
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002401static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002402{
2403 BcFunc *f = (BcFunc *) func;
2404 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002405 IF_BC(bc_vec_free(&f->labels);)
2406 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002407 IF_BC(bc_vec_free(&f->strs);)
2408 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002409}
2410
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002411static void bc_array_expand(BcVec *a, size_t len);
2412
Gavin Howard01055ba2018-11-03 11:00:21 -06002413static void bc_array_init(BcVec *a, bool nums)
2414{
2415 if (nums)
2416 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2417 else
2418 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2419 bc_array_expand(a, 1);
2420}
2421
Gavin Howard01055ba2018-11-03 11:00:21 -06002422static void bc_array_expand(BcVec *a, size_t len)
2423{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002424 if (a->dtor == bc_num_free
2425 // && a->size == sizeof(BcNum) - always true
2426 ) {
2427 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002428 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002429 bc_num_init_DEF_SIZE(&n);
2430 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002431 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002432 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002433 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002434 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002435 bc_array_init(&v, true);
2436 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002437 }
2438 }
2439}
2440
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002441static void bc_array_copy(BcVec *d, const BcVec *s)
2442{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002443 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002444 size_t i;
2445
2446 bc_vec_pop_all(d);
2447 bc_vec_expand(d, s->cap);
2448 d->len = s->len;
2449
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002450 dnum = (void*)d->v;
2451 snum = (void*)s->v;
2452 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002453 bc_num_init(dnum, snum->len);
2454 bc_num_copy(dnum, snum);
2455 }
2456}
2457
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002458#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002459static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002460{
2461 d->t = src->t;
2462
2463 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002464 case XC_RESULT_TEMP:
2465 case XC_RESULT_IBASE:
2466 case XC_RESULT_SCALE:
2467 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 bc_num_init(&d->d.n, src->d.n.len);
2469 bc_num_copy(&d->d.n, &src->d.n);
2470 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002471 case XC_RESULT_VAR:
2472 case XC_RESULT_ARRAY:
2473 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002474 d->d.id.name = xstrdup(src->d.id.name);
2475 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002476 case XC_RESULT_CONSTANT:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002477 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002478 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2479 break;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002480 default: // placate compiler
2481 // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen
2482 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002483 }
2484}
2485#endif // ENABLE_DC
2486
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002487static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002488{
2489 BcResult *r = (BcResult *) result;
2490
2491 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002492 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002493 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002494 case XC_RESULT_IBASE:
2495 case XC_RESULT_SCALE:
2496 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002497 bc_num_free(&r->d.n);
2498 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002499 case XC_RESULT_VAR:
2500 case XC_RESULT_ARRAY:
2501 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002502 free(r->d.id.name);
2503 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002504 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002505 // Do nothing.
2506 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002507 }
2508}
2509
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002510static int bad_input_byte(char c)
2511{
2512 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2513 || c > 0x7e
2514 ) {
2515 bc_error_fmt("illegal character 0x%02x", c);
2516 return 1;
2517 }
2518 return 0;
2519}
2520
Denys Vlasenko10bde142018-12-27 18:23:58 +01002521static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002522{
2523 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002524 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002525 fflush_and_check();
2526
Denys Vlasenko14767602018-12-27 22:52:13 +01002527#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002528 if (G_interrupt) { // ^C was pressed
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002529 if (fp != stdin) {
2530 // ^C while running a script (bc SCRIPT): die.
2531 // We do not return to interactive prompt:
2532 // user might be running us from a shell,
2533 // and SCRIPT might be intended to terminate
2534 // (e.g. contain a "halt" stmt).
2535 // ^C dropping user into a bc prompt instead of
2536 // the shell would be unexpected.
2537 xfunc_die();
2538 }
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002539 // There was ^C while running calculations
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002540 G_interrupt = 0;
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002541 // GNU bc says "interrupted execution." (to stdout, not stderr)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002542 // GNU dc says "Interrupt!"
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002543 puts("\ninterrupted execution");
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002544 }
2545
2546# if ENABLE_FEATURE_EDITING
2547 if (G_ttyin && fp == stdin) {
2548 int n, i;
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01002549 if (!G.line_input_state)
2550 G.line_input_state = new_line_input_t(DO_HISTORY);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002551# define line_buf bb_common_bufsiz1
2552 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2553 if (n <= 0) { // read errors or EOF, or ^D, or ^C
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002554 //GNU bc prints this on ^C:
2555 //if (n == 0) // ^C
2556 // puts("(interrupt) Exiting bc.");
2557 bc_vec_pushZeroByte(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002558 return;
2559 }
2560 i = 0;
2561 for (;;) {
2562 char c = line_buf[i++];
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002563 if (c == '\0') break;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002564 if (bad_input_byte(c)) goto again;
2565 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002566 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002567# undef line_buf
2568 } else
2569# endif
2570#endif
2571 {
2572 int c;
2573 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002574
2575 do {
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002576 get_char:
Denys Vlasenko14767602018-12-27 22:52:13 +01002577#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002578 if (G_interrupt) {
2579 // ^C was pressed: ignore entire line, get another one
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002580 goto again;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002581 }
2582#endif
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002583 c = fgetc(fp);
2584 if (c == '\0')
2585 goto get_char;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002586 if (c == EOF) {
2587 if (ferror(fp))
James Byrne69374872019-07-02 11:35:03 +02002588 bb_simple_perror_msg_and_die("input error");
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002589 // Note: EOF does not append '\n'
2590 break;
2591 }
2592 bad_chars |= bad_input_byte(c);
2593 bc_vec_pushByte(vec, (char)c);
2594 } while (c != '\n');
2595
2596 if (bad_chars) {
2597 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002598 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002599 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002600 goto again;
2601 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002602 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002603 }
2604 bc_vec_pushZeroByte(vec);
2605 }
2606}
2607
2608//
2609// Parsing routines
2610//
2611
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002612// "Input numbers may contain the characters 0-9 and A-Z.
2613// (Note: They must be capitals. Lower case letters are variable names.)
2614// Single digit numbers always have the value of the digit regardless of
2615// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2616// all input digits greater or equal to ibase to the value of ibase-1.
2617// This makes the number ZZZ always be the largest 3 digit number of the
2618// input base."
2619static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002620{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002621 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002622 for (;;) {
2623 BcDig c = *val++;
2624 if (c == '\0')
2625 break;
2626 if (c == '.') {
2627 if (radix) return false;
2628 radix = true;
2629 continue;
2630 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002631 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002632 return false;
2633 }
2634 return true;
2635}
2636
2637// Note: n is already "bc_num_zero()"ed,
2638// leading zeroes in "val" are removed
2639static void bc_num_parseDecimal(BcNum *n, const char *val)
2640{
2641 size_t len, i;
2642 const char *ptr;
2643
2644 len = strlen(val);
2645 if (len == 0)
2646 return;
2647
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002648 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002649
2650 ptr = strchr(val, '.');
2651
2652 n->rdx = 0;
2653 if (ptr != NULL)
2654 n->rdx = (size_t)((val + len) - (ptr + 1));
2655
2656 for (i = 0; val[i]; ++i) {
2657 if (val[i] != '0' && val[i] != '.') {
2658 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002659 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002660 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002661 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002662 if (c > 9) { // A-Z => 10-36
2663 n->len = 2;
2664 c -= ('A' - '9' - 1);
2665 n->num[1] = c/10;
2666 c = c%10;
2667 }
2668 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002669 break;
2670 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002671 i = len - 1;
2672 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002673 char c = val[i] - '0';
2674 if (c > 9) // A-Z => 9
2675 c = 9;
2676 n->num[n->len] = c;
2677 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002678 skip_dot:
2679 if (i == 0) break;
2680 if (val[--i] == '.') goto skip_dot;
2681 }
2682 break;
2683 }
2684 }
2685 // if for() exits without hitting if(), the value is entirely zero
2686}
2687
2688// Note: n is already "bc_num_zero()"ed,
2689// leading zeroes in "val" are removed
2690static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2691{
2692 BcStatus s;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002693 BcNum mult, result;
2694 BcNum temp;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002695 BcNum base;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002696 BcDig temp_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002697 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002698 size_t digits;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002699
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002700 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002701
2702 temp.cap = ARRAY_SIZE(temp_digs);
2703 temp.num = temp_digs;
2704
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002705 base.cap = ARRAY_SIZE(base_digs);
2706 base.num = base_digs;
2707 bc_num_ulong2num(&base, base_t);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002708 base_t--;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002709
2710 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002711 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002712 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002713
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002714 c = *val++;
2715 if (c == '\0') goto int_err;
2716 if (c == '.') break;
2717
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002718 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2719 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002720
2721 s = zbc_num_mul(n, &base, &mult, 0);
2722 if (s) goto int_err;
2723 bc_num_ulong2num(&temp, v);
2724 s = zbc_num_add(&mult, &temp, n, 0);
2725 if (s) goto int_err;
2726 }
2727
2728 bc_num_init(&result, base.len);
2729 //bc_num_zero(&result); - already is
2730 bc_num_one(&mult);
2731
2732 digits = 0;
2733 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002734 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002735 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002736
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002737 c = *val++;
2738 if (c == '\0') break;
2739 digits++;
2740
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002741 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2742 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002743
2744 s = zbc_num_mul(&result, &base, &result, 0);
2745 if (s) goto err;
2746 bc_num_ulong2num(&temp, v);
2747 s = zbc_num_add(&result, &temp, &result, 0);
2748 if (s) goto err;
2749 s = zbc_num_mul(&mult, &base, &mult, 0);
2750 if (s) goto err;
2751 }
2752
2753 s = zbc_num_div(&result, &mult, &result, digits);
2754 if (s) goto err;
2755 s = zbc_num_add(n, &result, n, digits);
2756 if (s) goto err;
2757
2758 if (n->len != 0) {
2759 if (n->rdx < digits)
2760 bc_num_extend(n, digits - n->rdx);
2761 } else
2762 bc_num_zero(n);
2763 err:
2764 bc_num_free(&result);
2765 int_err:
2766 bc_num_free(&mult);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002767}
2768
Denys Vlasenko10bde142018-12-27 18:23:58 +01002769static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002770{
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002771 size_t i;
2772
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002773 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002774 RETURN_STATUS(bc_error("bad number string"));
2775
2776 bc_num_zero(n);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002777 while (*val == '0')
2778 val++;
2779 for (i = 0; ; ++i) {
2780 if (val[i] == '\0')
2781 RETURN_STATUS(BC_STATUS_SUCCESS);
2782 if (val[i] != '.' && val[i] != '0')
2783 break;
2784 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002785
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002786 if (base_t == 10 || val[1] == '\0')
2787 // Decimal, or single-digit number
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002788 bc_num_parseDecimal(n, val);
2789 else
2790 bc_num_parseBase(n, val, base_t);
2791
2792 RETURN_STATUS(BC_STATUS_SUCCESS);
2793}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002794#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002795
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002796// p->lex_inbuf points to the current string to be parsed.
2797// if p->lex_inbuf points to '\0', it's either EOF or it points after
2798// last processed line's terminating '\n' (and more reading needs to be done
2799// to get next character).
2800//
2801// If you are in a situation where that is a possibility, call peek_inbuf().
2802// If necessary, it performs more reading and changes p->lex_inbuf,
2803// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2804// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2805// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2806//
2807// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2808// it returns current char and advances the pointer (if not EOF).
2809// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2810//
2811// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2812// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2813// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002814//
2815// bc cases to test interactively:
2816// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2817// 1 #comment/* - prints "1<newline>" at once
2818// 1 #comment" - prints "1<newline>" at once
2819// 1\#comment - error at once (\ is not a line continuation)
2820// 1 + /*"*/2 - prints "3<newline>" at once
2821// 1 + /*#*/2 - prints "3<newline>" at once
2822// "str\" - prints "str\" at once
2823// "str#" - prints "str#" at once
2824// "str/*" - prints "str/*" at once
2825// "str#\ - waits for second line
2826// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002827static char peek_inbuf(void)
2828{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002829 if (*G.prs.lex_inbuf == '\0'
2830 && G.prs.lex_input_fp
2831 ) {
2832 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2833 G.prs.lex_inbuf = G.input_buffer.v;
2834 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2835 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002836 }
2837 return *G.prs.lex_inbuf;
2838}
2839static char eat_inbuf(void)
2840{
2841 char c = peek_inbuf();
2842 if (c) G.prs.lex_inbuf++;
2843 return c;
2844}
2845
Denys Vlasenko10bde142018-12-27 18:23:58 +01002846static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002847{
2848 BcParse *p = &G.prs;
2849 char c;
2850
2851 // Try: echo -n '#foo' | bc
2852 p->lex = XC_LEX_WHITESPACE;
2853
Denys Vlasenko2747f612018-12-31 18:48:10 +01002854 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002855 // '\0' which isn't the EOF can only be seen after '\n'.
2856 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2857 p->lex_inbuf++;
2858}
2859
Denys Vlasenko10bde142018-12-27 18:23:58 +01002860static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002861{
2862 BcParse *p = &G.prs;
2863
2864 p->lex = XC_LEX_WHITESPACE;
2865 for (;;) {
2866 // We depend here on input being done in whole lines:
2867 // '\0' which isn't the EOF can only be seen after '\n'.
2868 char c = *p->lex_inbuf;
2869 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2870 break;
2871 if (!isspace(c))
2872 break;
2873 p->lex_inbuf++;
2874 }
2875}
2876
Denys Vlasenko10bde142018-12-27 18:23:58 +01002877static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002878{
2879 BcParse *p = &G.prs;
2880 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002881 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002882
2883 bc_vec_pop_all(&p->lex_strnumbuf);
2884 bc_vec_pushByte(&p->lex_strnumbuf, last);
2885
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002886// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002887// (Note: They must be capitals. Lower case letters are variable names.)
2888// Single digit numbers always have the value of the digit regardless of
2889// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2890// all input digits greater or equal to ibase to the value of ibase-1.
2891// This makes the number ZZZ always be the largest 3 digit number of the
2892// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002893// dc only allows A-F, the rules about single-char and multi-char are the same.
2894 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002895 pt = (last == '.');
2896 p->lex = XC_LEX_NUMBER;
2897 for (;;) {
2898 // We depend here on input being done in whole lines:
2899 // '\0' which isn't the EOF can only be seen after '\n'.
2900 char c = *p->lex_inbuf;
2901 check_c:
2902 if (c == '\0')
2903 break;
2904 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2905 p->lex_inbuf += 2;
2906 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01002907 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002908 c = peek_inbuf(); // force next line to be read
2909 goto check_c;
2910 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002911 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002912 if (c != '.') break;
2913 // if '.' was already seen, stop on second one:
2914 if (pt) break;
2915 pt = true;
2916 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002917 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002918 last = c;
2919 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2920 p->lex_inbuf++;
2921 }
2922 if (last == '.') // remove trailing '.' if any
2923 bc_vec_pop(&p->lex_strnumbuf);
2924 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2925
2926 G.err_line = G.prs.lex_line;
2927 RETURN_STATUS(BC_STATUS_SUCCESS);
2928}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002929#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002930
Denys Vlasenko10bde142018-12-27 18:23:58 +01002931static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002932{
2933 BcParse *p = &G.prs;
2934 size_t i;
2935 const char *buf;
2936
2937 p->lex = XC_LEX_NAME;
2938
2939 // Since names can't cross lines with \<newline>,
2940 // we depend on the fact that whole line is in the buffer
2941 i = 0;
2942 buf = p->lex_inbuf - 1;
2943 for (;;) {
2944 char c = buf[i];
2945 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2946 i++;
2947 }
2948
2949#if 0 // We do not protect against people with gigabyte-long names
2950 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2951 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2952 if (i > BC_MAX_STRING)
2953 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2954 }
2955#endif
2956 bc_vec_string(&p->lex_strnumbuf, i, buf);
2957
2958 // Increment the index. We minus 1 because it has already been incremented.
2959 p->lex_inbuf += i - 1;
2960
2961 //return BC_STATUS_SUCCESS;
2962}
2963
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002964IF_BC(static BC_STATUS zbc_lex_token(void);)
2965IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002966#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2967#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002968
Denys Vlasenko10bde142018-12-27 18:23:58 +01002969static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002970{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002971 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002972 BcStatus s;
2973
Denys Vlasenko22314682019-01-01 21:50:14 +01002974 G.err_line = p->lex_line;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002975 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002976//why?
2977// if (p->lex_last == XC_LEX_EOF)
2978// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002979
2980 // Loop until failure or we don't have whitespace. This
2981 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002982 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002983 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002984 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002985 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002986 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002987 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002988 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002989 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002990 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002991 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002992 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2993 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002994 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002995 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002996 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002997 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002998 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002999 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003000 } while (!s && p->lex == XC_LEX_WHITESPACE);
3001 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06003002
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003003 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003004}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003005#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003006
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003007#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003008static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003009{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003010 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01003011 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01003012 RETURN_STATUS(BC_STATUS_SUCCESS);
3013}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003014#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003015
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003016static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003017{
3018 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003019 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003020 if (s) RETURN_STATUS(s);
3021 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003022 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003023 RETURN_STATUS(s);
3024}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003025#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003026
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003027static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003028{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003029 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003030 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003031 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003032 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003033
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003034 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3035 const char *keyword8 = bc_lex_kws[i].name8;
3036 unsigned j = 0;
3037 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3038 j++;
3039 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003040 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003041 if (keyword8[j] != '\0')
3042 continue;
3043 match:
3044 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003045 if (isalnum(buf[j]) || buf[j]=='_')
3046 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003047 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003048 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003049 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3050 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003051 }
3052
3053 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003054 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003055 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003056 }
3057
Denys Vlasenko10bde142018-12-27 18:23:58 +01003058 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003059 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003060
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003061 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003062 // Prevent this:
3063 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003064 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003065 // '
3066 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003067 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003068 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003069
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003070 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003071}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003072#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003073
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003074static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003075{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003076 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003077
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003078 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003079 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003080 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003081 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003082 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003083 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003084 }
3085 if (c == '"')
3086 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003087 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003088 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003089 dbg_lex("++p->lex_line=%zd", p->lex_line);
3090 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003091 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3092 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003093 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003094 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3095 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003096
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003097 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003098 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003099}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003100#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003101
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003102static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003103{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003104 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003105 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003106 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003107 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003108 with_and_without >>= 8; // store "with" value
3109 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003110 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003111}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003112#define parse_lex_by_checking_eq_sign(with, without) \
3113 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003114
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003115static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003116{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003117 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003119 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003120 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003121 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003122 char c;
3123
3124 p->lex_inbuf++;
3125 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003126 check_star:
3127 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003128 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003129 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003130 if (c == '/')
3131 break;
3132 goto check_star;
3133 }
3134 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003135 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 }
Denys Vlasenko22314682019-01-01 21:50:14 +01003137 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003138 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003139 dbg_lex("++p->lex_line=%zd", p->lex_line);
3140 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003142 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003143
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003144 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003145 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003146}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003147#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003148
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003149#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003150static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003151{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003152 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003154 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003155 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003156
3157 // This is the workhorse of the lexer.
3158 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003159// case '\0': // probably never reached
3160// p->lex_inbuf--;
3161// p->lex = XC_LEX_EOF;
3162// break;
3163 case '\n':
3164 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003165 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003166 p->lex = XC_LEX_NLINE;
3167 break;
3168 case '\t':
3169 case '\v':
3170 case '\f':
3171 case '\r':
3172 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003173 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003174 break;
3175 case '!':
3176 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3177 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3178 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003179 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003180 }
3181 break;
3182 case '"':
3183 s = zbc_lex_string();
3184 break;
3185 case '#':
3186 s = zbc_POSIX_does_not_allow("'#' script comments");
3187 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003188 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003189 break;
3190 case '%':
3191 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3192 break;
3193 case '&':
3194 c2 = *p->lex_inbuf;
3195 if (c2 == '&') {
3196 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3197 if (s) RETURN_STATUS(s);
3198 p->lex_inbuf++;
3199 p->lex = BC_LEX_OP_BOOL_AND;
3200 } else {
3201 p->lex = XC_LEX_INVALID;
3202 s = bc_error_bad_character('&');
3203 }
3204 break;
3205 case '(':
3206 case ')':
3207 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3208 break;
3209 case '*':
3210 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3211 break;
3212 case '+':
3213 c2 = *p->lex_inbuf;
3214 if (c2 == '+') {
3215 p->lex_inbuf++;
3216 p->lex = BC_LEX_OP_INC;
3217 } else
3218 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3219 break;
3220 case ',':
3221 p->lex = BC_LEX_COMMA;
3222 break;
3223 case '-':
3224 c2 = *p->lex_inbuf;
3225 if (c2 == '-') {
3226 p->lex_inbuf++;
3227 p->lex = BC_LEX_OP_DEC;
3228 } else
3229 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3230 break;
3231 case '.':
3232 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003233 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003234 else {
3235 p->lex = BC_LEX_KEY_LAST;
3236 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3237 }
3238 break;
3239 case '/':
3240 c2 = *p->lex_inbuf;
3241 if (c2 == '*')
3242 s = zbc_lex_comment();
3243 else
3244 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3245 break;
3246 case '0':
3247 case '1':
3248 case '2':
3249 case '3':
3250 case '4':
3251 case '5':
3252 case '6':
3253 case '7':
3254 case '8':
3255 case '9':
3256 case 'A':
3257 case 'B':
3258 case 'C':
3259 case 'D':
3260 case 'E':
3261 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003262 case 'G':
3263 case 'H':
3264 case 'I':
3265 case 'J':
3266 case 'K':
3267 case 'L':
3268 case 'M':
3269 case 'N':
3270 case 'O':
3271 case 'P':
3272 case 'Q':
3273 case 'R':
3274 case 'S':
3275 case 'T':
3276 case 'U':
3277 case 'V':
3278 case 'W':
3279 case 'X':
3280 case 'Y':
3281 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003282 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003283 break;
3284 case ';':
3285 p->lex = BC_LEX_SCOLON;
3286 break;
3287 case '<':
3288 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3289 break;
3290 case '=':
3291 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3292 break;
3293 case '>':
3294 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3295 break;
3296 case '[':
3297 case ']':
3298 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3299 break;
3300 case '\\':
3301 if (*p->lex_inbuf == '\n') {
3302 p->lex = XC_LEX_WHITESPACE;
3303 p->lex_inbuf++;
3304 } else
3305 s = bc_error_bad_character(c);
3306 break;
3307 case '^':
3308 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3309 break;
3310 case 'a':
3311 case 'b':
3312 case 'c':
3313 case 'd':
3314 case 'e':
3315 case 'f':
3316 case 'g':
3317 case 'h':
3318 case 'i':
3319 case 'j':
3320 case 'k':
3321 case 'l':
3322 case 'm':
3323 case 'n':
3324 case 'o':
3325 case 'p':
3326 case 'q':
3327 case 'r':
3328 case 's':
3329 case 't':
3330 case 'u':
3331 case 'v':
3332 case 'w':
3333 case 'x':
3334 case 'y':
3335 case 'z':
3336 s = zbc_lex_identifier();
3337 break;
3338 case '{':
3339 case '}':
3340 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3341 break;
3342 case '|':
3343 c2 = *p->lex_inbuf;
3344 if (c2 == '|') {
3345 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3346 if (s) RETURN_STATUS(s);
3347 p->lex_inbuf++;
3348 p->lex = BC_LEX_OP_BOOL_OR;
3349 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003350 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003351 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003352 }
3353 break;
3354 default:
3355 p->lex = XC_LEX_INVALID;
3356 s = bc_error_bad_character(c);
3357 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003358 }
3359
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003360 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003361}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003362#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003363#endif // ENABLE_BC
3364
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003365#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003366static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003367{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003368 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003369 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003370 xc_lex_whitespace(); // eats whitespace (but not newline)
3371 p->lex_inbuf++; // xc_lex_name() expects this
3372 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003373 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003374 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003375 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003376 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3377 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003378 }
3379
Denys Vlasenko29301232018-12-11 15:29:32 +01003380 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003381}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003382#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003383
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003384static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003385{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003386 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003387 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003388
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003389 p->lex = XC_LEX_STR;
3390 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003391
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003392 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003393 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003394 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003395 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003396 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003397 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003398 if (c == '[') depth++;
3399 if (c == ']')
3400 if (--depth == 0)
3401 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003402 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003403 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003404 dbg_lex("++p->lex_line=%zd", p->lex_line);
3405 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003406 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3407 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003408 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003409 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003410 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003411
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003412 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003413 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003414}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003415#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003416
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003417#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003418static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003419{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003420 static const //BcLexType - should be this type, but narrower type saves size:
3421 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003422 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003423 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 +01003424 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3425 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003426 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003427 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003428
Denys Vlasenko10bde142018-12-27 18:23:58 +01003429 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003430 BcStatus s;
3431 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003432 size_t i;
3433
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003434 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003435 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003436 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003437 }
3438
Denys Vlasenko81293c82018-12-24 01:53:55 +01003439 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003440 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003441 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003442 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003443 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003444 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003445 }
3446
3447 // This is the workhorse of the lexer.
3448 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003449// case '\0': // probably never reached
3450// p->lex = XC_LEX_EOF;
3451// break;
3452 case '\n':
3453 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3454 // (and "case '\n':" is not just empty here)
3455 // only to allow interactive dc have a way to exit
3456 // "parse" stage of "parse,execute" loop
3457 // on <enter>, not on _next_ token (which would mean
3458 // commands are not executed on pressing <enter>).
3459 // IOW: typing "1p<enter>" should print "1" _at once_,
3460 // not after some more input.
3461 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003462 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003463 p->lex = XC_LEX_NLINE;
3464 break;
3465 case '\t':
3466 case '\v':
3467 case '\f':
3468 case '\r':
3469 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003470 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003471 break;
3472 case '!':
3473 c2 = *p->lex_inbuf;
3474 if (c2 == '=')
3475 p->lex = XC_LEX_OP_REL_NE;
3476 else if (c2 == '<')
3477 p->lex = XC_LEX_OP_REL_LE;
3478 else if (c2 == '>')
3479 p->lex = XC_LEX_OP_REL_GE;
3480 else
3481 RETURN_STATUS(bc_error_bad_character(c));
3482 p->lex_inbuf++;
3483 break;
3484 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003485 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003486 break;
3487 case '.':
3488 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003489 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003490 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003491 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003492 break;
3493 case '0':
3494 case '1':
3495 case '2':
3496 case '3':
3497 case '4':
3498 case '5':
3499 case '6':
3500 case '7':
3501 case '8':
3502 case '9':
3503 case 'A':
3504 case 'B':
3505 case 'C':
3506 case 'D':
3507 case 'E':
3508 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003509 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003510 break;
3511 case '[':
3512 s = zdc_lex_string();
3513 break;
3514 default:
3515 p->lex = XC_LEX_INVALID;
3516 s = bc_error_bad_character(c);
3517 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003518 }
3519
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003520 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003521}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003522#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003523#endif // ENABLE_DC
3524
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003525static void xc_parse_push(unsigned i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003526{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003527 BcVec *code = &G.prs.func->code;
3528 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003529 bc_vec_pushByte(code, (uint8_t)i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003530}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003531
Denys Vlasenko10bde142018-12-27 18:23:58 +01003532static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003533{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003534#if 1
3535 BcVec *code = &G.prs.func->code;
3536 size_t pos = code->len;
3537 size_t len = strlen(name) + 1;
3538
3539 bc_vec_expand(code, pos + len);
3540 strcpy(code->v + pos, name);
3541 code->len = pos + len;
3542#else
3543 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003544 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003545 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003546 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003547#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003548}
3549
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003550// Indexes < 0xfc are encoded verbatim, else first byte is
3551// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3552// followed by that many bytes, lsb first.
3553// (The above describes 32-bit case).
3554#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3555
Denys Vlasenko53799502019-01-25 14:24:03 +01003556static void bc_vec_pushIndex(BcVec *v, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003557{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003558 size_t mask;
3559 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003560
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003561 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003562 if (idx < SMALL_INDEX_LIMIT) {
Denys Vlasenko53799502019-01-25 14:24:03 +01003563 bc_vec_pushByte(v, idx);
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003564 return;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003565 }
3566
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003567 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3568 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003569 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003570 if (idx & mask) break;
3571 mask >>= 8;
3572 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003573 }
3574 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003575
Denys Vlasenko53799502019-01-25 14:24:03 +01003576 bc_vec_pushByte(v, (SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003577
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003578 do {
Denys Vlasenko53799502019-01-25 14:24:03 +01003579 bc_vec_pushByte(v, (unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003580 idx >>= 8;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003581 } while (idx != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003582}
3583
Denys Vlasenko53799502019-01-25 14:24:03 +01003584static void xc_parse_pushIndex(size_t idx)
3585{
3586 bc_vec_pushIndex(&G.prs.func->code, idx);
3587}
3588
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003589static void xc_parse_pushInst_and_Index(unsigned inst, size_t idx)
3590{
3591 xc_parse_push(inst);
3592 xc_parse_pushIndex(idx);
3593}
3594
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003595#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003596static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003597{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003598 xc_parse_pushInst_and_Index(BC_INST_JUMP, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003599}
3600
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003601static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003602{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003603 xc_parse_pushInst_and_Index(BC_INST_JUMP_ZERO, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003604}
3605
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003606static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003607{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003608 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003609 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003610
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003611 xc_parse_pushInst_and_Index(XC_INST_STR, p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003612 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003613
Denys Vlasenko10bde142018-12-27 18:23:58 +01003614 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003615}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003616#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003617#endif
3618
Denys Vlasenko10bde142018-12-27 18:23:58 +01003619static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003620{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003621 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003622 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003623#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003624 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003625#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003626 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003627#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003628 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003629#endif
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003630 xc_parse_pushInst_and_Index(XC_INST_NUM, idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003631}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003632
Denys Vlasenko10bde142018-12-27 18:23:58 +01003633static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003634{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003635 G.prs.func = xc_program_func(G.prs.fidx);
3636 G.prs.lex_inbuf = text;
3637 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3638 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003639}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003640#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003641
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003642// Called when parsing or execution detects a failure,
3643// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003644static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003645{
3646 BcFunc *f;
3647 BcInstPtr *ip;
3648
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003649 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003650 bc_vec_pop_all(&G.prog.results);
3651
Denys Vlasenko10bde142018-12-27 18:23:58 +01003652 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003653 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003654 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003655}
3656
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003657// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003658// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003659static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003660{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003661 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003662 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003663 bc_func_free(p->func);
3664 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003665
Denys Vlasenko65e10462018-12-19 15:13:14 +01003666 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003667 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003668 }
3669
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003670 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003671 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003672
Denys Vlasenko503faf92018-12-20 16:24:18 +01003673 IF_BC(bc_vec_pop_all(&p->exits);)
3674 IF_BC(bc_vec_pop_all(&p->conds);)
3675 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003676
Denys Vlasenko10bde142018-12-27 18:23:58 +01003677 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003678}
3679
Denys Vlasenko10bde142018-12-27 18:23:58 +01003680static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003681{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003682 IF_BC(bc_vec_free(&G.prs.exits);)
3683 IF_BC(bc_vec_free(&G.prs.conds);)
3684 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003685 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003686}
3687
Denys Vlasenko10bde142018-12-27 18:23:58 +01003688static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003689{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003690 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003691 memset(p, 0, sizeof(BcParse));
3692
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003693 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003694 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3695 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3696 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003697
Denys Vlasenkofa210792018-12-19 19:35:40 +01003698 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003699 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003700}
3701
Denys Vlasenko10bde142018-12-27 18:23:58 +01003702static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003703{
Denys Vlasenko04715442018-12-21 00:10:26 +01003704 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003705 BcFunc f;
3706 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003707 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003708 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003709 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003710}
3711
3712#if ENABLE_BC
3713
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003714// Note: takes ownership of 'name' (must be malloced)
3715static size_t bc_program_addFunc(char *name)
3716{
3717 size_t idx;
3718 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003719 int inserted;
3720
3721 entry.name = name;
3722 entry.idx = G.prog.fns.len;
3723
3724 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3725 if (!inserted) free(name);
3726
3727 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3728 idx = entry_ptr->idx;
3729
3730 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003731 // There is already a function with this name.
3732 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003733 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003734 bc_func_free(func);
3735 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003736 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003737 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003738 }
3739
3740 return idx;
3741}
3742
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003743#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003744// We can calculate the conversion between tokens and exprs by subtracting the
3745// position of the first operator in the lex enum and adding the position of the
3746// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003747#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003748
Denys Vlasenko132d7c02019-01-08 19:29:35 +01003749static BC_STATUS zbc_parse_expr(uint8_t flags);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003750#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003751
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003752static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003753#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003754
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003755static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003756{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003757 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003758}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003759#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003760
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003761static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003762{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003763 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003764 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3765 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003766 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003767 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003768 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003769 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003770
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003771 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003772}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003773#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003774
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003775static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003776{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003777 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003778 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3779 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003780
3781 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003782 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003783 if (t == BC_LEX_LPAREN) break;
3784
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003785 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003786 if (l >= r && (l != r || !left)) break;
3787
Denys Vlasenko10bde142018-12-27 18:23:58 +01003788 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003789 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003790 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003791 }
3792
3793 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003794}
3795
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003796static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003797{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003798 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 BcLexType top;
3800
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003801 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003802 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003803 top = BC_PARSE_TOP_OP(p);
3804
3805 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003806 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003807
3808 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003809 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003810
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003811 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003812 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003813 top = BC_PARSE_TOP_OP(p);
3814 }
3815
3816 bc_vec_pop(&p->ops);
3817
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003818 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003819}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003820#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003821
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003822static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003823{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003824 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003825 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003826 size_t nparams;
3827
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003828 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003829 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3830
Denys Vlasenko10bde142018-12-27 18:23:58 +01003831 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003832 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003833
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003834 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003835 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003836 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003837 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003838 if (s) RETURN_STATUS(s);
3839 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003840 if (p->lex != BC_LEX_COMMA) {
3841 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003842 break;
3843 RETURN_STATUS(bc_error_bad_token());
3844 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003845 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003846 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 }
3848 }
3849
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003850 xc_parse_pushInst_and_Index(BC_INST_CALL, nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003851
Denys Vlasenko26819db2018-12-12 16:08:46 +01003852 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003853}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003854#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003855
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003856// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003857static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003858{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003859 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003860 BcStatus s;
3861 BcId entry, *entry_ptr;
3862 size_t idx;
3863
3864 entry.name = name;
3865
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003866 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003867 if (s) goto err;
3868
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003869 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003870 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003871 goto err;
3872 }
3873
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003874 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003875
3876 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003877 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003878 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003879 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003880 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003881 free(name);
3882
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003883 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003884 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003885
Denys Vlasenko10bde142018-12-27 18:23:58 +01003886 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003887 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003889 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003890}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003891#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003892
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003893static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003894{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003895 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003896 BcStatus s;
3897 char *name;
3898
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003899 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003900 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003901 if (s) goto err;
3902
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003903 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003904 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 if (s) goto err;
3906
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003907 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003909 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003910 goto err;
3911 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003912 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003913 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003914 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003915 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003916 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003917 if (s) goto err;
3918 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003919 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003920 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003921 xc_parse_push(*type);
3922 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003923 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003924 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003925 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003926 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003927 goto err;
3928 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003929 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003930 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003931 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003932 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003933 xc_parse_push(XC_INST_VAR);
3934 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003935 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003936 }
3937
Denys Vlasenko26819db2018-12-12 16:08:46 +01003938 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003939 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003940 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003941 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003942}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003943#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003944
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003945static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003946{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003947 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003948 BcStatus s;
3949
Denys Vlasenko10bde142018-12-27 18:23:58 +01003950 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003951 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003952 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
Denys Vlasenko10bde142018-12-27 18:23:58 +01003954 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003955 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003956 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003957
Denys Vlasenko10bde142018-12-27 18:23:58 +01003958 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003959
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003960 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003961}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003962#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003963
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003964static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003965{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003966 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003967 BcStatus s;
3968
Denys Vlasenko10bde142018-12-27 18:23:58 +01003969 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003970 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003971 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003972
3973 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3974
Denys Vlasenko10bde142018-12-27 18:23:58 +01003975 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003976 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003977
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003978 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003979 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003980
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003981 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003982
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003983 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003984 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003985
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003986 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003987}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003988#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003989
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003990static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003991{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003992 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003993 BcStatus s;
3994
Denys Vlasenko10bde142018-12-27 18:23:58 +01003995 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003997
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003998 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003999 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004000 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004001 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004002 }
4003
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004004 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06004005 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4006
Denys Vlasenko10bde142018-12-27 18:23:58 +01004007 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004008 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004009
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004010 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004011 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004012 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01004013 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004014 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004015
Denys Vlasenko10bde142018-12-27 18:23:58 +01004016 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004017}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004018#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004019
Denys Vlasenko22314682019-01-01 21:50:14 +01004020static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004021{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004022 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 BcStatus s;
4024 BcLexType type;
4025 char inst;
4026 BcInst etype = *prev;
4027
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004028 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4029 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4030 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004031 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004032 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004033 xc_parse_push(inst);
4034 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004035 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004036 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004037
Denys Vlasenko10bde142018-12-27 18:23:58 +01004038 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004039 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004040 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004041
4042 // Because we parse the next part of the expression
4043 // right here, we need to increment this.
Denys Vlasenko22314682019-01-01 21:50:14 +01004044 *nexs = *nexs + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004045
4046 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004047 case XC_LEX_NAME:
4048 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
4049 break;
4050 case BC_LEX_KEY_IBASE:
4051 case BC_LEX_KEY_LAST:
4052 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004053 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4054 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004055 break;
4056 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004057 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004058 if (s) RETURN_STATUS(s);
4059 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004060 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004061 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01004062 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004063 break;
4064 default:
4065 s = bc_error_bad_token();
4066 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004067 }
4068
Denys Vlasenko10bde142018-12-27 18:23:58 +01004069 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004070 }
4071
Denys Vlasenko26819db2018-12-12 16:08:46 +01004072 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004073}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004074#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004075
Denys Vlasenko22314682019-01-01 21:50:14 +01004076static int bc_parse_inst_isLeaf(BcInst p)
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004077{
4078 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4079 || p == BC_INST_INC_POST
4080 || p == BC_INST_DEC_POST
4081 ;
4082}
Denys Vlasenko22314682019-01-01 21:50:14 +01004083#define BC_PARSE_LEAF(prev, bin_last, rparen) \
4084 (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev)))
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004085
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004086static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
Denys Vlasenko22314682019-01-01 21:50:14 +01004087 bool rparen, bool bin_last, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004088{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004089 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004090 BcStatus s;
4091 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004092
Denys Vlasenko10bde142018-12-27 18:23:58 +01004093 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004094 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004095
Denys Vlasenko22314682019-01-01 21:50:14 +01004096 type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004097 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004098
4099 // We can just push onto the op stack because this is the largest
4100 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004101 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004102 bc_vec_push(&p->ops, &type);
4103 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004104 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004105
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004106 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004107}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004108#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004109
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004110static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004111{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004112 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004113 BcStatus s;
4114 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004115
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004116 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004117 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004118 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004119 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004120 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004121 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004122 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004123 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004124 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004125 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004126 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004127 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004128 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004129 }
4130
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004131 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004132}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004133#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004134
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004135static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004136{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004137 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004138 BcStatus s;
4139 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004140
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004141 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004142 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004143 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004144
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004145 t = p->lex;
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004146 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004147 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004148 else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004149//TODO: if (p->func->voidfunc) ERROR
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004150 s = zbc_parse_expr(0);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004151 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004152
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004153 if (t != BC_LEX_LPAREN // "return EXPR", no ()
4154 || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b"
4155 ) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004156 s = zbc_POSIX_requires("parentheses around return expressions");
4157 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004158 }
4159
Denys Vlasenko10bde142018-12-27 18:23:58 +01004160 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004161 }
4162
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004163 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004164 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004165}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004166#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004167
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004168static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004169{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004170 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004171 size_t *label = bc_vec_item(&p->func->labels, idx);
4172 *label = p->func->code.len;
4173}
4174
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004175static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004176{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004177 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004178 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004179 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004180
4181 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004182 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004183 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004184 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004185
Denys Vlasenko10bde142018-12-27 18:23:58 +01004186 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004187 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004188 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +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 Vlasenkod1d29b42018-12-16 16:03:03 +01004191
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004192 // Encode "if zero, jump to ..."
4193 // Pushed value (destination of the jump) is uninitialized,
4194 // will be rewritten to be address of "end of if()" or of "else".
4195 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004196 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004197
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004198 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004199 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004200
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004201 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4202 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004203 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004204
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004205 // Encode "after then_stmt, jump to end of if()"
4206 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4207 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004208 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004209
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004210 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 +01004211 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004212
Denys Vlasenko15850832018-12-16 21:40:54 +01004213 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004214
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004215 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004216 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004217 }
4218
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004219 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 +01004220 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004221
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004222 dbg_lex_done("%s:%d done", __func__, __LINE__);
4223 RETURN_STATUS(s);
4224}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004225#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004226
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004227static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004228{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004229 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004230 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004231 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004232 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004233
Denys Vlasenko10bde142018-12-27 18:23:58 +01004234 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004235 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004236 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004237 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004238 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004239
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004240 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004241 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004242 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004243
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004244 bc_vec_push(&p->exits, &ip_idx);
4245 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004246
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004247 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004248 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004249 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004250
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004251 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004252
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004253 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004254 if (s) RETURN_STATUS(s);
4255
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004256 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004257 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004258
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004259 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004260 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004261
4262 bc_vec_pop(&p->exits);
4263 bc_vec_pop(&p->conds);
4264
4265 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004266}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004267#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004268
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004269static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004270{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004271 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004272 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004273 size_t cond_idx, exit_idx, body_idx, update_idx;
4274
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004275 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004276 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004277 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004278 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004279 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004280 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004282 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004283 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004284 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004285 if (s) RETURN_STATUS(s);
4286 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004287 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4288 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004289 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004290
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004291 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004292 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004293 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004294
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004295 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004296 update_idx = cond_idx + 1;
4297 body_idx = update_idx + 1;
4298 exit_idx = body_idx + 1;
4299
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004300 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004301 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004302 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004303 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004304 // This is safe to set because the current token is a semicolon,
4305 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004306 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004307 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004308 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004309 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004310 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004311
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004312 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004313
Denys Vlasenko10bde142018-12-27 18:23:58 +01004314 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004315 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004316
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004317 bc_parse_pushJUMP_ZERO(exit_idx);
4318 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004319
Gavin Howard01055ba2018-11-03 11:00:21 -06004320 bc_vec_push(&p->conds, &update_idx);
4321 bc_vec_push(&p->func->labels, &p->func->code.len);
4322
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004323 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004324 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004325 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004326 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004327 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004328 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004329 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4330 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004331 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004332
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004333 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004334 bc_vec_push(&p->func->labels, &p->func->code.len);
4335
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004336 bc_vec_push(&p->exits, &exit_idx);
4337 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004338
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004339 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004340 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004341
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004342 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004343 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004344
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004345 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004346 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004347
4348 bc_vec_pop(&p->exits);
4349 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004350
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004351 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004352}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004353#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004354
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004355static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004356{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004357 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004358 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004359
Gavin Howard01055ba2018-11-03 11:00:21 -06004360 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004361 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4362 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004363 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004364 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004365 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004366 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004367 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004368
Denys Vlasenko10bde142018-12-27 18:23:58 +01004369 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004370}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004371#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004372
Denys Vlasenko53799502019-01-25 14:24:03 +01004373static BC_STATUS zbc_func_insert(BcFunc *f, char *name, BcType type)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004374{
4375 BcId *autoid;
4376 BcId a;
4377 size_t i;
4378
4379 autoid = (void*)f->autos.v;
4380 for (i = 0; i < f->autos.len; i++, autoid++) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004381 if (strcmp(name, autoid->name) == 0
Denys Vlasenko53799502019-01-25 14:24:03 +01004382 && type == (BcType) autoid->idx
Denys Vlasenko22314682019-01-01 21:50:14 +01004383 ) {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004384 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko22314682019-01-01 21:50:14 +01004385 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004386 }
4387
Denys Vlasenko53799502019-01-25 14:24:03 +01004388 a.idx = type;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004389 a.name = name;
4390
4391 bc_vec_push(&f->autos, &a);
4392
4393 RETURN_STATUS(BC_STATUS_SUCCESS);
4394}
4395#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4396
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004397static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004398{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004399 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004400 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01004401 bool comma, voidfunc;
Gavin Howard01055ba2018-11-03 11:00:21 -06004402 char *name;
4403
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004404 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004405 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004406 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004407 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004408 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004409
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004410 // To be maximally both POSIX and GNU-compatible,
4411 // "void" is not treated as a normal keyword:
4412 // you can have variable named "void", and even a function
4413 // named "void": "define void() { return 6; }" is ok.
4414 // _Only_ "define void f() ..." syntax treats "void"
4415 // specially.
4416 voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004417
Denys Vlasenko10bde142018-12-27 18:23:58 +01004418 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004419 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004420
4421 voidfunc = (voidfunc && p->lex == XC_LEX_NAME);
4422 if (voidfunc) {
4423 s = zxc_lex_next();
4424 if (s) RETURN_STATUS(s);
4425 }
4426
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004427 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko22314682019-01-01 21:50:14 +01004428 RETURN_STATUS(bc_error_bad_function_definition());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004429
4430 p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v));
4431 p->func = xc_program_func(p->fidx);
4432 p->func->voidfunc = voidfunc;
4433
Denys Vlasenko10bde142018-12-27 18:23:58 +01004434 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004435 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004436
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004437 comma = false;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004438 while (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004439 BcType t = BC_TYPE_VAR;
4440
4441 if (p->lex == XC_LEX_OP_MULTIPLY) {
4442 t = BC_TYPE_REF;
4443 s = zxc_lex_next();
4444 if (s) RETURN_STATUS(s);
4445 s = zbc_POSIX_does_not_allow("references");
4446 if (s) RETURN_STATUS(s);
4447 }
4448
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004449 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004450 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004451
4452 ++p->func->nparams;
4453
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004454 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004455 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 if (s) goto err;
4457
Denys Vlasenko53799502019-01-25 14:24:03 +01004458 if (p->lex == BC_LEX_LBRACKET) {
4459 if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;
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
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004463 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004464 s = bc_error_bad_function_definition();
Gavin Howard01055ba2018-11-03 11:00:21 -06004465 goto err;
4466 }
4467
Denys Vlasenko10bde142018-12-27 18:23:58 +01004468 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004469 if (s) goto err;
4470 }
Denys Vlasenko53799502019-01-25 14:24:03 +01004471 else if (t == BC_TYPE_REF) {
4472 s = bc_error_at("vars can't be references");
4473 goto err;
4474 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004475
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004476 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004477 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004478 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004479 if (s) goto err;
4480 }
4481
Denys Vlasenko53799502019-01-25 14:24:03 +01004482 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004483 if (s) goto err;
4484 }
4485
Denys Vlasenko22314682019-01-01 21:50:14 +01004486 if (comma) RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004487
Denys Vlasenko10bde142018-12-27 18:23:58 +01004488 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004489 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004490
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004491 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004492 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004493 if (s) RETURN_STATUS(s);
4494 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004495
Denys Vlasenko202dd192018-12-16 17:30:35 +01004496 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004497 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004498 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004499 // GNU bc requires a {} block even if function body has single stmt, enforce this
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004500 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004501 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004502
4503 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004504 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004505 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004506 if (s) RETURN_STATUS(s);
4507
Denys Vlasenko10bde142018-12-27 18:23:58 +01004508 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004509
4510 // Subsequent code generation is into main program
4511 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004512 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004513
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004514 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004515 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004516 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004517 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004518 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004519 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004520}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004521#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004522
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004523static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004524{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004525 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004526 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004527 char *name;
4528
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004529 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004530 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004531 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004532
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004533 for (;;) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004534 BcType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004535
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004536 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004537 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004538
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004539 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004540 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 if (s) goto err;
4542
Denys Vlasenko53799502019-01-25 14:24:03 +01004543 t = BC_TYPE_VAR;
4544 if (p->lex == BC_LEX_LBRACKET) {
4545 t = BC_TYPE_ARRAY;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004546 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 if (s) goto err;
4548
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004549 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004550 s = bc_error_at("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 goto err;
4552 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004553 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004554 if (s) goto err;
4555 }
4556
Denys Vlasenko53799502019-01-25 14:24:03 +01004557 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004558 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004559
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004560 if (p->lex == XC_LEX_NLINE
4561 || p->lex == BC_LEX_SCOLON
4562 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004563 ) {
4564 break;
4565 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004566 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko22314682019-01-01 21:50:14 +01004567 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004568 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004569 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 }
4571
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004572 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004573 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004574 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004576 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004577 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004579#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004580
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004581#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004582static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004583{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004584 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 BcStatus s = BC_STATUS_SUCCESS;
4586
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004587 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004588
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004589 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004590 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004591 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004592 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004593 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004594 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004595 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004596 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004597
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004598 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004599 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4600 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004601 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004602 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004603 } while (p->lex == XC_LEX_NLINE);
4604 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004605 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004606 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004607 if (s) RETURN_STATUS(s);
4608 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004609 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004610 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004611 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004612 if (s) RETURN_STATUS(s);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004613 // Check that next token is a correct stmt delimiter -
4614 // disallows "print 1 print 2" and such.
4615 if (p->lex == BC_LEX_RBRACE)
4616 break;
4617 if (p->lex != BC_LEX_SCOLON
4618 && p->lex != XC_LEX_NLINE
4619 ) {
4620 RETURN_STATUS(bc_error_at("bad statement terminator"));
4621 }
4622 s = zxc_lex_next();
4623 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004624 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004625 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004626 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4627 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 }
4629
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004630 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4631 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004632 case XC_LEX_OP_MINUS:
4633 case BC_LEX_OP_INC:
4634 case BC_LEX_OP_DEC:
4635 case BC_LEX_OP_BOOL_NOT:
4636 case BC_LEX_LPAREN:
4637 case XC_LEX_NAME:
4638 case XC_LEX_NUMBER:
4639 case BC_LEX_KEY_IBASE:
4640 case BC_LEX_KEY_LAST:
4641 case BC_LEX_KEY_LENGTH:
4642 case BC_LEX_KEY_OBASE:
4643 case BC_LEX_KEY_READ:
4644 case BC_LEX_KEY_SCALE:
4645 case BC_LEX_KEY_SQRT:
4646 s = zbc_parse_expr(BC_PARSE_PRINT);
4647 break;
4648 case XC_LEX_STR:
4649 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004650 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004651 break;
4652 case BC_LEX_KEY_BREAK:
4653 case BC_LEX_KEY_CONTINUE:
4654 s = zbc_parse_break_or_continue(p->lex);
4655 break;
4656 case BC_LEX_KEY_FOR:
4657 s = zbc_parse_for();
4658 break;
4659 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004660 xc_parse_push(BC_INST_HALT);
4661 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004662 break;
4663 case BC_LEX_KEY_IF:
4664 s = zbc_parse_if();
4665 break;
4666 case BC_LEX_KEY_LIMITS:
4667 // "limits" is a compile-time command,
4668 // the output is produced at _parse time_.
4669 printf(
4670 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4671 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4672 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4673 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
Denys Vlasenkoe05ec6e2019-01-04 16:26:19 +01004674 // "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" - GNU bc does not show this
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004675 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4676 "Number of vars = "BC_MAX_VARS_STR "\n"
4677 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004678 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004679 break;
4680 case BC_LEX_KEY_PRINT:
4681 s = zbc_parse_print();
4682 break;
4683 case BC_LEX_KEY_QUIT:
4684 // "quit" is a compile-time command. For example,
4685 // "if (0 == 1) quit" terminates when parsing the statement,
4686 // not when it is executed
4687 QUIT_OR_RETURN_TO_MAIN;
4688 case BC_LEX_KEY_RETURN:
4689 if (!p->in_funcdef)
4690 RETURN_STATUS(bc_error("'return' not in a function"));
4691 s = zbc_parse_return();
4692 break;
4693 case BC_LEX_KEY_WHILE:
4694 s = zbc_parse_while();
4695 break;
4696 default:
4697 s = bc_error_bad_token();
4698 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004699 }
4700
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004701 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004702 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004703}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004704#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004705
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004706static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004707{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004708 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004709 BcStatus s;
4710
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004711 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004712//why?
4713// if (p->lex == XC_LEX_EOF)
4714// s = bc_error("end of file");
4715// else
4716 if (p->lex == BC_LEX_KEY_DEFINE) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004717 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004718 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004719 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004720 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004721 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004722 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004723
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004724 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004725 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004726}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004727#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004728
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004729#undef zbc_parse_expr
4730static BC_STATUS zbc_parse_expr(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004731{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004732 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004733 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004734 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004735 unsigned nparens, nrelops;
Denys Vlasenko22314682019-01-01 21:50:14 +01004736 bool paren_first, rprn, assign, bin_last, incdec;
Gavin Howard01055ba2018-11-03 11:00:21 -06004737
Denys Vlasenko17df8822018-12-14 23:00:24 +01004738 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004739 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004740 nparens = nrelops = 0;
Denys Vlasenko22314682019-01-01 21:50:14 +01004741 rprn = assign = incdec = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004742 bin_last = true;
4743
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004744 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004745 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004746 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004747 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004748
4749 if (!lex_allowed_in_bc_expr(t))
4750 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004751
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004752 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004753 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004754 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004755 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004756 case BC_LEX_OP_INC:
4757 case BC_LEX_OP_DEC:
4758 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004759 if (incdec) RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko22314682019-01-01 21:50:14 +01004760 s = zbc_parse_incdec(&prev, &nexprs, flags);
4761 incdec = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004762 rprn = bin_last = false;
4763 //get_token = false; - already is
4764 break;
4765 case XC_LEX_OP_MINUS:
4766 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004767 s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004768 rprn = false;
4769 //get_token = false; - already is
4770 bin_last = (prev == XC_INST_MINUS);
Denys Vlasenko22314682019-01-01 21:50:14 +01004771 if (bin_last) incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004772 break;
4773 case BC_LEX_OP_ASSIGN_POWER:
4774 case BC_LEX_OP_ASSIGN_MULTIPLY:
4775 case BC_LEX_OP_ASSIGN_DIVIDE:
4776 case BC_LEX_OP_ASSIGN_MODULUS:
4777 case BC_LEX_OP_ASSIGN_PLUS:
4778 case BC_LEX_OP_ASSIGN_MINUS:
4779 case BC_LEX_OP_ASSIGN:
4780 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4781 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4782 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4783 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4784 ) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004785 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004786 }
4787 // Fallthrough.
4788 case XC_LEX_OP_POWER:
4789 case XC_LEX_OP_MULTIPLY:
4790 case XC_LEX_OP_DIVIDE:
4791 case XC_LEX_OP_MODULUS:
4792 case XC_LEX_OP_PLUS:
4793 case XC_LEX_OP_REL_EQ:
4794 case XC_LEX_OP_REL_LE:
4795 case XC_LEX_OP_REL_GE:
4796 case XC_LEX_OP_REL_NE:
4797 case XC_LEX_OP_REL_LT:
4798 case XC_LEX_OP_REL_GT:
4799 case BC_LEX_OP_BOOL_NOT:
4800 case BC_LEX_OP_BOOL_OR:
4801 case BC_LEX_OP_BOOL_AND:
4802 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004803 if (t == BC_LEX_OP_BOOL_NOT) {
4804 if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004805 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko22314682019-01-01 21:50:14 +01004806 } else if (prev == XC_INST_BOOL_NOT) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004807 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004808 }
Denys Vlasenko22314682019-01-01 21:50:14 +01004809
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004810 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4811 prev = BC_TOKEN_2_INST(t);
4812 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko22314682019-01-01 21:50:14 +01004813 rprn = incdec = false;
4814 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004815 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4816 break;
4817 case BC_LEX_LPAREN:
4818 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004819 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004820 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004821 bc_vec_push(&p->ops, &t);
4822 nparens++;
4823 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004824 rprn = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004825 break;
4826 case BC_LEX_RPAREN:
4827 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenkoa1698a12019-01-08 19:32:38 +01004828//why?
4829// if (p->lex_last == BC_LEX_LPAREN) {
4830// RETURN_STATUS(bc_error_at("empty expression"));
4831// }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004832 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004833 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004834 if (nparens == 0) {
4835 goto exit_loop;
4836 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004837 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4838 nparens--;
4839 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004840 rprn = true;
4841 bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004842 break;
4843 case XC_LEX_NAME:
4844 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004845 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004846 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004847 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko22314682019-01-01 21:50:14 +01004848 rprn = (prev == BC_INST_CALL);
4849 bin_last = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004850 //get_token = false; - already is
4851 nexprs++;
4852 break;
4853 case XC_LEX_NUMBER:
4854 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004855 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004856 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004857 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004858 prev = XC_INST_NUM;
4859 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004860 rprn = bin_last = false;
4861 nexprs++;
4862 break;
4863 case BC_LEX_KEY_IBASE:
4864 case BC_LEX_KEY_LAST:
4865 case BC_LEX_KEY_OBASE:
4866 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004867 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004868 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004869 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004870 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004871 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004872 rprn = bin_last = false;
4873 nexprs++;
4874 break;
4875 case BC_LEX_KEY_LENGTH:
4876 case BC_LEX_KEY_SQRT:
4877 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004878 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004879 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004880 s = zbc_parse_builtin(t, flags, &prev);
4881 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004882 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004883 nexprs++;
4884 break;
4885 case BC_LEX_KEY_READ:
4886 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004887 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004888 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004889 s = zbc_parse_read();
4890 prev = XC_INST_READ;
4891 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004892 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004893 nexprs++;
4894 break;
4895 case BC_LEX_KEY_SCALE:
4896 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004897 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004898 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004899 s = zbc_parse_scale(&prev, flags);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004900 //get_token = false; - already is
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004901 rprn = bin_last = false;
4902 nexprs++;
4903 break;
4904 default:
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004905 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004906 }
4907
Denys Vlasenkobb116032018-12-25 01:16:37 +01004908 if (s || G_interrupt) // error, or ^C: stop parsing
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004909 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004910 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004911 s = zxc_lex_next();
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004912 if (s) RETURN_STATUS(s);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004913 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004914 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004915 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004916
4917 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004918 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004919 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004920
4921 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004922 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004923
Denys Vlasenko10bde142018-12-27 18:23:58 +01004924 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004925
Denys Vlasenko69560f42018-12-24 14:14:23 +01004926 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004927 bc_vec_pop(&p->ops);
4928 }
4929
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004930 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004931 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004932
Gavin Howard01055ba2018-11-03 11:00:21 -06004933 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004934 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004935 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004936 if (s) RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004937 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004938 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004939 s = zbc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004940 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004941 }
4942
4943 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004944 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004945 xc_parse_push(XC_INST_PRINT);
4946 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 }
4948
Denys Vlasenko17df8822018-12-14 23:00:24 +01004949 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004950 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004951}
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004952#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004953
Gavin Howard01055ba2018-11-03 11:00:21 -06004954#endif // ENABLE_BC
4955
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004956#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004957
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004958static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004959{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004960 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004961 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004962
Denys Vlasenko10bde142018-12-27 18:23:58 +01004963 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004964 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004965 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004966
Denys Vlasenko10bde142018-12-27 18:23:58 +01004967 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004968
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004969 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004970}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004971#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004972
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004973static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004974{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004975 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004976 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004977 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004978
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004979 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004980
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004981 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01004982 xc_parse_pushInst_and_Index(XC_INST_STR, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004983 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004984
Brian Foleyb64470b2019-09-05 10:50:13 +02004985 // Add an empty function so that if zdc_program_execStr ever needs to
4986 // parse the string into code (from the 'x' command) there's somewhere
4987 // to store the bytecode.
Denys Vlasenko10bde142018-12-27 18:23:58 +01004988 xc_program_add_fn();
4989 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004990
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004991 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004992}
4993
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004994static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004995{
4996 BcStatus s;
4997
Denys Vlasenko10bde142018-12-27 18:23:58 +01004998 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005000 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005001 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005002 }
5003
5004 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005005 xc_parse_push(DC_INST_SWAP);
5006 xc_parse_push(XC_INST_ASSIGN);
5007 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005008 }
5009
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005010 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005011}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005012#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005013
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005014static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005015{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005016 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06005017 BcStatus s;
5018
Denys Vlasenko10bde142018-12-27 18:23:58 +01005019 xc_parse_push(inst);
5020 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06005021
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005022 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005023 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005024
Denys Vlasenko10bde142018-12-27 18:23:58 +01005025 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005026 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005027
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005028 // Note that 'else' part can not be on the next line:
5029 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
5030 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005031 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005032 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005033 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005034 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005035 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005036 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005037 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005038
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005039 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005040}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005041#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005042
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005043static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005044{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005045 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005046 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005047 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005048
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005049 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005050 s = BC_STATUS_SUCCESS;
5051 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005053 case XC_LEX_OP_REL_EQ:
5054 case XC_LEX_OP_REL_LE:
5055 case XC_LEX_OP_REL_GE:
5056 case XC_LEX_OP_REL_NE:
5057 case XC_LEX_OP_REL_LT:
5058 case XC_LEX_OP_REL_GT:
5059 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
5060 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
5061 get_token = false;
5062 break;
5063 case DC_LEX_SCOLON:
5064 case DC_LEX_COLON:
5065 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
5066 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
5067 break;
5068 case XC_LEX_STR:
5069 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
5070 dc_parse_string();
5071 break;
5072 case XC_LEX_NEG:
5073 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005074 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005075 if (s) RETURN_STATUS(s);
5076 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005077 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005078 xc_parse_pushNUM();
5079 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005080 break;
5081 case XC_LEX_NUMBER:
5082 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005083 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005084 break;
5085 case DC_LEX_READ:
5086 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005087 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005088 break;
5089 case DC_LEX_OP_ASSIGN:
5090 case DC_LEX_STORE_PUSH:
5091 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
5092 assign = (t == DC_LEX_OP_ASSIGN);
5093 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
5094 s = zdc_parse_mem(inst, true, assign);
5095 break;
5096 case DC_LEX_LOAD:
5097 case DC_LEX_LOAD_POP:
5098 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
5099 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
5100 s = zdc_parse_mem(inst, true, false);
5101 break;
5102 case DC_LEX_STORE_IBASE:
5103 case DC_LEX_STORE_SCALE:
5104 case DC_LEX_STORE_OBASE:
5105 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5106 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5107 s = zdc_parse_mem(inst, false, true);
5108 break;
5109 default:
5110 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5111 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005112 }
5113
Denys Vlasenko10bde142018-12-27 18:23:58 +01005114 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005115
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005116 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005117 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005118}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005119#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005120
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005121static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005122{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005123 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005124 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005125
Denys Vlasenko6842c602019-01-04 05:41:47 +01005126 if (p->lex == XC_LEX_NLINE)
5127 RETURN_STATUS(zxc_lex_next());
5128
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005129 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005130 if (i >= 0) {
5131 BcInst inst = dc_LEX_to_INST[i];
5132 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005133 xc_parse_push(inst);
5134 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005135 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005136 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005137 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005138}
5139#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5140
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005141static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005142{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005143 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005144 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5145 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005146 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005147 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005148 }
5149
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005150 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005151 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005152}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005153#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005154
Gavin Howard01055ba2018-11-03 11:00:21 -06005155#endif // ENABLE_DC
5156
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005157//
5158// Execution engine
5159//
5160
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005161#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5162#define BC_PROG_NUM(r, n) \
5163 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5164
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005165#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5166#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5167
Denys Vlasenko53799502019-01-25 14:24:03 +01005168static size_t xc_program_index(char *code, size_t *bgn)
5169{
5170 unsigned char *bytes = (void*)(code + *bgn);
5171 unsigned amt;
5172 unsigned i;
5173 size_t res;
5174
5175 amt = *bytes++;
5176 if (amt < SMALL_INDEX_LIMIT) {
5177 *bgn += 1;
5178 return amt;
5179 }
5180 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
5181 *bgn += amt + 1;
5182
5183 res = 0;
5184 i = 0;
5185 do {
5186 res |= (size_t)(*bytes++) << i;
5187 i += 8;
5188 } while (--amt != 0);
5189
5190 return res;
5191}
5192
5193static char *xc_program_name(char *code, size_t *bgn)
5194{
5195 code += *bgn;
5196 *bgn += strlen(code) + 1;
5197
5198 return xstrdup(code);
5199}
5200
5201static BcVec* xc_program_dereference(BcVec *vec)
5202{
5203 BcVec *v;
5204 size_t vidx, nidx, i = 0;
5205
5206 //assert(vec->size == sizeof(uint8_t));
5207
5208 vidx = xc_program_index(vec->v, &i);
5209 nidx = xc_program_index(vec->v, &i);
5210
5211 v = bc_vec_item(&G.prog.arrs, vidx);
5212 v = bc_vec_item(v, nidx);
5213
5214 //assert(v->size != sizeof(uint8_t));
5215
5216 return v;
5217}
5218
5219static BcVec* xc_program_search(char *id, BcType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06005220{
Gavin Howard01055ba2018-11-03 11:00:21 -06005221 BcId e, *ptr;
5222 BcVec *v, *map;
5223 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005224 int new;
Denys Vlasenko53799502019-01-25 14:24:03 +01005225 bool var = (type == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005226
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005227 v = var ? &G.prog.vars : &G.prog.arrs;
5228 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005229
5230 e.name = id;
5231 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005232 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
5234 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005235 BcVec v2;
5236 bc_array_init(&v2, var);
5237 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005238 }
5239
5240 ptr = bc_vec_item(map, i);
5241 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005242 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005243}
5244
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005245// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005246static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005247{
Gavin Howard01055ba2018-11-03 11:00:21 -06005248 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005249 case XC_RESULT_STR:
5250 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005251 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005252 case XC_RESULT_IBASE:
5253 case XC_RESULT_SCALE:
5254 case XC_RESULT_OBASE:
5255 *num = &r->d.n;
5256 break;
5257 case XC_RESULT_CONSTANT: {
5258 BcStatus s;
5259 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005260 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005261
Denys Vlasenko10bde142018-12-27 18:23:58 +01005262 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005263 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005264
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005265 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005266
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005267 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005268 if (s) {
5269 bc_num_free(&r->d.n);
5270 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005271 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005272 *num = &r->d.n;
5273 r->t = XC_RESULT_TEMP;
5274 break;
5275 }
5276 case XC_RESULT_VAR:
5277 case XC_RESULT_ARRAY:
5278 case XC_RESULT_ARRAY_ELEM: {
Denys Vlasenko53799502019-01-25 14:24:03 +01005279 BcType type = (r->t == XC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY;
5280 BcVec *v = xc_program_search(r->d.id.name, type);
5281 void *p = bc_vec_top(v);
5282
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005283 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko53799502019-01-25 14:24:03 +01005284 size_t idx = r->d.id.idx;
5285
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005286 v = p;
Denys Vlasenko53799502019-01-25 14:24:03 +01005287 if (v->size == sizeof(uint8_t))
5288 v = xc_program_dereference(v);
5289 //assert(v->size == sizeof(BcNum));
5290 if (v->len <= idx)
5291 bc_array_expand(v, idx + 1);
5292 *num = bc_vec_item(v, idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005293 } else {
5294 *num = p;
5295 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005296 break;
5297 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005298#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005299 case BC_RESULT_LAST:
5300 *num = &G.prog.last;
5301 break;
5302 case BC_RESULT_ONE:
5303 *num = &G.prog.one;
5304 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005305#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005306#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005307 default:
5308 // Testing the theory that dc does not reach LAST/ONE
5309 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005310#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005311 }
5312
Denys Vlasenko29301232018-12-11 15:29:32 +01005313 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005314}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005315#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005316
Denys Vlasenko10bde142018-12-27 18:23:58 +01005317static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005318 BcResult **r, BcNum **rn, bool assign)
5319{
5320 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005321 BcResultType lt, rt;
5322
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005323 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005324 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005325
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005326 *r = bc_vec_item_rev(&G.prog.results, 0);
5327 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005328
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005329 s = zxc_program_num(*l, ln);
5330 if (s) RETURN_STATUS(s);
5331 s = zxc_program_num(*r, rn);
5332 if (s) RETURN_STATUS(s);
5333
Gavin Howard01055ba2018-11-03 11:00:21 -06005334 lt = (*l)->t;
5335 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005336
5337 // We run this again under these conditions in case any vector has been
5338 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005339 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005340 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005341 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005342 }
5343
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005344 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005345 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005346 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005347 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005348
Denys Vlasenko29301232018-12-11 15:29:32 +01005349 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005350}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005351#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005352
Denys Vlasenko10bde142018-12-27 18:23:58 +01005353static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005354{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005355 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005356 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005357 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005358}
5359
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005360// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005361static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005362{
5363 BcStatus s;
5364
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005365 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005366 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005367 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005368
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005369 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005370 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005371
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005372 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005373 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005374
Denys Vlasenko29301232018-12-11 15:29:32 +01005375 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005376}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005377#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005378
Denys Vlasenko10bde142018-12-27 18:23:58 +01005379static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005380{
5381 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005382 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005383}
5384
Denys Vlasenko10bde142018-12-27 18:23:58 +01005385static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005386{
5387 BcStatus s;
5388 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005389 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005390
Denys Vlasenko10bde142018-12-27 18:23:58 +01005391 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005392 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005393 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005394
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005395 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005396 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 -06005397 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005398 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005399
Denys Vlasenko259137d2018-12-11 19:42:05 +01005400 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005401 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005402 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005403 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005404}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005405#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005406
Denys Vlasenko10bde142018-12-27 18:23:58 +01005407static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005408{
5409 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005410 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005411 BcVec buf;
5412 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005413 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005414
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005415 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005416 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005417
Denys Vlasenko10bde142018-12-27 18:23:58 +01005418 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005419 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005420
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005421 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005422 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005423 //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 +01005424
Denys Vlasenko10bde142018-12-27 18:23:58 +01005425 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005426 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005427 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005428 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005429 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005430 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005431 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005432 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005433 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005434 s = bc_error_at("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005435 goto exec_err;
5436 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005437 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005438
5439 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005440 ip.inst_idx = 0;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005441 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005442
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005443 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005444 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005445 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005446 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005447 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005448}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005449#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005450
Denys Vlasenko10bde142018-12-27 18:23:58 +01005451static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005452{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005453#if ENABLE_DC
Denys Vlasenko266bec82019-01-02 05:03:53 +01005454 if (!str[0] && IS_DC) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005455 // Example: echo '[]ap' | dc
5456 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005457 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005458 return;
5459 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005460#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005461 while (*str) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005462 char c = *str++;
5463 if (c == '\\') {
5464 static const char esc[] ALIGN1 = "nabfrt""e\\";
5465 char *n;
5466
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005467 c = *str++;
Brian Foley10509a72019-09-05 10:53:21 +02005468 n = strchr(esc, c); // note: if c is NUL, n = \0 at end of esc
5469 if (!n || !c) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005470 // Just print the backslash and following character
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005471 bb_putchar('\\');
5472 ++G.prog.nchars;
Brian Foley10509a72019-09-05 10:53:21 +02005473 // But if we're at the end of the string, stop
5474 if (!c) break;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005475 } else {
5476 if (n - esc == 0) // "\n" ?
5477 G.prog.nchars = SIZE_MAX;
5478 c = "\n\a\b\f\r\t""\\\\""\\"[n - esc];
5479 // n a b f r t e \ \<end of line>
Gavin Howard01055ba2018-11-03 11:00:21 -06005480 }
5481 }
Denys Vlasenko266bec82019-01-02 05:03:53 +01005482 putchar(c);
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005483 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005484 }
5485}
5486
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005487static void bc_num_printNewline(void)
5488{
5489 if (G.prog.nchars == G.prog.len - 1) {
5490 bb_putchar('\\');
5491 bb_putchar('\n');
5492 G.prog.nchars = 0;
5493 }
5494}
5495
5496#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005497static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005498{
5499 (void) radix;
5500 bb_putchar((char) num);
5501 G.prog.nchars += width;
5502}
5503#endif
5504
5505static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5506{
5507 size_t exp, pow;
5508
5509 bc_num_printNewline();
5510 bb_putchar(radix ? '.' : ' ');
5511 ++G.prog.nchars;
5512
5513 bc_num_printNewline();
5514 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5515 continue;
5516
5517 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5518 size_t dig;
5519 bc_num_printNewline();
5520 dig = num / pow;
5521 num -= dig * pow;
5522 bb_putchar(((char) dig) + '0');
5523 }
5524}
5525
5526static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5527{
5528 if (radix) {
5529 bc_num_printNewline();
5530 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005531 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005532 }
5533
5534 bc_num_printNewline();
5535 bb_putchar(bb_hexdigits_upcase[num]);
5536 G.prog.nchars += width;
5537}
5538
5539static void bc_num_printDecimal(BcNum *n)
5540{
5541 size_t i, rdx = n->rdx - 1;
5542
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005543 if (n->neg) {
5544 bb_putchar('-');
5545 G.prog.nchars++;
5546 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005547
5548 for (i = n->len - 1; i < n->len; --i)
5549 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5550}
5551
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005552typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5553
Denys Vlasenko10bde142018-12-27 18:23:58 +01005554static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005555{
5556 BcStatus s;
5557 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005558 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005559 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005560 BcNum intp, fracp, digit, frac_len;
5561 unsigned long dig, *ptr;
5562 size_t i;
5563 bool radix;
5564
5565 if (n->len == 0) {
5566 print(0, width, false);
5567 RETURN_STATUS(BC_STATUS_SUCCESS);
5568 }
5569
5570 bc_vec_init(&stack, sizeof(long), NULL);
5571 bc_num_init(&intp, n->len);
5572 bc_num_init(&fracp, n->rdx);
5573 bc_num_init(&digit, width);
5574 bc_num_init(&frac_len, BC_NUM_INT(n));
5575 bc_num_copy(&intp, n);
5576 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005577 base.cap = ARRAY_SIZE(base_digs);
5578 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005579 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005580
5581 bc_num_truncate(&intp, intp.rdx);
5582 s = zbc_num_sub(n, &intp, &fracp, 0);
5583 if (s) goto err;
5584
5585 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005586 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005587 if (s) goto err;
5588 s = zbc_num_ulong(&digit, &dig);
5589 if (s) goto err;
5590 bc_vec_push(&stack, &dig);
5591 }
5592
5593 for (i = 0; i < stack.len; ++i) {
5594 ptr = bc_vec_item_rev(&stack, i);
5595 print(*ptr, width, false);
5596 }
5597
5598 if (!n->rdx) goto err;
5599
5600 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005601 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005602 if (s) goto err;
5603 s = zbc_num_ulong(&fracp, &dig);
5604 if (s) goto err;
5605 bc_num_ulong2num(&intp, dig);
5606 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5607 if (s) goto err;
5608 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005609 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005610 if (s) goto err;
5611 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005612 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005613 bc_num_free(&frac_len);
5614 bc_num_free(&digit);
5615 bc_num_free(&fracp);
5616 bc_num_free(&intp);
5617 bc_vec_free(&stack);
5618 RETURN_STATUS(s);
5619}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005620#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005621
Denys Vlasenko10bde142018-12-27 18:23:58 +01005622static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005623{
5624 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005625 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005626 BcNumDigitOp print;
5627 bool neg = n->neg;
5628
5629 if (neg) {
5630 bb_putchar('-');
5631 G.prog.nchars++;
5632 }
5633
5634 n->neg = false;
5635
Denys Vlasenko680ccd32018-12-31 19:42:13 +01005636 if (G.prog.ob_t <= 16) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005637 width = 1;
5638 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005639 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005640 unsigned i = G.prog.ob_t - 1;
5641 width = 0;
5642 for (;;) {
5643 width++;
5644 i /= 10;
5645 if (i == 0)
5646 break;
5647 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005648 print = bc_num_printDigits;
5649 }
5650
Denys Vlasenko10bde142018-12-27 18:23:58 +01005651 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005652 n->neg = neg;
5653
5654 RETURN_STATUS(s);
5655}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005656#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005657
Denys Vlasenko10bde142018-12-27 18:23:58 +01005658static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005659{
5660 BcStatus s = BC_STATUS_SUCCESS;
5661
5662 bc_num_printNewline();
5663
5664 if (n->len == 0) {
5665 bb_putchar('0');
5666 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005667 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005668 bc_num_printDecimal(n);
5669 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005670 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005671
5672 if (newline) {
5673 bb_putchar('\n');
5674 G.prog.nchars = 0;
5675 }
5676
5677 RETURN_STATUS(s);
5678}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005679#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005680
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005681#if !ENABLE_DC
5682// for bc, idx is always 0
5683#define xc_program_print(inst, idx) \
5684 xc_program_print(inst)
5685#endif
5686static BC_STATUS xc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005687{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005688 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005689 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005690 BcNum *num;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005691 IF_NOT_DC(size_t idx = 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005692
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005693 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005694 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005695
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005696 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005697#if ENABLE_BC
5698 if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID)
5699 // void function's result on stack, ignore
5700 RETURN_STATUS(BC_STATUS_SUCCESS);
5701#endif
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005702 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005703 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005704
5705 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005706 s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005707#if ENABLE_BC
5708 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5709#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005710 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005711 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005712
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005713 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005714 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005715
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005716 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005717 char *nl;
5718 G.prog.nchars += printf("%s", str);
5719 nl = strrchr(str, '\n');
5720 if (nl)
5721 G.prog.nchars = strlen(nl + 1);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005722 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005723 xc_program_printString(str);
Denys Vlasenko266bec82019-01-02 05:03:53 +01005724 if (inst == XC_INST_PRINT)
5725 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005726 }
5727 }
5728
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005729 if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005730
Denys Vlasenko29301232018-12-11 15:29:32 +01005731 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005732}
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005733#define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005734
Denys Vlasenko10bde142018-12-27 18:23:58 +01005735static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005736{
5737 BcStatus s;
5738 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005739 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
Denys Vlasenko10bde142018-12-27 18:23:58 +01005741 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005742 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005743
5744 bc_num_init(&res.d.n, num->len);
5745 bc_num_copy(&res.d.n, num);
5746 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5747
Denys Vlasenko10bde142018-12-27 18:23:58 +01005748 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005749
Denys Vlasenko29301232018-12-11 15:29:32 +01005750 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005751}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005752#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005753
Denys Vlasenko10bde142018-12-27 18:23:58 +01005754static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005755{
5756 BcStatus s;
5757 BcResult *opd1, *opd2, res;
5758 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005759 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005760
Denys Vlasenko10bde142018-12-27 18:23:58 +01005761 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005762 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005763
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005764 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005765
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005766 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005767 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005768 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005769 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005770 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005771 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005772 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005773 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005774 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005775 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005776 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005777 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005778 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005779 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005780 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005781 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005782 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005783 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005784 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005785 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005786 cond = (cond > 0);
5787 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005788 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005789 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005790 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005791 }
5792 }
5793
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005794 if (cond) bc_num_one(&res.d.n);
5795 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005796
Denys Vlasenko10bde142018-12-27 18:23:58 +01005797 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005798
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005799 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005800}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005801#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005802
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005803#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005804static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005805{
5806 BcNum n2;
5807 BcResult res;
5808
5809 memset(&n2, 0, sizeof(BcNum));
5810 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005811 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005812
5813 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005814 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005815 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005816 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005817 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005818 }
5819
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005820 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005821 bc_vec_push(v, &n2);
5822
Denys Vlasenko29301232018-12-11 15:29:32 +01005823 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005824}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005825#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005826#endif // ENABLE_DC
5827
Denys Vlasenko53799502019-01-25 14:24:03 +01005828static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, BcType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005829{
5830 BcStatus s;
5831 BcResult *ptr, r;
Denys Vlasenko53799502019-01-25 14:24:03 +01005832 BcVec *vec;
Gavin Howard01055ba2018-11-03 11:00:21 -06005833 BcNum *n;
Denys Vlasenko53799502019-01-25 14:24:03 +01005834 bool var = (t == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005835
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005836 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005837 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005838
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005839 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko53799502019-01-25 14:24:03 +01005840 if ((ptr->t == XC_RESULT_ARRAY) == var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005841 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005842 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005843
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005844#if ENABLE_DC
Denys Vlasenko53799502019-01-25 14:24:03 +01005845 if (ptr->t == XC_RESULT_STR) {
5846 if (!var)
5847 RETURN_STATUS(bc_error_variable_is_wrong_type());
5848 RETURN_STATUS(zdc_program_assignStr(ptr, vec, true));
5849 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005850#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005851
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005852 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005853 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005854
5855 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko53799502019-01-25 14:24:03 +01005856 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005857
5858 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005859 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005860 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005861 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01005862 BcVec *v = (BcVec*) n;
5863 bool ref, ref_size;
Gavin Howard01055ba2018-11-03 11:00:21 -06005864
Denys Vlasenko53799502019-01-25 14:24:03 +01005865 ref = (v->size == sizeof(BcVec) && t != BC_TYPE_ARRAY);
5866 ref_size = (v->size == sizeof(uint8_t));
5867
5868 if (ref || (ref_size && t == BC_TYPE_REF)) {
5869 bc_vec_init(&r.d.v, sizeof(uint8_t), NULL);
5870 if (ref) {
5871 size_t vidx, idx;
5872 BcId id;
5873
5874 id.name = ptr->d.id.name;
5875 v = xc_program_search(ptr->d.id.name, BC_TYPE_REF);
5876
5877 // Make sure the pointer was not invalidated.
5878 vec = xc_program_search(name, t);
5879
5880 vidx = bc_map_find_exact(&G.prog.arr_map, &id);
5881 //assert(vidx != BC_VEC_INVALID_IDX);
5882 vidx = ((BcId*) bc_vec_item(&G.prog.arr_map, vidx))->idx;
5883 idx = v->len - 1;
5884
5885 bc_vec_pushIndex(&r.d.v, vidx);
5886 bc_vec_pushIndex(&r.d.v, idx);
5887 }
5888 // If we get here, we are copying a ref to a ref.
5889 else bc_vec_npush(&r.d.v, v->len, v->v);
5890
5891 // We need to return early.
5892 goto ret;
5893 }
5894
5895 if (ref_size && t != BC_TYPE_REF)
5896 v = xc_program_dereference(v);
5897
5898 bc_array_init(&r.d.v, true);
5899 bc_array_copy(&r.d.v, v);
5900 }
5901 ret:
5902 bc_vec_push(vec, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005903 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005904
Denys Vlasenko29301232018-12-11 15:29:32 +01005905 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005906}
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005907#define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005908
Denys Vlasenko10bde142018-12-27 18:23:58 +01005909static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005910{
5911 BcStatus s;
5912 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005913 BcNum *l, *r;
5914 bool assign = (inst == XC_INST_ASSIGN);
5915 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005916
Denys Vlasenko10bde142018-12-27 18:23:58 +01005917 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005918 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005919
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005920 ib = left->t == XC_RESULT_IBASE;
5921 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005922
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005923#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005924 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 BcVec *v;
5926
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005927 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005928 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005929 v = xc_program_search(left->d.id.name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005930
Denys Vlasenkofa210792018-12-19 19:35:40 +01005931 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005932 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005933#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005934
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005935 if (left->t == XC_RESULT_CONSTANT
5936 || left->t == XC_RESULT_TEMP
5937 IF_BC(|| left->t == BC_RESULT_VOID)
5938 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005939 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005940 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005941
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005942#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 if (assign)
5944 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005945 else {
5946 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005947 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 +01005948 }
5949 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005950#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005951 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005952#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005953
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005954 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005955 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005956 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5957 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5958 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005959 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005960 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005961 size_t max;
5962 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005963
Denys Vlasenko29301232018-12-11 15:29:32 +01005964 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005965 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005966 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005967 if (sc) {
5968 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005969 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005970 } else {
5971 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005972 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005973 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005974 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005975 }
5976
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005977 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005978 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005979
5980 *ptr = (size_t) val;
5981 s = BC_STATUS_SUCCESS;
5982 }
5983
5984 bc_num_init(&res.d.n, l->len);
5985 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005986 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005987
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005988 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005989}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005990#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005991
Denys Vlasenko416ce762018-12-02 20:57:17 +01005992#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005993#define xc_program_pushVar(code, bgn, pop, copy) \
5994 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005995// for bc, 'pop' and 'copy' are always false
5996#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005997static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005998 bool pop, bool copy)
5999{
Gavin Howard01055ba2018-11-03 11:00:21 -06006000 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006001 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006002
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006003 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06006004 r.d.id.name = name;
6005
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006006#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006007 if (pop || copy) {
Denys Vlasenko53799502019-01-25 14:24:03 +01006008 BcVec *v = xc_program_search(name, BC_TYPE_VAR);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006009 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006010
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006011 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006012 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006013 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006014 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006015
6016 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006017 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006018 bc_num_init_DEF_SIZE(&r.d.n);
6019 bc_num_copy(&r.d.n, num);
6020 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006021 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006022 r.d.id.idx = num->rdx;
6023 }
6024
6025 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 }
6027#endif // ENABLE_DC
6028
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006029 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006030
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006031 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006032}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006033#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006034
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006035static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006036{
6037 BcStatus s = BC_STATUS_SUCCESS;
6038 BcResult r;
6039 BcNum *num;
6040
Denys Vlasenko10bde142018-12-27 18:23:58 +01006041 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006042
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006043 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006044 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006045 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006046 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06006047 BcResult *operand;
6048 unsigned long temp;
6049
Denys Vlasenko10bde142018-12-27 18:23:58 +01006050 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006051 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006052 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006053 if (s) goto err;
6054
6055 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006056 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 goto err;
6058 }
6059
6060 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006061 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006062 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006063 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006064 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006065 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006066}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006067#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006068
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006069#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006070static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006071{
6072 BcStatus s;
6073 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006074 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006075 char inst2 = inst;
6076
Denys Vlasenko10bde142018-12-27 18:23:58 +01006077 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006078 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006079
6080 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006081 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006082 bc_num_init(&copy.d.n, num->len);
6083 bc_num_copy(&copy.d.n, num);
6084 }
6085
6086 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006087 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
6088 ? BC_INST_ASSIGN_PLUS
6089 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006090
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006091 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006092 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006093 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006094
6095 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006096 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 }
6098
Denys Vlasenko29301232018-12-11 15:29:32 +01006099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006100}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006101#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006102
Denys Vlasenko29301232018-12-11 15:29:32 +01006103static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006104{
Gavin Howard01055ba2018-11-03 11:00:21 -06006105 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006106 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006107 BcId *a;
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006108 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006109
Denys Vlasenko10bde142018-12-27 18:23:58 +01006110 nparams = xc_program_index(code, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006111 ip.func = xc_program_index(code, idx);
6112 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006114 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006115 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006116 }
6117 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006118 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006119 }
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006120 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006121
6122 for (i = 0; i < nparams; ++i) {
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006123 BcResult *arg;
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006124 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01006125 bool arr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006126
6127 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006128 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006129
Denys Vlasenko53799502019-01-25 14:24:03 +01006130 arr = (a->idx == BC_TYPE_ARRAY || a->idx == BC_TYPE_REF);
6131
6132 if (arr != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006133 // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions)
6134 ) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006135 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006136 }
Denys Vlasenko53799502019-01-25 14:24:03 +01006137 s = zxc_program_popResultAndCopyToVar(a->name, (BcType) a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006138 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006139 }
6140
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006141 a = bc_vec_item(&func->autos, i);
6142 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006143 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006144
Denys Vlasenko53799502019-01-25 14:24:03 +01006145 v = xc_program_search(a->name, (BcType) a->idx);
6146 if (a->idx == BC_TYPE_VAR) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006147 BcNum n2;
6148 bc_num_init_DEF_SIZE(&n2);
6149 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006150 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01006151 //assert(a->idx == BC_TYPE_ARRAY);
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006152 BcVec v2;
6153 bc_array_init(&v2, true);
6154 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006155 }
6156 }
6157
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006158 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006159
Denys Vlasenko29301232018-12-11 15:29:32 +01006160 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006161}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006162#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006163
Denys Vlasenko29301232018-12-11 15:29:32 +01006164static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006165{
Gavin Howard01055ba2018-11-03 11:00:21 -06006166 BcResult res;
6167 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006168 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006169 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006170 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006171
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006172 f = xc_program_func(ip->func);
6173
6174 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006175 if (inst == XC_INST_RET) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006176 // bc needs this for e.g. RESULT_CONSTANT ("return 5")
6177 // because bc constants are per-function.
6178 // TODO: maybe avoid if value is already RESULT_TEMP?
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006179 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006180 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006181 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006182
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006183 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006184 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006185 bc_num_init(&res.d.n, num->len);
6186 bc_num_copy(&res.d.n, num);
Denys Vlasenko377cc972019-01-04 00:34:52 +01006187 bc_vec_pop(&G.prog.results);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006188 } else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006189 if (f->voidfunc)
6190 res.t = BC_RESULT_VOID;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006191 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006192 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006193 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006194 bc_vec_push(&G.prog.results, &res);
6195
6196 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006197
6198 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006199 a = (void*)f->autos.v;
6200 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006201 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006202 v = xc_program_search(a->name, (BcType) a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 bc_vec_pop(v);
6204 }
6205
Denys Vlasenko29301232018-12-11 15:29:32 +01006206 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006207}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006208#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006209#endif // ENABLE_BC
6210
Denys Vlasenko10bde142018-12-27 18:23:58 +01006211static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006212{
6213 return (unsigned long) n->rdx;
6214}
6215
Denys Vlasenko10bde142018-12-27 18:23:58 +01006216static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006217{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006218 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006219
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006220 if (n->rdx != len) return len;
6221 for (;;) {
6222 if (len == 0) break;
6223 len--;
6224 if (n->num[len] != 0) break;
6225 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006226 return len;
6227}
6228
Denys Vlasenko10bde142018-12-27 18:23:58 +01006229static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006230{
6231 BcStatus s;
6232 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006233 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006234 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006235 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006236
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006237 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006238 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006239 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006240
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006241 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006242 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006243
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006244#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006245 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006246 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006247#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006248
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006249 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006250
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006251 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006252 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006253#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006254 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006255 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006256 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006257#endif
6258#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006259 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006260 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006261 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006262
Denys Vlasenko10bde142018-12-27 18:23:58 +01006263 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006264 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006265 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006266#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006267 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006268 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006269 }
6270
Denys Vlasenko10bde142018-12-27 18:23:58 +01006271 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006272
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006273 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006274}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006275#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006276
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006277#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006278static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006279{
6280 BcStatus s;
6281 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006282 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006283
Denys Vlasenko10bde142018-12-27 18:23:58 +01006284 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006285 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006286
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006287 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006288 bc_num_init(&res2.d.n, n2->len);
6289
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006290 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006291 if (s) goto err;
6292
Denys Vlasenko10bde142018-12-27 18:23:58 +01006293 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006294 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006295 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006296
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006297 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006298 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 bc_num_free(&res2.d.n);
6300 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006301 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006302}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006303#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
Denys Vlasenkofa210792018-12-19 19:35:40 +01006305static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006306{
6307 BcStatus s;
6308 BcResult *r1, *r2, *r3, res;
6309 BcNum *n1, *n2, *n3;
6310
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006311 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006312 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006313 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006314 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006315
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006316 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006317 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006318 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006319 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006320 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006321
6322 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006323 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006324 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006325 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006326 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006328 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006329 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006330 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006331 }
6332 }
6333
6334 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006335 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336 if (s) goto err;
6337
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006338 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006339 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006340
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006341 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006342 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006343 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006344 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006345}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006346#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006347
Denys Vlasenkofa210792018-12-19 19:35:40 +01006348static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006349{
Gavin Howard01055ba2018-11-03 11:00:21 -06006350 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006351 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006352
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006353 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006354
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006355 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006356 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006357 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006358}
6359
Denys Vlasenkofa210792018-12-19 19:35:40 +01006360static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006361{
6362 BcStatus s;
6363 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006364 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006365 char **strs;
6366 char *str;
6367 char c;
6368 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006369
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006370 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006371 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006372
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006373 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006374 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006375 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006376
6377 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006378 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006379 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006380 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006381
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006382 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006383 bc_num_copy(&n, num);
6384 bc_num_truncate(&n, n.rdx);
6385
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006386 strmb.cap = ARRAY_SIZE(strmb_digs);
6387 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006388 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006389
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006390 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006392 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006393 if (s) goto num_err;
6394
6395 c = (char) val;
6396
6397 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006398 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006399 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006400 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006401 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006402 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 }
6404
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006405 strs = (void*)G.prog.strs.v;
6406 for (idx = 0; idx < G.prog.strs.len; idx++) {
6407 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6408 goto dup;
6409 }
6410 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006411 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006412 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006413 //str[1] = '\0'; - already is
Brian Foleyb64470b2019-09-05 10:50:13 +02006414 idx = bc_vec_push(&G.prog.strs, &str);
6415 // Add an empty function so that if zdc_program_execStr ever needs to
6416 // parse the string into code (from the 'x' command) there's somewhere
6417 // to store the bytecode.
6418 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006419 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006420 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006421 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006422 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006423
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006424 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006425 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006427 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006428}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006429#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006430
Denys Vlasenkofa210792018-12-19 19:35:40 +01006431static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006432{
6433 BcStatus s;
6434 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006435 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006436 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006437
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006438 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006439 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006440 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006441
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006442 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006443 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006444
Denys Vlasenko57734c92018-12-17 21:14:05 +01006445 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006446 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006447 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006448 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006449 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006450 str = *xc_program_str(idx);
Ron Yorstoncad3fc72021-02-03 20:47:14 +01006451 fputs_stdout(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 }
6453
Denys Vlasenko29301232018-12-11 15:29:32 +01006454 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006455}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006456#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
Denys Vlasenkofa210792018-12-19 19:35:40 +01006458static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006459{
6460 BcStatus s;
6461 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006462 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006463 unsigned long val;
6464
Denys Vlasenko10bde142018-12-27 18:23:58 +01006465 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006466 if (s) RETURN_STATUS(s);
6467 s = zbc_num_ulong(num, &val);
6468 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006469
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006470 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006471
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006472 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006473 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006474 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006475 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006476 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006477
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006478 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006479
Denys Vlasenko29301232018-12-11 15:29:32 +01006480 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006482#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006483
Denys Vlasenkofa210792018-12-19 19:35:40 +01006484static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006485{
6486 BcStatus s = BC_STATUS_SUCCESS;
6487 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006488 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006489 BcInstPtr ip;
6490 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006491
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006492 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006493 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006494
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006495 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006496
6497 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006498 BcNum *n = n; // for compiler
6499 bool exec;
6500 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006501 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006502 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006503
Denys Vlasenkof706a182018-12-26 20:02:09 +01006504 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006505 (*bgn) += 1;
6506 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006507 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006508
6509 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006510 name = then_name;
6511 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006512 exec = true;
6513 name = else_name;
6514 }
6515
6516 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006517 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006518 v = xc_program_search(name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 n = bc_vec_top(v);
6520 }
6521
6522 free(then_name);
6523 free(else_name);
6524
6525 if (!exec) goto exit;
6526 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006527 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006528 goto exit;
6529 }
6530
6531 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006532 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006533 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006534 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006535 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006536 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006537 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006538 if (s || !BC_PROG_STR(n)) goto exit;
6539 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006540 } else
Brian Foley74548792019-09-05 10:46:22 +02006541 goto exit_nopop;
Gavin Howard01055ba2018-11-03 11:00:21 -06006542 }
6543
6544 fidx = sidx + BC_PROG_REQ_FUNCS;
6545
Denys Vlasenko10bde142018-12-27 18:23:58 +01006546 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006547
6548 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006549 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006550 char *str;
6551
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006552 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006553 xc_parse_create(fidx);
6554 str = *xc_program_str(sidx);
6555 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006556 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006557
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006558 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006560 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006561 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006562 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006563 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006564 G.prs = sv_parse; // struct copy
6565 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006566 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006567 bc_vec_pop_all(&f->code);
6568 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006570 }
6571
Denys Vlasenko24e41942018-12-21 23:01:26 +01006572 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006573 ip.func = fidx;
6574
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006575 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006576 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006577
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006578 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006579 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006580 bc_vec_pop(&G.prog.results);
Brian Foley74548792019-09-05 10:46:22 +02006581 exit_nopop:
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006582 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006583}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006584#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006585#endif // ENABLE_DC
6586
Denys Vlasenko10bde142018-12-27 18:23:58 +01006587static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006588{
Gavin Howard01055ba2018-11-03 11:00:21 -06006589 BcResult res;
6590 unsigned long val;
6591
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006592 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006593 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006594 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006595 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006596 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006598 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006599
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006600 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006601 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006602 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006603}
6604
Denys Vlasenko10bde142018-12-27 18:23:58 +01006605static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006606{
Gavin Howard01055ba2018-11-03 11:00:21 -06006607 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006608 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006609 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006610 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006611
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006612 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006613 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6614 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006615 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006616 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006617
Denys Vlasenko24e41942018-12-21 23:01:26 +01006618 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 -06006619 switch (inst) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006620 case XC_INST_RET:
6621 if (IS_DC) { // end of '?' reached
6622 bc_vec_pop(&G.prog.exestack);
6623 goto read_updated_ip;
6624 }
6625 // bc: fall through
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006626#if ENABLE_BC
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006627 case BC_INST_RET0:
6628 dbg_exec("BC_INST_RET[0]:");
6629 s = zbc_program_return(inst);
6630 goto read_updated_ip;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006631 case BC_INST_JUMP_ZERO: {
6632 BcNum *num;
6633 bool zero;
6634 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006635 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006636 if (s) RETURN_STATUS(s);
6637 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6638 bc_vec_pop(&G.prog.results);
6639 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006640 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 break;
6642 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006643 // else: fall through
6644 }
6645 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006646 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006647 size_t *addr = bc_vec_item(&func->labels, idx);
6648 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6649 ip->inst_idx = *addr;
6650 break;
6651 }
6652 case BC_INST_CALL:
6653 dbg_exec("BC_INST_CALL:");
6654 s = zbc_program_call(code, &ip->inst_idx);
6655 goto read_updated_ip;
6656 case BC_INST_INC_PRE:
6657 case BC_INST_DEC_PRE:
6658 case BC_INST_INC_POST:
6659 case BC_INST_DEC_POST:
6660 dbg_exec("BC_INST_INCDEC:");
6661 s = zbc_program_incdec(inst);
6662 break;
6663 case BC_INST_HALT:
6664 dbg_exec("BC_INST_HALT:");
6665 QUIT_OR_RETURN_TO_MAIN;
6666 break;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006667 case XC_INST_BOOL_OR:
6668 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006669#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006670 case XC_INST_REL_EQ:
6671 case XC_INST_REL_LE:
6672 case XC_INST_REL_GE:
6673 case XC_INST_REL_NE:
6674 case XC_INST_REL_LT:
6675 case XC_INST_REL_GT:
6676 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006677 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006678 break;
6679 case XC_INST_READ:
6680 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006681 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006682 goto read_updated_ip;
6683 case XC_INST_VAR:
6684 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006685 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006686 break;
6687 case XC_INST_ARRAY_ELEM:
6688 case XC_INST_ARRAY:
6689 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6690 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6691 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006692#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006693 case BC_INST_LAST:
6694 dbg_exec("BC_INST_LAST:");
6695 r.t = BC_RESULT_LAST;
6696 bc_vec_push(&G.prog.results, &r);
6697 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006698#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006699 case XC_INST_IBASE:
6700 case XC_INST_OBASE:
6701 case XC_INST_SCALE:
6702 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006703 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006704 break;
6705 case XC_INST_SCALE_FUNC:
6706 case XC_INST_LENGTH:
6707 case XC_INST_SQRT:
6708 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006709 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006710 break;
6711 case XC_INST_NUM:
6712 dbg_exec("XC_INST_NUM:");
6713 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006714 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006715 bc_vec_push(&G.prog.results, &r);
6716 break;
6717 case XC_INST_POP:
6718 dbg_exec("XC_INST_POP:");
6719 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6720 s = bc_error_stack_has_too_few_elements();
6721 else
6722 bc_vec_pop(&G.prog.results);
6723 break;
6724 case XC_INST_PRINT:
6725 case XC_INST_PRINT_POP:
6726 case XC_INST_PRINT_STR:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006727 dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006728 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006729 break;
6730 case XC_INST_STR:
6731 dbg_exec("XC_INST_STR:");
6732 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006733 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006734 bc_vec_push(&G.prog.results, &r);
6735 break;
6736 case XC_INST_POWER:
6737 case XC_INST_MULTIPLY:
6738 case XC_INST_DIVIDE:
6739 case XC_INST_MODULUS:
6740 case XC_INST_PLUS:
6741 case XC_INST_MINUS:
6742 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006743 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006744 break;
6745 case XC_INST_BOOL_NOT: {
6746 BcNum *num;
6747 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006748 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006749 if (s) RETURN_STATUS(s);
6750 bc_num_init_DEF_SIZE(&r.d.n);
6751 if (bc_num_cmp(num, &G.prog.zero) == 0)
6752 bc_num_one(&r.d.n);
6753 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006754 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006755 break;
6756 }
6757 case XC_INST_NEG:
6758 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006759 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006760 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006761#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006762 case BC_INST_ASSIGN_POWER:
6763 case BC_INST_ASSIGN_MULTIPLY:
6764 case BC_INST_ASSIGN_DIVIDE:
6765 case BC_INST_ASSIGN_MODULUS:
6766 case BC_INST_ASSIGN_PLUS:
6767 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006768#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006769 case XC_INST_ASSIGN:
6770 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006771 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006772 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006773#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006774 case DC_INST_POP_EXEC:
6775 dbg_exec("DC_INST_POP_EXEC:");
6776 bc_vec_pop(&G.prog.exestack);
6777 goto read_updated_ip;
6778 case DC_INST_MODEXP:
6779 dbg_exec("DC_INST_MODEXP:");
6780 s = zdc_program_modexp();
6781 break;
6782 case DC_INST_DIVMOD:
6783 dbg_exec("DC_INST_DIVMOD:");
6784 s = zdc_program_divmod();
6785 break;
6786 case DC_INST_EXECUTE:
6787 case DC_INST_EXEC_COND:
6788 dbg_exec("DC_INST_EXEC[_COND]:");
6789 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6790 goto read_updated_ip;
6791 case DC_INST_PRINT_STACK: {
6792 size_t idx;
6793 dbg_exec("DC_INST_PRINT_STACK:");
6794 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006795 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006796 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006797 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006798 break;
6799 }
6800 case DC_INST_CLEAR_STACK:
6801 dbg_exec("DC_INST_CLEAR_STACK:");
6802 bc_vec_pop_all(&G.prog.results);
6803 break;
6804 case DC_INST_STACK_LEN:
6805 dbg_exec("DC_INST_STACK_LEN:");
6806 dc_program_stackLen();
6807 break;
6808 case DC_INST_DUPLICATE:
6809 dbg_exec("DC_INST_DUPLICATE:");
6810 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6811 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6812 ptr = bc_vec_top(&G.prog.results);
6813 dc_result_copy(&r, ptr);
6814 bc_vec_push(&G.prog.results, &r);
6815 break;
6816 case DC_INST_SWAP: {
6817 BcResult *ptr2;
6818 dbg_exec("DC_INST_SWAP:");
6819 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6820 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6821 ptr = bc_vec_item_rev(&G.prog.results, 0);
6822 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6823 memcpy(&r, ptr, sizeof(BcResult));
6824 memcpy(ptr, ptr2, sizeof(BcResult));
6825 memcpy(ptr2, &r, sizeof(BcResult));
6826 break;
6827 }
6828 case DC_INST_ASCIIFY:
6829 dbg_exec("DC_INST_ASCIIFY:");
6830 s = zdc_program_asciify();
6831 break;
6832 case DC_INST_PRINT_STREAM:
6833 dbg_exec("DC_INST_PRINT_STREAM:");
6834 s = zdc_program_printStream();
6835 break;
6836 case DC_INST_LOAD:
6837 case DC_INST_PUSH_VAR: {
6838 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006839 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006840 break;
6841 }
6842 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006843 char *name = xc_program_name(code, &ip->inst_idx);
Denys Vlasenko53799502019-01-25 14:24:03 +01006844 s = zxc_program_popResultAndCopyToVar(name, BC_TYPE_VAR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006845 free(name);
6846 break;
6847 }
6848 case DC_INST_QUIT:
6849 dbg_exec("DC_INST_QUIT:");
6850 if (G.prog.exestack.len <= 2)
6851 QUIT_OR_RETURN_TO_MAIN;
6852 bc_vec_npop(&G.prog.exestack, 2);
6853 goto read_updated_ip;
6854 case DC_INST_NQUIT:
6855 dbg_exec("DC_INST_NQUIT:");
6856 s = zdc_program_nquit();
6857 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006858#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006859 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006860 // Instruction stack has changed, read new pointers
6861 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006862 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006863 code = func->code.v;
6864 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006865 }
6866
Denys Vlasenkod38af482018-12-04 19:11:02 +01006867 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006868 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006869 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006870 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006871
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006872 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 }
6874
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006875 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006876}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006877#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006878
Denys Vlasenko10bde142018-12-27 18:23:58 +01006879static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006880{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006881 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006882
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006883 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006884 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006885 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006886
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006887 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006888 BcInstPtr *ip;
6889 BcFunc *f;
6890
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006891 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006892 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006893#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006894 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006895 if (s) goto err;
6896
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006897 // Check that next token is a correct stmt delimiter -
6898 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006899 if (G.prs.lex != BC_LEX_SCOLON
6900 && G.prs.lex != XC_LEX_NLINE
6901 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006902 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01006903 bc_error_at("bad statement terminator");
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006904 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006905 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006906 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006907 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006908#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006909 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006910#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006911 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006912#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006913 }
6914 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006915 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006916 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006917 RETURN_STATUS(BC_STATUS_FAILURE);
6918 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006919
Denys Vlasenko39287e02018-12-22 02:23:08 +01006920 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006921 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006922 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006923 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006924 break;
6925 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006926
6927 ip = (void*)G.prog.exestack.v;
6928#if SANITY_CHECKS
6929 if (G.prog.exestack.len != 1) // should have only main's IP
James Byrne69374872019-07-02 11:35:03 +02006930 bb_simple_error_msg_and_die("BUG:call stack");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006931 if (ip->func != BC_PROG_MAIN)
James Byrne69374872019-07-02 11:35:03 +02006932 bb_simple_error_msg_and_die("BUG:not MAIN");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006933#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006934 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006935 // bc discards strings, constants and code after each
6936 // top-level statement in the "main program".
6937 // This prevents "yes 1 | bc" from growing its memory
6938 // without bound. This can be done because data stack
6939 // is empty and thus can't hold any references to
6940 // strings or constants, there is no generated code
6941 // which can hold references (after we discard one
6942 // we just executed). Code of functions can have references,
6943 // but bc stores function strings/constants in per-function
6944 // storage.
6945 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006946#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006947 if (G.prog.results.len != 0) // should be empty
James Byrne69374872019-07-02 11:35:03 +02006948 bb_simple_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006949#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006950 IF_BC(bc_vec_pop_all(&f->strs);)
6951 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01006952 // We are at SCOLON/NLINE, skip it:
6953 s = zxc_lex_next();
6954 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006955 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006956 if (G.prog.results.len == 0
6957 && G.prog.vars.len == 0
6958 ) {
6959 // If stack is empty and no registers exist (TODO: or they are all empty),
6960 // we can get rid of accumulated strings and constants.
6961 // In this example dc process should not grow
6962 // its memory consumption with time:
6963 // yes 1pc | dc
6964 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6965 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6966 }
6967 // The code is discarded always (below), thus this example
6968 // should also not grow its memory consumption with time,
6969 // even though its data stack is not empty:
6970 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006971 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006972 // We drop generated and executed code for both bc and dc:
6973 bc_vec_pop_all(&f->code);
6974 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006975 }
Denys Vlasenko6842c602019-01-04 05:41:47 +01006976
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006977 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006978 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006979}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006980#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006981
Denys Vlasenko10bde142018-12-27 18:23:58 +01006982static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006983{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006984 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006985 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006986 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006987 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006988
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006989 G.prs.lex_filename = filename;
6990 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006991 G.err_line = G.prs.lex_line = 1;
Denys Vlasenko22314682019-01-01 21:50:14 +01006992 dbg_lex("p->lex_line reset to 1");
Gavin Howard01055ba2018-11-03 11:00:21 -06006993
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006994 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006995 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006996 // We do not stop looping on errors here if reading stdin.
6997 // Example: start interactive bc and enter "return".
6998 // It should say "'return' not in a function"
6999 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01007000 } while (G.prs.lex_input_fp == stdin);
7001 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007002 RETURN_STATUS(s);
7003}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007004#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007005
Denys Vlasenko10bde142018-12-27 18:23:58 +01007006static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007007{
7008 BcStatus s;
7009 FILE *fp;
7010
7011 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007012 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007013 fclose(fp);
7014
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007015 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007016}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007017#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007018
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007019#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01007020static void bc_vm_info(void)
7021{
7022 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07007023 "Adapted from https://github.com/gavinhoward/bc\n"
7024 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01007025 , applet_name);
7026}
7027
7028static void bc_args(char **argv)
7029{
7030 unsigned opts;
7031 int i;
7032
7033 GETOPT_RESET();
7034#if ENABLE_FEATURE_BC_LONG_OPTIONS
7035 opts = option_mask32 |= getopt32long(argv, "wvsqli",
7036 "warn\0" No_argument "w"
7037 "version\0" No_argument "v"
7038 "standard\0" No_argument "s"
7039 "quiet\0" No_argument "q"
7040 "mathlib\0" No_argument "l"
7041 "interactive\0" No_argument "i"
7042 );
7043#else
7044 opts = option_mask32 |= getopt32(argv, "wvsqli");
7045#endif
7046 if (getenv("POSIXLY_CORRECT"))
7047 option_mask32 |= BC_FLAG_S;
7048
7049 if (opts & BC_FLAG_V) {
7050 bc_vm_info();
7051 exit(0);
7052 }
7053
7054 for (i = optind; argv[i]; ++i)
7055 bc_vec_push(&G.files, argv + i);
7056}
7057
7058static void bc_vm_envArgs(void)
7059{
7060 BcVec v;
7061 char *buf;
7062 char *env_args = getenv("BC_ENV_ARGS");
7063
7064 if (!env_args) return;
7065
7066 G.env_args = xstrdup(env_args);
7067 buf = G.env_args;
7068
7069 bc_vec_init(&v, sizeof(char *), NULL);
7070
7071 while (*(buf = skip_whitespace(buf)) != '\0') {
7072 bc_vec_push(&v, &buf);
7073 buf = skip_non_whitespace(buf);
7074 if (!*buf)
7075 break;
7076 *buf++ = '\0';
7077 }
7078
7079 // NULL terminate, and pass argv[] so that first arg is argv[1]
7080 if (sizeof(int) == sizeof(char*)) {
7081 bc_vec_push(&v, &const_int_0);
7082 } else {
7083 static char *const nullptr = NULL;
7084 bc_vec_push(&v, &nullptr);
7085 }
7086 bc_args(((char **)v.v) - 1);
7087
7088 bc_vec_free(&v);
7089}
7090
7091static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007092 "scale=20"
7093"\n" "define e(x){"
7094"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007095////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7096//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7097//e(-.998896): GNU:.36828580434569428695
7098// above code:.36828580434569428696
7099// actual value:.3682858043456942869594...
7100// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007101"\n" "b=ibase"
7102"\n" "ibase=A"
7103"\n" "if(x<0){"
7104"\n" "n=1"
7105"\n" "x=-x"
7106"\n" "}"
7107"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007108"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007109"\n" "scale=scale(x)+1"
7110"\n" "while(x>1){"
7111"\n" "d+=1"
7112"\n" "x/=2"
7113"\n" "scale+=1"
7114"\n" "}"
7115"\n" "scale=r"
7116"\n" "r=x+1"
7117"\n" "p=x"
7118"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007119"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007120"\n" "p*=x"
7121"\n" "f*=i"
7122"\n" "v=p/f"
7123"\n" "r+=v"
7124"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007125"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007126"\n" "scale=s"
7127"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007128"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007129"\n" "return(r/1)"
7130"\n" "}"
7131"\n" "define l(x){"
7132"\n" "auto b,s,r,p,a,q,i,v"
7133"\n" "b=ibase"
7134"\n" "ibase=A"
7135"\n" "if(x<=0){"
7136"\n" "r=(1-10^scale)/1"
7137"\n" "ibase=b"
7138"\n" "return(r)"
7139"\n" "}"
7140"\n" "s=scale"
7141"\n" "scale+=6"
7142"\n" "p=2"
7143"\n" "while(x>=2){"
7144"\n" "p*=2"
7145"\n" "x=sqrt(x)"
7146"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007147"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007148"\n" "p*=2"
7149"\n" "x=sqrt(x)"
7150"\n" "}"
7151"\n" "r=a=(x-1)/(x+1)"
7152"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007153"\n" "v=1"
7154"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007155"\n" "a*=q"
7156"\n" "v=a/i"
7157"\n" "r+=v"
7158"\n" "}"
7159"\n" "r*=p"
7160"\n" "scale=s"
7161"\n" "ibase=b"
7162"\n" "return(r/1)"
7163"\n" "}"
7164"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007165"\n" "auto b,s,r,a,q,i"
7166"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007167"\n" "b=ibase"
7168"\n" "ibase=A"
7169"\n" "s=scale"
7170"\n" "scale=1.1*s+2"
7171"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007172"\n" "scale=0"
7173"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007174"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007175"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007176"\n" "scale=s+2"
7177"\n" "r=a=x"
7178"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007179"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007180"\n" "a*=q/(i*(i-1))"
7181"\n" "r+=a"
7182"\n" "}"
7183"\n" "scale=s"
7184"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007185"\n" "return(r/1)"
7186"\n" "}"
7187"\n" "define c(x){"
7188"\n" "auto b,s"
7189"\n" "b=ibase"
7190"\n" "ibase=A"
7191"\n" "s=scale"
7192"\n" "scale*=1.2"
7193"\n" "x=s(2*a(1)+x)"
7194"\n" "scale=s"
7195"\n" "ibase=b"
7196"\n" "return(x/1)"
7197"\n" "}"
7198"\n" "define a(x){"
7199"\n" "auto b,s,r,n,a,m,t,f,i,u"
7200"\n" "b=ibase"
7201"\n" "ibase=A"
7202"\n" "n=1"
7203"\n" "if(x<0){"
7204"\n" "n=-1"
7205"\n" "x=-x"
7206"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007207"\n" "if(scale<65){"
7208"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7209"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007210"\n" "}"
7211"\n" "s=scale"
7212"\n" "if(x>.2){"
7213"\n" "scale+=5"
7214"\n" "a=a(.2)"
7215"\n" "}"
7216"\n" "scale=s+3"
7217"\n" "while(x>.2){"
7218"\n" "m+=1"
7219"\n" "x=(x-.2)/(1+.2*x)"
7220"\n" "}"
7221"\n" "r=u=x"
7222"\n" "f=-x*x"
7223"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007224"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007225"\n" "u*=f"
7226"\n" "t=u/i"
7227"\n" "r+=t"
7228"\n" "}"
7229"\n" "scale=s"
7230"\n" "ibase=b"
7231"\n" "return((m*a+r)/n)"
7232"\n" "}"
7233"\n" "define j(n,x){"
7234"\n" "auto b,s,o,a,i,v,f"
7235"\n" "b=ibase"
7236"\n" "ibase=A"
7237"\n" "s=scale"
7238"\n" "scale=0"
7239"\n" "n/=1"
7240"\n" "if(n<0){"
7241"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007242"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007243"\n" "}"
7244"\n" "a=1"
7245"\n" "for(i=2;i<=n;++i)a*=i"
7246"\n" "scale=1.5*s"
7247"\n" "a=(x^n)/2^n/a"
7248"\n" "r=v=1"
7249"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007250"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007251"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007252"\n" "v=v*f/i/(n+i)"
7253"\n" "r+=v"
7254"\n" "}"
7255"\n" "scale=s"
7256"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007257"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007258"\n" "return(a*r/1)"
7259"\n" "}"
7260};
7261#endif // ENABLE_BC
7262
Denys Vlasenko10bde142018-12-27 18:23:58 +01007263static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007264{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007265 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007266 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007267 size_t i;
7268
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007269#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007270 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007271 // We know that internal library is not buggy,
7272 // thus error checking is normally disabled.
7273# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007274 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007275 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007276 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007277#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007278
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007279 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007280 fname = (void*)G.files.v;
7281 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007282 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007283 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7284 // Debug config, non-interactive mode:
7285 // return all the way back to main.
7286 // Non-debug builds do not come here
7287 // in non-interactive mode, they exit.
7288 RETURN_STATUS(s);
7289 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007290 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007291
Denys Vlasenko91cde952018-12-10 20:56:08 +01007292 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007293 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007294
Denys Vlasenko19f11072018-12-12 22:48:19 +01007295 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007296}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007297#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007298
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007299#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007300static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007301{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007302 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007303 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007304 bc_vec_free(&G.prog.vars);
7305 bc_vec_free(&G.prog.var_map);
7306 bc_vec_free(&G.prog.arrs);
7307 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007308 IF_DC(bc_vec_free(&G.prog.strs);)
7309 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007310 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007311 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007312 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007313 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007314 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007315 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007316}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007317#endif
7318
Denys Vlasenko10bde142018-12-27 18:23:58 +01007319static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007320{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007321 BcInstPtr ip;
7322
Denys Vlasenko82269122018-12-14 16:30:56 +01007323 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007324 memset(&ip, 0, sizeof(BcInstPtr));
7325
Denys Vlasenko82269122018-12-14 16:30:56 +01007326 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007327 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007328 G.prog.ob_t = 10;
7329
Denys Vlasenko503faf92018-12-20 16:24:18 +01007330 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7331 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007332
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007333 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007334 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007335
Denys Vlasenko503faf92018-12-20 16:24:18 +01007336 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7337 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007338
7339 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007340 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007341
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007342 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007343 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007344 // a valid function name (and be short)
7345 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007346 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007347 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007348 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007349 xc_program_add_fn();
7350 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007351 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007352
7353 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007354 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007355
7356 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007357 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007358
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007359 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7360 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007361 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007362 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7363 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007364
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007365 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007366}
Gavin Howard01055ba2018-11-03 11:00:21 -06007367
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01007368static unsigned xc_vm_envLen(const char *var)
7369{
7370 char *lenv;
7371 unsigned len;
7372
7373 lenv = getenv(var);
7374 len = BC_NUM_PRINT_WIDTH;
Denys Vlasenko29a90432020-12-29 18:50:56 +01007375 if (lenv) {
7376 len = bb_strtou(lenv, NULL, 10);
7377 if (len == 0 || len > INT_MAX)
7378 len = INT_MAX;
7379 if (errno)
7380 len = BC_NUM_PRINT_WIDTH;
7381 }
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01007382
Denys Vlasenko29a90432020-12-29 18:50:56 +01007383 // dc (GNU bc 1.07.1) 1.4.1 seems to use width
7384 // 1 char wider than bc from the same package.
7385 // Both default width, and xC_LINE_LENGTH=N are wider:
7386 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7387 // |1234\ |
7388 // |56 |
7389 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7390 // |123\ |
7391 // |456 |
7392 // Do the same, but it might be a bug in GNU package
7393 if (IS_BC)
7394 len--;
7395
7396 if (len < 2)
7397 len = IS_BC ? BC_NUM_PRINT_WIDTH - 1 : BC_NUM_PRINT_WIDTH;
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01007398
7399 return len;
7400}
7401
Denys Vlasenko10bde142018-12-27 18:23:58 +01007402static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007403{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007404 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007405 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007406
Denys Vlasenko10bde142018-12-27 18:23:58 +01007407 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007408 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007409 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007410
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007411//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7412//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007413 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007414#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007415 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007416 // With SA_RESTART, most system calls will restart
7417 // (IOW: they won't fail with EINTR).
7418 // In particular, this means ^C won't cause
7419 // stdout to get into "error state" if SIGINT hits
7420 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007421 //
7422 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007423 // will only be handled after [Enter] since read()
7424 // from stdin is not interrupted by ^C either,
7425 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007426 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007427 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7428
7429 // Without SA_RESTART, this exhibits a bug:
7430 // "while (1) print 1" and try ^C-ing it.
7431 // Intermittently, instead of returning to input line,
7432 // you'll get "output error: Interrupted system call"
7433 // and exit.
7434 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007435#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007436 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007437 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007438 return 0; // "not a tty"
7439}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007440
Denys Vlasenko10bde142018-12-27 18:23:58 +01007441static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007442{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007443 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007444#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007445 if (G_exiting) // it was actually "halt" or "quit"
7446 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007447
7448 bc_vec_free(&G.files);
7449 xc_program_free();
7450 xc_parse_free();
7451 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007452# if ENABLE_FEATURE_EDITING
7453 free_line_input_t(G.line_input_state);
7454# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007455 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007456#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007457 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007458 return st;
7459}
7460
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007461#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007462int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007463int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007464{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007465 int is_tty;
7466
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007467 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007468
Denys Vlasenko10bde142018-12-27 18:23:58 +01007469 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007470
7471 bc_args(argv);
7472
7473 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7474 bc_vm_info();
7475
Denys Vlasenko10bde142018-12-27 18:23:58 +01007476 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007477}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007478#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007479
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007480#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007481int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007482int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007483{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007484 int noscript;
7485
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007486 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007487
Denys Vlasenko10bde142018-12-27 18:23:58 +01007488 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007489
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007490 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7491 noscript = BC_FLAG_I;
7492 for (;;) {
7493 int n = getopt(argc, argv, "e:f:x");
7494 if (n <= 0)
7495 break;
7496 switch (n) {
7497 case 'e':
7498 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007499 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007500 if (n) return n;
7501 break;
7502 case 'f':
7503 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007504 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007505 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007506 break;
7507 case 'x':
7508 option_mask32 |= DC_FLAG_X;
7509 break;
7510 default:
7511 bb_show_usage();
7512 }
7513 }
7514 argv += optind;
7515
7516 while (*argv) {
7517 noscript = 0;
7518 bc_vec_push(&G.files, argv++);
7519 }
7520
7521 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7522
Denys Vlasenko10bde142018-12-27 18:23:58 +01007523 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007524}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007525#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007526
Denys Vlasenko14767602018-12-27 22:52:13 +01007527#endif // DC_BIG