blob: 92721d18fc9212cfd31b82c0dd454394b3f445c0 [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
234#define BC_NUM_PRINT_WIDTH 69
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 Vlasenko51fb8aa2018-12-05 00:22:34 +0100520static const struct BcLexKeyword bc_lex_kws[20] = {
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
1468 if (a_int != 0) return (ssize_t) a_int;
1469
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001470 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001471 if (a_max) {
1472 min = b->rdx;
1473 diff = a->rdx - b->rdx;
1474 max_num = a->num + diff;
1475 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001476 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1477 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001478 min = a->rdx;
1479 diff = b->rdx - a->rdx;
1480 max_num = b->num + diff;
1481 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001482 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001483 }
1484
1485 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001486 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001487
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001488 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001489 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001490 }
1491
1492 return 0;
1493}
1494
1495static void bc_num_truncate(BcNum *n, size_t places)
1496{
1497 if (places == 0) return;
1498
1499 n->rdx -= places;
1500
1501 if (n->len != 0) {
1502 n->len -= places;
1503 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1504 }
1505}
1506
1507static void bc_num_extend(BcNum *n, size_t places)
1508{
1509 size_t len = n->len + places;
1510
1511 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001512 if (n->cap < len) bc_num_expand(n, len);
1513
1514 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1515 memset(n->num, 0, sizeof(BcDig) * places);
1516
1517 n->len += places;
1518 n->rdx += places;
1519 }
1520}
1521
1522static void bc_num_clean(BcNum *n)
1523{
1524 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1525 if (n->len == 0)
1526 n->neg = false;
1527 else if (n->len < n->rdx)
1528 n->len = n->rdx;
1529}
1530
1531static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1532{
1533 if (n->rdx < scale)
1534 bc_num_extend(n, scale - n->rdx);
1535 else
1536 bc_num_truncate(n, n->rdx - scale);
1537
1538 bc_num_clean(n);
1539 if (n->len != 0) n->neg = !neg1 != !neg2;
1540}
1541
1542static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1543 BcNum *restrict b)
1544{
1545 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001546 b->len = n->len - idx;
1547 a->len = idx;
1548 a->rdx = b->rdx = 0;
1549
1550 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1551 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001552 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001553 bc_num_zero(b);
1554 bc_num_copy(a, n);
1555 }
1556
1557 bc_num_clean(a);
1558 bc_num_clean(b);
1559}
1560
Denys Vlasenko29301232018-12-11 15:29:32 +01001561static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001562{
Denys Vlasenko29301232018-12-11 15:29:32 +01001563 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001564
1565 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1566 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1567 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001568 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001569 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001570
1571 if (n->rdx >= places)
1572 n->rdx -= places;
1573 else {
1574 bc_num_extend(n, places - n->rdx);
1575 n->rdx = 0;
1576 }
1577
1578 bc_num_clean(n);
1579
Denys Vlasenko29301232018-12-11 15:29:32 +01001580 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001581}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001582#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001583
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001584typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1585
1586static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1587 BcNumBinaryOp op, size_t req)
1588{
1589 BcStatus s;
1590 BcNum num2, *ptr_a, *ptr_b;
1591 bool init = false;
1592
1593 if (c == a) {
1594 ptr_a = &num2;
1595 memcpy(ptr_a, c, sizeof(BcNum));
1596 init = true;
1597 } else
1598 ptr_a = a;
1599
1600 if (c == b) {
1601 ptr_b = &num2;
1602 if (c != a) {
1603 memcpy(ptr_b, c, sizeof(BcNum));
1604 init = true;
1605 }
1606 } else
1607 ptr_b = b;
1608
1609 if (init)
1610 bc_num_init(c, req);
1611 else
1612 bc_num_expand(c, req);
1613
1614 s = BC_STATUS_SUCCESS;
1615 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1616
1617 if (init) bc_num_free(&num2);
1618
1619 RETURN_STATUS(s);
1620}
1621#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1622
1623static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1624static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1625static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1626static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1627static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1628static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1629
1630static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1631{
1632 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1633 (void) scale;
1634 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1635}
1636
1637static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1638{
1639 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1640 (void) scale;
1641 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1642}
1643
1644static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1645{
1646 size_t req = BC_NUM_MREQ(a, b, scale);
1647 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1648}
1649
1650static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1651{
1652 size_t req = BC_NUM_MREQ(a, b, scale);
1653 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1654}
1655
1656static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1657{
1658 size_t req = BC_NUM_MREQ(a, b, scale);
1659 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1660}
1661
1662static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1663{
1664 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1665}
1666
Denys Vlasenko10bde142018-12-27 18:23:58 +01001667static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001668 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1669};
1670#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1671#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1672#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1673#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1674#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1675#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1676
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001677static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001678{
1679 BcNum one;
1680 BcDig num[2];
1681
1682 one.cap = 2;
1683 one.num = num;
1684 bc_num_one(&one);
1685
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001686 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001687}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001688#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001689
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001690static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001691{
1692 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1693 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001694 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001695
1696 // Because this function doesn't need to use scale (per the bc spec),
1697 // I am hijacking it to say whether it's doing an add or a subtract.
1698
1699 if (a->len == 0) {
1700 bc_num_copy(c, b);
1701 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001702 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001703 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001704 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001705 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001706 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001707 }
1708
1709 c->neg = a->neg;
1710 c->rdx = BC_MAX(a->rdx, b->rdx);
1711 min_rdx = BC_MIN(a->rdx, b->rdx);
1712 c->len = 0;
1713
1714 if (a->rdx > b->rdx) {
1715 diff = a->rdx - b->rdx;
1716 ptr = a->num;
1717 ptr_a = a->num + diff;
1718 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001719 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001720 diff = b->rdx - a->rdx;
1721 ptr = b->num;
1722 ptr_a = a->num;
1723 ptr_b = b->num + diff;
1724 }
1725
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001726 ptr_c = c->num;
1727 for (i = 0; i < diff; ++i, ++c->len)
1728 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001729
1730 ptr_c += diff;
1731 a_int = BC_NUM_INT(a);
1732 b_int = BC_NUM_INT(b);
1733
1734 if (a_int > b_int) {
1735 min_int = b_int;
1736 max = a_int;
1737 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001738 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001739 min_int = a_int;
1740 max = b_int;
1741 ptr = ptr_b;
1742 }
1743
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001744 carry = 0;
1745 for (i = 0; i < min_rdx + min_int; ++i) {
1746 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001747 carry = in / 10;
1748 ptr_c[i] = (BcDig)(in % 10);
1749 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001750 for (; i < max + min_rdx; ++i) {
1751 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001752 carry = in / 10;
1753 ptr_c[i] = (BcDig)(in % 10);
1754 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001755 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001756
1757 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1758
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001759 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001760}
1761
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001763{
Gavin Howard01055ba2018-11-03 11:00:21 -06001764 ssize_t cmp;
1765 BcNum *minuend, *subtrahend;
1766 size_t start;
1767 bool aneg, bneg, neg;
1768
1769 // Because this function doesn't need to use scale (per the bc spec),
1770 // I am hijacking it to say whether it's doing an add or a subtract.
1771
1772 if (a->len == 0) {
1773 bc_num_copy(c, b);
1774 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001775 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001776 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001777 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001778 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001779 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001780 }
1781
1782 aneg = a->neg;
1783 bneg = b->neg;
1784 a->neg = b->neg = false;
1785
1786 cmp = bc_num_cmp(a, b);
1787
1788 a->neg = aneg;
1789 b->neg = bneg;
1790
1791 if (cmp == 0) {
1792 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001793 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001795 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001796 neg = a->neg;
1797 minuend = a;
1798 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001799 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 neg = b->neg;
1801 if (sub) neg = !neg;
1802 minuend = b;
1803 subtrahend = a;
1804 }
1805
1806 bc_num_copy(c, minuend);
1807 c->neg = neg;
1808
1809 if (c->rdx < subtrahend->rdx) {
1810 bc_num_extend(c, subtrahend->rdx - c->rdx);
1811 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001812 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 start = c->rdx - subtrahend->rdx;
1814
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001815 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001816
1817 bc_num_clean(c);
1818
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001819 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001820}
1821
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001822static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001824#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001825{
1826 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001827 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001828 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001829 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001830
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 if (a->len == 0 || b->len == 0) {
1832 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001833 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001835 aone = BC_NUM_ONE(a);
1836 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001838 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001839 }
1840
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001841 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1842 || a->len < BC_NUM_KARATSUBA_LEN
1843 || b->len < BC_NUM_KARATSUBA_LEN
1844 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001845 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001846
Gavin Howard01055ba2018-11-03 11:00:21 -06001847 bc_num_expand(c, a->len + b->len + 1);
1848
1849 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001850 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001851
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001852 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001853 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001854 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001855 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001856 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001857 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001858 carry = in / 10;
1859 c->num[i + j] = (BcDig)(in % 10);
1860 }
1861
1862 c->num[i + j] += (BcDig) carry;
1863 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001864
Denys Vlasenko14767602018-12-27 22:52:13 +01001865#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001866 // a=2^1000000
1867 // a*a <- without check below, this will not be interruptible
1868 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001869#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 }
1871
1872 c->len = len;
1873
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001874 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 }
1876
1877 bc_num_init(&l1, max);
1878 bc_num_init(&h1, max);
1879 bc_num_init(&l2, max);
1880 bc_num_init(&h2, max);
1881 bc_num_init(&m1, max);
1882 bc_num_init(&m2, max);
1883 bc_num_init(&z0, max);
1884 bc_num_init(&z1, max);
1885 bc_num_init(&z2, max);
1886 bc_num_init(&temp, max + max);
1887
1888 bc_num_split(a, max2, &l1, &h1);
1889 bc_num_split(b, max2, &l2, &h2);
1890
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001891 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001893 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001894 if (s) goto err;
1895
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001897 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001898 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001900 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001901 if (s) goto err;
1902
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001903 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001905 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001906 if (s) goto err;
1907
Denys Vlasenko29301232018-12-11 15:29:32 +01001908 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001910 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001911 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001912 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001913 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001914 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001915 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 bc_num_free(&temp);
1917 bc_num_free(&z2);
1918 bc_num_free(&z1);
1919 bc_num_free(&z0);
1920 bc_num_free(&m2);
1921 bc_num_free(&m1);
1922 bc_num_free(&h2);
1923 bc_num_free(&l2);
1924 bc_num_free(&h1);
1925 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001926 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001927}
1928
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001929static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001930{
1931 BcStatus s;
1932 BcNum cpa, cpb;
1933 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1934
1935 scale = BC_MAX(scale, a->rdx);
1936 scale = BC_MAX(scale, b->rdx);
1937 scale = BC_MIN(a->rdx + b->rdx, scale);
1938 maxrdx = BC_MAX(maxrdx, scale);
1939
1940 bc_num_init(&cpa, a->len);
1941 bc_num_init(&cpb, b->len);
1942
1943 bc_num_copy(&cpa, a);
1944 bc_num_copy(&cpb, b);
1945 cpa.neg = cpb.neg = false;
1946
Denys Vlasenko29301232018-12-11 15:29:32 +01001947 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001948 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001949 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001951 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001952 if (s) goto err;
1953
1954 maxrdx += scale;
1955 bc_num_expand(c, c->len + maxrdx);
1956
1957 if (c->len < maxrdx) {
1958 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1959 c->len += maxrdx;
1960 }
1961
1962 c->rdx = maxrdx;
1963 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001964 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 bc_num_free(&cpb);
1966 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001967 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001969#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001970
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001971static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001972{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001973 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001974 size_t len, end, i;
1975 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001976
1977 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001978 RETURN_STATUS(bc_error("divide by zero"));
1979 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001980 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001981 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001983 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001984 bc_num_copy(c, a);
1985 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001986 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001987 }
1988
1989 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1990 bc_num_copy(&cp, a);
1991 len = b->len;
1992
1993 if (len > cp.len) {
1994 bc_num_expand(&cp, len + 2);
1995 bc_num_extend(&cp, len - cp.len);
1996 }
1997
1998 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1999 cp.rdx -= b->rdx;
2000 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2001
2002 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002003 for (;;) {
2004 if (len == 0) break;
2005 len--;
2006 if (b->num[len] != 0)
2007 break;
2008 }
2009 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002010 }
2011
2012 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2013
2014 // We want an extra zero in front to make things simpler.
2015 cp.num[cp.len++] = 0;
2016 end = cp.len - len;
2017
2018 bc_num_expand(c, cp.len);
2019
2020 bc_num_zero(c);
2021 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2022 c->rdx = cp.rdx;
2023 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002024
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002025 s = BC_STATUS_SUCCESS;
2026 for (i = end - 1; i < end; --i) {
2027 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002028 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002029 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2030 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002031 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01002032#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01002033 // a=2^100000
2034 // scale=40000
2035 // 1/a <- without check below, this will not be interruptible
2036 if (G_interrupt) {
2037 s = BC_STATUS_FAILURE;
2038 break;
2039 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002040#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002041 }
2042
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002043 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002044 bc_num_free(&cp);
2045
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002046 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002047}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002048#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002049
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002050static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002051 BcNum *restrict d, size_t scale, size_t ts)
2052{
2053 BcStatus s;
2054 BcNum temp;
2055 bool neg;
2056
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002057 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002059
2060 if (a->len == 0) {
2061 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002062 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002063 }
2064
2065 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002066 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002067 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002068
2069 if (scale != 0) scale = ts;
2070
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002071 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002072 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002073 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002074 if (s) goto err;
2075
2076 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2077
2078 neg = d->neg;
2079 bc_num_retireMul(d, ts, a->neg, b->neg);
2080 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002081 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002082 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002083 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002084}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002085#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002086
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002087static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002088{
2089 BcStatus s;
2090 BcNum c1;
2091 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2092
2093 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002094 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002095 bc_num_free(&c1);
2096
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002098}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002099#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002100
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002102{
2103 BcStatus s = BC_STATUS_SUCCESS;
2104 BcNum copy;
2105 unsigned long pow;
2106 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002107 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002108
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002109 // GNU bc does not allow 2^2.0 - we do
2110 for (i = 0; i < b->rdx; i++)
2111 if (b->num[i] != 0)
2112 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002113
2114 if (b->len == 0) {
2115 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002116 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002118 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002119 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002120 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002122 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 if (!b->neg)
2124 bc_num_copy(c, a);
2125 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002126 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 }
2129
2130 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002131 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002132 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002133 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002134
2135 bc_num_init(&copy, a->len);
2136 bc_num_copy(&copy, a);
2137
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002138 if (!neg) {
2139 if (a->rdx > scale)
2140 scale = a->rdx;
2141 if (a->rdx * pow < scale)
2142 scale = a->rdx * pow;
2143 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002144
Gavin Howard01055ba2018-11-03 11:00:21 -06002145
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002146 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002148 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002150 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002151 //if (G_interrupt) {
2152 // s = BC_STATUS_FAILURE;
2153 // goto err;
2154 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002155 }
2156
Gavin Howard01055ba2018-11-03 11:00:21 -06002157 bc_num_copy(c, &copy);
2158
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002159 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002160 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002161 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 if (s) goto err;
2163
2164 if (pow & 1) {
2165 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002166 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002167 if (s) goto err;
2168 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002169 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002170 //if (G_interrupt) {
2171 // s = BC_STATUS_FAILURE;
2172 // goto err;
2173 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002174 }
2175
2176 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002177 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002178 if (s) goto err;
2179 }
2180
Gavin Howard01055ba2018-11-03 11:00:21 -06002181 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2182
2183 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002184 for (i = 0; i < c->len; ++i)
2185 if (c->num[i] != 0)
2186 goto skip;
2187 bc_num_setToZero(c, scale);
2188 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002189
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002190 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002191 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002193}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002194#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002195
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002196static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002197{
2198 BcStatus s;
2199 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002200 BcDig half_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002201 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2202 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2203
2204 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2205 bc_num_expand(b, req);
2206
2207 if (a->len == 0) {
2208 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002209 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002210 }
2211 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002212 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002213 }
2214 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 bc_num_one(b);
2216 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002217 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002218 }
2219
2220 scale = BC_MAX(scale, a->rdx) + 1;
2221 len = a->len + scale;
2222
2223 bc_num_init(&num1, len);
2224 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002225
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002226 half.cap = ARRAY_SIZE(half_digs);
2227 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002228 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002229 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002230 half.rdx = 1;
2231
2232 bc_num_init(&f, len);
2233 bc_num_init(&fprime, len);
2234
2235 x0 = &num1;
2236 x1 = &num2;
2237
2238 bc_num_one(x0);
2239 pow = BC_NUM_INT(a);
2240
2241 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002242 if (pow & 1)
2243 x0->num[0] = 2;
2244 else
2245 x0->num[0] = 6;
2246
2247 pow -= 2 - (pow & 1);
2248
2249 bc_num_extend(x0, pow);
2250
2251 // Make sure to move the radix back.
2252 x0->rdx -= pow;
2253 }
2254
2255 x0->rdx = digs = digs1 = 0;
2256 resrdx = scale + 2;
2257 len = BC_NUM_INT(x0) + resrdx - 1;
2258
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002259 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002260 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002261 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002262 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002263 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002264 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002265 if (s) goto err;
2266
2267 cmp = bc_num_cmp(x1, x0);
2268 digs = x1->len - (unsigned long long) llabs(cmp);
2269
2270 if (cmp == cmp2 && digs == digs1)
2271 times += 1;
2272 else
2273 times = 0;
2274
2275 resrdx += times > 4;
2276
2277 cmp2 = cmp1;
2278 cmp1 = cmp;
2279 digs1 = digs;
2280
2281 temp = x0;
2282 x0 = x1;
2283 x1 = temp;
2284 }
2285
Gavin Howard01055ba2018-11-03 11:00:21 -06002286 bc_num_copy(b, x0);
2287 scale -= 1;
2288 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002289 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002290 bc_num_free(&fprime);
2291 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002292 bc_num_free(&num2);
2293 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002294 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002295}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002296#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002297
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002298static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002299 size_t scale)
2300{
2301 BcStatus s;
2302 BcNum num2, *ptr_a;
2303 bool init = false;
2304 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2305
2306 if (c == a) {
2307 memcpy(&num2, c, sizeof(BcNum));
2308 ptr_a = &num2;
2309 bc_num_init(c, len);
2310 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002311 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002312 ptr_a = a;
2313 bc_num_expand(c, len);
2314 }
2315
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002316 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002317
2318 if (init) bc_num_free(&num2);
2319
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002320 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002321}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002322#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002323
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002324#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002325static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002326{
2327 BcStatus s;
2328 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002329 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002330
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002331 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002332 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002333 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002334 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002335 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002336 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002337
2338 bc_num_expand(d, c->len);
2339 bc_num_init(&base, c->len);
2340 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002341 bc_num_init(&temp, b->len);
2342
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002343 two.cap = ARRAY_SIZE(two_digs);
2344 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002345 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002346 two_digs[0] = 2;
2347
Gavin Howard01055ba2018-11-03 11:00:21 -06002348 bc_num_one(d);
2349
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002350 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002351 if (s) goto err;
2352 bc_num_copy(&exp, b);
2353
2354 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002355 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002356 if (s) goto err;
2357
2358 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002359 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002360 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002361 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002362 if (s) goto err;
2363 }
2364
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002365 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002366 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002367 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002368 if (s) goto err;
2369 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002370 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002371 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002372 bc_num_free(&exp);
2373 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002374 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002375}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002376#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002377#endif // ENABLE_DC
2378
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002379static FAST_FUNC void bc_string_free(void *string)
2380{
2381 free(*(char**)string);
2382}
2383
Gavin Howard01055ba2018-11-03 11:00:21 -06002384static void bc_func_init(BcFunc *f)
2385{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002386 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002387 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2388 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002389 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2390 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002391 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002392}
2393
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002394static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002395{
2396 BcFunc *f = (BcFunc *) func;
2397 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002398 IF_BC(bc_vec_free(&f->labels);)
2399 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002400 IF_BC(bc_vec_free(&f->strs);)
2401 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002402}
2403
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002404static void bc_array_expand(BcVec *a, size_t len);
2405
Gavin Howard01055ba2018-11-03 11:00:21 -06002406static void bc_array_init(BcVec *a, bool nums)
2407{
2408 if (nums)
2409 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2410 else
2411 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2412 bc_array_expand(a, 1);
2413}
2414
Gavin Howard01055ba2018-11-03 11:00:21 -06002415static void bc_array_expand(BcVec *a, size_t len)
2416{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002417 if (a->dtor == bc_num_free
2418 // && a->size == sizeof(BcNum) - always true
2419 ) {
2420 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002421 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002422 bc_num_init_DEF_SIZE(&n);
2423 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002424 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002425 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002426 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002427 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002428 bc_array_init(&v, true);
2429 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002430 }
2431 }
2432}
2433
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002434static void bc_array_copy(BcVec *d, const BcVec *s)
2435{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002436 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002437 size_t i;
2438
2439 bc_vec_pop_all(d);
2440 bc_vec_expand(d, s->cap);
2441 d->len = s->len;
2442
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002443 dnum = (void*)d->v;
2444 snum = (void*)s->v;
2445 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002446 bc_num_init(dnum, snum->len);
2447 bc_num_copy(dnum, snum);
2448 }
2449}
2450
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002451#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002452static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002453{
2454 d->t = src->t;
2455
2456 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002457 case XC_RESULT_TEMP:
2458 case XC_RESULT_IBASE:
2459 case XC_RESULT_SCALE:
2460 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002461 bc_num_init(&d->d.n, src->d.n.len);
2462 bc_num_copy(&d->d.n, &src->d.n);
2463 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002464 case XC_RESULT_VAR:
2465 case XC_RESULT_ARRAY:
2466 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002467 d->d.id.name = xstrdup(src->d.id.name);
2468 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002469 case XC_RESULT_CONSTANT:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002470 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002471 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2472 break;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002473 default: // placate compiler
2474 // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen
2475 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002476 }
2477}
2478#endif // ENABLE_DC
2479
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002480static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002481{
2482 BcResult *r = (BcResult *) result;
2483
2484 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002485 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002486 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002487 case XC_RESULT_IBASE:
2488 case XC_RESULT_SCALE:
2489 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002490 bc_num_free(&r->d.n);
2491 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002492 case XC_RESULT_VAR:
2493 case XC_RESULT_ARRAY:
2494 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002495 free(r->d.id.name);
2496 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002497 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002498 // Do nothing.
2499 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002500 }
2501}
2502
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002503static int bad_input_byte(char c)
2504{
2505 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2506 || c > 0x7e
2507 ) {
2508 bc_error_fmt("illegal character 0x%02x", c);
2509 return 1;
2510 }
2511 return 0;
2512}
2513
Denys Vlasenko10bde142018-12-27 18:23:58 +01002514static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002515{
2516 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002517 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002518 fflush_and_check();
2519
Denys Vlasenko14767602018-12-27 22:52:13 +01002520#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002521 if (G_interrupt) { // ^C was pressed
2522 intr:
2523 if (fp != stdin) {
2524 // ^C while running a script (bc SCRIPT): die.
2525 // We do not return to interactive prompt:
2526 // user might be running us from a shell,
2527 // and SCRIPT might be intended to terminate
2528 // (e.g. contain a "halt" stmt).
2529 // ^C dropping user into a bc prompt instead of
2530 // the shell would be unexpected.
2531 xfunc_die();
2532 }
2533 // ^C while interactive input
2534 G_interrupt = 0;
2535 // GNU bc says "interrupted execution."
2536 // GNU dc says "Interrupt!"
2537 fputs("\ninterrupted execution\n", stderr);
2538 }
2539
2540# if ENABLE_FEATURE_EDITING
2541 if (G_ttyin && fp == stdin) {
2542 int n, i;
2543# define line_buf bb_common_bufsiz1
2544 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2545 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2546 if (n == 0) // ^C
2547 goto intr;
2548 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2549 return;
2550 }
2551 i = 0;
2552 for (;;) {
2553 char c = line_buf[i++];
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002554 if (c == '\0') break;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002555 if (bad_input_byte(c)) goto again;
2556 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002557 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002558# undef line_buf
2559 } else
2560# endif
2561#endif
2562 {
2563 int c;
2564 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002565
2566 do {
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002567 get_char:
Denys Vlasenko14767602018-12-27 22:52:13 +01002568#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002569 if (G_interrupt) {
2570 // ^C was pressed: ignore entire line, get another one
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002571 goto again;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002572 }
2573#endif
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002574 c = fgetc(fp);
2575 if (c == '\0')
2576 goto get_char;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002577 if (c == EOF) {
2578 if (ferror(fp))
James Byrne69374872019-07-02 11:35:03 +02002579 bb_simple_perror_msg_and_die("input error");
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002580 // Note: EOF does not append '\n'
2581 break;
2582 }
2583 bad_chars |= bad_input_byte(c);
2584 bc_vec_pushByte(vec, (char)c);
2585 } while (c != '\n');
2586
2587 if (bad_chars) {
2588 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002589 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002590 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002591 goto again;
2592 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002593 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002594 }
2595 bc_vec_pushZeroByte(vec);
2596 }
2597}
2598
2599//
2600// Parsing routines
2601//
2602
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002603// "Input numbers may contain the characters 0-9 and A-Z.
2604// (Note: They must be capitals. Lower case letters are variable names.)
2605// Single digit numbers always have the value of the digit regardless of
2606// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2607// all input digits greater or equal to ibase to the value of ibase-1.
2608// This makes the number ZZZ always be the largest 3 digit number of the
2609// input base."
2610static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002611{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002612 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002613 for (;;) {
2614 BcDig c = *val++;
2615 if (c == '\0')
2616 break;
2617 if (c == '.') {
2618 if (radix) return false;
2619 radix = true;
2620 continue;
2621 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002622 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002623 return false;
2624 }
2625 return true;
2626}
2627
2628// Note: n is already "bc_num_zero()"ed,
2629// leading zeroes in "val" are removed
2630static void bc_num_parseDecimal(BcNum *n, const char *val)
2631{
2632 size_t len, i;
2633 const char *ptr;
2634
2635 len = strlen(val);
2636 if (len == 0)
2637 return;
2638
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002639 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002640
2641 ptr = strchr(val, '.');
2642
2643 n->rdx = 0;
2644 if (ptr != NULL)
2645 n->rdx = (size_t)((val + len) - (ptr + 1));
2646
2647 for (i = 0; val[i]; ++i) {
2648 if (val[i] != '0' && val[i] != '.') {
2649 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002650 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002651 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002652 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002653 if (c > 9) { // A-Z => 10-36
2654 n->len = 2;
2655 c -= ('A' - '9' - 1);
2656 n->num[1] = c/10;
2657 c = c%10;
2658 }
2659 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002660 break;
2661 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002662 i = len - 1;
2663 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002664 char c = val[i] - '0';
2665 if (c > 9) // A-Z => 9
2666 c = 9;
2667 n->num[n->len] = c;
2668 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002669 skip_dot:
2670 if (i == 0) break;
2671 if (val[--i] == '.') goto skip_dot;
2672 }
2673 break;
2674 }
2675 }
2676 // if for() exits without hitting if(), the value is entirely zero
2677}
2678
2679// Note: n is already "bc_num_zero()"ed,
2680// leading zeroes in "val" are removed
2681static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2682{
2683 BcStatus s;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002684 BcNum mult, result;
2685 BcNum temp;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002686 BcNum base;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002687 BcDig temp_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002688 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002689 size_t digits;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002690
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002691 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002692
2693 temp.cap = ARRAY_SIZE(temp_digs);
2694 temp.num = temp_digs;
2695
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002696 base.cap = ARRAY_SIZE(base_digs);
2697 base.num = base_digs;
2698 bc_num_ulong2num(&base, base_t);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002699 base_t--;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002700
2701 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002702 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002703 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002704
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002705 c = *val++;
2706 if (c == '\0') goto int_err;
2707 if (c == '.') break;
2708
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002709 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2710 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002711
2712 s = zbc_num_mul(n, &base, &mult, 0);
2713 if (s) goto int_err;
2714 bc_num_ulong2num(&temp, v);
2715 s = zbc_num_add(&mult, &temp, n, 0);
2716 if (s) goto int_err;
2717 }
2718
2719 bc_num_init(&result, base.len);
2720 //bc_num_zero(&result); - already is
2721 bc_num_one(&mult);
2722
2723 digits = 0;
2724 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002725 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002726 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002727
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002728 c = *val++;
2729 if (c == '\0') break;
2730 digits++;
2731
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002732 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2733 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002734
2735 s = zbc_num_mul(&result, &base, &result, 0);
2736 if (s) goto err;
2737 bc_num_ulong2num(&temp, v);
2738 s = zbc_num_add(&result, &temp, &result, 0);
2739 if (s) goto err;
2740 s = zbc_num_mul(&mult, &base, &mult, 0);
2741 if (s) goto err;
2742 }
2743
2744 s = zbc_num_div(&result, &mult, &result, digits);
2745 if (s) goto err;
2746 s = zbc_num_add(n, &result, n, digits);
2747 if (s) goto err;
2748
2749 if (n->len != 0) {
2750 if (n->rdx < digits)
2751 bc_num_extend(n, digits - n->rdx);
2752 } else
2753 bc_num_zero(n);
2754 err:
2755 bc_num_free(&result);
2756 int_err:
2757 bc_num_free(&mult);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002758}
2759
Denys Vlasenko10bde142018-12-27 18:23:58 +01002760static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002761{
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002762 size_t i;
2763
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002764 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002765 RETURN_STATUS(bc_error("bad number string"));
2766
2767 bc_num_zero(n);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002768 while (*val == '0')
2769 val++;
2770 for (i = 0; ; ++i) {
2771 if (val[i] == '\0')
2772 RETURN_STATUS(BC_STATUS_SUCCESS);
2773 if (val[i] != '.' && val[i] != '0')
2774 break;
2775 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002776
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002777 if (base_t == 10 || val[1] == '\0')
2778 // Decimal, or single-digit number
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002779 bc_num_parseDecimal(n, val);
2780 else
2781 bc_num_parseBase(n, val, base_t);
2782
2783 RETURN_STATUS(BC_STATUS_SUCCESS);
2784}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002785#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002786
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002787// p->lex_inbuf points to the current string to be parsed.
2788// if p->lex_inbuf points to '\0', it's either EOF or it points after
2789// last processed line's terminating '\n' (and more reading needs to be done
2790// to get next character).
2791//
2792// If you are in a situation where that is a possibility, call peek_inbuf().
2793// If necessary, it performs more reading and changes p->lex_inbuf,
2794// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2795// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2796// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2797//
2798// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2799// it returns current char and advances the pointer (if not EOF).
2800// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2801//
2802// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2803// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2804// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002805//
2806// bc cases to test interactively:
2807// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2808// 1 #comment/* - prints "1<newline>" at once
2809// 1 #comment" - prints "1<newline>" at once
2810// 1\#comment - error at once (\ is not a line continuation)
2811// 1 + /*"*/2 - prints "3<newline>" at once
2812// 1 + /*#*/2 - prints "3<newline>" at once
2813// "str\" - prints "str\" at once
2814// "str#" - prints "str#" at once
2815// "str/*" - prints "str/*" at once
2816// "str#\ - waits for second line
2817// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002818static char peek_inbuf(void)
2819{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002820 if (*G.prs.lex_inbuf == '\0'
2821 && G.prs.lex_input_fp
2822 ) {
2823 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2824 G.prs.lex_inbuf = G.input_buffer.v;
2825 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2826 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002827 }
2828 return *G.prs.lex_inbuf;
2829}
2830static char eat_inbuf(void)
2831{
2832 char c = peek_inbuf();
2833 if (c) G.prs.lex_inbuf++;
2834 return c;
2835}
2836
Denys Vlasenko10bde142018-12-27 18:23:58 +01002837static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002838{
2839 BcParse *p = &G.prs;
2840 char c;
2841
2842 // Try: echo -n '#foo' | bc
2843 p->lex = XC_LEX_WHITESPACE;
2844
Denys Vlasenko2747f612018-12-31 18:48:10 +01002845 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002846 // '\0' which isn't the EOF can only be seen after '\n'.
2847 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2848 p->lex_inbuf++;
2849}
2850
Denys Vlasenko10bde142018-12-27 18:23:58 +01002851static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002852{
2853 BcParse *p = &G.prs;
2854
2855 p->lex = XC_LEX_WHITESPACE;
2856 for (;;) {
2857 // We depend here on input being done in whole lines:
2858 // '\0' which isn't the EOF can only be seen after '\n'.
2859 char c = *p->lex_inbuf;
2860 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2861 break;
2862 if (!isspace(c))
2863 break;
2864 p->lex_inbuf++;
2865 }
2866}
2867
Denys Vlasenko10bde142018-12-27 18:23:58 +01002868static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002869{
2870 BcParse *p = &G.prs;
2871 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002872 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002873
2874 bc_vec_pop_all(&p->lex_strnumbuf);
2875 bc_vec_pushByte(&p->lex_strnumbuf, last);
2876
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002877// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002878// (Note: They must be capitals. Lower case letters are variable names.)
2879// Single digit numbers always have the value of the digit regardless of
2880// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2881// all input digits greater or equal to ibase to the value of ibase-1.
2882// This makes the number ZZZ always be the largest 3 digit number of the
2883// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002884// dc only allows A-F, the rules about single-char and multi-char are the same.
2885 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002886 pt = (last == '.');
2887 p->lex = XC_LEX_NUMBER;
2888 for (;;) {
2889 // We depend here on input being done in whole lines:
2890 // '\0' which isn't the EOF can only be seen after '\n'.
2891 char c = *p->lex_inbuf;
2892 check_c:
2893 if (c == '\0')
2894 break;
2895 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2896 p->lex_inbuf += 2;
2897 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01002898 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002899 c = peek_inbuf(); // force next line to be read
2900 goto check_c;
2901 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002902 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002903 if (c != '.') break;
2904 // if '.' was already seen, stop on second one:
2905 if (pt) break;
2906 pt = true;
2907 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002908 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002909 last = c;
2910 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2911 p->lex_inbuf++;
2912 }
2913 if (last == '.') // remove trailing '.' if any
2914 bc_vec_pop(&p->lex_strnumbuf);
2915 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2916
2917 G.err_line = G.prs.lex_line;
2918 RETURN_STATUS(BC_STATUS_SUCCESS);
2919}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002920#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002921
Denys Vlasenko10bde142018-12-27 18:23:58 +01002922static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002923{
2924 BcParse *p = &G.prs;
2925 size_t i;
2926 const char *buf;
2927
2928 p->lex = XC_LEX_NAME;
2929
2930 // Since names can't cross lines with \<newline>,
2931 // we depend on the fact that whole line is in the buffer
2932 i = 0;
2933 buf = p->lex_inbuf - 1;
2934 for (;;) {
2935 char c = buf[i];
2936 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2937 i++;
2938 }
2939
2940#if 0 // We do not protect against people with gigabyte-long names
2941 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2942 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2943 if (i > BC_MAX_STRING)
2944 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2945 }
2946#endif
2947 bc_vec_string(&p->lex_strnumbuf, i, buf);
2948
2949 // Increment the index. We minus 1 because it has already been incremented.
2950 p->lex_inbuf += i - 1;
2951
2952 //return BC_STATUS_SUCCESS;
2953}
2954
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002955IF_BC(static BC_STATUS zbc_lex_token(void);)
2956IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002957#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2958#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002959
Denys Vlasenko10bde142018-12-27 18:23:58 +01002960static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002961{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002962 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002963 BcStatus s;
2964
Denys Vlasenko22314682019-01-01 21:50:14 +01002965 G.err_line = p->lex_line;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002966 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002967//why?
2968// if (p->lex_last == XC_LEX_EOF)
2969// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002970
2971 // Loop until failure or we don't have whitespace. This
2972 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002973 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002974 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002975 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002976 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002977 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002978 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002979 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002980 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002981 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002982 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002983 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2984 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002985 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002986 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002987 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002988 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002989 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002990 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002991 } while (!s && p->lex == XC_LEX_WHITESPACE);
2992 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002993
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002994 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002995}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002996#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002997
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002998#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002999static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003000{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003001 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01003002 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01003003 RETURN_STATUS(BC_STATUS_SUCCESS);
3004}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003005#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003006
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003007static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003008{
3009 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003010 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003011 if (s) RETURN_STATUS(s);
3012 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003013 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003014 RETURN_STATUS(s);
3015}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003016#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003017
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003018static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003019{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003020 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003021 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003022 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003023 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003024
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003025 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3026 const char *keyword8 = bc_lex_kws[i].name8;
3027 unsigned j = 0;
3028 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3029 j++;
3030 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003031 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003032 if (keyword8[j] != '\0')
3033 continue;
3034 match:
3035 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003036 if (isalnum(buf[j]) || buf[j]=='_')
3037 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003038 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003039 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003040 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3041 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003042 }
3043
3044 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003045 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003046 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003047 }
3048
Denys Vlasenko10bde142018-12-27 18:23:58 +01003049 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003050 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003051
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003052 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003053 // Prevent this:
3054 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003055 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003056 // '
3057 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003058 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003059 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003060
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003061 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003062}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003063#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003064
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003065static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003066{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003067 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003068
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003069 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003070 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003071 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003072 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003073 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003074 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003075 }
3076 if (c == '"')
3077 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003078 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003079 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003080 dbg_lex("++p->lex_line=%zd", p->lex_line);
3081 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003082 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3083 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003084 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003085 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3086 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003087
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003088 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003089 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003090}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003091#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003092
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003093static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003094{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003095 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003096 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003097 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003098 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003099 with_and_without >>= 8; // store "with" value
3100 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003101 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003102}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003103#define parse_lex_by_checking_eq_sign(with, without) \
3104 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003105
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003106static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003107{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003108 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003109
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003110 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003111 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003112 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003113 char c;
3114
3115 p->lex_inbuf++;
3116 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003117 check_star:
3118 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003119 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003120 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003121 if (c == '/')
3122 break;
3123 goto check_star;
3124 }
3125 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003126 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 }
Denys Vlasenko22314682019-01-01 21:50:14 +01003128 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003129 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003130 dbg_lex("++p->lex_line=%zd", p->lex_line);
3131 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003133 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003134
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003135 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003136 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003137}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003138#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003139
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003140#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003141static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003142{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003143 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003145 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003146 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003147
3148 // This is the workhorse of the lexer.
3149 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003150// case '\0': // probably never reached
3151// p->lex_inbuf--;
3152// p->lex = XC_LEX_EOF;
3153// break;
3154 case '\n':
3155 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003156 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003157 p->lex = XC_LEX_NLINE;
3158 break;
3159 case '\t':
3160 case '\v':
3161 case '\f':
3162 case '\r':
3163 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003164 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003165 break;
3166 case '!':
3167 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3168 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3169 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003170 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003171 }
3172 break;
3173 case '"':
3174 s = zbc_lex_string();
3175 break;
3176 case '#':
3177 s = zbc_POSIX_does_not_allow("'#' script comments");
3178 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003179 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003180 break;
3181 case '%':
3182 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3183 break;
3184 case '&':
3185 c2 = *p->lex_inbuf;
3186 if (c2 == '&') {
3187 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3188 if (s) RETURN_STATUS(s);
3189 p->lex_inbuf++;
3190 p->lex = BC_LEX_OP_BOOL_AND;
3191 } else {
3192 p->lex = XC_LEX_INVALID;
3193 s = bc_error_bad_character('&');
3194 }
3195 break;
3196 case '(':
3197 case ')':
3198 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3199 break;
3200 case '*':
3201 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3202 break;
3203 case '+':
3204 c2 = *p->lex_inbuf;
3205 if (c2 == '+') {
3206 p->lex_inbuf++;
3207 p->lex = BC_LEX_OP_INC;
3208 } else
3209 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3210 break;
3211 case ',':
3212 p->lex = BC_LEX_COMMA;
3213 break;
3214 case '-':
3215 c2 = *p->lex_inbuf;
3216 if (c2 == '-') {
3217 p->lex_inbuf++;
3218 p->lex = BC_LEX_OP_DEC;
3219 } else
3220 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3221 break;
3222 case '.':
3223 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003224 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003225 else {
3226 p->lex = BC_LEX_KEY_LAST;
3227 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3228 }
3229 break;
3230 case '/':
3231 c2 = *p->lex_inbuf;
3232 if (c2 == '*')
3233 s = zbc_lex_comment();
3234 else
3235 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3236 break;
3237 case '0':
3238 case '1':
3239 case '2':
3240 case '3':
3241 case '4':
3242 case '5':
3243 case '6':
3244 case '7':
3245 case '8':
3246 case '9':
3247 case 'A':
3248 case 'B':
3249 case 'C':
3250 case 'D':
3251 case 'E':
3252 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003253 case 'G':
3254 case 'H':
3255 case 'I':
3256 case 'J':
3257 case 'K':
3258 case 'L':
3259 case 'M':
3260 case 'N':
3261 case 'O':
3262 case 'P':
3263 case 'Q':
3264 case 'R':
3265 case 'S':
3266 case 'T':
3267 case 'U':
3268 case 'V':
3269 case 'W':
3270 case 'X':
3271 case 'Y':
3272 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003273 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003274 break;
3275 case ';':
3276 p->lex = BC_LEX_SCOLON;
3277 break;
3278 case '<':
3279 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3280 break;
3281 case '=':
3282 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3283 break;
3284 case '>':
3285 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3286 break;
3287 case '[':
3288 case ']':
3289 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3290 break;
3291 case '\\':
3292 if (*p->lex_inbuf == '\n') {
3293 p->lex = XC_LEX_WHITESPACE;
3294 p->lex_inbuf++;
3295 } else
3296 s = bc_error_bad_character(c);
3297 break;
3298 case '^':
3299 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3300 break;
3301 case 'a':
3302 case 'b':
3303 case 'c':
3304 case 'd':
3305 case 'e':
3306 case 'f':
3307 case 'g':
3308 case 'h':
3309 case 'i':
3310 case 'j':
3311 case 'k':
3312 case 'l':
3313 case 'm':
3314 case 'n':
3315 case 'o':
3316 case 'p':
3317 case 'q':
3318 case 'r':
3319 case 's':
3320 case 't':
3321 case 'u':
3322 case 'v':
3323 case 'w':
3324 case 'x':
3325 case 'y':
3326 case 'z':
3327 s = zbc_lex_identifier();
3328 break;
3329 case '{':
3330 case '}':
3331 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3332 break;
3333 case '|':
3334 c2 = *p->lex_inbuf;
3335 if (c2 == '|') {
3336 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3337 if (s) RETURN_STATUS(s);
3338 p->lex_inbuf++;
3339 p->lex = BC_LEX_OP_BOOL_OR;
3340 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003341 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003342 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003343 }
3344 break;
3345 default:
3346 p->lex = XC_LEX_INVALID;
3347 s = bc_error_bad_character(c);
3348 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003349 }
3350
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003351 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003352}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003353#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003354#endif // ENABLE_BC
3355
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003356#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003357static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003358{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003359 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003360 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003361 xc_lex_whitespace(); // eats whitespace (but not newline)
3362 p->lex_inbuf++; // xc_lex_name() expects this
3363 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003364 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003365 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003366 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003367 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3368 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003369 }
3370
Denys Vlasenko29301232018-12-11 15:29:32 +01003371 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003372}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003373#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003374
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003375static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003376{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003377 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003378 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003379
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003380 p->lex = XC_LEX_STR;
3381 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003382
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003383 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003384 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003385 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003386 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003387 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003388 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003389 if (c == '[') depth++;
3390 if (c == ']')
3391 if (--depth == 0)
3392 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003393 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003394 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003395 dbg_lex("++p->lex_line=%zd", p->lex_line);
3396 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003397 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3398 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003399 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003400 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003401 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003402
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003403 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003404 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003405}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003406#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003407
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003408#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003409static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003410{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003411 static const //BcLexType - should be this type, but narrower type saves size:
3412 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003413 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003414 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 +01003415 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3416 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003417 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003418 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003419
Denys Vlasenko10bde142018-12-27 18:23:58 +01003420 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003421 BcStatus s;
3422 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003423 size_t i;
3424
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003425 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003426 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003427 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003428 }
3429
Denys Vlasenko81293c82018-12-24 01:53:55 +01003430 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003431 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003432 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003433 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003434 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003435 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003436 }
3437
3438 // This is the workhorse of the lexer.
3439 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003440// case '\0': // probably never reached
3441// p->lex = XC_LEX_EOF;
3442// break;
3443 case '\n':
3444 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3445 // (and "case '\n':" is not just empty here)
3446 // only to allow interactive dc have a way to exit
3447 // "parse" stage of "parse,execute" loop
3448 // on <enter>, not on _next_ token (which would mean
3449 // commands are not executed on pressing <enter>).
3450 // IOW: typing "1p<enter>" should print "1" _at once_,
3451 // not after some more input.
3452 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003453 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003454 p->lex = XC_LEX_NLINE;
3455 break;
3456 case '\t':
3457 case '\v':
3458 case '\f':
3459 case '\r':
3460 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003461 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003462 break;
3463 case '!':
3464 c2 = *p->lex_inbuf;
3465 if (c2 == '=')
3466 p->lex = XC_LEX_OP_REL_NE;
3467 else if (c2 == '<')
3468 p->lex = XC_LEX_OP_REL_LE;
3469 else if (c2 == '>')
3470 p->lex = XC_LEX_OP_REL_GE;
3471 else
3472 RETURN_STATUS(bc_error_bad_character(c));
3473 p->lex_inbuf++;
3474 break;
3475 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003476 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003477 break;
3478 case '.':
3479 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003480 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003481 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003482 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003483 break;
3484 case '0':
3485 case '1':
3486 case '2':
3487 case '3':
3488 case '4':
3489 case '5':
3490 case '6':
3491 case '7':
3492 case '8':
3493 case '9':
3494 case 'A':
3495 case 'B':
3496 case 'C':
3497 case 'D':
3498 case 'E':
3499 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003500 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003501 break;
3502 case '[':
3503 s = zdc_lex_string();
3504 break;
3505 default:
3506 p->lex = XC_LEX_INVALID;
3507 s = bc_error_bad_character(c);
3508 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003509 }
3510
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003511 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003512}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003513#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003514#endif // ENABLE_DC
3515
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003516static void xc_parse_push(unsigned i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003517{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003518 BcVec *code = &G.prs.func->code;
3519 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003520 bc_vec_pushByte(code, (uint8_t)i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003521}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003522
Denys Vlasenko10bde142018-12-27 18:23:58 +01003523static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003524{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003525#if 1
3526 BcVec *code = &G.prs.func->code;
3527 size_t pos = code->len;
3528 size_t len = strlen(name) + 1;
3529
3530 bc_vec_expand(code, pos + len);
3531 strcpy(code->v + pos, name);
3532 code->len = pos + len;
3533#else
3534 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003535 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003536 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003537 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003538#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003539}
3540
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003541// Indexes < 0xfc are encoded verbatim, else first byte is
3542// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3543// followed by that many bytes, lsb first.
3544// (The above describes 32-bit case).
3545#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3546
Denys Vlasenko53799502019-01-25 14:24:03 +01003547static void bc_vec_pushIndex(BcVec *v, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003548{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003549 size_t mask;
3550 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003551
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003552 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003553 if (idx < SMALL_INDEX_LIMIT) {
Denys Vlasenko53799502019-01-25 14:24:03 +01003554 bc_vec_pushByte(v, idx);
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003555 return;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003556 }
3557
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003558 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3559 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003560 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003561 if (idx & mask) break;
3562 mask >>= 8;
3563 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003564 }
3565 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003566
Denys Vlasenko53799502019-01-25 14:24:03 +01003567 bc_vec_pushByte(v, (SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003568
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003569 do {
Denys Vlasenko53799502019-01-25 14:24:03 +01003570 bc_vec_pushByte(v, (unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003571 idx >>= 8;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003572 } while (idx != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003573}
3574
Denys Vlasenko53799502019-01-25 14:24:03 +01003575static void xc_parse_pushIndex(size_t idx)
3576{
3577 bc_vec_pushIndex(&G.prs.func->code, idx);
3578}
3579
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003580static void xc_parse_pushInst_and_Index(unsigned inst, size_t idx)
3581{
3582 xc_parse_push(inst);
3583 xc_parse_pushIndex(idx);
3584}
3585
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003586#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003587static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003588{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003589 xc_parse_pushInst_and_Index(BC_INST_JUMP, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003590}
3591
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003592static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003593{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003594 xc_parse_pushInst_and_Index(BC_INST_JUMP_ZERO, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003595}
3596
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003597static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003598{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003599 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003600 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003601
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003602 xc_parse_pushInst_and_Index(XC_INST_STR, p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003603 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003604
Denys Vlasenko10bde142018-12-27 18:23:58 +01003605 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003606}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003607#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003608#endif
3609
Denys Vlasenko10bde142018-12-27 18:23:58 +01003610static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003611{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003612 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003613 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003614#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003615 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003616#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003617 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003618#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003619 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003620#endif
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003621 xc_parse_pushInst_and_Index(XC_INST_NUM, idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003622}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003623
Denys Vlasenko10bde142018-12-27 18:23:58 +01003624static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003625{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003626 G.prs.func = xc_program_func(G.prs.fidx);
3627 G.prs.lex_inbuf = text;
3628 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3629 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003630}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003631#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003632
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003633// Called when parsing or execution detects a failure,
3634// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003635static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003636{
3637 BcFunc *f;
3638 BcInstPtr *ip;
3639
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003640 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003641 bc_vec_pop_all(&G.prog.results);
3642
Denys Vlasenko10bde142018-12-27 18:23:58 +01003643 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003644 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003645 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003646}
3647
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003648// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003649// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003650static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003651{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003652 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003653 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003654 bc_func_free(p->func);
3655 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003656
Denys Vlasenko65e10462018-12-19 15:13:14 +01003657 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003658 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003659 }
3660
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003661 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003662 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003663
Denys Vlasenko503faf92018-12-20 16:24:18 +01003664 IF_BC(bc_vec_pop_all(&p->exits);)
3665 IF_BC(bc_vec_pop_all(&p->conds);)
3666 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003667
Denys Vlasenko10bde142018-12-27 18:23:58 +01003668 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003669}
3670
Denys Vlasenko10bde142018-12-27 18:23:58 +01003671static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003672{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003673 IF_BC(bc_vec_free(&G.prs.exits);)
3674 IF_BC(bc_vec_free(&G.prs.conds);)
3675 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003676 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003677}
3678
Denys Vlasenko10bde142018-12-27 18:23:58 +01003679static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003680{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003681 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003682 memset(p, 0, sizeof(BcParse));
3683
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003684 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003685 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3686 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3687 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003688
Denys Vlasenkofa210792018-12-19 19:35:40 +01003689 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003690 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003691}
3692
Denys Vlasenko10bde142018-12-27 18:23:58 +01003693static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003694{
Denys Vlasenko04715442018-12-21 00:10:26 +01003695 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003696 BcFunc f;
3697 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003698 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003699 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003700 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003701}
3702
3703#if ENABLE_BC
3704
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003705// Note: takes ownership of 'name' (must be malloced)
3706static size_t bc_program_addFunc(char *name)
3707{
3708 size_t idx;
3709 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003710 int inserted;
3711
3712 entry.name = name;
3713 entry.idx = G.prog.fns.len;
3714
3715 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3716 if (!inserted) free(name);
3717
3718 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3719 idx = entry_ptr->idx;
3720
3721 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003722 // There is already a function with this name.
3723 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003724 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003725 bc_func_free(func);
3726 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003727 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003728 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003729 }
3730
3731 return idx;
3732}
3733
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003734#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003735// We can calculate the conversion between tokens and exprs by subtracting the
3736// position of the first operator in the lex enum and adding the position of the
3737// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003738#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003739
Denys Vlasenko132d7c02019-01-08 19:29:35 +01003740static BC_STATUS zbc_parse_expr(uint8_t flags);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003741#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003742
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003743static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003744#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003745
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003746static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003747{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003748 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003749}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003750#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003751
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003752static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003753{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003754 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003755 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3756 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003757 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003758 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003759 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003760 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003761
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003762 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003763}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003764#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003765
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003766static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003767{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003768 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003769 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3770 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003771
3772 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003773 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003774 if (t == BC_LEX_LPAREN) break;
3775
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003776 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003777 if (l >= r && (l != r || !left)) break;
3778
Denys Vlasenko10bde142018-12-27 18:23:58 +01003779 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003780 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003781 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003782 }
3783
3784 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003785}
3786
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003787static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003788{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003789 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003790 BcLexType top;
3791
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003792 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003793 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003794 top = BC_PARSE_TOP_OP(p);
3795
3796 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003797 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003798
3799 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003800 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003801
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003802 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003803 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003804 top = BC_PARSE_TOP_OP(p);
3805 }
3806
3807 bc_vec_pop(&p->ops);
3808
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003809 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003810}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003811#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003812
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003813static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003814{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003815 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003816 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 size_t nparams;
3818
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003819 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003820 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3821
Denys Vlasenko10bde142018-12-27 18:23:58 +01003822 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003823 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003824
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003825 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003826 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003827 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003828 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003829 if (s) RETURN_STATUS(s);
3830 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003831 if (p->lex != BC_LEX_COMMA) {
3832 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003833 break;
3834 RETURN_STATUS(bc_error_bad_token());
3835 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003836 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003837 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003838 }
3839 }
3840
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003841 xc_parse_pushInst_and_Index(BC_INST_CALL, nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003842
Denys Vlasenko26819db2018-12-12 16:08:46 +01003843 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003844}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003845#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003846
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003847// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003848static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003849{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003850 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003851 BcStatus s;
3852 BcId entry, *entry_ptr;
3853 size_t idx;
3854
3855 entry.name = name;
3856
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003857 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003858 if (s) goto err;
3859
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003860 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003861 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003862 goto err;
3863 }
3864
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003865 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003866
3867 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003868 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003869 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003870 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003871 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 free(name);
3873
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003874 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003875 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003876
Denys Vlasenko10bde142018-12-27 18:23:58 +01003877 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003878 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003879 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003880 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003881}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003882#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003883
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003884static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003885{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003886 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 BcStatus s;
3888 char *name;
3889
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003890 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003891 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003892 if (s) goto err;
3893
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003894 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003895 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003896 if (s) goto err;
3897
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003898 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003899 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003900 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003901 goto err;
3902 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003903 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003904 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003905 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003906 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003907 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 if (s) goto err;
3909 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003910 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003911 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003912 xc_parse_push(*type);
3913 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003914 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003915 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003916 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003917 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003918 goto err;
3919 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003920 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003921 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003922 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003923 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003924 xc_parse_push(XC_INST_VAR);
3925 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003926 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003927 }
3928
Denys Vlasenko26819db2018-12-12 16:08:46 +01003929 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003930 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003931 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003932 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003933}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003934#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003935
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003936static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003937{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003938 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003939 BcStatus s;
3940
Denys Vlasenko10bde142018-12-27 18:23:58 +01003941 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003942 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003943 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003944
Denys Vlasenko10bde142018-12-27 18:23:58 +01003945 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003946 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003947 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003948
Denys Vlasenko10bde142018-12-27 18:23:58 +01003949 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003951 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003952}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003953#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003954
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003955static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003956{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003957 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003958 BcStatus s;
3959
Denys Vlasenko10bde142018-12-27 18:23:58 +01003960 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003961 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003962 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003963
3964 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3965
Denys Vlasenko10bde142018-12-27 18:23:58 +01003966 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003967 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003968
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003969 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003970 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003971
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003972 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003973
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003974 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003975 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003976
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003977 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003978}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003979#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003980
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003981static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003982{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003983 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003984 BcStatus s;
3985
Denys Vlasenko10bde142018-12-27 18:23:58 +01003986 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003987 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003988
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003989 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003990 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003991 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003992 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003993 }
3994
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003995 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003996 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3997
Denys Vlasenko10bde142018-12-27 18:23:58 +01003998 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003999 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004000
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004001 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004002 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004003 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01004004 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004005 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006
Denys Vlasenko10bde142018-12-27 18:23:58 +01004007 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004008}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004009#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004010
Denys Vlasenko22314682019-01-01 21:50:14 +01004011static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004012{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004013 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004014 BcStatus s;
4015 BcLexType type;
4016 char inst;
4017 BcInst etype = *prev;
4018
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004019 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4020 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4021 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004022 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004023 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004024 xc_parse_push(inst);
4025 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004026 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004027 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004028
Denys Vlasenko10bde142018-12-27 18:23:58 +01004029 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004030 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004031 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004032
4033 // Because we parse the next part of the expression
4034 // right here, we need to increment this.
Denys Vlasenko22314682019-01-01 21:50:14 +01004035 *nexs = *nexs + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004036
4037 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004038 case XC_LEX_NAME:
4039 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
4040 break;
4041 case BC_LEX_KEY_IBASE:
4042 case BC_LEX_KEY_LAST:
4043 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004044 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4045 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004046 break;
4047 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004048 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004049 if (s) RETURN_STATUS(s);
4050 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004051 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004052 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01004053 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004054 break;
4055 default:
4056 s = bc_error_bad_token();
4057 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004058 }
4059
Denys Vlasenko10bde142018-12-27 18:23:58 +01004060 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004061 }
4062
Denys Vlasenko26819db2018-12-12 16:08:46 +01004063 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004064}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004065#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004066
Denys Vlasenko22314682019-01-01 21:50:14 +01004067static int bc_parse_inst_isLeaf(BcInst p)
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004068{
4069 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4070 || p == BC_INST_INC_POST
4071 || p == BC_INST_DEC_POST
4072 ;
4073}
Denys Vlasenko22314682019-01-01 21:50:14 +01004074#define BC_PARSE_LEAF(prev, bin_last, rparen) \
4075 (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev)))
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004076
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004077static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
Denys Vlasenko22314682019-01-01 21:50:14 +01004078 bool rparen, bool bin_last, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004079{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004080 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004081 BcStatus s;
4082 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004083
Denys Vlasenko10bde142018-12-27 18:23:58 +01004084 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004085 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004086
Denys Vlasenko22314682019-01-01 21:50:14 +01004087 type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004088 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004089
4090 // We can just push onto the op stack because this is the largest
4091 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004092 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004093 bc_vec_push(&p->ops, &type);
4094 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004095 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004096
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004097 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004098}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004099#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004100
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004101static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004102{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004103 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004104 BcStatus s;
4105 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004106
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004107 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004108 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004109 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004110 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004111 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004112 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004113 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004114 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004115 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004116 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004117 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004118 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004119 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004120 }
4121
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004122 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004123}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004124#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004125
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004126static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004127{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004128 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004129 BcStatus s;
4130 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004131
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004132 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004133 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004134 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004135
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004136 t = p->lex;
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004137 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004138 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004139 else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004140//TODO: if (p->func->voidfunc) ERROR
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004141 s = zbc_parse_expr(0);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004142 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004143
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004144 if (t != BC_LEX_LPAREN // "return EXPR", no ()
4145 || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b"
4146 ) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004147 s = zbc_POSIX_requires("parentheses around return expressions");
4148 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004149 }
4150
Denys Vlasenko10bde142018-12-27 18:23:58 +01004151 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004152 }
4153
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004154 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004155 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004157#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004158
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004159static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004160{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004161 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004162 size_t *label = bc_vec_item(&p->func->labels, idx);
4163 *label = p->func->code.len;
4164}
4165
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004166static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004167{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004168 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004169 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004170 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004171
4172 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004173 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004174 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004175 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004176
Denys Vlasenko10bde142018-12-27 18:23:58 +01004177 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004178 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004179 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004180 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004181 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004182
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004183 // Encode "if zero, jump to ..."
4184 // Pushed value (destination of the jump) is uninitialized,
4185 // will be rewritten to be address of "end of if()" or of "else".
4186 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004187 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004188
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004189 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004190 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004191
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004192 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4193 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004194 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004195
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004196 // Encode "after then_stmt, jump to end of if()"
4197 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4198 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004199 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004200
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004201 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 +01004202 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004203
Denys Vlasenko15850832018-12-16 21:40:54 +01004204 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004205
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004206 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004207 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004208 }
4209
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004210 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 +01004211 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004212
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004213 dbg_lex_done("%s:%d done", __func__, __LINE__);
4214 RETURN_STATUS(s);
4215}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004216#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004217
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004218static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004219{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004220 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004221 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004222 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004223 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004224
Denys Vlasenko10bde142018-12-27 18:23:58 +01004225 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004226 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004227 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004228 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004229 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004230
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004231 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004232 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004233 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004234
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004235 bc_vec_push(&p->exits, &ip_idx);
4236 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004237
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004238 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004239 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004240 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004241
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004242 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004243
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004244 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004245 if (s) RETURN_STATUS(s);
4246
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004247 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004248 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004249
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004250 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004251 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004252
4253 bc_vec_pop(&p->exits);
4254 bc_vec_pop(&p->conds);
4255
4256 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004257}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004258#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004259
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004260static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004261{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004262 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004263 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004264 size_t cond_idx, exit_idx, body_idx, update_idx;
4265
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004266 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004267 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004268 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004269 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004270 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004271 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004273 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004274 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004275 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004276 if (s) RETURN_STATUS(s);
4277 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004278 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4279 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004280 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004282 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004283 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004284 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004285
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004286 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004287 update_idx = cond_idx + 1;
4288 body_idx = update_idx + 1;
4289 exit_idx = body_idx + 1;
4290
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004291 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004292 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004293 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004294 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004295 // This is safe to set because the current token is a semicolon,
4296 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004297 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004298 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004299 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004300 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004301 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004302
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004303 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
Denys Vlasenko10bde142018-12-27 18:23:58 +01004305 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004306 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004307
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004308 bc_parse_pushJUMP_ZERO(exit_idx);
4309 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
Gavin Howard01055ba2018-11-03 11:00:21 -06004311 bc_vec_push(&p->conds, &update_idx);
4312 bc_vec_push(&p->func->labels, &p->func->code.len);
4313
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004314 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004315 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004316 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004317 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004318 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004319 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004320 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4321 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004322 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004323
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004324 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004325 bc_vec_push(&p->func->labels, &p->func->code.len);
4326
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004327 bc_vec_push(&p->exits, &exit_idx);
4328 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004329
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004330 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004331 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004332
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004333 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004334 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004335
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004336 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004337 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004338
4339 bc_vec_pop(&p->exits);
4340 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004341
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004342 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004343}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004344#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004345
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004346static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004347{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004348 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004349 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004350
Gavin Howard01055ba2018-11-03 11:00:21 -06004351 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004352 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4353 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004354 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004355 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004356 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004357 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004358 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004359
Denys Vlasenko10bde142018-12-27 18:23:58 +01004360 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004361}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004362#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004363
Denys Vlasenko53799502019-01-25 14:24:03 +01004364static BC_STATUS zbc_func_insert(BcFunc *f, char *name, BcType type)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004365{
4366 BcId *autoid;
4367 BcId a;
4368 size_t i;
4369
4370 autoid = (void*)f->autos.v;
4371 for (i = 0; i < f->autos.len; i++, autoid++) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004372 if (strcmp(name, autoid->name) == 0
Denys Vlasenko53799502019-01-25 14:24:03 +01004373 && type == (BcType) autoid->idx
Denys Vlasenko22314682019-01-01 21:50:14 +01004374 ) {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004375 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko22314682019-01-01 21:50:14 +01004376 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004377 }
4378
Denys Vlasenko53799502019-01-25 14:24:03 +01004379 a.idx = type;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004380 a.name = name;
4381
4382 bc_vec_push(&f->autos, &a);
4383
4384 RETURN_STATUS(BC_STATUS_SUCCESS);
4385}
4386#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4387
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004388static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004389{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004390 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004391 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01004392 bool comma, voidfunc;
Gavin Howard01055ba2018-11-03 11:00:21 -06004393 char *name;
4394
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004395 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004396 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004397 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004398 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004399 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004400
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004401 // To be maximally both POSIX and GNU-compatible,
4402 // "void" is not treated as a normal keyword:
4403 // you can have variable named "void", and even a function
4404 // named "void": "define void() { return 6; }" is ok.
4405 // _Only_ "define void f() ..." syntax treats "void"
4406 // specially.
4407 voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004408
Denys Vlasenko10bde142018-12-27 18:23:58 +01004409 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004410 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004411
4412 voidfunc = (voidfunc && p->lex == XC_LEX_NAME);
4413 if (voidfunc) {
4414 s = zxc_lex_next();
4415 if (s) RETURN_STATUS(s);
4416 }
4417
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004418 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko22314682019-01-01 21:50:14 +01004419 RETURN_STATUS(bc_error_bad_function_definition());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004420
4421 p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v));
4422 p->func = xc_program_func(p->fidx);
4423 p->func->voidfunc = voidfunc;
4424
Denys Vlasenko10bde142018-12-27 18:23:58 +01004425 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004426 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004427
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004428 comma = false;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004429 while (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004430 BcType t = BC_TYPE_VAR;
4431
4432 if (p->lex == XC_LEX_OP_MULTIPLY) {
4433 t = BC_TYPE_REF;
4434 s = zxc_lex_next();
4435 if (s) RETURN_STATUS(s);
4436 s = zbc_POSIX_does_not_allow("references");
4437 if (s) RETURN_STATUS(s);
4438 }
4439
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004440 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004441 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004442
4443 ++p->func->nparams;
4444
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004445 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004446 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004447 if (s) goto err;
4448
Denys Vlasenko53799502019-01-25 14:24:03 +01004449 if (p->lex == BC_LEX_LBRACKET) {
4450 if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004451 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 if (s) goto err;
4453
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004454 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004455 s = bc_error_bad_function_definition();
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 goto err;
4457 }
4458
Denys Vlasenko10bde142018-12-27 18:23:58 +01004459 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 if (s) goto err;
4461 }
Denys Vlasenko53799502019-01-25 14:24:03 +01004462 else if (t == BC_TYPE_REF) {
4463 s = bc_error_at("vars can't be references");
4464 goto err;
4465 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004466
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004467 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004468 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004469 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004470 if (s) goto err;
4471 }
4472
Denys Vlasenko53799502019-01-25 14:24:03 +01004473 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004474 if (s) goto err;
4475 }
4476
Denys Vlasenko22314682019-01-01 21:50:14 +01004477 if (comma) RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004478
Denys Vlasenko10bde142018-12-27 18:23:58 +01004479 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004480 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004481
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004482 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004483 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004484 if (s) RETURN_STATUS(s);
4485 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004486
Denys Vlasenko202dd192018-12-16 17:30:35 +01004487 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004488 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004489 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004490 // GNU bc requires a {} block even if function body has single stmt, enforce this
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004491 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004492 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004493
4494 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004495 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004496 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004497 if (s) RETURN_STATUS(s);
4498
Denys Vlasenko10bde142018-12-27 18:23:58 +01004499 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004500
4501 // Subsequent code generation is into main program
4502 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004503 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004504
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004505 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004506 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004507 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004508 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004509 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004510 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004511}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004512#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004513
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004514static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004515{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004516 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004517 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004518 char *name;
4519
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004520 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004521 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004522 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004523
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004524 for (;;) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004525 BcType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004526
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004527 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004528 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004529
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004530 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004531 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004532 if (s) goto err;
4533
Denys Vlasenko53799502019-01-25 14:24:03 +01004534 t = BC_TYPE_VAR;
4535 if (p->lex == BC_LEX_LBRACKET) {
4536 t = BC_TYPE_ARRAY;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004537 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004538 if (s) goto err;
4539
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004540 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004541 s = bc_error_at("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004542 goto err;
4543 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004544 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004545 if (s) goto err;
4546 }
4547
Denys Vlasenko53799502019-01-25 14:24:03 +01004548 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004550
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004551 if (p->lex == XC_LEX_NLINE
4552 || p->lex == BC_LEX_SCOLON
4553 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004554 ) {
4555 break;
4556 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004557 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko22314682019-01-01 21:50:14 +01004558 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004559 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004560 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004561 }
4562
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004563 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004564 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004565 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004566 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004567 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004568 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004569}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004570#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004571
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004572#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004573static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004574{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004575 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 BcStatus s = BC_STATUS_SUCCESS;
4577
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004578 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004579
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004580 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004581 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004582 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004583 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004584 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004585 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004586 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004587 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004588
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004589 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004590 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4591 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004592 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004593 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004594 } while (p->lex == XC_LEX_NLINE);
4595 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004596 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004597 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004598 if (s) RETURN_STATUS(s);
4599 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004600 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004601 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004602 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004603 if (s) RETURN_STATUS(s);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004604 // Check that next token is a correct stmt delimiter -
4605 // disallows "print 1 print 2" and such.
4606 if (p->lex == BC_LEX_RBRACE)
4607 break;
4608 if (p->lex != BC_LEX_SCOLON
4609 && p->lex != XC_LEX_NLINE
4610 ) {
4611 RETURN_STATUS(bc_error_at("bad statement terminator"));
4612 }
4613 s = zxc_lex_next();
4614 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004615 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004616 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004617 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4618 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004619 }
4620
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004621 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4622 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004623 case XC_LEX_OP_MINUS:
4624 case BC_LEX_OP_INC:
4625 case BC_LEX_OP_DEC:
4626 case BC_LEX_OP_BOOL_NOT:
4627 case BC_LEX_LPAREN:
4628 case XC_LEX_NAME:
4629 case XC_LEX_NUMBER:
4630 case BC_LEX_KEY_IBASE:
4631 case BC_LEX_KEY_LAST:
4632 case BC_LEX_KEY_LENGTH:
4633 case BC_LEX_KEY_OBASE:
4634 case BC_LEX_KEY_READ:
4635 case BC_LEX_KEY_SCALE:
4636 case BC_LEX_KEY_SQRT:
4637 s = zbc_parse_expr(BC_PARSE_PRINT);
4638 break;
4639 case XC_LEX_STR:
4640 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004641 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004642 break;
4643 case BC_LEX_KEY_BREAK:
4644 case BC_LEX_KEY_CONTINUE:
4645 s = zbc_parse_break_or_continue(p->lex);
4646 break;
4647 case BC_LEX_KEY_FOR:
4648 s = zbc_parse_for();
4649 break;
4650 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004651 xc_parse_push(BC_INST_HALT);
4652 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004653 break;
4654 case BC_LEX_KEY_IF:
4655 s = zbc_parse_if();
4656 break;
4657 case BC_LEX_KEY_LIMITS:
4658 // "limits" is a compile-time command,
4659 // the output is produced at _parse time_.
4660 printf(
4661 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4662 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4663 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4664 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
Denys Vlasenkoe05ec6e2019-01-04 16:26:19 +01004665 // "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" - GNU bc does not show this
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004666 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4667 "Number of vars = "BC_MAX_VARS_STR "\n"
4668 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004669 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004670 break;
4671 case BC_LEX_KEY_PRINT:
4672 s = zbc_parse_print();
4673 break;
4674 case BC_LEX_KEY_QUIT:
4675 // "quit" is a compile-time command. For example,
4676 // "if (0 == 1) quit" terminates when parsing the statement,
4677 // not when it is executed
4678 QUIT_OR_RETURN_TO_MAIN;
4679 case BC_LEX_KEY_RETURN:
4680 if (!p->in_funcdef)
4681 RETURN_STATUS(bc_error("'return' not in a function"));
4682 s = zbc_parse_return();
4683 break;
4684 case BC_LEX_KEY_WHILE:
4685 s = zbc_parse_while();
4686 break;
4687 default:
4688 s = bc_error_bad_token();
4689 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004690 }
4691
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004692 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004693 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004694}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004695#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004696
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004697static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004698{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004699 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004700 BcStatus s;
4701
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004702 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004703//why?
4704// if (p->lex == XC_LEX_EOF)
4705// s = bc_error("end of file");
4706// else
4707 if (p->lex == BC_LEX_KEY_DEFINE) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004708 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004709 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004710 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004711 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004712 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004713 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004714
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004715 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004716 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004717}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004718#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004719
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004720#undef zbc_parse_expr
4721static BC_STATUS zbc_parse_expr(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004722{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004723 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004724 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004725 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004726 unsigned nparens, nrelops;
Denys Vlasenko22314682019-01-01 21:50:14 +01004727 bool paren_first, rprn, assign, bin_last, incdec;
Gavin Howard01055ba2018-11-03 11:00:21 -06004728
Denys Vlasenko17df8822018-12-14 23:00:24 +01004729 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004730 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004731 nparens = nrelops = 0;
Denys Vlasenko22314682019-01-01 21:50:14 +01004732 rprn = assign = incdec = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004733 bin_last = true;
4734
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004735 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004736 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004737 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004738 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004739
4740 if (!lex_allowed_in_bc_expr(t))
4741 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004742
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004743 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004744 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004745 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004746 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004747 case BC_LEX_OP_INC:
4748 case BC_LEX_OP_DEC:
4749 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004750 if (incdec) RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko22314682019-01-01 21:50:14 +01004751 s = zbc_parse_incdec(&prev, &nexprs, flags);
4752 incdec = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004753 rprn = bin_last = false;
4754 //get_token = false; - already is
4755 break;
4756 case XC_LEX_OP_MINUS:
4757 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004758 s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004759 rprn = false;
4760 //get_token = false; - already is
4761 bin_last = (prev == XC_INST_MINUS);
Denys Vlasenko22314682019-01-01 21:50:14 +01004762 if (bin_last) incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004763 break;
4764 case BC_LEX_OP_ASSIGN_POWER:
4765 case BC_LEX_OP_ASSIGN_MULTIPLY:
4766 case BC_LEX_OP_ASSIGN_DIVIDE:
4767 case BC_LEX_OP_ASSIGN_MODULUS:
4768 case BC_LEX_OP_ASSIGN_PLUS:
4769 case BC_LEX_OP_ASSIGN_MINUS:
4770 case BC_LEX_OP_ASSIGN:
4771 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4772 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4773 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4774 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4775 ) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004776 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004777 }
4778 // Fallthrough.
4779 case XC_LEX_OP_POWER:
4780 case XC_LEX_OP_MULTIPLY:
4781 case XC_LEX_OP_DIVIDE:
4782 case XC_LEX_OP_MODULUS:
4783 case XC_LEX_OP_PLUS:
4784 case XC_LEX_OP_REL_EQ:
4785 case XC_LEX_OP_REL_LE:
4786 case XC_LEX_OP_REL_GE:
4787 case XC_LEX_OP_REL_NE:
4788 case XC_LEX_OP_REL_LT:
4789 case XC_LEX_OP_REL_GT:
4790 case BC_LEX_OP_BOOL_NOT:
4791 case BC_LEX_OP_BOOL_OR:
4792 case BC_LEX_OP_BOOL_AND:
4793 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004794 if (t == BC_LEX_OP_BOOL_NOT) {
4795 if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004796 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko22314682019-01-01 21:50:14 +01004797 } else if (prev == XC_INST_BOOL_NOT) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004798 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004799 }
Denys Vlasenko22314682019-01-01 21:50:14 +01004800
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004801 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4802 prev = BC_TOKEN_2_INST(t);
4803 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko22314682019-01-01 21:50:14 +01004804 rprn = incdec = false;
4805 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004806 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4807 break;
4808 case BC_LEX_LPAREN:
4809 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004810 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004811 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004812 bc_vec_push(&p->ops, &t);
4813 nparens++;
4814 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004815 rprn = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004816 break;
4817 case BC_LEX_RPAREN:
4818 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenkoa1698a12019-01-08 19:32:38 +01004819//why?
4820// if (p->lex_last == BC_LEX_LPAREN) {
4821// RETURN_STATUS(bc_error_at("empty expression"));
4822// }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004823 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004824 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004825 if (nparens == 0) {
4826 goto exit_loop;
4827 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004828 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4829 nparens--;
4830 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004831 rprn = true;
4832 bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004833 break;
4834 case XC_LEX_NAME:
4835 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004836 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004837 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004838 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko22314682019-01-01 21:50:14 +01004839 rprn = (prev == BC_INST_CALL);
4840 bin_last = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004841 //get_token = false; - already is
4842 nexprs++;
4843 break;
4844 case XC_LEX_NUMBER:
4845 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004846 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004847 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004848 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004849 prev = XC_INST_NUM;
4850 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004851 rprn = bin_last = false;
4852 nexprs++;
4853 break;
4854 case BC_LEX_KEY_IBASE:
4855 case BC_LEX_KEY_LAST:
4856 case BC_LEX_KEY_OBASE:
4857 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004858 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004859 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004860 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004861 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004862 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004863 rprn = bin_last = false;
4864 nexprs++;
4865 break;
4866 case BC_LEX_KEY_LENGTH:
4867 case BC_LEX_KEY_SQRT:
4868 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004869 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004870 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004871 s = zbc_parse_builtin(t, flags, &prev);
4872 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004873 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004874 nexprs++;
4875 break;
4876 case BC_LEX_KEY_READ:
4877 dbg_lex("%s:%d LEX_READ", __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_read();
4881 prev = XC_INST_READ;
4882 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004883 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004884 nexprs++;
4885 break;
4886 case BC_LEX_KEY_SCALE:
4887 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004888 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004889 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004890 s = zbc_parse_scale(&prev, flags);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004891 //get_token = false; - already is
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004892 rprn = bin_last = false;
4893 nexprs++;
4894 break;
4895 default:
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004896 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 }
4898
Denys Vlasenkobb116032018-12-25 01:16:37 +01004899 if (s || G_interrupt) // error, or ^C: stop parsing
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004900 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004901 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004902 s = zxc_lex_next();
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004903 if (s) RETURN_STATUS(s);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004904 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004905 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004906 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004907
4908 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004909 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004910 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004911
4912 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004913 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004914
Denys Vlasenko10bde142018-12-27 18:23:58 +01004915 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004916
Denys Vlasenko69560f42018-12-24 14:14:23 +01004917 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004918 bc_vec_pop(&p->ops);
4919 }
4920
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004921 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004922 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004923
Gavin Howard01055ba2018-11-03 11:00:21 -06004924 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004925 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004926 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004927 if (s) RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004928 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004929 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004930 s = zbc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004931 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004932 }
4933
4934 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004935 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004936 xc_parse_push(XC_INST_PRINT);
4937 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004938 }
4939
Denys Vlasenko17df8822018-12-14 23:00:24 +01004940 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004941 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004942}
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004943#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004944
Gavin Howard01055ba2018-11-03 11:00:21 -06004945#endif // ENABLE_BC
4946
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004947#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004948
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004949static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004950{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004951 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004952 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004953
Denys Vlasenko10bde142018-12-27 18:23:58 +01004954 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004955 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004956 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004957
Denys Vlasenko10bde142018-12-27 18:23:58 +01004958 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004959
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004960 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004961}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004962#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004963
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004964static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004965{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004966 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004967 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004968 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004969
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004970 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004971
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004972 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01004973 xc_parse_pushInst_and_Index(XC_INST_STR, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004974 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004975
Brian Foleyb64470b2019-09-05 10:50:13 +02004976 // Add an empty function so that if zdc_program_execStr ever needs to
4977 // parse the string into code (from the 'x' command) there's somewhere
4978 // to store the bytecode.
Denys Vlasenko10bde142018-12-27 18:23:58 +01004979 xc_program_add_fn();
4980 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004982 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004983}
4984
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004985static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004986{
4987 BcStatus s;
4988
Denys Vlasenko10bde142018-12-27 18:23:58 +01004989 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004990 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004991 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004992 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004993 }
4994
4995 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004996 xc_parse_push(DC_INST_SWAP);
4997 xc_parse_push(XC_INST_ASSIGN);
4998 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 }
5000
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005001 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005002}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005003#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005004
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005005static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005006{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005007 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06005008 BcStatus s;
5009
Denys Vlasenko10bde142018-12-27 18:23:58 +01005010 xc_parse_push(inst);
5011 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06005012
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005013 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005014 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005015
Denys Vlasenko10bde142018-12-27 18:23:58 +01005016 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005017 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005018
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005019 // Note that 'else' part can not be on the next line:
5020 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
5021 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005022 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005023 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005024 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005025 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005026 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005027 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005028 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005029
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005030 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005031}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005032#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005033
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005034static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005035{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005036 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005037 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005038 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005039
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005040 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005041 s = BC_STATUS_SUCCESS;
5042 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005043 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005044 case XC_LEX_OP_REL_EQ:
5045 case XC_LEX_OP_REL_LE:
5046 case XC_LEX_OP_REL_GE:
5047 case XC_LEX_OP_REL_NE:
5048 case XC_LEX_OP_REL_LT:
5049 case XC_LEX_OP_REL_GT:
5050 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
5051 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
5052 get_token = false;
5053 break;
5054 case DC_LEX_SCOLON:
5055 case DC_LEX_COLON:
5056 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
5057 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
5058 break;
5059 case XC_LEX_STR:
5060 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
5061 dc_parse_string();
5062 break;
5063 case XC_LEX_NEG:
5064 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005065 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005066 if (s) RETURN_STATUS(s);
5067 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005068 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005069 xc_parse_pushNUM();
5070 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005071 break;
5072 case XC_LEX_NUMBER:
5073 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005074 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005075 break;
5076 case DC_LEX_READ:
5077 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005078 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005079 break;
5080 case DC_LEX_OP_ASSIGN:
5081 case DC_LEX_STORE_PUSH:
5082 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
5083 assign = (t == DC_LEX_OP_ASSIGN);
5084 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
5085 s = zdc_parse_mem(inst, true, assign);
5086 break;
5087 case DC_LEX_LOAD:
5088 case DC_LEX_LOAD_POP:
5089 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
5090 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
5091 s = zdc_parse_mem(inst, true, false);
5092 break;
5093 case DC_LEX_STORE_IBASE:
5094 case DC_LEX_STORE_SCALE:
5095 case DC_LEX_STORE_OBASE:
5096 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5097 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5098 s = zdc_parse_mem(inst, false, true);
5099 break;
5100 default:
5101 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5102 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005103 }
5104
Denys Vlasenko10bde142018-12-27 18:23:58 +01005105 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005106
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005107 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005108 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005109}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005110#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005111
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005112static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005113{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005114 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005115 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005116
Denys Vlasenko6842c602019-01-04 05:41:47 +01005117 if (p->lex == XC_LEX_NLINE)
5118 RETURN_STATUS(zxc_lex_next());
5119
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005120 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005121 if (i >= 0) {
5122 BcInst inst = dc_LEX_to_INST[i];
5123 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005124 xc_parse_push(inst);
5125 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005126 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005127 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005128 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005129}
5130#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5131
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005132static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005133{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005134 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005135 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5136 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005137 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005138 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005139 }
5140
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005141 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005142 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005143}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005144#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005145
Gavin Howard01055ba2018-11-03 11:00:21 -06005146#endif // ENABLE_DC
5147
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005148//
5149// Execution engine
5150//
5151
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005152#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5153#define BC_PROG_NUM(r, n) \
5154 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5155
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005156#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5157#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5158
Denys Vlasenko53799502019-01-25 14:24:03 +01005159static size_t xc_program_index(char *code, size_t *bgn)
5160{
5161 unsigned char *bytes = (void*)(code + *bgn);
5162 unsigned amt;
5163 unsigned i;
5164 size_t res;
5165
5166 amt = *bytes++;
5167 if (amt < SMALL_INDEX_LIMIT) {
5168 *bgn += 1;
5169 return amt;
5170 }
5171 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
5172 *bgn += amt + 1;
5173
5174 res = 0;
5175 i = 0;
5176 do {
5177 res |= (size_t)(*bytes++) << i;
5178 i += 8;
5179 } while (--amt != 0);
5180
5181 return res;
5182}
5183
5184static char *xc_program_name(char *code, size_t *bgn)
5185{
5186 code += *bgn;
5187 *bgn += strlen(code) + 1;
5188
5189 return xstrdup(code);
5190}
5191
5192static BcVec* xc_program_dereference(BcVec *vec)
5193{
5194 BcVec *v;
5195 size_t vidx, nidx, i = 0;
5196
5197 //assert(vec->size == sizeof(uint8_t));
5198
5199 vidx = xc_program_index(vec->v, &i);
5200 nidx = xc_program_index(vec->v, &i);
5201
5202 v = bc_vec_item(&G.prog.arrs, vidx);
5203 v = bc_vec_item(v, nidx);
5204
5205 //assert(v->size != sizeof(uint8_t));
5206
5207 return v;
5208}
5209
5210static BcVec* xc_program_search(char *id, BcType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06005211{
Gavin Howard01055ba2018-11-03 11:00:21 -06005212 BcId e, *ptr;
5213 BcVec *v, *map;
5214 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005215 int new;
Denys Vlasenko53799502019-01-25 14:24:03 +01005216 bool var = (type == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005218 v = var ? &G.prog.vars : &G.prog.arrs;
5219 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005220
5221 e.name = id;
5222 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005223 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005224
5225 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005226 BcVec v2;
5227 bc_array_init(&v2, var);
5228 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005229 }
5230
5231 ptr = bc_vec_item(map, i);
5232 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005233 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005234}
5235
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005236// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005237static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005238{
Gavin Howard01055ba2018-11-03 11:00:21 -06005239 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005240 case XC_RESULT_STR:
5241 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005242 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005243 case XC_RESULT_IBASE:
5244 case XC_RESULT_SCALE:
5245 case XC_RESULT_OBASE:
5246 *num = &r->d.n;
5247 break;
5248 case XC_RESULT_CONSTANT: {
5249 BcStatus s;
5250 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005251 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005252
Denys Vlasenko10bde142018-12-27 18:23:58 +01005253 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005254 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005255
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005256 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005257
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005258 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005259 if (s) {
5260 bc_num_free(&r->d.n);
5261 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005262 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005263 *num = &r->d.n;
5264 r->t = XC_RESULT_TEMP;
5265 break;
5266 }
5267 case XC_RESULT_VAR:
5268 case XC_RESULT_ARRAY:
5269 case XC_RESULT_ARRAY_ELEM: {
Denys Vlasenko53799502019-01-25 14:24:03 +01005270 BcType type = (r->t == XC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY;
5271 BcVec *v = xc_program_search(r->d.id.name, type);
5272 void *p = bc_vec_top(v);
5273
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005274 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko53799502019-01-25 14:24:03 +01005275 size_t idx = r->d.id.idx;
5276
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005277 v = p;
Denys Vlasenko53799502019-01-25 14:24:03 +01005278 if (v->size == sizeof(uint8_t))
5279 v = xc_program_dereference(v);
5280 //assert(v->size == sizeof(BcNum));
5281 if (v->len <= idx)
5282 bc_array_expand(v, idx + 1);
5283 *num = bc_vec_item(v, idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005284 } else {
5285 *num = p;
5286 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005287 break;
5288 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005289#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005290 case BC_RESULT_LAST:
5291 *num = &G.prog.last;
5292 break;
5293 case BC_RESULT_ONE:
5294 *num = &G.prog.one;
5295 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005296#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005297#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005298 default:
5299 // Testing the theory that dc does not reach LAST/ONE
5300 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005301#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005302 }
5303
Denys Vlasenko29301232018-12-11 15:29:32 +01005304 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005305}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005306#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005307
Denys Vlasenko10bde142018-12-27 18:23:58 +01005308static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005309 BcResult **r, BcNum **rn, bool assign)
5310{
5311 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005312 BcResultType lt, rt;
5313
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005314 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005315 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005316
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005317 *r = bc_vec_item_rev(&G.prog.results, 0);
5318 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005319
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005320 s = zxc_program_num(*l, ln);
5321 if (s) RETURN_STATUS(s);
5322 s = zxc_program_num(*r, rn);
5323 if (s) RETURN_STATUS(s);
5324
Gavin Howard01055ba2018-11-03 11:00:21 -06005325 lt = (*l)->t;
5326 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005327
5328 // We run this again under these conditions in case any vector has been
5329 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005330 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005331 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005332 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005333 }
5334
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005335 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005336 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005337 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005338 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005339
Denys Vlasenko29301232018-12-11 15:29:32 +01005340 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005341}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005342#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005343
Denys Vlasenko10bde142018-12-27 18:23:58 +01005344static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005345{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005346 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005347 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005348 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005349}
5350
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005351// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005352static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005353{
5354 BcStatus s;
5355
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005356 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005357 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005358 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005359
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005360 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005361 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005362
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005363 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005364 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005365
Denys Vlasenko29301232018-12-11 15:29:32 +01005366 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005367}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005368#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005369
Denys Vlasenko10bde142018-12-27 18:23:58 +01005370static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005371{
5372 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005373 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005374}
5375
Denys Vlasenko10bde142018-12-27 18:23:58 +01005376static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005377{
5378 BcStatus s;
5379 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005380 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005381
Denys Vlasenko10bde142018-12-27 18:23:58 +01005382 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005383 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005384 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005385
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005386 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005387 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 -06005388 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005389 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005390
Denys Vlasenko259137d2018-12-11 19:42:05 +01005391 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005392 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005393 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005394 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005395}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005396#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005397
Denys Vlasenko10bde142018-12-27 18:23:58 +01005398static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005399{
5400 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005401 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005402 BcVec buf;
5403 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005404 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005405
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005406 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005407 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005408
Denys Vlasenko10bde142018-12-27 18:23:58 +01005409 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005410 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005411
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005412 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005413 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005414 //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 +01005415
Denys Vlasenko10bde142018-12-27 18:23:58 +01005416 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005417 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005418 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005419 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005420 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005421 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005422 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005423 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005424 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005425 s = bc_error_at("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005426 goto exec_err;
5427 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005428 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005429
5430 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005431 ip.inst_idx = 0;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005432 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005433
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005434 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005435 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005436 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005437 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005438 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005439}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005440#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005441
Denys Vlasenko10bde142018-12-27 18:23:58 +01005442static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005443{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005444#if ENABLE_DC
Denys Vlasenko266bec82019-01-02 05:03:53 +01005445 if (!str[0] && IS_DC) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005446 // Example: echo '[]ap' | dc
5447 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005448 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005449 return;
5450 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005451#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005452 while (*str) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005453 char c = *str++;
5454 if (c == '\\') {
5455 static const char esc[] ALIGN1 = "nabfrt""e\\";
5456 char *n;
5457
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005458 c = *str++;
Brian Foley10509a72019-09-05 10:53:21 +02005459 n = strchr(esc, c); // note: if c is NUL, n = \0 at end of esc
5460 if (!n || !c) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005461 // Just print the backslash and following character
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005462 bb_putchar('\\');
5463 ++G.prog.nchars;
Brian Foley10509a72019-09-05 10:53:21 +02005464 // But if we're at the end of the string, stop
5465 if (!c) break;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005466 } else {
5467 if (n - esc == 0) // "\n" ?
5468 G.prog.nchars = SIZE_MAX;
5469 c = "\n\a\b\f\r\t""\\\\""\\"[n - esc];
5470 // n a b f r t e \ \<end of line>
Gavin Howard01055ba2018-11-03 11:00:21 -06005471 }
5472 }
Denys Vlasenko266bec82019-01-02 05:03:53 +01005473 putchar(c);
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005474 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005475 }
5476}
5477
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005478static void bc_num_printNewline(void)
5479{
5480 if (G.prog.nchars == G.prog.len - 1) {
5481 bb_putchar('\\');
5482 bb_putchar('\n');
5483 G.prog.nchars = 0;
5484 }
5485}
5486
5487#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005488static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005489{
5490 (void) radix;
5491 bb_putchar((char) num);
5492 G.prog.nchars += width;
5493}
5494#endif
5495
5496static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5497{
5498 size_t exp, pow;
5499
5500 bc_num_printNewline();
5501 bb_putchar(radix ? '.' : ' ');
5502 ++G.prog.nchars;
5503
5504 bc_num_printNewline();
5505 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5506 continue;
5507
5508 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5509 size_t dig;
5510 bc_num_printNewline();
5511 dig = num / pow;
5512 num -= dig * pow;
5513 bb_putchar(((char) dig) + '0');
5514 }
5515}
5516
5517static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5518{
5519 if (radix) {
5520 bc_num_printNewline();
5521 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005522 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005523 }
5524
5525 bc_num_printNewline();
5526 bb_putchar(bb_hexdigits_upcase[num]);
5527 G.prog.nchars += width;
5528}
5529
5530static void bc_num_printDecimal(BcNum *n)
5531{
5532 size_t i, rdx = n->rdx - 1;
5533
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005534 if (n->neg) {
5535 bb_putchar('-');
5536 G.prog.nchars++;
5537 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005538
5539 for (i = n->len - 1; i < n->len; --i)
5540 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5541}
5542
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005543typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5544
Denys Vlasenko10bde142018-12-27 18:23:58 +01005545static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005546{
5547 BcStatus s;
5548 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005549 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005550 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005551 BcNum intp, fracp, digit, frac_len;
5552 unsigned long dig, *ptr;
5553 size_t i;
5554 bool radix;
5555
5556 if (n->len == 0) {
5557 print(0, width, false);
5558 RETURN_STATUS(BC_STATUS_SUCCESS);
5559 }
5560
5561 bc_vec_init(&stack, sizeof(long), NULL);
5562 bc_num_init(&intp, n->len);
5563 bc_num_init(&fracp, n->rdx);
5564 bc_num_init(&digit, width);
5565 bc_num_init(&frac_len, BC_NUM_INT(n));
5566 bc_num_copy(&intp, n);
5567 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005568 base.cap = ARRAY_SIZE(base_digs);
5569 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005570 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005571
5572 bc_num_truncate(&intp, intp.rdx);
5573 s = zbc_num_sub(n, &intp, &fracp, 0);
5574 if (s) goto err;
5575
5576 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005577 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005578 if (s) goto err;
5579 s = zbc_num_ulong(&digit, &dig);
5580 if (s) goto err;
5581 bc_vec_push(&stack, &dig);
5582 }
5583
5584 for (i = 0; i < stack.len; ++i) {
5585 ptr = bc_vec_item_rev(&stack, i);
5586 print(*ptr, width, false);
5587 }
5588
5589 if (!n->rdx) goto err;
5590
5591 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005592 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005593 if (s) goto err;
5594 s = zbc_num_ulong(&fracp, &dig);
5595 if (s) goto err;
5596 bc_num_ulong2num(&intp, dig);
5597 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5598 if (s) goto err;
5599 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005600 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005601 if (s) goto err;
5602 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005603 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005604 bc_num_free(&frac_len);
5605 bc_num_free(&digit);
5606 bc_num_free(&fracp);
5607 bc_num_free(&intp);
5608 bc_vec_free(&stack);
5609 RETURN_STATUS(s);
5610}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005611#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005612
Denys Vlasenko10bde142018-12-27 18:23:58 +01005613static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005614{
5615 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005616 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005617 BcNumDigitOp print;
5618 bool neg = n->neg;
5619
5620 if (neg) {
5621 bb_putchar('-');
5622 G.prog.nchars++;
5623 }
5624
5625 n->neg = false;
5626
Denys Vlasenko680ccd32018-12-31 19:42:13 +01005627 if (G.prog.ob_t <= 16) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005628 width = 1;
5629 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005630 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005631 unsigned i = G.prog.ob_t - 1;
5632 width = 0;
5633 for (;;) {
5634 width++;
5635 i /= 10;
5636 if (i == 0)
5637 break;
5638 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005639 print = bc_num_printDigits;
5640 }
5641
Denys Vlasenko10bde142018-12-27 18:23:58 +01005642 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005643 n->neg = neg;
5644
5645 RETURN_STATUS(s);
5646}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005647#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005648
Denys Vlasenko10bde142018-12-27 18:23:58 +01005649static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005650{
5651 BcStatus s = BC_STATUS_SUCCESS;
5652
5653 bc_num_printNewline();
5654
5655 if (n->len == 0) {
5656 bb_putchar('0');
5657 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005658 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005659 bc_num_printDecimal(n);
5660 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005661 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005662
5663 if (newline) {
5664 bb_putchar('\n');
5665 G.prog.nchars = 0;
5666 }
5667
5668 RETURN_STATUS(s);
5669}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005670#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005671
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005672#if !ENABLE_DC
5673// for bc, idx is always 0
5674#define xc_program_print(inst, idx) \
5675 xc_program_print(inst)
5676#endif
5677static BC_STATUS xc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005678{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005679 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005680 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005681 BcNum *num;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005682 IF_NOT_DC(size_t idx = 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005683
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005684 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005685 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005686
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005687 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005688#if ENABLE_BC
5689 if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID)
5690 // void function's result on stack, ignore
5691 RETURN_STATUS(BC_STATUS_SUCCESS);
5692#endif
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005693 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005694 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005695
5696 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005697 s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005698#if ENABLE_BC
5699 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5700#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005701 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005702 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005703
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005704 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005705 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005706
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005707 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005708 char *nl;
5709 G.prog.nchars += printf("%s", str);
5710 nl = strrchr(str, '\n');
5711 if (nl)
5712 G.prog.nchars = strlen(nl + 1);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005713 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005714 xc_program_printString(str);
Denys Vlasenko266bec82019-01-02 05:03:53 +01005715 if (inst == XC_INST_PRINT)
5716 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005717 }
5718 }
5719
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005720 if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005721
Denys Vlasenko29301232018-12-11 15:29:32 +01005722 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005723}
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005724#define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005725
Denys Vlasenko10bde142018-12-27 18:23:58 +01005726static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005727{
5728 BcStatus s;
5729 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005730 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005731
Denys Vlasenko10bde142018-12-27 18:23:58 +01005732 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005733 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005734
5735 bc_num_init(&res.d.n, num->len);
5736 bc_num_copy(&res.d.n, num);
5737 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5738
Denys Vlasenko10bde142018-12-27 18:23:58 +01005739 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
Denys Vlasenko29301232018-12-11 15:29:32 +01005741 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005742}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005743#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005744
Denys Vlasenko10bde142018-12-27 18:23:58 +01005745static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005746{
5747 BcStatus s;
5748 BcResult *opd1, *opd2, res;
5749 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005750 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005751
Denys Vlasenko10bde142018-12-27 18:23:58 +01005752 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005753 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005754
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005755 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005756
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005757 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005758 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005759 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005760 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005762 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005763 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005764 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005765 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005766 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005767 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005768 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005769 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005770 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005771 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005772 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005773 case XC_INST_REL_LT:
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_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005777 cond = (cond > 0);
5778 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005779 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005780 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005781 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005782 }
5783 }
5784
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005785 if (cond) bc_num_one(&res.d.n);
5786 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005787
Denys Vlasenko10bde142018-12-27 18:23:58 +01005788 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005789
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005790 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005791}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005792#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005793
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005794#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005795static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005796{
5797 BcNum n2;
5798 BcResult res;
5799
5800 memset(&n2, 0, sizeof(BcNum));
5801 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005802 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005803
5804 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005805 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005806 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005807 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005808 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005809 }
5810
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005811 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005812 bc_vec_push(v, &n2);
5813
Denys Vlasenko29301232018-12-11 15:29:32 +01005814 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005815}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005816#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005817#endif // ENABLE_DC
5818
Denys Vlasenko53799502019-01-25 14:24:03 +01005819static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, BcType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005820{
5821 BcStatus s;
5822 BcResult *ptr, r;
Denys Vlasenko53799502019-01-25 14:24:03 +01005823 BcVec *vec;
Gavin Howard01055ba2018-11-03 11:00:21 -06005824 BcNum *n;
Denys Vlasenko53799502019-01-25 14:24:03 +01005825 bool var = (t == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005826
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005827 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005828 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005829
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005830 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko53799502019-01-25 14:24:03 +01005831 if ((ptr->t == XC_RESULT_ARRAY) == var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005832 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005833 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005834
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005835#if ENABLE_DC
Denys Vlasenko53799502019-01-25 14:24:03 +01005836 if (ptr->t == XC_RESULT_STR) {
5837 if (!var)
5838 RETURN_STATUS(bc_error_variable_is_wrong_type());
5839 RETURN_STATUS(zdc_program_assignStr(ptr, vec, true));
5840 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005841#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005842
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005843 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005844 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005845
5846 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko53799502019-01-25 14:24:03 +01005847 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848
5849 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005850 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005851 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005852 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01005853 BcVec *v = (BcVec*) n;
5854 bool ref, ref_size;
Gavin Howard01055ba2018-11-03 11:00:21 -06005855
Denys Vlasenko53799502019-01-25 14:24:03 +01005856 ref = (v->size == sizeof(BcVec) && t != BC_TYPE_ARRAY);
5857 ref_size = (v->size == sizeof(uint8_t));
5858
5859 if (ref || (ref_size && t == BC_TYPE_REF)) {
5860 bc_vec_init(&r.d.v, sizeof(uint8_t), NULL);
5861 if (ref) {
5862 size_t vidx, idx;
5863 BcId id;
5864
5865 id.name = ptr->d.id.name;
5866 v = xc_program_search(ptr->d.id.name, BC_TYPE_REF);
5867
5868 // Make sure the pointer was not invalidated.
5869 vec = xc_program_search(name, t);
5870
5871 vidx = bc_map_find_exact(&G.prog.arr_map, &id);
5872 //assert(vidx != BC_VEC_INVALID_IDX);
5873 vidx = ((BcId*) bc_vec_item(&G.prog.arr_map, vidx))->idx;
5874 idx = v->len - 1;
5875
5876 bc_vec_pushIndex(&r.d.v, vidx);
5877 bc_vec_pushIndex(&r.d.v, idx);
5878 }
5879 // If we get here, we are copying a ref to a ref.
5880 else bc_vec_npush(&r.d.v, v->len, v->v);
5881
5882 // We need to return early.
5883 goto ret;
5884 }
5885
5886 if (ref_size && t != BC_TYPE_REF)
5887 v = xc_program_dereference(v);
5888
5889 bc_array_init(&r.d.v, true);
5890 bc_array_copy(&r.d.v, v);
5891 }
5892 ret:
5893 bc_vec_push(vec, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005894 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005895
Denys Vlasenko29301232018-12-11 15:29:32 +01005896 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897}
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005898#define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005899
Denys Vlasenko10bde142018-12-27 18:23:58 +01005900static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005901{
5902 BcStatus s;
5903 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005904 BcNum *l, *r;
5905 bool assign = (inst == XC_INST_ASSIGN);
5906 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005907
Denys Vlasenko10bde142018-12-27 18:23:58 +01005908 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005909 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005910
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005911 ib = left->t == XC_RESULT_IBASE;
5912 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005913
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005914#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005915 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005916 BcVec *v;
5917
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005918 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005919 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005920 v = xc_program_search(left->d.id.name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005921
Denys Vlasenkofa210792018-12-19 19:35:40 +01005922 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005923 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005924#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005925
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005926 if (left->t == XC_RESULT_CONSTANT
5927 || left->t == XC_RESULT_TEMP
5928 IF_BC(|| left->t == BC_RESULT_VOID)
5929 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005930 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005931 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005932
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005933#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005934 if (assign)
5935 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005936 else {
5937 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005938 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 +01005939 }
5940 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005941#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005942 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005943#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005944
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005945 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005946 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005947 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5948 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5949 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005950 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005951 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005952 size_t max;
5953 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005954
Denys Vlasenko29301232018-12-11 15:29:32 +01005955 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005956 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005957 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005958 if (sc) {
5959 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005960 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005961 } else {
5962 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005963 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005965 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005966 }
5967
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005968 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005969 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
5971 *ptr = (size_t) val;
5972 s = BC_STATUS_SUCCESS;
5973 }
5974
5975 bc_num_init(&res.d.n, l->len);
5976 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005977 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005978
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005979 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005980}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005981#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenko416ce762018-12-02 20:57:17 +01005983#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005984#define xc_program_pushVar(code, bgn, pop, copy) \
5985 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005986// for bc, 'pop' and 'copy' are always false
5987#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005988static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005989 bool pop, bool copy)
5990{
Gavin Howard01055ba2018-11-03 11:00:21 -06005991 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005992 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005993
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005994 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 r.d.id.name = name;
5996
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005997#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005998 if (pop || copy) {
Denys Vlasenko53799502019-01-25 14:24:03 +01005999 BcVec *v = xc_program_search(name, BC_TYPE_VAR);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006000 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006001
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006002 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006003 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006004 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006006
6007 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006008 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006009 bc_num_init_DEF_SIZE(&r.d.n);
6010 bc_num_copy(&r.d.n, num);
6011 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006012 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006013 r.d.id.idx = num->rdx;
6014 }
6015
6016 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006017 }
6018#endif // ENABLE_DC
6019
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006020 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006022 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006023}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006024#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006025
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006026static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006027{
6028 BcStatus s = BC_STATUS_SUCCESS;
6029 BcResult r;
6030 BcNum *num;
6031
Denys Vlasenko10bde142018-12-27 18:23:58 +01006032 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006033
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006034 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006035 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006036 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006037 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06006038 BcResult *operand;
6039 unsigned long temp;
6040
Denys Vlasenko10bde142018-12-27 18:23:58 +01006041 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006042 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006043 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044 if (s) goto err;
6045
6046 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006047 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006048 goto err;
6049 }
6050
6051 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006052 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006053 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006054 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006055 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006056 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006057}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006058#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006059
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006060#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006061static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006062{
6063 BcStatus s;
6064 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006065 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006066 char inst2 = inst;
6067
Denys Vlasenko10bde142018-12-27 18:23:58 +01006068 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006069 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006070
6071 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006072 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006073 bc_num_init(&copy.d.n, num->len);
6074 bc_num_copy(&copy.d.n, num);
6075 }
6076
6077 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006078 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
6079 ? BC_INST_ASSIGN_PLUS
6080 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006081
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006082 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006083 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006084 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006085
6086 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006087 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006088 }
6089
Denys Vlasenko29301232018-12-11 15:29:32 +01006090 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006091}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006092#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006093
Denys Vlasenko29301232018-12-11 15:29:32 +01006094static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006095{
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006097 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006098 BcId *a;
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006099 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006100
Denys Vlasenko10bde142018-12-27 18:23:58 +01006101 nparams = xc_program_index(code, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006102 ip.func = xc_program_index(code, idx);
6103 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006104
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006105 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006106 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006107 }
6108 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006109 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006110 }
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006111 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006112
6113 for (i = 0; i < nparams; ++i) {
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006114 BcResult *arg;
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006115 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01006116 bool arr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006117
6118 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006119 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006120
Denys Vlasenko53799502019-01-25 14:24:03 +01006121 arr = (a->idx == BC_TYPE_ARRAY || a->idx == BC_TYPE_REF);
6122
6123 if (arr != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006124 // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions)
6125 ) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006126 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006127 }
Denys Vlasenko53799502019-01-25 14:24:03 +01006128 s = zxc_program_popResultAndCopyToVar(a->name, (BcType) a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006129 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006130 }
6131
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006132 a = bc_vec_item(&func->autos, i);
6133 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006134 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006135
Denys Vlasenko53799502019-01-25 14:24:03 +01006136 v = xc_program_search(a->name, (BcType) a->idx);
6137 if (a->idx == BC_TYPE_VAR) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006138 BcNum n2;
6139 bc_num_init_DEF_SIZE(&n2);
6140 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006141 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01006142 //assert(a->idx == BC_TYPE_ARRAY);
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006143 BcVec v2;
6144 bc_array_init(&v2, true);
6145 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006146 }
6147 }
6148
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006149 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
Denys Vlasenko29301232018-12-11 15:29:32 +01006151 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006152}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006153#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006154
Denys Vlasenko29301232018-12-11 15:29:32 +01006155static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006156{
Gavin Howard01055ba2018-11-03 11:00:21 -06006157 BcResult res;
6158 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006159 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006161 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006162
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006163 f = xc_program_func(ip->func);
6164
6165 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006166 if (inst == XC_INST_RET) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006167 // bc needs this for e.g. RESULT_CONSTANT ("return 5")
6168 // because bc constants are per-function.
6169 // TODO: maybe avoid if value is already RESULT_TEMP?
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006170 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006171 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006172 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006173
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006174 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006175 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006176 bc_num_init(&res.d.n, num->len);
6177 bc_num_copy(&res.d.n, num);
Denys Vlasenko377cc972019-01-04 00:34:52 +01006178 bc_vec_pop(&G.prog.results);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006179 } else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006180 if (f->voidfunc)
6181 res.t = BC_RESULT_VOID;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006182 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006183 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006184 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006185 bc_vec_push(&G.prog.results, &res);
6186
6187 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006188
6189 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006190 a = (void*)f->autos.v;
6191 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006192 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006193 v = xc_program_search(a->name, (BcType) a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006194 bc_vec_pop(v);
6195 }
6196
Denys Vlasenko29301232018-12-11 15:29:32 +01006197 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006198}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006199#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006200#endif // ENABLE_BC
6201
Denys Vlasenko10bde142018-12-27 18:23:58 +01006202static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006203{
6204 return (unsigned long) n->rdx;
6205}
6206
Denys Vlasenko10bde142018-12-27 18:23:58 +01006207static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006208{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006209 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006210
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006211 if (n->rdx != len) return len;
6212 for (;;) {
6213 if (len == 0) break;
6214 len--;
6215 if (n->num[len] != 0) break;
6216 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006217 return len;
6218}
6219
Denys Vlasenko10bde142018-12-27 18:23:58 +01006220static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006221{
6222 BcStatus s;
6223 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006224 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006225 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006226 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006227
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006228 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006229 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006230 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006231
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006232 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006233 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006234
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006235#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006236 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006237 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006238#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006239
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006240 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006241
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006242 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006243 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006244#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006245 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006246 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006247 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006248#endif
6249#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006250 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006251 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006252 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006253
Denys Vlasenko10bde142018-12-27 18:23:58 +01006254 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006255 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006256 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006257#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006258 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006259 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006260 }
6261
Denys Vlasenko10bde142018-12-27 18:23:58 +01006262 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006264 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006266#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006267
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006268#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006269static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006270{
6271 BcStatus s;
6272 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006273 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006274
Denys Vlasenko10bde142018-12-27 18:23:58 +01006275 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006276 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006278 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279 bc_num_init(&res2.d.n, n2->len);
6280
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006281 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006282 if (s) goto err;
6283
Denys Vlasenko10bde142018-12-27 18:23:58 +01006284 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006285 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006286 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006287
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006288 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006289 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006290 bc_num_free(&res2.d.n);
6291 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006292 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006293}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006294#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006295
Denys Vlasenkofa210792018-12-19 19:35:40 +01006296static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006297{
6298 BcStatus s;
6299 BcResult *r1, *r2, *r3, res;
6300 BcNum *n1, *n2, *n3;
6301
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006302 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006303 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006304 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006305 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006306
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006307 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006308 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006309 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006310 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006311 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006312
6313 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006314 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006315 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006316 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006317 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006319 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006320 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006321 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006322 }
6323 }
6324
6325 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006326 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327 if (s) goto err;
6328
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006329 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006330 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006332 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006333 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006334 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006335 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006337#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006338
Denys Vlasenkofa210792018-12-19 19:35:40 +01006339static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006340{
Gavin Howard01055ba2018-11-03 11:00:21 -06006341 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006342 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006343
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006344 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006345
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006346 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006347 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006348 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006349}
6350
Denys Vlasenkofa210792018-12-19 19:35:40 +01006351static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006352{
6353 BcStatus s;
6354 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006355 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006356 char **strs;
6357 char *str;
6358 char c;
6359 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006360
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006361 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006362 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006364 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006365 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006366 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006367
6368 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006369 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006370 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006371 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006372
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006373 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006374 bc_num_copy(&n, num);
6375 bc_num_truncate(&n, n.rdx);
6376
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006377 strmb.cap = ARRAY_SIZE(strmb_digs);
6378 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006379 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006380
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006381 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006382 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006383 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006384 if (s) goto num_err;
6385
6386 c = (char) val;
6387
6388 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006389 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006390 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006391 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006392 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006393 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006394 }
6395
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006396 strs = (void*)G.prog.strs.v;
6397 for (idx = 0; idx < G.prog.strs.len; idx++) {
6398 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6399 goto dup;
6400 }
6401 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006402 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006404 //str[1] = '\0'; - already is
Brian Foleyb64470b2019-09-05 10:50:13 +02006405 idx = bc_vec_push(&G.prog.strs, &str);
6406 // Add an empty function so that if zdc_program_execStr ever needs to
6407 // parse the string into code (from the 'x' command) there's somewhere
6408 // to store the bytecode.
6409 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006410 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006411 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006412 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006413 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006414
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006415 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006416 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006417 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006418 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006419}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006420#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006421
Denys Vlasenkofa210792018-12-19 19:35:40 +01006422static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006423{
6424 BcStatus s;
6425 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006426 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006427 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006428
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006429 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006430 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006431 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006432
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006433 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006434 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006435
Denys Vlasenko57734c92018-12-17 21:14:05 +01006436 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006437 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006438 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006439 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006440 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006441 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006442 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 }
6444
Denys Vlasenko29301232018-12-11 15:29:32 +01006445 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006446}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006447#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006448
Denys Vlasenkofa210792018-12-19 19:35:40 +01006449static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006450{
6451 BcStatus s;
6452 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006453 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006454 unsigned long val;
6455
Denys Vlasenko10bde142018-12-27 18:23:58 +01006456 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006457 if (s) RETURN_STATUS(s);
6458 s = zbc_num_ulong(num, &val);
6459 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006460
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006461 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006462
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006463 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006464 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006465 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006466 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006467 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006468
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006469 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006470
Denys Vlasenko29301232018-12-11 15:29:32 +01006471 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006472}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006473#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006474
Denys Vlasenkofa210792018-12-19 19:35:40 +01006475static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006476{
6477 BcStatus s = BC_STATUS_SUCCESS;
6478 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006479 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 BcInstPtr ip;
6481 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006482
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006483 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006484 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006485
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006486 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006487
6488 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006489 BcNum *n = n; // for compiler
6490 bool exec;
6491 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006492 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006493 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006494
Denys Vlasenkof706a182018-12-26 20:02:09 +01006495 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006496 (*bgn) += 1;
6497 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006498 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006499
6500 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006501 name = then_name;
6502 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 exec = true;
6504 name = else_name;
6505 }
6506
6507 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006508 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006509 v = xc_program_search(name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06006510 n = bc_vec_top(v);
6511 }
6512
6513 free(then_name);
6514 free(else_name);
6515
6516 if (!exec) goto exit;
6517 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006518 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 goto exit;
6520 }
6521
6522 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006523 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006524 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006525 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006526 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006527 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006528 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006529 if (s || !BC_PROG_STR(n)) goto exit;
6530 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006531 } else
Brian Foley74548792019-09-05 10:46:22 +02006532 goto exit_nopop;
Gavin Howard01055ba2018-11-03 11:00:21 -06006533 }
6534
6535 fidx = sidx + BC_PROG_REQ_FUNCS;
6536
Denys Vlasenko10bde142018-12-27 18:23:58 +01006537 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006538
6539 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006540 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006541 char *str;
6542
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006543 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006544 xc_parse_create(fidx);
6545 str = *xc_program_str(sidx);
6546 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006547 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006548
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006549 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006550 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006551 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006552 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006553 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006554 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006555 G.prs = sv_parse; // struct copy
6556 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006557 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006558 bc_vec_pop_all(&f->code);
6559 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006560 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006561 }
6562
Denys Vlasenko24e41942018-12-21 23:01:26 +01006563 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006564 ip.func = fidx;
6565
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006566 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006567 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006568
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006569 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006570 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006571 bc_vec_pop(&G.prog.results);
Brian Foley74548792019-09-05 10:46:22 +02006572 exit_nopop:
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006573 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006574}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006575#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006576#endif // ENABLE_DC
6577
Denys Vlasenko10bde142018-12-27 18:23:58 +01006578static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006579{
Gavin Howard01055ba2018-11-03 11:00:21 -06006580 BcResult res;
6581 unsigned long val;
6582
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006583 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006584 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006585 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006586 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006587 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006588 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006589 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006590
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006591 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006592 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006593 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006594}
6595
Denys Vlasenko10bde142018-12-27 18:23:58 +01006596static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006597{
Gavin Howard01055ba2018-11-03 11:00:21 -06006598 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006599 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006600 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006601 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006602
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006603 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006604 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6605 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006606 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006607 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006608
Denys Vlasenko24e41942018-12-21 23:01:26 +01006609 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 -06006610 switch (inst) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006611 case XC_INST_RET:
6612 if (IS_DC) { // end of '?' reached
6613 bc_vec_pop(&G.prog.exestack);
6614 goto read_updated_ip;
6615 }
6616 // bc: fall through
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006617#if ENABLE_BC
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006618 case BC_INST_RET0:
6619 dbg_exec("BC_INST_RET[0]:");
6620 s = zbc_program_return(inst);
6621 goto read_updated_ip;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006622 case BC_INST_JUMP_ZERO: {
6623 BcNum *num;
6624 bool zero;
6625 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006626 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006627 if (s) RETURN_STATUS(s);
6628 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6629 bc_vec_pop(&G.prog.results);
6630 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006631 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006632 break;
6633 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006634 // else: fall through
6635 }
6636 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006637 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006638 size_t *addr = bc_vec_item(&func->labels, idx);
6639 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6640 ip->inst_idx = *addr;
6641 break;
6642 }
6643 case BC_INST_CALL:
6644 dbg_exec("BC_INST_CALL:");
6645 s = zbc_program_call(code, &ip->inst_idx);
6646 goto read_updated_ip;
6647 case BC_INST_INC_PRE:
6648 case BC_INST_DEC_PRE:
6649 case BC_INST_INC_POST:
6650 case BC_INST_DEC_POST:
6651 dbg_exec("BC_INST_INCDEC:");
6652 s = zbc_program_incdec(inst);
6653 break;
6654 case BC_INST_HALT:
6655 dbg_exec("BC_INST_HALT:");
6656 QUIT_OR_RETURN_TO_MAIN;
6657 break;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006658 case XC_INST_BOOL_OR:
6659 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006660#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006661 case XC_INST_REL_EQ:
6662 case XC_INST_REL_LE:
6663 case XC_INST_REL_GE:
6664 case XC_INST_REL_NE:
6665 case XC_INST_REL_LT:
6666 case XC_INST_REL_GT:
6667 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006668 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006669 break;
6670 case XC_INST_READ:
6671 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006672 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006673 goto read_updated_ip;
6674 case XC_INST_VAR:
6675 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006676 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006677 break;
6678 case XC_INST_ARRAY_ELEM:
6679 case XC_INST_ARRAY:
6680 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6681 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6682 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006683#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006684 case BC_INST_LAST:
6685 dbg_exec("BC_INST_LAST:");
6686 r.t = BC_RESULT_LAST;
6687 bc_vec_push(&G.prog.results, &r);
6688 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006689#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006690 case XC_INST_IBASE:
6691 case XC_INST_OBASE:
6692 case XC_INST_SCALE:
6693 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006694 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006695 break;
6696 case XC_INST_SCALE_FUNC:
6697 case XC_INST_LENGTH:
6698 case XC_INST_SQRT:
6699 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006700 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006701 break;
6702 case XC_INST_NUM:
6703 dbg_exec("XC_INST_NUM:");
6704 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006705 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006706 bc_vec_push(&G.prog.results, &r);
6707 break;
6708 case XC_INST_POP:
6709 dbg_exec("XC_INST_POP:");
6710 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6711 s = bc_error_stack_has_too_few_elements();
6712 else
6713 bc_vec_pop(&G.prog.results);
6714 break;
6715 case XC_INST_PRINT:
6716 case XC_INST_PRINT_POP:
6717 case XC_INST_PRINT_STR:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006718 dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006719 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006720 break;
6721 case XC_INST_STR:
6722 dbg_exec("XC_INST_STR:");
6723 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006724 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006725 bc_vec_push(&G.prog.results, &r);
6726 break;
6727 case XC_INST_POWER:
6728 case XC_INST_MULTIPLY:
6729 case XC_INST_DIVIDE:
6730 case XC_INST_MODULUS:
6731 case XC_INST_PLUS:
6732 case XC_INST_MINUS:
6733 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006734 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006735 break;
6736 case XC_INST_BOOL_NOT: {
6737 BcNum *num;
6738 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006739 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006740 if (s) RETURN_STATUS(s);
6741 bc_num_init_DEF_SIZE(&r.d.n);
6742 if (bc_num_cmp(num, &G.prog.zero) == 0)
6743 bc_num_one(&r.d.n);
6744 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006745 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006746 break;
6747 }
6748 case XC_INST_NEG:
6749 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006750 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006751 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006752#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006753 case BC_INST_ASSIGN_POWER:
6754 case BC_INST_ASSIGN_MULTIPLY:
6755 case BC_INST_ASSIGN_DIVIDE:
6756 case BC_INST_ASSIGN_MODULUS:
6757 case BC_INST_ASSIGN_PLUS:
6758 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006759#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006760 case XC_INST_ASSIGN:
6761 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006762 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006763 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006764#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006765 case DC_INST_POP_EXEC:
6766 dbg_exec("DC_INST_POP_EXEC:");
6767 bc_vec_pop(&G.prog.exestack);
6768 goto read_updated_ip;
6769 case DC_INST_MODEXP:
6770 dbg_exec("DC_INST_MODEXP:");
6771 s = zdc_program_modexp();
6772 break;
6773 case DC_INST_DIVMOD:
6774 dbg_exec("DC_INST_DIVMOD:");
6775 s = zdc_program_divmod();
6776 break;
6777 case DC_INST_EXECUTE:
6778 case DC_INST_EXEC_COND:
6779 dbg_exec("DC_INST_EXEC[_COND]:");
6780 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6781 goto read_updated_ip;
6782 case DC_INST_PRINT_STACK: {
6783 size_t idx;
6784 dbg_exec("DC_INST_PRINT_STACK:");
6785 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006786 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006787 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006788 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006789 break;
6790 }
6791 case DC_INST_CLEAR_STACK:
6792 dbg_exec("DC_INST_CLEAR_STACK:");
6793 bc_vec_pop_all(&G.prog.results);
6794 break;
6795 case DC_INST_STACK_LEN:
6796 dbg_exec("DC_INST_STACK_LEN:");
6797 dc_program_stackLen();
6798 break;
6799 case DC_INST_DUPLICATE:
6800 dbg_exec("DC_INST_DUPLICATE:");
6801 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6802 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6803 ptr = bc_vec_top(&G.prog.results);
6804 dc_result_copy(&r, ptr);
6805 bc_vec_push(&G.prog.results, &r);
6806 break;
6807 case DC_INST_SWAP: {
6808 BcResult *ptr2;
6809 dbg_exec("DC_INST_SWAP:");
6810 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6811 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6812 ptr = bc_vec_item_rev(&G.prog.results, 0);
6813 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6814 memcpy(&r, ptr, sizeof(BcResult));
6815 memcpy(ptr, ptr2, sizeof(BcResult));
6816 memcpy(ptr2, &r, sizeof(BcResult));
6817 break;
6818 }
6819 case DC_INST_ASCIIFY:
6820 dbg_exec("DC_INST_ASCIIFY:");
6821 s = zdc_program_asciify();
6822 break;
6823 case DC_INST_PRINT_STREAM:
6824 dbg_exec("DC_INST_PRINT_STREAM:");
6825 s = zdc_program_printStream();
6826 break;
6827 case DC_INST_LOAD:
6828 case DC_INST_PUSH_VAR: {
6829 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006830 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006831 break;
6832 }
6833 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006834 char *name = xc_program_name(code, &ip->inst_idx);
Denys Vlasenko53799502019-01-25 14:24:03 +01006835 s = zxc_program_popResultAndCopyToVar(name, BC_TYPE_VAR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006836 free(name);
6837 break;
6838 }
6839 case DC_INST_QUIT:
6840 dbg_exec("DC_INST_QUIT:");
6841 if (G.prog.exestack.len <= 2)
6842 QUIT_OR_RETURN_TO_MAIN;
6843 bc_vec_npop(&G.prog.exestack, 2);
6844 goto read_updated_ip;
6845 case DC_INST_NQUIT:
6846 dbg_exec("DC_INST_NQUIT:");
6847 s = zdc_program_nquit();
6848 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006849#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006850 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006851 // Instruction stack has changed, read new pointers
6852 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006853 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006854 code = func->code.v;
6855 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 }
6857
Denys Vlasenkod38af482018-12-04 19:11:02 +01006858 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006859 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006860 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006861 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006862
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006863 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006864 }
6865
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006866 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006868#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006869
Denys Vlasenko10bde142018-12-27 18:23:58 +01006870static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006871{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006872 char *lenv;
6873 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006874
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006875 lenv = getenv(var);
6876 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 if (!lenv) return len;
6878
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006879 len = bb_strtou(lenv, NULL, 10) - 1;
6880 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006881 len = BC_NUM_PRINT_WIDTH;
6882
6883 return len;
6884}
6885
Denys Vlasenko10bde142018-12-27 18:23:58 +01006886static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006887{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006888 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006889
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006890 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006891 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006892 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006893
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006894 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006895 BcInstPtr *ip;
6896 BcFunc *f;
6897
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006898 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006899 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006900#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006901 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006902 if (s) goto err;
6903
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006904 // Check that next token is a correct stmt delimiter -
6905 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006906 if (G.prs.lex != BC_LEX_SCOLON
6907 && G.prs.lex != XC_LEX_NLINE
6908 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006909 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01006910 bc_error_at("bad statement terminator");
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006911 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006912 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006913 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006914 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006915#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006916 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006917#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006918 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006919#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006920 }
6921 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006922 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006923 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006924 RETURN_STATUS(BC_STATUS_FAILURE);
6925 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006926
Denys Vlasenko39287e02018-12-22 02:23:08 +01006927 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006928 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006929 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006930 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006931 break;
6932 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006933
6934 ip = (void*)G.prog.exestack.v;
6935#if SANITY_CHECKS
6936 if (G.prog.exestack.len != 1) // should have only main's IP
James Byrne69374872019-07-02 11:35:03 +02006937 bb_simple_error_msg_and_die("BUG:call stack");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006938 if (ip->func != BC_PROG_MAIN)
James Byrne69374872019-07-02 11:35:03 +02006939 bb_simple_error_msg_and_die("BUG:not MAIN");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006940#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006941 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006942 // bc discards strings, constants and code after each
6943 // top-level statement in the "main program".
6944 // This prevents "yes 1 | bc" from growing its memory
6945 // without bound. This can be done because data stack
6946 // is empty and thus can't hold any references to
6947 // strings or constants, there is no generated code
6948 // which can hold references (after we discard one
6949 // we just executed). Code of functions can have references,
6950 // but bc stores function strings/constants in per-function
6951 // storage.
6952 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006953#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006954 if (G.prog.results.len != 0) // should be empty
James Byrne69374872019-07-02 11:35:03 +02006955 bb_simple_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006956#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006957 IF_BC(bc_vec_pop_all(&f->strs);)
6958 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01006959 // We are at SCOLON/NLINE, skip it:
6960 s = zxc_lex_next();
6961 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006962 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006963 if (G.prog.results.len == 0
6964 && G.prog.vars.len == 0
6965 ) {
6966 // If stack is empty and no registers exist (TODO: or they are all empty),
6967 // we can get rid of accumulated strings and constants.
6968 // In this example dc process should not grow
6969 // its memory consumption with time:
6970 // yes 1pc | dc
6971 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6972 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6973 }
6974 // The code is discarded always (below), thus this example
6975 // should also not grow its memory consumption with time,
6976 // even though its data stack is not empty:
6977 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006978 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006979 // We drop generated and executed code for both bc and dc:
6980 bc_vec_pop_all(&f->code);
6981 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006982 }
Denys Vlasenko6842c602019-01-04 05:41:47 +01006983
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006984 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006985 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006986}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006987#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006988
Denys Vlasenko10bde142018-12-27 18:23:58 +01006989static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006990{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006991 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006992 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006993 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006994 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006995
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006996 G.prs.lex_filename = filename;
6997 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006998 G.err_line = G.prs.lex_line = 1;
Denys Vlasenko22314682019-01-01 21:50:14 +01006999 dbg_lex("p->lex_line reset to 1");
Gavin Howard01055ba2018-11-03 11:00:21 -06007000
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007001 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007002 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007003 // We do not stop looping on errors here if reading stdin.
7004 // Example: start interactive bc and enter "return".
7005 // It should say "'return' not in a function"
7006 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01007007 } while (G.prs.lex_input_fp == stdin);
7008 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007009 RETURN_STATUS(s);
7010}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007011#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007012
Denys Vlasenko10bde142018-12-27 18:23:58 +01007013static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007014{
7015 BcStatus s;
7016 FILE *fp;
7017
7018 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007019 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007020 fclose(fp);
7021
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007022 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007023}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007024#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007025
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007026#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01007027static void bc_vm_info(void)
7028{
7029 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07007030 "Adapted from https://github.com/gavinhoward/bc\n"
7031 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01007032 , applet_name);
7033}
7034
7035static void bc_args(char **argv)
7036{
7037 unsigned opts;
7038 int i;
7039
7040 GETOPT_RESET();
7041#if ENABLE_FEATURE_BC_LONG_OPTIONS
7042 opts = option_mask32 |= getopt32long(argv, "wvsqli",
7043 "warn\0" No_argument "w"
7044 "version\0" No_argument "v"
7045 "standard\0" No_argument "s"
7046 "quiet\0" No_argument "q"
7047 "mathlib\0" No_argument "l"
7048 "interactive\0" No_argument "i"
7049 );
7050#else
7051 opts = option_mask32 |= getopt32(argv, "wvsqli");
7052#endif
7053 if (getenv("POSIXLY_CORRECT"))
7054 option_mask32 |= BC_FLAG_S;
7055
7056 if (opts & BC_FLAG_V) {
7057 bc_vm_info();
7058 exit(0);
7059 }
7060
7061 for (i = optind; argv[i]; ++i)
7062 bc_vec_push(&G.files, argv + i);
7063}
7064
7065static void bc_vm_envArgs(void)
7066{
7067 BcVec v;
7068 char *buf;
7069 char *env_args = getenv("BC_ENV_ARGS");
7070
7071 if (!env_args) return;
7072
7073 G.env_args = xstrdup(env_args);
7074 buf = G.env_args;
7075
7076 bc_vec_init(&v, sizeof(char *), NULL);
7077
7078 while (*(buf = skip_whitespace(buf)) != '\0') {
7079 bc_vec_push(&v, &buf);
7080 buf = skip_non_whitespace(buf);
7081 if (!*buf)
7082 break;
7083 *buf++ = '\0';
7084 }
7085
7086 // NULL terminate, and pass argv[] so that first arg is argv[1]
7087 if (sizeof(int) == sizeof(char*)) {
7088 bc_vec_push(&v, &const_int_0);
7089 } else {
7090 static char *const nullptr = NULL;
7091 bc_vec_push(&v, &nullptr);
7092 }
7093 bc_args(((char **)v.v) - 1);
7094
7095 bc_vec_free(&v);
7096}
7097
7098static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007099 "scale=20"
7100"\n" "define e(x){"
7101"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007102////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7103//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7104//e(-.998896): GNU:.36828580434569428695
7105// above code:.36828580434569428696
7106// actual value:.3682858043456942869594...
7107// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007108"\n" "b=ibase"
7109"\n" "ibase=A"
7110"\n" "if(x<0){"
7111"\n" "n=1"
7112"\n" "x=-x"
7113"\n" "}"
7114"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007115"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007116"\n" "scale=scale(x)+1"
7117"\n" "while(x>1){"
7118"\n" "d+=1"
7119"\n" "x/=2"
7120"\n" "scale+=1"
7121"\n" "}"
7122"\n" "scale=r"
7123"\n" "r=x+1"
7124"\n" "p=x"
7125"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007126"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007127"\n" "p*=x"
7128"\n" "f*=i"
7129"\n" "v=p/f"
7130"\n" "r+=v"
7131"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007132"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007133"\n" "scale=s"
7134"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007135"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007136"\n" "return(r/1)"
7137"\n" "}"
7138"\n" "define l(x){"
7139"\n" "auto b,s,r,p,a,q,i,v"
7140"\n" "b=ibase"
7141"\n" "ibase=A"
7142"\n" "if(x<=0){"
7143"\n" "r=(1-10^scale)/1"
7144"\n" "ibase=b"
7145"\n" "return(r)"
7146"\n" "}"
7147"\n" "s=scale"
7148"\n" "scale+=6"
7149"\n" "p=2"
7150"\n" "while(x>=2){"
7151"\n" "p*=2"
7152"\n" "x=sqrt(x)"
7153"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007154"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007155"\n" "p*=2"
7156"\n" "x=sqrt(x)"
7157"\n" "}"
7158"\n" "r=a=(x-1)/(x+1)"
7159"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007160"\n" "v=1"
7161"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007162"\n" "a*=q"
7163"\n" "v=a/i"
7164"\n" "r+=v"
7165"\n" "}"
7166"\n" "r*=p"
7167"\n" "scale=s"
7168"\n" "ibase=b"
7169"\n" "return(r/1)"
7170"\n" "}"
7171"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007172"\n" "auto b,s,r,a,q,i"
7173"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007174"\n" "b=ibase"
7175"\n" "ibase=A"
7176"\n" "s=scale"
7177"\n" "scale=1.1*s+2"
7178"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007179"\n" "scale=0"
7180"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007181"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007182"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007183"\n" "scale=s+2"
7184"\n" "r=a=x"
7185"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007186"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007187"\n" "a*=q/(i*(i-1))"
7188"\n" "r+=a"
7189"\n" "}"
7190"\n" "scale=s"
7191"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007192"\n" "return(r/1)"
7193"\n" "}"
7194"\n" "define c(x){"
7195"\n" "auto b,s"
7196"\n" "b=ibase"
7197"\n" "ibase=A"
7198"\n" "s=scale"
7199"\n" "scale*=1.2"
7200"\n" "x=s(2*a(1)+x)"
7201"\n" "scale=s"
7202"\n" "ibase=b"
7203"\n" "return(x/1)"
7204"\n" "}"
7205"\n" "define a(x){"
7206"\n" "auto b,s,r,n,a,m,t,f,i,u"
7207"\n" "b=ibase"
7208"\n" "ibase=A"
7209"\n" "n=1"
7210"\n" "if(x<0){"
7211"\n" "n=-1"
7212"\n" "x=-x"
7213"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007214"\n" "if(scale<65){"
7215"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7216"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007217"\n" "}"
7218"\n" "s=scale"
7219"\n" "if(x>.2){"
7220"\n" "scale+=5"
7221"\n" "a=a(.2)"
7222"\n" "}"
7223"\n" "scale=s+3"
7224"\n" "while(x>.2){"
7225"\n" "m+=1"
7226"\n" "x=(x-.2)/(1+.2*x)"
7227"\n" "}"
7228"\n" "r=u=x"
7229"\n" "f=-x*x"
7230"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007231"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007232"\n" "u*=f"
7233"\n" "t=u/i"
7234"\n" "r+=t"
7235"\n" "}"
7236"\n" "scale=s"
7237"\n" "ibase=b"
7238"\n" "return((m*a+r)/n)"
7239"\n" "}"
7240"\n" "define j(n,x){"
7241"\n" "auto b,s,o,a,i,v,f"
7242"\n" "b=ibase"
7243"\n" "ibase=A"
7244"\n" "s=scale"
7245"\n" "scale=0"
7246"\n" "n/=1"
7247"\n" "if(n<0){"
7248"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007249"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007250"\n" "}"
7251"\n" "a=1"
7252"\n" "for(i=2;i<=n;++i)a*=i"
7253"\n" "scale=1.5*s"
7254"\n" "a=(x^n)/2^n/a"
7255"\n" "r=v=1"
7256"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007257"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007258"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007259"\n" "v=v*f/i/(n+i)"
7260"\n" "r+=v"
7261"\n" "}"
7262"\n" "scale=s"
7263"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007264"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007265"\n" "return(a*r/1)"
7266"\n" "}"
7267};
7268#endif // ENABLE_BC
7269
Denys Vlasenko10bde142018-12-27 18:23:58 +01007270static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007271{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007272 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007273 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007274 size_t i;
7275
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007276#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007277 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007278 // We know that internal library is not buggy,
7279 // thus error checking is normally disabled.
7280# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007281 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007282 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007283 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007284#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007285
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007286 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007287 fname = (void*)G.files.v;
7288 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007289 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007290 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7291 // Debug config, non-interactive mode:
7292 // return all the way back to main.
7293 // Non-debug builds do not come here
7294 // in non-interactive mode, they exit.
7295 RETURN_STATUS(s);
7296 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007297 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007298
Denys Vlasenko91cde952018-12-10 20:56:08 +01007299 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007300 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007301
Denys Vlasenko19f11072018-12-12 22:48:19 +01007302 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007303}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007304#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007305
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007306#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007307static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007308{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007309 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007310 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007311 bc_vec_free(&G.prog.vars);
7312 bc_vec_free(&G.prog.var_map);
7313 bc_vec_free(&G.prog.arrs);
7314 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007315 IF_DC(bc_vec_free(&G.prog.strs);)
7316 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007317 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007318 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007319 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007320 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007321 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007322 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007323}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007324#endif
7325
Denys Vlasenko10bde142018-12-27 18:23:58 +01007326static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007327{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007328 BcInstPtr ip;
7329
Denys Vlasenko82269122018-12-14 16:30:56 +01007330 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007331 memset(&ip, 0, sizeof(BcInstPtr));
7332
Denys Vlasenko82269122018-12-14 16:30:56 +01007333 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007334 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007335 G.prog.ob_t = 10;
7336
Denys Vlasenko503faf92018-12-20 16:24:18 +01007337 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7338 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007339
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007340 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007341 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007342
Denys Vlasenko503faf92018-12-20 16:24:18 +01007343 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7344 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007345
7346 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007347 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007348
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007349 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007350 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007351 // a valid function name (and be short)
7352 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007353 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007354 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007355 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007356 xc_program_add_fn();
7357 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007358 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007359
7360 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007361 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007362
7363 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007364 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007365
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007366 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7367 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007368 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007369 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7370 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007371
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007372 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007373}
Gavin Howard01055ba2018-11-03 11:00:21 -06007374
Denys Vlasenko10bde142018-12-27 18:23:58 +01007375static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007376{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007377 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007378#if ENABLE_FEATURE_EDITING
7379 G.line_input_state = new_line_input_t(DO_HISTORY);
7380#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007381 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007382
Denys Vlasenko10bde142018-12-27 18:23:58 +01007383 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007384 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007385 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007386
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007387//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7388//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007389 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007390#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007391 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007392 // With SA_RESTART, most system calls will restart
7393 // (IOW: they won't fail with EINTR).
7394 // In particular, this means ^C won't cause
7395 // stdout to get into "error state" if SIGINT hits
7396 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007397 //
7398 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007399 // will only be handled after [Enter] since read()
7400 // from stdin is not interrupted by ^C either,
7401 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007402 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007403 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7404
7405 // Without SA_RESTART, this exhibits a bug:
7406 // "while (1) print 1" and try ^C-ing it.
7407 // Intermittently, instead of returning to input line,
7408 // you'll get "output error: Interrupted system call"
7409 // and exit.
7410 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007411#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007412 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007413 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007414 return 0; // "not a tty"
7415}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007416
Denys Vlasenko10bde142018-12-27 18:23:58 +01007417static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007418{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007419 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007420#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007421 if (G_exiting) // it was actually "halt" or "quit"
7422 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007423
7424 bc_vec_free(&G.files);
7425 xc_program_free();
7426 xc_parse_free();
7427 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007428# if ENABLE_FEATURE_EDITING
7429 free_line_input_t(G.line_input_state);
7430# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007431 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007432#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007433 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007434 return st;
7435}
7436
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007437#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007438int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007439int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007440{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007441 int is_tty;
7442
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007443 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007444
Denys Vlasenko10bde142018-12-27 18:23:58 +01007445 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007446
7447 bc_args(argv);
7448
7449 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7450 bc_vm_info();
7451
Denys Vlasenko10bde142018-12-27 18:23:58 +01007452 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007453}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007454#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007455
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007456#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007457int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007458int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007459{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007460 int noscript;
7461
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007462 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007463
7464 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7465 // 1 char wider than bc from the same package.
7466 // Both default width, and xC_LINE_LENGTH=N are wider:
7467 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007468 // |1234\ |
7469 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007470 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007471 // |123\ |
7472 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007473 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007474 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007475
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007476 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7477 noscript = BC_FLAG_I;
7478 for (;;) {
7479 int n = getopt(argc, argv, "e:f:x");
7480 if (n <= 0)
7481 break;
7482 switch (n) {
7483 case 'e':
7484 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007485 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007486 if (n) return n;
7487 break;
7488 case 'f':
7489 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007490 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007491 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007492 break;
7493 case 'x':
7494 option_mask32 |= DC_FLAG_X;
7495 break;
7496 default:
7497 bb_show_usage();
7498 }
7499 }
7500 argv += optind;
7501
7502 while (*argv) {
7503 noscript = 0;
7504 bc_vec_push(&G.files, argv++);
7505 }
7506
7507 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7508
Denys Vlasenko10bde142018-12-27 18:23:58 +01007509 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007510}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007511#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007512
Denys Vlasenko14767602018-12-27 22:52:13 +01007513#endif // DC_BIG