blob: 7fecb264d5de89ab5cd5ad7df57002044c860025 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Gavin Howardfa495ce2018-12-18 10:03:14 -07004 * Adapted from https://github.com/gavinhoward/bc
5 * Original code copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06006 */
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007//TODO: GNU extensions:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01008// support "define f(*param[])" - "pass array by reference" syntax
9
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
Gavin Howard01055ba2018-11-03 11:00:21 -0600383typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100384 XC_LEX_EOF,
385 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600386
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100387 XC_LEX_NLINE,
388 XC_LEX_WHITESPACE,
389 XC_LEX_STR,
390 XC_LEX_NAME,
391 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100392
Denys Vlasenko69560f42018-12-24 14:14:23 +0100393 XC_LEX_1st_op,
394 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100395
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100396 XC_LEX_OP_REL_EQ, // should
397 XC_LEX_OP_REL_LE, // match
398 XC_LEX_OP_REL_GE, // INST
399 XC_LEX_OP_REL_NE, // constants
400 XC_LEX_OP_REL_LT, // for
401 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100402
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100403 XC_LEX_OP_POWER, // operations
404 XC_LEX_OP_MULTIPLY, // |
405 XC_LEX_OP_DIVIDE, // |
406 XC_LEX_OP_MODULUS, // |
407 XC_LEX_OP_PLUS, // |
408 XC_LEX_OP_MINUS, // |
409 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100410#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100411 BC_LEX_OP_BOOL_NOT, // |
412 BC_LEX_OP_BOOL_OR, // |
413 BC_LEX_OP_BOOL_AND, // |
414
415 BC_LEX_OP_ASSIGN_POWER, // |
416 BC_LEX_OP_ASSIGN_MULTIPLY, // |
417 BC_LEX_OP_ASSIGN_DIVIDE, // |
418 BC_LEX_OP_ASSIGN_MODULUS, // |
419 BC_LEX_OP_ASSIGN_PLUS, // |
420 BC_LEX_OP_ASSIGN_MINUS, // |
421
422 BC_LEX_OP_ASSIGN, // V
423
Gavin Howard01055ba2018-11-03 11:00:21 -0600424 BC_LEX_OP_INC,
425 BC_LEX_OP_DEC,
426
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100427 BC_LEX_LPAREN, // () are 0x28 and 0x29
428 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600429
Denys Vlasenko10bde142018-12-27 18:23:58 +0100430 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600431 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100432 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600433
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100434 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600435 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100436 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600437
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100438 BC_LEX_KEY_1st_keyword,
439 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600440 BC_LEX_KEY_BREAK,
441 BC_LEX_KEY_CONTINUE,
442 BC_LEX_KEY_DEFINE,
443 BC_LEX_KEY_ELSE,
444 BC_LEX_KEY_FOR,
445 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100446 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100447 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
448 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600449 BC_LEX_KEY_IF,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100450 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600451 BC_LEX_KEY_LENGTH,
452 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600453 BC_LEX_KEY_PRINT,
454 BC_LEX_KEY_QUIT,
455 BC_LEX_KEY_READ,
456 BC_LEX_KEY_RETURN,
457 BC_LEX_KEY_SCALE,
458 BC_LEX_KEY_SQRT,
459 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100460#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600461
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100462#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100463 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
464 DC_LEX_OP_ASSIGN,
465
466 DC_LEX_LPAREN,
467 DC_LEX_SCOLON,
468 DC_LEX_READ,
469 DC_LEX_IBASE,
470 DC_LEX_SCALE,
471 DC_LEX_OBASE,
472 DC_LEX_LENGTH,
473 DC_LEX_PRINT,
474 DC_LEX_QUIT,
475 DC_LEX_SQRT,
476 DC_LEX_LBRACE,
477
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100478 DC_LEX_EQ_NO_REG,
479 DC_LEX_OP_MODEXP,
480 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600481
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100482 DC_LEX_COLON,
483 DC_LEX_ELSE,
484 DC_LEX_EXECUTE,
485 DC_LEX_PRINT_STACK,
486 DC_LEX_CLEAR_STACK,
487 DC_LEX_STACK_LEVEL,
488 DC_LEX_DUPLICATE,
489 DC_LEX_SWAP,
490 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600491
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100492 DC_LEX_ASCIIFY,
493 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600494
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100495 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
496 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
497 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
498 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
499 DC_LEX_LOAD,
500 DC_LEX_LOAD_POP,
501 DC_LEX_STORE_PUSH,
502 DC_LEX_PRINT_POP,
503 DC_LEX_NQUIT,
504 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100505#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600506} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100507// must match order of BC_LEX_KEY_foo etc above
508#if ENABLE_BC
509struct BcLexKeyword {
510 char name8[8];
511};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100512#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100513 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100514static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100515 LEX_KW_ENTRY("auto" , 1), // 0
516 LEX_KW_ENTRY("break" , 1), // 1
517 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
518 LEX_KW_ENTRY("define" , 1), // 3
519 LEX_KW_ENTRY("else" , 0), // 4
520 LEX_KW_ENTRY("for" , 1), // 5
521 LEX_KW_ENTRY("halt" , 0), // 6
522 LEX_KW_ENTRY("ibase" , 1), // 7
523 LEX_KW_ENTRY("obase" , 1), // 8
524 LEX_KW_ENTRY("if" , 1), // 9
525 LEX_KW_ENTRY("last" , 0), // 10
526 LEX_KW_ENTRY("length" , 1), // 11
527 LEX_KW_ENTRY("limits" , 0), // 12
528 LEX_KW_ENTRY("print" , 0), // 13
529 LEX_KW_ENTRY("quit" , 1), // 14
530 LEX_KW_ENTRY("read" , 0), // 15
531 LEX_KW_ENTRY("return" , 1), // 16
532 LEX_KW_ENTRY("scale" , 1), // 17
533 LEX_KW_ENTRY("sqrt" , 1), // 18
534 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100535};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100536#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100537#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100538#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100539#define STRING_if (bc_lex_kws[9].name8)
540#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100541enum {
542 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100543 | (1 << 0) // 0
544 | (1 << 1) // 1
545 | (0 << 2) // 2
546 | (1 << 3) // 3
547 | (0 << 4) // 4
548 | (1 << 5) // 5
549 | (0 << 6) // 6
550 | (1 << 7) // 7
551 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100552 | (1 << 9) // 9
553 | (0 << 10) // 10
554 | (1 << 11) // 11
555 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100556 | (0 << 13) // 13
557 | (1 << 14) // 14
558 | (0 << 15) // 15
559 | (1 << 16) // 16
560 | (1 << 17) // 17
561 | (1 << 18) // 18
562 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100563};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100564#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100565
566// This is a bit array that corresponds to token types. An entry is
567// true if the token is valid in an expression, false otherwise.
568// Used to figure out when expr parsing should stop *without error message*
569// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100570// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100571// by later processing.
572enum {
573#define EXBITS(a,b,c,d,e,f,g,h) \
574 ((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 +0100575 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100576 + (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 +0100577 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
578 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100579 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
580 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100581 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
582 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
583 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100584#undef EXBITS
585};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100586static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100587{
588#if ULONG_MAX > 0xffffffff
589 // 64-bit version (will not work correctly for 32-bit longs!)
590 return BC_PARSE_EXPRS_BITS & (1UL << i);
591#else
592 // 32-bit version
593 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
594 if (i >= 32) {
595 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
596 i &= 31;
597 }
598 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100599#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100600}
601
602// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100603// [XC_LEX_1st_op...] token types.
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100604static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100605#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100606 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100607 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 +0100608 OP(2, false), // pow
609 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
610 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100611 OP(1, false), // not
612 OP(7, true ), OP( 7, true ), // or and
613 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
614 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100615 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100616#undef OP
617};
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100618#define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f)
619#define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100620#endif // ENABLE_BC
621
622#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100623static const //BcLexType - should be this type
624uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100625dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100626 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100627 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100628 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100629 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100630 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100631 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100632 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100633 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
634 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
635 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100636 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100637 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
638 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100639 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100640 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
641 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100642 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100643 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
644 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenkof11b5b92019-01-04 15:54:40 +0100645 // QRSTUVWX
646 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID,
647 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR,
648 // YZ
649 XC_LEX_INVALID, DC_LEX_LENGTH,
650 // [\]
651 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100652 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100653 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100654 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100655 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
656 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100657 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100658 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100659 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100660 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100661 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
662 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100663 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100665 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100666 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100667};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100668static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100669int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100670dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
671 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
672 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
673 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100674 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
675 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
676 XC_INST_REL_GT, // DC_LEX_LPAREN
677 DC_INST_INVALID, // DC_LEX_SCOLON
678 DC_INST_INVALID, // DC_LEX_READ
679 XC_INST_IBASE, // DC_LEX_IBASE
680 XC_INST_SCALE, // DC_LEX_SCALE
681 XC_INST_OBASE, // DC_LEX_OBASE
682 XC_INST_LENGTH, // DC_LEX_LENGTH
683 XC_INST_PRINT, // DC_LEX_PRINT
684 DC_INST_QUIT, // DC_LEX_QUIT
685 XC_INST_SQRT, // DC_LEX_SQRT
686 XC_INST_REL_GE, // DC_LEX_LBRACE
687 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100688 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
689 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
690 DC_INST_EXECUTE, // DC_LEX_EXECUTE
691 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
692 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
693 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
694 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
695 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
696 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
697 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
698 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
699 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100700 // DC_INST_INVALID in this table either means that corresponding LEX
701 // is not possible for dc, or that it does not compile one-to-one
702 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100703};
704#endif // ENABLE_DC
705
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100706typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100707 smallint lex; // was BcLexType // first member is most used
708 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100709 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100710 const char *lex_inbuf;
711 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100712 const char *lex_filename;
713 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100714 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100715
Gavin Howard01055ba2018-11-03 11:00:21 -0600716 BcFunc *func;
717 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100718 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100719 IF_BC(BcVec exits;)
720 IF_BC(BcVec conds;)
721 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600722} BcParse;
723
Gavin Howard01055ba2018-11-03 11:00:21 -0600724typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600725 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100726 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600727
Denys Vlasenko503faf92018-12-20 16:24:18 +0100728 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600729 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600730 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600731
Gavin Howard01055ba2018-11-03 11:00:21 -0600732 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100733 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600734
735 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100736 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600737
738 BcVec vars;
739 BcVec var_map;
740
741 BcVec arrs;
742 BcVec arr_map;
743
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100744 IF_DC(BcVec strs;)
745 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600746
Gavin Howard01055ba2018-11-03 11:00:21 -0600747 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100748 IF_BC(BcNum one;)
749 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600750} BcProgram;
751
Denys Vlasenko6e618232018-12-25 22:20:14 +0100752struct globals {
753 BcParse prs; // first member is most used
754
755 // For error messages. Can be set to current parsed line,
756 // or [TODO] to current executing line (can be before last parsed one)
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100757 size_t err_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100758
759 BcVec input_buffer;
760
Denys Vlasenko14767602018-12-27 22:52:13 +0100761 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100762 IF_FEATURE_CLEAN_UP(smallint exiting;)
763
764 BcProgram prog;
765
766 BcVec files;
767
768 char *env_args;
769
770#if ENABLE_FEATURE_EDITING
771 line_input_t *line_input_state;
772#endif
773} FIX_ALIASING;
774#define G (*ptr_to_globals)
775#define INIT_G() do { \
776 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
777} while (0)
778#define FREE_G() do { \
779 FREE_PTR_TO_GLOBALS(); \
780} while (0)
781#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
782#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
783#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100784#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100785# define G_interrupt bb_got_signal
786# define G_ttyin G.ttyin
787#else
788# define G_interrupt 0
789# define G_ttyin 0
790#endif
791#if ENABLE_FEATURE_CLEAN_UP
792# define G_exiting G.exiting
793#else
794# define G_exiting 0
795#endif
796#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
797#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
798
Denys Vlasenko6e618232018-12-25 22:20:14 +0100799#if ENABLE_BC
800# define BC_PARSE_REL (1 << 0)
801# define BC_PARSE_PRINT (1 << 1)
802# define BC_PARSE_ARRAY (1 << 2)
803# define BC_PARSE_NOCALL (1 << 3)
804#endif
805
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100806#define BC_PROG_MAIN 0
807#define BC_PROG_READ 1
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100808#if ENABLE_DC
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100809#define BC_PROG_REQ_FUNCS 2
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100810#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600811
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100812#define BC_FLAG_W (1 << 0)
813#define BC_FLAG_V (1 << 1)
814#define BC_FLAG_S (1 << 2)
815#define BC_FLAG_Q (1 << 3)
816#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100817#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
818#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600819
Denys Vlasenko64074a12018-12-07 15:50:14 +0100820#define BC_MAX_OBASE ((unsigned) 999)
821#define BC_MAX_DIM ((unsigned) INT_MAX)
822#define BC_MAX_SCALE ((unsigned) UINT_MAX)
823#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
824#define BC_MAX_NUM BC_MAX_STRING
825// Unused apart from "limits" message. Just show a "biggish number" there.
Denys Vlasenko64074a12018-12-07 15:50:14 +0100826//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
827//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
Denys Vlasenko64074a12018-12-07 15:50:14 +0100828#define BC_MAX_EXP_STR "999999999"
829#define BC_MAX_VARS_STR "999999999"
830
831#define BC_MAX_OBASE_STR "999"
832
833#if INT_MAX == 2147483647
834# define BC_MAX_DIM_STR "2147483647"
835#elif INT_MAX == 9223372036854775807
836# define BC_MAX_DIM_STR "9223372036854775807"
837#else
838# error Strange INT_MAX
839#endif
840
841#if UINT_MAX == 4294967295
842# define BC_MAX_SCALE_STR "4294967295"
843# define BC_MAX_STRING_STR "4294967294"
844#elif UINT_MAX == 18446744073709551615
845# define BC_MAX_SCALE_STR "18446744073709551615"
846# define BC_MAX_STRING_STR "18446744073709551614"
847#else
848# error Strange UINT_MAX
849#endif
850#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600851
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100852// In configurations where errors abort instead of propagating error
853// return code up the call chain, functions returning BC_STATUS
854// actually don't return anything, they always succeed and return "void".
855// A macro wrapper is provided, which makes this statement work:
856// s = zbc_func(...)
857// and makes it visible to the compiler that s is always zero,
858// allowing compiler to optimize dead code after the statement.
859//
860// To make code more readable, each such function has a "z"
861// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
862//
Denys Vlasenko14767602018-12-27 22:52:13 +0100863#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100864# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100865# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100866# define IF_ERROR_RETURN_POSSIBLE(a) a
867# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100868# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100869# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100870#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100871# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100872# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100873# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
874# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100875# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100876# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100877#endif
878
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100879//
880// Utility routines
881//
Gavin Howard01055ba2018-11-03 11:00:21 -0600882
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100883#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
884#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
885
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100886static void fflush_and_check(void)
887{
888 fflush_all();
889 if (ferror(stdout) || ferror(stderr))
890 bb_perror_msg_and_die("output error");
891}
892
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100893#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100894#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100895do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100896 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100897 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100898 return BC_STATUS_FAILURE; \
899} while (0)
900#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100901static void quit(void) NORETURN;
902static void quit(void)
903{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100904 if (ferror(stdin))
905 bb_perror_msg_and_die("input error");
906 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100907 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100908 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100909}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100910#define QUIT_OR_RETURN_TO_MAIN quit()
911#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100912
Denys Vlasenko5318f812018-12-05 17:48:01 +0100913static void bc_verror_msg(const char *fmt, va_list p)
914{
Denys Vlasenko82269122018-12-14 16:30:56 +0100915 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100916 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100917 sv = applet_name;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100918 applet_name = xasprintf("%s: %s:%lu", applet_name,
919 G.prs.lex_filename, (unsigned long)G.err_line
920 );
Denys Vlasenko5318f812018-12-05 17:48:01 +0100921 }
922 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100923 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100924 free((char*)applet_name);
925 applet_name = sv;
926 }
927}
928
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100929static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100930{
931 va_list p;
932
933 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100934 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100935 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100936
Denys Vlasenkoec603182018-12-17 10:34:02 +0100937 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
938 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
939 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100940}
941
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100942#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100943static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100944{
945 va_list p;
946
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100947 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100948 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100949 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100950
951 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100952 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100953 va_end(p);
954
955 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100956 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100957 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
958
Denys Vlasenkoec603182018-12-17 10:34:02 +0100959 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100960 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100961 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100962}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100963#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100964#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100965
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100966// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
967// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100968// function must not have caller-cleaned parameters on stack.
969// Unfortunately, vararg function API does exactly that on most arches.
970// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100971static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100972{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100973 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100974}
Denys Vlasenko22314682019-01-01 21:50:14 +0100975static ERRORFUNC int bc_error_at(const char *msg)
976{
977 const char *err_at = G.prs.lex_next_at;
978 if (err_at) {
979 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt(
980 "%s at '%.*s'",
981 msg,
982 (int)(strchrnul(err_at, '\n') - err_at),
983 err_at
984 );
985 }
986 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
987}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100988static ERRORFUNC int bc_error_bad_character(char c)
989{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100990 if (!c)
991 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100992 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100993}
Denys Vlasenko22314682019-01-01 21:50:14 +0100994static ERRORFUNC int bc_error_bad_function_definition(void)
995{
996 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition");
997}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100998static ERRORFUNC int bc_error_bad_expression(void)
999{
Denys Vlasenko22314682019-01-01 21:50:14 +01001000 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression");
1001}
1002static ERRORFUNC int bc_error_bad_assignment(void)
1003{
1004 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at(
1005 "bad assignment: left side must be variable or array element"
1006 );
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001007}
1008static ERRORFUNC int bc_error_bad_token(void)
1009{
Denys Vlasenko22314682019-01-01 21:50:14 +01001010 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001011}
1012static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1013{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001014 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001015}
1016static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1017{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001018 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001019}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001020#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001021static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001022{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001023 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001024}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001025#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1026static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001027{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001028 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001029}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001030#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1031static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001032{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001033 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 +01001034}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001035#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)
1036static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001037{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001038 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001039}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001040#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 +01001041#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001042
Gavin Howard01055ba2018-11-03 11:00:21 -06001043static void bc_vec_grow(BcVec *v, size_t n)
1044{
1045 size_t cap = v->cap * 2;
1046 while (cap < v->len + n) cap *= 2;
1047 v->v = xrealloc(v->v, v->size * cap);
1048 v->cap = cap;
1049}
1050
1051static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1052{
1053 v->size = esize;
1054 v->cap = BC_VEC_START_CAP;
1055 v->len = 0;
1056 v->dtor = dtor;
1057 v->v = xmalloc(esize * BC_VEC_START_CAP);
1058}
1059
Denys Vlasenko7d628012018-12-04 21:46:47 +01001060static void bc_char_vec_init(BcVec *v)
1061{
1062 bc_vec_init(v, sizeof(char), NULL);
1063}
1064
Gavin Howard01055ba2018-11-03 11:00:21 -06001065static void bc_vec_expand(BcVec *v, size_t req)
1066{
1067 if (v->cap < req) {
1068 v->v = xrealloc(v->v, v->size * req);
1069 v->cap = req;
1070 }
1071}
1072
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001073static void bc_vec_pop(BcVec *v)
1074{
1075 v->len--;
1076 if (v->dtor)
1077 v->dtor(v->v + (v->size * v->len));
1078}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001079
Gavin Howard01055ba2018-11-03 11:00:21 -06001080static void bc_vec_npop(BcVec *v, size_t n)
1081{
1082 if (!v->dtor)
1083 v->len -= n;
1084 else {
1085 size_t len = v->len - n;
1086 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1087 }
1088}
1089
Denys Vlasenko7d628012018-12-04 21:46:47 +01001090static void bc_vec_pop_all(BcVec *v)
1091{
1092 bc_vec_npop(v, v->len);
1093}
1094
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001095static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001096{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001097 size_t len = v->len;
1098 if (len >= v->cap) bc_vec_grow(v, 1);
1099 memmove(v->v + (v->size * len), data, v->size);
1100 v->len++;
1101 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001102}
1103
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001104// G.prog.results often needs "pop old operand, push result" idiom.
1105// Can do this without a few extra ops
1106static size_t bc_result_pop_and_push(const void *data)
1107{
1108 BcVec *v = &G.prog.results;
1109 char *last;
1110 size_t len = v->len - 1;
1111
1112 last = v->v + (v->size * len);
1113 if (v->dtor)
1114 v->dtor(last);
1115 memmove(last, data, v->size);
1116 return len;
1117}
1118
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001119static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001120{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001121 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001122}
1123
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001124static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001125{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001126 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001127 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001128 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001129}
1130
Gavin Howard01055ba2018-11-03 11:00:21 -06001131static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1132{
1133 if (idx == v->len)
1134 bc_vec_push(v, data);
1135 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001136 char *ptr;
1137
1138 if (v->len == v->cap) bc_vec_grow(v, 1);
1139
1140 ptr = v->v + v->size * idx;
1141
1142 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1143 memmove(ptr, data, v->size);
1144 }
1145}
1146
1147static void bc_vec_string(BcVec *v, size_t len, const char *str)
1148{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001149 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001150 bc_vec_expand(v, len + 1);
1151 memcpy(v->v, str, len);
1152 v->len = len;
1153
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001154 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001155}
1156
Gavin Howard01055ba2018-11-03 11:00:21 -06001157static void *bc_vec_item(const BcVec *v, size_t idx)
1158{
1159 return v->v + v->size * idx;
1160}
1161
1162static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1163{
1164 return v->v + v->size * (v->len - idx - 1);
1165}
1166
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001167static void *bc_vec_top(const BcVec *v)
1168{
1169 return v->v + v->size * (v->len - 1);
1170}
1171
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001172static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001173{
1174 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001175 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001176 free(v->v);
1177}
1178
Denys Vlasenko10bde142018-12-27 18:23:58 +01001179static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001180{
1181 return bc_vec_item(&G.prog.fns, idx);
1182}
1183// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001184#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001185
1186#if ENABLE_BC
1187static BcFunc* bc_program_current_func(void)
1188{
1189 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001190 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001191 return func;
1192}
1193#endif
1194
Denys Vlasenko10bde142018-12-27 18:23:58 +01001195static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001196{
1197#if ENABLE_BC
1198 if (IS_BC) {
1199 BcFunc *func = bc_program_current_func();
1200 return bc_vec_item(&func->strs, idx);
1201 }
1202#endif
1203 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1204}
1205
Denys Vlasenko10bde142018-12-27 18:23:58 +01001206static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001207{
1208#if ENABLE_BC
1209 if (IS_BC) {
1210 BcFunc *func = bc_program_current_func();
1211 return bc_vec_item(&func->consts, idx);
1212 }
1213#endif
1214 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1215}
1216
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001217static int bc_id_cmp(const void *e1, const void *e2)
1218{
1219 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1220}
1221
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001222static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001223{
1224 free(((BcId *) id)->name);
1225}
1226
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001227static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001228{
1229 size_t low = 0, high = v->len;
1230
1231 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001232 size_t mid = (low + high) / 2;
1233 BcId *id = bc_vec_item(v, mid);
1234 int result = bc_id_cmp(ptr, id);
1235
1236 if (result == 0)
1237 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001238 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001239 high = mid;
1240 else
1241 low = mid + 1;
1242 }
1243
1244 return low;
1245}
1246
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001247static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001248{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001249 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001250
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001251 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001252 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001253 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1254 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001255 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001256 bc_vec_pushAt(v, ptr, n);
1257 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001258}
1259
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001260#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001261static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001262{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001263 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001264 if (i >= v->len) return BC_VEC_INVALID_IDX;
1265 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1266}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001267#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001268
Gavin Howard01055ba2018-11-03 11:00:21 -06001269static void bc_num_setToZero(BcNum *n, size_t scale)
1270{
1271 n->len = 0;
1272 n->neg = false;
1273 n->rdx = scale;
1274}
1275
1276static void bc_num_zero(BcNum *n)
1277{
1278 bc_num_setToZero(n, 0);
1279}
1280
1281static void bc_num_one(BcNum *n)
1282{
1283 bc_num_setToZero(n, 0);
1284 n->len = 1;
1285 n->num[0] = 1;
1286}
1287
Denys Vlasenko3129f702018-12-09 12:04:44 +01001288// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001289static void bc_num_init(BcNum *n, size_t req)
1290{
1291 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001292 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001293 n->num = xmalloc(req);
1294 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001295 n->rdx = 0;
1296 n->len = 0;
1297 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001298}
1299
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001300static void bc_num_init_DEF_SIZE(BcNum *n)
1301{
1302 bc_num_init(n, BC_NUM_DEF_SIZE);
1303}
1304
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001305static void bc_num_expand(BcNum *n, size_t req)
1306{
1307 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1308 if (req > n->cap) {
1309 n->num = xrealloc(n->num, req);
1310 n->cap = req;
1311 }
1312}
1313
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001314static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001315{
1316 free(((BcNum *) num)->num);
1317}
1318
1319static void bc_num_copy(BcNum *d, BcNum *s)
1320{
1321 if (d != s) {
1322 bc_num_expand(d, s->cap);
1323 d->len = s->len;
1324 d->neg = s->neg;
1325 d->rdx = s->rdx;
1326 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1327 }
1328}
1329
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001330static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001331{
1332 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001333 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001334
Denys Vlasenko1557b762018-12-22 21:37:46 +01001335 result = 0;
1336 i = n->len;
1337 while (i > n->rdx) {
1338 unsigned long prev = result;
1339 result = result * 10 + n->num[--i];
1340 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1341 // 0x1ff00000 * 10 is 0x13f600000,
1342 // or 0x3f600000 truncated to 32 bits. Which is larger.
1343 // However, (N*10)/8 < N check is always correct.
1344 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001345 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001346 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001347 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001348
Denys Vlasenko29301232018-12-11 15:29:32 +01001349 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001350}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001351#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1352
1353static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1354{
1355 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1356
1357 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1358}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001359#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001360
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001361#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1362# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1363#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1364# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1365#endif
1366// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1367// would not hit realloc() code path - not good if num[] is not malloced
1368
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001369static void bc_num_ulong2num(BcNum *n, unsigned long val)
1370{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001371 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001372
1373 bc_num_zero(n);
1374
1375 if (val == 0) return;
1376
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001377 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001378
1379 ptr = n->num;
1380 for (;;) {
1381 n->len++;
1382 *ptr++ = val % 10;
1383 val /= 10;
1384 if (val == 0) break;
1385 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001386}
1387
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001388static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001389{
1390 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001391 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001392 a[i] -= b[i];
1393 for (j = i; a[j] < 0;) {
1394 a[j++] += 10;
1395 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001396 }
1397 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001398}
1399
1400static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1401{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001402 size_t i = len;
1403 for (;;) {
1404 int c;
1405 if (i == 0)
1406 return 0;
1407 i--;
1408 c = a[i] - b[i];
1409 if (c != 0) {
1410 i++;
1411 if (c < 0)
1412 return -i;
1413 return i;
1414 }
1415 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001416}
1417
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001418#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1419#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1420#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1421//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1422static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1423{
1424 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1425}
1426//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1427static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1428{
1429 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1430}
1431
Gavin Howard01055ba2018-11-03 11:00:21 -06001432static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1433{
1434 size_t i, min, a_int, b_int, diff;
1435 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001436 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001437 ssize_t cmp;
1438
1439 if (a == b) return 0;
1440 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1441 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001442
1443 if (a->neg != b->neg) // signs of a and b differ
1444 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1445 return (int)b->neg - (int)a->neg;
1446 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001447
1448 a_int = BC_NUM_INT(a);
1449 b_int = BC_NUM_INT(b);
1450 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001451
1452 if (a_int != 0) return (ssize_t) a_int;
1453
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001454 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001455 if (a_max) {
1456 min = b->rdx;
1457 diff = a->rdx - b->rdx;
1458 max_num = a->num + diff;
1459 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001460 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1461 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001462 min = a->rdx;
1463 diff = b->rdx - a->rdx;
1464 max_num = b->num + diff;
1465 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001466 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001467 }
1468
1469 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001470 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001471
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001472 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001473 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001474 }
1475
1476 return 0;
1477}
1478
1479static void bc_num_truncate(BcNum *n, size_t places)
1480{
1481 if (places == 0) return;
1482
1483 n->rdx -= places;
1484
1485 if (n->len != 0) {
1486 n->len -= places;
1487 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1488 }
1489}
1490
1491static void bc_num_extend(BcNum *n, size_t places)
1492{
1493 size_t len = n->len + places;
1494
1495 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001496 if (n->cap < len) bc_num_expand(n, len);
1497
1498 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1499 memset(n->num, 0, sizeof(BcDig) * places);
1500
1501 n->len += places;
1502 n->rdx += places;
1503 }
1504}
1505
1506static void bc_num_clean(BcNum *n)
1507{
1508 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1509 if (n->len == 0)
1510 n->neg = false;
1511 else if (n->len < n->rdx)
1512 n->len = n->rdx;
1513}
1514
1515static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1516{
1517 if (n->rdx < scale)
1518 bc_num_extend(n, scale - n->rdx);
1519 else
1520 bc_num_truncate(n, n->rdx - scale);
1521
1522 bc_num_clean(n);
1523 if (n->len != 0) n->neg = !neg1 != !neg2;
1524}
1525
1526static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1527 BcNum *restrict b)
1528{
1529 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001530 b->len = n->len - idx;
1531 a->len = idx;
1532 a->rdx = b->rdx = 0;
1533
1534 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1535 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001536 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001537 bc_num_zero(b);
1538 bc_num_copy(a, n);
1539 }
1540
1541 bc_num_clean(a);
1542 bc_num_clean(b);
1543}
1544
Denys Vlasenko29301232018-12-11 15:29:32 +01001545static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001546{
Denys Vlasenko29301232018-12-11 15:29:32 +01001547 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001548
1549 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1550 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1551 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001552 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001553 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001554
1555 if (n->rdx >= places)
1556 n->rdx -= places;
1557 else {
1558 bc_num_extend(n, places - n->rdx);
1559 n->rdx = 0;
1560 }
1561
1562 bc_num_clean(n);
1563
Denys Vlasenko29301232018-12-11 15:29:32 +01001564 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001565}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001566#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001567
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001568typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1569
1570static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1571 BcNumBinaryOp op, size_t req)
1572{
1573 BcStatus s;
1574 BcNum num2, *ptr_a, *ptr_b;
1575 bool init = false;
1576
1577 if (c == a) {
1578 ptr_a = &num2;
1579 memcpy(ptr_a, c, sizeof(BcNum));
1580 init = true;
1581 } else
1582 ptr_a = a;
1583
1584 if (c == b) {
1585 ptr_b = &num2;
1586 if (c != a) {
1587 memcpy(ptr_b, c, sizeof(BcNum));
1588 init = true;
1589 }
1590 } else
1591 ptr_b = b;
1592
1593 if (init)
1594 bc_num_init(c, req);
1595 else
1596 bc_num_expand(c, req);
1597
1598 s = BC_STATUS_SUCCESS;
1599 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1600
1601 if (init) bc_num_free(&num2);
1602
1603 RETURN_STATUS(s);
1604}
1605#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1606
1607static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1608static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1609static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1610static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1611static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1612static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1613
1614static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1615{
1616 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1617 (void) scale;
1618 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1619}
1620
1621static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1622{
1623 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1624 (void) scale;
1625 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1626}
1627
1628static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1629{
1630 size_t req = BC_NUM_MREQ(a, b, scale);
1631 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1632}
1633
1634static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1635{
1636 size_t req = BC_NUM_MREQ(a, b, scale);
1637 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1638}
1639
1640static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1641{
1642 size_t req = BC_NUM_MREQ(a, b, scale);
1643 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1644}
1645
1646static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1647{
1648 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1649}
1650
Denys Vlasenko10bde142018-12-27 18:23:58 +01001651static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001652 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1653};
1654#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1655#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1656#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1657#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1658#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1659#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1660
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001661static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001662{
1663 BcNum one;
1664 BcDig num[2];
1665
1666 one.cap = 2;
1667 one.num = num;
1668 bc_num_one(&one);
1669
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001670 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001671}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001672#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001673
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001674static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001675{
1676 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1677 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001678 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001679
1680 // Because this function doesn't need to use scale (per the bc spec),
1681 // I am hijacking it to say whether it's doing an add or a subtract.
1682
1683 if (a->len == 0) {
1684 bc_num_copy(c, b);
1685 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001686 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001687 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001688 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001689 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001690 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001691 }
1692
1693 c->neg = a->neg;
1694 c->rdx = BC_MAX(a->rdx, b->rdx);
1695 min_rdx = BC_MIN(a->rdx, b->rdx);
1696 c->len = 0;
1697
1698 if (a->rdx > b->rdx) {
1699 diff = a->rdx - b->rdx;
1700 ptr = a->num;
1701 ptr_a = a->num + diff;
1702 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001703 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001704 diff = b->rdx - a->rdx;
1705 ptr = b->num;
1706 ptr_a = a->num;
1707 ptr_b = b->num + diff;
1708 }
1709
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001710 ptr_c = c->num;
1711 for (i = 0; i < diff; ++i, ++c->len)
1712 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001713
1714 ptr_c += diff;
1715 a_int = BC_NUM_INT(a);
1716 b_int = BC_NUM_INT(b);
1717
1718 if (a_int > b_int) {
1719 min_int = b_int;
1720 max = a_int;
1721 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001722 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001723 min_int = a_int;
1724 max = b_int;
1725 ptr = ptr_b;
1726 }
1727
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001728 carry = 0;
1729 for (i = 0; i < min_rdx + min_int; ++i) {
1730 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001731 carry = in / 10;
1732 ptr_c[i] = (BcDig)(in % 10);
1733 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001734 for (; i < max + min_rdx; ++i) {
1735 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001736 carry = in / 10;
1737 ptr_c[i] = (BcDig)(in % 10);
1738 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001739 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001740
1741 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1742
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001743 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001744}
1745
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001746static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001747{
Gavin Howard01055ba2018-11-03 11:00:21 -06001748 ssize_t cmp;
1749 BcNum *minuend, *subtrahend;
1750 size_t start;
1751 bool aneg, bneg, neg;
1752
1753 // Because this function doesn't need to use scale (per the bc spec),
1754 // I am hijacking it to say whether it's doing an add or a subtract.
1755
1756 if (a->len == 0) {
1757 bc_num_copy(c, b);
1758 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001759 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001760 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001761 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001762 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001763 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001764 }
1765
1766 aneg = a->neg;
1767 bneg = b->neg;
1768 a->neg = b->neg = false;
1769
1770 cmp = bc_num_cmp(a, b);
1771
1772 a->neg = aneg;
1773 b->neg = bneg;
1774
1775 if (cmp == 0) {
1776 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001777 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001778 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001779 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001780 neg = a->neg;
1781 minuend = a;
1782 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001783 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001784 neg = b->neg;
1785 if (sub) neg = !neg;
1786 minuend = b;
1787 subtrahend = a;
1788 }
1789
1790 bc_num_copy(c, minuend);
1791 c->neg = neg;
1792
1793 if (c->rdx < subtrahend->rdx) {
1794 bc_num_extend(c, subtrahend->rdx - c->rdx);
1795 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001796 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 start = c->rdx - subtrahend->rdx;
1798
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001799 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001800
1801 bc_num_clean(c);
1802
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001803 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001804}
1805
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001806static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001807 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001808#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001809{
1810 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001811 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001812 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001813 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001814
Gavin Howard01055ba2018-11-03 11:00:21 -06001815 if (a->len == 0 || b->len == 0) {
1816 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001817 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001818 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001819 aone = BC_NUM_ONE(a);
1820 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001821 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001822 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 }
1824
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001825 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1826 || a->len < BC_NUM_KARATSUBA_LEN
1827 || b->len < BC_NUM_KARATSUBA_LEN
1828 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001829 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001830
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 bc_num_expand(c, a->len + b->len + 1);
1832
1833 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001834 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001835
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001836 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001837 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001838 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001839 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001840 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001841 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 carry = in / 10;
1843 c->num[i + j] = (BcDig)(in % 10);
1844 }
1845
1846 c->num[i + j] += (BcDig) carry;
1847 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001848
Denys Vlasenko14767602018-12-27 22:52:13 +01001849#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001850 // a=2^1000000
1851 // a*a <- without check below, this will not be interruptible
1852 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001853#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001854 }
1855
1856 c->len = len;
1857
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001858 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001859 }
1860
1861 bc_num_init(&l1, max);
1862 bc_num_init(&h1, max);
1863 bc_num_init(&l2, max);
1864 bc_num_init(&h2, max);
1865 bc_num_init(&m1, max);
1866 bc_num_init(&m2, max);
1867 bc_num_init(&z0, max);
1868 bc_num_init(&z1, max);
1869 bc_num_init(&z2, max);
1870 bc_num_init(&temp, max + max);
1871
1872 bc_num_split(a, max2, &l1, &h1);
1873 bc_num_split(b, max2, &l2, &h2);
1874
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001875 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001877 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 if (s) goto err;
1879
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001880 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001882 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001883 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001884 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001885 if (s) goto err;
1886
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001887 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001888 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001889 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001890 if (s) goto err;
1891
Denys Vlasenko29301232018-12-11 15:29:32 +01001892 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001894 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001897 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001898 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001899 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001900 bc_num_free(&temp);
1901 bc_num_free(&z2);
1902 bc_num_free(&z1);
1903 bc_num_free(&z0);
1904 bc_num_free(&m2);
1905 bc_num_free(&m1);
1906 bc_num_free(&h2);
1907 bc_num_free(&l2);
1908 bc_num_free(&h1);
1909 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001910 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001911}
1912
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001913static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001914{
1915 BcStatus s;
1916 BcNum cpa, cpb;
1917 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1918
1919 scale = BC_MAX(scale, a->rdx);
1920 scale = BC_MAX(scale, b->rdx);
1921 scale = BC_MIN(a->rdx + b->rdx, scale);
1922 maxrdx = BC_MAX(maxrdx, scale);
1923
1924 bc_num_init(&cpa, a->len);
1925 bc_num_init(&cpb, b->len);
1926
1927 bc_num_copy(&cpa, a);
1928 bc_num_copy(&cpb, b);
1929 cpa.neg = cpb.neg = false;
1930
Denys Vlasenko29301232018-12-11 15:29:32 +01001931 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001932 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001933 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001934 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001935 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001936 if (s) goto err;
1937
1938 maxrdx += scale;
1939 bc_num_expand(c, c->len + maxrdx);
1940
1941 if (c->len < maxrdx) {
1942 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1943 c->len += maxrdx;
1944 }
1945
1946 c->rdx = maxrdx;
1947 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001948 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 bc_num_free(&cpb);
1950 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001951 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001952}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001953#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001954
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001955static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001956{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001957 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001958 size_t len, end, i;
1959 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001960
1961 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001962 RETURN_STATUS(bc_error("divide by zero"));
1963 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001964 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001965 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001966 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001967 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 bc_num_copy(c, a);
1969 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001970 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001971 }
1972
1973 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1974 bc_num_copy(&cp, a);
1975 len = b->len;
1976
1977 if (len > cp.len) {
1978 bc_num_expand(&cp, len + 2);
1979 bc_num_extend(&cp, len - cp.len);
1980 }
1981
1982 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1983 cp.rdx -= b->rdx;
1984 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1985
1986 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001987 for (;;) {
1988 if (len == 0) break;
1989 len--;
1990 if (b->num[len] != 0)
1991 break;
1992 }
1993 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001994 }
1995
1996 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1997
1998 // We want an extra zero in front to make things simpler.
1999 cp.num[cp.len++] = 0;
2000 end = cp.len - len;
2001
2002 bc_num_expand(c, cp.len);
2003
2004 bc_num_zero(c);
2005 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2006 c->rdx = cp.rdx;
2007 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002008
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002009 s = BC_STATUS_SUCCESS;
2010 for (i = end - 1; i < end; --i) {
2011 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002012 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002013 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2014 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002015 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01002016#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01002017 // a=2^100000
2018 // scale=40000
2019 // 1/a <- without check below, this will not be interruptible
2020 if (G_interrupt) {
2021 s = BC_STATUS_FAILURE;
2022 break;
2023 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002024#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002025 }
2026
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002027 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002028 bc_num_free(&cp);
2029
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002030 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002031}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002032#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002033
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002034static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002035 BcNum *restrict d, size_t scale, size_t ts)
2036{
2037 BcStatus s;
2038 BcNum temp;
2039 bool neg;
2040
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002041 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002042 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002043
2044 if (a->len == 0) {
2045 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002046 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002047 }
2048
2049 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002050 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002051 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002052
2053 if (scale != 0) scale = ts;
2054
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002055 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002056 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002057 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002058 if (s) goto err;
2059
2060 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2061
2062 neg = d->neg;
2063 bc_num_retireMul(d, ts, a->neg, b->neg);
2064 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002065 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002066 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002067 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002068}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002069#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002070
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002071static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002072{
2073 BcStatus s;
2074 BcNum c1;
2075 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2076
2077 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002078 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002079 bc_num_free(&c1);
2080
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002081 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002082}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002083#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002084
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002085static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002086{
2087 BcStatus s = BC_STATUS_SUCCESS;
2088 BcNum copy;
2089 unsigned long pow;
2090 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002091 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002092
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002093 // GNU bc does not allow 2^2.0 - we do
2094 for (i = 0; i < b->rdx; i++)
2095 if (b->num[i] != 0)
2096 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002097
2098 if (b->len == 0) {
2099 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002100 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002101 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002102 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002103 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002104 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002105 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002106 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002107 if (!b->neg)
2108 bc_num_copy(c, a);
2109 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002110 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002111 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002112 }
2113
2114 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002115 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002116 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002117 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002118
2119 bc_num_init(&copy, a->len);
2120 bc_num_copy(&copy, a);
2121
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002122 if (!neg) {
2123 if (a->rdx > scale)
2124 scale = a->rdx;
2125 if (a->rdx * pow < scale)
2126 scale = a->rdx * pow;
2127 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002128
Gavin Howard01055ba2018-11-03 11:00:21 -06002129
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002130 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002131 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002132 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002133 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002134 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002135 //if (G_interrupt) {
2136 // s = BC_STATUS_FAILURE;
2137 // goto err;
2138 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002139 }
2140
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 bc_num_copy(c, &copy);
2142
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002143 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002144 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002145 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 if (s) goto err;
2147
2148 if (pow & 1) {
2149 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002150 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002151 if (s) goto err;
2152 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002153 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002154 //if (G_interrupt) {
2155 // s = BC_STATUS_FAILURE;
2156 // goto err;
2157 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002158 }
2159
2160 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002161 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 if (s) goto err;
2163 }
2164
Gavin Howard01055ba2018-11-03 11:00:21 -06002165 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2166
2167 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002168 for (i = 0; i < c->len; ++i)
2169 if (c->num[i] != 0)
2170 goto skip;
2171 bc_num_setToZero(c, scale);
2172 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002173
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002174 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002175 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002176 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002177}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002178#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002179
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002180static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002181{
2182 BcStatus s;
2183 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002184 BcDig half_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002185 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2186 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2187
2188 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2189 bc_num_expand(b, req);
2190
2191 if (a->len == 0) {
2192 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002193 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002194 }
2195 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002196 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002197 }
2198 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002199 bc_num_one(b);
2200 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002201 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002202 }
2203
2204 scale = BC_MAX(scale, a->rdx) + 1;
2205 len = a->len + scale;
2206
2207 bc_num_init(&num1, len);
2208 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002209
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002210 half.cap = ARRAY_SIZE(half_digs);
2211 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002212 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002213 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002214 half.rdx = 1;
2215
2216 bc_num_init(&f, len);
2217 bc_num_init(&fprime, len);
2218
2219 x0 = &num1;
2220 x1 = &num2;
2221
2222 bc_num_one(x0);
2223 pow = BC_NUM_INT(a);
2224
2225 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002226 if (pow & 1)
2227 x0->num[0] = 2;
2228 else
2229 x0->num[0] = 6;
2230
2231 pow -= 2 - (pow & 1);
2232
2233 bc_num_extend(x0, pow);
2234
2235 // Make sure to move the radix back.
2236 x0->rdx -= pow;
2237 }
2238
2239 x0->rdx = digs = digs1 = 0;
2240 resrdx = scale + 2;
2241 len = BC_NUM_INT(x0) + resrdx - 1;
2242
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002243 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002244 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002245 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002246 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002247 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002248 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002249 if (s) goto err;
2250
2251 cmp = bc_num_cmp(x1, x0);
2252 digs = x1->len - (unsigned long long) llabs(cmp);
2253
2254 if (cmp == cmp2 && digs == digs1)
2255 times += 1;
2256 else
2257 times = 0;
2258
2259 resrdx += times > 4;
2260
2261 cmp2 = cmp1;
2262 cmp1 = cmp;
2263 digs1 = digs;
2264
2265 temp = x0;
2266 x0 = x1;
2267 x1 = temp;
2268 }
2269
Gavin Howard01055ba2018-11-03 11:00:21 -06002270 bc_num_copy(b, x0);
2271 scale -= 1;
2272 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002273 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002274 bc_num_free(&fprime);
2275 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002276 bc_num_free(&num2);
2277 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002278 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002279}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002280#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002281
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002282static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002283 size_t scale)
2284{
2285 BcStatus s;
2286 BcNum num2, *ptr_a;
2287 bool init = false;
2288 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2289
2290 if (c == a) {
2291 memcpy(&num2, c, sizeof(BcNum));
2292 ptr_a = &num2;
2293 bc_num_init(c, len);
2294 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002295 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002296 ptr_a = a;
2297 bc_num_expand(c, len);
2298 }
2299
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002300 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002301
2302 if (init) bc_num_free(&num2);
2303
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002304 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002305}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002306#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002307
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002308#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002309static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002310{
2311 BcStatus s;
2312 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002313 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002314
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002315 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002316 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002317 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002318 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002319 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002320 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002321
2322 bc_num_expand(d, c->len);
2323 bc_num_init(&base, c->len);
2324 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002325 bc_num_init(&temp, b->len);
2326
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002327 two.cap = ARRAY_SIZE(two_digs);
2328 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002329 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002330 two_digs[0] = 2;
2331
Gavin Howard01055ba2018-11-03 11:00:21 -06002332 bc_num_one(d);
2333
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002334 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002335 if (s) goto err;
2336 bc_num_copy(&exp, b);
2337
2338 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002339 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002340 if (s) goto err;
2341
2342 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002343 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002345 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002346 if (s) goto err;
2347 }
2348
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002349 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002350 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002351 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002352 if (s) goto err;
2353 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002354 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002355 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002356 bc_num_free(&exp);
2357 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002358 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002360#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002361#endif // ENABLE_DC
2362
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002363static FAST_FUNC void bc_string_free(void *string)
2364{
2365 free(*(char**)string);
2366}
2367
Gavin Howard01055ba2018-11-03 11:00:21 -06002368static void bc_func_init(BcFunc *f)
2369{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002370 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002371 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2372 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002373 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2374 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002375 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002376}
2377
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002378static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002379{
2380 BcFunc *f = (BcFunc *) func;
2381 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002382 IF_BC(bc_vec_free(&f->labels);)
2383 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002384 IF_BC(bc_vec_free(&f->strs);)
2385 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002386}
2387
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002388static void bc_array_expand(BcVec *a, size_t len);
2389
Gavin Howard01055ba2018-11-03 11:00:21 -06002390static void bc_array_init(BcVec *a, bool nums)
2391{
2392 if (nums)
2393 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2394 else
2395 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2396 bc_array_expand(a, 1);
2397}
2398
Gavin Howard01055ba2018-11-03 11:00:21 -06002399static void bc_array_expand(BcVec *a, size_t len)
2400{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002401 if (a->dtor == bc_num_free
2402 // && a->size == sizeof(BcNum) - always true
2403 ) {
2404 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002405 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002406 bc_num_init_DEF_SIZE(&n);
2407 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002408 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002409 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002410 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002411 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002412 bc_array_init(&v, true);
2413 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002414 }
2415 }
2416}
2417
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002418static void bc_array_copy(BcVec *d, const BcVec *s)
2419{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002420 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002421 size_t i;
2422
2423 bc_vec_pop_all(d);
2424 bc_vec_expand(d, s->cap);
2425 d->len = s->len;
2426
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002427 dnum = (void*)d->v;
2428 snum = (void*)s->v;
2429 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002430 bc_num_init(dnum, snum->len);
2431 bc_num_copy(dnum, snum);
2432 }
2433}
2434
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002435#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002436static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002437{
2438 d->t = src->t;
2439
2440 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002441 case XC_RESULT_TEMP:
2442 case XC_RESULT_IBASE:
2443 case XC_RESULT_SCALE:
2444 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002445 bc_num_init(&d->d.n, src->d.n.len);
2446 bc_num_copy(&d->d.n, &src->d.n);
2447 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002448 case XC_RESULT_VAR:
2449 case XC_RESULT_ARRAY:
2450 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002451 d->d.id.name = xstrdup(src->d.id.name);
2452 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002453 case XC_RESULT_CONSTANT:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002454 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002455 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2456 break;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002457 default: // placate compiler
2458 // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen
2459 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002460 }
2461}
2462#endif // ENABLE_DC
2463
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002464static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002465{
2466 BcResult *r = (BcResult *) result;
2467
2468 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002469 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002470 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002471 case XC_RESULT_IBASE:
2472 case XC_RESULT_SCALE:
2473 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002474 bc_num_free(&r->d.n);
2475 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002476 case XC_RESULT_VAR:
2477 case XC_RESULT_ARRAY:
2478 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002479 free(r->d.id.name);
2480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002481 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002482 // Do nothing.
2483 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002484 }
2485}
2486
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002487static int bad_input_byte(char c)
2488{
2489 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2490 || c > 0x7e
2491 ) {
2492 bc_error_fmt("illegal character 0x%02x", c);
2493 return 1;
2494 }
2495 return 0;
2496}
2497
Denys Vlasenko10bde142018-12-27 18:23:58 +01002498static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002499{
2500 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002501 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002502 fflush_and_check();
2503
Denys Vlasenko14767602018-12-27 22:52:13 +01002504#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002505 if (G_interrupt) { // ^C was pressed
2506 intr:
2507 if (fp != stdin) {
2508 // ^C while running a script (bc SCRIPT): die.
2509 // We do not return to interactive prompt:
2510 // user might be running us from a shell,
2511 // and SCRIPT might be intended to terminate
2512 // (e.g. contain a "halt" stmt).
2513 // ^C dropping user into a bc prompt instead of
2514 // the shell would be unexpected.
2515 xfunc_die();
2516 }
2517 // ^C while interactive input
2518 G_interrupt = 0;
2519 // GNU bc says "interrupted execution."
2520 // GNU dc says "Interrupt!"
2521 fputs("\ninterrupted execution\n", stderr);
2522 }
2523
2524# if ENABLE_FEATURE_EDITING
2525 if (G_ttyin && fp == stdin) {
2526 int n, i;
2527# define line_buf bb_common_bufsiz1
2528 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2529 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2530 if (n == 0) // ^C
2531 goto intr;
2532 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2533 return;
2534 }
2535 i = 0;
2536 for (;;) {
2537 char c = line_buf[i++];
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002538 if (c == '\0') break;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002539 if (bad_input_byte(c)) goto again;
2540 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002541 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002542# undef line_buf
2543 } else
2544# endif
2545#endif
2546 {
2547 int c;
2548 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002549
2550 do {
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002551 get_char:
Denys Vlasenko14767602018-12-27 22:52:13 +01002552#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002553 if (G_interrupt) {
2554 // ^C was pressed: ignore entire line, get another one
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002555 goto again;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002556 }
2557#endif
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002558 c = fgetc(fp);
2559 if (c == '\0')
2560 goto get_char;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002561 if (c == EOF) {
2562 if (ferror(fp))
2563 bb_perror_msg_and_die("input error");
2564 // Note: EOF does not append '\n'
2565 break;
2566 }
2567 bad_chars |= bad_input_byte(c);
2568 bc_vec_pushByte(vec, (char)c);
2569 } while (c != '\n');
2570
2571 if (bad_chars) {
2572 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002573 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002574 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002575 goto again;
2576 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002577 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002578 }
2579 bc_vec_pushZeroByte(vec);
2580 }
2581}
2582
2583//
2584// Parsing routines
2585//
2586
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002587// "Input numbers may contain the characters 0-9 and A-Z.
2588// (Note: They must be capitals. Lower case letters are variable names.)
2589// Single digit numbers always have the value of the digit regardless of
2590// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2591// all input digits greater or equal to ibase to the value of ibase-1.
2592// This makes the number ZZZ always be the largest 3 digit number of the
2593// input base."
2594static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002595{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002596 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002597 for (;;) {
2598 BcDig c = *val++;
2599 if (c == '\0')
2600 break;
2601 if (c == '.') {
2602 if (radix) return false;
2603 radix = true;
2604 continue;
2605 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002606 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002607 return false;
2608 }
2609 return true;
2610}
2611
2612// Note: n is already "bc_num_zero()"ed,
2613// leading zeroes in "val" are removed
2614static void bc_num_parseDecimal(BcNum *n, const char *val)
2615{
2616 size_t len, i;
2617 const char *ptr;
2618
2619 len = strlen(val);
2620 if (len == 0)
2621 return;
2622
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002623 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002624
2625 ptr = strchr(val, '.');
2626
2627 n->rdx = 0;
2628 if (ptr != NULL)
2629 n->rdx = (size_t)((val + len) - (ptr + 1));
2630
2631 for (i = 0; val[i]; ++i) {
2632 if (val[i] != '0' && val[i] != '.') {
2633 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002634 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002635 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002636 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002637 if (c > 9) { // A-Z => 10-36
2638 n->len = 2;
2639 c -= ('A' - '9' - 1);
2640 n->num[1] = c/10;
2641 c = c%10;
2642 }
2643 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002644 break;
2645 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002646 i = len - 1;
2647 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002648 char c = val[i] - '0';
2649 if (c > 9) // A-Z => 9
2650 c = 9;
2651 n->num[n->len] = c;
2652 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002653 skip_dot:
2654 if (i == 0) break;
2655 if (val[--i] == '.') goto skip_dot;
2656 }
2657 break;
2658 }
2659 }
2660 // if for() exits without hitting if(), the value is entirely zero
2661}
2662
2663// Note: n is already "bc_num_zero()"ed,
2664// leading zeroes in "val" are removed
2665static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2666{
2667 BcStatus s;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002668 BcNum mult, result;
2669 BcNum temp;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002670 BcNum base;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002671 BcDig temp_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002672 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002673 size_t digits;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002674
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002675 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002676
2677 temp.cap = ARRAY_SIZE(temp_digs);
2678 temp.num = temp_digs;
2679
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002680 base.cap = ARRAY_SIZE(base_digs);
2681 base.num = base_digs;
2682 bc_num_ulong2num(&base, base_t);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002683 base_t--;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002684
2685 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002686 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002687 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002688
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002689 c = *val++;
2690 if (c == '\0') goto int_err;
2691 if (c == '.') break;
2692
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002693 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2694 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002695
2696 s = zbc_num_mul(n, &base, &mult, 0);
2697 if (s) goto int_err;
2698 bc_num_ulong2num(&temp, v);
2699 s = zbc_num_add(&mult, &temp, n, 0);
2700 if (s) goto int_err;
2701 }
2702
2703 bc_num_init(&result, base.len);
2704 //bc_num_zero(&result); - already is
2705 bc_num_one(&mult);
2706
2707 digits = 0;
2708 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002709 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002710 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002711
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002712 c = *val++;
2713 if (c == '\0') break;
2714 digits++;
2715
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002716 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2717 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002718
2719 s = zbc_num_mul(&result, &base, &result, 0);
2720 if (s) goto err;
2721 bc_num_ulong2num(&temp, v);
2722 s = zbc_num_add(&result, &temp, &result, 0);
2723 if (s) goto err;
2724 s = zbc_num_mul(&mult, &base, &mult, 0);
2725 if (s) goto err;
2726 }
2727
2728 s = zbc_num_div(&result, &mult, &result, digits);
2729 if (s) goto err;
2730 s = zbc_num_add(n, &result, n, digits);
2731 if (s) goto err;
2732
2733 if (n->len != 0) {
2734 if (n->rdx < digits)
2735 bc_num_extend(n, digits - n->rdx);
2736 } else
2737 bc_num_zero(n);
2738 err:
2739 bc_num_free(&result);
2740 int_err:
2741 bc_num_free(&mult);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002742}
2743
Denys Vlasenko10bde142018-12-27 18:23:58 +01002744static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002745{
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002746 size_t i;
2747
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002748 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002749 RETURN_STATUS(bc_error("bad number string"));
2750
2751 bc_num_zero(n);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002752 while (*val == '0')
2753 val++;
2754 for (i = 0; ; ++i) {
2755 if (val[i] == '\0')
2756 RETURN_STATUS(BC_STATUS_SUCCESS);
2757 if (val[i] != '.' && val[i] != '0')
2758 break;
2759 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002760
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002761 if (base_t == 10 || val[1] == '\0')
2762 // Decimal, or single-digit number
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002763 bc_num_parseDecimal(n, val);
2764 else
2765 bc_num_parseBase(n, val, base_t);
2766
2767 RETURN_STATUS(BC_STATUS_SUCCESS);
2768}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002769#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002770
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002771// p->lex_inbuf points to the current string to be parsed.
2772// if p->lex_inbuf points to '\0', it's either EOF or it points after
2773// last processed line's terminating '\n' (and more reading needs to be done
2774// to get next character).
2775//
2776// If you are in a situation where that is a possibility, call peek_inbuf().
2777// If necessary, it performs more reading and changes p->lex_inbuf,
2778// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2779// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2780// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2781//
2782// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2783// it returns current char and advances the pointer (if not EOF).
2784// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2785//
2786// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2787// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2788// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002789//
2790// bc cases to test interactively:
2791// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2792// 1 #comment/* - prints "1<newline>" at once
2793// 1 #comment" - prints "1<newline>" at once
2794// 1\#comment - error at once (\ is not a line continuation)
2795// 1 + /*"*/2 - prints "3<newline>" at once
2796// 1 + /*#*/2 - prints "3<newline>" at once
2797// "str\" - prints "str\" at once
2798// "str#" - prints "str#" at once
2799// "str/*" - prints "str/*" at once
2800// "str#\ - waits for second line
2801// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002802static char peek_inbuf(void)
2803{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002804 if (*G.prs.lex_inbuf == '\0'
2805 && G.prs.lex_input_fp
2806 ) {
2807 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2808 G.prs.lex_inbuf = G.input_buffer.v;
2809 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2810 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002811 }
2812 return *G.prs.lex_inbuf;
2813}
2814static char eat_inbuf(void)
2815{
2816 char c = peek_inbuf();
2817 if (c) G.prs.lex_inbuf++;
2818 return c;
2819}
2820
Denys Vlasenko10bde142018-12-27 18:23:58 +01002821static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002822{
2823 BcParse *p = &G.prs;
2824 char c;
2825
2826 // Try: echo -n '#foo' | bc
2827 p->lex = XC_LEX_WHITESPACE;
2828
Denys Vlasenko2747f612018-12-31 18:48:10 +01002829 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002830 // '\0' which isn't the EOF can only be seen after '\n'.
2831 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2832 p->lex_inbuf++;
2833}
2834
Denys Vlasenko10bde142018-12-27 18:23:58 +01002835static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002836{
2837 BcParse *p = &G.prs;
2838
2839 p->lex = XC_LEX_WHITESPACE;
2840 for (;;) {
2841 // We depend here on input being done in whole lines:
2842 // '\0' which isn't the EOF can only be seen after '\n'.
2843 char c = *p->lex_inbuf;
2844 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2845 break;
2846 if (!isspace(c))
2847 break;
2848 p->lex_inbuf++;
2849 }
2850}
2851
Denys Vlasenko10bde142018-12-27 18:23:58 +01002852static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002853{
2854 BcParse *p = &G.prs;
2855 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002856 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002857
2858 bc_vec_pop_all(&p->lex_strnumbuf);
2859 bc_vec_pushByte(&p->lex_strnumbuf, last);
2860
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002861// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002862// (Note: They must be capitals. Lower case letters are variable names.)
2863// Single digit numbers always have the value of the digit regardless of
2864// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2865// all input digits greater or equal to ibase to the value of ibase-1.
2866// This makes the number ZZZ always be the largest 3 digit number of the
2867// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002868// dc only allows A-F, the rules about single-char and multi-char are the same.
2869 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002870 pt = (last == '.');
2871 p->lex = XC_LEX_NUMBER;
2872 for (;;) {
2873 // We depend here on input being done in whole lines:
2874 // '\0' which isn't the EOF can only be seen after '\n'.
2875 char c = *p->lex_inbuf;
2876 check_c:
2877 if (c == '\0')
2878 break;
2879 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2880 p->lex_inbuf += 2;
2881 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01002882 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002883 c = peek_inbuf(); // force next line to be read
2884 goto check_c;
2885 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002886 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002887 if (c != '.') break;
2888 // if '.' was already seen, stop on second one:
2889 if (pt) break;
2890 pt = true;
2891 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002892 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002893 last = c;
2894 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2895 p->lex_inbuf++;
2896 }
2897 if (last == '.') // remove trailing '.' if any
2898 bc_vec_pop(&p->lex_strnumbuf);
2899 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2900
2901 G.err_line = G.prs.lex_line;
2902 RETURN_STATUS(BC_STATUS_SUCCESS);
2903}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002904#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002905
Denys Vlasenko10bde142018-12-27 18:23:58 +01002906static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002907{
2908 BcParse *p = &G.prs;
2909 size_t i;
2910 const char *buf;
2911
2912 p->lex = XC_LEX_NAME;
2913
2914 // Since names can't cross lines with \<newline>,
2915 // we depend on the fact that whole line is in the buffer
2916 i = 0;
2917 buf = p->lex_inbuf - 1;
2918 for (;;) {
2919 char c = buf[i];
2920 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2921 i++;
2922 }
2923
2924#if 0 // We do not protect against people with gigabyte-long names
2925 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2926 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2927 if (i > BC_MAX_STRING)
2928 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2929 }
2930#endif
2931 bc_vec_string(&p->lex_strnumbuf, i, buf);
2932
2933 // Increment the index. We minus 1 because it has already been incremented.
2934 p->lex_inbuf += i - 1;
2935
2936 //return BC_STATUS_SUCCESS;
2937}
2938
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002939IF_BC(static BC_STATUS zbc_lex_token(void);)
2940IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002941#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2942#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002943
Denys Vlasenko10bde142018-12-27 18:23:58 +01002944static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002945{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002946 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002947 BcStatus s;
2948
Denys Vlasenko22314682019-01-01 21:50:14 +01002949 G.err_line = p->lex_line;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002950 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002951//why?
2952// if (p->lex_last == XC_LEX_EOF)
2953// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002954
2955 // Loop until failure or we don't have whitespace. This
2956 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002957 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002958 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002959 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002960 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002961 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002962 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002963 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002964 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002965 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002966 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002967 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2968 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002969 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002970 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002971 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002972 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002973 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002974 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002975 } while (!s && p->lex == XC_LEX_WHITESPACE);
2976 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002977
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002978 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002979}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002980#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002981
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002982#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002983static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002984{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002985 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002986 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002987 RETURN_STATUS(BC_STATUS_SUCCESS);
2988}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002989#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002990
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002991static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002992{
2993 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01002994 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002995 if (s) RETURN_STATUS(s);
2996 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002997 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002998 RETURN_STATUS(s);
2999}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003000#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003001
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003002static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003003{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003004 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003005 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003006 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003007 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003008
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003009 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3010 const char *keyword8 = bc_lex_kws[i].name8;
3011 unsigned j = 0;
3012 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3013 j++;
3014 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003015 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003016 if (keyword8[j] != '\0')
3017 continue;
3018 match:
3019 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003020 if (isalnum(buf[j]) || buf[j]=='_')
3021 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003022 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003023 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003024 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3025 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003026 }
3027
3028 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003029 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003030 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003031 }
3032
Denys Vlasenko10bde142018-12-27 18:23:58 +01003033 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003034 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003035
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003036 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003037 // Prevent this:
3038 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003039 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003040 // '
3041 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003042 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003043 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003044
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003045 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003046}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003047#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003048
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003049static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003050{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003051 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003052
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003053 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003054 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003055 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003056 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003057 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003058 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003059 }
3060 if (c == '"')
3061 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003062 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003063 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003064 dbg_lex("++p->lex_line=%zd", p->lex_line);
3065 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003066 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3067 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003068 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003069 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3070 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003071
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003072 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003073 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003074}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003075#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003076
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003077static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003078{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003079 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003080 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003081 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003082 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003083 with_and_without >>= 8; // store "with" value
3084 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003085 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003086}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003087#define parse_lex_by_checking_eq_sign(with, without) \
3088 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003089
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003090static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003091{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003092 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003093
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003094 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003095 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003096 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003097 char c;
3098
3099 p->lex_inbuf++;
3100 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003101 check_star:
3102 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003103 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003104 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003105 if (c == '/')
3106 break;
3107 goto check_star;
3108 }
3109 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003110 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003111 }
Denys Vlasenko22314682019-01-01 21:50:14 +01003112 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003113 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003114 dbg_lex("++p->lex_line=%zd", p->lex_line);
3115 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003117 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003118
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003119 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003120 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003121}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003122#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003123
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003124#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003125static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003126{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003127 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003129 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003130 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003131
3132 // This is the workhorse of the lexer.
3133 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003134// case '\0': // probably never reached
3135// p->lex_inbuf--;
3136// p->lex = XC_LEX_EOF;
3137// break;
3138 case '\n':
3139 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003140 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003141 p->lex = XC_LEX_NLINE;
3142 break;
3143 case '\t':
3144 case '\v':
3145 case '\f':
3146 case '\r':
3147 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003148 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003149 break;
3150 case '!':
3151 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3152 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3153 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003154 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003155 }
3156 break;
3157 case '"':
3158 s = zbc_lex_string();
3159 break;
3160 case '#':
3161 s = zbc_POSIX_does_not_allow("'#' script comments");
3162 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003163 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003164 break;
3165 case '%':
3166 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3167 break;
3168 case '&':
3169 c2 = *p->lex_inbuf;
3170 if (c2 == '&') {
3171 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3172 if (s) RETURN_STATUS(s);
3173 p->lex_inbuf++;
3174 p->lex = BC_LEX_OP_BOOL_AND;
3175 } else {
3176 p->lex = XC_LEX_INVALID;
3177 s = bc_error_bad_character('&');
3178 }
3179 break;
3180 case '(':
3181 case ')':
3182 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3183 break;
3184 case '*':
3185 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3186 break;
3187 case '+':
3188 c2 = *p->lex_inbuf;
3189 if (c2 == '+') {
3190 p->lex_inbuf++;
3191 p->lex = BC_LEX_OP_INC;
3192 } else
3193 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3194 break;
3195 case ',':
3196 p->lex = BC_LEX_COMMA;
3197 break;
3198 case '-':
3199 c2 = *p->lex_inbuf;
3200 if (c2 == '-') {
3201 p->lex_inbuf++;
3202 p->lex = BC_LEX_OP_DEC;
3203 } else
3204 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3205 break;
3206 case '.':
3207 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003208 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003209 else {
3210 p->lex = BC_LEX_KEY_LAST;
3211 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3212 }
3213 break;
3214 case '/':
3215 c2 = *p->lex_inbuf;
3216 if (c2 == '*')
3217 s = zbc_lex_comment();
3218 else
3219 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3220 break;
3221 case '0':
3222 case '1':
3223 case '2':
3224 case '3':
3225 case '4':
3226 case '5':
3227 case '6':
3228 case '7':
3229 case '8':
3230 case '9':
3231 case 'A':
3232 case 'B':
3233 case 'C':
3234 case 'D':
3235 case 'E':
3236 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003237 case 'G':
3238 case 'H':
3239 case 'I':
3240 case 'J':
3241 case 'K':
3242 case 'L':
3243 case 'M':
3244 case 'N':
3245 case 'O':
3246 case 'P':
3247 case 'Q':
3248 case 'R':
3249 case 'S':
3250 case 'T':
3251 case 'U':
3252 case 'V':
3253 case 'W':
3254 case 'X':
3255 case 'Y':
3256 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003257 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003258 break;
3259 case ';':
3260 p->lex = BC_LEX_SCOLON;
3261 break;
3262 case '<':
3263 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3264 break;
3265 case '=':
3266 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3267 break;
3268 case '>':
3269 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3270 break;
3271 case '[':
3272 case ']':
3273 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3274 break;
3275 case '\\':
3276 if (*p->lex_inbuf == '\n') {
3277 p->lex = XC_LEX_WHITESPACE;
3278 p->lex_inbuf++;
3279 } else
3280 s = bc_error_bad_character(c);
3281 break;
3282 case '^':
3283 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3284 break;
3285 case 'a':
3286 case 'b':
3287 case 'c':
3288 case 'd':
3289 case 'e':
3290 case 'f':
3291 case 'g':
3292 case 'h':
3293 case 'i':
3294 case 'j':
3295 case 'k':
3296 case 'l':
3297 case 'm':
3298 case 'n':
3299 case 'o':
3300 case 'p':
3301 case 'q':
3302 case 'r':
3303 case 's':
3304 case 't':
3305 case 'u':
3306 case 'v':
3307 case 'w':
3308 case 'x':
3309 case 'y':
3310 case 'z':
3311 s = zbc_lex_identifier();
3312 break;
3313 case '{':
3314 case '}':
3315 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3316 break;
3317 case '|':
3318 c2 = *p->lex_inbuf;
3319 if (c2 == '|') {
3320 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3321 if (s) RETURN_STATUS(s);
3322 p->lex_inbuf++;
3323 p->lex = BC_LEX_OP_BOOL_OR;
3324 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003325 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003326 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003327 }
3328 break;
3329 default:
3330 p->lex = XC_LEX_INVALID;
3331 s = bc_error_bad_character(c);
3332 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003333 }
3334
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003335 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003336}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003337#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003338#endif // ENABLE_BC
3339
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003340#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003341static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003342{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003343 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003344 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003345 xc_lex_whitespace(); // eats whitespace (but not newline)
3346 p->lex_inbuf++; // xc_lex_name() expects this
3347 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003348 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003349 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003350 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003351 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3352 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003353 }
3354
Denys Vlasenko29301232018-12-11 15:29:32 +01003355 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003357#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003358
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003359static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003360{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003361 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003362 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003363
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003364 p->lex = XC_LEX_STR;
3365 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003366
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003367 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003368 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003369 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003370 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003371 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003372 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003373 if (c == '[') depth++;
3374 if (c == ']')
3375 if (--depth == 0)
3376 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003377 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003378 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003379 dbg_lex("++p->lex_line=%zd", p->lex_line);
3380 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003381 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3382 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003383 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003384 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003385 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003386
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003387 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003388 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003389}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003390#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003391
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003392#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003393static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003394{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003395 static const //BcLexType - should be this type, but narrower type saves size:
3396 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003397 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003398 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 +01003399 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3400 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003401 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003402 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003403
Denys Vlasenko10bde142018-12-27 18:23:58 +01003404 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003405 BcStatus s;
3406 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003407 size_t i;
3408
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003409 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003410 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003411 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003412 }
3413
Denys Vlasenko81293c82018-12-24 01:53:55 +01003414 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003415 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003416 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003417 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003418 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003419 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003420 }
3421
3422 // This is the workhorse of the lexer.
3423 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003424// case '\0': // probably never reached
3425// p->lex = XC_LEX_EOF;
3426// break;
3427 case '\n':
3428 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3429 // (and "case '\n':" is not just empty here)
3430 // only to allow interactive dc have a way to exit
3431 // "parse" stage of "parse,execute" loop
3432 // on <enter>, not on _next_ token (which would mean
3433 // commands are not executed on pressing <enter>).
3434 // IOW: typing "1p<enter>" should print "1" _at once_,
3435 // not after some more input.
3436 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003437 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003438 p->lex = XC_LEX_NLINE;
3439 break;
3440 case '\t':
3441 case '\v':
3442 case '\f':
3443 case '\r':
3444 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003445 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003446 break;
3447 case '!':
3448 c2 = *p->lex_inbuf;
3449 if (c2 == '=')
3450 p->lex = XC_LEX_OP_REL_NE;
3451 else if (c2 == '<')
3452 p->lex = XC_LEX_OP_REL_LE;
3453 else if (c2 == '>')
3454 p->lex = XC_LEX_OP_REL_GE;
3455 else
3456 RETURN_STATUS(bc_error_bad_character(c));
3457 p->lex_inbuf++;
3458 break;
3459 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003460 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003461 break;
3462 case '.':
3463 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003464 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003465 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003466 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003467 break;
3468 case '0':
3469 case '1':
3470 case '2':
3471 case '3':
3472 case '4':
3473 case '5':
3474 case '6':
3475 case '7':
3476 case '8':
3477 case '9':
3478 case 'A':
3479 case 'B':
3480 case 'C':
3481 case 'D':
3482 case 'E':
3483 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003484 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003485 break;
3486 case '[':
3487 s = zdc_lex_string();
3488 break;
3489 default:
3490 p->lex = XC_LEX_INVALID;
3491 s = bc_error_bad_character(c);
3492 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003493 }
3494
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003495 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003496}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003497#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003498#endif // ENABLE_DC
3499
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003500static void xc_parse_push(unsigned i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003501{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003502 BcVec *code = &G.prs.func->code;
3503 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003504 bc_vec_pushByte(code, (uint8_t)i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003505}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003506
Denys Vlasenko10bde142018-12-27 18:23:58 +01003507static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003508{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003509#if 1
3510 BcVec *code = &G.prs.func->code;
3511 size_t pos = code->len;
3512 size_t len = strlen(name) + 1;
3513
3514 bc_vec_expand(code, pos + len);
3515 strcpy(code->v + pos, name);
3516 code->len = pos + len;
3517#else
3518 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003519 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003520 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003521 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003522#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003523}
3524
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003525// Indexes < 0xfc are encoded verbatim, else first byte is
3526// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3527// followed by that many bytes, lsb first.
3528// (The above describes 32-bit case).
3529#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3530
Denys Vlasenko10bde142018-12-27 18:23:58 +01003531static void xc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003532{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003533 size_t mask;
3534 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003535
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003536 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003537 if (idx < SMALL_INDEX_LIMIT) {
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003538 xc_parse_push(idx);
3539 return;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003540 }
3541
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003542 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3543 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003544 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003545 if (idx & mask) break;
3546 mask >>= 8;
3547 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003548 }
3549 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003550
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003551 xc_parse_push((SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003552
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003553 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003554 xc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003555 idx >>= 8;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003556 } while (idx != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003557}
3558
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003559static void xc_parse_pushInst_and_Index(unsigned inst, size_t idx)
3560{
3561 xc_parse_push(inst);
3562 xc_parse_pushIndex(idx);
3563}
3564
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003565#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003566static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003567{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003568 xc_parse_pushInst_and_Index(BC_INST_JUMP, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003569}
3570
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003571static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003572{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003573 xc_parse_pushInst_and_Index(BC_INST_JUMP_ZERO, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003574}
3575
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003576static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003577{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003578 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003579 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003580
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003581 xc_parse_pushInst_and_Index(XC_INST_STR, p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003582 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003583
Denys Vlasenko10bde142018-12-27 18:23:58 +01003584 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003585}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003586#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003587#endif
3588
Denys Vlasenko10bde142018-12-27 18:23:58 +01003589static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003590{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003591 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003592 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003593#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003594 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003595#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003596 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003597#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003598 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003599#endif
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003600 xc_parse_pushInst_and_Index(XC_INST_NUM, idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003601}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003602
Denys Vlasenko10bde142018-12-27 18:23:58 +01003603static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003604{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003605 G.prs.func = xc_program_func(G.prs.fidx);
3606 G.prs.lex_inbuf = text;
3607 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3608 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003609}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003610#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003611
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003612// Called when parsing or execution detects a failure,
3613// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003614static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003615{
3616 BcFunc *f;
3617 BcInstPtr *ip;
3618
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003619 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003620 bc_vec_pop_all(&G.prog.results);
3621
Denys Vlasenko10bde142018-12-27 18:23:58 +01003622 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003623 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003624 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003625}
3626
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003627// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003628// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003629static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003630{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003631 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003632 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003633 bc_func_free(p->func);
3634 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003635
Denys Vlasenko65e10462018-12-19 15:13:14 +01003636 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003637 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003638 }
3639
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003640 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003641 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003642
Denys Vlasenko503faf92018-12-20 16:24:18 +01003643 IF_BC(bc_vec_pop_all(&p->exits);)
3644 IF_BC(bc_vec_pop_all(&p->conds);)
3645 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003646
Denys Vlasenko10bde142018-12-27 18:23:58 +01003647 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003648}
3649
Denys Vlasenko10bde142018-12-27 18:23:58 +01003650static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003651{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003652 IF_BC(bc_vec_free(&G.prs.exits);)
3653 IF_BC(bc_vec_free(&G.prs.conds);)
3654 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003655 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003656}
3657
Denys Vlasenko10bde142018-12-27 18:23:58 +01003658static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003659{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003660 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003661 memset(p, 0, sizeof(BcParse));
3662
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003663 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003664 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3665 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3666 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003667
Denys Vlasenkofa210792018-12-19 19:35:40 +01003668 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003669 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003670}
3671
Denys Vlasenko10bde142018-12-27 18:23:58 +01003672static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003673{
Denys Vlasenko04715442018-12-21 00:10:26 +01003674 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003675 BcFunc f;
3676 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003677 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003678 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003679 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003680}
3681
3682#if ENABLE_BC
3683
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003684// Note: takes ownership of 'name' (must be malloced)
3685static size_t bc_program_addFunc(char *name)
3686{
3687 size_t idx;
3688 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003689 int inserted;
3690
3691 entry.name = name;
3692 entry.idx = G.prog.fns.len;
3693
3694 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3695 if (!inserted) free(name);
3696
3697 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3698 idx = entry_ptr->idx;
3699
3700 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003701 // There is already a function with this name.
3702 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003703 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003704 bc_func_free(func);
3705 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003706 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003707 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003708 }
3709
3710 return idx;
3711}
3712
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003713#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003714// We can calculate the conversion between tokens and exprs by subtracting the
3715// position of the first operator in the lex enum and adding the position of the
3716// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003717#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003718
Denys Vlasenko132d7c02019-01-08 19:29:35 +01003719static BC_STATUS zbc_parse_expr(uint8_t flags);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003720#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003721
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003722static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003723#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003724
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003725static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003726{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003727 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003728}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003729#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003730
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003731static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003732{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003733 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003734 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3735 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003736 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003737 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003738 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003739 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003740
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003741 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003742}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003743#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003744
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003745static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003746{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003747 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003748 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3749 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003750
3751 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003752 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003753 if (t == BC_LEX_LPAREN) break;
3754
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003755 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003756 if (l >= r && (l != r || !left)) break;
3757
Denys Vlasenko10bde142018-12-27 18:23:58 +01003758 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003759 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003760 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003761 }
3762
3763 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003764}
3765
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003766static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003767{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003768 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003769 BcLexType top;
3770
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003771 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003772 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003773 top = BC_PARSE_TOP_OP(p);
3774
3775 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003776 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003777
3778 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003779 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003780
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003781 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003782 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003783 top = BC_PARSE_TOP_OP(p);
3784 }
3785
3786 bc_vec_pop(&p->ops);
3787
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003788 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003789}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003790#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003791
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003792static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003793{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003794 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003795 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003796 size_t nparams;
3797
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003798 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003799 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3800
Denys Vlasenko10bde142018-12-27 18:23:58 +01003801 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003802 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003803
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003804 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003805 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003806 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003807 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003808 if (s) RETURN_STATUS(s);
3809 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003810 if (p->lex != BC_LEX_COMMA) {
3811 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003812 break;
3813 RETURN_STATUS(bc_error_bad_token());
3814 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003815 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003816 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 }
3818 }
3819
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003820 xc_parse_pushInst_and_Index(BC_INST_CALL, nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003821
Denys Vlasenko26819db2018-12-12 16:08:46 +01003822 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003823}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003824#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003825
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003826// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003827static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003828{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003829 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003830 BcStatus s;
3831 BcId entry, *entry_ptr;
3832 size_t idx;
3833
3834 entry.name = name;
3835
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003836 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003837 if (s) goto err;
3838
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003839 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003840 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003841 goto err;
3842 }
3843
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003844 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003845
3846 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003847 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003848 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003849 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003850 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003851 free(name);
3852
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003853 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003854 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003855
Denys Vlasenko10bde142018-12-27 18:23:58 +01003856 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003857 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003858 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003859 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003860}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003861#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003862
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003863static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003864{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003865 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003866 BcStatus s;
3867 char *name;
3868
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003869 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003870 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003871 if (s) goto err;
3872
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003873 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003874 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003875 if (s) goto err;
3876
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003877 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003879 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003880 goto err;
3881 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003882 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003883 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003884 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003886 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 if (s) goto err;
3888 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003889 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003890 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003891 xc_parse_push(*type);
3892 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003893 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003894 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003895 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003896 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 goto err;
3898 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003899 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003900 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003901 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003902 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003903 xc_parse_push(XC_INST_VAR);
3904 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003905 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003906 }
3907
Denys Vlasenko26819db2018-12-12 16:08:46 +01003908 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003909 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003910 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003911 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003912}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003913#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003914
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003915static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003916{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003917 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003918 BcStatus s;
3919
Denys Vlasenko10bde142018-12-27 18:23:58 +01003920 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003921 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003922 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003923
Denys Vlasenko10bde142018-12-27 18:23:58 +01003924 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003925 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003926 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003927
Denys Vlasenko10bde142018-12-27 18:23:58 +01003928 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003929
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003930 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003931}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003932#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003933
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003934static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003935{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003936 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003937 BcStatus s;
3938
Denys Vlasenko10bde142018-12-27 18:23:58 +01003939 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003940 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003941 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003942
3943 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3944
Denys Vlasenko10bde142018-12-27 18:23:58 +01003945 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003946 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003947
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003948 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003949 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003951 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003952
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003953 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003954 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003955
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003956 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003957}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003958#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003959
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003960static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003961{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003962 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003963 BcStatus s;
3964
Denys Vlasenko10bde142018-12-27 18:23:58 +01003965 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003966 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003967
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003968 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003969 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003970 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003971 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003972 }
3973
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003974 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003975 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3976
Denys Vlasenko10bde142018-12-27 18:23:58 +01003977 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003978 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003979
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003980 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003981 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003982 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003983 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01003984 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003985
Denys Vlasenko10bde142018-12-27 18:23:58 +01003986 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003987}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003988#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003989
Denys Vlasenko22314682019-01-01 21:50:14 +01003990static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003991{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003992 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003993 BcStatus s;
3994 BcLexType type;
3995 char inst;
3996 BcInst etype = *prev;
3997
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003998 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3999 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4000 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004001 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004002 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004003 xc_parse_push(inst);
4004 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004005 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004006 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004007
Denys Vlasenko10bde142018-12-27 18:23:58 +01004008 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004009 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004010 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
4012 // Because we parse the next part of the expression
4013 // right here, we need to increment this.
Denys Vlasenko22314682019-01-01 21:50:14 +01004014 *nexs = *nexs + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004015
4016 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004017 case XC_LEX_NAME:
4018 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
4019 break;
4020 case BC_LEX_KEY_IBASE:
4021 case BC_LEX_KEY_LAST:
4022 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004023 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4024 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004025 break;
4026 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004027 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004028 if (s) RETURN_STATUS(s);
4029 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004030 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004031 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01004032 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004033 break;
4034 default:
4035 s = bc_error_bad_token();
4036 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004037 }
4038
Denys Vlasenko10bde142018-12-27 18:23:58 +01004039 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004040 }
4041
Denys Vlasenko26819db2018-12-12 16:08:46 +01004042 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004043}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004044#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004045
Denys Vlasenko22314682019-01-01 21:50:14 +01004046static int bc_parse_inst_isLeaf(BcInst p)
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004047{
4048 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4049 || p == BC_INST_INC_POST
4050 || p == BC_INST_DEC_POST
4051 ;
4052}
Denys Vlasenko22314682019-01-01 21:50:14 +01004053#define BC_PARSE_LEAF(prev, bin_last, rparen) \
4054 (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev)))
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004055
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004056static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
Denys Vlasenko22314682019-01-01 21:50:14 +01004057 bool rparen, bool bin_last, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004058{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004059 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004060 BcStatus s;
4061 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004062
Denys Vlasenko10bde142018-12-27 18:23:58 +01004063 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004064 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004065
Denys Vlasenko22314682019-01-01 21:50:14 +01004066 type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004067 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004068
4069 // We can just push onto the op stack because this is the largest
4070 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004071 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004072 bc_vec_push(&p->ops, &type);
4073 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004074 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004075
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004076 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004077}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004078#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004079
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004080static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004081{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004082 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004083 BcStatus s;
4084 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004085
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004086 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004087 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004088 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004089 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004090 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004091 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004092 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004093 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004094 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004095 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004096 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004097 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004098 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004099 }
4100
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004101 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004102}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004103#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004104
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004105static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004106{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004107 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004108 BcStatus s;
4109 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004110
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004111 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004112 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004113 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004114
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004115 t = p->lex;
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004116 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004117 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004118 else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004119//TODO: if (p->func->voidfunc) ERROR
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004120 s = zbc_parse_expr(0);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004121 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004122
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004123 if (t != BC_LEX_LPAREN // "return EXPR", no ()
4124 || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b"
4125 ) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004126 s = zbc_POSIX_requires("parentheses around return expressions");
4127 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004128 }
4129
Denys Vlasenko10bde142018-12-27 18:23:58 +01004130 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004131 }
4132
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004133 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004134 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004135}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004136#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004137
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004138static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004139{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004140 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004141 size_t *label = bc_vec_item(&p->func->labels, idx);
4142 *label = p->func->code.len;
4143}
4144
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004145static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004146{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004147 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004148 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004149 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004150
4151 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004152 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004153 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004154 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004155
Denys Vlasenko10bde142018-12-27 18:23:58 +01004156 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004157 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004158 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004160 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004161
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004162 // Encode "if zero, jump to ..."
4163 // Pushed value (destination of the jump) is uninitialized,
4164 // will be rewritten to be address of "end of if()" or of "else".
4165 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004166 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004167
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004168 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004169 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004170
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004171 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4172 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004173 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004174
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004175 // Encode "after then_stmt, jump to end of if()"
4176 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4177 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004178 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004179
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004180 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 +01004181 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004182
Denys Vlasenko15850832018-12-16 21:40:54 +01004183 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004184
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004185 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004186 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004187 }
4188
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004189 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 +01004190 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004191
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004192 dbg_lex_done("%s:%d done", __func__, __LINE__);
4193 RETURN_STATUS(s);
4194}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004195#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004196
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004197static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004198{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004199 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004200 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004201 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004202 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004203
Denys Vlasenko10bde142018-12-27 18:23:58 +01004204 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004205 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004206 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004207 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004208 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004209
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004210 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004211 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004212 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004213
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004214 bc_vec_push(&p->exits, &ip_idx);
4215 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004216
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004217 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004218 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004219 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004220
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004221 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004222
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004223 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004224 if (s) RETURN_STATUS(s);
4225
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004226 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004227 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004228
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004229 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004230 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004231
4232 bc_vec_pop(&p->exits);
4233 bc_vec_pop(&p->conds);
4234
4235 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004236}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004237#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004238
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004239static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004240{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004241 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004242 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004243 size_t cond_idx, exit_idx, body_idx, update_idx;
4244
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004245 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004246 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004247 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004248 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004249 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004250 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004251
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004252 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004253 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004254 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004255 if (s) RETURN_STATUS(s);
4256 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004257 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4258 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004259 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004260
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004261 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004262 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004263 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004264
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004265 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004266 update_idx = cond_idx + 1;
4267 body_idx = update_idx + 1;
4268 exit_idx = body_idx + 1;
4269
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004270 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004271 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004272 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004273 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004274 // This is safe to set because the current token is a semicolon,
4275 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004276 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004277 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004278 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004279 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004280 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004281
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004282 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004283
Denys Vlasenko10bde142018-12-27 18:23:58 +01004284 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004285 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004286
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004287 bc_parse_pushJUMP_ZERO(exit_idx);
4288 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004289
Gavin Howard01055ba2018-11-03 11:00:21 -06004290 bc_vec_push(&p->conds, &update_idx);
4291 bc_vec_push(&p->func->labels, &p->func->code.len);
4292
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004293 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004294 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004295 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004296 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004297 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004298 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004299 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4300 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004301 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004302
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004303 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004304 bc_vec_push(&p->func->labels, &p->func->code.len);
4305
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004306 bc_vec_push(&p->exits, &exit_idx);
4307 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004308
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004309 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004310 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004311
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004312 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004313 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004314
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004315 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004316 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004317
4318 bc_vec_pop(&p->exits);
4319 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004320
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004321 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004322}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004323#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004324
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004325static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004326{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004327 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004328 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004329
Gavin Howard01055ba2018-11-03 11:00:21 -06004330 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004331 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4332 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004333 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004334 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004335 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004336 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004337 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004338
Denys Vlasenko10bde142018-12-27 18:23:58 +01004339 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004340}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004341#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004342
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004343static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4344{
4345 BcId *autoid;
4346 BcId a;
4347 size_t i;
4348
4349 autoid = (void*)f->autos.v;
4350 for (i = 0; i < f->autos.len; i++, autoid++) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004351 if (strcmp(name, autoid->name) == 0
4352 && var == autoid->idx
4353 ) {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004354 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko22314682019-01-01 21:50:14 +01004355 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004356 }
4357
4358 a.idx = var;
4359 a.name = name;
4360
4361 bc_vec_push(&f->autos, &a);
4362
4363 RETURN_STATUS(BC_STATUS_SUCCESS);
4364}
4365#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4366
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004367static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004368{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004369 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004370 BcStatus s;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004371 bool var, comma, voidfunc;
Gavin Howard01055ba2018-11-03 11:00:21 -06004372 char *name;
4373
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004374 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004375 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004376 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004377 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004378 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004379
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004380 // To be maximally both POSIX and GNU-compatible,
4381 // "void" is not treated as a normal keyword:
4382 // you can have variable named "void", and even a function
4383 // named "void": "define void() { return 6; }" is ok.
4384 // _Only_ "define void f() ..." syntax treats "void"
4385 // specially.
4386 voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004387
Denys Vlasenko10bde142018-12-27 18:23:58 +01004388 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004389 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004390
4391 voidfunc = (voidfunc && p->lex == XC_LEX_NAME);
4392 if (voidfunc) {
4393 s = zxc_lex_next();
4394 if (s) RETURN_STATUS(s);
4395 }
4396
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004397 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko22314682019-01-01 21:50:14 +01004398 RETURN_STATUS(bc_error_bad_function_definition());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004399
4400 p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v));
4401 p->func = xc_program_func(p->fidx);
4402 p->func->voidfunc = voidfunc;
4403
Denys Vlasenko10bde142018-12-27 18:23:58 +01004404 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004405 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004406
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004407 comma = false;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004408 while (p->lex != BC_LEX_RPAREN) {
4409 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004410 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004411
4412 ++p->func->nparams;
4413
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004414 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004415 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004416 if (s) goto err;
4417
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004418 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004419
4420 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004421 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004422 if (s) goto err;
4423
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004424 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004425 s = bc_error_bad_function_definition();
Gavin Howard01055ba2018-11-03 11:00:21 -06004426 goto err;
4427 }
4428
Denys Vlasenko10bde142018-12-27 18:23:58 +01004429 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004430 if (s) goto err;
4431 }
4432
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004433 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004434 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004435 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004436 if (s) goto err;
4437 }
4438
Denys Vlasenko29301232018-12-11 15:29:32 +01004439 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004440 if (s) goto err;
4441 }
4442
Denys Vlasenko22314682019-01-01 21:50:14 +01004443 if (comma) RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004444
Denys Vlasenko10bde142018-12-27 18:23:58 +01004445 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004446 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004447
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004448 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004449 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004450 if (s) RETURN_STATUS(s);
4451 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004452
Denys Vlasenko202dd192018-12-16 17:30:35 +01004453 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004454 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004455 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004456 // GNU bc requires a {} block even if function body has single stmt, enforce this
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004457 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004458 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004459
4460 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004461 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004462 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004463 if (s) RETURN_STATUS(s);
4464
Denys Vlasenko10bde142018-12-27 18:23:58 +01004465 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004466
4467 // Subsequent code generation is into main program
4468 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004469 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004470
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004471 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004472 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004473 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004474 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004475 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004476 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004477}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004478#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004479
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004480static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004481{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004482 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004483 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004484 char *name;
4485
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004486 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004487 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004488 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004489
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004490 for (;;) {
4491 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004492
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004493 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004494 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004495
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004496 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004497 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004498 if (s) goto err;
4499
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004500 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004501 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004502 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004503 if (s) goto err;
4504
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004505 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004506 s = bc_error_at("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004507 goto err;
4508 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004509 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004510 if (s) goto err;
4511 }
4512
Denys Vlasenko29301232018-12-11 15:29:32 +01004513 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004514 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004515
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004516 if (p->lex == XC_LEX_NLINE
4517 || p->lex == BC_LEX_SCOLON
4518 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004519 ) {
4520 break;
4521 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004522 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko22314682019-01-01 21:50:14 +01004523 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004524 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004525 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004526 }
4527
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004528 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004529 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004530 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004531 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004532 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004533 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004534}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004535#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004536
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004537#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004538static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004539{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004540 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 BcStatus s = BC_STATUS_SUCCESS;
4542
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004543 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004544
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004545 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004546 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004547 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004548 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004549 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004550 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004551 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004552 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004553
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004554 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004555 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4556 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004557 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004558 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004559 } while (p->lex == XC_LEX_NLINE);
4560 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004561 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004562 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004563 if (s) RETURN_STATUS(s);
4564 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004565 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004566 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004567 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004568 if (s) RETURN_STATUS(s);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004569 // Check that next token is a correct stmt delimiter -
4570 // disallows "print 1 print 2" and such.
4571 if (p->lex == BC_LEX_RBRACE)
4572 break;
4573 if (p->lex != BC_LEX_SCOLON
4574 && p->lex != XC_LEX_NLINE
4575 ) {
4576 RETURN_STATUS(bc_error_at("bad statement terminator"));
4577 }
4578 s = zxc_lex_next();
4579 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004580 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004581 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004582 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4583 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 }
4585
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004586 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4587 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004588 case XC_LEX_OP_MINUS:
4589 case BC_LEX_OP_INC:
4590 case BC_LEX_OP_DEC:
4591 case BC_LEX_OP_BOOL_NOT:
4592 case BC_LEX_LPAREN:
4593 case XC_LEX_NAME:
4594 case XC_LEX_NUMBER:
4595 case BC_LEX_KEY_IBASE:
4596 case BC_LEX_KEY_LAST:
4597 case BC_LEX_KEY_LENGTH:
4598 case BC_LEX_KEY_OBASE:
4599 case BC_LEX_KEY_READ:
4600 case BC_LEX_KEY_SCALE:
4601 case BC_LEX_KEY_SQRT:
4602 s = zbc_parse_expr(BC_PARSE_PRINT);
4603 break;
4604 case XC_LEX_STR:
4605 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004606 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004607 break;
4608 case BC_LEX_KEY_BREAK:
4609 case BC_LEX_KEY_CONTINUE:
4610 s = zbc_parse_break_or_continue(p->lex);
4611 break;
4612 case BC_LEX_KEY_FOR:
4613 s = zbc_parse_for();
4614 break;
4615 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004616 xc_parse_push(BC_INST_HALT);
4617 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004618 break;
4619 case BC_LEX_KEY_IF:
4620 s = zbc_parse_if();
4621 break;
4622 case BC_LEX_KEY_LIMITS:
4623 // "limits" is a compile-time command,
4624 // the output is produced at _parse time_.
4625 printf(
4626 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4627 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4628 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4629 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
Denys Vlasenkoe05ec6e2019-01-04 16:26:19 +01004630 // "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" - GNU bc does not show this
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004631 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4632 "Number of vars = "BC_MAX_VARS_STR "\n"
4633 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004634 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004635 break;
4636 case BC_LEX_KEY_PRINT:
4637 s = zbc_parse_print();
4638 break;
4639 case BC_LEX_KEY_QUIT:
4640 // "quit" is a compile-time command. For example,
4641 // "if (0 == 1) quit" terminates when parsing the statement,
4642 // not when it is executed
4643 QUIT_OR_RETURN_TO_MAIN;
4644 case BC_LEX_KEY_RETURN:
4645 if (!p->in_funcdef)
4646 RETURN_STATUS(bc_error("'return' not in a function"));
4647 s = zbc_parse_return();
4648 break;
4649 case BC_LEX_KEY_WHILE:
4650 s = zbc_parse_while();
4651 break;
4652 default:
4653 s = bc_error_bad_token();
4654 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004655 }
4656
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004657 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004658 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004659}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004660#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004661
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004662static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004663{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004664 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 BcStatus s;
4666
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004667 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004668//why?
4669// if (p->lex == XC_LEX_EOF)
4670// s = bc_error("end of file");
4671// else
4672 if (p->lex == BC_LEX_KEY_DEFINE) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004673 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004674 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004675 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004676 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004677 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004678 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004679
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004680 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004681 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004682}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004683#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004684
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004685#undef zbc_parse_expr
4686static BC_STATUS zbc_parse_expr(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004687{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004688 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004689 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004690 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004691 unsigned nparens, nrelops;
Denys Vlasenko22314682019-01-01 21:50:14 +01004692 bool paren_first, rprn, assign, bin_last, incdec;
Gavin Howard01055ba2018-11-03 11:00:21 -06004693
Denys Vlasenko17df8822018-12-14 23:00:24 +01004694 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004695 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004696 nparens = nrelops = 0;
Denys Vlasenko22314682019-01-01 21:50:14 +01004697 rprn = assign = incdec = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004698 bin_last = true;
4699
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004700 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004701 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004702 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004703 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004704
4705 if (!lex_allowed_in_bc_expr(t))
4706 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004707
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004708 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004709 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004710 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004711 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004712 case BC_LEX_OP_INC:
4713 case BC_LEX_OP_DEC:
4714 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004715 if (incdec) RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko22314682019-01-01 21:50:14 +01004716 s = zbc_parse_incdec(&prev, &nexprs, flags);
4717 incdec = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004718 rprn = bin_last = false;
4719 //get_token = false; - already is
4720 break;
4721 case XC_LEX_OP_MINUS:
4722 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004723 s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004724 rprn = false;
4725 //get_token = false; - already is
4726 bin_last = (prev == XC_INST_MINUS);
Denys Vlasenko22314682019-01-01 21:50:14 +01004727 if (bin_last) incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004728 break;
4729 case BC_LEX_OP_ASSIGN_POWER:
4730 case BC_LEX_OP_ASSIGN_MULTIPLY:
4731 case BC_LEX_OP_ASSIGN_DIVIDE:
4732 case BC_LEX_OP_ASSIGN_MODULUS:
4733 case BC_LEX_OP_ASSIGN_PLUS:
4734 case BC_LEX_OP_ASSIGN_MINUS:
4735 case BC_LEX_OP_ASSIGN:
4736 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4737 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4738 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4739 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4740 ) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004741 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004742 }
4743 // Fallthrough.
4744 case XC_LEX_OP_POWER:
4745 case XC_LEX_OP_MULTIPLY:
4746 case XC_LEX_OP_DIVIDE:
4747 case XC_LEX_OP_MODULUS:
4748 case XC_LEX_OP_PLUS:
4749 case XC_LEX_OP_REL_EQ:
4750 case XC_LEX_OP_REL_LE:
4751 case XC_LEX_OP_REL_GE:
4752 case XC_LEX_OP_REL_NE:
4753 case XC_LEX_OP_REL_LT:
4754 case XC_LEX_OP_REL_GT:
4755 case BC_LEX_OP_BOOL_NOT:
4756 case BC_LEX_OP_BOOL_OR:
4757 case BC_LEX_OP_BOOL_AND:
4758 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004759 if (t == BC_LEX_OP_BOOL_NOT) {
4760 if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004761 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko22314682019-01-01 21:50:14 +01004762 } else if (prev == XC_INST_BOOL_NOT) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004763 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004764 }
Denys Vlasenko22314682019-01-01 21:50:14 +01004765
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004766 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4767 prev = BC_TOKEN_2_INST(t);
4768 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko22314682019-01-01 21:50:14 +01004769 rprn = incdec = false;
4770 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004771 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4772 break;
4773 case BC_LEX_LPAREN:
4774 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004775 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004776 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004777 bc_vec_push(&p->ops, &t);
4778 nparens++;
4779 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004780 rprn = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004781 break;
4782 case BC_LEX_RPAREN:
4783 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenkoa1698a12019-01-08 19:32:38 +01004784//why?
4785// if (p->lex_last == BC_LEX_LPAREN) {
4786// RETURN_STATUS(bc_error_at("empty expression"));
4787// }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004788 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004789 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004790 if (nparens == 0) {
4791 goto exit_loop;
4792 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004793 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4794 nparens--;
4795 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004796 rprn = true;
4797 bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004798 break;
4799 case XC_LEX_NAME:
4800 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004801 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004802 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004803 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko22314682019-01-01 21:50:14 +01004804 rprn = (prev == BC_INST_CALL);
4805 bin_last = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004806 //get_token = false; - already is
4807 nexprs++;
4808 break;
4809 case XC_LEX_NUMBER:
4810 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004811 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004812 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004813 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004814 prev = XC_INST_NUM;
4815 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004816 rprn = bin_last = false;
4817 nexprs++;
4818 break;
4819 case BC_LEX_KEY_IBASE:
4820 case BC_LEX_KEY_LAST:
4821 case BC_LEX_KEY_OBASE:
4822 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004823 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004824 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004825 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004826 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004827 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004828 rprn = bin_last = false;
4829 nexprs++;
4830 break;
4831 case BC_LEX_KEY_LENGTH:
4832 case BC_LEX_KEY_SQRT:
4833 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004834 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004835 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004836 s = zbc_parse_builtin(t, flags, &prev);
4837 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004838 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004839 nexprs++;
4840 break;
4841 case BC_LEX_KEY_READ:
4842 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004843 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004844 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004845 s = zbc_parse_read();
4846 prev = XC_INST_READ;
4847 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004848 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004849 nexprs++;
4850 break;
4851 case BC_LEX_KEY_SCALE:
4852 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004853 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004854 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004855 s = zbc_parse_scale(&prev, flags);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004856 //get_token = false; - already is
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004857 rprn = bin_last = false;
4858 nexprs++;
4859 break;
4860 default:
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004861 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004862 }
4863
Denys Vlasenkobb116032018-12-25 01:16:37 +01004864 if (s || G_interrupt) // error, or ^C: stop parsing
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004865 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004866 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004867 s = zxc_lex_next();
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004868 if (s) RETURN_STATUS(s);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004869 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004870 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004871 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004872
4873 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004874 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004875 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004876
4877 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004878 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004879
Denys Vlasenko10bde142018-12-27 18:23:58 +01004880 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004881
Denys Vlasenko69560f42018-12-24 14:14:23 +01004882 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004883 bc_vec_pop(&p->ops);
4884 }
4885
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004886 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004887 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004888
Gavin Howard01055ba2018-11-03 11:00:21 -06004889 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004890 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004891 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004892 if (s) RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004893 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004894 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004895 s = zbc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004896 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 }
4898
4899 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004900 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004901 xc_parse_push(XC_INST_PRINT);
4902 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004903 }
4904
Denys Vlasenko17df8822018-12-14 23:00:24 +01004905 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004906 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004907}
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004908#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004909
Gavin Howard01055ba2018-11-03 11:00:21 -06004910#endif // ENABLE_BC
4911
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004912#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004913
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004914static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004915{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004916 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004917 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004918
Denys Vlasenko10bde142018-12-27 18:23:58 +01004919 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004920 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004921 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004922
Denys Vlasenko10bde142018-12-27 18:23:58 +01004923 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004924
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004925 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004926}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004927#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004928
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004929static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004930{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004931 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004932 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004933 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004934
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004935 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004936
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004937 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01004938 xc_parse_pushInst_and_Index(XC_INST_STR, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004939 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004940
4941 // Explanation needed here
Denys Vlasenko10bde142018-12-27 18:23:58 +01004942 xc_program_add_fn();
4943 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004944
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004945 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004946}
4947
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004948static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004949{
4950 BcStatus s;
4951
Denys Vlasenko10bde142018-12-27 18:23:58 +01004952 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004953 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004954 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004955 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004956 }
4957
4958 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004959 xc_parse_push(DC_INST_SWAP);
4960 xc_parse_push(XC_INST_ASSIGN);
4961 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004962 }
4963
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004964 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004965}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004966#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004967
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004968static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004969{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004970 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004971 BcStatus s;
4972
Denys Vlasenko10bde142018-12-27 18:23:58 +01004973 xc_parse_push(inst);
4974 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004975
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004976 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004977 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004978
Denys Vlasenko10bde142018-12-27 18:23:58 +01004979 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004980 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004982 // Note that 'else' part can not be on the next line:
4983 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4984 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004985 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004986 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004987 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004988 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004989 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004990 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004991 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004992
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004993 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004994}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004995#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004996
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004997static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004998{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004999 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005000 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005001 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005002
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005003 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005004 s = BC_STATUS_SUCCESS;
5005 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005006 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005007 case XC_LEX_OP_REL_EQ:
5008 case XC_LEX_OP_REL_LE:
5009 case XC_LEX_OP_REL_GE:
5010 case XC_LEX_OP_REL_NE:
5011 case XC_LEX_OP_REL_LT:
5012 case XC_LEX_OP_REL_GT:
5013 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
5014 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
5015 get_token = false;
5016 break;
5017 case DC_LEX_SCOLON:
5018 case DC_LEX_COLON:
5019 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
5020 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
5021 break;
5022 case XC_LEX_STR:
5023 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
5024 dc_parse_string();
5025 break;
5026 case XC_LEX_NEG:
5027 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005028 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005029 if (s) RETURN_STATUS(s);
5030 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005031 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005032 xc_parse_pushNUM();
5033 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005034 break;
5035 case XC_LEX_NUMBER:
5036 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005037 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005038 break;
5039 case DC_LEX_READ:
5040 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005041 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005042 break;
5043 case DC_LEX_OP_ASSIGN:
5044 case DC_LEX_STORE_PUSH:
5045 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
5046 assign = (t == DC_LEX_OP_ASSIGN);
5047 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
5048 s = zdc_parse_mem(inst, true, assign);
5049 break;
5050 case DC_LEX_LOAD:
5051 case DC_LEX_LOAD_POP:
5052 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
5053 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
5054 s = zdc_parse_mem(inst, true, false);
5055 break;
5056 case DC_LEX_STORE_IBASE:
5057 case DC_LEX_STORE_SCALE:
5058 case DC_LEX_STORE_OBASE:
5059 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5060 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5061 s = zdc_parse_mem(inst, false, true);
5062 break;
5063 default:
5064 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5065 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 }
5067
Denys Vlasenko10bde142018-12-27 18:23:58 +01005068 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005069
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005070 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005071 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005072}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005073#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005074
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005075static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005076{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005077 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005078 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005079
Denys Vlasenko6842c602019-01-04 05:41:47 +01005080 if (p->lex == XC_LEX_NLINE)
5081 RETURN_STATUS(zxc_lex_next());
5082
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005083 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005084 if (i >= 0) {
5085 BcInst inst = dc_LEX_to_INST[i];
5086 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005087 xc_parse_push(inst);
5088 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005089 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005090 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005091 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005092}
5093#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5094
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005095static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005096{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005097 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005098 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5099 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005100 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005101 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005102 }
5103
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005104 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005105 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005106}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005107#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005108
Gavin Howard01055ba2018-11-03 11:00:21 -06005109#endif // ENABLE_DC
5110
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005111//
5112// Execution engine
5113//
5114
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005115#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5116#define BC_PROG_NUM(r, n) \
5117 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5118
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005119#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5120#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5121
Denys Vlasenko10bde142018-12-27 18:23:58 +01005122static BcVec* xc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005123{
Gavin Howard01055ba2018-11-03 11:00:21 -06005124 BcId e, *ptr;
5125 BcVec *v, *map;
5126 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005127 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005128
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005129 v = var ? &G.prog.vars : &G.prog.arrs;
5130 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005131
5132 e.name = id;
5133 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005134 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005135
5136 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005137 BcVec v2;
5138 bc_array_init(&v2, var);
5139 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005140 }
5141
5142 ptr = bc_vec_item(map, i);
5143 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005144 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005145}
5146
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005147// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005148static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005149{
Gavin Howard01055ba2018-11-03 11:00:21 -06005150 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005151 case XC_RESULT_STR:
5152 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005153 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005154 case XC_RESULT_IBASE:
5155 case XC_RESULT_SCALE:
5156 case XC_RESULT_OBASE:
5157 *num = &r->d.n;
5158 break;
5159 case XC_RESULT_CONSTANT: {
5160 BcStatus s;
5161 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005162 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005163
Denys Vlasenko10bde142018-12-27 18:23:58 +01005164 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005165 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005166
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005167 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005168
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005169 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005170 if (s) {
5171 bc_num_free(&r->d.n);
5172 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005173 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005174 *num = &r->d.n;
5175 r->t = XC_RESULT_TEMP;
5176 break;
5177 }
5178 case XC_RESULT_VAR:
5179 case XC_RESULT_ARRAY:
5180 case XC_RESULT_ARRAY_ELEM: {
5181 BcVec *v;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005182 void *p;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005183 v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005184// dc variables are all stacks, so here we have this:
5185 p = bc_vec_top(v);
5186// TODO: eliminate these stacks for bc-only config?
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005187 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005188 v = p;
5189 if (v->len <= r->d.id.idx)
5190 bc_array_expand(v, r->d.id.idx + 1);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005191 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005192 } else {
5193 *num = p;
5194 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005195 break;
5196 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005197#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005198 case BC_RESULT_LAST:
5199 *num = &G.prog.last;
5200 break;
5201 case BC_RESULT_ONE:
5202 *num = &G.prog.one;
5203 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005204#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005205#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005206 default:
5207 // Testing the theory that dc does not reach LAST/ONE
5208 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005209#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005210 }
5211
Denys Vlasenko29301232018-12-11 15:29:32 +01005212 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005213}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005214#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005215
Denys Vlasenko10bde142018-12-27 18:23:58 +01005216static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005217 BcResult **r, BcNum **rn, bool assign)
5218{
5219 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005220 BcResultType lt, rt;
5221
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005222 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005223 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005224
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005225 *r = bc_vec_item_rev(&G.prog.results, 0);
5226 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005227
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005228 s = zxc_program_num(*l, ln);
5229 if (s) RETURN_STATUS(s);
5230 s = zxc_program_num(*r, rn);
5231 if (s) RETURN_STATUS(s);
5232
Gavin Howard01055ba2018-11-03 11:00:21 -06005233 lt = (*l)->t;
5234 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005235
5236 // We run this again under these conditions in case any vector has been
5237 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005238 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005239 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005240 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005241 }
5242
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005243 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005244 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005245 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005246 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005247
Denys Vlasenko29301232018-12-11 15:29:32 +01005248 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005249}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005250#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005251
Denys Vlasenko10bde142018-12-27 18:23:58 +01005252static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005253{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005254 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005255 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005256 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005257}
5258
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005259// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005260static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005261{
5262 BcStatus s;
5263
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005264 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005265 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005266 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005267
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005268 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005269 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005270
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005271 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005272 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005273
Denys Vlasenko29301232018-12-11 15:29:32 +01005274 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005275}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005276#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005277
Denys Vlasenko10bde142018-12-27 18:23:58 +01005278static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005279{
5280 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005281 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005282}
5283
Denys Vlasenko10bde142018-12-27 18:23:58 +01005284static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005285{
5286 BcStatus s;
5287 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005288 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005289
Denys Vlasenko10bde142018-12-27 18:23:58 +01005290 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005291 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005292 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005293
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005294 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005295 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 -06005296 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005297 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005298
Denys Vlasenko259137d2018-12-11 19:42:05 +01005299 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005300 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005301 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005302 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005303}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005304#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005305
Denys Vlasenko10bde142018-12-27 18:23:58 +01005306static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005307{
5308 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005309 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005310 BcVec buf;
5311 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005312 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005313
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005314 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005315 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005316
Denys Vlasenko10bde142018-12-27 18:23:58 +01005317 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005318 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005319
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005320 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005321 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005322 //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 +01005323
Denys Vlasenko10bde142018-12-27 18:23:58 +01005324 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005325 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005326 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005327 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005328 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005329 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005330 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005331 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005332 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005333 s = bc_error_at("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005334 goto exec_err;
5335 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005336 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005337
5338 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005339 ip.inst_idx = 0;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005340 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005341
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005342 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005343 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005344 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005345 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005346 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005347}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005348#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005349
Denys Vlasenko10bde142018-12-27 18:23:58 +01005350static size_t xc_program_index(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005351{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005352 unsigned char *bytes = (void*)(code + *bgn);
5353 unsigned amt;
5354 unsigned i;
5355 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005356
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005357 amt = *bytes++;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01005358 if (amt < SMALL_INDEX_LIMIT) {
5359 *bgn += 1;
5360 return amt;
5361 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005362 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005363 *bgn += amt + 1;
5364
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005365 res = 0;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005366 i = 0;
5367 do {
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005368 res |= (size_t)(*bytes++) << i;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005369 i += 8;
5370 } while (--amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005371
5372 return res;
5373}
5374
Denys Vlasenko10bde142018-12-27 18:23:58 +01005375static char *xc_program_name(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005376{
Denys Vlasenko694d2982018-12-18 19:17:11 +01005377 code += *bgn;
Denys Vlasenkof706a182018-12-26 20:02:09 +01005378 *bgn += strlen(code) + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005379
Denys Vlasenkof706a182018-12-26 20:02:09 +01005380 return xstrdup(code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005381}
5382
Denys Vlasenko10bde142018-12-27 18:23:58 +01005383static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005384{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005385#if ENABLE_DC
Denys Vlasenko266bec82019-01-02 05:03:53 +01005386 if (!str[0] && IS_DC) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005387 // Example: echo '[]ap' | dc
5388 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005389 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005390 return;
5391 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005392#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005393 while (*str) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005394 char c = *str++;
5395 if (c == '\\') {
5396 static const char esc[] ALIGN1 = "nabfrt""e\\";
5397 char *n;
5398
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005399 c = *str++;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005400 n = strchr(esc, c); // note: c can be NUL
5401 if (!n) {
5402 // Just print the backslash and following character
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005403 bb_putchar('\\');
5404 ++G.prog.nchars;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005405 } else {
5406 if (n - esc == 0) // "\n" ?
5407 G.prog.nchars = SIZE_MAX;
5408 c = "\n\a\b\f\r\t""\\\\""\\"[n - esc];
5409 // n a b f r t e \ \<end of line>
Gavin Howard01055ba2018-11-03 11:00:21 -06005410 }
5411 }
Denys Vlasenko266bec82019-01-02 05:03:53 +01005412 putchar(c);
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005413 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005414 }
5415}
5416
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005417static void bc_num_printNewline(void)
5418{
5419 if (G.prog.nchars == G.prog.len - 1) {
5420 bb_putchar('\\');
5421 bb_putchar('\n');
5422 G.prog.nchars = 0;
5423 }
5424}
5425
5426#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005427static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005428{
5429 (void) radix;
5430 bb_putchar((char) num);
5431 G.prog.nchars += width;
5432}
5433#endif
5434
5435static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5436{
5437 size_t exp, pow;
5438
5439 bc_num_printNewline();
5440 bb_putchar(radix ? '.' : ' ');
5441 ++G.prog.nchars;
5442
5443 bc_num_printNewline();
5444 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5445 continue;
5446
5447 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5448 size_t dig;
5449 bc_num_printNewline();
5450 dig = num / pow;
5451 num -= dig * pow;
5452 bb_putchar(((char) dig) + '0');
5453 }
5454}
5455
5456static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5457{
5458 if (radix) {
5459 bc_num_printNewline();
5460 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005461 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005462 }
5463
5464 bc_num_printNewline();
5465 bb_putchar(bb_hexdigits_upcase[num]);
5466 G.prog.nchars += width;
5467}
5468
5469static void bc_num_printDecimal(BcNum *n)
5470{
5471 size_t i, rdx = n->rdx - 1;
5472
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005473 if (n->neg) {
5474 bb_putchar('-');
5475 G.prog.nchars++;
5476 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005477
5478 for (i = n->len - 1; i < n->len; --i)
5479 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5480}
5481
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005482typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5483
Denys Vlasenko10bde142018-12-27 18:23:58 +01005484static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005485{
5486 BcStatus s;
5487 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005488 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005489 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005490 BcNum intp, fracp, digit, frac_len;
5491 unsigned long dig, *ptr;
5492 size_t i;
5493 bool radix;
5494
5495 if (n->len == 0) {
5496 print(0, width, false);
5497 RETURN_STATUS(BC_STATUS_SUCCESS);
5498 }
5499
5500 bc_vec_init(&stack, sizeof(long), NULL);
5501 bc_num_init(&intp, n->len);
5502 bc_num_init(&fracp, n->rdx);
5503 bc_num_init(&digit, width);
5504 bc_num_init(&frac_len, BC_NUM_INT(n));
5505 bc_num_copy(&intp, n);
5506 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005507 base.cap = ARRAY_SIZE(base_digs);
5508 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005509 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005510
5511 bc_num_truncate(&intp, intp.rdx);
5512 s = zbc_num_sub(n, &intp, &fracp, 0);
5513 if (s) goto err;
5514
5515 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005516 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005517 if (s) goto err;
5518 s = zbc_num_ulong(&digit, &dig);
5519 if (s) goto err;
5520 bc_vec_push(&stack, &dig);
5521 }
5522
5523 for (i = 0; i < stack.len; ++i) {
5524 ptr = bc_vec_item_rev(&stack, i);
5525 print(*ptr, width, false);
5526 }
5527
5528 if (!n->rdx) goto err;
5529
5530 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005531 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005532 if (s) goto err;
5533 s = zbc_num_ulong(&fracp, &dig);
5534 if (s) goto err;
5535 bc_num_ulong2num(&intp, dig);
5536 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5537 if (s) goto err;
5538 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005539 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005540 if (s) goto err;
5541 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005542 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005543 bc_num_free(&frac_len);
5544 bc_num_free(&digit);
5545 bc_num_free(&fracp);
5546 bc_num_free(&intp);
5547 bc_vec_free(&stack);
5548 RETURN_STATUS(s);
5549}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005550#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005551
Denys Vlasenko10bde142018-12-27 18:23:58 +01005552static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005553{
5554 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005555 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005556 BcNumDigitOp print;
5557 bool neg = n->neg;
5558
5559 if (neg) {
5560 bb_putchar('-');
5561 G.prog.nchars++;
5562 }
5563
5564 n->neg = false;
5565
Denys Vlasenko680ccd32018-12-31 19:42:13 +01005566 if (G.prog.ob_t <= 16) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005567 width = 1;
5568 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005569 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005570 unsigned i = G.prog.ob_t - 1;
5571 width = 0;
5572 for (;;) {
5573 width++;
5574 i /= 10;
5575 if (i == 0)
5576 break;
5577 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005578 print = bc_num_printDigits;
5579 }
5580
Denys Vlasenko10bde142018-12-27 18:23:58 +01005581 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005582 n->neg = neg;
5583
5584 RETURN_STATUS(s);
5585}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005586#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005587
Denys Vlasenko10bde142018-12-27 18:23:58 +01005588static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005589{
5590 BcStatus s = BC_STATUS_SUCCESS;
5591
5592 bc_num_printNewline();
5593
5594 if (n->len == 0) {
5595 bb_putchar('0');
5596 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005597 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005598 bc_num_printDecimal(n);
5599 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005600 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005601
5602 if (newline) {
5603 bb_putchar('\n');
5604 G.prog.nchars = 0;
5605 }
5606
5607 RETURN_STATUS(s);
5608}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005609#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005610
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005611#if !ENABLE_DC
5612// for bc, idx is always 0
5613#define xc_program_print(inst, idx) \
5614 xc_program_print(inst)
5615#endif
5616static BC_STATUS xc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005617{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005618 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005619 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005620 BcNum *num;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005621 IF_NOT_DC(size_t idx = 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005622
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005623 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005624 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005625
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005626 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005627#if ENABLE_BC
5628 if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID)
5629 // void function's result on stack, ignore
5630 RETURN_STATUS(BC_STATUS_SUCCESS);
5631#endif
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005632 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005633 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005634
5635 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005636 s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005637#if ENABLE_BC
5638 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5639#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005640 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005641 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005642
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005643 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005644 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005645
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005646 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005647 char *nl;
5648 G.prog.nchars += printf("%s", str);
5649 nl = strrchr(str, '\n');
5650 if (nl)
5651 G.prog.nchars = strlen(nl + 1);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005652 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005653 xc_program_printString(str);
Denys Vlasenko266bec82019-01-02 05:03:53 +01005654 if (inst == XC_INST_PRINT)
5655 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005656 }
5657 }
5658
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005659 if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005660
Denys Vlasenko29301232018-12-11 15:29:32 +01005661 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005662}
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005663#define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005664
Denys Vlasenko10bde142018-12-27 18:23:58 +01005665static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005666{
5667 BcStatus s;
5668 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005669 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005670
Denys Vlasenko10bde142018-12-27 18:23:58 +01005671 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005672 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005673
5674 bc_num_init(&res.d.n, num->len);
5675 bc_num_copy(&res.d.n, num);
5676 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5677
Denys Vlasenko10bde142018-12-27 18:23:58 +01005678 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005679
Denys Vlasenko29301232018-12-11 15:29:32 +01005680 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005681}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005682#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005683
Denys Vlasenko10bde142018-12-27 18:23:58 +01005684static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005685{
5686 BcStatus s;
5687 BcResult *opd1, *opd2, res;
5688 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005689 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005690
Denys Vlasenko10bde142018-12-27 18:23:58 +01005691 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005692 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005693
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005694 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005695
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005696 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005697 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005698 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005699 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005700 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005701 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005702 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005703 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005704 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005705 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005706 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005707 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005708 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005709 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005710 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005711 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005712 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005713 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005714 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005715 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005716 cond = (cond > 0);
5717 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005718 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005719 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005720 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005721 }
5722 }
5723
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005724 if (cond) bc_num_one(&res.d.n);
5725 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
Denys Vlasenko10bde142018-12-27 18:23:58 +01005727 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005728
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005729 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005730}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005731#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005732
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005733#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005734static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005735{
5736 BcNum n2;
5737 BcResult res;
5738
5739 memset(&n2, 0, sizeof(BcNum));
5740 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005741 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005742
5743 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005744 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005745 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005746 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005747 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005748 }
5749
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005750 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005751 bc_vec_push(v, &n2);
5752
Denys Vlasenko29301232018-12-11 15:29:32 +01005753 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005754}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005755#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005756#endif // ENABLE_DC
5757
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005758static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005759{
5760 BcStatus s;
5761 BcResult *ptr, r;
5762 BcVec *v;
5763 BcNum *n;
5764
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005765 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005766 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005767
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005768 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005769 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005770 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005771 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005772
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005773#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005774 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005775 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005776 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005777 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005778#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005779
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005780 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005781 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005782
5783 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko10bde142018-12-27 18:23:58 +01005784 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005785
5786 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005787 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005788 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005789 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 bc_array_init(&r.d.v, true);
5791 bc_array_copy(&r.d.v, (BcVec *) n);
5792 }
5793
5794 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005795 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005796
Denys Vlasenko29301232018-12-11 15:29:32 +01005797 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005798}
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005799#define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005800
Denys Vlasenko10bde142018-12-27 18:23:58 +01005801static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005802{
5803 BcStatus s;
5804 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005805 BcNum *l, *r;
5806 bool assign = (inst == XC_INST_ASSIGN);
5807 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005808
Denys Vlasenko10bde142018-12-27 18:23:58 +01005809 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005810 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005811
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005812 ib = left->t == XC_RESULT_IBASE;
5813 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005814
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005815#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005816 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005817 BcVec *v;
5818
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005819 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005820 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005821 v = xc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005822
Denys Vlasenkofa210792018-12-19 19:35:40 +01005823 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005824 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005825#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005826
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005827 if (left->t == XC_RESULT_CONSTANT
5828 || left->t == XC_RESULT_TEMP
5829 IF_BC(|| left->t == BC_RESULT_VOID)
5830 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005831 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005832 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005833
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005834#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005835 if (assign)
5836 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005837 else {
5838 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005839 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 +01005840 }
5841 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005842#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005843 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005844#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005845
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005846 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005847 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005848 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5849 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5850 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005851 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005852 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005853 size_t max;
5854 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005855
Denys Vlasenko29301232018-12-11 15:29:32 +01005856 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005857 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005858 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005859 if (sc) {
5860 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005861 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005862 } else {
5863 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005864 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005865 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005866 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005867 }
5868
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005869 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005870 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005871
5872 *ptr = (size_t) val;
5873 s = BC_STATUS_SUCCESS;
5874 }
5875
5876 bc_num_init(&res.d.n, l->len);
5877 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005878 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005880 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005881}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005882#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005883
Denys Vlasenko416ce762018-12-02 20:57:17 +01005884#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005885#define xc_program_pushVar(code, bgn, pop, copy) \
5886 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005887// for bc, 'pop' and 'copy' are always false
5888#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005889static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005890 bool pop, bool copy)
5891{
Gavin Howard01055ba2018-11-03 11:00:21 -06005892 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005893 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005895 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005896 r.d.id.name = name;
5897
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005898#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005899 if (pop || copy) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005900 BcVec *v = xc_program_search(name, true);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005901 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005903 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005904 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005905 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005906 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005907
5908 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005909 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005910 bc_num_init_DEF_SIZE(&r.d.n);
5911 bc_num_copy(&r.d.n, num);
5912 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005913 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005914 r.d.id.idx = num->rdx;
5915 }
5916
5917 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005918 }
5919#endif // ENABLE_DC
5920
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005921 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005922
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005923 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005924}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005925#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005926
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005927static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005928{
5929 BcStatus s = BC_STATUS_SUCCESS;
5930 BcResult r;
5931 BcNum *num;
5932
Denys Vlasenko10bde142018-12-27 18:23:58 +01005933 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005934
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005935 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005936 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005937 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005938 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005939 BcResult *operand;
5940 unsigned long temp;
5941
Denys Vlasenko10bde142018-12-27 18:23:58 +01005942 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005944 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005945 if (s) goto err;
5946
5947 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005948 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005949 goto err;
5950 }
5951
5952 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005953 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005954 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005955 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005956 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005957 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005958}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005959#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005960
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005961#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005962static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005963{
5964 BcStatus s;
5965 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005966 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005967 char inst2 = inst;
5968
Denys Vlasenko10bde142018-12-27 18:23:58 +01005969 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005970 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005971
5972 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005973 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005974 bc_num_init(&copy.d.n, num->len);
5975 bc_num_copy(&copy.d.n, num);
5976 }
5977
5978 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005979 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5980 ? BC_INST_ASSIGN_PLUS
5981 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005983 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005984 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005985 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005986
5987 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005988 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005989 }
5990
Denys Vlasenko29301232018-12-11 15:29:32 +01005991 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005992}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005993#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005994
Denys Vlasenko29301232018-12-11 15:29:32 +01005995static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005996{
Gavin Howard01055ba2018-11-03 11:00:21 -06005997 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005998 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005999 BcId *a;
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006000 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006001
Denys Vlasenko10bde142018-12-27 18:23:58 +01006002 nparams = xc_program_index(code, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006003 ip.func = xc_program_index(code, idx);
6004 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006005
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006006 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006007 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006008 }
6009 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006010 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006011 }
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006012 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
6014 for (i = 0; i < nparams; ++i) {
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006015 BcResult *arg;
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006016 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006017
6018 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006019 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006020
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006021 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch
6022 // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions)
6023 ) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006024 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006025 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006026 s = zxc_program_popResultAndCopyToVar(a->name, a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006027 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006028 }
6029
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006030 a = bc_vec_item(&func->autos, i);
6031 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006032 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006033
Denys Vlasenko10bde142018-12-27 18:23:58 +01006034 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006035 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006036 BcNum n2;
6037 bc_num_init_DEF_SIZE(&n2);
6038 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006039 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006040 BcVec v2;
6041 bc_array_init(&v2, true);
6042 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006043 }
6044 }
6045
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006046 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006047
Denys Vlasenko29301232018-12-11 15:29:32 +01006048 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006049}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006050#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006051
Denys Vlasenko29301232018-12-11 15:29:32 +01006052static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006053{
Gavin Howard01055ba2018-11-03 11:00:21 -06006054 BcResult res;
6055 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006056 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006058 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006059
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006060 f = xc_program_func(ip->func);
6061
6062 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006063 if (inst == XC_INST_RET) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006064 // bc needs this for e.g. RESULT_CONSTANT ("return 5")
6065 // because bc constants are per-function.
6066 // TODO: maybe avoid if value is already RESULT_TEMP?
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006067 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006068 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006069 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006070
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006071 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006072 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006073 bc_num_init(&res.d.n, num->len);
6074 bc_num_copy(&res.d.n, num);
Denys Vlasenko377cc972019-01-04 00:34:52 +01006075 bc_vec_pop(&G.prog.results);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006076 } else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006077 if (f->voidfunc)
6078 res.t = BC_RESULT_VOID;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006079 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006080 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006081 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006082 bc_vec_push(&G.prog.results, &res);
6083
6084 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006085
6086 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006087 a = (void*)f->autos.v;
6088 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006090 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 bc_vec_pop(v);
6092 }
6093
Denys Vlasenko29301232018-12-11 15:29:32 +01006094 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006095}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006096#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006097#endif // ENABLE_BC
6098
Denys Vlasenko10bde142018-12-27 18:23:58 +01006099static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006100{
6101 return (unsigned long) n->rdx;
6102}
6103
Denys Vlasenko10bde142018-12-27 18:23:58 +01006104static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006105{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006106 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006107
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006108 if (n->rdx != len) return len;
6109 for (;;) {
6110 if (len == 0) break;
6111 len--;
6112 if (n->num[len] != 0) break;
6113 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006114 return len;
6115}
6116
Denys Vlasenko10bde142018-12-27 18:23:58 +01006117static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006118{
6119 BcStatus s;
6120 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006121 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006122 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006123 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006124
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006125 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006126 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006127 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006129 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006130 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006131
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006132#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006133 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006134 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006135#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006136
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006137 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006138
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006139 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006140 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006141#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006142 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006143 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006144 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006145#endif
6146#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006147 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006148 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006149 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
Denys Vlasenko10bde142018-12-27 18:23:58 +01006151 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006152 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006153 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006154#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006155 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006156 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006157 }
6158
Denys Vlasenko10bde142018-12-27 18:23:58 +01006159 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006160
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006161 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006162}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006163#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006164
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006165#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006166static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006167{
6168 BcStatus s;
6169 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006170 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006171
Denys Vlasenko10bde142018-12-27 18:23:58 +01006172 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006173 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006175 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006176 bc_num_init(&res2.d.n, n2->len);
6177
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006178 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006179 if (s) goto err;
6180
Denys Vlasenko10bde142018-12-27 18:23:58 +01006181 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006182 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006183 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006184
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006185 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006186 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006187 bc_num_free(&res2.d.n);
6188 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006189 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006190}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006191#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006192
Denys Vlasenkofa210792018-12-19 19:35:40 +01006193static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006194{
6195 BcStatus s;
6196 BcResult *r1, *r2, *r3, res;
6197 BcNum *n1, *n2, *n3;
6198
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006199 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006200 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006201 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006202 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006204 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006205 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006206 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006207 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006208 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006209
6210 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006211 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006212 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006213 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006214 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006215 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006216 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006217 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006218 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006219 }
6220 }
6221
6222 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006223 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006224 if (s) goto err;
6225
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006226 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006227 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006228
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006229 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006230 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006231 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006232 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006233}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006234#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006235
Denys Vlasenkofa210792018-12-19 19:35:40 +01006236static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006237{
Gavin Howard01055ba2018-11-03 11:00:21 -06006238 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006239 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006240
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006241 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006242
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006243 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006244 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006245 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006246}
6247
Denys Vlasenkofa210792018-12-19 19:35:40 +01006248static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006249{
6250 BcStatus s;
6251 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006252 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006253 char **strs;
6254 char *str;
6255 char c;
6256 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006258 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006259 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006260
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006261 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006262 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006263 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
6265 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006266 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006267 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006268 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006269
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006270 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006271 bc_num_copy(&n, num);
6272 bc_num_truncate(&n, n.rdx);
6273
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006274 strmb.cap = ARRAY_SIZE(strmb_digs);
6275 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006276 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006277
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006278 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006280 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006281 if (s) goto num_err;
6282
6283 c = (char) val;
6284
6285 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006286 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006287 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006288 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006289 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006290 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006291 }
6292
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006293 strs = (void*)G.prog.strs.v;
6294 for (idx = 0; idx < G.prog.strs.len; idx++) {
6295 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6296 goto dup;
6297 }
6298 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006299 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006300 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006301 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006302 bc_vec_push(&G.prog.strs, &str);
6303 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006304 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006305 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006306 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006308 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006309 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006310 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006311 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006312}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006313#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006314
Denys Vlasenkofa210792018-12-19 19:35:40 +01006315static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006316{
6317 BcStatus s;
6318 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006319 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006320 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006321
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006322 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006323 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006324 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006325
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006326 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006327 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006328
Denys Vlasenko57734c92018-12-17 21:14:05 +01006329 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006330 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006331 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006332 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006333 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006334 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006335 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336 }
6337
Denys Vlasenko29301232018-12-11 15:29:32 +01006338 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006339}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006340#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006341
Denys Vlasenkofa210792018-12-19 19:35:40 +01006342static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006343{
6344 BcStatus s;
6345 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006346 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006347 unsigned long val;
6348
Denys Vlasenko10bde142018-12-27 18:23:58 +01006349 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006350 if (s) RETURN_STATUS(s);
6351 s = zbc_num_ulong(num, &val);
6352 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006353
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006354 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006355
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006356 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006357 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006358 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006359 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006360 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006361
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006362 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenko29301232018-12-11 15:29:32 +01006364 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006365}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006366#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006367
Denys Vlasenkofa210792018-12-19 19:35:40 +01006368static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006369{
6370 BcStatus s = BC_STATUS_SUCCESS;
6371 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006372 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006373 BcInstPtr ip;
6374 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006375
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006376 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006377 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006378
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006379 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006380
6381 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006382 BcNum *n = n; // for compiler
6383 bool exec;
6384 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006385 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006386 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006387
Denys Vlasenkof706a182018-12-26 20:02:09 +01006388 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006389 (*bgn) += 1;
6390 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006391 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006392
6393 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006394 name = then_name;
6395 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006396 exec = true;
6397 name = else_name;
6398 }
6399
6400 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006401 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006402 v = xc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 n = bc_vec_top(v);
6404 }
6405
6406 free(then_name);
6407 free(else_name);
6408
6409 if (!exec) goto exit;
6410 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006411 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006412 goto exit;
6413 }
6414
6415 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006416 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006417 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006419 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006420 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006421 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006422 if (s || !BC_PROG_STR(n)) goto exit;
6423 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006424 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006425 goto exit;
6426 }
6427
6428 fidx = sidx + BC_PROG_REQ_FUNCS;
6429
Denys Vlasenko10bde142018-12-27 18:23:58 +01006430 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006431
6432 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006433 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006434 char *str;
6435
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006436 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006437 xc_parse_create(fidx);
6438 str = *xc_program_str(sidx);
6439 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006440 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006441
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006442 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006444 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006445 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006446 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006447 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006448 G.prs = sv_parse; // struct copy
6449 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006450 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006451 bc_vec_pop_all(&f->code);
6452 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006453 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006454 }
6455
Denys Vlasenko24e41942018-12-21 23:01:26 +01006456 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006457 ip.func = fidx;
6458
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006459 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006460 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006461
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006462 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006463 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006464 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006465 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006466}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006467#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006468#endif // ENABLE_DC
6469
Denys Vlasenko10bde142018-12-27 18:23:58 +01006470static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006471{
Gavin Howard01055ba2018-11-03 11:00:21 -06006472 BcResult res;
6473 unsigned long val;
6474
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006475 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006476 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006477 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006478 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006479 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006481 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006482
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006483 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006484 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006485 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006486}
6487
Denys Vlasenko10bde142018-12-27 18:23:58 +01006488static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006489{
Gavin Howard01055ba2018-11-03 11:00:21 -06006490 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006491 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006492 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006493 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006494
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006495 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006496 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6497 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006498 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006499 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006500
Denys Vlasenko24e41942018-12-21 23:01:26 +01006501 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 -06006502 switch (inst) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006503 case XC_INST_RET:
6504 if (IS_DC) { // end of '?' reached
6505 bc_vec_pop(&G.prog.exestack);
6506 goto read_updated_ip;
6507 }
6508 // bc: fall through
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006509#if ENABLE_BC
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006510 case BC_INST_RET0:
6511 dbg_exec("BC_INST_RET[0]:");
6512 s = zbc_program_return(inst);
6513 goto read_updated_ip;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006514 case BC_INST_JUMP_ZERO: {
6515 BcNum *num;
6516 bool zero;
6517 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006518 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006519 if (s) RETURN_STATUS(s);
6520 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6521 bc_vec_pop(&G.prog.results);
6522 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006523 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006524 break;
6525 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006526 // else: fall through
6527 }
6528 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006529 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006530 size_t *addr = bc_vec_item(&func->labels, idx);
6531 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6532 ip->inst_idx = *addr;
6533 break;
6534 }
6535 case BC_INST_CALL:
6536 dbg_exec("BC_INST_CALL:");
6537 s = zbc_program_call(code, &ip->inst_idx);
6538 goto read_updated_ip;
6539 case BC_INST_INC_PRE:
6540 case BC_INST_DEC_PRE:
6541 case BC_INST_INC_POST:
6542 case BC_INST_DEC_POST:
6543 dbg_exec("BC_INST_INCDEC:");
6544 s = zbc_program_incdec(inst);
6545 break;
6546 case BC_INST_HALT:
6547 dbg_exec("BC_INST_HALT:");
6548 QUIT_OR_RETURN_TO_MAIN;
6549 break;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006550 case XC_INST_BOOL_OR:
6551 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006552#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006553 case XC_INST_REL_EQ:
6554 case XC_INST_REL_LE:
6555 case XC_INST_REL_GE:
6556 case XC_INST_REL_NE:
6557 case XC_INST_REL_LT:
6558 case XC_INST_REL_GT:
6559 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006560 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006561 break;
6562 case XC_INST_READ:
6563 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006564 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006565 goto read_updated_ip;
6566 case XC_INST_VAR:
6567 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006568 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006569 break;
6570 case XC_INST_ARRAY_ELEM:
6571 case XC_INST_ARRAY:
6572 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6573 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6574 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006575#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006576 case BC_INST_LAST:
6577 dbg_exec("BC_INST_LAST:");
6578 r.t = BC_RESULT_LAST;
6579 bc_vec_push(&G.prog.results, &r);
6580 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006581#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006582 case XC_INST_IBASE:
6583 case XC_INST_OBASE:
6584 case XC_INST_SCALE:
6585 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006586 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006587 break;
6588 case XC_INST_SCALE_FUNC:
6589 case XC_INST_LENGTH:
6590 case XC_INST_SQRT:
6591 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006592 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006593 break;
6594 case XC_INST_NUM:
6595 dbg_exec("XC_INST_NUM:");
6596 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006597 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006598 bc_vec_push(&G.prog.results, &r);
6599 break;
6600 case XC_INST_POP:
6601 dbg_exec("XC_INST_POP:");
6602 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6603 s = bc_error_stack_has_too_few_elements();
6604 else
6605 bc_vec_pop(&G.prog.results);
6606 break;
6607 case XC_INST_PRINT:
6608 case XC_INST_PRINT_POP:
6609 case XC_INST_PRINT_STR:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006610 dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006611 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006612 break;
6613 case XC_INST_STR:
6614 dbg_exec("XC_INST_STR:");
6615 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006616 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006617 bc_vec_push(&G.prog.results, &r);
6618 break;
6619 case XC_INST_POWER:
6620 case XC_INST_MULTIPLY:
6621 case XC_INST_DIVIDE:
6622 case XC_INST_MODULUS:
6623 case XC_INST_PLUS:
6624 case XC_INST_MINUS:
6625 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006626 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006627 break;
6628 case XC_INST_BOOL_NOT: {
6629 BcNum *num;
6630 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006631 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006632 if (s) RETURN_STATUS(s);
6633 bc_num_init_DEF_SIZE(&r.d.n);
6634 if (bc_num_cmp(num, &G.prog.zero) == 0)
6635 bc_num_one(&r.d.n);
6636 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006637 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006638 break;
6639 }
6640 case XC_INST_NEG:
6641 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006642 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006643 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006644#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006645 case BC_INST_ASSIGN_POWER:
6646 case BC_INST_ASSIGN_MULTIPLY:
6647 case BC_INST_ASSIGN_DIVIDE:
6648 case BC_INST_ASSIGN_MODULUS:
6649 case BC_INST_ASSIGN_PLUS:
6650 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006651#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006652 case XC_INST_ASSIGN:
6653 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006654 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006655 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006656#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006657 case DC_INST_POP_EXEC:
6658 dbg_exec("DC_INST_POP_EXEC:");
6659 bc_vec_pop(&G.prog.exestack);
6660 goto read_updated_ip;
6661 case DC_INST_MODEXP:
6662 dbg_exec("DC_INST_MODEXP:");
6663 s = zdc_program_modexp();
6664 break;
6665 case DC_INST_DIVMOD:
6666 dbg_exec("DC_INST_DIVMOD:");
6667 s = zdc_program_divmod();
6668 break;
6669 case DC_INST_EXECUTE:
6670 case DC_INST_EXEC_COND:
6671 dbg_exec("DC_INST_EXEC[_COND]:");
6672 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6673 goto read_updated_ip;
6674 case DC_INST_PRINT_STACK: {
6675 size_t idx;
6676 dbg_exec("DC_INST_PRINT_STACK:");
6677 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006678 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006679 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006680 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006681 break;
6682 }
6683 case DC_INST_CLEAR_STACK:
6684 dbg_exec("DC_INST_CLEAR_STACK:");
6685 bc_vec_pop_all(&G.prog.results);
6686 break;
6687 case DC_INST_STACK_LEN:
6688 dbg_exec("DC_INST_STACK_LEN:");
6689 dc_program_stackLen();
6690 break;
6691 case DC_INST_DUPLICATE:
6692 dbg_exec("DC_INST_DUPLICATE:");
6693 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6694 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6695 ptr = bc_vec_top(&G.prog.results);
6696 dc_result_copy(&r, ptr);
6697 bc_vec_push(&G.prog.results, &r);
6698 break;
6699 case DC_INST_SWAP: {
6700 BcResult *ptr2;
6701 dbg_exec("DC_INST_SWAP:");
6702 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6703 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6704 ptr = bc_vec_item_rev(&G.prog.results, 0);
6705 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6706 memcpy(&r, ptr, sizeof(BcResult));
6707 memcpy(ptr, ptr2, sizeof(BcResult));
6708 memcpy(ptr2, &r, sizeof(BcResult));
6709 break;
6710 }
6711 case DC_INST_ASCIIFY:
6712 dbg_exec("DC_INST_ASCIIFY:");
6713 s = zdc_program_asciify();
6714 break;
6715 case DC_INST_PRINT_STREAM:
6716 dbg_exec("DC_INST_PRINT_STREAM:");
6717 s = zdc_program_printStream();
6718 break;
6719 case DC_INST_LOAD:
6720 case DC_INST_PUSH_VAR: {
6721 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006722 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006723 break;
6724 }
6725 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006726 char *name = xc_program_name(code, &ip->inst_idx);
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006727 s = zxc_program_popResultAndCopyToVar(name, true);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006728 free(name);
6729 break;
6730 }
6731 case DC_INST_QUIT:
6732 dbg_exec("DC_INST_QUIT:");
6733 if (G.prog.exestack.len <= 2)
6734 QUIT_OR_RETURN_TO_MAIN;
6735 bc_vec_npop(&G.prog.exestack, 2);
6736 goto read_updated_ip;
6737 case DC_INST_NQUIT:
6738 dbg_exec("DC_INST_NQUIT:");
6739 s = zdc_program_nquit();
6740 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006741#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006742 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006743 // Instruction stack has changed, read new pointers
6744 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006745 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006746 code = func->code.v;
6747 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006748 }
6749
Denys Vlasenkod38af482018-12-04 19:11:02 +01006750 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006751 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006752 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006753 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006754
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006755 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006756 }
6757
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006758 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006759}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006760#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006761
Denys Vlasenko10bde142018-12-27 18:23:58 +01006762static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006763{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006764 char *lenv;
6765 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006766
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006767 lenv = getenv(var);
6768 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 if (!lenv) return len;
6770
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006771 len = bb_strtou(lenv, NULL, 10) - 1;
6772 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 len = BC_NUM_PRINT_WIDTH;
6774
6775 return len;
6776}
6777
Denys Vlasenko10bde142018-12-27 18:23:58 +01006778static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006779{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006780 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006781
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006782 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006783 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006784 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006785
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006786 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006787 BcInstPtr *ip;
6788 BcFunc *f;
6789
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006790 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006791 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006792#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006793 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006794 if (s) goto err;
6795
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006796 // Check that next token is a correct stmt delimiter -
6797 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006798 if (G.prs.lex != BC_LEX_SCOLON
6799 && G.prs.lex != XC_LEX_NLINE
6800 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006801 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01006802 bc_error_at("bad statement terminator");
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006803 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006804 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006805 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006806 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006807#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006808 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006809#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006810 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006811#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006812 }
6813 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006814 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006815 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006816 RETURN_STATUS(BC_STATUS_FAILURE);
6817 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006818
Denys Vlasenko39287e02018-12-22 02:23:08 +01006819 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006820 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006821 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006822 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006823 break;
6824 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006825
6826 ip = (void*)G.prog.exestack.v;
6827#if SANITY_CHECKS
6828 if (G.prog.exestack.len != 1) // should have only main's IP
6829 bb_error_msg_and_die("BUG:call stack");
6830 if (ip->func != BC_PROG_MAIN)
6831 bb_error_msg_and_die("BUG:not MAIN");
6832#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006833 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006834 // bc discards strings, constants and code after each
6835 // top-level statement in the "main program".
6836 // This prevents "yes 1 | bc" from growing its memory
6837 // without bound. This can be done because data stack
6838 // is empty and thus can't hold any references to
6839 // strings or constants, there is no generated code
6840 // which can hold references (after we discard one
6841 // we just executed). Code of functions can have references,
6842 // but bc stores function strings/constants in per-function
6843 // storage.
6844 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006845#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006846 if (G.prog.results.len != 0) // should be empty
6847 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006848#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006849 IF_BC(bc_vec_pop_all(&f->strs);)
6850 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01006851 // We are at SCOLON/NLINE, skip it:
6852 s = zxc_lex_next();
6853 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006854 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006855 if (G.prog.results.len == 0
6856 && G.prog.vars.len == 0
6857 ) {
6858 // If stack is empty and no registers exist (TODO: or they are all empty),
6859 // we can get rid of accumulated strings and constants.
6860 // In this example dc process should not grow
6861 // its memory consumption with time:
6862 // yes 1pc | dc
6863 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6864 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6865 }
6866 // The code is discarded always (below), thus this example
6867 // should also not grow its memory consumption with time,
6868 // even though its data stack is not empty:
6869 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006870 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006871 // We drop generated and executed code for both bc and dc:
6872 bc_vec_pop_all(&f->code);
6873 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 }
Denys Vlasenko6842c602019-01-04 05:41:47 +01006875
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006876 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006877 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006878}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006879#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006880
Denys Vlasenko10bde142018-12-27 18:23:58 +01006881static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006882{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006883 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006884 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006885 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006886 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006887
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006888 G.prs.lex_filename = filename;
6889 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006890 G.err_line = G.prs.lex_line = 1;
Denys Vlasenko22314682019-01-01 21:50:14 +01006891 dbg_lex("p->lex_line reset to 1");
Gavin Howard01055ba2018-11-03 11:00:21 -06006892
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006893 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006894 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006895 // We do not stop looping on errors here if reading stdin.
6896 // Example: start interactive bc and enter "return".
6897 // It should say "'return' not in a function"
6898 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006899 } while (G.prs.lex_input_fp == stdin);
6900 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006901 RETURN_STATUS(s);
6902}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006903#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006904
Denys Vlasenko10bde142018-12-27 18:23:58 +01006905static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006906{
6907 BcStatus s;
6908 FILE *fp;
6909
6910 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006911 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006912 fclose(fp);
6913
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006914 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006915}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006916#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006917
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006918#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006919static void bc_vm_info(void)
6920{
6921 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006922 "Adapted from https://github.com/gavinhoward/bc\n"
6923 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006924 , applet_name);
6925}
6926
6927static void bc_args(char **argv)
6928{
6929 unsigned opts;
6930 int i;
6931
6932 GETOPT_RESET();
6933#if ENABLE_FEATURE_BC_LONG_OPTIONS
6934 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6935 "warn\0" No_argument "w"
6936 "version\0" No_argument "v"
6937 "standard\0" No_argument "s"
6938 "quiet\0" No_argument "q"
6939 "mathlib\0" No_argument "l"
6940 "interactive\0" No_argument "i"
6941 );
6942#else
6943 opts = option_mask32 |= getopt32(argv, "wvsqli");
6944#endif
6945 if (getenv("POSIXLY_CORRECT"))
6946 option_mask32 |= BC_FLAG_S;
6947
6948 if (opts & BC_FLAG_V) {
6949 bc_vm_info();
6950 exit(0);
6951 }
6952
6953 for (i = optind; argv[i]; ++i)
6954 bc_vec_push(&G.files, argv + i);
6955}
6956
6957static void bc_vm_envArgs(void)
6958{
6959 BcVec v;
6960 char *buf;
6961 char *env_args = getenv("BC_ENV_ARGS");
6962
6963 if (!env_args) return;
6964
6965 G.env_args = xstrdup(env_args);
6966 buf = G.env_args;
6967
6968 bc_vec_init(&v, sizeof(char *), NULL);
6969
6970 while (*(buf = skip_whitespace(buf)) != '\0') {
6971 bc_vec_push(&v, &buf);
6972 buf = skip_non_whitespace(buf);
6973 if (!*buf)
6974 break;
6975 *buf++ = '\0';
6976 }
6977
6978 // NULL terminate, and pass argv[] so that first arg is argv[1]
6979 if (sizeof(int) == sizeof(char*)) {
6980 bc_vec_push(&v, &const_int_0);
6981 } else {
6982 static char *const nullptr = NULL;
6983 bc_vec_push(&v, &nullptr);
6984 }
6985 bc_args(((char **)v.v) - 1);
6986
6987 bc_vec_free(&v);
6988}
6989
6990static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006991 "scale=20"
6992"\n" "define e(x){"
6993"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006994////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6995//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6996//e(-.998896): GNU:.36828580434569428695
6997// above code:.36828580434569428696
6998// actual value:.3682858043456942869594...
6999// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007000"\n" "b=ibase"
7001"\n" "ibase=A"
7002"\n" "if(x<0){"
7003"\n" "n=1"
7004"\n" "x=-x"
7005"\n" "}"
7006"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007007"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007008"\n" "scale=scale(x)+1"
7009"\n" "while(x>1){"
7010"\n" "d+=1"
7011"\n" "x/=2"
7012"\n" "scale+=1"
7013"\n" "}"
7014"\n" "scale=r"
7015"\n" "r=x+1"
7016"\n" "p=x"
7017"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007018"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007019"\n" "p*=x"
7020"\n" "f*=i"
7021"\n" "v=p/f"
7022"\n" "r+=v"
7023"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007024"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007025"\n" "scale=s"
7026"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007027"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007028"\n" "return(r/1)"
7029"\n" "}"
7030"\n" "define l(x){"
7031"\n" "auto b,s,r,p,a,q,i,v"
7032"\n" "b=ibase"
7033"\n" "ibase=A"
7034"\n" "if(x<=0){"
7035"\n" "r=(1-10^scale)/1"
7036"\n" "ibase=b"
7037"\n" "return(r)"
7038"\n" "}"
7039"\n" "s=scale"
7040"\n" "scale+=6"
7041"\n" "p=2"
7042"\n" "while(x>=2){"
7043"\n" "p*=2"
7044"\n" "x=sqrt(x)"
7045"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007046"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007047"\n" "p*=2"
7048"\n" "x=sqrt(x)"
7049"\n" "}"
7050"\n" "r=a=(x-1)/(x+1)"
7051"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007052"\n" "v=1"
7053"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007054"\n" "a*=q"
7055"\n" "v=a/i"
7056"\n" "r+=v"
7057"\n" "}"
7058"\n" "r*=p"
7059"\n" "scale=s"
7060"\n" "ibase=b"
7061"\n" "return(r/1)"
7062"\n" "}"
7063"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007064"\n" "auto b,s,r,a,q,i"
7065"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007066"\n" "b=ibase"
7067"\n" "ibase=A"
7068"\n" "s=scale"
7069"\n" "scale=1.1*s+2"
7070"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007071"\n" "scale=0"
7072"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007073"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007074"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007075"\n" "scale=s+2"
7076"\n" "r=a=x"
7077"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007078"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007079"\n" "a*=q/(i*(i-1))"
7080"\n" "r+=a"
7081"\n" "}"
7082"\n" "scale=s"
7083"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007084"\n" "return(r/1)"
7085"\n" "}"
7086"\n" "define c(x){"
7087"\n" "auto b,s"
7088"\n" "b=ibase"
7089"\n" "ibase=A"
7090"\n" "s=scale"
7091"\n" "scale*=1.2"
7092"\n" "x=s(2*a(1)+x)"
7093"\n" "scale=s"
7094"\n" "ibase=b"
7095"\n" "return(x/1)"
7096"\n" "}"
7097"\n" "define a(x){"
7098"\n" "auto b,s,r,n,a,m,t,f,i,u"
7099"\n" "b=ibase"
7100"\n" "ibase=A"
7101"\n" "n=1"
7102"\n" "if(x<0){"
7103"\n" "n=-1"
7104"\n" "x=-x"
7105"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007106"\n" "if(scale<65){"
7107"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7108"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007109"\n" "}"
7110"\n" "s=scale"
7111"\n" "if(x>.2){"
7112"\n" "scale+=5"
7113"\n" "a=a(.2)"
7114"\n" "}"
7115"\n" "scale=s+3"
7116"\n" "while(x>.2){"
7117"\n" "m+=1"
7118"\n" "x=(x-.2)/(1+.2*x)"
7119"\n" "}"
7120"\n" "r=u=x"
7121"\n" "f=-x*x"
7122"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007123"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007124"\n" "u*=f"
7125"\n" "t=u/i"
7126"\n" "r+=t"
7127"\n" "}"
7128"\n" "scale=s"
7129"\n" "ibase=b"
7130"\n" "return((m*a+r)/n)"
7131"\n" "}"
7132"\n" "define j(n,x){"
7133"\n" "auto b,s,o,a,i,v,f"
7134"\n" "b=ibase"
7135"\n" "ibase=A"
7136"\n" "s=scale"
7137"\n" "scale=0"
7138"\n" "n/=1"
7139"\n" "if(n<0){"
7140"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007141"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007142"\n" "}"
7143"\n" "a=1"
7144"\n" "for(i=2;i<=n;++i)a*=i"
7145"\n" "scale=1.5*s"
7146"\n" "a=(x^n)/2^n/a"
7147"\n" "r=v=1"
7148"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007149"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007150"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007151"\n" "v=v*f/i/(n+i)"
7152"\n" "r+=v"
7153"\n" "}"
7154"\n" "scale=s"
7155"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007156"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007157"\n" "return(a*r/1)"
7158"\n" "}"
7159};
7160#endif // ENABLE_BC
7161
Denys Vlasenko10bde142018-12-27 18:23:58 +01007162static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007163{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007164 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007165 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007166 size_t i;
7167
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007168#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007169 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007170 // We know that internal library is not buggy,
7171 // thus error checking is normally disabled.
7172# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007173 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007174 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007175 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007176#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007177
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007178 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007179 fname = (void*)G.files.v;
7180 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007181 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007182 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7183 // Debug config, non-interactive mode:
7184 // return all the way back to main.
7185 // Non-debug builds do not come here
7186 // in non-interactive mode, they exit.
7187 RETURN_STATUS(s);
7188 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007189 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007190
Denys Vlasenko91cde952018-12-10 20:56:08 +01007191 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007192 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007193
Denys Vlasenko19f11072018-12-12 22:48:19 +01007194 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007195}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007196#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007197
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007198#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007199static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007200{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007201 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007202 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007203 bc_vec_free(&G.prog.vars);
7204 bc_vec_free(&G.prog.var_map);
7205 bc_vec_free(&G.prog.arrs);
7206 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007207 IF_DC(bc_vec_free(&G.prog.strs);)
7208 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007209 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007210 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007211 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007212 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007213 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007214 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007215}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007216#endif
7217
Denys Vlasenko10bde142018-12-27 18:23:58 +01007218static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007219{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007220 BcInstPtr ip;
7221
Denys Vlasenko82269122018-12-14 16:30:56 +01007222 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007223 memset(&ip, 0, sizeof(BcInstPtr));
7224
Denys Vlasenko82269122018-12-14 16:30:56 +01007225 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007226 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007227 G.prog.ob_t = 10;
7228
Denys Vlasenko503faf92018-12-20 16:24:18 +01007229 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7230 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007231
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007232 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007233 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007234
Denys Vlasenko503faf92018-12-20 16:24:18 +01007235 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7236 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007237
7238 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007239 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007240
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007241 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007242 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007243 // a valid function name (and be short)
7244 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007245 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007246 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007247 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007248 xc_program_add_fn();
7249 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007250 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007251
7252 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007253 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007254
7255 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007256 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007257
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007258 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7259 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007260 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007261 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7262 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007263
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007264 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007265}
Gavin Howard01055ba2018-11-03 11:00:21 -06007266
Denys Vlasenko10bde142018-12-27 18:23:58 +01007267static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007268{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007269 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007270#if ENABLE_FEATURE_EDITING
7271 G.line_input_state = new_line_input_t(DO_HISTORY);
7272#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007273 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007274
Denys Vlasenko10bde142018-12-27 18:23:58 +01007275 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007276 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007277 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007278
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007279//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7280//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007281 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007282#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007283 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007284 // With SA_RESTART, most system calls will restart
7285 // (IOW: they won't fail with EINTR).
7286 // In particular, this means ^C won't cause
7287 // stdout to get into "error state" if SIGINT hits
7288 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007289 //
7290 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007291 // will only be handled after [Enter] since read()
7292 // from stdin is not interrupted by ^C either,
7293 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007294 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007295 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7296
7297 // Without SA_RESTART, this exhibits a bug:
7298 // "while (1) print 1" and try ^C-ing it.
7299 // Intermittently, instead of returning to input line,
7300 // you'll get "output error: Interrupted system call"
7301 // and exit.
7302 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007303#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007304 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007305 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007306 return 0; // "not a tty"
7307}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007308
Denys Vlasenko10bde142018-12-27 18:23:58 +01007309static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007310{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007311 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007312#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007313 if (G_exiting) // it was actually "halt" or "quit"
7314 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007315
7316 bc_vec_free(&G.files);
7317 xc_program_free();
7318 xc_parse_free();
7319 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007320# if ENABLE_FEATURE_EDITING
7321 free_line_input_t(G.line_input_state);
7322# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007323 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007324#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007325 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007326 return st;
7327}
7328
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007329#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007330int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007331int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007332{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007333 int is_tty;
7334
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007335 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007336
Denys Vlasenko10bde142018-12-27 18:23:58 +01007337 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007338
7339 bc_args(argv);
7340
7341 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7342 bc_vm_info();
7343
Denys Vlasenko10bde142018-12-27 18:23:58 +01007344 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007345}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007346#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007347
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007348#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007349int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007350int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007351{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007352 int noscript;
7353
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007354 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007355
7356 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7357 // 1 char wider than bc from the same package.
7358 // Both default width, and xC_LINE_LENGTH=N are wider:
7359 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007360 // |1234\ |
7361 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007362 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007363 // |123\ |
7364 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007365 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007366 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007367
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007368 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7369 noscript = BC_FLAG_I;
7370 for (;;) {
7371 int n = getopt(argc, argv, "e:f:x");
7372 if (n <= 0)
7373 break;
7374 switch (n) {
7375 case 'e':
7376 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007377 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007378 if (n) return n;
7379 break;
7380 case 'f':
7381 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007382 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007383 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007384 break;
7385 case 'x':
7386 option_mask32 |= DC_FLAG_X;
7387 break;
7388 default:
7389 bb_show_usage();
7390 }
7391 }
7392 argv += optind;
7393
7394 while (*argv) {
7395 noscript = 0;
7396 bc_vec_push(&G.files, argv++);
7397 }
7398
7399 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7400
Denys Vlasenko10bde142018-12-27 18:23:58 +01007401 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007402}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007403#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007404
Denys Vlasenko14767602018-12-27 22:52:13 +01007405#endif // DC_BIG