blob: f339b895c148ec0f2257a1d25cc402ce3266bd3f [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Gavin Howardfa495ce2018-12-18 10:03:14 -07004 * Adapted from https://github.com/gavinhoward/bc
5 * Original code copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06006 */
Denys Vlasenko53799502019-01-25 14:24:03 +01007//TODO:
8// maybe implement a^b for non-integer b?
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01009
10#define DEBUG_LEXER 0
11#define DEBUG_COMPILE 0
12#define DEBUG_EXEC 0
13// This can be left enabled for production as well:
14#define SANITY_CHECKS 1
15
Gavin Howard01055ba2018-11-03 11:00:21 -060016//config:config BC
Denys Vlasenkob097a842018-12-28 03:20:17 +010017//config: bool "bc (45 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060018//config: default y
Denys Vlasenkocdadad52018-12-28 15:13:23 +010019//config: select FEATURE_DC_BIG
Gavin Howard01055ba2018-11-03 11:00:21 -060020//config: help
21//config: bc is a command-line, arbitrary-precision calculator with a
22//config: Turing-complete language. See the GNU bc manual
23//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010024//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060025//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010026//config: This bc has five differences to the GNU bc:
27//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config: 2) Arrays are copied before being passed as arguments to
29//config: functions. This behavior is required by the bc spec.
30//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: the number of elements in the array. This prints "1":
32//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060033//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010034//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060035//config: allows POSIX-compliant scripts to work while somewhat
36//config: preserving expected behavior (versus C) and making parsing
37//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010038//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060039//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060040//config:config DC
Denys Vlasenkob097a842018-12-28 03:20:17 +010041//config: bool "dc (36 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060042//config: default y
43//config: help
44//config: dc is a reverse-polish notation command-line calculator which
45//config: supports unlimited precision arithmetic. See the FreeBSD man page
46//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010047//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060048//config:
49//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010050//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060051//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: 4) Like the FreeBSD dc, this dc supports extended registers.
56//config: However, they are implemented differently. When it encounters
57//config: whitespace where a register should be, it skips the whitespace.
58//config: If the character following is not a lowercase letter, an error
59//config: is issued. Otherwise, the register name is parsed by the
Denys Vlasenko14767602018-12-27 22:52:13 +010060//config: following regex: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060061//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010062//config: whitespace. Examples:
63//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: Also note that, like the FreeBSD dc, extended registers are not
65//config: allowed unless the "-x" option is given.
66//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010067//config:if BC || DC # for menuconfig indenting
68//config:
69//config:config FEATURE_DC_BIG
70//config: bool "Use bc code base for dc (larger, more features)"
Denys Vlasenko14767602018-12-27 22:52:13 +010071//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010072//config:
73//config:config FEATURE_DC_LIBM
74//config: bool "Enable power and exp functions (requires libm)"
75//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010076//config: depends on DC && !BC && !FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010077//config: help
78//config: Enable power and exp functions.
79//config: NOTE: This will require libm to be present for linking.
80//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010081//config:config FEATURE_BC_INTERACTIVE
Denys Vlasenko89e785a2018-12-13 16:35:52 +010082//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010084//config: depends on BC || (DC && FEATURE_DC_BIG)
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010086//config: Enable interactive mode: when started on a tty,
87//config: ^C interrupts execution and returns to command line,
88//config: errors also return to command line instead of exiting,
89//config: line editing with history is available.
90//config:
91//config: With this option off, input can still be taken from tty,
92//config: but all errors are fatal, ^C is fatal,
93//config: tty is treated exactly the same as any other
94//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060095//config:
96//config:config FEATURE_BC_LONG_OPTIONS
97//config: bool "Enable bc/dc long options"
98//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010099//config: depends on BC || (DC && FEATURE_DC_BIG)
100//config:
101//config:endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600102
103//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
104//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
105
106//kbuild:lib-$(CONFIG_BC) += bc.o
107//kbuild:lib-$(CONFIG_DC) += bc.o
108
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100109//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600110//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100111//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100113//usage:#define bc_full_usage "\n"
114//usage: "\nArbitrary precision calculator"
115//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100116///////: "\n -i Interactive" - has no effect for now
117//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100118//usage: "\n -l Load standard math library"
119//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -w Warn if extensions are used"
121///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100122//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100123//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600124//usage:
125//usage:#define bc_example_usage
126//usage: "3 + 4.129\n"
127//usage: "1903 - 2893\n"
128//usage: "-129 * 213.28935\n"
129//usage: "12 / -1932\n"
130//usage: "12 % 12\n"
131//usage: "34 ^ 189\n"
132//usage: "scale = 13\n"
133//usage: "ibase = 2\n"
134//usage: "obase = A\n"
135//usage:
136//usage:#define dc_trivial_usage
Denys Vlasenko14767602018-12-27 22:52:13 +0100137//usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600138//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100139//usage:#define dc_full_usage "\n"
140//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko14767602018-12-27 22:52:13 +0100141//usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100142//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100143//usage: "\nf - print entire stack"
144//usage: "\nk - pop the value and set the precision"
145//usage: "\ni - pop the value and set input radix"
146//usage: "\no - pop the value and set output radix"
147//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600148//usage:
149//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100150//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600151//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "64\n"
160
161#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100162#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600163
Denys Vlasenko14767602018-12-27 22:52:13 +0100164#if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100165# include "dc.c"
166#else
167
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100168#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100169static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100170#define dbg_lex(...) \
171 do { \
172 fprintf(stderr, "%*s", lex_indent, ""); \
173 bb_error_msg(__VA_ARGS__); \
174 } while (0)
175#define dbg_lex_enter(...) \
176 do { \
177 dbg_lex(__VA_ARGS__); \
178 lex_indent++; \
179 } while (0)
180#define dbg_lex_done(...) \
181 do { \
182 lex_indent--; \
183 dbg_lex(__VA_ARGS__); \
184 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100185#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100186# define dbg_lex(...) ((void)0)
187# define dbg_lex_enter(...) ((void)0)
188# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100189#endif
190
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100191#if DEBUG_COMPILE
192# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
193#else
194# define dbg_compile(...) ((void)0)
195#endif
196
Denys Vlasenko99b37622018-12-15 20:06:59 +0100197#if DEBUG_EXEC
198# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
199#else
200# define dbg_exec(...) ((void)0)
201#endif
202
Gavin Howard01055ba2018-11-03 11:00:21 -0600203typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100204 BC_STATUS_SUCCESS = 0,
205 BC_STATUS_FAILURE = 1,
Gavin Howard01055ba2018-11-03 11:00:21 -0600206} BcStatus;
207
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100208#define BC_VEC_INVALID_IDX ((size_t) -1)
209#define BC_VEC_START_CAP (1 << 5)
Gavin Howard01055ba2018-11-03 11:00:21 -0600210
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100211typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600212
213typedef struct BcVec {
214 char *v;
215 size_t len;
216 size_t cap;
217 size_t size;
218 BcVecFree dtor;
219} BcVec;
220
Gavin Howard01055ba2018-11-03 11:00:21 -0600221typedef signed char BcDig;
222
223typedef struct BcNum {
224 BcDig *restrict num;
225 size_t rdx;
226 size_t len;
227 size_t cap;
228 bool neg;
229} BcNum;
230
Denys Vlasenko680ccd32018-12-31 19:42:13 +0100231#define BC_NUM_MAX_IBASE 36
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100232// larger value might speed up BIGNUM calculations a bit:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100233#define BC_NUM_DEF_SIZE 16
234#define BC_NUM_PRINT_WIDTH 69
Gavin Howard01055ba2018-11-03 11:00:21 -0600235
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100236#define BC_NUM_KARATSUBA_LEN 32
Gavin Howard01055ba2018-11-03 11:00:21 -0600237
Gavin Howard01055ba2018-11-03 11:00:21 -0600238typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100239#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600240 BC_INST_INC_PRE,
241 BC_INST_DEC_PRE,
242 BC_INST_INC_POST,
243 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100244#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100245 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100247 XC_INST_REL_EQ, // should
248 XC_INST_REL_LE, // match
249 XC_INST_REL_GE, // LEX
250 XC_INST_REL_NE, // constants
251 XC_INST_REL_LT, // for
252 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600253
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100254 XC_INST_POWER, // operations
255 XC_INST_MULTIPLY, // |
256 XC_INST_DIVIDE, // |
257 XC_INST_MODULUS, // |
258 XC_INST_PLUS, // |
259 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600260
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100261 XC_INST_BOOL_NOT, // |
262 XC_INST_BOOL_OR, // |
263 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100264#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100265 BC_INST_ASSIGN_POWER, // |
266 BC_INST_ASSIGN_MULTIPLY,// |
267 BC_INST_ASSIGN_DIVIDE, // |
268 BC_INST_ASSIGN_MODULUS, // |
269 BC_INST_ASSIGN_PLUS, // |
270 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100271#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100272 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600273
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100274 XC_INST_NUM,
275 XC_INST_VAR,
276 XC_INST_ARRAY_ELEM,
277 XC_INST_ARRAY,
278 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100279
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100280 XC_INST_IBASE, // order of these constans should match other enums
281 XC_INST_OBASE, // order of these constans should match other enums
282 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100283 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100284 XC_INST_LENGTH,
285 XC_INST_READ,
286 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600287
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100288 XC_INST_PRINT,
289 XC_INST_PRINT_POP,
290 XC_INST_STR,
291 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600292
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100293#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100294 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600295 BC_INST_JUMP,
296 BC_INST_JUMP_ZERO,
297
298 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600299 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100300#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100301 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600302
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100303 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100304#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100305 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600306
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100307 DC_INST_MODEXP,
308 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600309
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 DC_INST_EXECUTE,
311 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600312
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100313 DC_INST_ASCIIFY,
314 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_PRINT_STACK,
317 DC_INST_CLEAR_STACK,
318 DC_INST_STACK_LEN,
319 DC_INST_DUPLICATE,
320 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600321
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100322 DC_INST_LOAD,
323 DC_INST_PUSH_VAR,
324 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600325
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100326 DC_INST_QUIT,
327 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600328
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100329 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100330#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600331} BcInst;
332
333typedef struct BcId {
334 char *name;
335 size_t idx;
336} BcId;
337
338typedef struct BcFunc {
339 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100340 IF_BC(BcVec labels;)
341 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100342 IF_BC(BcVec strs;)
343 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100344 IF_BC(size_t nparams;)
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100345 IF_BC(bool voidfunc;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600346} BcFunc;
347
348typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100349 XC_RESULT_TEMP,
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100350 IF_BC(BC_RESULT_VOID,) // same as TEMP, but INST_PRINT will ignore it
Gavin Howard01055ba2018-11-03 11:00:21 -0600351
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100352 XC_RESULT_VAR,
353 XC_RESULT_ARRAY_ELEM,
354 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600355
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100356 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600357
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100358 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
359 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
360 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
361 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100362 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100363 XC_RESULT_CONSTANT,
364 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600365} BcResultType;
366
367typedef union BcResultData {
368 BcNum n;
369 BcVec v;
370 BcId id;
371} BcResultData;
372
373typedef struct BcResult {
374 BcResultType t;
375 BcResultData d;
376} BcResult;
377
378typedef struct BcInstPtr {
379 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100380 size_t inst_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -0600381} BcInstPtr;
382
Denys Vlasenko53799502019-01-25 14:24:03 +0100383typedef enum BcType {
384 BC_TYPE_VAR,
385 BC_TYPE_ARRAY,
386 BC_TYPE_REF,
387} BcType;
388
Gavin Howard01055ba2018-11-03 11:00:21 -0600389typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100390 XC_LEX_EOF,
391 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600392
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100393 XC_LEX_NLINE,
394 XC_LEX_WHITESPACE,
395 XC_LEX_STR,
396 XC_LEX_NAME,
397 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100398
Denys Vlasenko69560f42018-12-24 14:14:23 +0100399 XC_LEX_1st_op,
400 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100401
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100402 XC_LEX_OP_REL_EQ, // should
403 XC_LEX_OP_REL_LE, // match
404 XC_LEX_OP_REL_GE, // INST
405 XC_LEX_OP_REL_NE, // constants
406 XC_LEX_OP_REL_LT, // for
407 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100408
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100409 XC_LEX_OP_POWER, // operations
410 XC_LEX_OP_MULTIPLY, // |
411 XC_LEX_OP_DIVIDE, // |
412 XC_LEX_OP_MODULUS, // |
413 XC_LEX_OP_PLUS, // |
414 XC_LEX_OP_MINUS, // |
415 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100416#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100417 BC_LEX_OP_BOOL_NOT, // |
418 BC_LEX_OP_BOOL_OR, // |
419 BC_LEX_OP_BOOL_AND, // |
420
421 BC_LEX_OP_ASSIGN_POWER, // |
422 BC_LEX_OP_ASSIGN_MULTIPLY, // |
423 BC_LEX_OP_ASSIGN_DIVIDE, // |
424 BC_LEX_OP_ASSIGN_MODULUS, // |
425 BC_LEX_OP_ASSIGN_PLUS, // |
426 BC_LEX_OP_ASSIGN_MINUS, // |
427
428 BC_LEX_OP_ASSIGN, // V
429
Gavin Howard01055ba2018-11-03 11:00:21 -0600430 BC_LEX_OP_INC,
431 BC_LEX_OP_DEC,
432
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100433 BC_LEX_LPAREN, // () are 0x28 and 0x29
434 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600435
Denys Vlasenko10bde142018-12-27 18:23:58 +0100436 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600437 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100438 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600439
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100440 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600441 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100442 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600443
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100444 BC_LEX_KEY_1st_keyword,
445 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600446 BC_LEX_KEY_BREAK,
447 BC_LEX_KEY_CONTINUE,
448 BC_LEX_KEY_DEFINE,
449 BC_LEX_KEY_ELSE,
450 BC_LEX_KEY_FOR,
451 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100452 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100453 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
454 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600455 BC_LEX_KEY_IF,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100456 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_LENGTH,
458 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_PRINT,
460 BC_LEX_KEY_QUIT,
461 BC_LEX_KEY_READ,
462 BC_LEX_KEY_RETURN,
463 BC_LEX_KEY_SCALE,
464 BC_LEX_KEY_SQRT,
465 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100466#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600467
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100468#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100469 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
470 DC_LEX_OP_ASSIGN,
471
472 DC_LEX_LPAREN,
473 DC_LEX_SCOLON,
474 DC_LEX_READ,
475 DC_LEX_IBASE,
476 DC_LEX_SCALE,
477 DC_LEX_OBASE,
478 DC_LEX_LENGTH,
479 DC_LEX_PRINT,
480 DC_LEX_QUIT,
481 DC_LEX_SQRT,
482 DC_LEX_LBRACE,
483
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100484 DC_LEX_EQ_NO_REG,
485 DC_LEX_OP_MODEXP,
486 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600487
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100488 DC_LEX_COLON,
489 DC_LEX_ELSE,
490 DC_LEX_EXECUTE,
491 DC_LEX_PRINT_STACK,
492 DC_LEX_CLEAR_STACK,
493 DC_LEX_STACK_LEVEL,
494 DC_LEX_DUPLICATE,
495 DC_LEX_SWAP,
496 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600497
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100498 DC_LEX_ASCIIFY,
499 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600500
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100501 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
502 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
503 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
504 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
505 DC_LEX_LOAD,
506 DC_LEX_LOAD_POP,
507 DC_LEX_STORE_PUSH,
508 DC_LEX_PRINT_POP,
509 DC_LEX_NQUIT,
510 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100511#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600512} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100513// must match order of BC_LEX_KEY_foo etc above
514#if ENABLE_BC
515struct BcLexKeyword {
516 char name8[8];
517};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100518#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100519 { .name8 = a /*, .posix = b */ }
Denys Vlasenko6cc49622020-11-30 14:58:02 +0100520static const struct BcLexKeyword bc_lex_kws[20] ALIGN8 = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100521 LEX_KW_ENTRY("auto" , 1), // 0
522 LEX_KW_ENTRY("break" , 1), // 1
523 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
524 LEX_KW_ENTRY("define" , 1), // 3
525 LEX_KW_ENTRY("else" , 0), // 4
526 LEX_KW_ENTRY("for" , 1), // 5
527 LEX_KW_ENTRY("halt" , 0), // 6
528 LEX_KW_ENTRY("ibase" , 1), // 7
529 LEX_KW_ENTRY("obase" , 1), // 8
530 LEX_KW_ENTRY("if" , 1), // 9
531 LEX_KW_ENTRY("last" , 0), // 10
532 LEX_KW_ENTRY("length" , 1), // 11
533 LEX_KW_ENTRY("limits" , 0), // 12
534 LEX_KW_ENTRY("print" , 0), // 13
535 LEX_KW_ENTRY("quit" , 1), // 14
536 LEX_KW_ENTRY("read" , 0), // 15
537 LEX_KW_ENTRY("return" , 1), // 16
538 LEX_KW_ENTRY("scale" , 1), // 17
539 LEX_KW_ENTRY("sqrt" , 1), // 18
540 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100541};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100542#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100543#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100544#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100545#define STRING_if (bc_lex_kws[9].name8)
546#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100547enum {
548 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100549 | (1 << 0) // 0
550 | (1 << 1) // 1
551 | (0 << 2) // 2
552 | (1 << 3) // 3
553 | (0 << 4) // 4
554 | (1 << 5) // 5
555 | (0 << 6) // 6
556 | (1 << 7) // 7
557 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100558 | (1 << 9) // 9
559 | (0 << 10) // 10
560 | (1 << 11) // 11
561 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100562 | (0 << 13) // 13
563 | (1 << 14) // 14
564 | (0 << 15) // 15
565 | (1 << 16) // 16
566 | (1 << 17) // 17
567 | (1 << 18) // 18
568 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100569};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100570#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100571
572// This is a bit array that corresponds to token types. An entry is
573// true if the token is valid in an expression, false otherwise.
574// Used to figure out when expr parsing should stop *without error message*
575// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100576// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100577// by later processing.
578enum {
579#define EXBITS(a,b,c,d,e,f,g,h) \
580 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100581 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100582 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100583 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
584 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100585 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
586 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100587 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
588 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
589 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100590#undef EXBITS
591};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100592static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100593{
594#if ULONG_MAX > 0xffffffff
595 // 64-bit version (will not work correctly for 32-bit longs!)
596 return BC_PARSE_EXPRS_BITS & (1UL << i);
597#else
598 // 32-bit version
599 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
600 if (i >= 32) {
601 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
602 i &= 31;
603 }
604 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100605#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100606}
607
608// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100609// [XC_LEX_1st_op...] token types.
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100610static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100611#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100613 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614 OP(2, false), // pow
615 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
616 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100617 OP(1, false), // not
618 OP(7, true ), OP( 7, true ), // or and
619 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
620 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100621 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100622#undef OP
623};
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100624#define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f)
625#define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100626#endif // ENABLE_BC
627
628#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100629static const //BcLexType - should be this type
630uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100631dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100632 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100633 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100634 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100635 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100636 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100637 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100638 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100639 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
640 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
641 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100642 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100643 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
644 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100645 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100646 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
647 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100648 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100649 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
650 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenkof11b5b92019-01-04 15:54:40 +0100651 // QRSTUVWX
652 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID,
653 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR,
654 // YZ
655 XC_LEX_INVALID, DC_LEX_LENGTH,
656 // [\]
657 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100658 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100659 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100660 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100661 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
662 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100663 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100666 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100667 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
668 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100669 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100670 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100671 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100672 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100673};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100674static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100675int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100676dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
677 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
678 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
679 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100680 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
681 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
682 XC_INST_REL_GT, // DC_LEX_LPAREN
683 DC_INST_INVALID, // DC_LEX_SCOLON
684 DC_INST_INVALID, // DC_LEX_READ
685 XC_INST_IBASE, // DC_LEX_IBASE
686 XC_INST_SCALE, // DC_LEX_SCALE
687 XC_INST_OBASE, // DC_LEX_OBASE
688 XC_INST_LENGTH, // DC_LEX_LENGTH
689 XC_INST_PRINT, // DC_LEX_PRINT
690 DC_INST_QUIT, // DC_LEX_QUIT
691 XC_INST_SQRT, // DC_LEX_SQRT
692 XC_INST_REL_GE, // DC_LEX_LBRACE
693 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100694 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
695 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
696 DC_INST_EXECUTE, // DC_LEX_EXECUTE
697 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
698 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
699 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
700 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
701 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
702 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
703 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
704 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
705 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100706 // DC_INST_INVALID in this table either means that corresponding LEX
707 // is not possible for dc, or that it does not compile one-to-one
708 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100709};
710#endif // ENABLE_DC
711
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100712typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100713 smallint lex; // was BcLexType // first member is most used
714 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100715 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100716 const char *lex_inbuf;
717 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100718 const char *lex_filename;
719 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100720 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100721
Gavin Howard01055ba2018-11-03 11:00:21 -0600722 BcFunc *func;
723 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100724 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100725 IF_BC(BcVec exits;)
726 IF_BC(BcVec conds;)
727 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600728} BcParse;
729
Gavin Howard01055ba2018-11-03 11:00:21 -0600730typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600731 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100732 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600733
Denys Vlasenko503faf92018-12-20 16:24:18 +0100734 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600735 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600736 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600737
Gavin Howard01055ba2018-11-03 11:00:21 -0600738 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100739 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600740
741 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100742 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600743
744 BcVec vars;
745 BcVec var_map;
746
747 BcVec arrs;
748 BcVec arr_map;
749
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100750 IF_DC(BcVec strs;)
751 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600752
Gavin Howard01055ba2018-11-03 11:00:21 -0600753 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100754 IF_BC(BcNum one;)
755 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600756} BcProgram;
757
Denys Vlasenko6e618232018-12-25 22:20:14 +0100758struct globals {
759 BcParse prs; // first member is most used
760
761 // For error messages. Can be set to current parsed line,
762 // or [TODO] to current executing line (can be before last parsed one)
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100763 size_t err_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100764
765 BcVec input_buffer;
766
Denys Vlasenko14767602018-12-27 22:52:13 +0100767 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100768 IF_FEATURE_CLEAN_UP(smallint exiting;)
769
770 BcProgram prog;
771
772 BcVec files;
773
774 char *env_args;
775
776#if ENABLE_FEATURE_EDITING
777 line_input_t *line_input_state;
778#endif
779} FIX_ALIASING;
780#define G (*ptr_to_globals)
781#define INIT_G() do { \
782 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
783} while (0)
784#define FREE_G() do { \
785 FREE_PTR_TO_GLOBALS(); \
786} while (0)
787#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
788#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
789#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100790#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100791# define G_interrupt bb_got_signal
792# define G_ttyin G.ttyin
793#else
794# define G_interrupt 0
795# define G_ttyin 0
796#endif
797#if ENABLE_FEATURE_CLEAN_UP
798# define G_exiting G.exiting
799#else
800# define G_exiting 0
801#endif
802#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
803#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
804
Denys Vlasenko6e618232018-12-25 22:20:14 +0100805#if ENABLE_BC
806# define BC_PARSE_REL (1 << 0)
807# define BC_PARSE_PRINT (1 << 1)
808# define BC_PARSE_ARRAY (1 << 2)
809# define BC_PARSE_NOCALL (1 << 3)
810#endif
811
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100812#define BC_PROG_MAIN 0
813#define BC_PROG_READ 1
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100814#if ENABLE_DC
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100815#define BC_PROG_REQ_FUNCS 2
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100816#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600817
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100818#define BC_FLAG_W (1 << 0)
819#define BC_FLAG_V (1 << 1)
820#define BC_FLAG_S (1 << 2)
821#define BC_FLAG_Q (1 << 3)
822#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100823#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
824#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600825
Denys Vlasenko64074a12018-12-07 15:50:14 +0100826#define BC_MAX_OBASE ((unsigned) 999)
827#define BC_MAX_DIM ((unsigned) INT_MAX)
828#define BC_MAX_SCALE ((unsigned) UINT_MAX)
829#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
830#define BC_MAX_NUM BC_MAX_STRING
831// Unused apart from "limits" message. Just show a "biggish number" there.
Denys Vlasenko64074a12018-12-07 15:50:14 +0100832//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
833//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
Denys Vlasenko64074a12018-12-07 15:50:14 +0100834#define BC_MAX_EXP_STR "999999999"
835#define BC_MAX_VARS_STR "999999999"
836
837#define BC_MAX_OBASE_STR "999"
838
839#if INT_MAX == 2147483647
840# define BC_MAX_DIM_STR "2147483647"
841#elif INT_MAX == 9223372036854775807
842# define BC_MAX_DIM_STR "9223372036854775807"
843#else
844# error Strange INT_MAX
845#endif
846
Kang-Che Sungf1593522019-09-05 23:40:38 +0800847#if UINT_MAX == 4294967295U
Denys Vlasenko64074a12018-12-07 15:50:14 +0100848# define BC_MAX_SCALE_STR "4294967295"
849# define BC_MAX_STRING_STR "4294967294"
Kang-Che Sungf1593522019-09-05 23:40:38 +0800850#elif UINT_MAX == 18446744073709551615U
Denys Vlasenko64074a12018-12-07 15:50:14 +0100851# define BC_MAX_SCALE_STR "18446744073709551615"
852# define BC_MAX_STRING_STR "18446744073709551614"
853#else
854# error Strange UINT_MAX
855#endif
856#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600857
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100858// In configurations where errors abort instead of propagating error
859// return code up the call chain, functions returning BC_STATUS
860// actually don't return anything, they always succeed and return "void".
861// A macro wrapper is provided, which makes this statement work:
862// s = zbc_func(...)
863// and makes it visible to the compiler that s is always zero,
864// allowing compiler to optimize dead code after the statement.
865//
866// To make code more readable, each such function has a "z"
867// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
868//
Denys Vlasenko14767602018-12-27 22:52:13 +0100869#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100870# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100871# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100872# define IF_ERROR_RETURN_POSSIBLE(a) a
873# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100874# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100875# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100876#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100877# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100878# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100879# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
880# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100881# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100882# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100883#endif
884
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100885//
886// Utility routines
887//
Gavin Howard01055ba2018-11-03 11:00:21 -0600888
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100889#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
890#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
891
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100892static void fflush_and_check(void)
893{
894 fflush_all();
895 if (ferror(stdout) || ferror(stderr))
James Byrne69374872019-07-02 11:35:03 +0200896 bb_simple_perror_msg_and_die("output error");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100897}
898
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100899#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100900#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100901do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100902 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100903 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100904 return BC_STATUS_FAILURE; \
905} while (0)
906#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100907static void quit(void) NORETURN;
908static void quit(void)
909{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100910 if (ferror(stdin))
James Byrne69374872019-07-02 11:35:03 +0200911 bb_simple_perror_msg_and_die("input error");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100912 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100913 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100914 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100915}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100916#define QUIT_OR_RETURN_TO_MAIN quit()
917#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100918
Denys Vlasenko5318f812018-12-05 17:48:01 +0100919static void bc_verror_msg(const char *fmt, va_list p)
920{
Denys Vlasenko82269122018-12-14 16:30:56 +0100921 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100922 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100923 sv = applet_name;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100924 applet_name = xasprintf("%s: %s:%lu", applet_name,
925 G.prs.lex_filename, (unsigned long)G.err_line
926 );
Denys Vlasenko5318f812018-12-05 17:48:01 +0100927 }
928 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100929 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100930 free((char*)applet_name);
931 applet_name = sv;
932 }
933}
934
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100935static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100936{
937 va_list p;
938
939 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100940 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100941 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100942
Denys Vlasenkoec603182018-12-17 10:34:02 +0100943 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
944 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
945 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100946}
947
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100948#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100949static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100950{
951 va_list p;
952
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100953 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100954 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100955 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100956
957 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100958 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100959 va_end(p);
960
961 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100962 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100963 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
964
Denys Vlasenkoec603182018-12-17 10:34:02 +0100965 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100966 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100967 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100968}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100969#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100970#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100971
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100972// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
973// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100974// function must not have caller-cleaned parameters on stack.
975// Unfortunately, vararg function API does exactly that on most arches.
976// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100977static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100978{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100979 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100980}
Denys Vlasenko22314682019-01-01 21:50:14 +0100981static ERRORFUNC int bc_error_at(const char *msg)
982{
983 const char *err_at = G.prs.lex_next_at;
984 if (err_at) {
985 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt(
986 "%s at '%.*s'",
987 msg,
988 (int)(strchrnul(err_at, '\n') - err_at),
989 err_at
990 );
991 }
992 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
993}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100994static ERRORFUNC int bc_error_bad_character(char c)
995{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100996 if (!c)
997 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100998 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100999}
Denys Vlasenko7a4e5542019-06-08 12:39:30 +02001000#if ENABLE_BC
Denys Vlasenko22314682019-01-01 21:50:14 +01001001static ERRORFUNC int bc_error_bad_function_definition(void)
1002{
1003 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition");
1004}
Denys Vlasenko7a4e5542019-06-08 12:39:30 +02001005#endif
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006static ERRORFUNC int bc_error_bad_expression(void)
1007{
Denys Vlasenko22314682019-01-01 21:50:14 +01001008 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression");
1009}
1010static ERRORFUNC int bc_error_bad_assignment(void)
1011{
1012 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at(
1013 "bad assignment: left side must be variable or array element"
1014 );
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001015}
1016static ERRORFUNC int bc_error_bad_token(void)
1017{
Denys Vlasenko22314682019-01-01 21:50:14 +01001018 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001019}
1020static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1021{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001022 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001023}
1024static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1025{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001026 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001027}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001028#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001029static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001030{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001031 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001032}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001033#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1034static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001035{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001036 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001037}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001038#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1039static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001040{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001041 RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; this is bad:", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001042}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001043#define zbc_POSIX_does_not_allow_bool_ops_this_is_bad(...) (zbc_POSIX_does_not_allow_bool_ops_this_is_bad(__VA_ARGS__) COMMA_SUCCESS)
1044static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001045{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001046 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001047}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001048#define zbc_POSIX_does_not_allow_empty_X_expression_in_for(...) (zbc_POSIX_does_not_allow_empty_X_expression_in_for(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001049#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001050
Gavin Howard01055ba2018-11-03 11:00:21 -06001051static void bc_vec_grow(BcVec *v, size_t n)
1052{
1053 size_t cap = v->cap * 2;
1054 while (cap < v->len + n) cap *= 2;
1055 v->v = xrealloc(v->v, v->size * cap);
1056 v->cap = cap;
1057}
1058
1059static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1060{
1061 v->size = esize;
1062 v->cap = BC_VEC_START_CAP;
1063 v->len = 0;
1064 v->dtor = dtor;
1065 v->v = xmalloc(esize * BC_VEC_START_CAP);
1066}
1067
Denys Vlasenko7d628012018-12-04 21:46:47 +01001068static void bc_char_vec_init(BcVec *v)
1069{
1070 bc_vec_init(v, sizeof(char), NULL);
1071}
1072
Gavin Howard01055ba2018-11-03 11:00:21 -06001073static void bc_vec_expand(BcVec *v, size_t req)
1074{
1075 if (v->cap < req) {
1076 v->v = xrealloc(v->v, v->size * req);
1077 v->cap = req;
1078 }
1079}
1080
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001081static void bc_vec_pop(BcVec *v)
1082{
1083 v->len--;
1084 if (v->dtor)
1085 v->dtor(v->v + (v->size * v->len));
1086}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001087
Gavin Howard01055ba2018-11-03 11:00:21 -06001088static void bc_vec_npop(BcVec *v, size_t n)
1089{
1090 if (!v->dtor)
1091 v->len -= n;
1092 else {
1093 size_t len = v->len - n;
1094 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1095 }
1096}
1097
Denys Vlasenko7d628012018-12-04 21:46:47 +01001098static void bc_vec_pop_all(BcVec *v)
1099{
1100 bc_vec_npop(v, v->len);
1101}
1102
Denys Vlasenko53799502019-01-25 14:24:03 +01001103static size_t bc_vec_npush(BcVec *v, size_t n, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001104{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001105 size_t len = v->len;
Denys Vlasenko53799502019-01-25 14:24:03 +01001106 if (len + n > v->cap) bc_vec_grow(v, n);
1107 memmove(v->v + (v->size * len), data, v->size * n);
1108 v->len = len + n;
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001109 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001110}
1111
Denys Vlasenko53799502019-01-25 14:24:03 +01001112static size_t bc_vec_push(BcVec *v, const void *data)
1113{
1114 return bc_vec_npush(v, 1, data);
1115 //size_t len = v->len;
1116 //if (len >= v->cap) bc_vec_grow(v, 1);
1117 //memmove(v->v + (v->size * len), data, v->size);
1118 //v->len = len + 1;
1119 //return len;
1120}
1121
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001122// G.prog.results often needs "pop old operand, push result" idiom.
1123// Can do this without a few extra ops
1124static size_t bc_result_pop_and_push(const void *data)
1125{
1126 BcVec *v = &G.prog.results;
1127 char *last;
1128 size_t len = v->len - 1;
1129
1130 last = v->v + (v->size * len);
1131 if (v->dtor)
1132 v->dtor(last);
1133 memmove(last, data, v->size);
1134 return len;
1135}
1136
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001137static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001138{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001139 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001140}
1141
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001142static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001143{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001144 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001145 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001146 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001147}
1148
Gavin Howard01055ba2018-11-03 11:00:21 -06001149static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1150{
1151 if (idx == v->len)
1152 bc_vec_push(v, data);
1153 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001154 char *ptr;
1155
1156 if (v->len == v->cap) bc_vec_grow(v, 1);
1157
1158 ptr = v->v + v->size * idx;
1159
1160 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1161 memmove(ptr, data, v->size);
1162 }
1163}
1164
1165static void bc_vec_string(BcVec *v, size_t len, const char *str)
1166{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001167 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168 bc_vec_expand(v, len + 1);
1169 memcpy(v->v, str, len);
1170 v->len = len;
1171
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001172 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001173}
1174
Gavin Howard01055ba2018-11-03 11:00:21 -06001175static void *bc_vec_item(const BcVec *v, size_t idx)
1176{
1177 return v->v + v->size * idx;
1178}
1179
1180static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1181{
1182 return v->v + v->size * (v->len - idx - 1);
1183}
1184
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001185static void *bc_vec_top(const BcVec *v)
1186{
1187 return v->v + v->size * (v->len - 1);
1188}
1189
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001190static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001191{
1192 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001193 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001194 free(v->v);
1195}
1196
Denys Vlasenko10bde142018-12-27 18:23:58 +01001197static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001198{
1199 return bc_vec_item(&G.prog.fns, idx);
1200}
1201// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001202#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001203
1204#if ENABLE_BC
1205static BcFunc* bc_program_current_func(void)
1206{
1207 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001208 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001209 return func;
1210}
1211#endif
1212
Denys Vlasenko10bde142018-12-27 18:23:58 +01001213static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001214{
1215#if ENABLE_BC
1216 if (IS_BC) {
1217 BcFunc *func = bc_program_current_func();
1218 return bc_vec_item(&func->strs, idx);
1219 }
1220#endif
1221 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1222}
1223
Denys Vlasenko10bde142018-12-27 18:23:58 +01001224static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001225{
1226#if ENABLE_BC
1227 if (IS_BC) {
1228 BcFunc *func = bc_program_current_func();
1229 return bc_vec_item(&func->consts, idx);
1230 }
1231#endif
1232 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1233}
1234
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001235static int bc_id_cmp(const void *e1, const void *e2)
1236{
1237 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1238}
1239
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001240static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001241{
1242 free(((BcId *) id)->name);
1243}
1244
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001245static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001246{
1247 size_t low = 0, high = v->len;
1248
1249 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001250 size_t mid = (low + high) / 2;
1251 BcId *id = bc_vec_item(v, mid);
1252 int result = bc_id_cmp(ptr, id);
1253
1254 if (result == 0)
1255 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001256 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001257 high = mid;
1258 else
1259 low = mid + 1;
1260 }
1261
1262 return low;
1263}
1264
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001265static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001266{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001267 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001268
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001270 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001271 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1272 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001273 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001274 bc_vec_pushAt(v, ptr, n);
1275 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001276}
1277
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001278static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001279{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001280 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001281 if (i >= v->len) return BC_VEC_INVALID_IDX;
1282 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1283}
1284
Gavin Howard01055ba2018-11-03 11:00:21 -06001285static void bc_num_setToZero(BcNum *n, size_t scale)
1286{
1287 n->len = 0;
1288 n->neg = false;
1289 n->rdx = scale;
1290}
1291
1292static void bc_num_zero(BcNum *n)
1293{
1294 bc_num_setToZero(n, 0);
1295}
1296
1297static void bc_num_one(BcNum *n)
1298{
1299 bc_num_setToZero(n, 0);
1300 n->len = 1;
1301 n->num[0] = 1;
1302}
1303
Denys Vlasenko3129f702018-12-09 12:04:44 +01001304// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001305static void bc_num_init(BcNum *n, size_t req)
1306{
1307 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001308 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001309 n->num = xmalloc(req);
1310 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001311 n->rdx = 0;
1312 n->len = 0;
1313 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001314}
1315
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001316static void bc_num_init_DEF_SIZE(BcNum *n)
1317{
1318 bc_num_init(n, BC_NUM_DEF_SIZE);
1319}
1320
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001321static void bc_num_expand(BcNum *n, size_t req)
1322{
1323 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1324 if (req > n->cap) {
1325 n->num = xrealloc(n->num, req);
1326 n->cap = req;
1327 }
1328}
1329
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001330static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001331{
1332 free(((BcNum *) num)->num);
1333}
1334
1335static void bc_num_copy(BcNum *d, BcNum *s)
1336{
1337 if (d != s) {
1338 bc_num_expand(d, s->cap);
1339 d->len = s->len;
1340 d->neg = s->neg;
1341 d->rdx = s->rdx;
1342 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1343 }
1344}
1345
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001346static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347{
1348 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001349 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001350
Denys Vlasenko1557b762018-12-22 21:37:46 +01001351 result = 0;
1352 i = n->len;
1353 while (i > n->rdx) {
1354 unsigned long prev = result;
1355 result = result * 10 + n->num[--i];
1356 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1357 // 0x1ff00000 * 10 is 0x13f600000,
1358 // or 0x3f600000 truncated to 32 bits. Which is larger.
1359 // However, (N*10)/8 < N check is always correct.
1360 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001361 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001363 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001364
Denys Vlasenko29301232018-12-11 15:29:32 +01001365 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001366}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001367#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1368
1369static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1370{
1371 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1372
1373 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1374}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001375#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001376
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001377#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1378# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1379#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1380# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1381#endif
1382// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1383// would not hit realloc() code path - not good if num[] is not malloced
1384
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001385static void bc_num_ulong2num(BcNum *n, unsigned long val)
1386{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001387 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001388
1389 bc_num_zero(n);
1390
1391 if (val == 0) return;
1392
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001393 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001394
1395 ptr = n->num;
1396 for (;;) {
1397 n->len++;
1398 *ptr++ = val % 10;
1399 val /= 10;
1400 if (val == 0) break;
1401 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001402}
1403
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001404static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001405{
1406 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001407 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001408 a[i] -= b[i];
1409 for (j = i; a[j] < 0;) {
1410 a[j++] += 10;
1411 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001412 }
1413 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001414}
1415
1416static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1417{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001418 size_t i = len;
1419 for (;;) {
1420 int c;
1421 if (i == 0)
1422 return 0;
1423 i--;
1424 c = a[i] - b[i];
1425 if (c != 0) {
1426 i++;
1427 if (c < 0)
1428 return -i;
1429 return i;
1430 }
1431 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001432}
1433
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001434#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1435#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1436#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1437//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1438static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1439{
1440 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1441}
1442//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1443static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1444{
1445 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1446}
1447
Gavin Howard01055ba2018-11-03 11:00:21 -06001448static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1449{
1450 size_t i, min, a_int, b_int, diff;
1451 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001452 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001453 ssize_t cmp;
1454
1455 if (a == b) return 0;
1456 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1457 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001458
1459 if (a->neg != b->neg) // signs of a and b differ
1460 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1461 return (int)b->neg - (int)a->neg;
1462 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001463
1464 a_int = BC_NUM_INT(a);
1465 b_int = BC_NUM_INT(b);
1466 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001467
Denys Vlasenko00841372019-11-23 17:25:21 +01001468 if (a_int != 0) {
1469 if (neg) return - (ssize_t) a_int;
1470 return (ssize_t) a_int;
1471 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001472
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001473 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001474 if (a_max) {
1475 min = b->rdx;
1476 diff = a->rdx - b->rdx;
1477 max_num = a->num + diff;
1478 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001479 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1480 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001481 min = a->rdx;
1482 diff = b->rdx - a->rdx;
1483 max_num = b->num + diff;
1484 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001485 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001486 }
1487
1488 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001489 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001490
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001491 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001492 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001493 }
1494
1495 return 0;
1496}
1497
1498static void bc_num_truncate(BcNum *n, size_t places)
1499{
1500 if (places == 0) return;
1501
1502 n->rdx -= places;
1503
1504 if (n->len != 0) {
1505 n->len -= places;
1506 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1507 }
1508}
1509
1510static void bc_num_extend(BcNum *n, size_t places)
1511{
1512 size_t len = n->len + places;
1513
1514 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001515 if (n->cap < len) bc_num_expand(n, len);
1516
1517 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1518 memset(n->num, 0, sizeof(BcDig) * places);
1519
1520 n->len += places;
1521 n->rdx += places;
1522 }
1523}
1524
1525static void bc_num_clean(BcNum *n)
1526{
1527 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1528 if (n->len == 0)
1529 n->neg = false;
1530 else if (n->len < n->rdx)
1531 n->len = n->rdx;
1532}
1533
1534static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1535{
1536 if (n->rdx < scale)
1537 bc_num_extend(n, scale - n->rdx);
1538 else
1539 bc_num_truncate(n, n->rdx - scale);
1540
1541 bc_num_clean(n);
1542 if (n->len != 0) n->neg = !neg1 != !neg2;
1543}
1544
1545static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1546 BcNum *restrict b)
1547{
1548 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001549 b->len = n->len - idx;
1550 a->len = idx;
1551 a->rdx = b->rdx = 0;
1552
1553 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1554 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001555 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001556 bc_num_zero(b);
1557 bc_num_copy(a, n);
1558 }
1559
1560 bc_num_clean(a);
1561 bc_num_clean(b);
1562}
1563
Denys Vlasenko29301232018-12-11 15:29:32 +01001564static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001565{
Denys Vlasenko29301232018-12-11 15:29:32 +01001566 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001567
1568 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1569 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1570 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001571 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001572 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001573
1574 if (n->rdx >= places)
1575 n->rdx -= places;
1576 else {
1577 bc_num_extend(n, places - n->rdx);
1578 n->rdx = 0;
1579 }
1580
1581 bc_num_clean(n);
1582
Denys Vlasenko29301232018-12-11 15:29:32 +01001583 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001584}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001585#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001586
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001587typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1588
1589static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1590 BcNumBinaryOp op, size_t req)
1591{
1592 BcStatus s;
1593 BcNum num2, *ptr_a, *ptr_b;
1594 bool init = false;
1595
1596 if (c == a) {
1597 ptr_a = &num2;
1598 memcpy(ptr_a, c, sizeof(BcNum));
1599 init = true;
1600 } else
1601 ptr_a = a;
1602
1603 if (c == b) {
1604 ptr_b = &num2;
1605 if (c != a) {
1606 memcpy(ptr_b, c, sizeof(BcNum));
1607 init = true;
1608 }
1609 } else
1610 ptr_b = b;
1611
1612 if (init)
1613 bc_num_init(c, req);
1614 else
1615 bc_num_expand(c, req);
1616
1617 s = BC_STATUS_SUCCESS;
1618 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1619
1620 if (init) bc_num_free(&num2);
1621
1622 RETURN_STATUS(s);
1623}
1624#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1625
1626static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1627static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1628static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1629static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1630static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1631static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1632
1633static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1634{
1635 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1636 (void) scale;
1637 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1638}
1639
1640static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1641{
1642 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1643 (void) scale;
1644 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1645}
1646
1647static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1648{
1649 size_t req = BC_NUM_MREQ(a, b, scale);
1650 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1651}
1652
1653static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1654{
1655 size_t req = BC_NUM_MREQ(a, b, scale);
1656 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1657}
1658
1659static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1660{
1661 size_t req = BC_NUM_MREQ(a, b, scale);
1662 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1663}
1664
1665static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1666{
1667 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1668}
1669
Denys Vlasenko10bde142018-12-27 18:23:58 +01001670static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001671 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1672};
1673#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1674#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1675#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1676#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1677#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1678#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1679
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001680static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001681{
1682 BcNum one;
1683 BcDig num[2];
1684
1685 one.cap = 2;
1686 one.num = num;
1687 bc_num_one(&one);
1688
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001689 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001690}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001691#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001692
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001693static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001694{
1695 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1696 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001697 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001698
1699 // Because this function doesn't need to use scale (per the bc spec),
1700 // I am hijacking it to say whether it's doing an add or a subtract.
1701
1702 if (a->len == 0) {
1703 bc_num_copy(c, b);
1704 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001705 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001706 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001707 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001708 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001709 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001710 }
1711
1712 c->neg = a->neg;
1713 c->rdx = BC_MAX(a->rdx, b->rdx);
1714 min_rdx = BC_MIN(a->rdx, b->rdx);
1715 c->len = 0;
1716
1717 if (a->rdx > b->rdx) {
1718 diff = a->rdx - b->rdx;
1719 ptr = a->num;
1720 ptr_a = a->num + diff;
1721 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001722 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001723 diff = b->rdx - a->rdx;
1724 ptr = b->num;
1725 ptr_a = a->num;
1726 ptr_b = b->num + diff;
1727 }
1728
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001729 ptr_c = c->num;
1730 for (i = 0; i < diff; ++i, ++c->len)
1731 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001732
1733 ptr_c += diff;
1734 a_int = BC_NUM_INT(a);
1735 b_int = BC_NUM_INT(b);
1736
1737 if (a_int > b_int) {
1738 min_int = b_int;
1739 max = a_int;
1740 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001741 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001742 min_int = a_int;
1743 max = b_int;
1744 ptr = ptr_b;
1745 }
1746
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001747 carry = 0;
1748 for (i = 0; i < min_rdx + min_int; ++i) {
1749 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001750 carry = in / 10;
1751 ptr_c[i] = (BcDig)(in % 10);
1752 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001753 for (; i < max + min_rdx; ++i) {
1754 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001755 carry = in / 10;
1756 ptr_c[i] = (BcDig)(in % 10);
1757 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001758 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001759
1760 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1761
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001763}
1764
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001765static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001766{
Gavin Howard01055ba2018-11-03 11:00:21 -06001767 ssize_t cmp;
1768 BcNum *minuend, *subtrahend;
1769 size_t start;
1770 bool aneg, bneg, neg;
1771
1772 // Because this function doesn't need to use scale (per the bc spec),
1773 // I am hijacking it to say whether it's doing an add or a subtract.
1774
1775 if (a->len == 0) {
1776 bc_num_copy(c, b);
1777 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001778 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001779 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001780 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001781 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001782 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001783 }
1784
1785 aneg = a->neg;
1786 bneg = b->neg;
1787 a->neg = b->neg = false;
1788
1789 cmp = bc_num_cmp(a, b);
1790
1791 a->neg = aneg;
1792 b->neg = bneg;
1793
1794 if (cmp == 0) {
1795 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001796 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001798 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001799 neg = a->neg;
1800 minuend = a;
1801 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001802 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 neg = b->neg;
1804 if (sub) neg = !neg;
1805 minuend = b;
1806 subtrahend = a;
1807 }
1808
1809 bc_num_copy(c, minuend);
1810 c->neg = neg;
1811
1812 if (c->rdx < subtrahend->rdx) {
1813 bc_num_extend(c, subtrahend->rdx - c->rdx);
1814 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001815 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 start = c->rdx - subtrahend->rdx;
1817
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001818 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001819
1820 bc_num_clean(c);
1821
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001822 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001823}
1824
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001825static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001826 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001827#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001828{
1829 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001830 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001832 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001833
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 if (a->len == 0 || b->len == 0) {
1835 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001836 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001838 aone = BC_NUM_ONE(a);
1839 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001840 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 }
1843
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001844 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1845 || a->len < BC_NUM_KARATSUBA_LEN
1846 || b->len < BC_NUM_KARATSUBA_LEN
1847 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001848 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001849
Gavin Howard01055ba2018-11-03 11:00:21 -06001850 bc_num_expand(c, a->len + b->len + 1);
1851
1852 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001853 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001854
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001855 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001856 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001857 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001858 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001859 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001860 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001861 carry = in / 10;
1862 c->num[i + j] = (BcDig)(in % 10);
1863 }
1864
1865 c->num[i + j] += (BcDig) carry;
1866 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001867
Denys Vlasenko14767602018-12-27 22:52:13 +01001868#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001869 // a=2^1000000
1870 // a*a <- without check below, this will not be interruptible
1871 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001872#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 }
1874
1875 c->len = len;
1876
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001877 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 }
1879
1880 bc_num_init(&l1, max);
1881 bc_num_init(&h1, max);
1882 bc_num_init(&l2, max);
1883 bc_num_init(&h2, max);
1884 bc_num_init(&m1, max);
1885 bc_num_init(&m2, max);
1886 bc_num_init(&z0, max);
1887 bc_num_init(&z1, max);
1888 bc_num_init(&z2, max);
1889 bc_num_init(&temp, max + max);
1890
1891 bc_num_split(a, max2, &l1, &h1);
1892 bc_num_split(b, max2, &l2, &h2);
1893
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001894 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001897 if (s) goto err;
1898
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001899 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001900 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001901 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001902 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001903 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 if (s) goto err;
1905
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001906 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001907 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001908 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 if (s) goto err;
1910
Denys Vlasenko29301232018-12-11 15:29:32 +01001911 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001912 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001913 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001914 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001915 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001917 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001918 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001919 bc_num_free(&temp);
1920 bc_num_free(&z2);
1921 bc_num_free(&z1);
1922 bc_num_free(&z0);
1923 bc_num_free(&m2);
1924 bc_num_free(&m1);
1925 bc_num_free(&h2);
1926 bc_num_free(&l2);
1927 bc_num_free(&h1);
1928 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001929 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001930}
1931
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001932static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001933{
1934 BcStatus s;
1935 BcNum cpa, cpb;
1936 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1937
1938 scale = BC_MAX(scale, a->rdx);
1939 scale = BC_MAX(scale, b->rdx);
1940 scale = BC_MIN(a->rdx + b->rdx, scale);
1941 maxrdx = BC_MAX(maxrdx, scale);
1942
1943 bc_num_init(&cpa, a->len);
1944 bc_num_init(&cpb, b->len);
1945
1946 bc_num_copy(&cpa, a);
1947 bc_num_copy(&cpb, b);
1948 cpa.neg = cpb.neg = false;
1949
Denys Vlasenko29301232018-12-11 15:29:32 +01001950 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001952 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001953 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001954 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001955 if (s) goto err;
1956
1957 maxrdx += scale;
1958 bc_num_expand(c, c->len + maxrdx);
1959
1960 if (c->len < maxrdx) {
1961 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1962 c->len += maxrdx;
1963 }
1964
1965 c->rdx = maxrdx;
1966 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001967 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 bc_num_free(&cpb);
1969 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001970 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001971}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001972#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001973
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001974static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001975{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001976 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001977 size_t len, end, i;
1978 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001979
1980 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001981 RETURN_STATUS(bc_error("divide by zero"));
1982 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001983 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001984 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001985 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001986 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001987 bc_num_copy(c, a);
1988 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001989 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001990 }
1991
1992 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1993 bc_num_copy(&cp, a);
1994 len = b->len;
1995
1996 if (len > cp.len) {
1997 bc_num_expand(&cp, len + 2);
1998 bc_num_extend(&cp, len - cp.len);
1999 }
2000
2001 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2002 cp.rdx -= b->rdx;
2003 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2004
2005 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002006 for (;;) {
2007 if (len == 0) break;
2008 len--;
2009 if (b->num[len] != 0)
2010 break;
2011 }
2012 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 }
2014
2015 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2016
2017 // We want an extra zero in front to make things simpler.
2018 cp.num[cp.len++] = 0;
2019 end = cp.len - len;
2020
2021 bc_num_expand(c, cp.len);
2022
2023 bc_num_zero(c);
2024 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2025 c->rdx = cp.rdx;
2026 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002027
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002028 s = BC_STATUS_SUCCESS;
2029 for (i = end - 1; i < end; --i) {
2030 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002031 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002032 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2033 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002034 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01002035#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01002036 // a=2^100000
2037 // scale=40000
2038 // 1/a <- without check below, this will not be interruptible
2039 if (G_interrupt) {
2040 s = BC_STATUS_FAILURE;
2041 break;
2042 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002043#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002044 }
2045
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002046 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002047 bc_num_free(&cp);
2048
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002049 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002050}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002051#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002052
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002053static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002054 BcNum *restrict d, size_t scale, size_t ts)
2055{
2056 BcStatus s;
2057 BcNum temp;
2058 bool neg;
2059
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002060 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002061 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002062
2063 if (a->len == 0) {
2064 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002065 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002066 }
2067
2068 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002069 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002070 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002071
2072 if (scale != 0) scale = ts;
2073
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002074 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002075 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002076 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002077 if (s) goto err;
2078
2079 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2080
2081 neg = d->neg;
2082 bc_num_retireMul(d, ts, a->neg, b->neg);
2083 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002084 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002085 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002086 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002087}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002088#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002089
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002090static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002091{
2092 BcStatus s;
2093 BcNum c1;
2094 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2095
2096 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002097 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002098 bc_num_free(&c1);
2099
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002100 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002101}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002102#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002103
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002104static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002105{
2106 BcStatus s = BC_STATUS_SUCCESS;
2107 BcNum copy;
2108 unsigned long pow;
2109 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002110 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002111
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002112 // GNU bc does not allow 2^2.0 - we do
2113 for (i = 0; i < b->rdx; i++)
2114 if (b->num[i] != 0)
2115 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002116
2117 if (b->len == 0) {
2118 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002119 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002121 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002123 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002124 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002125 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002126 if (!b->neg)
2127 bc_num_copy(c, a);
2128 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002129 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002130 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002131 }
2132
2133 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002134 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002135 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002136 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002137
2138 bc_num_init(&copy, a->len);
2139 bc_num_copy(&copy, a);
2140
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002141 if (!neg) {
2142 if (a->rdx > scale)
2143 scale = a->rdx;
2144 if (a->rdx * pow < scale)
2145 scale = a->rdx * pow;
2146 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002147
Gavin Howard01055ba2018-11-03 11:00:21 -06002148
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002149 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002150 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002151 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002153 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002154 //if (G_interrupt) {
2155 // s = BC_STATUS_FAILURE;
2156 // goto err;
2157 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002158 }
2159
Gavin Howard01055ba2018-11-03 11:00:21 -06002160 bc_num_copy(c, &copy);
2161
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002162 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002163 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002164 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002165 if (s) goto err;
2166
2167 if (pow & 1) {
2168 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002169 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002170 if (s) goto err;
2171 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002172 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002173 //if (G_interrupt) {
2174 // s = BC_STATUS_FAILURE;
2175 // goto err;
2176 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002177 }
2178
2179 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002180 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002181 if (s) goto err;
2182 }
2183
Gavin Howard01055ba2018-11-03 11:00:21 -06002184 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2185
2186 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002187 for (i = 0; i < c->len; ++i)
2188 if (c->num[i] != 0)
2189 goto skip;
2190 bc_num_setToZero(c, scale);
2191 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002192
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002193 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002194 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002195 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002196}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002197#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002198
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002199static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002200{
2201 BcStatus s;
2202 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002203 BcDig half_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002204 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2205 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2206
2207 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2208 bc_num_expand(b, req);
2209
2210 if (a->len == 0) {
2211 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002212 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002213 }
2214 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002215 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002216 }
2217 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002218 bc_num_one(b);
2219 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002220 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002221 }
2222
2223 scale = BC_MAX(scale, a->rdx) + 1;
2224 len = a->len + scale;
2225
2226 bc_num_init(&num1, len);
2227 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002228
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002229 half.cap = ARRAY_SIZE(half_digs);
2230 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002231 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002232 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002233 half.rdx = 1;
2234
2235 bc_num_init(&f, len);
2236 bc_num_init(&fprime, len);
2237
2238 x0 = &num1;
2239 x1 = &num2;
2240
2241 bc_num_one(x0);
2242 pow = BC_NUM_INT(a);
2243
2244 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002245 if (pow & 1)
2246 x0->num[0] = 2;
2247 else
2248 x0->num[0] = 6;
2249
2250 pow -= 2 - (pow & 1);
2251
2252 bc_num_extend(x0, pow);
2253
2254 // Make sure to move the radix back.
2255 x0->rdx -= pow;
2256 }
2257
2258 x0->rdx = digs = digs1 = 0;
2259 resrdx = scale + 2;
2260 len = BC_NUM_INT(x0) + resrdx - 1;
2261
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002262 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002263 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002264 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002265 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002266 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002267 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002268 if (s) goto err;
2269
2270 cmp = bc_num_cmp(x1, x0);
2271 digs = x1->len - (unsigned long long) llabs(cmp);
2272
2273 if (cmp == cmp2 && digs == digs1)
2274 times += 1;
2275 else
2276 times = 0;
2277
2278 resrdx += times > 4;
2279
2280 cmp2 = cmp1;
2281 cmp1 = cmp;
2282 digs1 = digs;
2283
2284 temp = x0;
2285 x0 = x1;
2286 x1 = temp;
2287 }
2288
Gavin Howard01055ba2018-11-03 11:00:21 -06002289 bc_num_copy(b, x0);
2290 scale -= 1;
2291 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002292 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002293 bc_num_free(&fprime);
2294 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002295 bc_num_free(&num2);
2296 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002297 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002298}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002299#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002300
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002301static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002302 size_t scale)
2303{
2304 BcStatus s;
2305 BcNum num2, *ptr_a;
2306 bool init = false;
2307 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2308
2309 if (c == a) {
2310 memcpy(&num2, c, sizeof(BcNum));
2311 ptr_a = &num2;
2312 bc_num_init(c, len);
2313 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002314 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002315 ptr_a = a;
2316 bc_num_expand(c, len);
2317 }
2318
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002319 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002320
2321 if (init) bc_num_free(&num2);
2322
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002323 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002324}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002325#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002326
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002327#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002328static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002329{
2330 BcStatus s;
2331 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002332 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002333
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002334 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002335 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002336 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002337 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002338 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002339 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002340
2341 bc_num_expand(d, c->len);
2342 bc_num_init(&base, c->len);
2343 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 bc_num_init(&temp, b->len);
2345
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002346 two.cap = ARRAY_SIZE(two_digs);
2347 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002348 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002349 two_digs[0] = 2;
2350
Gavin Howard01055ba2018-11-03 11:00:21 -06002351 bc_num_one(d);
2352
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002353 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002354 if (s) goto err;
2355 bc_num_copy(&exp, b);
2356
2357 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002358 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359 if (s) goto err;
2360
2361 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002362 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002363 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002364 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002365 if (s) goto err;
2366 }
2367
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002368 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002369 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002370 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002371 if (s) goto err;
2372 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002373 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002374 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002375 bc_num_free(&exp);
2376 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002377 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002378}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002379#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002380#endif // ENABLE_DC
2381
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002382static FAST_FUNC void bc_string_free(void *string)
2383{
2384 free(*(char**)string);
2385}
2386
Gavin Howard01055ba2018-11-03 11:00:21 -06002387static void bc_func_init(BcFunc *f)
2388{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002389 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002390 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2391 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002392 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2393 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002394 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002395}
2396
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002397static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002398{
2399 BcFunc *f = (BcFunc *) func;
2400 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002401 IF_BC(bc_vec_free(&f->labels);)
2402 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002403 IF_BC(bc_vec_free(&f->strs);)
2404 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002405}
2406
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002407static void bc_array_expand(BcVec *a, size_t len);
2408
Gavin Howard01055ba2018-11-03 11:00:21 -06002409static void bc_array_init(BcVec *a, bool nums)
2410{
2411 if (nums)
2412 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2413 else
2414 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2415 bc_array_expand(a, 1);
2416}
2417
Gavin Howard01055ba2018-11-03 11:00:21 -06002418static void bc_array_expand(BcVec *a, size_t len)
2419{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002420 if (a->dtor == bc_num_free
2421 // && a->size == sizeof(BcNum) - always true
2422 ) {
2423 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002424 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002425 bc_num_init_DEF_SIZE(&n);
2426 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002427 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002428 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002429 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002430 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002431 bc_array_init(&v, true);
2432 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002433 }
2434 }
2435}
2436
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002437static void bc_array_copy(BcVec *d, const BcVec *s)
2438{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002439 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002440 size_t i;
2441
2442 bc_vec_pop_all(d);
2443 bc_vec_expand(d, s->cap);
2444 d->len = s->len;
2445
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002446 dnum = (void*)d->v;
2447 snum = (void*)s->v;
2448 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002449 bc_num_init(dnum, snum->len);
2450 bc_num_copy(dnum, snum);
2451 }
2452}
2453
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002454#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002455static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002456{
2457 d->t = src->t;
2458
2459 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002460 case XC_RESULT_TEMP:
2461 case XC_RESULT_IBASE:
2462 case XC_RESULT_SCALE:
2463 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002464 bc_num_init(&d->d.n, src->d.n.len);
2465 bc_num_copy(&d->d.n, &src->d.n);
2466 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002467 case XC_RESULT_VAR:
2468 case XC_RESULT_ARRAY:
2469 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002470 d->d.id.name = xstrdup(src->d.id.name);
2471 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002472 case XC_RESULT_CONSTANT:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002473 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002474 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2475 break;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002476 default: // placate compiler
2477 // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen
2478 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002479 }
2480}
2481#endif // ENABLE_DC
2482
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002483static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002484{
2485 BcResult *r = (BcResult *) result;
2486
2487 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002488 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002489 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002490 case XC_RESULT_IBASE:
2491 case XC_RESULT_SCALE:
2492 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002493 bc_num_free(&r->d.n);
2494 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002495 case XC_RESULT_VAR:
2496 case XC_RESULT_ARRAY:
2497 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002498 free(r->d.id.name);
2499 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002500 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002501 // Do nothing.
2502 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002503 }
2504}
2505
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002506static int bad_input_byte(char c)
2507{
2508 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2509 || c > 0x7e
2510 ) {
2511 bc_error_fmt("illegal character 0x%02x", c);
2512 return 1;
2513 }
2514 return 0;
2515}
2516
Denys Vlasenko10bde142018-12-27 18:23:58 +01002517static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002518{
2519 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002520 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002521 fflush_and_check();
2522
Denys Vlasenko14767602018-12-27 22:52:13 +01002523#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002524 if (G_interrupt) { // ^C was pressed
Denys Vlasenko5663a172020-06-24 13:43:11 +02002525# if ENABLE_FEATURE_EDITING
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002526 intr:
Denys Vlasenko5663a172020-06-24 13:43:11 +02002527# endif
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002528 if (fp != stdin) {
2529 // ^C while running a script (bc SCRIPT): die.
2530 // We do not return to interactive prompt:
2531 // user might be running us from a shell,
2532 // and SCRIPT might be intended to terminate
2533 // (e.g. contain a "halt" stmt).
2534 // ^C dropping user into a bc prompt instead of
2535 // the shell would be unexpected.
2536 xfunc_die();
2537 }
2538 // ^C while interactive input
2539 G_interrupt = 0;
2540 // GNU bc says "interrupted execution."
2541 // GNU dc says "Interrupt!"
2542 fputs("\ninterrupted execution\n", stderr);
2543 }
2544
2545# if ENABLE_FEATURE_EDITING
2546 if (G_ttyin && fp == stdin) {
2547 int n, i;
2548# define line_buf bb_common_bufsiz1
2549 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2550 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2551 if (n == 0) // ^C
2552 goto intr;
2553 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2554 return;
2555 }
2556 i = 0;
2557 for (;;) {
2558 char c = line_buf[i++];
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002559 if (c == '\0') break;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002560 if (bad_input_byte(c)) goto again;
2561 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002562 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002563# undef line_buf
2564 } else
2565# endif
2566#endif
2567 {
2568 int c;
2569 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002570
2571 do {
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002572 get_char:
Denys Vlasenko14767602018-12-27 22:52:13 +01002573#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002574 if (G_interrupt) {
2575 // ^C was pressed: ignore entire line, get another one
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002576 goto again;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002577 }
2578#endif
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002579 c = fgetc(fp);
2580 if (c == '\0')
2581 goto get_char;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002582 if (c == EOF) {
2583 if (ferror(fp))
James Byrne69374872019-07-02 11:35:03 +02002584 bb_simple_perror_msg_and_die("input error");
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002585 // Note: EOF does not append '\n'
2586 break;
2587 }
2588 bad_chars |= bad_input_byte(c);
2589 bc_vec_pushByte(vec, (char)c);
2590 } while (c != '\n');
2591
2592 if (bad_chars) {
2593 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002594 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002595 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002596 goto again;
2597 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002598 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002599 }
2600 bc_vec_pushZeroByte(vec);
2601 }
2602}
2603
2604//
2605// Parsing routines
2606//
2607
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002608// "Input numbers may contain the characters 0-9 and A-Z.
2609// (Note: They must be capitals. Lower case letters are variable names.)
2610// Single digit numbers always have the value of the digit regardless of
2611// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2612// all input digits greater or equal to ibase to the value of ibase-1.
2613// This makes the number ZZZ always be the largest 3 digit number of the
2614// input base."
2615static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002616{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002617 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002618 for (;;) {
2619 BcDig c = *val++;
2620 if (c == '\0')
2621 break;
2622 if (c == '.') {
2623 if (radix) return false;
2624 radix = true;
2625 continue;
2626 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002627 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002628 return false;
2629 }
2630 return true;
2631}
2632
2633// Note: n is already "bc_num_zero()"ed,
2634// leading zeroes in "val" are removed
2635static void bc_num_parseDecimal(BcNum *n, const char *val)
2636{
2637 size_t len, i;
2638 const char *ptr;
2639
2640 len = strlen(val);
2641 if (len == 0)
2642 return;
2643
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002644 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002645
2646 ptr = strchr(val, '.');
2647
2648 n->rdx = 0;
2649 if (ptr != NULL)
2650 n->rdx = (size_t)((val + len) - (ptr + 1));
2651
2652 for (i = 0; val[i]; ++i) {
2653 if (val[i] != '0' && val[i] != '.') {
2654 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002655 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002656 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002657 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002658 if (c > 9) { // A-Z => 10-36
2659 n->len = 2;
2660 c -= ('A' - '9' - 1);
2661 n->num[1] = c/10;
2662 c = c%10;
2663 }
2664 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002665 break;
2666 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002667 i = len - 1;
2668 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002669 char c = val[i] - '0';
2670 if (c > 9) // A-Z => 9
2671 c = 9;
2672 n->num[n->len] = c;
2673 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002674 skip_dot:
2675 if (i == 0) break;
2676 if (val[--i] == '.') goto skip_dot;
2677 }
2678 break;
2679 }
2680 }
2681 // if for() exits without hitting if(), the value is entirely zero
2682}
2683
2684// Note: n is already "bc_num_zero()"ed,
2685// leading zeroes in "val" are removed
2686static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2687{
2688 BcStatus s;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002689 BcNum mult, result;
2690 BcNum temp;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002691 BcNum base;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002692 BcDig temp_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002693 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002694 size_t digits;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002695
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002696 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002697
2698 temp.cap = ARRAY_SIZE(temp_digs);
2699 temp.num = temp_digs;
2700
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002701 base.cap = ARRAY_SIZE(base_digs);
2702 base.num = base_digs;
2703 bc_num_ulong2num(&base, base_t);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002704 base_t--;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002705
2706 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002707 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002708 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002709
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002710 c = *val++;
2711 if (c == '\0') goto int_err;
2712 if (c == '.') break;
2713
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002714 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2715 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002716
2717 s = zbc_num_mul(n, &base, &mult, 0);
2718 if (s) goto int_err;
2719 bc_num_ulong2num(&temp, v);
2720 s = zbc_num_add(&mult, &temp, n, 0);
2721 if (s) goto int_err;
2722 }
2723
2724 bc_num_init(&result, base.len);
2725 //bc_num_zero(&result); - already is
2726 bc_num_one(&mult);
2727
2728 digits = 0;
2729 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002730 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002731 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002732
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002733 c = *val++;
2734 if (c == '\0') break;
2735 digits++;
2736
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002737 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2738 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002739
2740 s = zbc_num_mul(&result, &base, &result, 0);
2741 if (s) goto err;
2742 bc_num_ulong2num(&temp, v);
2743 s = zbc_num_add(&result, &temp, &result, 0);
2744 if (s) goto err;
2745 s = zbc_num_mul(&mult, &base, &mult, 0);
2746 if (s) goto err;
2747 }
2748
2749 s = zbc_num_div(&result, &mult, &result, digits);
2750 if (s) goto err;
2751 s = zbc_num_add(n, &result, n, digits);
2752 if (s) goto err;
2753
2754 if (n->len != 0) {
2755 if (n->rdx < digits)
2756 bc_num_extend(n, digits - n->rdx);
2757 } else
2758 bc_num_zero(n);
2759 err:
2760 bc_num_free(&result);
2761 int_err:
2762 bc_num_free(&mult);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002763}
2764
Denys Vlasenko10bde142018-12-27 18:23:58 +01002765static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002766{
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002767 size_t i;
2768
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002769 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002770 RETURN_STATUS(bc_error("bad number string"));
2771
2772 bc_num_zero(n);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002773 while (*val == '0')
2774 val++;
2775 for (i = 0; ; ++i) {
2776 if (val[i] == '\0')
2777 RETURN_STATUS(BC_STATUS_SUCCESS);
2778 if (val[i] != '.' && val[i] != '0')
2779 break;
2780 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002781
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002782 if (base_t == 10 || val[1] == '\0')
2783 // Decimal, or single-digit number
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002784 bc_num_parseDecimal(n, val);
2785 else
2786 bc_num_parseBase(n, val, base_t);
2787
2788 RETURN_STATUS(BC_STATUS_SUCCESS);
2789}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002790#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002791
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002792// p->lex_inbuf points to the current string to be parsed.
2793// if p->lex_inbuf points to '\0', it's either EOF or it points after
2794// last processed line's terminating '\n' (and more reading needs to be done
2795// to get next character).
2796//
2797// If you are in a situation where that is a possibility, call peek_inbuf().
2798// If necessary, it performs more reading and changes p->lex_inbuf,
2799// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2800// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2801// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2802//
2803// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2804// it returns current char and advances the pointer (if not EOF).
2805// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2806//
2807// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2808// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2809// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002810//
2811// bc cases to test interactively:
2812// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2813// 1 #comment/* - prints "1<newline>" at once
2814// 1 #comment" - prints "1<newline>" at once
2815// 1\#comment - error at once (\ is not a line continuation)
2816// 1 + /*"*/2 - prints "3<newline>" at once
2817// 1 + /*#*/2 - prints "3<newline>" at once
2818// "str\" - prints "str\" at once
2819// "str#" - prints "str#" at once
2820// "str/*" - prints "str/*" at once
2821// "str#\ - waits for second line
2822// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002823static char peek_inbuf(void)
2824{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002825 if (*G.prs.lex_inbuf == '\0'
2826 && G.prs.lex_input_fp
2827 ) {
2828 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2829 G.prs.lex_inbuf = G.input_buffer.v;
2830 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2831 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002832 }
2833 return *G.prs.lex_inbuf;
2834}
2835static char eat_inbuf(void)
2836{
2837 char c = peek_inbuf();
2838 if (c) G.prs.lex_inbuf++;
2839 return c;
2840}
2841
Denys Vlasenko10bde142018-12-27 18:23:58 +01002842static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002843{
2844 BcParse *p = &G.prs;
2845 char c;
2846
2847 // Try: echo -n '#foo' | bc
2848 p->lex = XC_LEX_WHITESPACE;
2849
Denys Vlasenko2747f612018-12-31 18:48:10 +01002850 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002851 // '\0' which isn't the EOF can only be seen after '\n'.
2852 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2853 p->lex_inbuf++;
2854}
2855
Denys Vlasenko10bde142018-12-27 18:23:58 +01002856static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002857{
2858 BcParse *p = &G.prs;
2859
2860 p->lex = XC_LEX_WHITESPACE;
2861 for (;;) {
2862 // We depend here on input being done in whole lines:
2863 // '\0' which isn't the EOF can only be seen after '\n'.
2864 char c = *p->lex_inbuf;
2865 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2866 break;
2867 if (!isspace(c))
2868 break;
2869 p->lex_inbuf++;
2870 }
2871}
2872
Denys Vlasenko10bde142018-12-27 18:23:58 +01002873static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002874{
2875 BcParse *p = &G.prs;
2876 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002877 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002878
2879 bc_vec_pop_all(&p->lex_strnumbuf);
2880 bc_vec_pushByte(&p->lex_strnumbuf, last);
2881
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002882// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002883// (Note: They must be capitals. Lower case letters are variable names.)
2884// Single digit numbers always have the value of the digit regardless of
2885// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2886// all input digits greater or equal to ibase to the value of ibase-1.
2887// This makes the number ZZZ always be the largest 3 digit number of the
2888// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002889// dc only allows A-F, the rules about single-char and multi-char are the same.
2890 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002891 pt = (last == '.');
2892 p->lex = XC_LEX_NUMBER;
2893 for (;;) {
2894 // We depend here on input being done in whole lines:
2895 // '\0' which isn't the EOF can only be seen after '\n'.
2896 char c = *p->lex_inbuf;
2897 check_c:
2898 if (c == '\0')
2899 break;
2900 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2901 p->lex_inbuf += 2;
2902 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01002903 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002904 c = peek_inbuf(); // force next line to be read
2905 goto check_c;
2906 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002907 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002908 if (c != '.') break;
2909 // if '.' was already seen, stop on second one:
2910 if (pt) break;
2911 pt = true;
2912 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002913 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002914 last = c;
2915 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2916 p->lex_inbuf++;
2917 }
2918 if (last == '.') // remove trailing '.' if any
2919 bc_vec_pop(&p->lex_strnumbuf);
2920 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2921
2922 G.err_line = G.prs.lex_line;
2923 RETURN_STATUS(BC_STATUS_SUCCESS);
2924}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002925#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002926
Denys Vlasenko10bde142018-12-27 18:23:58 +01002927static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002928{
2929 BcParse *p = &G.prs;
2930 size_t i;
2931 const char *buf;
2932
2933 p->lex = XC_LEX_NAME;
2934
2935 // Since names can't cross lines with \<newline>,
2936 // we depend on the fact that whole line is in the buffer
2937 i = 0;
2938 buf = p->lex_inbuf - 1;
2939 for (;;) {
2940 char c = buf[i];
2941 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2942 i++;
2943 }
2944
2945#if 0 // We do not protect against people with gigabyte-long names
2946 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2947 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2948 if (i > BC_MAX_STRING)
2949 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2950 }
2951#endif
2952 bc_vec_string(&p->lex_strnumbuf, i, buf);
2953
2954 // Increment the index. We minus 1 because it has already been incremented.
2955 p->lex_inbuf += i - 1;
2956
2957 //return BC_STATUS_SUCCESS;
2958}
2959
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002960IF_BC(static BC_STATUS zbc_lex_token(void);)
2961IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002962#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2963#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002964
Denys Vlasenko10bde142018-12-27 18:23:58 +01002965static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002966{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002967 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002968 BcStatus s;
2969
Denys Vlasenko22314682019-01-01 21:50:14 +01002970 G.err_line = p->lex_line;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002971 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002972//why?
2973// if (p->lex_last == XC_LEX_EOF)
2974// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002975
2976 // Loop until failure or we don't have whitespace. This
2977 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002978 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002979 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002980 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002981 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002982 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002983 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002984 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002985 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002986 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002987 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002988 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2989 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002990 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002991 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002992 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002993 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002994 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002995 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002996 } while (!s && p->lex == XC_LEX_WHITESPACE);
2997 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002998
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002999 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003000}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003001#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003002
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003003#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003004static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003005{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003006 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01003007 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01003008 RETURN_STATUS(BC_STATUS_SUCCESS);
3009}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003010#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003011
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003012static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003013{
3014 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003015 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003016 if (s) RETURN_STATUS(s);
3017 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003018 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003019 RETURN_STATUS(s);
3020}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003021#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003022
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003023static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003024{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003025 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003026 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003027 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003028 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003029
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003030 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3031 const char *keyword8 = bc_lex_kws[i].name8;
3032 unsigned j = 0;
3033 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3034 j++;
3035 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003036 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003037 if (keyword8[j] != '\0')
3038 continue;
3039 match:
3040 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003041 if (isalnum(buf[j]) || buf[j]=='_')
3042 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003043 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003044 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003045 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3046 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003047 }
3048
3049 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003050 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003051 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003052 }
3053
Denys Vlasenko10bde142018-12-27 18:23:58 +01003054 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003055 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003056
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003057 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003058 // Prevent this:
3059 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003060 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003061 // '
3062 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003063 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003064 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003065
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003066 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003067}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003068#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003069
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003070static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003071{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003072 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003073
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003074 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003075 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003076 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003077 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003078 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003079 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003080 }
3081 if (c == '"')
3082 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003083 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003084 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003085 dbg_lex("++p->lex_line=%zd", p->lex_line);
3086 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003087 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3088 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003089 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003090 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3091 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003092
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003093 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003094 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003095}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003096#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003097
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003098static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003099{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003100 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003101 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003102 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003103 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003104 with_and_without >>= 8; // store "with" value
3105 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003106 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003107}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003108#define parse_lex_by_checking_eq_sign(with, without) \
3109 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003110
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003111static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003112{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003113 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003114
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003115 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003116 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003117 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003118 char c;
3119
3120 p->lex_inbuf++;
3121 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003122 check_star:
3123 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003124 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003125 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003126 if (c == '/')
3127 break;
3128 goto check_star;
3129 }
3130 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003131 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 }
Denys Vlasenko22314682019-01-01 21:50:14 +01003133 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003134 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003135 dbg_lex("++p->lex_line=%zd", p->lex_line);
3136 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003138 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003139
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003140 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003141 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003142}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003143#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003144
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003145#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003146static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003147{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003148 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003149 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003150 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003151 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003152
3153 // This is the workhorse of the lexer.
3154 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003155// case '\0': // probably never reached
3156// p->lex_inbuf--;
3157// p->lex = XC_LEX_EOF;
3158// break;
3159 case '\n':
3160 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003161 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003162 p->lex = XC_LEX_NLINE;
3163 break;
3164 case '\t':
3165 case '\v':
3166 case '\f':
3167 case '\r':
3168 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003169 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003170 break;
3171 case '!':
3172 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3173 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3174 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003175 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003176 }
3177 break;
3178 case '"':
3179 s = zbc_lex_string();
3180 break;
3181 case '#':
3182 s = zbc_POSIX_does_not_allow("'#' script comments");
3183 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003184 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003185 break;
3186 case '%':
3187 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3188 break;
3189 case '&':
3190 c2 = *p->lex_inbuf;
3191 if (c2 == '&') {
3192 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3193 if (s) RETURN_STATUS(s);
3194 p->lex_inbuf++;
3195 p->lex = BC_LEX_OP_BOOL_AND;
3196 } else {
3197 p->lex = XC_LEX_INVALID;
3198 s = bc_error_bad_character('&');
3199 }
3200 break;
3201 case '(':
3202 case ')':
3203 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3204 break;
3205 case '*':
3206 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3207 break;
3208 case '+':
3209 c2 = *p->lex_inbuf;
3210 if (c2 == '+') {
3211 p->lex_inbuf++;
3212 p->lex = BC_LEX_OP_INC;
3213 } else
3214 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3215 break;
3216 case ',':
3217 p->lex = BC_LEX_COMMA;
3218 break;
3219 case '-':
3220 c2 = *p->lex_inbuf;
3221 if (c2 == '-') {
3222 p->lex_inbuf++;
3223 p->lex = BC_LEX_OP_DEC;
3224 } else
3225 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3226 break;
3227 case '.':
3228 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003229 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003230 else {
3231 p->lex = BC_LEX_KEY_LAST;
3232 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3233 }
3234 break;
3235 case '/':
3236 c2 = *p->lex_inbuf;
3237 if (c2 == '*')
3238 s = zbc_lex_comment();
3239 else
3240 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3241 break;
3242 case '0':
3243 case '1':
3244 case '2':
3245 case '3':
3246 case '4':
3247 case '5':
3248 case '6':
3249 case '7':
3250 case '8':
3251 case '9':
3252 case 'A':
3253 case 'B':
3254 case 'C':
3255 case 'D':
3256 case 'E':
3257 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003258 case 'G':
3259 case 'H':
3260 case 'I':
3261 case 'J':
3262 case 'K':
3263 case 'L':
3264 case 'M':
3265 case 'N':
3266 case 'O':
3267 case 'P':
3268 case 'Q':
3269 case 'R':
3270 case 'S':
3271 case 'T':
3272 case 'U':
3273 case 'V':
3274 case 'W':
3275 case 'X':
3276 case 'Y':
3277 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003278 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003279 break;
3280 case ';':
3281 p->lex = BC_LEX_SCOLON;
3282 break;
3283 case '<':
3284 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3285 break;
3286 case '=':
3287 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3288 break;
3289 case '>':
3290 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3291 break;
3292 case '[':
3293 case ']':
3294 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3295 break;
3296 case '\\':
3297 if (*p->lex_inbuf == '\n') {
3298 p->lex = XC_LEX_WHITESPACE;
3299 p->lex_inbuf++;
3300 } else
3301 s = bc_error_bad_character(c);
3302 break;
3303 case '^':
3304 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3305 break;
3306 case 'a':
3307 case 'b':
3308 case 'c':
3309 case 'd':
3310 case 'e':
3311 case 'f':
3312 case 'g':
3313 case 'h':
3314 case 'i':
3315 case 'j':
3316 case 'k':
3317 case 'l':
3318 case 'm':
3319 case 'n':
3320 case 'o':
3321 case 'p':
3322 case 'q':
3323 case 'r':
3324 case 's':
3325 case 't':
3326 case 'u':
3327 case 'v':
3328 case 'w':
3329 case 'x':
3330 case 'y':
3331 case 'z':
3332 s = zbc_lex_identifier();
3333 break;
3334 case '{':
3335 case '}':
3336 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3337 break;
3338 case '|':
3339 c2 = *p->lex_inbuf;
3340 if (c2 == '|') {
3341 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3342 if (s) RETURN_STATUS(s);
3343 p->lex_inbuf++;
3344 p->lex = BC_LEX_OP_BOOL_OR;
3345 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003346 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003347 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003348 }
3349 break;
3350 default:
3351 p->lex = XC_LEX_INVALID;
3352 s = bc_error_bad_character(c);
3353 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003354 }
3355
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003356 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003357}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003358#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003359#endif // ENABLE_BC
3360
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003361#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003362static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003363{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003364 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003365 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003366 xc_lex_whitespace(); // eats whitespace (but not newline)
3367 p->lex_inbuf++; // xc_lex_name() expects this
3368 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003369 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003370 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003371 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003372 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3373 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003374 }
3375
Denys Vlasenko29301232018-12-11 15:29:32 +01003376 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003377}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003378#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003379
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003380static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003381{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003382 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003383 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003384
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003385 p->lex = XC_LEX_STR;
3386 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003387
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003388 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003389 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003390 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003391 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003392 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003393 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003394 if (c == '[') depth++;
3395 if (c == ']')
3396 if (--depth == 0)
3397 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003398 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003399 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003400 dbg_lex("++p->lex_line=%zd", p->lex_line);
3401 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003402 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3403 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003404 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003405 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003406 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003407
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003408 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003409 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003410}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003411#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003412
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003413#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003414static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003415{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003416 static const //BcLexType - should be this type, but narrower type saves size:
3417 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003418 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003419 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 +01003420 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3421 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003422 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003423 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003424
Denys Vlasenko10bde142018-12-27 18:23:58 +01003425 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003426 BcStatus s;
3427 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003428 size_t i;
3429
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003430 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003431 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003432 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003433 }
3434
Denys Vlasenko81293c82018-12-24 01:53:55 +01003435 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003436 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003437 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003438 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003439 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003440 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003441 }
3442
3443 // This is the workhorse of the lexer.
3444 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003445// case '\0': // probably never reached
3446// p->lex = XC_LEX_EOF;
3447// break;
3448 case '\n':
3449 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3450 // (and "case '\n':" is not just empty here)
3451 // only to allow interactive dc have a way to exit
3452 // "parse" stage of "parse,execute" loop
3453 // on <enter>, not on _next_ token (which would mean
3454 // commands are not executed on pressing <enter>).
3455 // IOW: typing "1p<enter>" should print "1" _at once_,
3456 // not after some more input.
3457 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003458 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003459 p->lex = XC_LEX_NLINE;
3460 break;
3461 case '\t':
3462 case '\v':
3463 case '\f':
3464 case '\r':
3465 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003466 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003467 break;
3468 case '!':
3469 c2 = *p->lex_inbuf;
3470 if (c2 == '=')
3471 p->lex = XC_LEX_OP_REL_NE;
3472 else if (c2 == '<')
3473 p->lex = XC_LEX_OP_REL_LE;
3474 else if (c2 == '>')
3475 p->lex = XC_LEX_OP_REL_GE;
3476 else
3477 RETURN_STATUS(bc_error_bad_character(c));
3478 p->lex_inbuf++;
3479 break;
3480 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003481 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003482 break;
3483 case '.':
3484 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003485 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003486 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003487 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003488 break;
3489 case '0':
3490 case '1':
3491 case '2':
3492 case '3':
3493 case '4':
3494 case '5':
3495 case '6':
3496 case '7':
3497 case '8':
3498 case '9':
3499 case 'A':
3500 case 'B':
3501 case 'C':
3502 case 'D':
3503 case 'E':
3504 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003505 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003506 break;
3507 case '[':
3508 s = zdc_lex_string();
3509 break;
3510 default:
3511 p->lex = XC_LEX_INVALID;
3512 s = bc_error_bad_character(c);
3513 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 }
3515
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003516 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003517}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003518#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003519#endif // ENABLE_DC
3520
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003521static void xc_parse_push(unsigned i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003522{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003523 BcVec *code = &G.prs.func->code;
3524 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003525 bc_vec_pushByte(code, (uint8_t)i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003526}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003527
Denys Vlasenko10bde142018-12-27 18:23:58 +01003528static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003529{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003530#if 1
3531 BcVec *code = &G.prs.func->code;
3532 size_t pos = code->len;
3533 size_t len = strlen(name) + 1;
3534
3535 bc_vec_expand(code, pos + len);
3536 strcpy(code->v + pos, name);
3537 code->len = pos + len;
3538#else
3539 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003540 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003541 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003542 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003543#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003544}
3545
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003546// Indexes < 0xfc are encoded verbatim, else first byte is
3547// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3548// followed by that many bytes, lsb first.
3549// (The above describes 32-bit case).
3550#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3551
Denys Vlasenko53799502019-01-25 14:24:03 +01003552static void bc_vec_pushIndex(BcVec *v, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003553{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003554 size_t mask;
3555 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003556
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003557 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003558 if (idx < SMALL_INDEX_LIMIT) {
Denys Vlasenko53799502019-01-25 14:24:03 +01003559 bc_vec_pushByte(v, idx);
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003560 return;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003561 }
3562
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003563 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3564 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003565 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003566 if (idx & mask) break;
3567 mask >>= 8;
3568 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003569 }
3570 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003571
Denys Vlasenko53799502019-01-25 14:24:03 +01003572 bc_vec_pushByte(v, (SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003573
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003574 do {
Denys Vlasenko53799502019-01-25 14:24:03 +01003575 bc_vec_pushByte(v, (unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003576 idx >>= 8;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003577 } while (idx != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003578}
3579
Denys Vlasenko53799502019-01-25 14:24:03 +01003580static void xc_parse_pushIndex(size_t idx)
3581{
3582 bc_vec_pushIndex(&G.prs.func->code, idx);
3583}
3584
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003585static void xc_parse_pushInst_and_Index(unsigned inst, size_t idx)
3586{
3587 xc_parse_push(inst);
3588 xc_parse_pushIndex(idx);
3589}
3590
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003591#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003592static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003593{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003594 xc_parse_pushInst_and_Index(BC_INST_JUMP, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003595}
3596
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003597static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003598{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003599 xc_parse_pushInst_and_Index(BC_INST_JUMP_ZERO, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003600}
3601
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003602static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003603{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003604 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003605 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003606
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003607 xc_parse_pushInst_and_Index(XC_INST_STR, p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003608 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003609
Denys Vlasenko10bde142018-12-27 18:23:58 +01003610 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003611}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003612#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003613#endif
3614
Denys Vlasenko10bde142018-12-27 18:23:58 +01003615static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003616{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003617 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003618 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003619#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003620 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003621#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003622 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003623#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003624 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003625#endif
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003626 xc_parse_pushInst_and_Index(XC_INST_NUM, idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003627}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003628
Denys Vlasenko10bde142018-12-27 18:23:58 +01003629static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003630{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003631 G.prs.func = xc_program_func(G.prs.fidx);
3632 G.prs.lex_inbuf = text;
3633 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3634 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003635}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003636#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003637
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003638// Called when parsing or execution detects a failure,
3639// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003640static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003641{
3642 BcFunc *f;
3643 BcInstPtr *ip;
3644
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003645 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003646 bc_vec_pop_all(&G.prog.results);
3647
Denys Vlasenko10bde142018-12-27 18:23:58 +01003648 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003649 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003650 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003651}
3652
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003653// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003654// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003655static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003656{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003657 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003658 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003659 bc_func_free(p->func);
3660 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003661
Denys Vlasenko65e10462018-12-19 15:13:14 +01003662 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003663 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003664 }
3665
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003666 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003667 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003668
Denys Vlasenko503faf92018-12-20 16:24:18 +01003669 IF_BC(bc_vec_pop_all(&p->exits);)
3670 IF_BC(bc_vec_pop_all(&p->conds);)
3671 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003672
Denys Vlasenko10bde142018-12-27 18:23:58 +01003673 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003674}
3675
Denys Vlasenko10bde142018-12-27 18:23:58 +01003676static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003677{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003678 IF_BC(bc_vec_free(&G.prs.exits);)
3679 IF_BC(bc_vec_free(&G.prs.conds);)
3680 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003681 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003682}
3683
Denys Vlasenko10bde142018-12-27 18:23:58 +01003684static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003685{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003686 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003687 memset(p, 0, sizeof(BcParse));
3688
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003689 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003690 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3691 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3692 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003693
Denys Vlasenkofa210792018-12-19 19:35:40 +01003694 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003695 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003696}
3697
Denys Vlasenko10bde142018-12-27 18:23:58 +01003698static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003699{
Denys Vlasenko04715442018-12-21 00:10:26 +01003700 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003701 BcFunc f;
3702 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003703 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003704 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003705 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003706}
3707
3708#if ENABLE_BC
3709
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003710// Note: takes ownership of 'name' (must be malloced)
3711static size_t bc_program_addFunc(char *name)
3712{
3713 size_t idx;
3714 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003715 int inserted;
3716
3717 entry.name = name;
3718 entry.idx = G.prog.fns.len;
3719
3720 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3721 if (!inserted) free(name);
3722
3723 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3724 idx = entry_ptr->idx;
3725
3726 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003727 // There is already a function with this name.
3728 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003729 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003730 bc_func_free(func);
3731 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003732 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003733 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003734 }
3735
3736 return idx;
3737}
3738
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003739#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003740// We can calculate the conversion between tokens and exprs by subtracting the
3741// position of the first operator in the lex enum and adding the position of the
3742// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003743#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003744
Denys Vlasenko132d7c02019-01-08 19:29:35 +01003745static BC_STATUS zbc_parse_expr(uint8_t flags);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003746#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003747
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003748static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003749#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003750
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003751static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003752{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003753 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003754}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003755#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003756
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003757static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003758{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003759 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003760 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3761 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003762 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003763 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003764 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003765 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003766
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003767 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003768}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003769#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003770
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003771static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003772{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003773 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003774 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3775 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003776
3777 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003778 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003779 if (t == BC_LEX_LPAREN) break;
3780
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003781 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003782 if (l >= r && (l != r || !left)) break;
3783
Denys Vlasenko10bde142018-12-27 18:23:58 +01003784 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003785 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003786 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003787 }
3788
3789 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003790}
3791
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003792static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003793{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003794 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003795 BcLexType top;
3796
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003797 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003798 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 top = BC_PARSE_TOP_OP(p);
3800
3801 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003802 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003803
3804 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003805 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003806
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003807 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003808 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 top = BC_PARSE_TOP_OP(p);
3810 }
3811
3812 bc_vec_pop(&p->ops);
3813
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003814 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003815}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003816#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003817
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003818static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003819{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003820 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003821 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003822 size_t nparams;
3823
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003824 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003825 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3826
Denys Vlasenko10bde142018-12-27 18:23:58 +01003827 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003828 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003829
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003830 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003831 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003832 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003833 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003834 if (s) RETURN_STATUS(s);
3835 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003836 if (p->lex != BC_LEX_COMMA) {
3837 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003838 break;
3839 RETURN_STATUS(bc_error_bad_token());
3840 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003841 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003842 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 }
3844 }
3845
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003846 xc_parse_pushInst_and_Index(BC_INST_CALL, nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003847
Denys Vlasenko26819db2018-12-12 16:08:46 +01003848 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003849}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003850#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003851
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003852// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003853static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003854{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003855 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003856 BcStatus s;
3857 BcId entry, *entry_ptr;
3858 size_t idx;
3859
3860 entry.name = name;
3861
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003862 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003863 if (s) goto err;
3864
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003865 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003866 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003867 goto err;
3868 }
3869
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003870 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003871
3872 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003873 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003874 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003875 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003876 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003877 free(name);
3878
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003879 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003880 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003881
Denys Vlasenko10bde142018-12-27 18:23:58 +01003882 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003883 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003885 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003886}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003887#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003888
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003889static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003890{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003891 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003892 BcStatus s;
3893 char *name;
3894
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003895 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003896 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 if (s) goto err;
3898
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003899 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003900 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003901 if (s) goto err;
3902
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003903 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003904 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003905 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003906 goto err;
3907 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003908 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003909 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003910 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003911 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003912 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003913 if (s) goto err;
3914 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003915 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003916 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003917 xc_parse_push(*type);
3918 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003919 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003920 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003921 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003922 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003923 goto err;
3924 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003925 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003926 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003927 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003928 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003929 xc_parse_push(XC_INST_VAR);
3930 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003931 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003932 }
3933
Denys Vlasenko26819db2018-12-12 16:08:46 +01003934 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003935 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003936 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003937 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003938}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003939#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003940
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003941static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003942{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003943 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003944 BcStatus s;
3945
Denys Vlasenko10bde142018-12-27 18:23:58 +01003946 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003947 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003948 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003949
Denys Vlasenko10bde142018-12-27 18:23:58 +01003950 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003951 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003952 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
Denys Vlasenko10bde142018-12-27 18:23:58 +01003954 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003955
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003956 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003957}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003958#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003959
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003960static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003961{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003962 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003963 BcStatus s;
3964
Denys Vlasenko10bde142018-12-27 18:23:58 +01003965 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003966 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003967 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003968
3969 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3970
Denys Vlasenko10bde142018-12-27 18:23:58 +01003971 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003972 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003973
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003974 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003975 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003976
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003977 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003978
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003979 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003980 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003981
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003982 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003983}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003984#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003985
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003986static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003987{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003988 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003989 BcStatus s;
3990
Denys Vlasenko10bde142018-12-27 18:23:58 +01003991 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003992 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003993
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003994 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003995 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003996 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003997 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003998 }
3999
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004000 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06004001 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4002
Denys Vlasenko10bde142018-12-27 18:23:58 +01004003 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004004 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004005
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004006 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004007 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004008 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01004009 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004010 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
Denys Vlasenko10bde142018-12-27 18:23:58 +01004012 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004013}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004014#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004015
Denys Vlasenko22314682019-01-01 21:50:14 +01004016static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004017{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004018 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004019 BcStatus s;
4020 BcLexType type;
4021 char inst;
4022 BcInst etype = *prev;
4023
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004024 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4025 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4026 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004027 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004028 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004029 xc_parse_push(inst);
4030 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004031 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004032 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004033
Denys Vlasenko10bde142018-12-27 18:23:58 +01004034 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004035 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004036 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004037
4038 // Because we parse the next part of the expression
4039 // right here, we need to increment this.
Denys Vlasenko22314682019-01-01 21:50:14 +01004040 *nexs = *nexs + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004041
4042 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004043 case XC_LEX_NAME:
4044 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
4045 break;
4046 case BC_LEX_KEY_IBASE:
4047 case BC_LEX_KEY_LAST:
4048 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004049 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4050 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004051 break;
4052 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004053 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004054 if (s) RETURN_STATUS(s);
4055 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004056 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004057 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01004058 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004059 break;
4060 default:
4061 s = bc_error_bad_token();
4062 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 }
4064
Denys Vlasenko10bde142018-12-27 18:23:58 +01004065 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004066 }
4067
Denys Vlasenko26819db2018-12-12 16:08:46 +01004068 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004069}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004070#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004071
Denys Vlasenko22314682019-01-01 21:50:14 +01004072static int bc_parse_inst_isLeaf(BcInst p)
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004073{
4074 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4075 || p == BC_INST_INC_POST
4076 || p == BC_INST_DEC_POST
4077 ;
4078}
Denys Vlasenko22314682019-01-01 21:50:14 +01004079#define BC_PARSE_LEAF(prev, bin_last, rparen) \
4080 (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev)))
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004081
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004082static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
Denys Vlasenko22314682019-01-01 21:50:14 +01004083 bool rparen, bool bin_last, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004084{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004085 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004086 BcStatus s;
4087 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004088
Denys Vlasenko10bde142018-12-27 18:23:58 +01004089 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004090 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004091
Denys Vlasenko22314682019-01-01 21:50:14 +01004092 type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004093 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004094
4095 // We can just push onto the op stack because this is the largest
4096 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004097 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004098 bc_vec_push(&p->ops, &type);
4099 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004100 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004101
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004102 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004103}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004104#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004105
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004106static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004107{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004108 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004109 BcStatus s;
4110 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004111
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004112 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004113 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004114 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004115 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004116 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004117 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004118 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004119 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004120 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004121 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004122 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004123 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004125 }
4126
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004128}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004129#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004130
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004131static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004132{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004133 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004134 BcStatus s;
4135 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004136
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004137 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004138 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004139 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004140
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004141 t = p->lex;
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004142 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004143 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004144 else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004145//TODO: if (p->func->voidfunc) ERROR
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004146 s = zbc_parse_expr(0);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004147 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004148
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004149 if (t != BC_LEX_LPAREN // "return EXPR", no ()
4150 || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b"
4151 ) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004152 s = zbc_POSIX_requires("parentheses around return expressions");
4153 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004154 }
4155
Denys Vlasenko10bde142018-12-27 18:23:58 +01004156 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004157 }
4158
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004160 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004161}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004162#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004163
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004164static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004165{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004166 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004167 size_t *label = bc_vec_item(&p->func->labels, idx);
4168 *label = p->func->code.len;
4169}
4170
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004171static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004172{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004173 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004174 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004175 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004176
4177 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004178 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004179 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004180 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004181
Denys Vlasenko10bde142018-12-27 18:23:58 +01004182 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004183 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004184 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004185 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004186 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004187
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004188 // Encode "if zero, jump to ..."
4189 // Pushed value (destination of the jump) is uninitialized,
4190 // will be rewritten to be address of "end of if()" or of "else".
4191 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004192 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004193
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004194 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004195 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004196
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004197 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4198 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004199 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004200
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004201 // Encode "after then_stmt, jump to end of if()"
4202 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4203 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004204 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004205
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004206 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 +01004207 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004208
Denys Vlasenko15850832018-12-16 21:40:54 +01004209 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004210
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004211 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004212 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004213 }
4214
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004215 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 +01004216 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004217
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004218 dbg_lex_done("%s:%d done", __func__, __LINE__);
4219 RETURN_STATUS(s);
4220}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004221#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004222
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004223static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004224{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004225 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004226 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004227 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004228 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004229
Denys Vlasenko10bde142018-12-27 18:23:58 +01004230 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004231 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004232 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004233 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004234 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004235
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004236 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004237 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004238 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004239
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004240 bc_vec_push(&p->exits, &ip_idx);
4241 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004242
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004243 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004244 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004245 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004246
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004247 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004248
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004249 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004250 if (s) RETURN_STATUS(s);
4251
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004252 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004253 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004254
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004255 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004256 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004257
4258 bc_vec_pop(&p->exits);
4259 bc_vec_pop(&p->conds);
4260
4261 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004262}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004263#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004264
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004265static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004266{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004267 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004268 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004269 size_t cond_idx, exit_idx, body_idx, update_idx;
4270
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004271 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
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);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004274 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004275 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004276 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004277
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004278 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004279 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004280 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004281 if (s) RETURN_STATUS(s);
4282 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004283 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4284 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004285 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004286
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004287 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004288 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004289 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004290
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004291 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004292 update_idx = cond_idx + 1;
4293 body_idx = update_idx + 1;
4294 exit_idx = body_idx + 1;
4295
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004296 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004297 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004298 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004299 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004300 // This is safe to set because the current token is a semicolon,
4301 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004302 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004303 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004304 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004305 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004306 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004307
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004308 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004309
Denys Vlasenko10bde142018-12-27 18:23:58 +01004310 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004311 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004312
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004313 bc_parse_pushJUMP_ZERO(exit_idx);
4314 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004315
Gavin Howard01055ba2018-11-03 11:00:21 -06004316 bc_vec_push(&p->conds, &update_idx);
4317 bc_vec_push(&p->func->labels, &p->func->code.len);
4318
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004319 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004320 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004321 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004322 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004323 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004324 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004325 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4326 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004327 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004328
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004329 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004330 bc_vec_push(&p->func->labels, &p->func->code.len);
4331
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004332 bc_vec_push(&p->exits, &exit_idx);
4333 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004334
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004335 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004336 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004337
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004338 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004339 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004340
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004341 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004342 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004343
4344 bc_vec_pop(&p->exits);
4345 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004346
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004347 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004348}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004349#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004350
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004351static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004352{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004353 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004354 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004355
Gavin Howard01055ba2018-11-03 11:00:21 -06004356 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004357 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4358 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004359 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004360 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004361 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004362 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004363 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004364
Denys Vlasenko10bde142018-12-27 18:23:58 +01004365 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004366}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004367#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004368
Denys Vlasenko53799502019-01-25 14:24:03 +01004369static BC_STATUS zbc_func_insert(BcFunc *f, char *name, BcType type)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004370{
4371 BcId *autoid;
4372 BcId a;
4373 size_t i;
4374
4375 autoid = (void*)f->autos.v;
4376 for (i = 0; i < f->autos.len; i++, autoid++) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004377 if (strcmp(name, autoid->name) == 0
Denys Vlasenko53799502019-01-25 14:24:03 +01004378 && type == (BcType) autoid->idx
Denys Vlasenko22314682019-01-01 21:50:14 +01004379 ) {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004380 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko22314682019-01-01 21:50:14 +01004381 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004382 }
4383
Denys Vlasenko53799502019-01-25 14:24:03 +01004384 a.idx = type;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004385 a.name = name;
4386
4387 bc_vec_push(&f->autos, &a);
4388
4389 RETURN_STATUS(BC_STATUS_SUCCESS);
4390}
4391#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4392
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004393static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004394{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004395 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004396 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01004397 bool comma, voidfunc;
Gavin Howard01055ba2018-11-03 11:00:21 -06004398 char *name;
4399
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004400 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004401 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004402 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004403 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004404 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004405
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004406 // To be maximally both POSIX and GNU-compatible,
4407 // "void" is not treated as a normal keyword:
4408 // you can have variable named "void", and even a function
4409 // named "void": "define void() { return 6; }" is ok.
4410 // _Only_ "define void f() ..." syntax treats "void"
4411 // specially.
4412 voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004413
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);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004416
4417 voidfunc = (voidfunc && p->lex == XC_LEX_NAME);
4418 if (voidfunc) {
4419 s = zxc_lex_next();
4420 if (s) RETURN_STATUS(s);
4421 }
4422
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004423 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko22314682019-01-01 21:50:14 +01004424 RETURN_STATUS(bc_error_bad_function_definition());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004425
4426 p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v));
4427 p->func = xc_program_func(p->fidx);
4428 p->func->voidfunc = voidfunc;
4429
Denys Vlasenko10bde142018-12-27 18:23:58 +01004430 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004431 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004432
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004433 comma = false;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004434 while (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004435 BcType t = BC_TYPE_VAR;
4436
4437 if (p->lex == XC_LEX_OP_MULTIPLY) {
4438 t = BC_TYPE_REF;
4439 s = zxc_lex_next();
4440 if (s) RETURN_STATUS(s);
4441 s = zbc_POSIX_does_not_allow("references");
4442 if (s) RETURN_STATUS(s);
4443 }
4444
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004445 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004446 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004447
4448 ++p->func->nparams;
4449
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004450 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004451 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 if (s) goto err;
4453
Denys Vlasenko53799502019-01-25 14:24:03 +01004454 if (p->lex == BC_LEX_LBRACKET) {
4455 if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004456 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004457 if (s) goto err;
4458
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004459 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004460 s = bc_error_bad_function_definition();
Gavin Howard01055ba2018-11-03 11:00:21 -06004461 goto err;
4462 }
4463
Denys Vlasenko10bde142018-12-27 18:23:58 +01004464 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004465 if (s) goto err;
4466 }
Denys Vlasenko53799502019-01-25 14:24:03 +01004467 else if (t == BC_TYPE_REF) {
4468 s = bc_error_at("vars can't be references");
4469 goto err;
4470 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004471
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004472 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004473 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004474 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004475 if (s) goto err;
4476 }
4477
Denys Vlasenko53799502019-01-25 14:24:03 +01004478 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004479 if (s) goto err;
4480 }
4481
Denys Vlasenko22314682019-01-01 21:50:14 +01004482 if (comma) RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004483
Denys Vlasenko10bde142018-12-27 18:23:58 +01004484 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004485 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004486
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004487 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004488 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004489 if (s) RETURN_STATUS(s);
4490 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004491
Denys Vlasenko202dd192018-12-16 17:30:35 +01004492 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004493 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004494 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004495 // GNU bc requires a {} block even if function body has single stmt, enforce this
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004496 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004497 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004498
4499 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004500 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004501 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004502 if (s) RETURN_STATUS(s);
4503
Denys Vlasenko10bde142018-12-27 18:23:58 +01004504 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004505
4506 // Subsequent code generation is into main program
4507 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004508 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004509
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004510 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004511 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004512 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004513 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004514 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004515 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004516}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004517#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004518
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004519static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004520{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004521 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004522 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004523 char *name;
4524
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004525 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004526 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004527 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004528
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004529 for (;;) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004530 BcType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004531
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004532 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004533 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004534
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004535 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004536 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004537 if (s) goto err;
4538
Denys Vlasenko53799502019-01-25 14:24:03 +01004539 t = BC_TYPE_VAR;
4540 if (p->lex == BC_LEX_LBRACKET) {
4541 t = BC_TYPE_ARRAY;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004542 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 if (s) goto err;
4544
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004545 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004546 s = bc_error_at("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 goto err;
4548 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004549 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 if (s) goto err;
4551 }
4552
Denys Vlasenko53799502019-01-25 14:24:03 +01004553 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004554 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004555
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004556 if (p->lex == XC_LEX_NLINE
4557 || p->lex == BC_LEX_SCOLON
4558 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004559 ) {
4560 break;
4561 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004562 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko22314682019-01-01 21:50:14 +01004563 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004564 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004565 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004566 }
4567
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004568 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004569 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004570 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004572 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004573 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004574}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004575#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004576
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004577#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004578static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004579{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004580 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 BcStatus s = BC_STATUS_SUCCESS;
4582
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004583 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004584
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004585 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004586 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004587 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004588 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004589 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004590 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004591 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004592 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004593
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004594 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004595 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4596 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004597 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004598 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004599 } while (p->lex == XC_LEX_NLINE);
4600 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004601 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004602 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004603 if (s) RETURN_STATUS(s);
4604 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004605 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004606 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004607 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004608 if (s) RETURN_STATUS(s);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004609 // Check that next token is a correct stmt delimiter -
4610 // disallows "print 1 print 2" and such.
4611 if (p->lex == BC_LEX_RBRACE)
4612 break;
4613 if (p->lex != BC_LEX_SCOLON
4614 && p->lex != XC_LEX_NLINE
4615 ) {
4616 RETURN_STATUS(bc_error_at("bad statement terminator"));
4617 }
4618 s = zxc_lex_next();
4619 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004620 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004621 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004622 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4623 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 }
4625
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004626 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4627 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004628 case XC_LEX_OP_MINUS:
4629 case BC_LEX_OP_INC:
4630 case BC_LEX_OP_DEC:
4631 case BC_LEX_OP_BOOL_NOT:
4632 case BC_LEX_LPAREN:
4633 case XC_LEX_NAME:
4634 case XC_LEX_NUMBER:
4635 case BC_LEX_KEY_IBASE:
4636 case BC_LEX_KEY_LAST:
4637 case BC_LEX_KEY_LENGTH:
4638 case BC_LEX_KEY_OBASE:
4639 case BC_LEX_KEY_READ:
4640 case BC_LEX_KEY_SCALE:
4641 case BC_LEX_KEY_SQRT:
4642 s = zbc_parse_expr(BC_PARSE_PRINT);
4643 break;
4644 case XC_LEX_STR:
4645 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004646 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004647 break;
4648 case BC_LEX_KEY_BREAK:
4649 case BC_LEX_KEY_CONTINUE:
4650 s = zbc_parse_break_or_continue(p->lex);
4651 break;
4652 case BC_LEX_KEY_FOR:
4653 s = zbc_parse_for();
4654 break;
4655 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004656 xc_parse_push(BC_INST_HALT);
4657 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004658 break;
4659 case BC_LEX_KEY_IF:
4660 s = zbc_parse_if();
4661 break;
4662 case BC_LEX_KEY_LIMITS:
4663 // "limits" is a compile-time command,
4664 // the output is produced at _parse time_.
4665 printf(
4666 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4667 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4668 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4669 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
Denys Vlasenkoe05ec6e2019-01-04 16:26:19 +01004670 // "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" - GNU bc does not show this
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004671 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4672 "Number of vars = "BC_MAX_VARS_STR "\n"
4673 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004674 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004675 break;
4676 case BC_LEX_KEY_PRINT:
4677 s = zbc_parse_print();
4678 break;
4679 case BC_LEX_KEY_QUIT:
4680 // "quit" is a compile-time command. For example,
4681 // "if (0 == 1) quit" terminates when parsing the statement,
4682 // not when it is executed
4683 QUIT_OR_RETURN_TO_MAIN;
4684 case BC_LEX_KEY_RETURN:
4685 if (!p->in_funcdef)
4686 RETURN_STATUS(bc_error("'return' not in a function"));
4687 s = zbc_parse_return();
4688 break;
4689 case BC_LEX_KEY_WHILE:
4690 s = zbc_parse_while();
4691 break;
4692 default:
4693 s = bc_error_bad_token();
4694 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004695 }
4696
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004697 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004698 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004699}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004700#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004701
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004702static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004703{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004704 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004705 BcStatus s;
4706
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004707 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004708//why?
4709// if (p->lex == XC_LEX_EOF)
4710// s = bc_error("end of file");
4711// else
4712 if (p->lex == BC_LEX_KEY_DEFINE) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004713 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004714 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004715 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004716 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004717 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004718 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004719
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004720 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004721 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004722}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004723#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004724
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004725#undef zbc_parse_expr
4726static BC_STATUS zbc_parse_expr(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004727{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004728 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004729 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004730 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004731 unsigned nparens, nrelops;
Denys Vlasenko22314682019-01-01 21:50:14 +01004732 bool paren_first, rprn, assign, bin_last, incdec;
Gavin Howard01055ba2018-11-03 11:00:21 -06004733
Denys Vlasenko17df8822018-12-14 23:00:24 +01004734 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004735 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 nparens = nrelops = 0;
Denys Vlasenko22314682019-01-01 21:50:14 +01004737 rprn = assign = incdec = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004738 bin_last = true;
4739
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004740 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004741 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004742 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004743 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004744
4745 if (!lex_allowed_in_bc_expr(t))
4746 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004747
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004748 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004749 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004750 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004751 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004752 case BC_LEX_OP_INC:
4753 case BC_LEX_OP_DEC:
4754 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004755 if (incdec) RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko22314682019-01-01 21:50:14 +01004756 s = zbc_parse_incdec(&prev, &nexprs, flags);
4757 incdec = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004758 rprn = bin_last = false;
4759 //get_token = false; - already is
4760 break;
4761 case XC_LEX_OP_MINUS:
4762 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004763 s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004764 rprn = false;
4765 //get_token = false; - already is
4766 bin_last = (prev == XC_INST_MINUS);
Denys Vlasenko22314682019-01-01 21:50:14 +01004767 if (bin_last) incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004768 break;
4769 case BC_LEX_OP_ASSIGN_POWER:
4770 case BC_LEX_OP_ASSIGN_MULTIPLY:
4771 case BC_LEX_OP_ASSIGN_DIVIDE:
4772 case BC_LEX_OP_ASSIGN_MODULUS:
4773 case BC_LEX_OP_ASSIGN_PLUS:
4774 case BC_LEX_OP_ASSIGN_MINUS:
4775 case BC_LEX_OP_ASSIGN:
4776 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4777 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4778 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4779 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4780 ) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004781 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004782 }
4783 // Fallthrough.
4784 case XC_LEX_OP_POWER:
4785 case XC_LEX_OP_MULTIPLY:
4786 case XC_LEX_OP_DIVIDE:
4787 case XC_LEX_OP_MODULUS:
4788 case XC_LEX_OP_PLUS:
4789 case XC_LEX_OP_REL_EQ:
4790 case XC_LEX_OP_REL_LE:
4791 case XC_LEX_OP_REL_GE:
4792 case XC_LEX_OP_REL_NE:
4793 case XC_LEX_OP_REL_LT:
4794 case XC_LEX_OP_REL_GT:
4795 case BC_LEX_OP_BOOL_NOT:
4796 case BC_LEX_OP_BOOL_OR:
4797 case BC_LEX_OP_BOOL_AND:
4798 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004799 if (t == BC_LEX_OP_BOOL_NOT) {
4800 if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004801 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko22314682019-01-01 21:50:14 +01004802 } else if (prev == XC_INST_BOOL_NOT) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004803 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004804 }
Denys Vlasenko22314682019-01-01 21:50:14 +01004805
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004806 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4807 prev = BC_TOKEN_2_INST(t);
4808 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko22314682019-01-01 21:50:14 +01004809 rprn = incdec = false;
4810 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004811 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4812 break;
4813 case BC_LEX_LPAREN:
4814 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004815 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004816 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004817 bc_vec_push(&p->ops, &t);
4818 nparens++;
4819 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004820 rprn = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004821 break;
4822 case BC_LEX_RPAREN:
4823 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenkoa1698a12019-01-08 19:32:38 +01004824//why?
4825// if (p->lex_last == BC_LEX_LPAREN) {
4826// RETURN_STATUS(bc_error_at("empty expression"));
4827// }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004828 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004829 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004830 if (nparens == 0) {
4831 goto exit_loop;
4832 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004833 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4834 nparens--;
4835 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004836 rprn = true;
4837 bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004838 break;
4839 case XC_LEX_NAME:
4840 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004841 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004842 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004843 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko22314682019-01-01 21:50:14 +01004844 rprn = (prev == BC_INST_CALL);
4845 bin_last = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004846 //get_token = false; - already is
4847 nexprs++;
4848 break;
4849 case XC_LEX_NUMBER:
4850 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004851 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004852 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004853 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004854 prev = XC_INST_NUM;
4855 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004856 rprn = bin_last = false;
4857 nexprs++;
4858 break;
4859 case BC_LEX_KEY_IBASE:
4860 case BC_LEX_KEY_LAST:
4861 case BC_LEX_KEY_OBASE:
4862 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004863 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004864 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004865 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004866 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004867 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004868 rprn = bin_last = false;
4869 nexprs++;
4870 break;
4871 case BC_LEX_KEY_LENGTH:
4872 case BC_LEX_KEY_SQRT:
4873 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004874 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004875 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004876 s = zbc_parse_builtin(t, flags, &prev);
4877 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004878 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004879 nexprs++;
4880 break;
4881 case BC_LEX_KEY_READ:
4882 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004883 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004884 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004885 s = zbc_parse_read();
4886 prev = XC_INST_READ;
4887 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004888 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004889 nexprs++;
4890 break;
4891 case BC_LEX_KEY_SCALE:
4892 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004893 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004894 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004895 s = zbc_parse_scale(&prev, flags);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004896 //get_token = false; - already is
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004897 rprn = bin_last = false;
4898 nexprs++;
4899 break;
4900 default:
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004901 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004902 }
4903
Denys Vlasenkobb116032018-12-25 01:16:37 +01004904 if (s || G_interrupt) // error, or ^C: stop parsing
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004905 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004906 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004907 s = zxc_lex_next();
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004908 if (s) RETURN_STATUS(s);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004909 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004910 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004911 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004912
4913 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004914 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004915 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004916
4917 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004918 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004919
Denys Vlasenko10bde142018-12-27 18:23:58 +01004920 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004921
Denys Vlasenko69560f42018-12-24 14:14:23 +01004922 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004923 bc_vec_pop(&p->ops);
4924 }
4925
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004926 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004927 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004928
Gavin Howard01055ba2018-11-03 11:00:21 -06004929 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004930 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004931 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004932 if (s) RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004933 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004934 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004935 s = zbc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004936 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004937 }
4938
4939 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004940 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004941 xc_parse_push(XC_INST_PRINT);
4942 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004943 }
4944
Denys Vlasenko17df8822018-12-14 23:00:24 +01004945 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004946 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004947}
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004948#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004949
Gavin Howard01055ba2018-11-03 11:00:21 -06004950#endif // ENABLE_BC
4951
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004952#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004953
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004954static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004955{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004956 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004957 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004958
Denys Vlasenko10bde142018-12-27 18:23:58 +01004959 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004960 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004961 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004962
Denys Vlasenko10bde142018-12-27 18:23:58 +01004963 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004964
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004965 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004966}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004967#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004968
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004969static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004970{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004971 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004972 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004973 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004974
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004975 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004976
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004977 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01004978 xc_parse_pushInst_and_Index(XC_INST_STR, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004979 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004980
Brian Foleyb64470b2019-09-05 10:50:13 +02004981 // Add an empty function so that if zdc_program_execStr ever needs to
4982 // parse the string into code (from the 'x' command) there's somewhere
4983 // to store the bytecode.
Denys Vlasenko10bde142018-12-27 18:23:58 +01004984 xc_program_add_fn();
4985 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004986
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004987 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004988}
4989
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004990static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004991{
4992 BcStatus s;
4993
Denys Vlasenko10bde142018-12-27 18:23:58 +01004994 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004995 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004996 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004997 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004998 }
4999
5000 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005001 xc_parse_push(DC_INST_SWAP);
5002 xc_parse_push(XC_INST_ASSIGN);
5003 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005004 }
5005
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005006 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005007}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005008#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005009
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005010static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005011{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005012 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06005013 BcStatus s;
5014
Denys Vlasenko10bde142018-12-27 18:23:58 +01005015 xc_parse_push(inst);
5016 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06005017
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005018 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005019 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005020
Denys Vlasenko10bde142018-12-27 18:23:58 +01005021 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005022 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005023
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005024 // Note that 'else' part can not be on the next line:
5025 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
5026 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005027 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005028 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005029 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005030 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005031 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005032 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005033 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005034
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005035 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005036}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005037#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005038
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005039static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005040{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005041 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005042 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005043 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005044
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005045 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005046 s = BC_STATUS_SUCCESS;
5047 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005048 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005049 case XC_LEX_OP_REL_EQ:
5050 case XC_LEX_OP_REL_LE:
5051 case XC_LEX_OP_REL_GE:
5052 case XC_LEX_OP_REL_NE:
5053 case XC_LEX_OP_REL_LT:
5054 case XC_LEX_OP_REL_GT:
5055 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
5056 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
5057 get_token = false;
5058 break;
5059 case DC_LEX_SCOLON:
5060 case DC_LEX_COLON:
5061 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
5062 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
5063 break;
5064 case XC_LEX_STR:
5065 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
5066 dc_parse_string();
5067 break;
5068 case XC_LEX_NEG:
5069 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005070 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005071 if (s) RETURN_STATUS(s);
5072 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005073 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005074 xc_parse_pushNUM();
5075 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005076 break;
5077 case XC_LEX_NUMBER:
5078 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005079 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005080 break;
5081 case DC_LEX_READ:
5082 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005083 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005084 break;
5085 case DC_LEX_OP_ASSIGN:
5086 case DC_LEX_STORE_PUSH:
5087 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
5088 assign = (t == DC_LEX_OP_ASSIGN);
5089 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
5090 s = zdc_parse_mem(inst, true, assign);
5091 break;
5092 case DC_LEX_LOAD:
5093 case DC_LEX_LOAD_POP:
5094 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
5095 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
5096 s = zdc_parse_mem(inst, true, false);
5097 break;
5098 case DC_LEX_STORE_IBASE:
5099 case DC_LEX_STORE_SCALE:
5100 case DC_LEX_STORE_OBASE:
5101 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5102 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5103 s = zdc_parse_mem(inst, false, true);
5104 break;
5105 default:
5106 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5107 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005108 }
5109
Denys Vlasenko10bde142018-12-27 18:23:58 +01005110 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005111
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005112 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005113 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005114}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005115#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005116
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005117static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005118{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005119 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005120 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005121
Denys Vlasenko6842c602019-01-04 05:41:47 +01005122 if (p->lex == XC_LEX_NLINE)
5123 RETURN_STATUS(zxc_lex_next());
5124
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005125 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005126 if (i >= 0) {
5127 BcInst inst = dc_LEX_to_INST[i];
5128 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005129 xc_parse_push(inst);
5130 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005131 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005132 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005133 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005134}
5135#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5136
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005137static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005138{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005139 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005140 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5141 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005142 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005143 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005144 }
5145
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005146 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005147 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005148}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005149#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005150
Gavin Howard01055ba2018-11-03 11:00:21 -06005151#endif // ENABLE_DC
5152
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005153//
5154// Execution engine
5155//
5156
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005157#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5158#define BC_PROG_NUM(r, n) \
5159 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5160
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005161#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5162#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5163
Denys Vlasenko53799502019-01-25 14:24:03 +01005164static size_t xc_program_index(char *code, size_t *bgn)
5165{
5166 unsigned char *bytes = (void*)(code + *bgn);
5167 unsigned amt;
5168 unsigned i;
5169 size_t res;
5170
5171 amt = *bytes++;
5172 if (amt < SMALL_INDEX_LIMIT) {
5173 *bgn += 1;
5174 return amt;
5175 }
5176 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
5177 *bgn += amt + 1;
5178
5179 res = 0;
5180 i = 0;
5181 do {
5182 res |= (size_t)(*bytes++) << i;
5183 i += 8;
5184 } while (--amt != 0);
5185
5186 return res;
5187}
5188
5189static char *xc_program_name(char *code, size_t *bgn)
5190{
5191 code += *bgn;
5192 *bgn += strlen(code) + 1;
5193
5194 return xstrdup(code);
5195}
5196
5197static BcVec* xc_program_dereference(BcVec *vec)
5198{
5199 BcVec *v;
5200 size_t vidx, nidx, i = 0;
5201
5202 //assert(vec->size == sizeof(uint8_t));
5203
5204 vidx = xc_program_index(vec->v, &i);
5205 nidx = xc_program_index(vec->v, &i);
5206
5207 v = bc_vec_item(&G.prog.arrs, vidx);
5208 v = bc_vec_item(v, nidx);
5209
5210 //assert(v->size != sizeof(uint8_t));
5211
5212 return v;
5213}
5214
5215static BcVec* xc_program_search(char *id, BcType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06005216{
Gavin Howard01055ba2018-11-03 11:00:21 -06005217 BcId e, *ptr;
5218 BcVec *v, *map;
5219 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005220 int new;
Denys Vlasenko53799502019-01-25 14:24:03 +01005221 bool var = (type == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005222
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005223 v = var ? &G.prog.vars : &G.prog.arrs;
5224 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005225
5226 e.name = id;
5227 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005228 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005229
5230 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005231 BcVec v2;
5232 bc_array_init(&v2, var);
5233 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005234 }
5235
5236 ptr = bc_vec_item(map, i);
5237 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005238 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005239}
5240
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005241// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005242static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005243{
Gavin Howard01055ba2018-11-03 11:00:21 -06005244 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005245 case XC_RESULT_STR:
5246 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005247 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005248 case XC_RESULT_IBASE:
5249 case XC_RESULT_SCALE:
5250 case XC_RESULT_OBASE:
5251 *num = &r->d.n;
5252 break;
5253 case XC_RESULT_CONSTANT: {
5254 BcStatus s;
5255 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005256 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005257
Denys Vlasenko10bde142018-12-27 18:23:58 +01005258 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005259 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005260
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005261 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005262
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005263 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005264 if (s) {
5265 bc_num_free(&r->d.n);
5266 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005267 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005268 *num = &r->d.n;
5269 r->t = XC_RESULT_TEMP;
5270 break;
5271 }
5272 case XC_RESULT_VAR:
5273 case XC_RESULT_ARRAY:
5274 case XC_RESULT_ARRAY_ELEM: {
Denys Vlasenko53799502019-01-25 14:24:03 +01005275 BcType type = (r->t == XC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY;
5276 BcVec *v = xc_program_search(r->d.id.name, type);
5277 void *p = bc_vec_top(v);
5278
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005279 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko53799502019-01-25 14:24:03 +01005280 size_t idx = r->d.id.idx;
5281
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005282 v = p;
Denys Vlasenko53799502019-01-25 14:24:03 +01005283 if (v->size == sizeof(uint8_t))
5284 v = xc_program_dereference(v);
5285 //assert(v->size == sizeof(BcNum));
5286 if (v->len <= idx)
5287 bc_array_expand(v, idx + 1);
5288 *num = bc_vec_item(v, idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005289 } else {
5290 *num = p;
5291 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005292 break;
5293 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005294#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005295 case BC_RESULT_LAST:
5296 *num = &G.prog.last;
5297 break;
5298 case BC_RESULT_ONE:
5299 *num = &G.prog.one;
5300 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005301#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005302#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005303 default:
5304 // Testing the theory that dc does not reach LAST/ONE
5305 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005306#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005307 }
5308
Denys Vlasenko29301232018-12-11 15:29:32 +01005309 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005310}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005311#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005312
Denys Vlasenko10bde142018-12-27 18:23:58 +01005313static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005314 BcResult **r, BcNum **rn, bool assign)
5315{
5316 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005317 BcResultType lt, rt;
5318
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005319 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005320 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005321
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005322 *r = bc_vec_item_rev(&G.prog.results, 0);
5323 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005324
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005325 s = zxc_program_num(*l, ln);
5326 if (s) RETURN_STATUS(s);
5327 s = zxc_program_num(*r, rn);
5328 if (s) RETURN_STATUS(s);
5329
Gavin Howard01055ba2018-11-03 11:00:21 -06005330 lt = (*l)->t;
5331 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005332
5333 // We run this again under these conditions in case any vector has been
5334 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005335 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005336 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005337 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005338 }
5339
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005340 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005341 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005342 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005343 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005344
Denys Vlasenko29301232018-12-11 15:29:32 +01005345 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005346}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005347#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005348
Denys Vlasenko10bde142018-12-27 18:23:58 +01005349static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005350{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005351 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005352 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005353 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005354}
5355
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005356// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005357static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005358{
5359 BcStatus s;
5360
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005361 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005362 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005363 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005364
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005365 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005366 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005367
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005368 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005369 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005370
Denys Vlasenko29301232018-12-11 15:29:32 +01005371 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005372}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005373#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005374
Denys Vlasenko10bde142018-12-27 18:23:58 +01005375static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005376{
5377 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005378 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005379}
5380
Denys Vlasenko10bde142018-12-27 18:23:58 +01005381static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005382{
5383 BcStatus s;
5384 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005385 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005386
Denys Vlasenko10bde142018-12-27 18:23:58 +01005387 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005388 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005389 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005390
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005391 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005392 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 -06005393 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005394 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005395
Denys Vlasenko259137d2018-12-11 19:42:05 +01005396 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005397 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005398 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005399 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005400}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005401#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005402
Denys Vlasenko10bde142018-12-27 18:23:58 +01005403static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005404{
5405 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005406 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005407 BcVec buf;
5408 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005409 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005410
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005411 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005412 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005413
Denys Vlasenko10bde142018-12-27 18:23:58 +01005414 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005415 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005416
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005417 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005418 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005419 //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 +01005420
Denys Vlasenko10bde142018-12-27 18:23:58 +01005421 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005422 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005423 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005424 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005425 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005426 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005427 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005428 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005429 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005430 s = bc_error_at("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005431 goto exec_err;
5432 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005433 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005434
5435 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005436 ip.inst_idx = 0;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005437 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005438
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005439 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005440 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005441 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005442 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005443 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005444}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005445#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005446
Denys Vlasenko10bde142018-12-27 18:23:58 +01005447static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005448{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005449#if ENABLE_DC
Denys Vlasenko266bec82019-01-02 05:03:53 +01005450 if (!str[0] && IS_DC) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005451 // Example: echo '[]ap' | dc
5452 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005453 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005454 return;
5455 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005456#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005457 while (*str) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005458 char c = *str++;
5459 if (c == '\\') {
5460 static const char esc[] ALIGN1 = "nabfrt""e\\";
5461 char *n;
5462
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005463 c = *str++;
Brian Foley10509a72019-09-05 10:53:21 +02005464 n = strchr(esc, c); // note: if c is NUL, n = \0 at end of esc
5465 if (!n || !c) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005466 // Just print the backslash and following character
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005467 bb_putchar('\\');
5468 ++G.prog.nchars;
Brian Foley10509a72019-09-05 10:53:21 +02005469 // But if we're at the end of the string, stop
5470 if (!c) break;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005471 } else {
5472 if (n - esc == 0) // "\n" ?
5473 G.prog.nchars = SIZE_MAX;
5474 c = "\n\a\b\f\r\t""\\\\""\\"[n - esc];
5475 // n a b f r t e \ \<end of line>
Gavin Howard01055ba2018-11-03 11:00:21 -06005476 }
5477 }
Denys Vlasenko266bec82019-01-02 05:03:53 +01005478 putchar(c);
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005479 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005480 }
5481}
5482
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005483static void bc_num_printNewline(void)
5484{
5485 if (G.prog.nchars == G.prog.len - 1) {
5486 bb_putchar('\\');
5487 bb_putchar('\n');
5488 G.prog.nchars = 0;
5489 }
5490}
5491
5492#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005493static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005494{
5495 (void) radix;
5496 bb_putchar((char) num);
5497 G.prog.nchars += width;
5498}
5499#endif
5500
5501static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5502{
5503 size_t exp, pow;
5504
5505 bc_num_printNewline();
5506 bb_putchar(radix ? '.' : ' ');
5507 ++G.prog.nchars;
5508
5509 bc_num_printNewline();
5510 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5511 continue;
5512
5513 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5514 size_t dig;
5515 bc_num_printNewline();
5516 dig = num / pow;
5517 num -= dig * pow;
5518 bb_putchar(((char) dig) + '0');
5519 }
5520}
5521
5522static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5523{
5524 if (radix) {
5525 bc_num_printNewline();
5526 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005527 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005528 }
5529
5530 bc_num_printNewline();
5531 bb_putchar(bb_hexdigits_upcase[num]);
5532 G.prog.nchars += width;
5533}
5534
5535static void bc_num_printDecimal(BcNum *n)
5536{
5537 size_t i, rdx = n->rdx - 1;
5538
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005539 if (n->neg) {
5540 bb_putchar('-');
5541 G.prog.nchars++;
5542 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005543
5544 for (i = n->len - 1; i < n->len; --i)
5545 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5546}
5547
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005548typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5549
Denys Vlasenko10bde142018-12-27 18:23:58 +01005550static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005551{
5552 BcStatus s;
5553 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005554 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005555 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005556 BcNum intp, fracp, digit, frac_len;
5557 unsigned long dig, *ptr;
5558 size_t i;
5559 bool radix;
5560
5561 if (n->len == 0) {
5562 print(0, width, false);
5563 RETURN_STATUS(BC_STATUS_SUCCESS);
5564 }
5565
5566 bc_vec_init(&stack, sizeof(long), NULL);
5567 bc_num_init(&intp, n->len);
5568 bc_num_init(&fracp, n->rdx);
5569 bc_num_init(&digit, width);
5570 bc_num_init(&frac_len, BC_NUM_INT(n));
5571 bc_num_copy(&intp, n);
5572 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005573 base.cap = ARRAY_SIZE(base_digs);
5574 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005575 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005576
5577 bc_num_truncate(&intp, intp.rdx);
5578 s = zbc_num_sub(n, &intp, &fracp, 0);
5579 if (s) goto err;
5580
5581 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005582 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005583 if (s) goto err;
5584 s = zbc_num_ulong(&digit, &dig);
5585 if (s) goto err;
5586 bc_vec_push(&stack, &dig);
5587 }
5588
5589 for (i = 0; i < stack.len; ++i) {
5590 ptr = bc_vec_item_rev(&stack, i);
5591 print(*ptr, width, false);
5592 }
5593
5594 if (!n->rdx) goto err;
5595
5596 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005597 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005598 if (s) goto err;
5599 s = zbc_num_ulong(&fracp, &dig);
5600 if (s) goto err;
5601 bc_num_ulong2num(&intp, dig);
5602 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5603 if (s) goto err;
5604 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005605 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005606 if (s) goto err;
5607 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005608 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005609 bc_num_free(&frac_len);
5610 bc_num_free(&digit);
5611 bc_num_free(&fracp);
5612 bc_num_free(&intp);
5613 bc_vec_free(&stack);
5614 RETURN_STATUS(s);
5615}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005616#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005617
Denys Vlasenko10bde142018-12-27 18:23:58 +01005618static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005619{
5620 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005621 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005622 BcNumDigitOp print;
5623 bool neg = n->neg;
5624
5625 if (neg) {
5626 bb_putchar('-');
5627 G.prog.nchars++;
5628 }
5629
5630 n->neg = false;
5631
Denys Vlasenko680ccd32018-12-31 19:42:13 +01005632 if (G.prog.ob_t <= 16) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005633 width = 1;
5634 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005635 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005636 unsigned i = G.prog.ob_t - 1;
5637 width = 0;
5638 for (;;) {
5639 width++;
5640 i /= 10;
5641 if (i == 0)
5642 break;
5643 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005644 print = bc_num_printDigits;
5645 }
5646
Denys Vlasenko10bde142018-12-27 18:23:58 +01005647 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005648 n->neg = neg;
5649
5650 RETURN_STATUS(s);
5651}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005652#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005653
Denys Vlasenko10bde142018-12-27 18:23:58 +01005654static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005655{
5656 BcStatus s = BC_STATUS_SUCCESS;
5657
5658 bc_num_printNewline();
5659
5660 if (n->len == 0) {
5661 bb_putchar('0');
5662 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005663 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005664 bc_num_printDecimal(n);
5665 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005666 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005667
5668 if (newline) {
5669 bb_putchar('\n');
5670 G.prog.nchars = 0;
5671 }
5672
5673 RETURN_STATUS(s);
5674}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005675#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005676
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005677#if !ENABLE_DC
5678// for bc, idx is always 0
5679#define xc_program_print(inst, idx) \
5680 xc_program_print(inst)
5681#endif
5682static BC_STATUS xc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005683{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005684 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005685 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005686 BcNum *num;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005687 IF_NOT_DC(size_t idx = 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005688
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005689 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005690 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005691
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005692 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005693#if ENABLE_BC
5694 if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID)
5695 // void function's result on stack, ignore
5696 RETURN_STATUS(BC_STATUS_SUCCESS);
5697#endif
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005698 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005699 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005700
5701 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005702 s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005703#if ENABLE_BC
5704 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5705#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005706 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005707 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005708
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005709 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005710 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005711
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005712 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005713 char *nl;
5714 G.prog.nchars += printf("%s", str);
5715 nl = strrchr(str, '\n');
5716 if (nl)
5717 G.prog.nchars = strlen(nl + 1);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005718 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005719 xc_program_printString(str);
Denys Vlasenko266bec82019-01-02 05:03:53 +01005720 if (inst == XC_INST_PRINT)
5721 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005722 }
5723 }
5724
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005725 if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
Denys Vlasenko29301232018-12-11 15:29:32 +01005727 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005728}
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005729#define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005730
Denys Vlasenko10bde142018-12-27 18:23:58 +01005731static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005732{
5733 BcStatus s;
5734 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005735 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005736
Denys Vlasenko10bde142018-12-27 18:23:58 +01005737 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005738 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005739
5740 bc_num_init(&res.d.n, num->len);
5741 bc_num_copy(&res.d.n, num);
5742 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5743
Denys Vlasenko10bde142018-12-27 18:23:58 +01005744 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005745
Denys Vlasenko29301232018-12-11 15:29:32 +01005746 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005747}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005748#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005749
Denys Vlasenko10bde142018-12-27 18:23:58 +01005750static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005751{
5752 BcStatus s;
5753 BcResult *opd1, *opd2, res;
5754 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005755 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005756
Denys Vlasenko10bde142018-12-27 18:23:58 +01005757 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005758 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005759
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005760 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005762 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005763 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005764 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005765 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005766 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005767 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005768 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005769 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005770 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005771 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005772 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005773 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005774 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005775 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005776 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005777 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005778 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005779 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005780 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005781 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005782 cond = (cond > 0);
5783 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005784 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005785 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005786 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005787 }
5788 }
5789
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005790 if (cond) bc_num_one(&res.d.n);
5791 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005792
Denys Vlasenko10bde142018-12-27 18:23:58 +01005793 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005794
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005795 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005796}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005797#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005798
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005799#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005800static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005801{
5802 BcNum n2;
5803 BcResult res;
5804
5805 memset(&n2, 0, sizeof(BcNum));
5806 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005807 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005808
5809 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005810 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005811 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005812 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005813 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005814 }
5815
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005816 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005817 bc_vec_push(v, &n2);
5818
Denys Vlasenko29301232018-12-11 15:29:32 +01005819 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005820}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005821#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005822#endif // ENABLE_DC
5823
Denys Vlasenko53799502019-01-25 14:24:03 +01005824static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, BcType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005825{
5826 BcStatus s;
5827 BcResult *ptr, r;
Denys Vlasenko53799502019-01-25 14:24:03 +01005828 BcVec *vec;
Gavin Howard01055ba2018-11-03 11:00:21 -06005829 BcNum *n;
Denys Vlasenko53799502019-01-25 14:24:03 +01005830 bool var = (t == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005831
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005832 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005833 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005834
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005835 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko53799502019-01-25 14:24:03 +01005836 if ((ptr->t == XC_RESULT_ARRAY) == var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005837 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005838 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005839
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005840#if ENABLE_DC
Denys Vlasenko53799502019-01-25 14:24:03 +01005841 if (ptr->t == XC_RESULT_STR) {
5842 if (!var)
5843 RETURN_STATUS(bc_error_variable_is_wrong_type());
5844 RETURN_STATUS(zdc_program_assignStr(ptr, vec, true));
5845 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005846#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005847
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005848 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005849 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005850
5851 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko53799502019-01-25 14:24:03 +01005852 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005853
5854 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005855 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005856 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005857 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01005858 BcVec *v = (BcVec*) n;
5859 bool ref, ref_size;
Gavin Howard01055ba2018-11-03 11:00:21 -06005860
Denys Vlasenko53799502019-01-25 14:24:03 +01005861 ref = (v->size == sizeof(BcVec) && t != BC_TYPE_ARRAY);
5862 ref_size = (v->size == sizeof(uint8_t));
5863
5864 if (ref || (ref_size && t == BC_TYPE_REF)) {
5865 bc_vec_init(&r.d.v, sizeof(uint8_t), NULL);
5866 if (ref) {
5867 size_t vidx, idx;
5868 BcId id;
5869
5870 id.name = ptr->d.id.name;
5871 v = xc_program_search(ptr->d.id.name, BC_TYPE_REF);
5872
5873 // Make sure the pointer was not invalidated.
5874 vec = xc_program_search(name, t);
5875
5876 vidx = bc_map_find_exact(&G.prog.arr_map, &id);
5877 //assert(vidx != BC_VEC_INVALID_IDX);
5878 vidx = ((BcId*) bc_vec_item(&G.prog.arr_map, vidx))->idx;
5879 idx = v->len - 1;
5880
5881 bc_vec_pushIndex(&r.d.v, vidx);
5882 bc_vec_pushIndex(&r.d.v, idx);
5883 }
5884 // If we get here, we are copying a ref to a ref.
5885 else bc_vec_npush(&r.d.v, v->len, v->v);
5886
5887 // We need to return early.
5888 goto ret;
5889 }
5890
5891 if (ref_size && t != BC_TYPE_REF)
5892 v = xc_program_dereference(v);
5893
5894 bc_array_init(&r.d.v, true);
5895 bc_array_copy(&r.d.v, v);
5896 }
5897 ret:
5898 bc_vec_push(vec, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005899 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
Denys Vlasenko29301232018-12-11 15:29:32 +01005901 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902}
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005903#define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005904
Denys Vlasenko10bde142018-12-27 18:23:58 +01005905static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005906{
5907 BcStatus s;
5908 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005909 BcNum *l, *r;
5910 bool assign = (inst == XC_INST_ASSIGN);
5911 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenko10bde142018-12-27 18:23:58 +01005913 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005914 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005915
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005916 ib = left->t == XC_RESULT_IBASE;
5917 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005918
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005919#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005920 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005921 BcVec *v;
5922
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005923 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005924 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005925 v = xc_program_search(left->d.id.name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005926
Denys Vlasenkofa210792018-12-19 19:35:40 +01005927 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005928 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005929#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005930
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005931 if (left->t == XC_RESULT_CONSTANT
5932 || left->t == XC_RESULT_TEMP
5933 IF_BC(|| left->t == BC_RESULT_VOID)
5934 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005935 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005936 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005937
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005938#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005939 if (assign)
5940 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005941 else {
5942 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005943 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 +01005944 }
5945 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005946#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005948#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005949
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005950 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005951 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005952 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5953 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5954 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005955 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005956 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005957 size_t max;
5958 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005959
Denys Vlasenko29301232018-12-11 15:29:32 +01005960 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005961 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005962 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005963 if (sc) {
5964 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005965 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005966 } else {
5967 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005968 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005969 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005970 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005971 }
5972
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005973 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005974 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005975
5976 *ptr = (size_t) val;
5977 s = BC_STATUS_SUCCESS;
5978 }
5979
5980 bc_num_init(&res.d.n, l->len);
5981 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005982 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005983
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005984 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005985}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005986#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005987
Denys Vlasenko416ce762018-12-02 20:57:17 +01005988#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005989#define xc_program_pushVar(code, bgn, pop, copy) \
5990 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005991// for bc, 'pop' and 'copy' are always false
5992#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005993static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005994 bool pop, bool copy)
5995{
Gavin Howard01055ba2018-11-03 11:00:21 -06005996 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005997 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005998
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005999 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06006000 r.d.id.name = name;
6001
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006002#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006003 if (pop || copy) {
Denys Vlasenko53799502019-01-25 14:24:03 +01006004 BcVec *v = xc_program_search(name, BC_TYPE_VAR);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006005 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006006
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006007 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006008 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006009 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006010 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006011
6012 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006013 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006014 bc_num_init_DEF_SIZE(&r.d.n);
6015 bc_num_copy(&r.d.n, num);
6016 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006017 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006018 r.d.id.idx = num->rdx;
6019 }
6020
6021 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006022 }
6023#endif // ENABLE_DC
6024
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006025 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006026
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006027 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006028}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006029#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006030
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006031static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006032{
6033 BcStatus s = BC_STATUS_SUCCESS;
6034 BcResult r;
6035 BcNum *num;
6036
Denys Vlasenko10bde142018-12-27 18:23:58 +01006037 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006038
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006039 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006040 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006041 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006042 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06006043 BcResult *operand;
6044 unsigned long temp;
6045
Denys Vlasenko10bde142018-12-27 18:23:58 +01006046 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006047 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006048 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006049 if (s) goto err;
6050
6051 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006052 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006053 goto err;
6054 }
6055
6056 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006057 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006058 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006059 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006060 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006061 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006062}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006063#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006064
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006065#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006066static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006067{
6068 BcStatus s;
6069 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006070 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006071 char inst2 = inst;
6072
Denys Vlasenko10bde142018-12-27 18:23:58 +01006073 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006074 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006075
6076 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006077 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006078 bc_num_init(&copy.d.n, num->len);
6079 bc_num_copy(&copy.d.n, num);
6080 }
6081
6082 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006083 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
6084 ? BC_INST_ASSIGN_PLUS
6085 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006086
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006087 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006088 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006089 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006090
6091 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006092 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 }
6094
Denys Vlasenko29301232018-12-11 15:29:32 +01006095 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006096}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006097#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006098
Denys Vlasenko29301232018-12-11 15:29:32 +01006099static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006100{
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006102 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006103 BcId *a;
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006104 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
Denys Vlasenko10bde142018-12-27 18:23:58 +01006106 nparams = xc_program_index(code, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006107 ip.func = xc_program_index(code, idx);
6108 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006109
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006110 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006111 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006112 }
6113 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006114 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006115 }
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006116 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006117
6118 for (i = 0; i < nparams; ++i) {
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006119 BcResult *arg;
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006120 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01006121 bool arr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006122
6123 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006124 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125
Denys Vlasenko53799502019-01-25 14:24:03 +01006126 arr = (a->idx == BC_TYPE_ARRAY || a->idx == BC_TYPE_REF);
6127
6128 if (arr != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006129 // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions)
6130 ) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006131 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006132 }
Denys Vlasenko53799502019-01-25 14:24:03 +01006133 s = zxc_program_popResultAndCopyToVar(a->name, (BcType) a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006134 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006135 }
6136
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006137 a = bc_vec_item(&func->autos, i);
6138 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006139 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006140
Denys Vlasenko53799502019-01-25 14:24:03 +01006141 v = xc_program_search(a->name, (BcType) a->idx);
6142 if (a->idx == BC_TYPE_VAR) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006143 BcNum n2;
6144 bc_num_init_DEF_SIZE(&n2);
6145 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006146 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01006147 //assert(a->idx == BC_TYPE_ARRAY);
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006148 BcVec v2;
6149 bc_array_init(&v2, true);
6150 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 }
6152 }
6153
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006154 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006155
Denys Vlasenko29301232018-12-11 15:29:32 +01006156 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006157}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006158#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006159
Denys Vlasenko29301232018-12-11 15:29:32 +01006160static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006161{
Gavin Howard01055ba2018-11-03 11:00:21 -06006162 BcResult res;
6163 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006164 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006166 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006167
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006168 f = xc_program_func(ip->func);
6169
6170 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006171 if (inst == XC_INST_RET) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006172 // bc needs this for e.g. RESULT_CONSTANT ("return 5")
6173 // because bc constants are per-function.
6174 // TODO: maybe avoid if value is already RESULT_TEMP?
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006175 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006176 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006177 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006178
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006179 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006180 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 bc_num_init(&res.d.n, num->len);
6182 bc_num_copy(&res.d.n, num);
Denys Vlasenko377cc972019-01-04 00:34:52 +01006183 bc_vec_pop(&G.prog.results);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006184 } else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006185 if (f->voidfunc)
6186 res.t = BC_RESULT_VOID;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006187 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006188 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006189 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006190 bc_vec_push(&G.prog.results, &res);
6191
6192 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006193
6194 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006195 a = (void*)f->autos.v;
6196 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006197 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006198 v = xc_program_search(a->name, (BcType) a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006199 bc_vec_pop(v);
6200 }
6201
Denys Vlasenko29301232018-12-11 15:29:32 +01006202 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006204#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006205#endif // ENABLE_BC
6206
Denys Vlasenko10bde142018-12-27 18:23:58 +01006207static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006208{
6209 return (unsigned long) n->rdx;
6210}
6211
Denys Vlasenko10bde142018-12-27 18:23:58 +01006212static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006213{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006214 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006215
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006216 if (n->rdx != len) return len;
6217 for (;;) {
6218 if (len == 0) break;
6219 len--;
6220 if (n->num[len] != 0) break;
6221 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 return len;
6223}
6224
Denys Vlasenko10bde142018-12-27 18:23:58 +01006225static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006226{
6227 BcStatus s;
6228 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006229 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006230 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006231 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006232
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006233 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006234 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006235 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006236
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006237 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006238 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006239
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006240#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006241 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006242 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006243#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006244
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006245 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006246
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006247 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006248 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006249#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006250 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006251 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006252 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006253#endif
6254#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006255 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006256 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006257 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006258
Denys Vlasenko10bde142018-12-27 18:23:58 +01006259 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006260 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006261 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006262#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006263 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006264 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006265 }
6266
Denys Vlasenko10bde142018-12-27 18:23:58 +01006267 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006268
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006269 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006270}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006271#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006272
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006273#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006274static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006275{
6276 BcStatus s;
6277 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006278 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006279
Denys Vlasenko10bde142018-12-27 18:23:58 +01006280 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006281 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006282
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006283 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284 bc_num_init(&res2.d.n, n2->len);
6285
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006286 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006287 if (s) goto err;
6288
Denys Vlasenko10bde142018-12-27 18:23:58 +01006289 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006290 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006291 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006292
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006293 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006294 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006295 bc_num_free(&res2.d.n);
6296 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006297 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006298}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006299#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenkofa210792018-12-19 19:35:40 +01006301static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006302{
6303 BcStatus s;
6304 BcResult *r1, *r2, *r3, res;
6305 BcNum *n1, *n2, *n3;
6306
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006307 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006308 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006309 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006310 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006311
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006312 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006313 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006314 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006315 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006316 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006317
6318 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006319 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006320 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006321 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006322 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006323 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006324 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006325 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006326 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327 }
6328 }
6329
6330 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006331 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006332 if (s) goto err;
6333
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006334 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006335 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006337 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006338 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006339 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006340 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006342#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006343
Denys Vlasenkofa210792018-12-19 19:35:40 +01006344static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006345{
Gavin Howard01055ba2018-11-03 11:00:21 -06006346 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006347 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006348
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006349 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006350
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006351 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006352 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006353 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006354}
6355
Denys Vlasenkofa210792018-12-19 19:35:40 +01006356static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006357{
6358 BcStatus s;
6359 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006360 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006361 char **strs;
6362 char *str;
6363 char c;
6364 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006365
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006366 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006367 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006368
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006369 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006370 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006371 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006372
6373 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006374 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006375 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006376 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006377
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006378 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006379 bc_num_copy(&n, num);
6380 bc_num_truncate(&n, n.rdx);
6381
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006382 strmb.cap = ARRAY_SIZE(strmb_digs);
6383 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006384 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006385
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006386 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006387 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006388 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006389 if (s) goto num_err;
6390
6391 c = (char) val;
6392
6393 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006394 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006395 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006396 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006397 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006398 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 }
6400
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006401 strs = (void*)G.prog.strs.v;
6402 for (idx = 0; idx < G.prog.strs.len; idx++) {
6403 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6404 goto dup;
6405 }
6406 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006407 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006408 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006409 //str[1] = '\0'; - already is
Brian Foleyb64470b2019-09-05 10:50:13 +02006410 idx = bc_vec_push(&G.prog.strs, &str);
6411 // Add an empty function so that if zdc_program_execStr ever needs to
6412 // parse the string into code (from the 'x' command) there's somewhere
6413 // to store the bytecode.
6414 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006415 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006416 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006417 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006418 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006419
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006420 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006421 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006422 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006423 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006424}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006425#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006426
Denys Vlasenkofa210792018-12-19 19:35:40 +01006427static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006428{
6429 BcStatus s;
6430 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006431 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006432 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006433
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006434 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006435 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006436 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006437
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006438 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006439 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006440
Denys Vlasenko57734c92018-12-17 21:14:05 +01006441 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006442 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006443 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006444 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006445 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006446 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006447 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448 }
6449
Denys Vlasenko29301232018-12-11 15:29:32 +01006450 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006451}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006452#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006453
Denys Vlasenkofa210792018-12-19 19:35:40 +01006454static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006455{
6456 BcStatus s;
6457 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006458 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006459 unsigned long val;
6460
Denys Vlasenko10bde142018-12-27 18:23:58 +01006461 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006462 if (s) RETURN_STATUS(s);
6463 s = zbc_num_ulong(num, &val);
6464 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006465
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006466 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006467
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006468 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006469 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006470 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006471 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006472 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006473
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006474 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006475
Denys Vlasenko29301232018-12-11 15:29:32 +01006476 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006478#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006479
Denys Vlasenkofa210792018-12-19 19:35:40 +01006480static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006481{
6482 BcStatus s = BC_STATUS_SUCCESS;
6483 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006484 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006485 BcInstPtr ip;
6486 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006487
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006488 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006489 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006490
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006491 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006492
6493 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006494 BcNum *n = n; // for compiler
6495 bool exec;
6496 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006497 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006498 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006499
Denys Vlasenkof706a182018-12-26 20:02:09 +01006500 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006501 (*bgn) += 1;
6502 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006503 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006504
6505 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006506 name = then_name;
6507 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006508 exec = true;
6509 name = else_name;
6510 }
6511
6512 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006513 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006514 v = xc_program_search(name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 n = bc_vec_top(v);
6516 }
6517
6518 free(then_name);
6519 free(else_name);
6520
6521 if (!exec) goto exit;
6522 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006523 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006524 goto exit;
6525 }
6526
6527 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006528 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006529 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006530 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006531 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006532 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006533 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006534 if (s || !BC_PROG_STR(n)) goto exit;
6535 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006536 } else
Brian Foley74548792019-09-05 10:46:22 +02006537 goto exit_nopop;
Gavin Howard01055ba2018-11-03 11:00:21 -06006538 }
6539
6540 fidx = sidx + BC_PROG_REQ_FUNCS;
6541
Denys Vlasenko10bde142018-12-27 18:23:58 +01006542 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006543
6544 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006545 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006546 char *str;
6547
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006548 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006549 xc_parse_create(fidx);
6550 str = *xc_program_str(sidx);
6551 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006552 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006553
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006554 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006555 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006556 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006557 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006558 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006559 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006560 G.prs = sv_parse; // struct copy
6561 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006562 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006563 bc_vec_pop_all(&f->code);
6564 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006565 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006566 }
6567
Denys Vlasenko24e41942018-12-21 23:01:26 +01006568 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 ip.func = fidx;
6570
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006571 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006572 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006573
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006574 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006575 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006576 bc_vec_pop(&G.prog.results);
Brian Foley74548792019-09-05 10:46:22 +02006577 exit_nopop:
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006578 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006579}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006580#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006581#endif // ENABLE_DC
6582
Denys Vlasenko10bde142018-12-27 18:23:58 +01006583static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006584{
Gavin Howard01055ba2018-11-03 11:00:21 -06006585 BcResult res;
6586 unsigned long val;
6587
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006588 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006589 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006590 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006591 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006592 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006594 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006595
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006596 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006597 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006598 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006599}
6600
Denys Vlasenko10bde142018-12-27 18:23:58 +01006601static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006602{
Gavin Howard01055ba2018-11-03 11:00:21 -06006603 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006604 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006605 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006606 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006607
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006608 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006609 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6610 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006611 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006612 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006613
Denys Vlasenko24e41942018-12-21 23:01:26 +01006614 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 -06006615 switch (inst) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006616 case XC_INST_RET:
6617 if (IS_DC) { // end of '?' reached
6618 bc_vec_pop(&G.prog.exestack);
6619 goto read_updated_ip;
6620 }
6621 // bc: fall through
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006622#if ENABLE_BC
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006623 case BC_INST_RET0:
6624 dbg_exec("BC_INST_RET[0]:");
6625 s = zbc_program_return(inst);
6626 goto read_updated_ip;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006627 case BC_INST_JUMP_ZERO: {
6628 BcNum *num;
6629 bool zero;
6630 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006631 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006632 if (s) RETURN_STATUS(s);
6633 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6634 bc_vec_pop(&G.prog.results);
6635 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006636 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006637 break;
6638 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006639 // else: fall through
6640 }
6641 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006642 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006643 size_t *addr = bc_vec_item(&func->labels, idx);
6644 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6645 ip->inst_idx = *addr;
6646 break;
6647 }
6648 case BC_INST_CALL:
6649 dbg_exec("BC_INST_CALL:");
6650 s = zbc_program_call(code, &ip->inst_idx);
6651 goto read_updated_ip;
6652 case BC_INST_INC_PRE:
6653 case BC_INST_DEC_PRE:
6654 case BC_INST_INC_POST:
6655 case BC_INST_DEC_POST:
6656 dbg_exec("BC_INST_INCDEC:");
6657 s = zbc_program_incdec(inst);
6658 break;
6659 case BC_INST_HALT:
6660 dbg_exec("BC_INST_HALT:");
6661 QUIT_OR_RETURN_TO_MAIN;
6662 break;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006663 case XC_INST_BOOL_OR:
6664 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006665#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006666 case XC_INST_REL_EQ:
6667 case XC_INST_REL_LE:
6668 case XC_INST_REL_GE:
6669 case XC_INST_REL_NE:
6670 case XC_INST_REL_LT:
6671 case XC_INST_REL_GT:
6672 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006673 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006674 break;
6675 case XC_INST_READ:
6676 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006677 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006678 goto read_updated_ip;
6679 case XC_INST_VAR:
6680 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006681 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006682 break;
6683 case XC_INST_ARRAY_ELEM:
6684 case XC_INST_ARRAY:
6685 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6686 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6687 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006688#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006689 case BC_INST_LAST:
6690 dbg_exec("BC_INST_LAST:");
6691 r.t = BC_RESULT_LAST;
6692 bc_vec_push(&G.prog.results, &r);
6693 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006694#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006695 case XC_INST_IBASE:
6696 case XC_INST_OBASE:
6697 case XC_INST_SCALE:
6698 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006699 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006700 break;
6701 case XC_INST_SCALE_FUNC:
6702 case XC_INST_LENGTH:
6703 case XC_INST_SQRT:
6704 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006705 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006706 break;
6707 case XC_INST_NUM:
6708 dbg_exec("XC_INST_NUM:");
6709 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006710 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006711 bc_vec_push(&G.prog.results, &r);
6712 break;
6713 case XC_INST_POP:
6714 dbg_exec("XC_INST_POP:");
6715 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6716 s = bc_error_stack_has_too_few_elements();
6717 else
6718 bc_vec_pop(&G.prog.results);
6719 break;
6720 case XC_INST_PRINT:
6721 case XC_INST_PRINT_POP:
6722 case XC_INST_PRINT_STR:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006723 dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006724 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006725 break;
6726 case XC_INST_STR:
6727 dbg_exec("XC_INST_STR:");
6728 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006729 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006730 bc_vec_push(&G.prog.results, &r);
6731 break;
6732 case XC_INST_POWER:
6733 case XC_INST_MULTIPLY:
6734 case XC_INST_DIVIDE:
6735 case XC_INST_MODULUS:
6736 case XC_INST_PLUS:
6737 case XC_INST_MINUS:
6738 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006739 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006740 break;
6741 case XC_INST_BOOL_NOT: {
6742 BcNum *num;
6743 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006744 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006745 if (s) RETURN_STATUS(s);
6746 bc_num_init_DEF_SIZE(&r.d.n);
6747 if (bc_num_cmp(num, &G.prog.zero) == 0)
6748 bc_num_one(&r.d.n);
6749 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006750 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006751 break;
6752 }
6753 case XC_INST_NEG:
6754 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006755 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006756 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006757#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006758 case BC_INST_ASSIGN_POWER:
6759 case BC_INST_ASSIGN_MULTIPLY:
6760 case BC_INST_ASSIGN_DIVIDE:
6761 case BC_INST_ASSIGN_MODULUS:
6762 case BC_INST_ASSIGN_PLUS:
6763 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006764#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006765 case XC_INST_ASSIGN:
6766 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006767 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006768 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006769#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006770 case DC_INST_POP_EXEC:
6771 dbg_exec("DC_INST_POP_EXEC:");
6772 bc_vec_pop(&G.prog.exestack);
6773 goto read_updated_ip;
6774 case DC_INST_MODEXP:
6775 dbg_exec("DC_INST_MODEXP:");
6776 s = zdc_program_modexp();
6777 break;
6778 case DC_INST_DIVMOD:
6779 dbg_exec("DC_INST_DIVMOD:");
6780 s = zdc_program_divmod();
6781 break;
6782 case DC_INST_EXECUTE:
6783 case DC_INST_EXEC_COND:
6784 dbg_exec("DC_INST_EXEC[_COND]:");
6785 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6786 goto read_updated_ip;
6787 case DC_INST_PRINT_STACK: {
6788 size_t idx;
6789 dbg_exec("DC_INST_PRINT_STACK:");
6790 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006791 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006792 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006793 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006794 break;
6795 }
6796 case DC_INST_CLEAR_STACK:
6797 dbg_exec("DC_INST_CLEAR_STACK:");
6798 bc_vec_pop_all(&G.prog.results);
6799 break;
6800 case DC_INST_STACK_LEN:
6801 dbg_exec("DC_INST_STACK_LEN:");
6802 dc_program_stackLen();
6803 break;
6804 case DC_INST_DUPLICATE:
6805 dbg_exec("DC_INST_DUPLICATE:");
6806 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6807 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6808 ptr = bc_vec_top(&G.prog.results);
6809 dc_result_copy(&r, ptr);
6810 bc_vec_push(&G.prog.results, &r);
6811 break;
6812 case DC_INST_SWAP: {
6813 BcResult *ptr2;
6814 dbg_exec("DC_INST_SWAP:");
6815 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6816 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6817 ptr = bc_vec_item_rev(&G.prog.results, 0);
6818 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6819 memcpy(&r, ptr, sizeof(BcResult));
6820 memcpy(ptr, ptr2, sizeof(BcResult));
6821 memcpy(ptr2, &r, sizeof(BcResult));
6822 break;
6823 }
6824 case DC_INST_ASCIIFY:
6825 dbg_exec("DC_INST_ASCIIFY:");
6826 s = zdc_program_asciify();
6827 break;
6828 case DC_INST_PRINT_STREAM:
6829 dbg_exec("DC_INST_PRINT_STREAM:");
6830 s = zdc_program_printStream();
6831 break;
6832 case DC_INST_LOAD:
6833 case DC_INST_PUSH_VAR: {
6834 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006835 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006836 break;
6837 }
6838 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006839 char *name = xc_program_name(code, &ip->inst_idx);
Denys Vlasenko53799502019-01-25 14:24:03 +01006840 s = zxc_program_popResultAndCopyToVar(name, BC_TYPE_VAR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006841 free(name);
6842 break;
6843 }
6844 case DC_INST_QUIT:
6845 dbg_exec("DC_INST_QUIT:");
6846 if (G.prog.exestack.len <= 2)
6847 QUIT_OR_RETURN_TO_MAIN;
6848 bc_vec_npop(&G.prog.exestack, 2);
6849 goto read_updated_ip;
6850 case DC_INST_NQUIT:
6851 dbg_exec("DC_INST_NQUIT:");
6852 s = zdc_program_nquit();
6853 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006854#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006855 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006856 // Instruction stack has changed, read new pointers
6857 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006858 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006859 code = func->code.v;
6860 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006861 }
6862
Denys Vlasenkod38af482018-12-04 19:11:02 +01006863 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006864 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006865 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006866 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006867
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006868 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006869 }
6870
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006871 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006872}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006873#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006874
Denys Vlasenko10bde142018-12-27 18:23:58 +01006875static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006876{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006877 char *lenv;
6878 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006879
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006880 lenv = getenv(var);
6881 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006882 if (!lenv) return len;
6883
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006884 len = bb_strtou(lenv, NULL, 10) - 1;
6885 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006886 len = BC_NUM_PRINT_WIDTH;
6887
6888 return len;
6889}
6890
Denys Vlasenko10bde142018-12-27 18:23:58 +01006891static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006892{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006893 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006894
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006895 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006896 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006897 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006898
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006899 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006900 BcInstPtr *ip;
6901 BcFunc *f;
6902
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006903 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006904 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006905#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006906 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006907 if (s) goto err;
6908
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006909 // Check that next token is a correct stmt delimiter -
6910 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006911 if (G.prs.lex != BC_LEX_SCOLON
6912 && G.prs.lex != XC_LEX_NLINE
6913 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006914 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01006915 bc_error_at("bad statement terminator");
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006916 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006917 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006918 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006919 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006920#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006921 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006922#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006923 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006924#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006925 }
6926 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006927 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006928 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006929 RETURN_STATUS(BC_STATUS_FAILURE);
6930 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006931
Denys Vlasenko39287e02018-12-22 02:23:08 +01006932 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006933 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006934 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006935 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006936 break;
6937 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006938
6939 ip = (void*)G.prog.exestack.v;
6940#if SANITY_CHECKS
6941 if (G.prog.exestack.len != 1) // should have only main's IP
James Byrne69374872019-07-02 11:35:03 +02006942 bb_simple_error_msg_and_die("BUG:call stack");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006943 if (ip->func != BC_PROG_MAIN)
James Byrne69374872019-07-02 11:35:03 +02006944 bb_simple_error_msg_and_die("BUG:not MAIN");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006945#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006946 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006947 // bc discards strings, constants and code after each
6948 // top-level statement in the "main program".
6949 // This prevents "yes 1 | bc" from growing its memory
6950 // without bound. This can be done because data stack
6951 // is empty and thus can't hold any references to
6952 // strings or constants, there is no generated code
6953 // which can hold references (after we discard one
6954 // we just executed). Code of functions can have references,
6955 // but bc stores function strings/constants in per-function
6956 // storage.
6957 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006958#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006959 if (G.prog.results.len != 0) // should be empty
James Byrne69374872019-07-02 11:35:03 +02006960 bb_simple_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006961#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006962 IF_BC(bc_vec_pop_all(&f->strs);)
6963 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01006964 // We are at SCOLON/NLINE, skip it:
6965 s = zxc_lex_next();
6966 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006967 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006968 if (G.prog.results.len == 0
6969 && G.prog.vars.len == 0
6970 ) {
6971 // If stack is empty and no registers exist (TODO: or they are all empty),
6972 // we can get rid of accumulated strings and constants.
6973 // In this example dc process should not grow
6974 // its memory consumption with time:
6975 // yes 1pc | dc
6976 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6977 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6978 }
6979 // The code is discarded always (below), thus this example
6980 // should also not grow its memory consumption with time,
6981 // even though its data stack is not empty:
6982 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006983 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006984 // We drop generated and executed code for both bc and dc:
6985 bc_vec_pop_all(&f->code);
6986 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006987 }
Denys Vlasenko6842c602019-01-04 05:41:47 +01006988
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006989 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006990 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006991}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006992#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006993
Denys Vlasenko10bde142018-12-27 18:23:58 +01006994static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006995{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006996 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006997 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006998 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006999 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007000
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01007001 G.prs.lex_filename = filename;
7002 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01007003 G.err_line = G.prs.lex_line = 1;
Denys Vlasenko22314682019-01-01 21:50:14 +01007004 dbg_lex("p->lex_line reset to 1");
Gavin Howard01055ba2018-11-03 11:00:21 -06007005
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007006 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007007 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007008 // We do not stop looping on errors here if reading stdin.
7009 // Example: start interactive bc and enter "return".
7010 // It should say "'return' not in a function"
7011 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01007012 } while (G.prs.lex_input_fp == stdin);
7013 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007014 RETURN_STATUS(s);
7015}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007016#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007017
Denys Vlasenko10bde142018-12-27 18:23:58 +01007018static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007019{
7020 BcStatus s;
7021 FILE *fp;
7022
7023 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007024 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007025 fclose(fp);
7026
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007027 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007028}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007029#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007030
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007031#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01007032static void bc_vm_info(void)
7033{
7034 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07007035 "Adapted from https://github.com/gavinhoward/bc\n"
7036 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01007037 , applet_name);
7038}
7039
7040static void bc_args(char **argv)
7041{
7042 unsigned opts;
7043 int i;
7044
7045 GETOPT_RESET();
7046#if ENABLE_FEATURE_BC_LONG_OPTIONS
7047 opts = option_mask32 |= getopt32long(argv, "wvsqli",
7048 "warn\0" No_argument "w"
7049 "version\0" No_argument "v"
7050 "standard\0" No_argument "s"
7051 "quiet\0" No_argument "q"
7052 "mathlib\0" No_argument "l"
7053 "interactive\0" No_argument "i"
7054 );
7055#else
7056 opts = option_mask32 |= getopt32(argv, "wvsqli");
7057#endif
7058 if (getenv("POSIXLY_CORRECT"))
7059 option_mask32 |= BC_FLAG_S;
7060
7061 if (opts & BC_FLAG_V) {
7062 bc_vm_info();
7063 exit(0);
7064 }
7065
7066 for (i = optind; argv[i]; ++i)
7067 bc_vec_push(&G.files, argv + i);
7068}
7069
7070static void bc_vm_envArgs(void)
7071{
7072 BcVec v;
7073 char *buf;
7074 char *env_args = getenv("BC_ENV_ARGS");
7075
7076 if (!env_args) return;
7077
7078 G.env_args = xstrdup(env_args);
7079 buf = G.env_args;
7080
7081 bc_vec_init(&v, sizeof(char *), NULL);
7082
7083 while (*(buf = skip_whitespace(buf)) != '\0') {
7084 bc_vec_push(&v, &buf);
7085 buf = skip_non_whitespace(buf);
7086 if (!*buf)
7087 break;
7088 *buf++ = '\0';
7089 }
7090
7091 // NULL terminate, and pass argv[] so that first arg is argv[1]
7092 if (sizeof(int) == sizeof(char*)) {
7093 bc_vec_push(&v, &const_int_0);
7094 } else {
7095 static char *const nullptr = NULL;
7096 bc_vec_push(&v, &nullptr);
7097 }
7098 bc_args(((char **)v.v) - 1);
7099
7100 bc_vec_free(&v);
7101}
7102
7103static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007104 "scale=20"
7105"\n" "define e(x){"
7106"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007107////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7108//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7109//e(-.998896): GNU:.36828580434569428695
7110// above code:.36828580434569428696
7111// actual value:.3682858043456942869594...
7112// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007113"\n" "b=ibase"
7114"\n" "ibase=A"
7115"\n" "if(x<0){"
7116"\n" "n=1"
7117"\n" "x=-x"
7118"\n" "}"
7119"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007120"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007121"\n" "scale=scale(x)+1"
7122"\n" "while(x>1){"
7123"\n" "d+=1"
7124"\n" "x/=2"
7125"\n" "scale+=1"
7126"\n" "}"
7127"\n" "scale=r"
7128"\n" "r=x+1"
7129"\n" "p=x"
7130"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007131"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007132"\n" "p*=x"
7133"\n" "f*=i"
7134"\n" "v=p/f"
7135"\n" "r+=v"
7136"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007137"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007138"\n" "scale=s"
7139"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007140"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007141"\n" "return(r/1)"
7142"\n" "}"
7143"\n" "define l(x){"
7144"\n" "auto b,s,r,p,a,q,i,v"
7145"\n" "b=ibase"
7146"\n" "ibase=A"
7147"\n" "if(x<=0){"
7148"\n" "r=(1-10^scale)/1"
7149"\n" "ibase=b"
7150"\n" "return(r)"
7151"\n" "}"
7152"\n" "s=scale"
7153"\n" "scale+=6"
7154"\n" "p=2"
7155"\n" "while(x>=2){"
7156"\n" "p*=2"
7157"\n" "x=sqrt(x)"
7158"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007159"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007160"\n" "p*=2"
7161"\n" "x=sqrt(x)"
7162"\n" "}"
7163"\n" "r=a=(x-1)/(x+1)"
7164"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007165"\n" "v=1"
7166"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007167"\n" "a*=q"
7168"\n" "v=a/i"
7169"\n" "r+=v"
7170"\n" "}"
7171"\n" "r*=p"
7172"\n" "scale=s"
7173"\n" "ibase=b"
7174"\n" "return(r/1)"
7175"\n" "}"
7176"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007177"\n" "auto b,s,r,a,q,i"
7178"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007179"\n" "b=ibase"
7180"\n" "ibase=A"
7181"\n" "s=scale"
7182"\n" "scale=1.1*s+2"
7183"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007184"\n" "scale=0"
7185"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007186"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007187"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007188"\n" "scale=s+2"
7189"\n" "r=a=x"
7190"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007191"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007192"\n" "a*=q/(i*(i-1))"
7193"\n" "r+=a"
7194"\n" "}"
7195"\n" "scale=s"
7196"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007197"\n" "return(r/1)"
7198"\n" "}"
7199"\n" "define c(x){"
7200"\n" "auto b,s"
7201"\n" "b=ibase"
7202"\n" "ibase=A"
7203"\n" "s=scale"
7204"\n" "scale*=1.2"
7205"\n" "x=s(2*a(1)+x)"
7206"\n" "scale=s"
7207"\n" "ibase=b"
7208"\n" "return(x/1)"
7209"\n" "}"
7210"\n" "define a(x){"
7211"\n" "auto b,s,r,n,a,m,t,f,i,u"
7212"\n" "b=ibase"
7213"\n" "ibase=A"
7214"\n" "n=1"
7215"\n" "if(x<0){"
7216"\n" "n=-1"
7217"\n" "x=-x"
7218"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007219"\n" "if(scale<65){"
7220"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7221"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007222"\n" "}"
7223"\n" "s=scale"
7224"\n" "if(x>.2){"
7225"\n" "scale+=5"
7226"\n" "a=a(.2)"
7227"\n" "}"
7228"\n" "scale=s+3"
7229"\n" "while(x>.2){"
7230"\n" "m+=1"
7231"\n" "x=(x-.2)/(1+.2*x)"
7232"\n" "}"
7233"\n" "r=u=x"
7234"\n" "f=-x*x"
7235"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007236"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007237"\n" "u*=f"
7238"\n" "t=u/i"
7239"\n" "r+=t"
7240"\n" "}"
7241"\n" "scale=s"
7242"\n" "ibase=b"
7243"\n" "return((m*a+r)/n)"
7244"\n" "}"
7245"\n" "define j(n,x){"
7246"\n" "auto b,s,o,a,i,v,f"
7247"\n" "b=ibase"
7248"\n" "ibase=A"
7249"\n" "s=scale"
7250"\n" "scale=0"
7251"\n" "n/=1"
7252"\n" "if(n<0){"
7253"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007254"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007255"\n" "}"
7256"\n" "a=1"
7257"\n" "for(i=2;i<=n;++i)a*=i"
7258"\n" "scale=1.5*s"
7259"\n" "a=(x^n)/2^n/a"
7260"\n" "r=v=1"
7261"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007262"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007263"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007264"\n" "v=v*f/i/(n+i)"
7265"\n" "r+=v"
7266"\n" "}"
7267"\n" "scale=s"
7268"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007269"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007270"\n" "return(a*r/1)"
7271"\n" "}"
7272};
7273#endif // ENABLE_BC
7274
Denys Vlasenko10bde142018-12-27 18:23:58 +01007275static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007276{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007277 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007278 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007279 size_t i;
7280
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007281#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007282 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007283 // We know that internal library is not buggy,
7284 // thus error checking is normally disabled.
7285# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007286 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007287 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007288 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007289#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007290
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007291 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007292 fname = (void*)G.files.v;
7293 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007294 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007295 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7296 // Debug config, non-interactive mode:
7297 // return all the way back to main.
7298 // Non-debug builds do not come here
7299 // in non-interactive mode, they exit.
7300 RETURN_STATUS(s);
7301 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007302 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007303
Denys Vlasenko91cde952018-12-10 20:56:08 +01007304 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007305 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007306
Denys Vlasenko19f11072018-12-12 22:48:19 +01007307 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007308}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007309#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007310
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007311#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007312static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007313{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007314 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007315 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007316 bc_vec_free(&G.prog.vars);
7317 bc_vec_free(&G.prog.var_map);
7318 bc_vec_free(&G.prog.arrs);
7319 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007320 IF_DC(bc_vec_free(&G.prog.strs);)
7321 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007322 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007323 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007324 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007325 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007326 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007327 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007328}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007329#endif
7330
Denys Vlasenko10bde142018-12-27 18:23:58 +01007331static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007332{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007333 BcInstPtr ip;
7334
Denys Vlasenko82269122018-12-14 16:30:56 +01007335 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007336 memset(&ip, 0, sizeof(BcInstPtr));
7337
Denys Vlasenko82269122018-12-14 16:30:56 +01007338 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007339 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007340 G.prog.ob_t = 10;
7341
Denys Vlasenko503faf92018-12-20 16:24:18 +01007342 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7343 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007344
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007345 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007346 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007347
Denys Vlasenko503faf92018-12-20 16:24:18 +01007348 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7349 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007350
7351 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007352 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007353
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007354 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007355 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007356 // a valid function name (and be short)
7357 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007358 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007359 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007360 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007361 xc_program_add_fn();
7362 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007363 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007364
7365 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007366 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007367
7368 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007369 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007370
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007371 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7372 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007373 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007374 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7375 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007376
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007377 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007378}
Gavin Howard01055ba2018-11-03 11:00:21 -06007379
Denys Vlasenko10bde142018-12-27 18:23:58 +01007380static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007381{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007382 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007383#if ENABLE_FEATURE_EDITING
7384 G.line_input_state = new_line_input_t(DO_HISTORY);
7385#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007386 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007387
Denys Vlasenko10bde142018-12-27 18:23:58 +01007388 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007389 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007390 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007391
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007392//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7393//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007394 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007395#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007396 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007397 // With SA_RESTART, most system calls will restart
7398 // (IOW: they won't fail with EINTR).
7399 // In particular, this means ^C won't cause
7400 // stdout to get into "error state" if SIGINT hits
7401 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007402 //
7403 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007404 // will only be handled after [Enter] since read()
7405 // from stdin is not interrupted by ^C either,
7406 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007407 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007408 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7409
7410 // Without SA_RESTART, this exhibits a bug:
7411 // "while (1) print 1" and try ^C-ing it.
7412 // Intermittently, instead of returning to input line,
7413 // you'll get "output error: Interrupted system call"
7414 // and exit.
7415 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007416#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007417 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007418 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007419 return 0; // "not a tty"
7420}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007421
Denys Vlasenko10bde142018-12-27 18:23:58 +01007422static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007423{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007424 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007425#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007426 if (G_exiting) // it was actually "halt" or "quit"
7427 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007428
7429 bc_vec_free(&G.files);
7430 xc_program_free();
7431 xc_parse_free();
7432 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007433# if ENABLE_FEATURE_EDITING
7434 free_line_input_t(G.line_input_state);
7435# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007436 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007437#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007438 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007439 return st;
7440}
7441
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007442#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007443int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007444int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007445{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007446 int is_tty;
7447
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007448 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007449
Denys Vlasenko10bde142018-12-27 18:23:58 +01007450 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007451
7452 bc_args(argv);
7453
7454 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7455 bc_vm_info();
7456
Denys Vlasenko10bde142018-12-27 18:23:58 +01007457 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007458}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007459#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007460
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007461#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007462int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007463int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007464{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007465 int noscript;
7466
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007467 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007468
7469 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7470 // 1 char wider than bc from the same package.
7471 // Both default width, and xC_LINE_LENGTH=N are wider:
7472 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007473 // |1234\ |
7474 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007475 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007476 // |123\ |
7477 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007478 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007479 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007480
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007481 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7482 noscript = BC_FLAG_I;
7483 for (;;) {
7484 int n = getopt(argc, argv, "e:f:x");
7485 if (n <= 0)
7486 break;
7487 switch (n) {
7488 case 'e':
7489 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007490 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007491 if (n) return n;
7492 break;
7493 case 'f':
7494 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007495 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007496 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007497 break;
7498 case 'x':
7499 option_mask32 |= DC_FLAG_X;
7500 break;
7501 default:
7502 bb_show_usage();
7503 }
7504 }
7505 argv += optind;
7506
7507 while (*argv) {
7508 noscript = 0;
7509 bc_vec_push(&G.files, argv++);
7510 }
7511
7512 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7513
Denys Vlasenko10bde142018-12-27 18:23:58 +01007514 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007515}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007516#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007517
Denys Vlasenko14767602018-12-27 22:52:13 +01007518#endif // DC_BIG