blob: 69d3920c0d5348a8bc11ce97ab7c878a2aedd073 [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,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100206 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600207} BcStatus;
208
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100209#define BC_VEC_INVALID_IDX ((size_t) -1)
210#define BC_VEC_START_CAP (1 << 5)
Gavin Howard01055ba2018-11-03 11:00:21 -0600211
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100212typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600213
214typedef struct BcVec {
215 char *v;
216 size_t len;
217 size_t cap;
218 size_t size;
219 BcVecFree dtor;
220} BcVec;
221
Gavin Howard01055ba2018-11-03 11:00:21 -0600222typedef signed char BcDig;
223
224typedef struct BcNum {
225 BcDig *restrict num;
226 size_t rdx;
227 size_t len;
228 size_t cap;
229 bool neg;
230} BcNum;
231
Denys Vlasenko680ccd32018-12-31 19:42:13 +0100232#define BC_NUM_MAX_IBASE 36
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100233// larger value might speed up BIGNUM calculations a bit:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100234#define BC_NUM_DEF_SIZE 16
235#define BC_NUM_PRINT_WIDTH 69
Gavin Howard01055ba2018-11-03 11:00:21 -0600236
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100237#define BC_NUM_KARATSUBA_LEN 32
Gavin Howard01055ba2018-11-03 11:00:21 -0600238
Gavin Howard01055ba2018-11-03 11:00:21 -0600239typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100240#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600241 BC_INST_INC_PRE,
242 BC_INST_DEC_PRE,
243 BC_INST_INC_POST,
244 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100245#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100246 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600247
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100248 XC_INST_REL_EQ, // should
249 XC_INST_REL_LE, // match
250 XC_INST_REL_GE, // LEX
251 XC_INST_REL_NE, // constants
252 XC_INST_REL_LT, // for
253 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600254
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100255 XC_INST_POWER, // operations
256 XC_INST_MULTIPLY, // |
257 XC_INST_DIVIDE, // |
258 XC_INST_MODULUS, // |
259 XC_INST_PLUS, // |
260 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600261
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100262 XC_INST_BOOL_NOT, // |
263 XC_INST_BOOL_OR, // |
264 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100265#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100266 BC_INST_ASSIGN_POWER, // |
267 BC_INST_ASSIGN_MULTIPLY,// |
268 BC_INST_ASSIGN_DIVIDE, // |
269 BC_INST_ASSIGN_MODULUS, // |
270 BC_INST_ASSIGN_PLUS, // |
271 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100272#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100273 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600274
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100275 XC_INST_NUM,
276 XC_INST_VAR,
277 XC_INST_ARRAY_ELEM,
278 XC_INST_ARRAY,
279 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100280
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100281 XC_INST_IBASE, // order of these constans should match other enums
282 XC_INST_OBASE, // order of these constans should match other enums
283 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100284 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100285 XC_INST_LENGTH,
286 XC_INST_READ,
287 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600288
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100289 XC_INST_PRINT,
290 XC_INST_PRINT_POP,
291 XC_INST_STR,
292 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600293
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100294#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100295 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600296 BC_INST_JUMP,
297 BC_INST_JUMP_ZERO,
298
299 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600300 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100301#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100302 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600303
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100304 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100305#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100306 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600307
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100308 DC_INST_MODEXP,
309 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600310
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100311 DC_INST_EXECUTE,
312 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600313
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100314 DC_INST_ASCIIFY,
315 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600316
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100317 DC_INST_PRINT_STACK,
318 DC_INST_CLEAR_STACK,
319 DC_INST_STACK_LEN,
320 DC_INST_DUPLICATE,
321 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600322
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100323 DC_INST_LOAD,
324 DC_INST_PUSH_VAR,
325 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600326
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100327 DC_INST_QUIT,
328 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600329
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100330 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100331#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600332} BcInst;
333
334typedef struct BcId {
335 char *name;
336 size_t idx;
337} BcId;
338
339typedef struct BcFunc {
340 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100341 IF_BC(BcVec labels;)
342 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100343 IF_BC(BcVec strs;)
344 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100345 IF_BC(size_t nparams;)
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100346 IF_BC(bool voidfunc;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600347} BcFunc;
348
349typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100350 XC_RESULT_TEMP,
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100351 IF_BC(BC_RESULT_VOID,) // same as TEMP, but INST_PRINT will ignore it
Gavin Howard01055ba2018-11-03 11:00:21 -0600352
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100353 XC_RESULT_VAR,
354 XC_RESULT_ARRAY_ELEM,
355 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600356
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100357 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600358
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100359 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
360 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
361 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
362 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100363 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100364 XC_RESULT_CONSTANT,
365 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600366} BcResultType;
367
368typedef union BcResultData {
369 BcNum n;
370 BcVec v;
371 BcId id;
372} BcResultData;
373
374typedef struct BcResult {
375 BcResultType t;
376 BcResultData d;
377} BcResult;
378
379typedef struct BcInstPtr {
380 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100381 size_t inst_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -0600382} BcInstPtr;
383
Gavin Howard01055ba2018-11-03 11:00:21 -0600384typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100385 XC_LEX_EOF,
386 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600387
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100388 XC_LEX_NLINE,
389 XC_LEX_WHITESPACE,
390 XC_LEX_STR,
391 XC_LEX_NAME,
392 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100393
Denys Vlasenko69560f42018-12-24 14:14:23 +0100394 XC_LEX_1st_op,
395 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100396
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100397 XC_LEX_OP_REL_EQ, // should
398 XC_LEX_OP_REL_LE, // match
399 XC_LEX_OP_REL_GE, // INST
400 XC_LEX_OP_REL_NE, // constants
401 XC_LEX_OP_REL_LT, // for
402 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100403
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100404 XC_LEX_OP_POWER, // operations
405 XC_LEX_OP_MULTIPLY, // |
406 XC_LEX_OP_DIVIDE, // |
407 XC_LEX_OP_MODULUS, // |
408 XC_LEX_OP_PLUS, // |
409 XC_LEX_OP_MINUS, // |
410 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100411#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100412 BC_LEX_OP_BOOL_NOT, // |
413 BC_LEX_OP_BOOL_OR, // |
414 BC_LEX_OP_BOOL_AND, // |
415
416 BC_LEX_OP_ASSIGN_POWER, // |
417 BC_LEX_OP_ASSIGN_MULTIPLY, // |
418 BC_LEX_OP_ASSIGN_DIVIDE, // |
419 BC_LEX_OP_ASSIGN_MODULUS, // |
420 BC_LEX_OP_ASSIGN_PLUS, // |
421 BC_LEX_OP_ASSIGN_MINUS, // |
422
423 BC_LEX_OP_ASSIGN, // V
424
Gavin Howard01055ba2018-11-03 11:00:21 -0600425 BC_LEX_OP_INC,
426 BC_LEX_OP_DEC,
427
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100428 BC_LEX_LPAREN, // () are 0x28 and 0x29
429 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600430
Denys Vlasenko10bde142018-12-27 18:23:58 +0100431 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100433 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600434
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100435 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600436 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100437 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600438
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100439 BC_LEX_KEY_1st_keyword,
440 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600441 BC_LEX_KEY_BREAK,
442 BC_LEX_KEY_CONTINUE,
443 BC_LEX_KEY_DEFINE,
444 BC_LEX_KEY_ELSE,
445 BC_LEX_KEY_FOR,
446 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100447 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100448 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
449 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600450 BC_LEX_KEY_IF,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100451 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600452 BC_LEX_KEY_LENGTH,
453 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600454 BC_LEX_KEY_PRINT,
455 BC_LEX_KEY_QUIT,
456 BC_LEX_KEY_READ,
457 BC_LEX_KEY_RETURN,
458 BC_LEX_KEY_SCALE,
459 BC_LEX_KEY_SQRT,
460 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100461#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600462
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100463#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100464 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
465 DC_LEX_OP_ASSIGN,
466
467 DC_LEX_LPAREN,
468 DC_LEX_SCOLON,
469 DC_LEX_READ,
470 DC_LEX_IBASE,
471 DC_LEX_SCALE,
472 DC_LEX_OBASE,
473 DC_LEX_LENGTH,
474 DC_LEX_PRINT,
475 DC_LEX_QUIT,
476 DC_LEX_SQRT,
477 DC_LEX_LBRACE,
478
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100479 DC_LEX_EQ_NO_REG,
480 DC_LEX_OP_MODEXP,
481 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600482
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100483 DC_LEX_COLON,
484 DC_LEX_ELSE,
485 DC_LEX_EXECUTE,
486 DC_LEX_PRINT_STACK,
487 DC_LEX_CLEAR_STACK,
488 DC_LEX_STACK_LEVEL,
489 DC_LEX_DUPLICATE,
490 DC_LEX_SWAP,
491 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600492
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100493 DC_LEX_ASCIIFY,
494 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600495
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100496 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
497 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
498 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
499 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
500 DC_LEX_LOAD,
501 DC_LEX_LOAD_POP,
502 DC_LEX_STORE_PUSH,
503 DC_LEX_PRINT_POP,
504 DC_LEX_NQUIT,
505 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100506#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600507} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100508// must match order of BC_LEX_KEY_foo etc above
509#if ENABLE_BC
510struct BcLexKeyword {
511 char name8[8];
512};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100513#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100514 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100516 LEX_KW_ENTRY("auto" , 1), // 0
517 LEX_KW_ENTRY("break" , 1), // 1
518 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
519 LEX_KW_ENTRY("define" , 1), // 3
520 LEX_KW_ENTRY("else" , 0), // 4
521 LEX_KW_ENTRY("for" , 1), // 5
522 LEX_KW_ENTRY("halt" , 0), // 6
523 LEX_KW_ENTRY("ibase" , 1), // 7
524 LEX_KW_ENTRY("obase" , 1), // 8
525 LEX_KW_ENTRY("if" , 1), // 9
526 LEX_KW_ENTRY("last" , 0), // 10
527 LEX_KW_ENTRY("length" , 1), // 11
528 LEX_KW_ENTRY("limits" , 0), // 12
529 LEX_KW_ENTRY("print" , 0), // 13
530 LEX_KW_ENTRY("quit" , 1), // 14
531 LEX_KW_ENTRY("read" , 0), // 15
532 LEX_KW_ENTRY("return" , 1), // 16
533 LEX_KW_ENTRY("scale" , 1), // 17
534 LEX_KW_ENTRY("sqrt" , 1), // 18
535 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100536};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100537#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100538#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100539#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100540#define STRING_if (bc_lex_kws[9].name8)
541#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100542enum {
543 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100544 | (1 << 0) // 0
545 | (1 << 1) // 1
546 | (0 << 2) // 2
547 | (1 << 3) // 3
548 | (0 << 4) // 4
549 | (1 << 5) // 5
550 | (0 << 6) // 6
551 | (1 << 7) // 7
552 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100553 | (1 << 9) // 9
554 | (0 << 10) // 10
555 | (1 << 11) // 11
556 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100557 | (0 << 13) // 13
558 | (1 << 14) // 14
559 | (0 << 15) // 15
560 | (1 << 16) // 16
561 | (1 << 17) // 17
562 | (1 << 18) // 18
563 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100564};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100565#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100566
567// This is a bit array that corresponds to token types. An entry is
568// true if the token is valid in an expression, false otherwise.
569// Used to figure out when expr parsing should stop *without error message*
570// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100571// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100572// by later processing.
573enum {
574#define EXBITS(a,b,c,d,e,f,g,h) \
575 ((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 +0100576 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100577 + (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 +0100578 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
579 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100580 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
581 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100582 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
583 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
584 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100585#undef EXBITS
586};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100587static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100588{
589#if ULONG_MAX > 0xffffffff
590 // 64-bit version (will not work correctly for 32-bit longs!)
591 return BC_PARSE_EXPRS_BITS & (1UL << i);
592#else
593 // 32-bit version
594 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
595 if (i >= 32) {
596 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
597 i &= 31;
598 }
599 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100600#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100601}
602
603// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100604// [XC_LEX_1st_op...] token types.
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100605static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100606#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100607 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100608 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 +0100609 OP(2, false), // pow
610 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
611 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612 OP(1, false), // not
613 OP(7, true ), OP( 7, true ), // or and
614 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
615 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100616 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100617#undef OP
618};
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100619#define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f)
620#define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100621#endif // ENABLE_BC
622
623#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100624static const //BcLexType - should be this type
625uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100626dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100627 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100628 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100629 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100630 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100631 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100632 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100633 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100634 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
635 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
636 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100637 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100638 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
639 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100640 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100641 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
642 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100643 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100644 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
645 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100646 // QRSTUVWXY
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100647 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
648 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100649 // Z[\]
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100650 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100651 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100652 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100653 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100654 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
655 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100656 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100657 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100658 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100659 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100660 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
661 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100662 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100663 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100664 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100666};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100667static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100668int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100669dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
670 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
671 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
672 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100673 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
674 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
675 XC_INST_REL_GT, // DC_LEX_LPAREN
676 DC_INST_INVALID, // DC_LEX_SCOLON
677 DC_INST_INVALID, // DC_LEX_READ
678 XC_INST_IBASE, // DC_LEX_IBASE
679 XC_INST_SCALE, // DC_LEX_SCALE
680 XC_INST_OBASE, // DC_LEX_OBASE
681 XC_INST_LENGTH, // DC_LEX_LENGTH
682 XC_INST_PRINT, // DC_LEX_PRINT
683 DC_INST_QUIT, // DC_LEX_QUIT
684 XC_INST_SQRT, // DC_LEX_SQRT
685 XC_INST_REL_GE, // DC_LEX_LBRACE
686 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100687 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
688 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
689 DC_INST_EXECUTE, // DC_LEX_EXECUTE
690 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
691 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
692 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
693 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
694 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
695 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
696 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
697 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
698 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100699 // DC_INST_INVALID in this table either means that corresponding LEX
700 // is not possible for dc, or that it does not compile one-to-one
701 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100702};
703#endif // ENABLE_DC
704
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100705typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100706 smallint lex; // was BcLexType // first member is most used
707 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100708 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100709 const char *lex_inbuf;
710 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100711 const char *lex_filename;
712 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100713 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100714
Gavin Howard01055ba2018-11-03 11:00:21 -0600715 BcFunc *func;
716 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100717 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100718 IF_BC(BcVec exits;)
719 IF_BC(BcVec conds;)
720 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600721} BcParse;
722
Gavin Howard01055ba2018-11-03 11:00:21 -0600723typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600724 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100725 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600726
Denys Vlasenko503faf92018-12-20 16:24:18 +0100727 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600728 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600729 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600730
Gavin Howard01055ba2018-11-03 11:00:21 -0600731 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100732 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600733
734 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100735 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600736
737 BcVec vars;
738 BcVec var_map;
739
740 BcVec arrs;
741 BcVec arr_map;
742
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100743 IF_DC(BcVec strs;)
744 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600745
Gavin Howard01055ba2018-11-03 11:00:21 -0600746 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100747 IF_BC(BcNum one;)
748 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600749} BcProgram;
750
Denys Vlasenko6e618232018-12-25 22:20:14 +0100751struct globals {
752 BcParse prs; // first member is most used
753
754 // For error messages. Can be set to current parsed line,
755 // or [TODO] to current executing line (can be before last parsed one)
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100756 size_t err_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100757
758 BcVec input_buffer;
759
Denys Vlasenko14767602018-12-27 22:52:13 +0100760 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100761 IF_FEATURE_CLEAN_UP(smallint exiting;)
762
763 BcProgram prog;
764
765 BcVec files;
766
767 char *env_args;
768
769#if ENABLE_FEATURE_EDITING
770 line_input_t *line_input_state;
771#endif
772} FIX_ALIASING;
773#define G (*ptr_to_globals)
774#define INIT_G() do { \
775 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
776} while (0)
777#define FREE_G() do { \
778 FREE_PTR_TO_GLOBALS(); \
779} while (0)
780#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
781#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
782#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100783#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100784# define G_interrupt bb_got_signal
785# define G_ttyin G.ttyin
786#else
787# define G_interrupt 0
788# define G_ttyin 0
789#endif
790#if ENABLE_FEATURE_CLEAN_UP
791# define G_exiting G.exiting
792#else
793# define G_exiting 0
794#endif
795#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
796#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
797
Denys Vlasenko6e618232018-12-25 22:20:14 +0100798#if ENABLE_BC
799# define BC_PARSE_REL (1 << 0)
800# define BC_PARSE_PRINT (1 << 1)
801# define BC_PARSE_ARRAY (1 << 2)
802# define BC_PARSE_NOCALL (1 << 3)
803#endif
804
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100805#define BC_PROG_MAIN 0
806#define BC_PROG_READ 1
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100807#if ENABLE_DC
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100808#define BC_PROG_REQ_FUNCS 2
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100809#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600810
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100811#define BC_FLAG_W (1 << 0)
812#define BC_FLAG_V (1 << 1)
813#define BC_FLAG_S (1 << 2)
814#define BC_FLAG_Q (1 << 3)
815#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100816#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
817#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600818
Denys Vlasenko64074a12018-12-07 15:50:14 +0100819#define BC_MAX_OBASE ((unsigned) 999)
820#define BC_MAX_DIM ((unsigned) INT_MAX)
821#define BC_MAX_SCALE ((unsigned) UINT_MAX)
822#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
823#define BC_MAX_NUM BC_MAX_STRING
824// Unused apart from "limits" message. Just show a "biggish number" there.
825//#define BC_MAX_NAME BC_MAX_STRING
826//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
827//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
828#define BC_MAX_NAME_STR "999999999"
829#define BC_MAX_EXP_STR "999999999"
830#define BC_MAX_VARS_STR "999999999"
831
832#define BC_MAX_OBASE_STR "999"
833
834#if INT_MAX == 2147483647
835# define BC_MAX_DIM_STR "2147483647"
836#elif INT_MAX == 9223372036854775807
837# define BC_MAX_DIM_STR "9223372036854775807"
838#else
839# error Strange INT_MAX
840#endif
841
842#if UINT_MAX == 4294967295
843# define BC_MAX_SCALE_STR "4294967295"
844# define BC_MAX_STRING_STR "4294967294"
845#elif UINT_MAX == 18446744073709551615
846# define BC_MAX_SCALE_STR "18446744073709551615"
847# define BC_MAX_STRING_STR "18446744073709551614"
848#else
849# error Strange UINT_MAX
850#endif
851#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600852
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100853// In configurations where errors abort instead of propagating error
854// return code up the call chain, functions returning BC_STATUS
855// actually don't return anything, they always succeed and return "void".
856// A macro wrapper is provided, which makes this statement work:
857// s = zbc_func(...)
858// and makes it visible to the compiler that s is always zero,
859// allowing compiler to optimize dead code after the statement.
860//
861// To make code more readable, each such function has a "z"
862// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
863//
Denys Vlasenko14767602018-12-27 22:52:13 +0100864#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100865# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100866# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100867# define IF_ERROR_RETURN_POSSIBLE(a) a
868# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100869# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100870# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100871#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100872# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100873# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100874# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
875# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100876# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100877# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100878#endif
879
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100880//
881// Utility routines
882//
Gavin Howard01055ba2018-11-03 11:00:21 -0600883
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100884#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
885#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
886
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100887static void fflush_and_check(void)
888{
889 fflush_all();
890 if (ferror(stdout) || ferror(stderr))
891 bb_perror_msg_and_die("output error");
892}
893
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100894#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100895#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100896do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100897 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100898 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100899 return BC_STATUS_FAILURE; \
900} while (0)
901#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100902static void quit(void) NORETURN;
903static void quit(void)
904{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100905 if (ferror(stdin))
906 bb_perror_msg_and_die("input error");
907 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100908 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100909 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100910}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100911#define QUIT_OR_RETURN_TO_MAIN quit()
912#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100913
Denys Vlasenko5318f812018-12-05 17:48:01 +0100914static void bc_verror_msg(const char *fmt, va_list p)
915{
Denys Vlasenko82269122018-12-14 16:30:56 +0100916 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100917 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100918 sv = applet_name;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100919 applet_name = xasprintf("%s: %s:%lu", applet_name,
920 G.prs.lex_filename, (unsigned long)G.err_line
921 );
Denys Vlasenko5318f812018-12-05 17:48:01 +0100922 }
923 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100924 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100925 free((char*)applet_name);
926 applet_name = sv;
927 }
928}
929
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100930static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100931{
932 va_list p;
933
934 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100935 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100936 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100937
Denys Vlasenkoec603182018-12-17 10:34:02 +0100938 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
939 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
940 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100941}
942
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100943#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100944static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100945{
946 va_list p;
947
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100948 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100949 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100950 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100951
952 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100953 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100954 va_end(p);
955
956 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100957 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100958 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
959
Denys Vlasenkoec603182018-12-17 10:34:02 +0100960 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100961 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100962 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100963}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100964#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100965#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100966
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100967// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
968// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100969// function must not have caller-cleaned parameters on stack.
970// Unfortunately, vararg function API does exactly that on most arches.
971// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100972static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100973{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100974 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100975}
Denys Vlasenko22314682019-01-01 21:50:14 +0100976static ERRORFUNC int bc_error_at(const char *msg)
977{
978 const char *err_at = G.prs.lex_next_at;
979 if (err_at) {
980 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt(
981 "%s at '%.*s'",
982 msg,
983 (int)(strchrnul(err_at, '\n') - err_at),
984 err_at
985 );
986 }
987 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
988}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100989static ERRORFUNC int bc_error_bad_character(char c)
990{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100991 if (!c)
992 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100993 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100994}
Denys Vlasenko22314682019-01-01 21:50:14 +0100995static ERRORFUNC int bc_error_bad_function_definition(void)
996{
997 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition");
998}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100999static ERRORFUNC int bc_error_bad_expression(void)
1000{
Denys Vlasenko22314682019-01-01 21:50:14 +01001001 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression");
1002}
1003static ERRORFUNC int bc_error_bad_assignment(void)
1004{
1005 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at(
1006 "bad assignment: left side must be variable or array element"
1007 );
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001008}
1009static ERRORFUNC int bc_error_bad_token(void)
1010{
Denys Vlasenko22314682019-01-01 21:50:14 +01001011 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001012}
1013static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1014{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001015 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001016}
1017static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1018{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001019 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001020}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001021#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001022static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001023{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001024 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001025}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001026#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1027static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001028{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001029 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001030}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001031#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1032static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001033{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001034 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 +01001035}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001036#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)
1037static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001038{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001039 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001040}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001041#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 +01001042#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001043
Gavin Howard01055ba2018-11-03 11:00:21 -06001044static void bc_vec_grow(BcVec *v, size_t n)
1045{
1046 size_t cap = v->cap * 2;
1047 while (cap < v->len + n) cap *= 2;
1048 v->v = xrealloc(v->v, v->size * cap);
1049 v->cap = cap;
1050}
1051
1052static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1053{
1054 v->size = esize;
1055 v->cap = BC_VEC_START_CAP;
1056 v->len = 0;
1057 v->dtor = dtor;
1058 v->v = xmalloc(esize * BC_VEC_START_CAP);
1059}
1060
Denys Vlasenko7d628012018-12-04 21:46:47 +01001061static void bc_char_vec_init(BcVec *v)
1062{
1063 bc_vec_init(v, sizeof(char), NULL);
1064}
1065
Gavin Howard01055ba2018-11-03 11:00:21 -06001066static void bc_vec_expand(BcVec *v, size_t req)
1067{
1068 if (v->cap < req) {
1069 v->v = xrealloc(v->v, v->size * req);
1070 v->cap = req;
1071 }
1072}
1073
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001074static void bc_vec_pop(BcVec *v)
1075{
1076 v->len--;
1077 if (v->dtor)
1078 v->dtor(v->v + (v->size * v->len));
1079}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001080
Gavin Howard01055ba2018-11-03 11:00:21 -06001081static void bc_vec_npop(BcVec *v, size_t n)
1082{
1083 if (!v->dtor)
1084 v->len -= n;
1085 else {
1086 size_t len = v->len - n;
1087 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1088 }
1089}
1090
Denys Vlasenko7d628012018-12-04 21:46:47 +01001091static void bc_vec_pop_all(BcVec *v)
1092{
1093 bc_vec_npop(v, v->len);
1094}
1095
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001096static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001097{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001098 size_t len = v->len;
1099 if (len >= v->cap) bc_vec_grow(v, 1);
1100 memmove(v->v + (v->size * len), data, v->size);
1101 v->len++;
1102 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001103}
1104
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001105// G.prog.results often needs "pop old operand, push result" idiom.
1106// Can do this without a few extra ops
1107static size_t bc_result_pop_and_push(const void *data)
1108{
1109 BcVec *v = &G.prog.results;
1110 char *last;
1111 size_t len = v->len - 1;
1112
1113 last = v->v + (v->size * len);
1114 if (v->dtor)
1115 v->dtor(last);
1116 memmove(last, data, v->size);
1117 return len;
1118}
1119
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001121{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001122 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001123}
1124
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001125static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001126{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001127 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001128 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001129 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001130}
1131
Gavin Howard01055ba2018-11-03 11:00:21 -06001132static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1133{
1134 if (idx == v->len)
1135 bc_vec_push(v, data);
1136 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001137 char *ptr;
1138
1139 if (v->len == v->cap) bc_vec_grow(v, 1);
1140
1141 ptr = v->v + v->size * idx;
1142
1143 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1144 memmove(ptr, data, v->size);
1145 }
1146}
1147
1148static void bc_vec_string(BcVec *v, size_t len, const char *str)
1149{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001150 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001151 bc_vec_expand(v, len + 1);
1152 memcpy(v->v, str, len);
1153 v->len = len;
1154
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001155 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001156}
1157
Gavin Howard01055ba2018-11-03 11:00:21 -06001158static void *bc_vec_item(const BcVec *v, size_t idx)
1159{
1160 return v->v + v->size * idx;
1161}
1162
1163static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1164{
1165 return v->v + v->size * (v->len - idx - 1);
1166}
1167
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001168static void *bc_vec_top(const BcVec *v)
1169{
1170 return v->v + v->size * (v->len - 1);
1171}
1172
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001173static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001174{
1175 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001176 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001177 free(v->v);
1178}
1179
Denys Vlasenko10bde142018-12-27 18:23:58 +01001180static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001181{
1182 return bc_vec_item(&G.prog.fns, idx);
1183}
1184// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001185#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001186
1187#if ENABLE_BC
1188static BcFunc* bc_program_current_func(void)
1189{
1190 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001191 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001192 return func;
1193}
1194#endif
1195
Denys Vlasenko10bde142018-12-27 18:23:58 +01001196static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001197{
1198#if ENABLE_BC
1199 if (IS_BC) {
1200 BcFunc *func = bc_program_current_func();
1201 return bc_vec_item(&func->strs, idx);
1202 }
1203#endif
1204 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1205}
1206
Denys Vlasenko10bde142018-12-27 18:23:58 +01001207static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001208{
1209#if ENABLE_BC
1210 if (IS_BC) {
1211 BcFunc *func = bc_program_current_func();
1212 return bc_vec_item(&func->consts, idx);
1213 }
1214#endif
1215 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1216}
1217
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001218static int bc_id_cmp(const void *e1, const void *e2)
1219{
1220 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1221}
1222
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001223static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001224{
1225 free(((BcId *) id)->name);
1226}
1227
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001228static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001229{
1230 size_t low = 0, high = v->len;
1231
1232 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001233 size_t mid = (low + high) / 2;
1234 BcId *id = bc_vec_item(v, mid);
1235 int result = bc_id_cmp(ptr, id);
1236
1237 if (result == 0)
1238 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001239 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001240 high = mid;
1241 else
1242 low = mid + 1;
1243 }
1244
1245 return low;
1246}
1247
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001248static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001249{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001250 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001251
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001252 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001253 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001254 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1255 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001256 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001257 bc_vec_pushAt(v, ptr, n);
1258 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001259}
1260
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001261#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001262static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001263{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001264 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001265 if (i >= v->len) return BC_VEC_INVALID_IDX;
1266 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1267}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001268#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001269
Gavin Howard01055ba2018-11-03 11:00:21 -06001270static void bc_num_setToZero(BcNum *n, size_t scale)
1271{
1272 n->len = 0;
1273 n->neg = false;
1274 n->rdx = scale;
1275}
1276
1277static void bc_num_zero(BcNum *n)
1278{
1279 bc_num_setToZero(n, 0);
1280}
1281
1282static void bc_num_one(BcNum *n)
1283{
1284 bc_num_setToZero(n, 0);
1285 n->len = 1;
1286 n->num[0] = 1;
1287}
1288
Denys Vlasenko3129f702018-12-09 12:04:44 +01001289// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001290static void bc_num_init(BcNum *n, size_t req)
1291{
1292 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001293 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001294 n->num = xmalloc(req);
1295 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001296 n->rdx = 0;
1297 n->len = 0;
1298 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001299}
1300
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001301static void bc_num_init_DEF_SIZE(BcNum *n)
1302{
1303 bc_num_init(n, BC_NUM_DEF_SIZE);
1304}
1305
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001306static void bc_num_expand(BcNum *n, size_t req)
1307{
1308 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1309 if (req > n->cap) {
1310 n->num = xrealloc(n->num, req);
1311 n->cap = req;
1312 }
1313}
1314
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001315static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001316{
1317 free(((BcNum *) num)->num);
1318}
1319
1320static void bc_num_copy(BcNum *d, BcNum *s)
1321{
1322 if (d != s) {
1323 bc_num_expand(d, s->cap);
1324 d->len = s->len;
1325 d->neg = s->neg;
1326 d->rdx = s->rdx;
1327 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1328 }
1329}
1330
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001331static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001332{
1333 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001334 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001335
Denys Vlasenko1557b762018-12-22 21:37:46 +01001336 result = 0;
1337 i = n->len;
1338 while (i > n->rdx) {
1339 unsigned long prev = result;
1340 result = result * 10 + n->num[--i];
1341 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1342 // 0x1ff00000 * 10 is 0x13f600000,
1343 // or 0x3f600000 truncated to 32 bits. Which is larger.
1344 // However, (N*10)/8 < N check is always correct.
1345 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001346 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001348 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001349
Denys Vlasenko29301232018-12-11 15:29:32 +01001350 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001351}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001352#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1353
1354static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1355{
1356 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1357
1358 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1359}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001360#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001361
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001362#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1363# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1364#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1365# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1366#endif
1367// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1368// would not hit realloc() code path - not good if num[] is not malloced
1369
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001370static void bc_num_ulong2num(BcNum *n, unsigned long val)
1371{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001372 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001373
1374 bc_num_zero(n);
1375
1376 if (val == 0) return;
1377
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001378 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001379
1380 ptr = n->num;
1381 for (;;) {
1382 n->len++;
1383 *ptr++ = val % 10;
1384 val /= 10;
1385 if (val == 0) break;
1386 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001387}
1388
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001389static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001390{
1391 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001392 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001393 a[i] -= b[i];
1394 for (j = i; a[j] < 0;) {
1395 a[j++] += 10;
1396 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001397 }
1398 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001399}
1400
1401static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1402{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001403 size_t i = len;
1404 for (;;) {
1405 int c;
1406 if (i == 0)
1407 return 0;
1408 i--;
1409 c = a[i] - b[i];
1410 if (c != 0) {
1411 i++;
1412 if (c < 0)
1413 return -i;
1414 return i;
1415 }
1416 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001417}
1418
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001419#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1420#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1421#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1422//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1423static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1424{
1425 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1426}
1427//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1428static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1429{
1430 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1431}
1432
Gavin Howard01055ba2018-11-03 11:00:21 -06001433static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1434{
1435 size_t i, min, a_int, b_int, diff;
1436 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001437 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001438 ssize_t cmp;
1439
1440 if (a == b) return 0;
1441 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1442 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001443
1444 if (a->neg != b->neg) // signs of a and b differ
1445 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1446 return (int)b->neg - (int)a->neg;
1447 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001448
1449 a_int = BC_NUM_INT(a);
1450 b_int = BC_NUM_INT(b);
1451 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001452
1453 if (a_int != 0) return (ssize_t) a_int;
1454
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001455 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001456 if (a_max) {
1457 min = b->rdx;
1458 diff = a->rdx - b->rdx;
1459 max_num = a->num + diff;
1460 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001461 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1462 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001463 min = a->rdx;
1464 diff = b->rdx - a->rdx;
1465 max_num = b->num + diff;
1466 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001467 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001468 }
1469
1470 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001471 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001472
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001473 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001474 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001475 }
1476
1477 return 0;
1478}
1479
1480static void bc_num_truncate(BcNum *n, size_t places)
1481{
1482 if (places == 0) return;
1483
1484 n->rdx -= places;
1485
1486 if (n->len != 0) {
1487 n->len -= places;
1488 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1489 }
1490}
1491
1492static void bc_num_extend(BcNum *n, size_t places)
1493{
1494 size_t len = n->len + places;
1495
1496 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001497 if (n->cap < len) bc_num_expand(n, len);
1498
1499 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1500 memset(n->num, 0, sizeof(BcDig) * places);
1501
1502 n->len += places;
1503 n->rdx += places;
1504 }
1505}
1506
1507static void bc_num_clean(BcNum *n)
1508{
1509 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1510 if (n->len == 0)
1511 n->neg = false;
1512 else if (n->len < n->rdx)
1513 n->len = n->rdx;
1514}
1515
1516static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1517{
1518 if (n->rdx < scale)
1519 bc_num_extend(n, scale - n->rdx);
1520 else
1521 bc_num_truncate(n, n->rdx - scale);
1522
1523 bc_num_clean(n);
1524 if (n->len != 0) n->neg = !neg1 != !neg2;
1525}
1526
1527static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1528 BcNum *restrict b)
1529{
1530 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001531 b->len = n->len - idx;
1532 a->len = idx;
1533 a->rdx = b->rdx = 0;
1534
1535 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1536 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001537 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001538 bc_num_zero(b);
1539 bc_num_copy(a, n);
1540 }
1541
1542 bc_num_clean(a);
1543 bc_num_clean(b);
1544}
1545
Denys Vlasenko29301232018-12-11 15:29:32 +01001546static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001547{
Denys Vlasenko29301232018-12-11 15:29:32 +01001548 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001549
1550 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1551 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1552 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001553 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001554 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001555
1556 if (n->rdx >= places)
1557 n->rdx -= places;
1558 else {
1559 bc_num_extend(n, places - n->rdx);
1560 n->rdx = 0;
1561 }
1562
1563 bc_num_clean(n);
1564
Denys Vlasenko29301232018-12-11 15:29:32 +01001565 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001566}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001567#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001568
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001569typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1570
1571static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1572 BcNumBinaryOp op, size_t req)
1573{
1574 BcStatus s;
1575 BcNum num2, *ptr_a, *ptr_b;
1576 bool init = false;
1577
1578 if (c == a) {
1579 ptr_a = &num2;
1580 memcpy(ptr_a, c, sizeof(BcNum));
1581 init = true;
1582 } else
1583 ptr_a = a;
1584
1585 if (c == b) {
1586 ptr_b = &num2;
1587 if (c != a) {
1588 memcpy(ptr_b, c, sizeof(BcNum));
1589 init = true;
1590 }
1591 } else
1592 ptr_b = b;
1593
1594 if (init)
1595 bc_num_init(c, req);
1596 else
1597 bc_num_expand(c, req);
1598
1599 s = BC_STATUS_SUCCESS;
1600 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1601
1602 if (init) bc_num_free(&num2);
1603
1604 RETURN_STATUS(s);
1605}
1606#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1607
1608static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1609static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1610static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1611static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1612static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1613static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1614
1615static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1616{
1617 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1618 (void) scale;
1619 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1620}
1621
1622static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1623{
1624 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1625 (void) scale;
1626 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1627}
1628
1629static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1630{
1631 size_t req = BC_NUM_MREQ(a, b, scale);
1632 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1633}
1634
1635static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1636{
1637 size_t req = BC_NUM_MREQ(a, b, scale);
1638 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1639}
1640
1641static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1642{
1643 size_t req = BC_NUM_MREQ(a, b, scale);
1644 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1645}
1646
1647static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1648{
1649 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1650}
1651
Denys Vlasenko10bde142018-12-27 18:23:58 +01001652static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001653 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1654};
1655#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1656#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1657#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1658#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1659#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1660#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1661
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001662static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001663{
1664 BcNum one;
1665 BcDig num[2];
1666
1667 one.cap = 2;
1668 one.num = num;
1669 bc_num_one(&one);
1670
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001671 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001672}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001673#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001674
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001675static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001676{
1677 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1678 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001679 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001680
1681 // Because this function doesn't need to use scale (per the bc spec),
1682 // I am hijacking it to say whether it's doing an add or a subtract.
1683
1684 if (a->len == 0) {
1685 bc_num_copy(c, b);
1686 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001687 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001688 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001689 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001690 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001691 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001692 }
1693
1694 c->neg = a->neg;
1695 c->rdx = BC_MAX(a->rdx, b->rdx);
1696 min_rdx = BC_MIN(a->rdx, b->rdx);
1697 c->len = 0;
1698
1699 if (a->rdx > b->rdx) {
1700 diff = a->rdx - b->rdx;
1701 ptr = a->num;
1702 ptr_a = a->num + diff;
1703 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001704 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001705 diff = b->rdx - a->rdx;
1706 ptr = b->num;
1707 ptr_a = a->num;
1708 ptr_b = b->num + diff;
1709 }
1710
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001711 ptr_c = c->num;
1712 for (i = 0; i < diff; ++i, ++c->len)
1713 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001714
1715 ptr_c += diff;
1716 a_int = BC_NUM_INT(a);
1717 b_int = BC_NUM_INT(b);
1718
1719 if (a_int > b_int) {
1720 min_int = b_int;
1721 max = a_int;
1722 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001723 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001724 min_int = a_int;
1725 max = b_int;
1726 ptr = ptr_b;
1727 }
1728
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001729 carry = 0;
1730 for (i = 0; i < min_rdx + min_int; ++i) {
1731 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001732 carry = in / 10;
1733 ptr_c[i] = (BcDig)(in % 10);
1734 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001735 for (; i < max + min_rdx; ++i) {
1736 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001737 carry = in / 10;
1738 ptr_c[i] = (BcDig)(in % 10);
1739 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001740 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001741
1742 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1743
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001744 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001745}
1746
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001747static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001748{
Gavin Howard01055ba2018-11-03 11:00:21 -06001749 ssize_t cmp;
1750 BcNum *minuend, *subtrahend;
1751 size_t start;
1752 bool aneg, bneg, neg;
1753
1754 // Because this function doesn't need to use scale (per the bc spec),
1755 // I am hijacking it to say whether it's doing an add or a subtract.
1756
1757 if (a->len == 0) {
1758 bc_num_copy(c, b);
1759 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001760 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001761 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001763 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001764 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001765 }
1766
1767 aneg = a->neg;
1768 bneg = b->neg;
1769 a->neg = b->neg = false;
1770
1771 cmp = bc_num_cmp(a, b);
1772
1773 a->neg = aneg;
1774 b->neg = bneg;
1775
1776 if (cmp == 0) {
1777 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001778 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001779 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001780 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001781 neg = a->neg;
1782 minuend = a;
1783 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001784 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001785 neg = b->neg;
1786 if (sub) neg = !neg;
1787 minuend = b;
1788 subtrahend = a;
1789 }
1790
1791 bc_num_copy(c, minuend);
1792 c->neg = neg;
1793
1794 if (c->rdx < subtrahend->rdx) {
1795 bc_num_extend(c, subtrahend->rdx - c->rdx);
1796 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001797 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001798 start = c->rdx - subtrahend->rdx;
1799
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001800 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001801
1802 bc_num_clean(c);
1803
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001804 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001805}
1806
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001807static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001808 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001809#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001810{
1811 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001812 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001814 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001815
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 if (a->len == 0 || b->len == 0) {
1817 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001818 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001819 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001820 aone = BC_NUM_ONE(a);
1821 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001823 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001824 }
1825
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001826 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1827 || a->len < BC_NUM_KARATSUBA_LEN
1828 || b->len < BC_NUM_KARATSUBA_LEN
1829 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001830 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001831
Gavin Howard01055ba2018-11-03 11:00:21 -06001832 bc_num_expand(c, a->len + b->len + 1);
1833
1834 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001835 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001836
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001837 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001838 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001839 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001840 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001841 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001842 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001843 carry = in / 10;
1844 c->num[i + j] = (BcDig)(in % 10);
1845 }
1846
1847 c->num[i + j] += (BcDig) carry;
1848 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001849
Denys Vlasenko14767602018-12-27 22:52:13 +01001850#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001851 // a=2^1000000
1852 // a*a <- without check below, this will not be interruptible
1853 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001854#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001855 }
1856
1857 c->len = len;
1858
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001859 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001860 }
1861
1862 bc_num_init(&l1, max);
1863 bc_num_init(&h1, max);
1864 bc_num_init(&l2, max);
1865 bc_num_init(&h2, max);
1866 bc_num_init(&m1, max);
1867 bc_num_init(&m2, max);
1868 bc_num_init(&z0, max);
1869 bc_num_init(&z1, max);
1870 bc_num_init(&z2, max);
1871 bc_num_init(&temp, max + max);
1872
1873 bc_num_split(a, max2, &l1, &h1);
1874 bc_num_split(b, max2, &l2, &h2);
1875
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001876 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001878 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001879 if (s) goto err;
1880
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001881 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001882 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001883 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001884 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001885 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 if (s) goto err;
1887
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001888 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001889 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001890 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 if (s) goto err;
1892
Denys Vlasenko29301232018-12-11 15:29:32 +01001893 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001894 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001895 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001896 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001897 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001899 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001900 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001901 bc_num_free(&temp);
1902 bc_num_free(&z2);
1903 bc_num_free(&z1);
1904 bc_num_free(&z0);
1905 bc_num_free(&m2);
1906 bc_num_free(&m1);
1907 bc_num_free(&h2);
1908 bc_num_free(&l2);
1909 bc_num_free(&h1);
1910 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001911 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001912}
1913
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001914static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001915{
1916 BcStatus s;
1917 BcNum cpa, cpb;
1918 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1919
1920 scale = BC_MAX(scale, a->rdx);
1921 scale = BC_MAX(scale, b->rdx);
1922 scale = BC_MIN(a->rdx + b->rdx, scale);
1923 maxrdx = BC_MAX(maxrdx, scale);
1924
1925 bc_num_init(&cpa, a->len);
1926 bc_num_init(&cpb, b->len);
1927
1928 bc_num_copy(&cpa, a);
1929 bc_num_copy(&cpb, b);
1930 cpa.neg = cpb.neg = false;
1931
Denys Vlasenko29301232018-12-11 15:29:32 +01001932 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001933 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001934 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001935 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001936 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001937 if (s) goto err;
1938
1939 maxrdx += scale;
1940 bc_num_expand(c, c->len + maxrdx);
1941
1942 if (c->len < maxrdx) {
1943 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1944 c->len += maxrdx;
1945 }
1946
1947 c->rdx = maxrdx;
1948 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001949 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 bc_num_free(&cpb);
1951 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001952 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001953}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001954#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001955
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001956static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001957{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001958 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 size_t len, end, i;
1960 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001961
1962 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001963 RETURN_STATUS(bc_error("divide by zero"));
1964 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001966 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001967 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001968 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001969 bc_num_copy(c, a);
1970 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001971 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001972 }
1973
1974 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1975 bc_num_copy(&cp, a);
1976 len = b->len;
1977
1978 if (len > cp.len) {
1979 bc_num_expand(&cp, len + 2);
1980 bc_num_extend(&cp, len - cp.len);
1981 }
1982
1983 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1984 cp.rdx -= b->rdx;
1985 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1986
1987 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001988 for (;;) {
1989 if (len == 0) break;
1990 len--;
1991 if (b->num[len] != 0)
1992 break;
1993 }
1994 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001995 }
1996
1997 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1998
1999 // We want an extra zero in front to make things simpler.
2000 cp.num[cp.len++] = 0;
2001 end = cp.len - len;
2002
2003 bc_num_expand(c, cp.len);
2004
2005 bc_num_zero(c);
2006 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2007 c->rdx = cp.rdx;
2008 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002009
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002010 s = BC_STATUS_SUCCESS;
2011 for (i = end - 1; i < end; --i) {
2012 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002014 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2015 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002016 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01002017#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01002018 // a=2^100000
2019 // scale=40000
2020 // 1/a <- without check below, this will not be interruptible
2021 if (G_interrupt) {
2022 s = BC_STATUS_FAILURE;
2023 break;
2024 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002025#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002026 }
2027
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002028 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002029 bc_num_free(&cp);
2030
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002031 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002032}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002033#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002034
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002035static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002036 BcNum *restrict d, size_t scale, size_t ts)
2037{
2038 BcStatus s;
2039 BcNum temp;
2040 bool neg;
2041
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002042 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002043 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002044
2045 if (a->len == 0) {
2046 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002047 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002048 }
2049
2050 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002051 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002052 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002053
2054 if (scale != 0) scale = ts;
2055
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002056 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002057 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002058 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002059 if (s) goto err;
2060
2061 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2062
2063 neg = d->neg;
2064 bc_num_retireMul(d, ts, a->neg, b->neg);
2065 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002066 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002067 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002068 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002069}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002070#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002071
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002072static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002073{
2074 BcStatus s;
2075 BcNum c1;
2076 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2077
2078 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002079 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002080 bc_num_free(&c1);
2081
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002082 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002083}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002084#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002085
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002086static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002087{
2088 BcStatus s = BC_STATUS_SUCCESS;
2089 BcNum copy;
2090 unsigned long pow;
2091 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002092 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002093
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002094 // GNU bc does not allow 2^2.0 - we do
2095 for (i = 0; i < b->rdx; i++)
2096 if (b->num[i] != 0)
2097 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002098
2099 if (b->len == 0) {
2100 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002102 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002103 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002104 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002105 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002106 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002107 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002108 if (!b->neg)
2109 bc_num_copy(c, a);
2110 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002111 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002112 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002113 }
2114
2115 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002116 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002117 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002118 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002119
2120 bc_num_init(&copy, a->len);
2121 bc_num_copy(&copy, a);
2122
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002123 if (!neg) {
2124 if (a->rdx > scale)
2125 scale = a->rdx;
2126 if (a->rdx * pow < scale)
2127 scale = a->rdx * pow;
2128 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002129
Gavin Howard01055ba2018-11-03 11:00:21 -06002130
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002131 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002132 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002133 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002134 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002135 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002136 //if (G_interrupt) {
2137 // s = BC_STATUS_FAILURE;
2138 // goto err;
2139 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002140 }
2141
Gavin Howard01055ba2018-11-03 11:00:21 -06002142 bc_num_copy(c, &copy);
2143
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002144 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002145 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002146 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 if (s) goto err;
2148
2149 if (pow & 1) {
2150 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002151 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 if (s) goto err;
2153 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002154 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002155 //if (G_interrupt) {
2156 // s = BC_STATUS_FAILURE;
2157 // goto err;
2158 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 }
2160
2161 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002162 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002163 if (s) goto err;
2164 }
2165
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2167
2168 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002169 for (i = 0; i < c->len; ++i)
2170 if (c->num[i] != 0)
2171 goto skip;
2172 bc_num_setToZero(c, scale);
2173 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002174
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002175 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002176 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002177 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002178}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002179#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002180
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002181static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002182{
2183 BcStatus s;
2184 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002185 BcDig half_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002186 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2187 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2188
2189 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2190 bc_num_expand(b, req);
2191
2192 if (a->len == 0) {
2193 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002194 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002195 }
2196 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002197 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002198 }
2199 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002200 bc_num_one(b);
2201 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002202 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002203 }
2204
2205 scale = BC_MAX(scale, a->rdx) + 1;
2206 len = a->len + scale;
2207
2208 bc_num_init(&num1, len);
2209 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002210
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002211 half.cap = ARRAY_SIZE(half_digs);
2212 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002213 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002214 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 half.rdx = 1;
2216
2217 bc_num_init(&f, len);
2218 bc_num_init(&fprime, len);
2219
2220 x0 = &num1;
2221 x1 = &num2;
2222
2223 bc_num_one(x0);
2224 pow = BC_NUM_INT(a);
2225
2226 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002227 if (pow & 1)
2228 x0->num[0] = 2;
2229 else
2230 x0->num[0] = 6;
2231
2232 pow -= 2 - (pow & 1);
2233
2234 bc_num_extend(x0, pow);
2235
2236 // Make sure to move the radix back.
2237 x0->rdx -= pow;
2238 }
2239
2240 x0->rdx = digs = digs1 = 0;
2241 resrdx = scale + 2;
2242 len = BC_NUM_INT(x0) + resrdx - 1;
2243
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002244 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002245 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002246 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002247 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002248 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002249 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002250 if (s) goto err;
2251
2252 cmp = bc_num_cmp(x1, x0);
2253 digs = x1->len - (unsigned long long) llabs(cmp);
2254
2255 if (cmp == cmp2 && digs == digs1)
2256 times += 1;
2257 else
2258 times = 0;
2259
2260 resrdx += times > 4;
2261
2262 cmp2 = cmp1;
2263 cmp1 = cmp;
2264 digs1 = digs;
2265
2266 temp = x0;
2267 x0 = x1;
2268 x1 = temp;
2269 }
2270
Gavin Howard01055ba2018-11-03 11:00:21 -06002271 bc_num_copy(b, x0);
2272 scale -= 1;
2273 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002274 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002275 bc_num_free(&fprime);
2276 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002277 bc_num_free(&num2);
2278 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002279 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002280}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002281#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002282
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002283static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002284 size_t scale)
2285{
2286 BcStatus s;
2287 BcNum num2, *ptr_a;
2288 bool init = false;
2289 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2290
2291 if (c == a) {
2292 memcpy(&num2, c, sizeof(BcNum));
2293 ptr_a = &num2;
2294 bc_num_init(c, len);
2295 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002296 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002297 ptr_a = a;
2298 bc_num_expand(c, len);
2299 }
2300
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002301 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002302
2303 if (init) bc_num_free(&num2);
2304
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002305 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002306}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002307#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002308
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002309#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002310static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002311{
2312 BcStatus s;
2313 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002314 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002315
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002316 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002317 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002318 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002319 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002320 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002321 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002322
2323 bc_num_expand(d, c->len);
2324 bc_num_init(&base, c->len);
2325 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002326 bc_num_init(&temp, b->len);
2327
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002328 two.cap = ARRAY_SIZE(two_digs);
2329 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002330 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002331 two_digs[0] = 2;
2332
Gavin Howard01055ba2018-11-03 11:00:21 -06002333 bc_num_one(d);
2334
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002335 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002336 if (s) goto err;
2337 bc_num_copy(&exp, b);
2338
2339 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002340 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002341 if (s) goto err;
2342
2343 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002344 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002345 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002346 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002347 if (s) goto err;
2348 }
2349
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002350 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002351 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002352 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002353 if (s) goto err;
2354 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002355 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002356 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002357 bc_num_free(&exp);
2358 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002359 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002360}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002361#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002362#endif // ENABLE_DC
2363
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002364static FAST_FUNC void bc_string_free(void *string)
2365{
2366 free(*(char**)string);
2367}
2368
Gavin Howard01055ba2018-11-03 11:00:21 -06002369static void bc_func_init(BcFunc *f)
2370{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002371 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002372 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2373 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002374 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2375 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002376 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002377}
2378
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002379static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002380{
2381 BcFunc *f = (BcFunc *) func;
2382 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002383 IF_BC(bc_vec_free(&f->labels);)
2384 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002385 IF_BC(bc_vec_free(&f->strs);)
2386 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002387}
2388
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002389static void bc_array_expand(BcVec *a, size_t len);
2390
Gavin Howard01055ba2018-11-03 11:00:21 -06002391static void bc_array_init(BcVec *a, bool nums)
2392{
2393 if (nums)
2394 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2395 else
2396 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2397 bc_array_expand(a, 1);
2398}
2399
Gavin Howard01055ba2018-11-03 11:00:21 -06002400static void bc_array_expand(BcVec *a, size_t len)
2401{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002402 if (a->dtor == bc_num_free
2403 // && a->size == sizeof(BcNum) - always true
2404 ) {
2405 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002406 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002407 bc_num_init_DEF_SIZE(&n);
2408 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002409 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002410 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002411 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002412 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002413 bc_array_init(&v, true);
2414 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002415 }
2416 }
2417}
2418
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002419static void bc_array_copy(BcVec *d, const BcVec *s)
2420{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002421 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002422 size_t i;
2423
2424 bc_vec_pop_all(d);
2425 bc_vec_expand(d, s->cap);
2426 d->len = s->len;
2427
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002428 dnum = (void*)d->v;
2429 snum = (void*)s->v;
2430 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002431 bc_num_init(dnum, snum->len);
2432 bc_num_copy(dnum, snum);
2433 }
2434}
2435
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002436#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002437static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002438{
2439 d->t = src->t;
2440
2441 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002442 case XC_RESULT_TEMP:
2443 case XC_RESULT_IBASE:
2444 case XC_RESULT_SCALE:
2445 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002446 bc_num_init(&d->d.n, src->d.n.len);
2447 bc_num_copy(&d->d.n, &src->d.n);
2448 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002449 case XC_RESULT_VAR:
2450 case XC_RESULT_ARRAY:
2451 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002452 d->d.id.name = xstrdup(src->d.id.name);
2453 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002454 case XC_RESULT_CONSTANT:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002455 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002456 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2457 break;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002458 default: // placate compiler
2459 // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen
2460 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002461 }
2462}
2463#endif // ENABLE_DC
2464
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002465static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002466{
2467 BcResult *r = (BcResult *) result;
2468
2469 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002470 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002471 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002472 case XC_RESULT_IBASE:
2473 case XC_RESULT_SCALE:
2474 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002475 bc_num_free(&r->d.n);
2476 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002477 case XC_RESULT_VAR:
2478 case XC_RESULT_ARRAY:
2479 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002480 free(r->d.id.name);
2481 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002482 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002483 // Do nothing.
2484 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002485 }
2486}
2487
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002488static int bad_input_byte(char c)
2489{
2490 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2491 || c > 0x7e
2492 ) {
2493 bc_error_fmt("illegal character 0x%02x", c);
2494 return 1;
2495 }
2496 return 0;
2497}
2498
Denys Vlasenko10bde142018-12-27 18:23:58 +01002499static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002500{
2501 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002502 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002503 fflush_and_check();
2504
Denys Vlasenko14767602018-12-27 22:52:13 +01002505#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002506 if (G_interrupt) { // ^C was pressed
2507 intr:
2508 if (fp != stdin) {
2509 // ^C while running a script (bc SCRIPT): die.
2510 // We do not return to interactive prompt:
2511 // user might be running us from a shell,
2512 // and SCRIPT might be intended to terminate
2513 // (e.g. contain a "halt" stmt).
2514 // ^C dropping user into a bc prompt instead of
2515 // the shell would be unexpected.
2516 xfunc_die();
2517 }
2518 // ^C while interactive input
2519 G_interrupt = 0;
2520 // GNU bc says "interrupted execution."
2521 // GNU dc says "Interrupt!"
2522 fputs("\ninterrupted execution\n", stderr);
2523 }
2524
2525# if ENABLE_FEATURE_EDITING
2526 if (G_ttyin && fp == stdin) {
2527 int n, i;
2528# define line_buf bb_common_bufsiz1
2529 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2530 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2531 if (n == 0) // ^C
2532 goto intr;
2533 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2534 return;
2535 }
2536 i = 0;
2537 for (;;) {
2538 char c = line_buf[i++];
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002539 if (c == '\0') break;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002540 if (bad_input_byte(c)) goto again;
2541 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002542 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002543# undef line_buf
2544 } else
2545# endif
2546#endif
2547 {
2548 int c;
2549 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002550
2551 do {
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002552 get_char:
Denys Vlasenko14767602018-12-27 22:52:13 +01002553#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002554 if (G_interrupt) {
2555 // ^C was pressed: ignore entire line, get another one
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002556 goto again;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002557 }
2558#endif
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002559 c = fgetc(fp);
2560 if (c == '\0')
2561 goto get_char;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002562 if (c == EOF) {
2563 if (ferror(fp))
2564 bb_perror_msg_and_die("input error");
2565 // Note: EOF does not append '\n'
2566 break;
2567 }
2568 bad_chars |= bad_input_byte(c);
2569 bc_vec_pushByte(vec, (char)c);
2570 } while (c != '\n');
2571
2572 if (bad_chars) {
2573 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002574 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002575 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002576 goto again;
2577 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002578 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002579 }
2580 bc_vec_pushZeroByte(vec);
2581 }
2582}
2583
2584//
2585// Parsing routines
2586//
2587
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002588// "Input numbers may contain the characters 0-9 and A-Z.
2589// (Note: They must be capitals. Lower case letters are variable names.)
2590// Single digit numbers always have the value of the digit regardless of
2591// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2592// all input digits greater or equal to ibase to the value of ibase-1.
2593// This makes the number ZZZ always be the largest 3 digit number of the
2594// input base."
2595static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002596{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002597 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002598 for (;;) {
2599 BcDig c = *val++;
2600 if (c == '\0')
2601 break;
2602 if (c == '.') {
2603 if (radix) return false;
2604 radix = true;
2605 continue;
2606 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002607 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002608 return false;
2609 }
2610 return true;
2611}
2612
2613// Note: n is already "bc_num_zero()"ed,
2614// leading zeroes in "val" are removed
2615static void bc_num_parseDecimal(BcNum *n, const char *val)
2616{
2617 size_t len, i;
2618 const char *ptr;
2619
2620 len = strlen(val);
2621 if (len == 0)
2622 return;
2623
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002624 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002625
2626 ptr = strchr(val, '.');
2627
2628 n->rdx = 0;
2629 if (ptr != NULL)
2630 n->rdx = (size_t)((val + len) - (ptr + 1));
2631
2632 for (i = 0; val[i]; ++i) {
2633 if (val[i] != '0' && val[i] != '.') {
2634 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002635 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002636 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002637 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002638 if (c > 9) { // A-Z => 10-36
2639 n->len = 2;
2640 c -= ('A' - '9' - 1);
2641 n->num[1] = c/10;
2642 c = c%10;
2643 }
2644 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002645 break;
2646 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002647 i = len - 1;
2648 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002649 char c = val[i] - '0';
2650 if (c > 9) // A-Z => 9
2651 c = 9;
2652 n->num[n->len] = c;
2653 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002654 skip_dot:
2655 if (i == 0) break;
2656 if (val[--i] == '.') goto skip_dot;
2657 }
2658 break;
2659 }
2660 }
2661 // if for() exits without hitting if(), the value is entirely zero
2662}
2663
2664// Note: n is already "bc_num_zero()"ed,
2665// leading zeroes in "val" are removed
2666static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2667{
2668 BcStatus s;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002669 BcNum mult, result;
2670 BcNum temp;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002671 BcNum base;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002672 BcDig temp_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002673 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002674 size_t digits;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002675
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002676 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002677
2678 temp.cap = ARRAY_SIZE(temp_digs);
2679 temp.num = temp_digs;
2680
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002681 base.cap = ARRAY_SIZE(base_digs);
2682 base.num = base_digs;
2683 bc_num_ulong2num(&base, base_t);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002684 base_t--;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002685
2686 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002687 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002688 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002689
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002690 c = *val++;
2691 if (c == '\0') goto int_err;
2692 if (c == '.') break;
2693
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002694 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2695 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002696
2697 s = zbc_num_mul(n, &base, &mult, 0);
2698 if (s) goto int_err;
2699 bc_num_ulong2num(&temp, v);
2700 s = zbc_num_add(&mult, &temp, n, 0);
2701 if (s) goto int_err;
2702 }
2703
2704 bc_num_init(&result, base.len);
2705 //bc_num_zero(&result); - already is
2706 bc_num_one(&mult);
2707
2708 digits = 0;
2709 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002710 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002711 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002712
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002713 c = *val++;
2714 if (c == '\0') break;
2715 digits++;
2716
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002717 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2718 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002719
2720 s = zbc_num_mul(&result, &base, &result, 0);
2721 if (s) goto err;
2722 bc_num_ulong2num(&temp, v);
2723 s = zbc_num_add(&result, &temp, &result, 0);
2724 if (s) goto err;
2725 s = zbc_num_mul(&mult, &base, &mult, 0);
2726 if (s) goto err;
2727 }
2728
2729 s = zbc_num_div(&result, &mult, &result, digits);
2730 if (s) goto err;
2731 s = zbc_num_add(n, &result, n, digits);
2732 if (s) goto err;
2733
2734 if (n->len != 0) {
2735 if (n->rdx < digits)
2736 bc_num_extend(n, digits - n->rdx);
2737 } else
2738 bc_num_zero(n);
2739 err:
2740 bc_num_free(&result);
2741 int_err:
2742 bc_num_free(&mult);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002743}
2744
Denys Vlasenko10bde142018-12-27 18:23:58 +01002745static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002746{
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002747 size_t i;
2748
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002749 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002750 RETURN_STATUS(bc_error("bad number string"));
2751
2752 bc_num_zero(n);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002753 while (*val == '0')
2754 val++;
2755 for (i = 0; ; ++i) {
2756 if (val[i] == '\0')
2757 RETURN_STATUS(BC_STATUS_SUCCESS);
2758 if (val[i] != '.' && val[i] != '0')
2759 break;
2760 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002761
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002762 if (base_t == 10 || val[1] == '\0')
2763 // Decimal, or single-digit number
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002764 bc_num_parseDecimal(n, val);
2765 else
2766 bc_num_parseBase(n, val, base_t);
2767
2768 RETURN_STATUS(BC_STATUS_SUCCESS);
2769}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002770#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002771
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002772// p->lex_inbuf points to the current string to be parsed.
2773// if p->lex_inbuf points to '\0', it's either EOF or it points after
2774// last processed line's terminating '\n' (and more reading needs to be done
2775// to get next character).
2776//
2777// If you are in a situation where that is a possibility, call peek_inbuf().
2778// If necessary, it performs more reading and changes p->lex_inbuf,
2779// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2780// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2781// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2782//
2783// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2784// it returns current char and advances the pointer (if not EOF).
2785// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2786//
2787// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2788// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2789// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002790//
2791// bc cases to test interactively:
2792// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2793// 1 #comment/* - prints "1<newline>" at once
2794// 1 #comment" - prints "1<newline>" at once
2795// 1\#comment - error at once (\ is not a line continuation)
2796// 1 + /*"*/2 - prints "3<newline>" at once
2797// 1 + /*#*/2 - prints "3<newline>" at once
2798// "str\" - prints "str\" at once
2799// "str#" - prints "str#" at once
2800// "str/*" - prints "str/*" at once
2801// "str#\ - waits for second line
2802// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002803static char peek_inbuf(void)
2804{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002805 if (*G.prs.lex_inbuf == '\0'
2806 && G.prs.lex_input_fp
2807 ) {
2808 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2809 G.prs.lex_inbuf = G.input_buffer.v;
2810 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2811 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002812 }
2813 return *G.prs.lex_inbuf;
2814}
2815static char eat_inbuf(void)
2816{
2817 char c = peek_inbuf();
2818 if (c) G.prs.lex_inbuf++;
2819 return c;
2820}
2821
Denys Vlasenko10bde142018-12-27 18:23:58 +01002822static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002823{
2824 BcParse *p = &G.prs;
2825 char c;
2826
2827 // Try: echo -n '#foo' | bc
2828 p->lex = XC_LEX_WHITESPACE;
2829
Denys Vlasenko2747f612018-12-31 18:48:10 +01002830 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002831 // '\0' which isn't the EOF can only be seen after '\n'.
2832 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2833 p->lex_inbuf++;
2834}
2835
Denys Vlasenko10bde142018-12-27 18:23:58 +01002836static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002837{
2838 BcParse *p = &G.prs;
2839
2840 p->lex = XC_LEX_WHITESPACE;
2841 for (;;) {
2842 // We depend here on input being done in whole lines:
2843 // '\0' which isn't the EOF can only be seen after '\n'.
2844 char c = *p->lex_inbuf;
2845 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2846 break;
2847 if (!isspace(c))
2848 break;
2849 p->lex_inbuf++;
2850 }
2851}
2852
Denys Vlasenko10bde142018-12-27 18:23:58 +01002853static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002854{
2855 BcParse *p = &G.prs;
2856 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002857 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002858
2859 bc_vec_pop_all(&p->lex_strnumbuf);
2860 bc_vec_pushByte(&p->lex_strnumbuf, last);
2861
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002862// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002863// (Note: They must be capitals. Lower case letters are variable names.)
2864// Single digit numbers always have the value of the digit regardless of
2865// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2866// all input digits greater or equal to ibase to the value of ibase-1.
2867// This makes the number ZZZ always be the largest 3 digit number of the
2868// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002869// dc only allows A-F, the rules about single-char and multi-char are the same.
2870 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002871 pt = (last == '.');
2872 p->lex = XC_LEX_NUMBER;
2873 for (;;) {
2874 // We depend here on input being done in whole lines:
2875 // '\0' which isn't the EOF can only be seen after '\n'.
2876 char c = *p->lex_inbuf;
2877 check_c:
2878 if (c == '\0')
2879 break;
2880 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2881 p->lex_inbuf += 2;
2882 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01002883 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002884 c = peek_inbuf(); // force next line to be read
2885 goto check_c;
2886 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002887 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002888 if (c != '.') break;
2889 // if '.' was already seen, stop on second one:
2890 if (pt) break;
2891 pt = true;
2892 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002893 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002894 last = c;
2895 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2896 p->lex_inbuf++;
2897 }
2898 if (last == '.') // remove trailing '.' if any
2899 bc_vec_pop(&p->lex_strnumbuf);
2900 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2901
2902 G.err_line = G.prs.lex_line;
2903 RETURN_STATUS(BC_STATUS_SUCCESS);
2904}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002905#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002906
Denys Vlasenko10bde142018-12-27 18:23:58 +01002907static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002908{
2909 BcParse *p = &G.prs;
2910 size_t i;
2911 const char *buf;
2912
2913 p->lex = XC_LEX_NAME;
2914
2915 // Since names can't cross lines with \<newline>,
2916 // we depend on the fact that whole line is in the buffer
2917 i = 0;
2918 buf = p->lex_inbuf - 1;
2919 for (;;) {
2920 char c = buf[i];
2921 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2922 i++;
2923 }
2924
2925#if 0 // We do not protect against people with gigabyte-long names
2926 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2927 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2928 if (i > BC_MAX_STRING)
2929 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2930 }
2931#endif
2932 bc_vec_string(&p->lex_strnumbuf, i, buf);
2933
2934 // Increment the index. We minus 1 because it has already been incremented.
2935 p->lex_inbuf += i - 1;
2936
2937 //return BC_STATUS_SUCCESS;
2938}
2939
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002940IF_BC(static BC_STATUS zbc_lex_token(void);)
2941IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002942#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2943#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002944
Denys Vlasenko10bde142018-12-27 18:23:58 +01002945static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002946{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002947 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002948 BcStatus s;
2949
Denys Vlasenko22314682019-01-01 21:50:14 +01002950 G.err_line = p->lex_line;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002951 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002952//why?
2953// if (p->lex_last == XC_LEX_EOF)
2954// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002955
2956 // Loop until failure or we don't have whitespace. This
2957 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002958 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002959 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002960 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002961 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002962 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002963 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002964 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002965 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002966 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002967 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002968 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2969 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002970 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002971 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002972 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002973 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002974 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002975 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002976 } while (!s && p->lex == XC_LEX_WHITESPACE);
2977 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002978
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002979 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002980}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002981#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002982
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002983#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002984static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002985{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002986 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002987 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002988 RETURN_STATUS(BC_STATUS_SUCCESS);
2989}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002990#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002991
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002992static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002993{
2994 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01002995 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002996 if (s) RETURN_STATUS(s);
2997 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002998 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002999 RETURN_STATUS(s);
3000}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003001#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003002
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003003static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003004{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003005 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003006 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003007 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003008 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003009
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003010 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3011 const char *keyword8 = bc_lex_kws[i].name8;
3012 unsigned j = 0;
3013 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3014 j++;
3015 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003016 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003017 if (keyword8[j] != '\0')
3018 continue;
3019 match:
3020 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003021 if (isalnum(buf[j]) || buf[j]=='_')
3022 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003023 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003024 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003025 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3026 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003027 }
3028
3029 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003030 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003031 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003032 }
3033
Denys Vlasenko10bde142018-12-27 18:23:58 +01003034 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003035 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003036
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003037 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003038 // Prevent this:
3039 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003040 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003041 // '
3042 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003043 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003044 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003045
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003046 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003047}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003048#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003049
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003050static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003051{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003052 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003053
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003054 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003055 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003056 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003057 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003058 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003059 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003060 }
3061 if (c == '"')
3062 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003063 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003064 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003065 dbg_lex("++p->lex_line=%zd", p->lex_line);
3066 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003067 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3068 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003069 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003070 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3071 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003072
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003073 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003074 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003075}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003076#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003077
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003078static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003079{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003080 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003081 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003082 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003083 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003084 with_and_without >>= 8; // store "with" value
3085 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003086 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003087}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003088#define parse_lex_by_checking_eq_sign(with, without) \
3089 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003091static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003092{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003093 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003094
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003095 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003096 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003097 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003098 char c;
3099
3100 p->lex_inbuf++;
3101 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003102 check_star:
3103 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003104 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003105 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003106 if (c == '/')
3107 break;
3108 goto check_star;
3109 }
3110 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003111 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003112 }
Denys Vlasenko22314682019-01-01 21:50:14 +01003113 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003114 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003115 dbg_lex("++p->lex_line=%zd", p->lex_line);
3116 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003118 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003119
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003120 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003121 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003122}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003123#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003124
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003125#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003126static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003127{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003128 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003129 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003130 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003131 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003132
3133 // This is the workhorse of the lexer.
3134 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003135// case '\0': // probably never reached
3136// p->lex_inbuf--;
3137// p->lex = XC_LEX_EOF;
3138// break;
3139 case '\n':
3140 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003141 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003142 p->lex = XC_LEX_NLINE;
3143 break;
3144 case '\t':
3145 case '\v':
3146 case '\f':
3147 case '\r':
3148 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003149 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003150 break;
3151 case '!':
3152 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3153 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3154 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003155 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003156 }
3157 break;
3158 case '"':
3159 s = zbc_lex_string();
3160 break;
3161 case '#':
3162 s = zbc_POSIX_does_not_allow("'#' script comments");
3163 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003164 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003165 break;
3166 case '%':
3167 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3168 break;
3169 case '&':
3170 c2 = *p->lex_inbuf;
3171 if (c2 == '&') {
3172 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3173 if (s) RETURN_STATUS(s);
3174 p->lex_inbuf++;
3175 p->lex = BC_LEX_OP_BOOL_AND;
3176 } else {
3177 p->lex = XC_LEX_INVALID;
3178 s = bc_error_bad_character('&');
3179 }
3180 break;
3181 case '(':
3182 case ')':
3183 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3184 break;
3185 case '*':
3186 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3187 break;
3188 case '+':
3189 c2 = *p->lex_inbuf;
3190 if (c2 == '+') {
3191 p->lex_inbuf++;
3192 p->lex = BC_LEX_OP_INC;
3193 } else
3194 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3195 break;
3196 case ',':
3197 p->lex = BC_LEX_COMMA;
3198 break;
3199 case '-':
3200 c2 = *p->lex_inbuf;
3201 if (c2 == '-') {
3202 p->lex_inbuf++;
3203 p->lex = BC_LEX_OP_DEC;
3204 } else
3205 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3206 break;
3207 case '.':
3208 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003209 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003210 else {
3211 p->lex = BC_LEX_KEY_LAST;
3212 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3213 }
3214 break;
3215 case '/':
3216 c2 = *p->lex_inbuf;
3217 if (c2 == '*')
3218 s = zbc_lex_comment();
3219 else
3220 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3221 break;
3222 case '0':
3223 case '1':
3224 case '2':
3225 case '3':
3226 case '4':
3227 case '5':
3228 case '6':
3229 case '7':
3230 case '8':
3231 case '9':
3232 case 'A':
3233 case 'B':
3234 case 'C':
3235 case 'D':
3236 case 'E':
3237 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003238 case 'G':
3239 case 'H':
3240 case 'I':
3241 case 'J':
3242 case 'K':
3243 case 'L':
3244 case 'M':
3245 case 'N':
3246 case 'O':
3247 case 'P':
3248 case 'Q':
3249 case 'R':
3250 case 'S':
3251 case 'T':
3252 case 'U':
3253 case 'V':
3254 case 'W':
3255 case 'X':
3256 case 'Y':
3257 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003258 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003259 break;
3260 case ';':
3261 p->lex = BC_LEX_SCOLON;
3262 break;
3263 case '<':
3264 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3265 break;
3266 case '=':
3267 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3268 break;
3269 case '>':
3270 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3271 break;
3272 case '[':
3273 case ']':
3274 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3275 break;
3276 case '\\':
3277 if (*p->lex_inbuf == '\n') {
3278 p->lex = XC_LEX_WHITESPACE;
3279 p->lex_inbuf++;
3280 } else
3281 s = bc_error_bad_character(c);
3282 break;
3283 case '^':
3284 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3285 break;
3286 case 'a':
3287 case 'b':
3288 case 'c':
3289 case 'd':
3290 case 'e':
3291 case 'f':
3292 case 'g':
3293 case 'h':
3294 case 'i':
3295 case 'j':
3296 case 'k':
3297 case 'l':
3298 case 'm':
3299 case 'n':
3300 case 'o':
3301 case 'p':
3302 case 'q':
3303 case 'r':
3304 case 's':
3305 case 't':
3306 case 'u':
3307 case 'v':
3308 case 'w':
3309 case 'x':
3310 case 'y':
3311 case 'z':
3312 s = zbc_lex_identifier();
3313 break;
3314 case '{':
3315 case '}':
3316 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3317 break;
3318 case '|':
3319 c2 = *p->lex_inbuf;
3320 if (c2 == '|') {
3321 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3322 if (s) RETURN_STATUS(s);
3323 p->lex_inbuf++;
3324 p->lex = BC_LEX_OP_BOOL_OR;
3325 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003326 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003327 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003328 }
3329 break;
3330 default:
3331 p->lex = XC_LEX_INVALID;
3332 s = bc_error_bad_character(c);
3333 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003334 }
3335
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003336 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003337}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003338#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003339#endif // ENABLE_BC
3340
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003341#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003342static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003343{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003344 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003345 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003346 xc_lex_whitespace(); // eats whitespace (but not newline)
3347 p->lex_inbuf++; // xc_lex_name() expects this
3348 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003349 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003350 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003351 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003352 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3353 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003354 }
3355
Denys Vlasenko29301232018-12-11 15:29:32 +01003356 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003357}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003358#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003359
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003360static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003361{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003362 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003363 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003364
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003365 p->lex = XC_LEX_STR;
3366 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003367
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003368 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003369 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003370 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003371 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003372 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003373 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003374 if (c == '[') depth++;
3375 if (c == ']')
3376 if (--depth == 0)
3377 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003378 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003379 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003380 dbg_lex("++p->lex_line=%zd", p->lex_line);
3381 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003382 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3383 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003384 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003385 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003386 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003387
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003388 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003389 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003390}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003391#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003392
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003393#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003394static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003395{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003396 static const //BcLexType - should be this type, but narrower type saves size:
3397 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003398 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003399 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 +01003400 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3401 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003402 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003403 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003404
Denys Vlasenko10bde142018-12-27 18:23:58 +01003405 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003406 BcStatus s;
3407 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003408 size_t i;
3409
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003410 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003411 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003412 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003413 }
3414
Denys Vlasenko81293c82018-12-24 01:53:55 +01003415 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003416 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003417 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003418 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003419 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003420 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003421 }
3422
3423 // This is the workhorse of the lexer.
3424 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003425// case '\0': // probably never reached
3426// p->lex = XC_LEX_EOF;
3427// break;
3428 case '\n':
3429 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3430 // (and "case '\n':" is not just empty here)
3431 // only to allow interactive dc have a way to exit
3432 // "parse" stage of "parse,execute" loop
3433 // on <enter>, not on _next_ token (which would mean
3434 // commands are not executed on pressing <enter>).
3435 // IOW: typing "1p<enter>" should print "1" _at once_,
3436 // not after some more input.
3437 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003438 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003439 p->lex = XC_LEX_NLINE;
3440 break;
3441 case '\t':
3442 case '\v':
3443 case '\f':
3444 case '\r':
3445 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003446 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003447 break;
3448 case '!':
3449 c2 = *p->lex_inbuf;
3450 if (c2 == '=')
3451 p->lex = XC_LEX_OP_REL_NE;
3452 else if (c2 == '<')
3453 p->lex = XC_LEX_OP_REL_LE;
3454 else if (c2 == '>')
3455 p->lex = XC_LEX_OP_REL_GE;
3456 else
3457 RETURN_STATUS(bc_error_bad_character(c));
3458 p->lex_inbuf++;
3459 break;
3460 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003461 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003462 break;
3463 case '.':
3464 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003465 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003466 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003467 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003468 break;
3469 case '0':
3470 case '1':
3471 case '2':
3472 case '3':
3473 case '4':
3474 case '5':
3475 case '6':
3476 case '7':
3477 case '8':
3478 case '9':
3479 case 'A':
3480 case 'B':
3481 case 'C':
3482 case 'D':
3483 case 'E':
3484 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003485 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003486 break;
3487 case '[':
3488 s = zdc_lex_string();
3489 break;
3490 default:
3491 p->lex = XC_LEX_INVALID;
3492 s = bc_error_bad_character(c);
3493 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003494 }
3495
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003496 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003497}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003498#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003499#endif // ENABLE_DC
3500
Denys Vlasenko10bde142018-12-27 18:23:58 +01003501static void xc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003502{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003503 BcVec *code = &G.prs.func->code;
3504 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
3505 bc_vec_pushByte(code, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003506}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003507
Denys Vlasenko10bde142018-12-27 18:23:58 +01003508static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003509{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003510#if 1
3511 BcVec *code = &G.prs.func->code;
3512 size_t pos = code->len;
3513 size_t len = strlen(name) + 1;
3514
3515 bc_vec_expand(code, pos + len);
3516 strcpy(code->v + pos, name);
3517 code->len = pos + len;
3518#else
3519 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003520 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003521 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003522 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003523#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003524}
3525
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003526// Indexes < 0xfc are encoded verbatim, else first byte is
3527// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3528// followed by that many bytes, lsb first.
3529// (The above describes 32-bit case).
3530#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3531
Denys Vlasenko10bde142018-12-27 18:23:58 +01003532static void xc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003533{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003534 size_t mask;
3535 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003536
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003537 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003538 if (idx < SMALL_INDEX_LIMIT) {
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003539 xc_parse_push(idx);
3540 return;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003541 }
3542
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003543 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3544 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003545 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003546 if (idx & mask) break;
3547 mask >>= 8;
3548 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003549 }
3550 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003551
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003552 xc_parse_push((SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003553
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003554 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003555 xc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003556 idx >>= 8;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003557 } while (idx != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003558}
3559
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003560#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003561static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003562{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003563 xc_parse_push(BC_INST_JUMP);
3564 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003565}
3566
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003567static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003568{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003569 xc_parse_push(BC_INST_JUMP_ZERO);
3570 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003571}
3572
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003573static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003574{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003575 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003576 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003577
Denys Vlasenko10bde142018-12-27 18:23:58 +01003578 xc_parse_push(XC_INST_STR);
3579 xc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003580 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003581
Denys Vlasenko10bde142018-12-27 18:23:58 +01003582 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003583}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003584#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003585#endif
3586
Denys Vlasenko10bde142018-12-27 18:23:58 +01003587static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003588{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003589 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003590 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003591#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003592 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003593#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003594 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003595#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003596 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003597#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01003598 xc_parse_push(XC_INST_NUM);
3599 xc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003600}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003601
Denys Vlasenko10bde142018-12-27 18:23:58 +01003602static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003603{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003604 G.prs.func = xc_program_func(G.prs.fidx);
3605 G.prs.lex_inbuf = text;
3606 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3607 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003608}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003609#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003610
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003611// Called when parsing or execution detects a failure,
3612// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003613static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003614{
3615 BcFunc *f;
3616 BcInstPtr *ip;
3617
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003618 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003619 bc_vec_pop_all(&G.prog.results);
3620
Denys Vlasenko10bde142018-12-27 18:23:58 +01003621 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003622 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003623 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003624}
3625
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003626// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003627// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003628static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003629{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003630 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003631 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003632 bc_func_free(p->func);
3633 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003634
Denys Vlasenko65e10462018-12-19 15:13:14 +01003635 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003636 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003637 }
3638
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003639 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003640 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003641
Denys Vlasenko503faf92018-12-20 16:24:18 +01003642 IF_BC(bc_vec_pop_all(&p->exits);)
3643 IF_BC(bc_vec_pop_all(&p->conds);)
3644 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003645
Denys Vlasenko10bde142018-12-27 18:23:58 +01003646 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003647}
3648
Denys Vlasenko10bde142018-12-27 18:23:58 +01003649static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003650{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003651 IF_BC(bc_vec_free(&G.prs.exits);)
3652 IF_BC(bc_vec_free(&G.prs.conds);)
3653 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003654 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003655}
3656
Denys Vlasenko10bde142018-12-27 18:23:58 +01003657static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003658{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003659 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003660 memset(p, 0, sizeof(BcParse));
3661
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003662 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003663 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3664 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3665 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003666
Denys Vlasenkofa210792018-12-19 19:35:40 +01003667 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003668 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003669}
3670
Denys Vlasenko10bde142018-12-27 18:23:58 +01003671static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003672{
Denys Vlasenko04715442018-12-21 00:10:26 +01003673 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003674 BcFunc f;
3675 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003676 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003677 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003678 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003679}
3680
3681#if ENABLE_BC
3682
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003683// Note: takes ownership of 'name' (must be malloced)
3684static size_t bc_program_addFunc(char *name)
3685{
3686 size_t idx;
3687 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003688 int inserted;
3689
3690 entry.name = name;
3691 entry.idx = G.prog.fns.len;
3692
3693 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3694 if (!inserted) free(name);
3695
3696 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3697 idx = entry_ptr->idx;
3698
3699 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003700 // There is already a function with this name.
3701 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003702 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003703 bc_func_free(func);
3704 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003705 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003706 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003707 }
3708
3709 return idx;
3710}
3711
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003712#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003713// We can calculate the conversion between tokens and exprs by subtracting the
3714// position of the first operator in the lex enum and adding the position of the
3715// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003716#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003717
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003718static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003719
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003720static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003721{
3722 BcStatus s;
3723
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003724 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003725 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3726 RETURN_STATUS(bc_error("empty expression"));
3727 RETURN_STATUS(s);
3728}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003729#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003730
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003731static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003732#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003733
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003734static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003735{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003736 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003737}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003738#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003739
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003740static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003741{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003742 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003743 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3744 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003745 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003746 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003747 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003748 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003749
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003750 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003751}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003752#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003753
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003754static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003755{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003756 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003757 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3758 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003759
3760 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003761 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003762 if (t == BC_LEX_LPAREN) break;
3763
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003764 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003765 if (l >= r && (l != r || !left)) break;
3766
Denys Vlasenko10bde142018-12-27 18:23:58 +01003767 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003768 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003769 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003770 }
3771
3772 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003773}
3774
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003775static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003776{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003777 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003778 BcLexType top;
3779
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003780 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003781 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003782 top = BC_PARSE_TOP_OP(p);
3783
3784 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003785 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003786
3787 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003788 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003789
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003790 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003791 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003792 top = BC_PARSE_TOP_OP(p);
3793 }
3794
3795 bc_vec_pop(&p->ops);
3796
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003797 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003798}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003799#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003800
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003801static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003802{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003803 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003804 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003805 size_t nparams;
3806
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003807 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003808 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3809
Denys Vlasenko10bde142018-12-27 18:23:58 +01003810 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003811 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003812
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003813 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003814 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003815 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003816 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003817 if (s) RETURN_STATUS(s);
3818 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003819 if (p->lex != BC_LEX_COMMA) {
3820 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003821 break;
3822 RETURN_STATUS(bc_error_bad_token());
3823 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003824 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003825 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003826 }
3827 }
3828
Denys Vlasenko10bde142018-12-27 18:23:58 +01003829 xc_parse_push(BC_INST_CALL);
3830 xc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003831
Denys Vlasenko26819db2018-12-12 16:08:46 +01003832 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003833}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003834#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003835
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003836// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003837static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003838{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003839 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003840 BcStatus s;
3841 BcId entry, *entry_ptr;
3842 size_t idx;
3843
3844 entry.name = name;
3845
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003846 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 if (s) goto err;
3848
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003849 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003850 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003851 goto err;
3852 }
3853
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003854 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003855
3856 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003857 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003858 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003859 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003860 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003861 free(name);
3862
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003863 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003864 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003865
Denys Vlasenko10bde142018-12-27 18:23:58 +01003866 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003867 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003869 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003870}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003871#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003872
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003873static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003874{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003875 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003876 BcStatus s;
3877 char *name;
3878
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003879 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003880 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003881 if (s) goto err;
3882
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003883 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003884 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 if (s) goto err;
3886
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003887 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003889 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003890 goto err;
3891 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003892 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003893 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003894 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003895 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003896 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 if (s) goto err;
3898 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003899 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003900 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003901 xc_parse_push(*type);
3902 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003903 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003904 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003906 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 goto err;
3908 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003909 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003910 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003911 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003912 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003913 xc_parse_push(XC_INST_VAR);
3914 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003915 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003916 }
3917
Denys Vlasenko26819db2018-12-12 16:08:46 +01003918 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003919 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003920 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003921 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003922}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003923#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003924
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003925static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003926{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003927 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003928 BcStatus s;
3929
Denys Vlasenko10bde142018-12-27 18:23:58 +01003930 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003931 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003932 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003933
Denys Vlasenko10bde142018-12-27 18:23:58 +01003934 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003935 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003936 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003937
Denys Vlasenko10bde142018-12-27 18:23:58 +01003938 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003940 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003941}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003942#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003943
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003944static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003945{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003946 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003947 BcStatus s;
3948
Denys Vlasenko10bde142018-12-27 18:23:58 +01003949 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003950 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003951 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003952
3953 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3954
Denys Vlasenko10bde142018-12-27 18:23:58 +01003955 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003956 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003957
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003958 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003959 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003960
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003961 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003962
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003963 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003964 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003966 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003967}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003968#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003969
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003970static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003971{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003972 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003973 BcStatus s;
3974
Denys Vlasenko10bde142018-12-27 18:23:58 +01003975 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003976 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003977
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003978 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003979 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003980 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003981 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003982 }
3983
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003984 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003985 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3986
Denys Vlasenko10bde142018-12-27 18:23:58 +01003987 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003988 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003989
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003990 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003991 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003992 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003993 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01003994 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003995
Denys Vlasenko10bde142018-12-27 18:23:58 +01003996 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003997}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003998#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003999
Denys Vlasenko22314682019-01-01 21:50:14 +01004000static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004001{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004002 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004003 BcStatus s;
4004 BcLexType type;
4005 char inst;
4006 BcInst etype = *prev;
4007
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004008 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4009 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4010 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004011 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004012 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004013 xc_parse_push(inst);
4014 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004015 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004016 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004017
Denys Vlasenko10bde142018-12-27 18:23:58 +01004018 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004019 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004020 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004021
4022 // Because we parse the next part of the expression
4023 // right here, we need to increment this.
Denys Vlasenko22314682019-01-01 21:50:14 +01004024 *nexs = *nexs + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004025
4026 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004027 case XC_LEX_NAME:
4028 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
4029 break;
4030 case BC_LEX_KEY_IBASE:
4031 case BC_LEX_KEY_LAST:
4032 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004033 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4034 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004035 break;
4036 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004037 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004038 if (s) RETURN_STATUS(s);
4039 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004040 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004041 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01004042 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004043 break;
4044 default:
4045 s = bc_error_bad_token();
4046 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004047 }
4048
Denys Vlasenko10bde142018-12-27 18:23:58 +01004049 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004050 }
4051
Denys Vlasenko26819db2018-12-12 16:08:46 +01004052 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004053}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004054#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
Denys Vlasenko22314682019-01-01 21:50:14 +01004056static int bc_parse_inst_isLeaf(BcInst p)
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004057{
4058 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4059 || p == BC_INST_INC_POST
4060 || p == BC_INST_DEC_POST
4061 ;
4062}
Denys Vlasenko22314682019-01-01 21:50:14 +01004063#define BC_PARSE_LEAF(prev, bin_last, rparen) \
4064 (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev)))
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004065
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004066static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
Denys Vlasenko22314682019-01-01 21:50:14 +01004067 bool rparen, bool bin_last, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004068{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004069 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004070 BcStatus s;
4071 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004072
Denys Vlasenko10bde142018-12-27 18:23:58 +01004073 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004074 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004075
Denys Vlasenko22314682019-01-01 21:50:14 +01004076 type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004077 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004078
4079 // We can just push onto the op stack because this is the largest
4080 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004081 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004082 bc_vec_push(&p->ops, &type);
4083 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004084 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004085
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004086 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004087}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004088#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004089
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004090static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004091{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004092 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004093 BcStatus s;
4094 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004095
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004096 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004097 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004098 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004099 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004100 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004101 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004102 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004103 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004104 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004105 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004106 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004107 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004108 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004109 }
4110
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004111 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004112}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004113#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004114
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004115static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004116{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004117 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004118 BcStatus s;
4119 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004120
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004121 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004122 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004123 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004124
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004125 t = p->lex;
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004126 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004127 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004128 else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004129//TODO: if (p->func->voidfunc) ERROR
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004130 s = zbc_parse_expr(0);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004131 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004132
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004133 if (t != BC_LEX_LPAREN // "return EXPR", no ()
4134 || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b"
4135 ) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004136 s = zbc_POSIX_requires("parentheses around return expressions");
4137 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004138 }
4139
Denys Vlasenko10bde142018-12-27 18:23:58 +01004140 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004141 }
4142
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004143 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004144 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004145}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004146#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004147
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004148static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004149{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004150 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004151 size_t *label = bc_vec_item(&p->func->labels, idx);
4152 *label = p->func->code.len;
4153}
4154
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004155static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004156{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004157 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004158 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004159 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004160
4161 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004162 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004163 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004164 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004165
Denys Vlasenko10bde142018-12-27 18:23:58 +01004166 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004167 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004168 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004169 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004170 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004171
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004172 // Encode "if zero, jump to ..."
4173 // Pushed value (destination of the jump) is uninitialized,
4174 // will be rewritten to be address of "end of if()" or of "else".
4175 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004176 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004177
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004178 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004179 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004180
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004181 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4182 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004183 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004184
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004185 // Encode "after then_stmt, jump to end of if()"
4186 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4187 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004188 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004189
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004190 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 +01004191 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004192
Denys Vlasenko15850832018-12-16 21:40:54 +01004193 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004194
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004195 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004196 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004197 }
4198
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004199 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 +01004200 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004201
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004202 dbg_lex_done("%s:%d done", __func__, __LINE__);
4203 RETURN_STATUS(s);
4204}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004205#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004206
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004207static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004208{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004209 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004210 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004211 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004212 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004213
Denys Vlasenko10bde142018-12-27 18:23:58 +01004214 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004215 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004216 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004217 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004218 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004219
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004220 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004221 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004222 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004223
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004224 bc_vec_push(&p->exits, &ip_idx);
4225 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004226
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004227 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004228 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004229 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004230
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004231 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004232
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004233 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004234 if (s) RETURN_STATUS(s);
4235
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004236 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004237 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004238
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004239 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004240 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004241
4242 bc_vec_pop(&p->exits);
4243 bc_vec_pop(&p->conds);
4244
4245 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004246}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004247#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004248
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004249static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004250{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004251 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004252 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004253 size_t cond_idx, exit_idx, body_idx, update_idx;
4254
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004255 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004256 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004257 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004258 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004259 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004260 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004261
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004262 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004263 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004264 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004265 if (s) RETURN_STATUS(s);
4266 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004267 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4268 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004269 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004270
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004271 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004272 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004273 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004274
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004275 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004276 update_idx = cond_idx + 1;
4277 body_idx = update_idx + 1;
4278 exit_idx = body_idx + 1;
4279
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004280 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004281 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004282 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004283 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004284 // This is safe to set because the current token is a semicolon,
4285 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004286 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004287 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004288 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004289 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004290 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004291
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004292 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004293
Denys Vlasenko10bde142018-12-27 18:23:58 +01004294 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004295 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004296
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004297 bc_parse_pushJUMP_ZERO(exit_idx);
4298 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004299
Gavin Howard01055ba2018-11-03 11:00:21 -06004300 bc_vec_push(&p->conds, &update_idx);
4301 bc_vec_push(&p->func->labels, &p->func->code.len);
4302
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004303 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004304 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004305 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004306 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004307 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004308 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004309 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4310 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004311 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004312
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004313 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004314 bc_vec_push(&p->func->labels, &p->func->code.len);
4315
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004316 bc_vec_push(&p->exits, &exit_idx);
4317 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004318
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004319 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004320 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004321
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004322 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004323 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004324
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004325 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004326 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004327
4328 bc_vec_pop(&p->exits);
4329 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004330
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004331 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004332}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004333#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004334
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004335static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004336{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004337 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004338 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004339
Gavin Howard01055ba2018-11-03 11:00:21 -06004340 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004341 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4342 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004343 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004344 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004345 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004346 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004347 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004348
Denys Vlasenko10bde142018-12-27 18:23:58 +01004349 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004350}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004351#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004352
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004353static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4354{
4355 BcId *autoid;
4356 BcId a;
4357 size_t i;
4358
4359 autoid = (void*)f->autos.v;
4360 for (i = 0; i < f->autos.len; i++, autoid++) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004361 if (strcmp(name, autoid->name) == 0
4362 && var == autoid->idx
4363 ) {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004364 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko22314682019-01-01 21:50:14 +01004365 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004366 }
4367
4368 a.idx = var;
4369 a.name = name;
4370
4371 bc_vec_push(&f->autos, &a);
4372
4373 RETURN_STATUS(BC_STATUS_SUCCESS);
4374}
4375#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4376
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004377static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004378{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004379 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004380 BcStatus s;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004381 bool var, comma, voidfunc;
Gavin Howard01055ba2018-11-03 11:00:21 -06004382 char *name;
4383
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004384 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004385 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004386 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004387 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004388 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004389
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004390 // To be maximally both POSIX and GNU-compatible,
4391 // "void" is not treated as a normal keyword:
4392 // you can have variable named "void", and even a function
4393 // named "void": "define void() { return 6; }" is ok.
4394 // _Only_ "define void f() ..." syntax treats "void"
4395 // specially.
4396 voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004397
Denys Vlasenko10bde142018-12-27 18:23:58 +01004398 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004399 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004400
4401 voidfunc = (voidfunc && p->lex == XC_LEX_NAME);
4402 if (voidfunc) {
4403 s = zxc_lex_next();
4404 if (s) RETURN_STATUS(s);
4405 }
4406
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004407 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko22314682019-01-01 21:50:14 +01004408 RETURN_STATUS(bc_error_bad_function_definition());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004409
4410 p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v));
4411 p->func = xc_program_func(p->fidx);
4412 p->func->voidfunc = voidfunc;
4413
Denys Vlasenko10bde142018-12-27 18:23:58 +01004414 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004415 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004416
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004417 comma = false;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004418 while (p->lex != BC_LEX_RPAREN) {
4419 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004420 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004421
4422 ++p->func->nparams;
4423
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004424 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004425 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004426 if (s) goto err;
4427
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004428 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004429
4430 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004431 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004432 if (s) goto err;
4433
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004434 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004435 s = bc_error_bad_function_definition();
Gavin Howard01055ba2018-11-03 11:00:21 -06004436 goto err;
4437 }
4438
Denys Vlasenko10bde142018-12-27 18:23:58 +01004439 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004440 if (s) goto err;
4441 }
4442
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004443 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004444 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004445 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004446 if (s) goto err;
4447 }
4448
Denys Vlasenko29301232018-12-11 15:29:32 +01004449 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004450 if (s) goto err;
4451 }
4452
Denys Vlasenko22314682019-01-01 21:50:14 +01004453 if (comma) RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004454
Denys Vlasenko10bde142018-12-27 18:23:58 +01004455 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004456 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004457
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004458 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004459 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004460 if (s) RETURN_STATUS(s);
4461 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004462
Denys Vlasenko202dd192018-12-16 17:30:35 +01004463 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004464 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004465 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004466 // GNU bc requires a {} block even if function body has single stmt, enforce this
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004467 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004468 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004469
4470 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004471 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004472 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004473 if (s) RETURN_STATUS(s);
4474
Denys Vlasenko10bde142018-12-27 18:23:58 +01004475 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004476
4477 // Subsequent code generation is into main program
4478 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004479 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004480
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004481 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004482 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004483 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004484 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004485 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004486 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004487}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004488#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004489
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004490static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004491{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004492 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004493 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004494 char *name;
4495
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004496 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004497 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004498 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004499
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004500 for (;;) {
4501 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004502
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004503 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004504 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004505
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004506 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004507 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004508 if (s) goto err;
4509
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004510 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004511 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004512 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004513 if (s) goto err;
4514
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004515 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004516 s = bc_error_at("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004517 goto err;
4518 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004519 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004520 if (s) goto err;
4521 }
4522
Denys Vlasenko29301232018-12-11 15:29:32 +01004523 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004524 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004525
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004526 if (p->lex == XC_LEX_NLINE
4527 || p->lex == BC_LEX_SCOLON
4528 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004529 ) {
4530 break;
4531 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004532 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko22314682019-01-01 21:50:14 +01004533 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004534 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004535 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004536 }
4537
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004538 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004539 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004540 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004542 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004543 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004544}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004545#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004546
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004547#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004548static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004549{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004550 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 BcStatus s = BC_STATUS_SUCCESS;
4552
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004553 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004554
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004555 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004556 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004557 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004558 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004559 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004560 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004561 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004562 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004563
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004564 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004565 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4566 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004567 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004568 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004569 } while (p->lex == XC_LEX_NLINE);
4570 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004571 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004572 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004573 if (s) RETURN_STATUS(s);
4574 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004575 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004576 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004577 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004578 if (s) RETURN_STATUS(s);
4579 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004580 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004581 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4582 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 }
4584
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004585 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4586 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004587 case XC_LEX_OP_MINUS:
4588 case BC_LEX_OP_INC:
4589 case BC_LEX_OP_DEC:
4590 case BC_LEX_OP_BOOL_NOT:
4591 case BC_LEX_LPAREN:
4592 case XC_LEX_NAME:
4593 case XC_LEX_NUMBER:
4594 case BC_LEX_KEY_IBASE:
4595 case BC_LEX_KEY_LAST:
4596 case BC_LEX_KEY_LENGTH:
4597 case BC_LEX_KEY_OBASE:
4598 case BC_LEX_KEY_READ:
4599 case BC_LEX_KEY_SCALE:
4600 case BC_LEX_KEY_SQRT:
4601 s = zbc_parse_expr(BC_PARSE_PRINT);
4602 break;
4603 case XC_LEX_STR:
4604 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004605 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004606 break;
4607 case BC_LEX_KEY_BREAK:
4608 case BC_LEX_KEY_CONTINUE:
4609 s = zbc_parse_break_or_continue(p->lex);
4610 break;
4611 case BC_LEX_KEY_FOR:
4612 s = zbc_parse_for();
4613 break;
4614 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004615 xc_parse_push(BC_INST_HALT);
4616 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004617 break;
4618 case BC_LEX_KEY_IF:
4619 s = zbc_parse_if();
4620 break;
4621 case BC_LEX_KEY_LIMITS:
4622 // "limits" is a compile-time command,
4623 // the output is produced at _parse time_.
4624 printf(
4625 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4626 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4627 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4628 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4629 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4630 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4631 "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 Vlasenkoecb62ed2018-12-25 22:32:41 +01004668 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004669 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004670 else if (p->lex == BC_LEX_KEY_DEFINE) {
4671 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004672 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004673 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004674 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004675 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004676 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004677
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004678 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004679 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004680}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004681#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004682
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004683// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004684static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004685{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004686 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004687 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004688 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004689 unsigned nparens, nrelops;
Denys Vlasenko22314682019-01-01 21:50:14 +01004690 bool paren_first, rprn, assign, bin_last, incdec;
Gavin Howard01055ba2018-11-03 11:00:21 -06004691
Denys Vlasenko17df8822018-12-14 23:00:24 +01004692 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004693 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004694 nparens = nrelops = 0;
Denys Vlasenko22314682019-01-01 21:50:14 +01004695 rprn = assign = incdec = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004696 bin_last = true;
4697
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004698 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004699 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004700 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004701 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004702
4703 if (!lex_allowed_in_bc_expr(t))
4704 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004705
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004706 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004707 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004708 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004709 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004710 case BC_LEX_OP_INC:
4711 case BC_LEX_OP_DEC:
4712 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004713 if (incdec) return bc_error_bad_assignment();
4714 s = zbc_parse_incdec(&prev, &nexprs, flags);
4715 incdec = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004716 rprn = bin_last = false;
4717 //get_token = false; - already is
4718 break;
4719 case XC_LEX_OP_MINUS:
4720 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004721 s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004722 rprn = false;
4723 //get_token = false; - already is
4724 bin_last = (prev == XC_INST_MINUS);
Denys Vlasenko22314682019-01-01 21:50:14 +01004725 if (bin_last) incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004726 break;
4727 case BC_LEX_OP_ASSIGN_POWER:
4728 case BC_LEX_OP_ASSIGN_MULTIPLY:
4729 case BC_LEX_OP_ASSIGN_DIVIDE:
4730 case BC_LEX_OP_ASSIGN_MODULUS:
4731 case BC_LEX_OP_ASSIGN_PLUS:
4732 case BC_LEX_OP_ASSIGN_MINUS:
4733 case BC_LEX_OP_ASSIGN:
4734 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4735 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4736 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4737 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4738 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004739 return bc_error_bad_assignment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004740 }
4741 // Fallthrough.
4742 case XC_LEX_OP_POWER:
4743 case XC_LEX_OP_MULTIPLY:
4744 case XC_LEX_OP_DIVIDE:
4745 case XC_LEX_OP_MODULUS:
4746 case XC_LEX_OP_PLUS:
4747 case XC_LEX_OP_REL_EQ:
4748 case XC_LEX_OP_REL_LE:
4749 case XC_LEX_OP_REL_GE:
4750 case XC_LEX_OP_REL_NE:
4751 case XC_LEX_OP_REL_LT:
4752 case XC_LEX_OP_REL_GT:
4753 case BC_LEX_OP_BOOL_NOT:
4754 case BC_LEX_OP_BOOL_OR:
4755 case BC_LEX_OP_BOOL_AND:
4756 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004757 if (t == BC_LEX_OP_BOOL_NOT) {
4758 if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT)
4759 return bc_error_bad_expression();
4760 } else if (prev == XC_INST_BOOL_NOT) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004761 return bc_error_bad_expression();
4762 }
Denys Vlasenko22314682019-01-01 21:50:14 +01004763
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004764 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4765 prev = BC_TOKEN_2_INST(t);
4766 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko22314682019-01-01 21:50:14 +01004767 rprn = incdec = false;
4768 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004769 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4770 break;
4771 case BC_LEX_LPAREN:
4772 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004773 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004774 return bc_error_bad_expression();
4775 bc_vec_push(&p->ops, &t);
4776 nparens++;
4777 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004778 rprn = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004779 break;
4780 case BC_LEX_RPAREN:
4781 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004782 if (p->lex_last == BC_LEX_LPAREN) {
4783 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
4784 return BC_STATUS_PARSE_EMPTY_EXP;
4785 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004786 if (bin_last || prev == XC_INST_BOOL_NOT)
4787 return bc_error_bad_expression();
4788 if (nparens == 0) {
4789 goto exit_loop;
4790 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004791 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4792 nparens--;
4793 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004794 rprn = true;
4795 bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004796 break;
4797 case XC_LEX_NAME:
4798 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004799 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004800 return bc_error_bad_expression();
4801 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko22314682019-01-01 21:50:14 +01004802 rprn = (prev == BC_INST_CALL);
4803 bin_last = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004804 //get_token = false; - already is
4805 nexprs++;
4806 break;
4807 case XC_LEX_NUMBER:
4808 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004809 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004810 return bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004811 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004812 prev = XC_INST_NUM;
4813 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004814 rprn = bin_last = false;
4815 nexprs++;
4816 break;
4817 case BC_LEX_KEY_IBASE:
4818 case BC_LEX_KEY_LAST:
4819 case BC_LEX_KEY_OBASE:
4820 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004821 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004822 return bc_error_bad_expression();
4823 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004824 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004825 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004826 rprn = bin_last = false;
4827 nexprs++;
4828 break;
4829 case BC_LEX_KEY_LENGTH:
4830 case BC_LEX_KEY_SQRT:
4831 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004832 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004833 return bc_error_bad_expression();
4834 s = zbc_parse_builtin(t, flags, &prev);
4835 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004836 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004837 nexprs++;
4838 break;
4839 case BC_LEX_KEY_READ:
4840 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004841 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004842 return bc_error_bad_expression();
4843 s = zbc_parse_read();
4844 prev = XC_INST_READ;
4845 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004846 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004847 nexprs++;
4848 break;
4849 case BC_LEX_KEY_SCALE:
4850 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004851 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004852 return bc_error_bad_expression();
4853 s = zbc_parse_scale(&prev, flags);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004854 //get_token = false; - already is
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004855 rprn = bin_last = false;
4856 nexprs++;
4857 break;
4858 default:
4859 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004860 }
4861
Denys Vlasenkobb116032018-12-25 01:16:37 +01004862 if (s || G_interrupt) // error, or ^C: stop parsing
4863 return BC_STATUS_FAILURE;
4864 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004865 s = zxc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004866 if (s) return s;
4867 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004868 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004869 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004870
4871 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004872 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004873 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004874
4875 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004876 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004877
Denys Vlasenko10bde142018-12-27 18:23:58 +01004878 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004879
Denys Vlasenko69560f42018-12-24 14:14:23 +01004880 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004881 bc_vec_pop(&p->ops);
4882 }
4883
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004884 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004885 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004886
Gavin Howard01055ba2018-11-03 11:00:21 -06004887 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004888 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004889 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4890 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004891 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004892 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004893 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4894 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004895 }
4896
4897 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004898 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004899 xc_parse_push(XC_INST_PRINT);
4900 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004901 }
4902
Denys Vlasenko17df8822018-12-14 23:00:24 +01004903 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004904 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004905}
4906
Gavin Howard01055ba2018-11-03 11:00:21 -06004907#endif // ENABLE_BC
4908
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004909#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004910
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004911static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004912{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004913 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004914 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004915
Denys Vlasenko10bde142018-12-27 18:23:58 +01004916 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004917 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004918 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004919
Denys Vlasenko10bde142018-12-27 18:23:58 +01004920 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004921
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004922 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004923}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004924#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004925
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004926static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004927{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004928 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004929 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004930 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004931
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004932 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004933
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004934 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004935 xc_parse_push(XC_INST_STR);
4936 xc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004937 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004938
4939 // Explanation needed here
Denys Vlasenko10bde142018-12-27 18:23:58 +01004940 xc_program_add_fn();
4941 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004942
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004943 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004944}
4945
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004946static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004947{
4948 BcStatus s;
4949
Denys Vlasenko10bde142018-12-27 18:23:58 +01004950 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004951 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004952 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004953 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004954 }
4955
4956 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004957 xc_parse_push(DC_INST_SWAP);
4958 xc_parse_push(XC_INST_ASSIGN);
4959 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004960 }
4961
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004962 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004963}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004964#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004965
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004966static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004967{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004968 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004969 BcStatus s;
4970
Denys Vlasenko10bde142018-12-27 18:23:58 +01004971 xc_parse_push(inst);
4972 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004973
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004974 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004975 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004976
Denys Vlasenko10bde142018-12-27 18:23:58 +01004977 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004978 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004979
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004980 // Note that 'else' part can not be on the next line:
4981 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4982 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004983 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004984 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004985 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004986 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004987 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004988 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004989 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004990
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004991 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004992}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004993#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004994
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004995static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004996{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004997 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004998 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004999 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005000
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005001 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005002 s = BC_STATUS_SUCCESS;
5003 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005004 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005005 case XC_LEX_OP_REL_EQ:
5006 case XC_LEX_OP_REL_LE:
5007 case XC_LEX_OP_REL_GE:
5008 case XC_LEX_OP_REL_NE:
5009 case XC_LEX_OP_REL_LT:
5010 case XC_LEX_OP_REL_GT:
5011 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
5012 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
5013 get_token = false;
5014 break;
5015 case DC_LEX_SCOLON:
5016 case DC_LEX_COLON:
5017 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
5018 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
5019 break;
5020 case XC_LEX_STR:
5021 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
5022 dc_parse_string();
5023 break;
5024 case XC_LEX_NEG:
5025 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005026 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005027 if (s) RETURN_STATUS(s);
5028 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005029 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005030 xc_parse_pushNUM();
5031 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005032 break;
5033 case XC_LEX_NUMBER:
5034 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005035 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005036 break;
5037 case DC_LEX_READ:
5038 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005039 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005040 break;
5041 case DC_LEX_OP_ASSIGN:
5042 case DC_LEX_STORE_PUSH:
5043 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
5044 assign = (t == DC_LEX_OP_ASSIGN);
5045 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
5046 s = zdc_parse_mem(inst, true, assign);
5047 break;
5048 case DC_LEX_LOAD:
5049 case DC_LEX_LOAD_POP:
5050 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
5051 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
5052 s = zdc_parse_mem(inst, true, false);
5053 break;
5054 case DC_LEX_STORE_IBASE:
5055 case DC_LEX_STORE_SCALE:
5056 case DC_LEX_STORE_OBASE:
5057 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5058 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5059 s = zdc_parse_mem(inst, false, true);
5060 break;
5061 default:
5062 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5063 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005064 }
5065
Denys Vlasenko10bde142018-12-27 18:23:58 +01005066 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005067
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005068 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005069 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005070}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005071#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005072
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005073static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005074{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005075 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005076 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005077
Denys Vlasenko6842c602019-01-04 05:41:47 +01005078 if (p->lex == XC_LEX_NLINE)
5079 RETURN_STATUS(zxc_lex_next());
5080
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005081 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005082 if (i >= 0) {
5083 BcInst inst = dc_LEX_to_INST[i];
5084 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005085 xc_parse_push(inst);
5086 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005087 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005088 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005089 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005090}
5091#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5092
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005093static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005094{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005095 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005096 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5097 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005098 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005099 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005100 }
5101
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005102 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005103 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005104}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005105#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005106
Gavin Howard01055ba2018-11-03 11:00:21 -06005107#endif // ENABLE_DC
5108
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005109//
5110// Execution engine
5111//
5112
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005113#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5114#define BC_PROG_NUM(r, n) \
5115 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5116
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005117#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5118#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5119
Denys Vlasenko10bde142018-12-27 18:23:58 +01005120static BcVec* xc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005121{
Gavin Howard01055ba2018-11-03 11:00:21 -06005122 BcId e, *ptr;
5123 BcVec *v, *map;
5124 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005125 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005126
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005127 v = var ? &G.prog.vars : &G.prog.arrs;
5128 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005129
5130 e.name = id;
5131 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005132 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005133
5134 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005135 BcVec v2;
5136 bc_array_init(&v2, var);
5137 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005138 }
5139
5140 ptr = bc_vec_item(map, i);
5141 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005142 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005143}
5144
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005145// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005146static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005147{
Gavin Howard01055ba2018-11-03 11:00:21 -06005148 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005149 case XC_RESULT_STR:
5150 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005151 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005152 case XC_RESULT_IBASE:
5153 case XC_RESULT_SCALE:
5154 case XC_RESULT_OBASE:
5155 *num = &r->d.n;
5156 break;
5157 case XC_RESULT_CONSTANT: {
5158 BcStatus s;
5159 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005160 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005161
Denys Vlasenko10bde142018-12-27 18:23:58 +01005162 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005163 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005164
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005165 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005166
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005167 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005168 if (s) {
5169 bc_num_free(&r->d.n);
5170 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005171 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005172 *num = &r->d.n;
5173 r->t = XC_RESULT_TEMP;
5174 break;
5175 }
5176 case XC_RESULT_VAR:
5177 case XC_RESULT_ARRAY:
5178 case XC_RESULT_ARRAY_ELEM: {
5179 BcVec *v;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005180 void *p;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005181 v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005182// dc variables are all stacks, so here we have this:
5183 p = bc_vec_top(v);
5184// TODO: eliminate these stacks for bc-only config?
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005185 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005186 v = p;
5187 if (v->len <= r->d.id.idx)
5188 bc_array_expand(v, r->d.id.idx + 1);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005189 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005190 } else {
5191 *num = p;
5192 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005193 break;
5194 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005195#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005196 case BC_RESULT_LAST:
5197 *num = &G.prog.last;
5198 break;
5199 case BC_RESULT_ONE:
5200 *num = &G.prog.one;
5201 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005202#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005203#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005204 default:
5205 // Testing the theory that dc does not reach LAST/ONE
5206 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005207#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005208 }
5209
Denys Vlasenko29301232018-12-11 15:29:32 +01005210 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005211}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005212#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005213
Denys Vlasenko10bde142018-12-27 18:23:58 +01005214static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005215 BcResult **r, BcNum **rn, bool assign)
5216{
5217 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005218 BcResultType lt, rt;
5219
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005220 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005221 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005222
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005223 *r = bc_vec_item_rev(&G.prog.results, 0);
5224 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005225
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005226 s = zxc_program_num(*l, ln);
5227 if (s) RETURN_STATUS(s);
5228 s = zxc_program_num(*r, rn);
5229 if (s) RETURN_STATUS(s);
5230
Gavin Howard01055ba2018-11-03 11:00:21 -06005231 lt = (*l)->t;
5232 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
5234 // We run this again under these conditions in case any vector has been
5235 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005236 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005237 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005238 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005239 }
5240
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005241 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005242 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005243 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005244 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005245
Denys Vlasenko29301232018-12-11 15:29:32 +01005246 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005247}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005248#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005249
Denys Vlasenko10bde142018-12-27 18:23:58 +01005250static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005251{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005252 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005253 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005254 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005255}
5256
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005257// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005258static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005259{
5260 BcStatus s;
5261
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005262 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005263 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005264 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005265
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005266 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005267 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005268
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005269 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005270 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005271
Denys Vlasenko29301232018-12-11 15:29:32 +01005272 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005273}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005274#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005275
Denys Vlasenko10bde142018-12-27 18:23:58 +01005276static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005277{
5278 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005279 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005280}
5281
Denys Vlasenko10bde142018-12-27 18:23:58 +01005282static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005283{
5284 BcStatus s;
5285 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005286 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005287
Denys Vlasenko10bde142018-12-27 18:23:58 +01005288 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005289 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005290 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005291
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005292 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005293 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 -06005294 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005295 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005296
Denys Vlasenko259137d2018-12-11 19:42:05 +01005297 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005298 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005299 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005300 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005301}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005302#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005303
Denys Vlasenko10bde142018-12-27 18:23:58 +01005304static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005305{
5306 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005307 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005308 BcVec buf;
5309 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005310 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005311
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005312 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005313 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005314
Denys Vlasenko10bde142018-12-27 18:23:58 +01005315 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005316 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005317
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005318 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005319 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005320 //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 +01005321
Denys Vlasenko10bde142018-12-27 18:23:58 +01005322 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005323 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005324 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005325 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005326 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005327 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005328 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005329 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005330 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005331 s = bc_error_at("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005332 goto exec_err;
5333 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005334 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005335
5336 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005337 ip.inst_idx = 0;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005338 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005339
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005340 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005341 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005342 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005343 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005344 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005345}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005346#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005347
Denys Vlasenko10bde142018-12-27 18:23:58 +01005348static size_t xc_program_index(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005349{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005350 unsigned char *bytes = (void*)(code + *bgn);
5351 unsigned amt;
5352 unsigned i;
5353 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005354
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005355 amt = *bytes++;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01005356 if (amt < SMALL_INDEX_LIMIT) {
5357 *bgn += 1;
5358 return amt;
5359 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005360 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005361 *bgn += amt + 1;
5362
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005363 res = 0;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005364 i = 0;
5365 do {
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005366 res |= (size_t)(*bytes++) << i;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005367 i += 8;
5368 } while (--amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005369
5370 return res;
5371}
5372
Denys Vlasenko10bde142018-12-27 18:23:58 +01005373static char *xc_program_name(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005374{
Denys Vlasenko694d2982018-12-18 19:17:11 +01005375 code += *bgn;
Denys Vlasenkof706a182018-12-26 20:02:09 +01005376 *bgn += strlen(code) + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005377
Denys Vlasenkof706a182018-12-26 20:02:09 +01005378 return xstrdup(code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005379}
5380
Denys Vlasenko10bde142018-12-27 18:23:58 +01005381static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005382{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005383#if ENABLE_DC
Denys Vlasenko266bec82019-01-02 05:03:53 +01005384 if (!str[0] && IS_DC) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005385 // Example: echo '[]ap' | dc
5386 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005387 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005388 return;
5389 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005390#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005391 while (*str) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005392 char c = *str++;
5393 if (c == '\\') {
5394 static const char esc[] ALIGN1 = "nabfrt""e\\";
5395 char *n;
5396
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005397 c = *str++;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005398 n = strchr(esc, c); // note: c can be NUL
5399 if (!n) {
5400 // Just print the backslash and following character
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005401 bb_putchar('\\');
5402 ++G.prog.nchars;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005403 } else {
5404 if (n - esc == 0) // "\n" ?
5405 G.prog.nchars = SIZE_MAX;
5406 c = "\n\a\b\f\r\t""\\\\""\\"[n - esc];
5407 // n a b f r t e \ \<end of line>
Gavin Howard01055ba2018-11-03 11:00:21 -06005408 }
5409 }
Denys Vlasenko266bec82019-01-02 05:03:53 +01005410 putchar(c);
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005411 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005412 }
5413}
5414
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005415static void bc_num_printNewline(void)
5416{
5417 if (G.prog.nchars == G.prog.len - 1) {
5418 bb_putchar('\\');
5419 bb_putchar('\n');
5420 G.prog.nchars = 0;
5421 }
5422}
5423
5424#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005425static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005426{
5427 (void) radix;
5428 bb_putchar((char) num);
5429 G.prog.nchars += width;
5430}
5431#endif
5432
5433static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5434{
5435 size_t exp, pow;
5436
5437 bc_num_printNewline();
5438 bb_putchar(radix ? '.' : ' ');
5439 ++G.prog.nchars;
5440
5441 bc_num_printNewline();
5442 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5443 continue;
5444
5445 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5446 size_t dig;
5447 bc_num_printNewline();
5448 dig = num / pow;
5449 num -= dig * pow;
5450 bb_putchar(((char) dig) + '0');
5451 }
5452}
5453
5454static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5455{
5456 if (radix) {
5457 bc_num_printNewline();
5458 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005459 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005460 }
5461
5462 bc_num_printNewline();
5463 bb_putchar(bb_hexdigits_upcase[num]);
5464 G.prog.nchars += width;
5465}
5466
5467static void bc_num_printDecimal(BcNum *n)
5468{
5469 size_t i, rdx = n->rdx - 1;
5470
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005471 if (n->neg) {
5472 bb_putchar('-');
5473 G.prog.nchars++;
5474 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005475
5476 for (i = n->len - 1; i < n->len; --i)
5477 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5478}
5479
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005480typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5481
Denys Vlasenko10bde142018-12-27 18:23:58 +01005482static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005483{
5484 BcStatus s;
5485 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005486 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005487 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005488 BcNum intp, fracp, digit, frac_len;
5489 unsigned long dig, *ptr;
5490 size_t i;
5491 bool radix;
5492
5493 if (n->len == 0) {
5494 print(0, width, false);
5495 RETURN_STATUS(BC_STATUS_SUCCESS);
5496 }
5497
5498 bc_vec_init(&stack, sizeof(long), NULL);
5499 bc_num_init(&intp, n->len);
5500 bc_num_init(&fracp, n->rdx);
5501 bc_num_init(&digit, width);
5502 bc_num_init(&frac_len, BC_NUM_INT(n));
5503 bc_num_copy(&intp, n);
5504 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005505 base.cap = ARRAY_SIZE(base_digs);
5506 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005507 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005508
5509 bc_num_truncate(&intp, intp.rdx);
5510 s = zbc_num_sub(n, &intp, &fracp, 0);
5511 if (s) goto err;
5512
5513 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005514 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005515 if (s) goto err;
5516 s = zbc_num_ulong(&digit, &dig);
5517 if (s) goto err;
5518 bc_vec_push(&stack, &dig);
5519 }
5520
5521 for (i = 0; i < stack.len; ++i) {
5522 ptr = bc_vec_item_rev(&stack, i);
5523 print(*ptr, width, false);
5524 }
5525
5526 if (!n->rdx) goto err;
5527
5528 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005529 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005530 if (s) goto err;
5531 s = zbc_num_ulong(&fracp, &dig);
5532 if (s) goto err;
5533 bc_num_ulong2num(&intp, dig);
5534 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5535 if (s) goto err;
5536 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005537 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005538 if (s) goto err;
5539 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005540 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005541 bc_num_free(&frac_len);
5542 bc_num_free(&digit);
5543 bc_num_free(&fracp);
5544 bc_num_free(&intp);
5545 bc_vec_free(&stack);
5546 RETURN_STATUS(s);
5547}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005548#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005549
Denys Vlasenko10bde142018-12-27 18:23:58 +01005550static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005551{
5552 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005553 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005554 BcNumDigitOp print;
5555 bool neg = n->neg;
5556
5557 if (neg) {
5558 bb_putchar('-');
5559 G.prog.nchars++;
5560 }
5561
5562 n->neg = false;
5563
Denys Vlasenko680ccd32018-12-31 19:42:13 +01005564 if (G.prog.ob_t <= 16) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005565 width = 1;
5566 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005567 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005568 unsigned i = G.prog.ob_t - 1;
5569 width = 0;
5570 for (;;) {
5571 width++;
5572 i /= 10;
5573 if (i == 0)
5574 break;
5575 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005576 print = bc_num_printDigits;
5577 }
5578
Denys Vlasenko10bde142018-12-27 18:23:58 +01005579 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005580 n->neg = neg;
5581
5582 RETURN_STATUS(s);
5583}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005584#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005585
Denys Vlasenko10bde142018-12-27 18:23:58 +01005586static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005587{
5588 BcStatus s = BC_STATUS_SUCCESS;
5589
5590 bc_num_printNewline();
5591
5592 if (n->len == 0) {
5593 bb_putchar('0');
5594 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005595 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005596 bc_num_printDecimal(n);
5597 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005598 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005599
5600 if (newline) {
5601 bb_putchar('\n');
5602 G.prog.nchars = 0;
5603 }
5604
5605 RETURN_STATUS(s);
5606}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005607#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005608
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005609#if !ENABLE_DC
5610// for bc, idx is always 0
5611#define xc_program_print(inst, idx) \
5612 xc_program_print(inst)
5613#endif
5614static BC_STATUS xc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005615{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005616 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005617 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005618 BcNum *num;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005619 IF_NOT_DC(size_t idx = 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005620
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005621 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005622 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005623
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005624 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005625#if ENABLE_BC
5626 if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID)
5627 // void function's result on stack, ignore
5628 RETURN_STATUS(BC_STATUS_SUCCESS);
5629#endif
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005630 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005631 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005632
5633 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005634 s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005635#if ENABLE_BC
5636 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5637#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005638 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005639 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005640
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005641 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005642 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005643
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005644 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005645 char *nl;
5646 G.prog.nchars += printf("%s", str);
5647 nl = strrchr(str, '\n');
5648 if (nl)
5649 G.prog.nchars = strlen(nl + 1);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005650 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005651 xc_program_printString(str);
Denys Vlasenko266bec82019-01-02 05:03:53 +01005652 if (inst == XC_INST_PRINT)
5653 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005654 }
5655 }
5656
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005657 if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005658
Denys Vlasenko29301232018-12-11 15:29:32 +01005659 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005660}
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005661#define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005662
Denys Vlasenko10bde142018-12-27 18:23:58 +01005663static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005664{
5665 BcStatus s;
5666 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005667 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005668
Denys Vlasenko10bde142018-12-27 18:23:58 +01005669 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005670 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005671
5672 bc_num_init(&res.d.n, num->len);
5673 bc_num_copy(&res.d.n, num);
5674 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5675
Denys Vlasenko10bde142018-12-27 18:23:58 +01005676 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005677
Denys Vlasenko29301232018-12-11 15:29:32 +01005678 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005679}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005680#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005681
Denys Vlasenko10bde142018-12-27 18:23:58 +01005682static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005683{
5684 BcStatus s;
5685 BcResult *opd1, *opd2, res;
5686 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005687 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005688
Denys Vlasenko10bde142018-12-27 18:23:58 +01005689 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005690 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005691
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005692 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005693
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005694 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005695 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005696 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005697 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005698 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005699 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005700 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005701 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005702 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005703 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005704 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005705 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005706 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005707 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005708 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005709 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005710 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005711 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005712 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005713 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005714 cond = (cond > 0);
5715 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005716 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005717 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005718 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005719 }
5720 }
5721
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005722 if (cond) bc_num_one(&res.d.n);
5723 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005724
Denys Vlasenko10bde142018-12-27 18:23:58 +01005725 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005727 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005728}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005729#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005730
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005731#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005732static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005733{
5734 BcNum n2;
5735 BcResult res;
5736
5737 memset(&n2, 0, sizeof(BcNum));
5738 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005739 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
5741 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005742 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005743 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005744 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005745 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005746 }
5747
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005748 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005749 bc_vec_push(v, &n2);
5750
Denys Vlasenko29301232018-12-11 15:29:32 +01005751 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005752}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005753#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005754#endif // ENABLE_DC
5755
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005756static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005757{
5758 BcStatus s;
5759 BcResult *ptr, r;
5760 BcVec *v;
5761 BcNum *n;
5762
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005763 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005764 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005765
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005766 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005767 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005768 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005769 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005770
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005771#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005772 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005773 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005774 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005775 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005776#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005777
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005778 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005779 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005780
5781 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko10bde142018-12-27 18:23:58 +01005782 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005783
5784 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005785 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005786 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005787 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005788 bc_array_init(&r.d.v, true);
5789 bc_array_copy(&r.d.v, (BcVec *) n);
5790 }
5791
5792 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005793 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005794
Denys Vlasenko29301232018-12-11 15:29:32 +01005795 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005796}
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005797#define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005798
Denys Vlasenko10bde142018-12-27 18:23:58 +01005799static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005800{
5801 BcStatus s;
5802 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005803 BcNum *l, *r;
5804 bool assign = (inst == XC_INST_ASSIGN);
5805 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005806
Denys Vlasenko10bde142018-12-27 18:23:58 +01005807 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005808 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005809
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005810 ib = left->t == XC_RESULT_IBASE;
5811 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005812
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005813#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005814 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005815 BcVec *v;
5816
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005817 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005818 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005819 v = xc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005820
Denys Vlasenkofa210792018-12-19 19:35:40 +01005821 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005822 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005823#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005824
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005825 if (left->t == XC_RESULT_CONSTANT
5826 || left->t == XC_RESULT_TEMP
5827 IF_BC(|| left->t == BC_RESULT_VOID)
5828 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005829 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005830 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005831
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005832#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005833 if (assign)
5834 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005835 else {
5836 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005837 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 +01005838 }
5839 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005840#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005842#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005843
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005844 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005845 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005846 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5847 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5848 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005849 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005850 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005851 size_t max;
5852 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005853
Denys Vlasenko29301232018-12-11 15:29:32 +01005854 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005855 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005856 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005857 if (sc) {
5858 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005859 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005860 } else {
5861 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005862 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005863 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005864 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005865 }
5866
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005867 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005868 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005869
5870 *ptr = (size_t) val;
5871 s = BC_STATUS_SUCCESS;
5872 }
5873
5874 bc_num_init(&res.d.n, l->len);
5875 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005876 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005877
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005878 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005880#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005881
Denys Vlasenko416ce762018-12-02 20:57:17 +01005882#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005883#define xc_program_pushVar(code, bgn, pop, copy) \
5884 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005885// for bc, 'pop' and 'copy' are always false
5886#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005887static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005888 bool pop, bool copy)
5889{
Gavin Howard01055ba2018-11-03 11:00:21 -06005890 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005891 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005892
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005893 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005894 r.d.id.name = name;
5895
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005896#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005897 if (pop || copy) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005898 BcVec *v = xc_program_search(name, true);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005899 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005901 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005902 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005903 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005904 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005905
5906 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005907 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005908 bc_num_init_DEF_SIZE(&r.d.n);
5909 bc_num_copy(&r.d.n, num);
5910 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005911 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005912 r.d.id.idx = num->rdx;
5913 }
5914
5915 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005916 }
5917#endif // ENABLE_DC
5918
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005919 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005920
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005921 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005922}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005923#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005924
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005925static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005926{
5927 BcStatus s = BC_STATUS_SUCCESS;
5928 BcResult r;
5929 BcNum *num;
5930
Denys Vlasenko10bde142018-12-27 18:23:58 +01005931 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005932
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005933 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005934 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005935 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005936 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005937 BcResult *operand;
5938 unsigned long temp;
5939
Denys Vlasenko10bde142018-12-27 18:23:58 +01005940 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005941 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005942 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 if (s) goto err;
5944
5945 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005946 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 goto err;
5948 }
5949
5950 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005951 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005952 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005953 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005954 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005955 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005956}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005957#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005958
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005959#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005960static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005961{
5962 BcStatus s;
5963 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005964 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005965 char inst2 = inst;
5966
Denys Vlasenko10bde142018-12-27 18:23:58 +01005967 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005968 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005969
5970 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005971 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005972 bc_num_init(&copy.d.n, num->len);
5973 bc_num_copy(&copy.d.n, num);
5974 }
5975
5976 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005977 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5978 ? BC_INST_ASSIGN_PLUS
5979 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005980
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005981 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005982 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005983 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005984
5985 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005986 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005987 }
5988
Denys Vlasenko29301232018-12-11 15:29:32 +01005989 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005990}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005991#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005992
Denys Vlasenko29301232018-12-11 15:29:32 +01005993static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005994{
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005996 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005997 BcId *a;
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01005998 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005999
Denys Vlasenko10bde142018-12-27 18:23:58 +01006000 nparams = xc_program_index(code, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006001 ip.func = xc_program_index(code, idx);
6002 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006003
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006004 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006005 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006006 }
6007 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006008 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006009 }
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006010 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
6012 for (i = 0; i < nparams; ++i) {
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006013 BcResult *arg;
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006014 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
6016 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006017 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006018
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006019 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch
6020 // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions)
6021 ) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006022 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006023 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006024 s = zxc_program_popResultAndCopyToVar(a->name, a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006025 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 }
6027
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006028 a = bc_vec_item(&func->autos, i);
6029 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006030 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006031
Denys Vlasenko10bde142018-12-27 18:23:58 +01006032 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006033 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006034 BcNum n2;
6035 bc_num_init_DEF_SIZE(&n2);
6036 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006037 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006038 BcVec v2;
6039 bc_array_init(&v2, true);
6040 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006041 }
6042 }
6043
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006044 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006045
Denys Vlasenko29301232018-12-11 15:29:32 +01006046 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006047}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006048#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006049
Denys Vlasenko29301232018-12-11 15:29:32 +01006050static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006051{
Gavin Howard01055ba2018-11-03 11:00:21 -06006052 BcResult res;
6053 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006054 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006055 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006056 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006057
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006058 f = xc_program_func(ip->func);
6059
6060 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006061 if (inst == XC_INST_RET) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006062 // bc needs this for e.g. RESULT_CONSTANT ("return 5")
6063 // because bc constants are per-function.
6064 // TODO: maybe avoid if value is already RESULT_TEMP?
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006065 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006066 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006067 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006068
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006069 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006070 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006071 bc_num_init(&res.d.n, num->len);
6072 bc_num_copy(&res.d.n, num);
Denys Vlasenko377cc972019-01-04 00:34:52 +01006073 bc_vec_pop(&G.prog.results);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006074 } else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006075 if (f->voidfunc)
6076 res.t = BC_RESULT_VOID;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006077 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006078 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006079 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006080 bc_vec_push(&G.prog.results, &res);
6081
6082 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006083
6084 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006085 a = (void*)f->autos.v;
6086 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006087 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006088 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 bc_vec_pop(v);
6090 }
6091
Denys Vlasenko29301232018-12-11 15:29:32 +01006092 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006093}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006094#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006095#endif // ENABLE_BC
6096
Denys Vlasenko10bde142018-12-27 18:23:58 +01006097static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006098{
6099 return (unsigned long) n->rdx;
6100}
6101
Denys Vlasenko10bde142018-12-27 18:23:58 +01006102static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006103{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006104 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006106 if (n->rdx != len) return len;
6107 for (;;) {
6108 if (len == 0) break;
6109 len--;
6110 if (n->num[len] != 0) break;
6111 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006112 return len;
6113}
6114
Denys Vlasenko10bde142018-12-27 18:23:58 +01006115static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006116{
6117 BcStatus s;
6118 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006119 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006120 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006121 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006123 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006124 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006125 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006126
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006127 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006128 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006129
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006130#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006131 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006132 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006133#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006134
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006135 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006136
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006137 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006138 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006139#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006140 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006141 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006142 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006143#endif
6144#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006145 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006146 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006147 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
Denys Vlasenko10bde142018-12-27 18:23:58 +01006149 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006150 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006152#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006153 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006154 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006155 }
6156
Denys Vlasenko10bde142018-12-27 18:23:58 +01006157 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006159 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006160}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006161#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006162
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006163#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006164static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006165{
6166 BcStatus s;
6167 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006168 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006169
Denys Vlasenko10bde142018-12-27 18:23:58 +01006170 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006171 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006172
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006173 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174 bc_num_init(&res2.d.n, n2->len);
6175
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006176 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006177 if (s) goto err;
6178
Denys Vlasenko10bde142018-12-27 18:23:58 +01006179 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006180 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006181 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006182
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006183 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006184 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006185 bc_num_free(&res2.d.n);
6186 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006187 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006188}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006189#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006190
Denys Vlasenkofa210792018-12-19 19:35:40 +01006191static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006192{
6193 BcStatus s;
6194 BcResult *r1, *r2, *r3, res;
6195 BcNum *n1, *n2, *n3;
6196
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006197 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006198 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006199 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006200 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006201
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006202 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006203 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006204 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006205 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006206 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006207
6208 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006209 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006211 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006212 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006213 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006214 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006215 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006216 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006217 }
6218 }
6219
6220 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006221 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 if (s) goto err;
6223
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006224 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006225 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006226
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006227 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006228 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006229 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006230 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006231}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006232#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006233
Denys Vlasenkofa210792018-12-19 19:35:40 +01006234static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006235{
Gavin Howard01055ba2018-11-03 11:00:21 -06006236 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006237 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006238
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006239 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006240
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006241 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006242 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006243 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006244}
6245
Denys Vlasenkofa210792018-12-19 19:35:40 +01006246static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006247{
6248 BcStatus s;
6249 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006250 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006251 char **strs;
6252 char *str;
6253 char c;
6254 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006255
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006256 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006257 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006258
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006259 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006260 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006261 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006262
6263 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006264 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006265 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006266 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006267
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006268 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006269 bc_num_copy(&n, num);
6270 bc_num_truncate(&n, n.rdx);
6271
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006272 strmb.cap = ARRAY_SIZE(strmb_digs);
6273 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006274 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006275
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006276 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006278 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279 if (s) goto num_err;
6280
6281 c = (char) val;
6282
6283 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006284 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006285 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006286 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006287 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006288 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 }
6290
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006291 strs = (void*)G.prog.strs.v;
6292 for (idx = 0; idx < G.prog.strs.len; idx++) {
6293 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6294 goto dup;
6295 }
6296 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006297 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006298 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006299 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006300 bc_vec_push(&G.prog.strs, &str);
6301 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006302 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006303 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006304 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006305
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006306 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006307 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006308 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006309 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006310}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006311#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006312
Denys Vlasenkofa210792018-12-19 19:35:40 +01006313static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006314{
6315 BcStatus s;
6316 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006317 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006318 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006319
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006320 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006321 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006322 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006323
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006324 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006325 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006326
Denys Vlasenko57734c92018-12-17 21:14:05 +01006327 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006328 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006329 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006330 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006331 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006332 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006333 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006334 }
6335
Denys Vlasenko29301232018-12-11 15:29:32 +01006336 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006337}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006338#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006339
Denys Vlasenkofa210792018-12-19 19:35:40 +01006340static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006341{
6342 BcStatus s;
6343 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006344 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006345 unsigned long val;
6346
Denys Vlasenko10bde142018-12-27 18:23:58 +01006347 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006348 if (s) RETURN_STATUS(s);
6349 s = zbc_num_ulong(num, &val);
6350 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006351
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006352 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006353
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006354 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006355 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006356 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006357 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006358 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006359
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006360 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006361
Denys Vlasenko29301232018-12-11 15:29:32 +01006362 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006363}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006364#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006365
Denys Vlasenkofa210792018-12-19 19:35:40 +01006366static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006367{
6368 BcStatus s = BC_STATUS_SUCCESS;
6369 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006370 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006371 BcInstPtr ip;
6372 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006373
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006374 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006375 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006376
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006377 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006378
6379 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006380 BcNum *n = n; // for compiler
6381 bool exec;
6382 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006383 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006384 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006385
Denys Vlasenkof706a182018-12-26 20:02:09 +01006386 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006387 (*bgn) += 1;
6388 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006389 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006390
6391 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006392 name = then_name;
6393 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006394 exec = true;
6395 name = else_name;
6396 }
6397
6398 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006399 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006400 v = xc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006401 n = bc_vec_top(v);
6402 }
6403
6404 free(then_name);
6405 free(else_name);
6406
6407 if (!exec) goto exit;
6408 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006409 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006410 goto exit;
6411 }
6412
6413 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006414 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006415 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006416 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006417 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006418 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006419 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006420 if (s || !BC_PROG_STR(n)) goto exit;
6421 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006422 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006423 goto exit;
6424 }
6425
6426 fidx = sidx + BC_PROG_REQ_FUNCS;
6427
Denys Vlasenko10bde142018-12-27 18:23:58 +01006428 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006429
6430 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006431 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006432 char *str;
6433
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006434 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006435 xc_parse_create(fidx);
6436 str = *xc_program_str(sidx);
6437 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006438 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006439
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006440 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006441 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006442 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006443 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006444 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006445 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006446 G.prs = sv_parse; // struct copy
6447 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006448 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006449 bc_vec_pop_all(&f->code);
6450 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006451 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 }
6453
Denys Vlasenko24e41942018-12-21 23:01:26 +01006454 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006455 ip.func = fidx;
6456
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006457 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006458 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006459
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006460 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006461 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006462 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006463 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006464}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006465#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006466#endif // ENABLE_DC
6467
Denys Vlasenko10bde142018-12-27 18:23:58 +01006468static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006469{
Gavin Howard01055ba2018-11-03 11:00:21 -06006470 BcResult res;
6471 unsigned long val;
6472
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006473 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006474 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006475 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006476 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006477 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006478 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006479 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006480
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006481 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006482 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006483 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006484}
6485
Denys Vlasenko10bde142018-12-27 18:23:58 +01006486static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006487{
Gavin Howard01055ba2018-11-03 11:00:21 -06006488 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006489 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006490 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006491 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006492
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006493 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006494 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6495 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006496 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006497 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006498
Denys Vlasenko24e41942018-12-21 23:01:26 +01006499 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 -06006500 switch (inst) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006501 case XC_INST_RET:
6502 if (IS_DC) { // end of '?' reached
6503 bc_vec_pop(&G.prog.exestack);
6504 goto read_updated_ip;
6505 }
6506 // bc: fall through
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006507#if ENABLE_BC
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006508 case BC_INST_RET0:
6509 dbg_exec("BC_INST_RET[0]:");
6510 s = zbc_program_return(inst);
6511 goto read_updated_ip;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006512 case BC_INST_JUMP_ZERO: {
6513 BcNum *num;
6514 bool zero;
6515 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006516 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006517 if (s) RETURN_STATUS(s);
6518 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6519 bc_vec_pop(&G.prog.results);
6520 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006521 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006522 break;
6523 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006524 // else: fall through
6525 }
6526 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006527 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006528 size_t *addr = bc_vec_item(&func->labels, idx);
6529 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6530 ip->inst_idx = *addr;
6531 break;
6532 }
6533 case BC_INST_CALL:
6534 dbg_exec("BC_INST_CALL:");
6535 s = zbc_program_call(code, &ip->inst_idx);
6536 goto read_updated_ip;
6537 case BC_INST_INC_PRE:
6538 case BC_INST_DEC_PRE:
6539 case BC_INST_INC_POST:
6540 case BC_INST_DEC_POST:
6541 dbg_exec("BC_INST_INCDEC:");
6542 s = zbc_program_incdec(inst);
6543 break;
6544 case BC_INST_HALT:
6545 dbg_exec("BC_INST_HALT:");
6546 QUIT_OR_RETURN_TO_MAIN;
6547 break;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006548 case XC_INST_BOOL_OR:
6549 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006550#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006551 case XC_INST_REL_EQ:
6552 case XC_INST_REL_LE:
6553 case XC_INST_REL_GE:
6554 case XC_INST_REL_NE:
6555 case XC_INST_REL_LT:
6556 case XC_INST_REL_GT:
6557 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006558 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006559 break;
6560 case XC_INST_READ:
6561 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006562 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006563 goto read_updated_ip;
6564 case XC_INST_VAR:
6565 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006566 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006567 break;
6568 case XC_INST_ARRAY_ELEM:
6569 case XC_INST_ARRAY:
6570 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6571 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6572 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006573#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006574 case BC_INST_LAST:
6575 dbg_exec("BC_INST_LAST:");
6576 r.t = BC_RESULT_LAST;
6577 bc_vec_push(&G.prog.results, &r);
6578 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006579#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006580 case XC_INST_IBASE:
6581 case XC_INST_OBASE:
6582 case XC_INST_SCALE:
6583 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006584 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006585 break;
6586 case XC_INST_SCALE_FUNC:
6587 case XC_INST_LENGTH:
6588 case XC_INST_SQRT:
6589 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006590 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006591 break;
6592 case XC_INST_NUM:
6593 dbg_exec("XC_INST_NUM:");
6594 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006595 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006596 bc_vec_push(&G.prog.results, &r);
6597 break;
6598 case XC_INST_POP:
6599 dbg_exec("XC_INST_POP:");
6600 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6601 s = bc_error_stack_has_too_few_elements();
6602 else
6603 bc_vec_pop(&G.prog.results);
6604 break;
6605 case XC_INST_PRINT:
6606 case XC_INST_PRINT_POP:
6607 case XC_INST_PRINT_STR:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006608 dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006609 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006610 break;
6611 case XC_INST_STR:
6612 dbg_exec("XC_INST_STR:");
6613 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006614 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006615 bc_vec_push(&G.prog.results, &r);
6616 break;
6617 case XC_INST_POWER:
6618 case XC_INST_MULTIPLY:
6619 case XC_INST_DIVIDE:
6620 case XC_INST_MODULUS:
6621 case XC_INST_PLUS:
6622 case XC_INST_MINUS:
6623 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006624 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006625 break;
6626 case XC_INST_BOOL_NOT: {
6627 BcNum *num;
6628 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006629 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006630 if (s) RETURN_STATUS(s);
6631 bc_num_init_DEF_SIZE(&r.d.n);
6632 if (bc_num_cmp(num, &G.prog.zero) == 0)
6633 bc_num_one(&r.d.n);
6634 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006635 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006636 break;
6637 }
6638 case XC_INST_NEG:
6639 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006640 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006641 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006642#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006643 case BC_INST_ASSIGN_POWER:
6644 case BC_INST_ASSIGN_MULTIPLY:
6645 case BC_INST_ASSIGN_DIVIDE:
6646 case BC_INST_ASSIGN_MODULUS:
6647 case BC_INST_ASSIGN_PLUS:
6648 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006649#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006650 case XC_INST_ASSIGN:
6651 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006652 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006653 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006654#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006655 case DC_INST_POP_EXEC:
6656 dbg_exec("DC_INST_POP_EXEC:");
6657 bc_vec_pop(&G.prog.exestack);
6658 goto read_updated_ip;
6659 case DC_INST_MODEXP:
6660 dbg_exec("DC_INST_MODEXP:");
6661 s = zdc_program_modexp();
6662 break;
6663 case DC_INST_DIVMOD:
6664 dbg_exec("DC_INST_DIVMOD:");
6665 s = zdc_program_divmod();
6666 break;
6667 case DC_INST_EXECUTE:
6668 case DC_INST_EXEC_COND:
6669 dbg_exec("DC_INST_EXEC[_COND]:");
6670 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6671 goto read_updated_ip;
6672 case DC_INST_PRINT_STACK: {
6673 size_t idx;
6674 dbg_exec("DC_INST_PRINT_STACK:");
6675 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006676 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006677 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006678 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006679 break;
6680 }
6681 case DC_INST_CLEAR_STACK:
6682 dbg_exec("DC_INST_CLEAR_STACK:");
6683 bc_vec_pop_all(&G.prog.results);
6684 break;
6685 case DC_INST_STACK_LEN:
6686 dbg_exec("DC_INST_STACK_LEN:");
6687 dc_program_stackLen();
6688 break;
6689 case DC_INST_DUPLICATE:
6690 dbg_exec("DC_INST_DUPLICATE:");
6691 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6692 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6693 ptr = bc_vec_top(&G.prog.results);
6694 dc_result_copy(&r, ptr);
6695 bc_vec_push(&G.prog.results, &r);
6696 break;
6697 case DC_INST_SWAP: {
6698 BcResult *ptr2;
6699 dbg_exec("DC_INST_SWAP:");
6700 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6701 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6702 ptr = bc_vec_item_rev(&G.prog.results, 0);
6703 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6704 memcpy(&r, ptr, sizeof(BcResult));
6705 memcpy(ptr, ptr2, sizeof(BcResult));
6706 memcpy(ptr2, &r, sizeof(BcResult));
6707 break;
6708 }
6709 case DC_INST_ASCIIFY:
6710 dbg_exec("DC_INST_ASCIIFY:");
6711 s = zdc_program_asciify();
6712 break;
6713 case DC_INST_PRINT_STREAM:
6714 dbg_exec("DC_INST_PRINT_STREAM:");
6715 s = zdc_program_printStream();
6716 break;
6717 case DC_INST_LOAD:
6718 case DC_INST_PUSH_VAR: {
6719 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006720 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006721 break;
6722 }
6723 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006724 char *name = xc_program_name(code, &ip->inst_idx);
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006725 s = zxc_program_popResultAndCopyToVar(name, true);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006726 free(name);
6727 break;
6728 }
6729 case DC_INST_QUIT:
6730 dbg_exec("DC_INST_QUIT:");
6731 if (G.prog.exestack.len <= 2)
6732 QUIT_OR_RETURN_TO_MAIN;
6733 bc_vec_npop(&G.prog.exestack, 2);
6734 goto read_updated_ip;
6735 case DC_INST_NQUIT:
6736 dbg_exec("DC_INST_NQUIT:");
6737 s = zdc_program_nquit();
6738 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006739#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006740 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006741 // Instruction stack has changed, read new pointers
6742 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006743 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006744 code = func->code.v;
6745 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006746 }
6747
Denys Vlasenkod38af482018-12-04 19:11:02 +01006748 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006749 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006750 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006751 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006752
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006753 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006754 }
6755
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006756 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006757}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006758#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006759
Denys Vlasenko10bde142018-12-27 18:23:58 +01006760static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006761{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006762 char *lenv;
6763 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006764
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006765 lenv = getenv(var);
6766 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 if (!lenv) return len;
6768
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006769 len = bb_strtou(lenv, NULL, 10) - 1;
6770 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006771 len = BC_NUM_PRINT_WIDTH;
6772
6773 return len;
6774}
6775
Denys Vlasenko10bde142018-12-27 18:23:58 +01006776static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006777{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006778 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006779
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006780 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006781 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006782 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006783
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006784 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006785 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006786 BcInstPtr *ip;
6787 BcFunc *f;
6788
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006789 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006790 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006791#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006792 if (G.prs.lex == BC_LEX_SCOLON
6793 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006794 ) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006795 s = zxc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006796 if (s) goto err;
6797 goto check_eof;
6798 }
6799
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006800 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006801 if (s) goto err;
6802
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006803 // Check that next token is a correct stmt delimiter -
6804 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006805 if (G.prs.lex != BC_LEX_SCOLON
6806 && G.prs.lex != XC_LEX_NLINE
6807 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006808 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01006809 bc_error_at("bad statement terminator");
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006810 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006811 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006812 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006813 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006814#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006815 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006816#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006817 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006818#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006819 }
6820 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006821 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006822 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006823 RETURN_STATUS(BC_STATUS_FAILURE);
6824 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006825
Denys Vlasenko39287e02018-12-22 02:23:08 +01006826 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006827 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006828 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006829 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006830 break;
6831 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006832
6833 ip = (void*)G.prog.exestack.v;
6834#if SANITY_CHECKS
6835 if (G.prog.exestack.len != 1) // should have only main's IP
6836 bb_error_msg_and_die("BUG:call stack");
6837 if (ip->func != BC_PROG_MAIN)
6838 bb_error_msg_and_die("BUG:not MAIN");
6839#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006840 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006841 // bc discards strings, constants and code after each
6842 // top-level statement in the "main program".
6843 // This prevents "yes 1 | bc" from growing its memory
6844 // without bound. This can be done because data stack
6845 // is empty and thus can't hold any references to
6846 // strings or constants, there is no generated code
6847 // which can hold references (after we discard one
6848 // we just executed). Code of functions can have references,
6849 // but bc stores function strings/constants in per-function
6850 // storage.
6851 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006852#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006853 if (G.prog.results.len != 0) // should be empty
6854 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006855#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006856 IF_BC(bc_vec_pop_all(&f->strs);)
6857 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006858 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006859 if (G.prog.results.len == 0
6860 && G.prog.vars.len == 0
6861 ) {
6862 // If stack is empty and no registers exist (TODO: or they are all empty),
6863 // we can get rid of accumulated strings and constants.
6864 // In this example dc process should not grow
6865 // its memory consumption with time:
6866 // yes 1pc | dc
6867 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6868 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6869 }
6870 // The code is discarded always (below), thus this example
6871 // should also not grow its memory consumption with time,
6872 // even though its data stack is not empty:
6873 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006874 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006875 // We drop generated and executed code for both bc and dc:
6876 bc_vec_pop_all(&f->code);
6877 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006878 }
Denys Vlasenko6842c602019-01-04 05:41:47 +01006879
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006880 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006881 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006882}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006883#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006884
Denys Vlasenko10bde142018-12-27 18:23:58 +01006885static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006886{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006887 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006888 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006889 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006890 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006891
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006892 G.prs.lex_filename = filename;
6893 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006894 G.err_line = G.prs.lex_line = 1;
Denys Vlasenko22314682019-01-01 21:50:14 +01006895 dbg_lex("p->lex_line reset to 1");
Gavin Howard01055ba2018-11-03 11:00:21 -06006896
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006897 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006898 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006899 // We do not stop looping on errors here if reading stdin.
6900 // Example: start interactive bc and enter "return".
6901 // It should say "'return' not in a function"
6902 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006903 } while (G.prs.lex_input_fp == stdin);
6904 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006905 RETURN_STATUS(s);
6906}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006907#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006908
Denys Vlasenko10bde142018-12-27 18:23:58 +01006909static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006910{
6911 BcStatus s;
6912 FILE *fp;
6913
6914 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006915 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006916 fclose(fp);
6917
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006918 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006919}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006920#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006921
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006922#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006923static void bc_vm_info(void)
6924{
6925 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006926 "Adapted from https://github.com/gavinhoward/bc\n"
6927 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006928 , applet_name);
6929}
6930
6931static void bc_args(char **argv)
6932{
6933 unsigned opts;
6934 int i;
6935
6936 GETOPT_RESET();
6937#if ENABLE_FEATURE_BC_LONG_OPTIONS
6938 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6939 "warn\0" No_argument "w"
6940 "version\0" No_argument "v"
6941 "standard\0" No_argument "s"
6942 "quiet\0" No_argument "q"
6943 "mathlib\0" No_argument "l"
6944 "interactive\0" No_argument "i"
6945 );
6946#else
6947 opts = option_mask32 |= getopt32(argv, "wvsqli");
6948#endif
6949 if (getenv("POSIXLY_CORRECT"))
6950 option_mask32 |= BC_FLAG_S;
6951
6952 if (opts & BC_FLAG_V) {
6953 bc_vm_info();
6954 exit(0);
6955 }
6956
6957 for (i = optind; argv[i]; ++i)
6958 bc_vec_push(&G.files, argv + i);
6959}
6960
6961static void bc_vm_envArgs(void)
6962{
6963 BcVec v;
6964 char *buf;
6965 char *env_args = getenv("BC_ENV_ARGS");
6966
6967 if (!env_args) return;
6968
6969 G.env_args = xstrdup(env_args);
6970 buf = G.env_args;
6971
6972 bc_vec_init(&v, sizeof(char *), NULL);
6973
6974 while (*(buf = skip_whitespace(buf)) != '\0') {
6975 bc_vec_push(&v, &buf);
6976 buf = skip_non_whitespace(buf);
6977 if (!*buf)
6978 break;
6979 *buf++ = '\0';
6980 }
6981
6982 // NULL terminate, and pass argv[] so that first arg is argv[1]
6983 if (sizeof(int) == sizeof(char*)) {
6984 bc_vec_push(&v, &const_int_0);
6985 } else {
6986 static char *const nullptr = NULL;
6987 bc_vec_push(&v, &nullptr);
6988 }
6989 bc_args(((char **)v.v) - 1);
6990
6991 bc_vec_free(&v);
6992}
6993
6994static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006995 "scale=20"
6996"\n" "define e(x){"
6997"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006998////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6999//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7000//e(-.998896): GNU:.36828580434569428695
7001// above code:.36828580434569428696
7002// actual value:.3682858043456942869594...
7003// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007004"\n" "b=ibase"
7005"\n" "ibase=A"
7006"\n" "if(x<0){"
7007"\n" "n=1"
7008"\n" "x=-x"
7009"\n" "}"
7010"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007011"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007012"\n" "scale=scale(x)+1"
7013"\n" "while(x>1){"
7014"\n" "d+=1"
7015"\n" "x/=2"
7016"\n" "scale+=1"
7017"\n" "}"
7018"\n" "scale=r"
7019"\n" "r=x+1"
7020"\n" "p=x"
7021"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007022"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007023"\n" "p*=x"
7024"\n" "f*=i"
7025"\n" "v=p/f"
7026"\n" "r+=v"
7027"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007028"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007029"\n" "scale=s"
7030"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007031"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007032"\n" "return(r/1)"
7033"\n" "}"
7034"\n" "define l(x){"
7035"\n" "auto b,s,r,p,a,q,i,v"
7036"\n" "b=ibase"
7037"\n" "ibase=A"
7038"\n" "if(x<=0){"
7039"\n" "r=(1-10^scale)/1"
7040"\n" "ibase=b"
7041"\n" "return(r)"
7042"\n" "}"
7043"\n" "s=scale"
7044"\n" "scale+=6"
7045"\n" "p=2"
7046"\n" "while(x>=2){"
7047"\n" "p*=2"
7048"\n" "x=sqrt(x)"
7049"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007050"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007051"\n" "p*=2"
7052"\n" "x=sqrt(x)"
7053"\n" "}"
7054"\n" "r=a=(x-1)/(x+1)"
7055"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007056"\n" "v=1"
7057"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007058"\n" "a*=q"
7059"\n" "v=a/i"
7060"\n" "r+=v"
7061"\n" "}"
7062"\n" "r*=p"
7063"\n" "scale=s"
7064"\n" "ibase=b"
7065"\n" "return(r/1)"
7066"\n" "}"
7067"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007068"\n" "auto b,s,r,a,q,i"
7069"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007070"\n" "b=ibase"
7071"\n" "ibase=A"
7072"\n" "s=scale"
7073"\n" "scale=1.1*s+2"
7074"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007075"\n" "scale=0"
7076"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007077"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007078"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007079"\n" "scale=s+2"
7080"\n" "r=a=x"
7081"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007082"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007083"\n" "a*=q/(i*(i-1))"
7084"\n" "r+=a"
7085"\n" "}"
7086"\n" "scale=s"
7087"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007088"\n" "return(r/1)"
7089"\n" "}"
7090"\n" "define c(x){"
7091"\n" "auto b,s"
7092"\n" "b=ibase"
7093"\n" "ibase=A"
7094"\n" "s=scale"
7095"\n" "scale*=1.2"
7096"\n" "x=s(2*a(1)+x)"
7097"\n" "scale=s"
7098"\n" "ibase=b"
7099"\n" "return(x/1)"
7100"\n" "}"
7101"\n" "define a(x){"
7102"\n" "auto b,s,r,n,a,m,t,f,i,u"
7103"\n" "b=ibase"
7104"\n" "ibase=A"
7105"\n" "n=1"
7106"\n" "if(x<0){"
7107"\n" "n=-1"
7108"\n" "x=-x"
7109"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007110"\n" "if(scale<65){"
7111"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7112"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007113"\n" "}"
7114"\n" "s=scale"
7115"\n" "if(x>.2){"
7116"\n" "scale+=5"
7117"\n" "a=a(.2)"
7118"\n" "}"
7119"\n" "scale=s+3"
7120"\n" "while(x>.2){"
7121"\n" "m+=1"
7122"\n" "x=(x-.2)/(1+.2*x)"
7123"\n" "}"
7124"\n" "r=u=x"
7125"\n" "f=-x*x"
7126"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007127"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007128"\n" "u*=f"
7129"\n" "t=u/i"
7130"\n" "r+=t"
7131"\n" "}"
7132"\n" "scale=s"
7133"\n" "ibase=b"
7134"\n" "return((m*a+r)/n)"
7135"\n" "}"
7136"\n" "define j(n,x){"
7137"\n" "auto b,s,o,a,i,v,f"
7138"\n" "b=ibase"
7139"\n" "ibase=A"
7140"\n" "s=scale"
7141"\n" "scale=0"
7142"\n" "n/=1"
7143"\n" "if(n<0){"
7144"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007145"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007146"\n" "}"
7147"\n" "a=1"
7148"\n" "for(i=2;i<=n;++i)a*=i"
7149"\n" "scale=1.5*s"
7150"\n" "a=(x^n)/2^n/a"
7151"\n" "r=v=1"
7152"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007153"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007154"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007155"\n" "v=v*f/i/(n+i)"
7156"\n" "r+=v"
7157"\n" "}"
7158"\n" "scale=s"
7159"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007160"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007161"\n" "return(a*r/1)"
7162"\n" "}"
7163};
7164#endif // ENABLE_BC
7165
Denys Vlasenko10bde142018-12-27 18:23:58 +01007166static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007167{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007168 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007169 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007170 size_t i;
7171
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007172#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007173 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007174 // We know that internal library is not buggy,
7175 // thus error checking is normally disabled.
7176# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007177 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007178 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007179 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007180#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007181
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007182 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007183 fname = (void*)G.files.v;
7184 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007185 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007186 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7187 // Debug config, non-interactive mode:
7188 // return all the way back to main.
7189 // Non-debug builds do not come here
7190 // in non-interactive mode, they exit.
7191 RETURN_STATUS(s);
7192 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007193 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007194
Denys Vlasenko91cde952018-12-10 20:56:08 +01007195 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007196 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007197
Denys Vlasenko19f11072018-12-12 22:48:19 +01007198 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007199}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007200#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007201
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007202#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007203static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007204{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007205 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007206 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007207 bc_vec_free(&G.prog.vars);
7208 bc_vec_free(&G.prog.var_map);
7209 bc_vec_free(&G.prog.arrs);
7210 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007211 IF_DC(bc_vec_free(&G.prog.strs);)
7212 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007213 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007214 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007215 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007216 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007217 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007218 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007219}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007220#endif
7221
Denys Vlasenko10bde142018-12-27 18:23:58 +01007222static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007223{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007224 BcInstPtr ip;
7225
Denys Vlasenko82269122018-12-14 16:30:56 +01007226 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007227 memset(&ip, 0, sizeof(BcInstPtr));
7228
Denys Vlasenko82269122018-12-14 16:30:56 +01007229 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007230 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007231 G.prog.ob_t = 10;
7232
Denys Vlasenko503faf92018-12-20 16:24:18 +01007233 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7234 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007235
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007236 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007237 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007238
Denys Vlasenko503faf92018-12-20 16:24:18 +01007239 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7240 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007241
7242 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007243 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007244
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007245 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007246 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007247 // a valid function name (and be short)
7248 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007249 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007250 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007251 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007252 xc_program_add_fn();
7253 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007254 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007255
7256 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007257 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007258
7259 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007260 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007261
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007262 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7263 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007264 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007265 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7266 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007267
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007268 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007269}
Gavin Howard01055ba2018-11-03 11:00:21 -06007270
Denys Vlasenko10bde142018-12-27 18:23:58 +01007271static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007272{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007273 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007274#if ENABLE_FEATURE_EDITING
7275 G.line_input_state = new_line_input_t(DO_HISTORY);
7276#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007277 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007278
Denys Vlasenko10bde142018-12-27 18:23:58 +01007279 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007280 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007281 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007282
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007283//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7284//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007285 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007286#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007287 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007288 // With SA_RESTART, most system calls will restart
7289 // (IOW: they won't fail with EINTR).
7290 // In particular, this means ^C won't cause
7291 // stdout to get into "error state" if SIGINT hits
7292 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007293 //
7294 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007295 // will only be handled after [Enter] since read()
7296 // from stdin is not interrupted by ^C either,
7297 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007298 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007299 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7300
7301 // Without SA_RESTART, this exhibits a bug:
7302 // "while (1) print 1" and try ^C-ing it.
7303 // Intermittently, instead of returning to input line,
7304 // you'll get "output error: Interrupted system call"
7305 // and exit.
7306 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007307#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007308 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007309 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007310 return 0; // "not a tty"
7311}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007312
Denys Vlasenko10bde142018-12-27 18:23:58 +01007313static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007314{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007315 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007316#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007317 if (G_exiting) // it was actually "halt" or "quit"
7318 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007319
7320 bc_vec_free(&G.files);
7321 xc_program_free();
7322 xc_parse_free();
7323 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007324# if ENABLE_FEATURE_EDITING
7325 free_line_input_t(G.line_input_state);
7326# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007327 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007328#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007329 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007330 return st;
7331}
7332
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007333#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007334int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007335int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007336{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007337 int is_tty;
7338
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007339 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007340
Denys Vlasenko10bde142018-12-27 18:23:58 +01007341 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007342
7343 bc_args(argv);
7344
7345 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7346 bc_vm_info();
7347
Denys Vlasenko10bde142018-12-27 18:23:58 +01007348 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007349}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007350#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007351
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007352#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007353int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007354int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007355{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007356 int noscript;
7357
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007358 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007359
7360 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7361 // 1 char wider than bc from the same package.
7362 // Both default width, and xC_LINE_LENGTH=N are wider:
7363 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007364 // |1234\ |
7365 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007366 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007367 // |123\ |
7368 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007369 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007370 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007371
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007372 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7373 noscript = BC_FLAG_I;
7374 for (;;) {
7375 int n = getopt(argc, argv, "e:f:x");
7376 if (n <= 0)
7377 break;
7378 switch (n) {
7379 case 'e':
7380 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007381 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007382 if (n) return n;
7383 break;
7384 case 'f':
7385 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007386 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007387 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007388 break;
7389 case 'x':
7390 option_mask32 |= DC_FLAG_X;
7391 break;
7392 default:
7393 bb_show_usage();
7394 }
7395 }
7396 argv += optind;
7397
7398 while (*argv) {
7399 noscript = 0;
7400 bc_vec_push(&G.files, argv++);
7401 }
7402
7403 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7404
Denys Vlasenko10bde142018-12-27 18:23:58 +01007405 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007406}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007407#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007408
Denys Vlasenko14767602018-12-27 22:52:13 +01007409#endif // DC_BIG
7410