blob: 930baaaaa02754b0b9c1b5c195b3983d662ad549 [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 Vlasenkoba9f9c22021-04-14 15:15:45 +0200111//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 Vlasenkoba9f9c22021-04-14 15:15:45 +0200118//usage: "\n -l Load standard library"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100119//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 Vlasenko3d88cc12021-02-26 13:26:48 +0100141//usage: "\nArithmetic: + - * / % ^"
142//usage: IF_FEATURE_DC_BIG(
143//usage: "\n~ - divide with remainder"
144//usage: "\n| - modular exponentiation"
145//usage: "\nv - square root"
Denys Vlasenko90377572021-02-26 17:11:55 +0100146//////// "\nA-F - digits 10..15
147//////// "\n_NNN - push negative number -NNN
148//////// "\n[string] - push string (in FreeBSD, \[, \] and \\ are escapes, not implemented here and in GNU)
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100149//////// "\nR - DC_LEX_POP pop and discard
150//////// "\nc - DC_LEX_CLEAR_STACK clear stack
Denys Vlasenko90377572021-02-26 17:11:55 +0100151//////// "\nd - DC_LEX_DUPLICATE duplicate top-of-stack
152//////// "\nr - DC_LEX_SWAP swap top-of-stack
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100153//////// "\n:r - DC_LEX_COLON pop index, pop value, store to array 'r'
154//////// "\n;r - DC_LEX_SCOLON pop index, fetch from array 'r', push
Denys Vlasenko9b6bcfd2021-02-26 21:20:18 +0100155//////// "\nLr - DC_LEX_LOAD_POP pop register 'r', push
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100156//////// "\nSr - DC_LEX_STORE_PUSH pop, push to register 'r'
157//////// "\nlr - DC_LEX_LOAD read register 'r', push
158//////// "\nsr - DC_LEX_OP_ASSIGN pop, assign to register 'r'
159//////// "\n? - DC_LEX_READ read line and execute
160//////// "\nx - DC_LEX_EXECUTE pop string and execute
Denys Vlasenko90377572021-02-26 17:11:55 +0100161//////// "\n<r - XC_LEX_OP_REL_GT pop, pop, execute register 'r' if top-of-stack was less
162//////// "\n>r - XC_LEX_OP_REL_LT pop, pop, execute register 'r' if top-of-stack was greater
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100163//////// "\n=r - XC_LEX_OP_REL_EQ pop, pop, execute register 'r' if equal
Denys Vlasenko90377572021-02-26 17:11:55 +0100164//////// "\n !<r !>r !=r - negated forms
165//////// "\n >tef - "if greater execute register 't' else execute 'f'"
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100166//////// "\nQ - DC_LEX_NQUIT pop, "break N" from macro invocations
167//////// "\nq - DC_LEX_QUIT "break 2" (if less than 2 levels of macros, exit dc)
168//////// "\nX - DC_LEX_SCALE_FACTOR pop, push number of fractional digits
169//////// "\nZ - DC_LEX_LENGTH pop, push number of digits it has (or number of characters in string)
Denys Vlasenko1a37aa72021-02-26 14:48:04 +0100170//////// "\na - DC_LEX_ASCIIFY pop, push low-order byte as char or 1st char of string
Denys Vlasenko90377572021-02-26 17:11:55 +0100171//////// "\n( - DC_LEX_LPAREN (FreeBSD, not in GNU) pop, pop, if top-of-stack was less push 1 else push 0
172//////// "\n{ - DC_LEX_LBRACE (FreeBSD, not in GNU) pop, pop, if top-of-stack was less-or-equal push 1 else push 0
173//////// "\nG - DC_LEX_EQ_NO_REG (FreeBSD, not in GNU) pop, pop, if equal push 1 else push 0
174//////// "\nN - DC_LEX_OP_BOOL_NOT (FreeBSD, not in GNU) pop, if 0 push 1 else push 0
175//////// FreeBSD also has J and M commands, used internally by bc
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100176//////// "\nn - DC_LEX_PRINT_POP pop, print without newline
177//////// "\nP - DC_LEX_PRINT_STREAM pop, print string or hex bytes
178//usage: )
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100179//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100180//usage: "\nf - print entire stack"
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100181//////// "\nz - DC_LEX_STACK_LEVEL push stack depth
182//////// "\nK - DC_LEX_SCALE push precision
183//////// "\nI - DC_LEX_IBASE push input radix
184//////// "\nO - DC_LEX_OBASE push output radix
185//usage: IF_FEATURE_DC_BIG(
186//usage: "\nk - pop the value and set precision"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100187//usage: "\ni - pop the value and set input radix"
Denys Vlasenko3d88cc12021-02-26 13:26:48 +0100188//usage: )
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100189//usage: "\no - pop the value and set output radix"
190//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600191//usage:
192//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100193//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600194//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100195//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600196//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100197//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600198//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100199//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600200//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100201//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600202//usage: "64\n"
203
204#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100205#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600206
Denys Vlasenko14767602018-12-27 22:52:13 +0100207#if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100208# include "dc.c"
209#else
210
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100211#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100212static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100213#define dbg_lex(...) \
214 do { \
215 fprintf(stderr, "%*s", lex_indent, ""); \
216 bb_error_msg(__VA_ARGS__); \
217 } while (0)
218#define dbg_lex_enter(...) \
219 do { \
220 dbg_lex(__VA_ARGS__); \
221 lex_indent++; \
222 } while (0)
223#define dbg_lex_done(...) \
224 do { \
225 lex_indent--; \
226 dbg_lex(__VA_ARGS__); \
227 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100228#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100229# define dbg_lex(...) ((void)0)
230# define dbg_lex_enter(...) ((void)0)
231# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100232#endif
233
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100234#if DEBUG_COMPILE
235# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
236#else
237# define dbg_compile(...) ((void)0)
238#endif
239
Denys Vlasenko99b37622018-12-15 20:06:59 +0100240#if DEBUG_EXEC
241# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
242#else
243# define dbg_exec(...) ((void)0)
244#endif
245
Gavin Howard01055ba2018-11-03 11:00:21 -0600246typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100247 BC_STATUS_SUCCESS = 0,
248 BC_STATUS_FAILURE = 1,
Gavin Howard01055ba2018-11-03 11:00:21 -0600249} BcStatus;
250
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100251#define BC_VEC_INVALID_IDX ((size_t) -1)
252#define BC_VEC_START_CAP (1 << 5)
Gavin Howard01055ba2018-11-03 11:00:21 -0600253
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100254typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600255
256typedef struct BcVec {
257 char *v;
258 size_t len;
259 size_t cap;
260 size_t size;
261 BcVecFree dtor;
262} BcVec;
263
Gavin Howard01055ba2018-11-03 11:00:21 -0600264typedef signed char BcDig;
265
266typedef struct BcNum {
267 BcDig *restrict num;
268 size_t rdx;
269 size_t len;
270 size_t cap;
271 bool neg;
272} BcNum;
273
Denys Vlasenko680ccd32018-12-31 19:42:13 +0100274#define BC_NUM_MAX_IBASE 36
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100275// larger value might speed up BIGNUM calculations a bit:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100276#define BC_NUM_DEF_SIZE 16
Denys Vlasenko29a90432020-12-29 18:50:56 +0100277#define BC_NUM_PRINT_WIDTH 70
Gavin Howard01055ba2018-11-03 11:00:21 -0600278
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100279#define BC_NUM_KARATSUBA_LEN 32
Gavin Howard01055ba2018-11-03 11:00:21 -0600280
Gavin Howard01055ba2018-11-03 11:00:21 -0600281typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100282#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600283 BC_INST_INC_PRE,
284 BC_INST_DEC_PRE,
285 BC_INST_INC_POST,
286 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100287#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100288 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600289
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100290 XC_INST_REL_EQ, // should
291 XC_INST_REL_LE, // match
292 XC_INST_REL_GE, // LEX
293 XC_INST_REL_NE, // constants
294 XC_INST_REL_LT, // for
295 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600296
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100297 XC_INST_POWER, // operations
298 XC_INST_MULTIPLY, // |
299 XC_INST_DIVIDE, // |
300 XC_INST_MODULUS, // |
301 XC_INST_PLUS, // |
302 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600303
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100304 XC_INST_BOOL_NOT, // |
305 XC_INST_BOOL_OR, // |
306 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100307#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100308 BC_INST_ASSIGN_POWER, // |
309 BC_INST_ASSIGN_MULTIPLY,// |
310 BC_INST_ASSIGN_DIVIDE, // |
311 BC_INST_ASSIGN_MODULUS, // |
312 BC_INST_ASSIGN_PLUS, // |
313 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100314#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100315 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600316
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100317 XC_INST_NUM,
318 XC_INST_VAR,
319 XC_INST_ARRAY_ELEM,
320 XC_INST_ARRAY,
321 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100322
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100323 XC_INST_IBASE, // order of these constans should match other enums
324 XC_INST_OBASE, // order of these constans should match other enums
325 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100326 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100327 XC_INST_LENGTH,
328 XC_INST_READ,
329 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100331 XC_INST_PRINT,
332 XC_INST_PRINT_POP,
333 XC_INST_STR,
334 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600335
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100336#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100337 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600338 BC_INST_JUMP,
339 BC_INST_JUMP_ZERO,
340
341 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600342 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100343#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100344 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600345
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100346 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100347#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100348 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600349
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100350 DC_INST_MODEXP,
351 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600352
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100353 DC_INST_EXECUTE,
354 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600355
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100356 DC_INST_ASCIIFY,
357 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600358
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100359 DC_INST_PRINT_STACK,
360 DC_INST_CLEAR_STACK,
361 DC_INST_STACK_LEN,
362 DC_INST_DUPLICATE,
363 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600364
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100365 DC_INST_LOAD,
366 DC_INST_PUSH_VAR,
367 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600368
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100369 DC_INST_QUIT,
370 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600371
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100372 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100373#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600374} BcInst;
375
376typedef struct BcId {
377 char *name;
378 size_t idx;
379} BcId;
380
381typedef struct BcFunc {
382 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100383 IF_BC(BcVec labels;)
384 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100385 IF_BC(BcVec strs;)
386 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100387 IF_BC(size_t nparams;)
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100388 IF_BC(bool voidfunc;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600389} BcFunc;
390
391typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100392 XC_RESULT_TEMP,
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +0100393 IF_BC(BC_RESULT_VOID,) // same as TEMP, but INST_PRINT will ignore it
Gavin Howard01055ba2018-11-03 11:00:21 -0600394
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100395 XC_RESULT_VAR,
396 XC_RESULT_ARRAY_ELEM,
397 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600398
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100399 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600400
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100401 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
402 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
403 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
404 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100405 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100406 XC_RESULT_CONSTANT,
407 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600408} BcResultType;
409
410typedef union BcResultData {
411 BcNum n;
412 BcVec v;
413 BcId id;
414} BcResultData;
415
416typedef struct BcResult {
417 BcResultType t;
418 BcResultData d;
419} BcResult;
420
421typedef struct BcInstPtr {
422 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100423 size_t inst_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -0600424} BcInstPtr;
425
Denys Vlasenko53799502019-01-25 14:24:03 +0100426typedef enum BcType {
427 BC_TYPE_VAR,
428 BC_TYPE_ARRAY,
429 BC_TYPE_REF,
430} BcType;
431
Gavin Howard01055ba2018-11-03 11:00:21 -0600432typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100433 XC_LEX_EOF,
434 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600435
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100436 XC_LEX_NLINE,
437 XC_LEX_WHITESPACE,
438 XC_LEX_STR,
439 XC_LEX_NAME,
440 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100441
Denys Vlasenko69560f42018-12-24 14:14:23 +0100442 XC_LEX_1st_op,
443 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100444
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100445 XC_LEX_OP_REL_EQ, // should
446 XC_LEX_OP_REL_LE, // match
447 XC_LEX_OP_REL_GE, // INST
448 XC_LEX_OP_REL_NE, // constants
449 XC_LEX_OP_REL_LT, // for
450 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100451
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100452 XC_LEX_OP_POWER, // operations
453 XC_LEX_OP_MULTIPLY, // |
454 XC_LEX_OP_DIVIDE, // |
455 XC_LEX_OP_MODULUS, // |
456 XC_LEX_OP_PLUS, // |
457 XC_LEX_OP_MINUS, // |
458 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100459#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100460 BC_LEX_OP_BOOL_NOT, // |
461 BC_LEX_OP_BOOL_OR, // |
462 BC_LEX_OP_BOOL_AND, // |
463
464 BC_LEX_OP_ASSIGN_POWER, // |
465 BC_LEX_OP_ASSIGN_MULTIPLY, // |
466 BC_LEX_OP_ASSIGN_DIVIDE, // |
467 BC_LEX_OP_ASSIGN_MODULUS, // |
468 BC_LEX_OP_ASSIGN_PLUS, // |
469 BC_LEX_OP_ASSIGN_MINUS, // |
470
471 BC_LEX_OP_ASSIGN, // V
472
Gavin Howard01055ba2018-11-03 11:00:21 -0600473 BC_LEX_OP_INC,
474 BC_LEX_OP_DEC,
475
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100476 BC_LEX_LPAREN, // () are 0x28 and 0x29
477 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600478
Denys Vlasenko10bde142018-12-27 18:23:58 +0100479 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600480 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100481 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600482
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100483 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600484 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100485 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600486
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100487 BC_LEX_KEY_1st_keyword,
488 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600489 BC_LEX_KEY_BREAK,
490 BC_LEX_KEY_CONTINUE,
491 BC_LEX_KEY_DEFINE,
492 BC_LEX_KEY_ELSE,
493 BC_LEX_KEY_FOR,
494 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100495 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100496 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
497 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600498 BC_LEX_KEY_IF,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100499 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600500 BC_LEX_KEY_LENGTH,
501 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600502 BC_LEX_KEY_PRINT,
503 BC_LEX_KEY_QUIT,
504 BC_LEX_KEY_READ,
505 BC_LEX_KEY_RETURN,
506 BC_LEX_KEY_SCALE,
507 BC_LEX_KEY_SQRT,
508 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100509#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600510
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100511#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100512 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
513 DC_LEX_OP_ASSIGN,
514
515 DC_LEX_LPAREN,
516 DC_LEX_SCOLON,
517 DC_LEX_READ,
518 DC_LEX_IBASE,
519 DC_LEX_SCALE,
520 DC_LEX_OBASE,
521 DC_LEX_LENGTH,
522 DC_LEX_PRINT,
523 DC_LEX_QUIT,
524 DC_LEX_SQRT,
525 DC_LEX_LBRACE,
526
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100527 DC_LEX_EQ_NO_REG,
528 DC_LEX_OP_MODEXP,
529 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600530
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100531 DC_LEX_COLON,
532 DC_LEX_ELSE,
533 DC_LEX_EXECUTE,
534 DC_LEX_PRINT_STACK,
535 DC_LEX_CLEAR_STACK,
536 DC_LEX_STACK_LEVEL,
537 DC_LEX_DUPLICATE,
538 DC_LEX_SWAP,
539 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600540
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100541 DC_LEX_ASCIIFY,
542 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600543
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100544 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
545 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
546 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
547 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
548 DC_LEX_LOAD,
549 DC_LEX_LOAD_POP,
550 DC_LEX_STORE_PUSH,
551 DC_LEX_PRINT_POP,
552 DC_LEX_NQUIT,
553 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100554#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600555} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100556// must match order of BC_LEX_KEY_foo etc above
557#if ENABLE_BC
558struct BcLexKeyword {
559 char name8[8];
560};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100561#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100562 { .name8 = a /*, .posix = b */ }
Denys Vlasenko6cc49622020-11-30 14:58:02 +0100563static const struct BcLexKeyword bc_lex_kws[20] ALIGN8 = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100564 LEX_KW_ENTRY("auto" , 1), // 0
565 LEX_KW_ENTRY("break" , 1), // 1
566 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
567 LEX_KW_ENTRY("define" , 1), // 3
568 LEX_KW_ENTRY("else" , 0), // 4
569 LEX_KW_ENTRY("for" , 1), // 5
570 LEX_KW_ENTRY("halt" , 0), // 6
571 LEX_KW_ENTRY("ibase" , 1), // 7
572 LEX_KW_ENTRY("obase" , 1), // 8
573 LEX_KW_ENTRY("if" , 1), // 9
574 LEX_KW_ENTRY("last" , 0), // 10
575 LEX_KW_ENTRY("length" , 1), // 11
576 LEX_KW_ENTRY("limits" , 0), // 12
577 LEX_KW_ENTRY("print" , 0), // 13
578 LEX_KW_ENTRY("quit" , 1), // 14
579 LEX_KW_ENTRY("read" , 0), // 15
580 LEX_KW_ENTRY("return" , 1), // 16
581 LEX_KW_ENTRY("scale" , 1), // 17
582 LEX_KW_ENTRY("sqrt" , 1), // 18
583 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100584};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100585#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100586#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100587#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100588#define STRING_if (bc_lex_kws[9].name8)
589#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100590enum {
591 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100592 | (1 << 0) // 0
593 | (1 << 1) // 1
594 | (0 << 2) // 2
595 | (1 << 3) // 3
596 | (0 << 4) // 4
597 | (1 << 5) // 5
598 | (0 << 6) // 6
599 | (1 << 7) // 7
600 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100601 | (1 << 9) // 9
602 | (0 << 10) // 10
603 | (1 << 11) // 11
604 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100605 | (0 << 13) // 13
606 | (1 << 14) // 14
607 | (0 << 15) // 15
608 | (1 << 16) // 16
609 | (1 << 17) // 17
610 | (1 << 18) // 18
611 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100612};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100613#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614
615// This is a bit array that corresponds to token types. An entry is
616// true if the token is valid in an expression, false otherwise.
617// Used to figure out when expr parsing should stop *without error message*
618// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100619// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100620// by later processing.
621enum {
622#define EXBITS(a,b,c,d,e,f,g,h) \
623 ((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 +0100624 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100625 + (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 +0100626 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
627 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100628 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
629 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100630 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
631 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
632 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100633#undef EXBITS
634};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100635static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100636{
637#if ULONG_MAX > 0xffffffff
638 // 64-bit version (will not work correctly for 32-bit longs!)
639 return BC_PARSE_EXPRS_BITS & (1UL << i);
640#else
641 // 32-bit version
642 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
643 if (i >= 32) {
644 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
645 i &= 31;
646 }
647 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100648#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100649}
650
651// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100652// [XC_LEX_1st_op...] token types.
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100653static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100654#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100655 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100656 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 +0100657 OP(2, false), // pow
658 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
659 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100660 OP(1, false), // not
661 OP(7, true ), OP( 7, true ), // or and
662 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
663 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100664 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100665#undef OP
666};
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100667#define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f)
668#define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100669#endif // ENABLE_BC
670
671#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100672static const //BcLexType - should be this type
673uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100674dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100675 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100676 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100677 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100678 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100679 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100680 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100681 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100682 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
683 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
684 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100685 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100686 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
687 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100688 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100689 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
690 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100691 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100692 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
693 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenkof11b5b92019-01-04 15:54:40 +0100694 // QRSTUVWX
695 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID,
696 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR,
697 // YZ
698 XC_LEX_INVALID, DC_LEX_LENGTH,
699 // [\]
700 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100701 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100702 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100703 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100704 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
705 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100706 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100707 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100708 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100709 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100710 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
711 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100712 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100713 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100714 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100715 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100716};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100717static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100718int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100719dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
720 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
721 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
722 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100723 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
724 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
725 XC_INST_REL_GT, // DC_LEX_LPAREN
726 DC_INST_INVALID, // DC_LEX_SCOLON
727 DC_INST_INVALID, // DC_LEX_READ
728 XC_INST_IBASE, // DC_LEX_IBASE
729 XC_INST_SCALE, // DC_LEX_SCALE
730 XC_INST_OBASE, // DC_LEX_OBASE
731 XC_INST_LENGTH, // DC_LEX_LENGTH
732 XC_INST_PRINT, // DC_LEX_PRINT
733 DC_INST_QUIT, // DC_LEX_QUIT
734 XC_INST_SQRT, // DC_LEX_SQRT
735 XC_INST_REL_GE, // DC_LEX_LBRACE
736 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100737 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
738 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
739 DC_INST_EXECUTE, // DC_LEX_EXECUTE
740 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
741 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
742 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
743 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
744 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
745 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
746 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
747 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
748 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100749 // DC_INST_INVALID in this table either means that corresponding LEX
750 // is not possible for dc, or that it does not compile one-to-one
751 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100752};
753#endif // ENABLE_DC
754
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100755typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100756 smallint lex; // was BcLexType // first member is most used
757 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100758 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100759 const char *lex_inbuf;
760 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100761 const char *lex_filename;
762 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100763 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100764
Gavin Howard01055ba2018-11-03 11:00:21 -0600765 BcFunc *func;
766 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100767 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100768 IF_BC(BcVec exits;)
769 IF_BC(BcVec conds;)
770 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600771} BcParse;
772
Gavin Howard01055ba2018-11-03 11:00:21 -0600773typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600774 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100775 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600776
Denys Vlasenko503faf92018-12-20 16:24:18 +0100777 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600778 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600779 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600780
Gavin Howard01055ba2018-11-03 11:00:21 -0600781 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100782 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600783
784 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100785 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600786
787 BcVec vars;
788 BcVec var_map;
789
790 BcVec arrs;
791 BcVec arr_map;
792
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100793 IF_DC(BcVec strs;)
794 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600795
Gavin Howard01055ba2018-11-03 11:00:21 -0600796 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100797 IF_BC(BcNum one;)
798 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600799} BcProgram;
800
Denys Vlasenko6e618232018-12-25 22:20:14 +0100801struct globals {
802 BcParse prs; // first member is most used
803
804 // For error messages. Can be set to current parsed line,
805 // or [TODO] to current executing line (can be before last parsed one)
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100806 size_t err_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100807
808 BcVec input_buffer;
809
Denys Vlasenko14767602018-12-27 22:52:13 +0100810 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100811 IF_FEATURE_CLEAN_UP(smallint exiting;)
812
813 BcProgram prog;
814
815 BcVec files;
816
817 char *env_args;
818
819#if ENABLE_FEATURE_EDITING
820 line_input_t *line_input_state;
821#endif
822} FIX_ALIASING;
823#define G (*ptr_to_globals)
824#define INIT_G() do { \
825 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
826} while (0)
827#define FREE_G() do { \
828 FREE_PTR_TO_GLOBALS(); \
829} while (0)
830#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
831#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
832#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100833#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100834# define G_interrupt bb_got_signal
835# define G_ttyin G.ttyin
836#else
837# define G_interrupt 0
838# define G_ttyin 0
839#endif
840#if ENABLE_FEATURE_CLEAN_UP
841# define G_exiting G.exiting
842#else
843# define G_exiting 0
844#endif
845#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
846#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
847
Denys Vlasenko6e618232018-12-25 22:20:14 +0100848#if ENABLE_BC
849# define BC_PARSE_REL (1 << 0)
850# define BC_PARSE_PRINT (1 << 1)
851# define BC_PARSE_ARRAY (1 << 2)
852# define BC_PARSE_NOCALL (1 << 3)
853#endif
854
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100855#define BC_PROG_MAIN 0
856#define BC_PROG_READ 1
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100857#if ENABLE_DC
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100858#define BC_PROG_REQ_FUNCS 2
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100859#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600860
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100861#define BC_FLAG_W (1 << 0)
862#define BC_FLAG_V (1 << 1)
863#define BC_FLAG_S (1 << 2)
864#define BC_FLAG_Q (1 << 3)
865#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100866#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
867#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600868
Denys Vlasenko64074a12018-12-07 15:50:14 +0100869#define BC_MAX_OBASE ((unsigned) 999)
870#define BC_MAX_DIM ((unsigned) INT_MAX)
871#define BC_MAX_SCALE ((unsigned) UINT_MAX)
872#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
873#define BC_MAX_NUM BC_MAX_STRING
874// Unused apart from "limits" message. Just show a "biggish number" there.
Denys Vlasenko64074a12018-12-07 15:50:14 +0100875//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
876//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
Denys Vlasenko64074a12018-12-07 15:50:14 +0100877#define BC_MAX_EXP_STR "999999999"
878#define BC_MAX_VARS_STR "999999999"
879
880#define BC_MAX_OBASE_STR "999"
881
882#if INT_MAX == 2147483647
883# define BC_MAX_DIM_STR "2147483647"
884#elif INT_MAX == 9223372036854775807
885# define BC_MAX_DIM_STR "9223372036854775807"
886#else
887# error Strange INT_MAX
888#endif
889
Kang-Che Sungf1593522019-09-05 23:40:38 +0800890#if UINT_MAX == 4294967295U
Denys Vlasenko64074a12018-12-07 15:50:14 +0100891# define BC_MAX_SCALE_STR "4294967295"
892# define BC_MAX_STRING_STR "4294967294"
Kang-Che Sungf1593522019-09-05 23:40:38 +0800893#elif UINT_MAX == 18446744073709551615U
Denys Vlasenko64074a12018-12-07 15:50:14 +0100894# define BC_MAX_SCALE_STR "18446744073709551615"
895# define BC_MAX_STRING_STR "18446744073709551614"
896#else
897# error Strange UINT_MAX
898#endif
899#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600900
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901// In configurations where errors abort instead of propagating error
902// return code up the call chain, functions returning BC_STATUS
903// actually don't return anything, they always succeed and return "void".
904// A macro wrapper is provided, which makes this statement work:
905// s = zbc_func(...)
906// and makes it visible to the compiler that s is always zero,
907// allowing compiler to optimize dead code after the statement.
908//
909// To make code more readable, each such function has a "z"
910// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
911//
Denys Vlasenko14767602018-12-27 22:52:13 +0100912#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100913# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100914# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100915# define IF_ERROR_RETURN_POSSIBLE(a) a
916# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100917# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100918# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100919#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100920# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100921# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100922# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
923# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100924# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100925# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100926#endif
927
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100928//
929// Utility routines
930//
Gavin Howard01055ba2018-11-03 11:00:21 -0600931
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100932#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
933#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
934
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100935static void fflush_and_check(void)
936{
937 fflush_all();
938 if (ferror(stdout) || ferror(stderr))
James Byrne69374872019-07-02 11:35:03 +0200939 bb_simple_perror_msg_and_die("output error");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100940}
941
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100942#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100943#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100944do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100945 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100946 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100947 return BC_STATUS_FAILURE; \
948} while (0)
949#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100950static void quit(void) NORETURN;
951static void quit(void)
952{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100953 if (ferror(stdin))
James Byrne69374872019-07-02 11:35:03 +0200954 bb_simple_perror_msg_and_die("input error");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100955 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100956 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100957 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100958}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100959#define QUIT_OR_RETURN_TO_MAIN quit()
960#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100961
Denys Vlasenko5318f812018-12-05 17:48:01 +0100962static void bc_verror_msg(const char *fmt, va_list p)
963{
Denys Vlasenko82269122018-12-14 16:30:56 +0100964 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100965 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100966 sv = applet_name;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +0100967 applet_name = xasprintf("%s: %s:%lu", applet_name,
968 G.prs.lex_filename, (unsigned long)G.err_line
969 );
Denys Vlasenko5318f812018-12-05 17:48:01 +0100970 }
971 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100972 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100973 free((char*)applet_name);
974 applet_name = sv;
975 }
976}
977
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100978static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100979{
980 va_list p;
981
982 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100983 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100984 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100985
Denys Vlasenkoec603182018-12-17 10:34:02 +0100986 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
987 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
988 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100989}
990
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100991#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100992static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100993{
994 va_list p;
995
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100996 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100997 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100998 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100999
1000 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001001 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001002 va_end(p);
1003
1004 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001005 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001006 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
1007
Denys Vlasenkoec603182018-12-17 10:34:02 +01001008 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001009 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +01001010 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001011}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001012#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001013#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001014
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001015// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1016// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001017// function must not have caller-cleaned parameters on stack.
1018// Unfortunately, vararg function API does exactly that on most arches.
1019// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001020static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001021{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001022 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001023}
Denys Vlasenko22314682019-01-01 21:50:14 +01001024static ERRORFUNC int bc_error_at(const char *msg)
1025{
1026 const char *err_at = G.prs.lex_next_at;
1027 if (err_at) {
1028 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt(
1029 "%s at '%.*s'",
1030 msg,
1031 (int)(strchrnul(err_at, '\n') - err_at),
1032 err_at
1033 );
1034 }
1035 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
1036}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001037static ERRORFUNC int bc_error_bad_character(char c)
1038{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001039 if (!c)
1040 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001041 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001042}
Denys Vlasenko7a4e5542019-06-08 12:39:30 +02001043#if ENABLE_BC
Denys Vlasenko22314682019-01-01 21:50:14 +01001044static ERRORFUNC int bc_error_bad_function_definition(void)
1045{
1046 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition");
1047}
Denys Vlasenko7a4e5542019-06-08 12:39:30 +02001048#endif
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001049static ERRORFUNC int bc_error_bad_expression(void)
1050{
Denys Vlasenko22314682019-01-01 21:50:14 +01001051 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression");
1052}
1053static ERRORFUNC int bc_error_bad_assignment(void)
1054{
1055 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at(
1056 "bad assignment: left side must be variable or array element"
1057 );
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001058}
1059static ERRORFUNC int bc_error_bad_token(void)
1060{
Denys Vlasenko22314682019-01-01 21:50:14 +01001061 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001062}
1063static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1064{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001065 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001066}
1067static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1068{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001069 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001070}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001071#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001072static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001073{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001074 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001075}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001076#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1077static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001078{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001079 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001080}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001081#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1082static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001083{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001084 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 +01001085}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001086#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)
1087static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001088{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001089 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001090}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001091#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 +01001092#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001093
Gavin Howard01055ba2018-11-03 11:00:21 -06001094static void bc_vec_grow(BcVec *v, size_t n)
1095{
1096 size_t cap = v->cap * 2;
1097 while (cap < v->len + n) cap *= 2;
1098 v->v = xrealloc(v->v, v->size * cap);
1099 v->cap = cap;
1100}
1101
1102static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1103{
1104 v->size = esize;
1105 v->cap = BC_VEC_START_CAP;
1106 v->len = 0;
1107 v->dtor = dtor;
1108 v->v = xmalloc(esize * BC_VEC_START_CAP);
1109}
1110
Denys Vlasenko7d628012018-12-04 21:46:47 +01001111static void bc_char_vec_init(BcVec *v)
1112{
1113 bc_vec_init(v, sizeof(char), NULL);
1114}
1115
Gavin Howard01055ba2018-11-03 11:00:21 -06001116static void bc_vec_expand(BcVec *v, size_t req)
1117{
1118 if (v->cap < req) {
1119 v->v = xrealloc(v->v, v->size * req);
1120 v->cap = req;
1121 }
1122}
1123
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001124static void bc_vec_pop(BcVec *v)
1125{
1126 v->len--;
1127 if (v->dtor)
1128 v->dtor(v->v + (v->size * v->len));
1129}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001130
Gavin Howard01055ba2018-11-03 11:00:21 -06001131static void bc_vec_npop(BcVec *v, size_t n)
1132{
1133 if (!v->dtor)
1134 v->len -= n;
1135 else {
1136 size_t len = v->len - n;
1137 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1138 }
1139}
1140
Denys Vlasenko7d628012018-12-04 21:46:47 +01001141static void bc_vec_pop_all(BcVec *v)
1142{
1143 bc_vec_npop(v, v->len);
1144}
1145
Denys Vlasenko53799502019-01-25 14:24:03 +01001146static size_t bc_vec_npush(BcVec *v, size_t n, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001147{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001148 size_t len = v->len;
Denys Vlasenko53799502019-01-25 14:24:03 +01001149 if (len + n > v->cap) bc_vec_grow(v, n);
1150 memmove(v->v + (v->size * len), data, v->size * n);
1151 v->len = len + n;
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001152 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001153}
1154
Denys Vlasenko53799502019-01-25 14:24:03 +01001155static size_t bc_vec_push(BcVec *v, const void *data)
1156{
1157 return bc_vec_npush(v, 1, data);
1158 //size_t len = v->len;
1159 //if (len >= v->cap) bc_vec_grow(v, 1);
1160 //memmove(v->v + (v->size * len), data, v->size);
1161 //v->len = len + 1;
1162 //return len;
1163}
1164
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001165// G.prog.results often needs "pop old operand, push result" idiom.
1166// Can do this without a few extra ops
1167static size_t bc_result_pop_and_push(const void *data)
1168{
1169 BcVec *v = &G.prog.results;
1170 char *last;
1171 size_t len = v->len - 1;
1172
1173 last = v->v + (v->size * len);
1174 if (v->dtor)
1175 v->dtor(last);
1176 memmove(last, data, v->size);
1177 return len;
1178}
1179
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001180static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001181{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001182 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001183}
1184
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001185static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001186{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001187 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001188 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001189 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001190}
1191
Gavin Howard01055ba2018-11-03 11:00:21 -06001192static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1193{
1194 if (idx == v->len)
1195 bc_vec_push(v, data);
1196 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001197 char *ptr;
1198
1199 if (v->len == v->cap) bc_vec_grow(v, 1);
1200
1201 ptr = v->v + v->size * idx;
1202
1203 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1204 memmove(ptr, data, v->size);
1205 }
1206}
1207
1208static void bc_vec_string(BcVec *v, size_t len, const char *str)
1209{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001210 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001211 bc_vec_expand(v, len + 1);
1212 memcpy(v->v, str, len);
1213 v->len = len;
1214
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001215 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001216}
1217
Gavin Howard01055ba2018-11-03 11:00:21 -06001218static void *bc_vec_item(const BcVec *v, size_t idx)
1219{
1220 return v->v + v->size * idx;
1221}
1222
1223static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1224{
1225 return v->v + v->size * (v->len - idx - 1);
1226}
1227
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001228static void *bc_vec_top(const BcVec *v)
1229{
1230 return v->v + v->size * (v->len - 1);
1231}
1232
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001233static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001234{
1235 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001236 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001237 free(v->v);
1238}
1239
Denys Vlasenko10bde142018-12-27 18:23:58 +01001240static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001241{
1242 return bc_vec_item(&G.prog.fns, idx);
1243}
1244// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001245#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001246
1247#if ENABLE_BC
1248static BcFunc* bc_program_current_func(void)
1249{
1250 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001251 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001252 return func;
1253}
1254#endif
1255
Denys Vlasenko10bde142018-12-27 18:23:58 +01001256static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001257{
1258#if ENABLE_BC
1259 if (IS_BC) {
1260 BcFunc *func = bc_program_current_func();
1261 return bc_vec_item(&func->strs, idx);
1262 }
1263#endif
1264 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1265}
1266
Denys Vlasenko10bde142018-12-27 18:23:58 +01001267static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001268{
1269#if ENABLE_BC
1270 if (IS_BC) {
1271 BcFunc *func = bc_program_current_func();
1272 return bc_vec_item(&func->consts, idx);
1273 }
1274#endif
1275 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1276}
1277
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001278static int bc_id_cmp(const void *e1, const void *e2)
1279{
1280 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1281}
1282
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001283static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001284{
1285 free(((BcId *) id)->name);
1286}
1287
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001288static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001289{
1290 size_t low = 0, high = v->len;
1291
1292 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001293 size_t mid = (low + high) / 2;
1294 BcId *id = bc_vec_item(v, mid);
1295 int result = bc_id_cmp(ptr, id);
1296
1297 if (result == 0)
1298 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001299 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001300 high = mid;
1301 else
1302 low = mid + 1;
1303 }
1304
1305 return low;
1306}
1307
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001308static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001309{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001310 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001311
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001312 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001313 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001314 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1315 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001316 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001317 bc_vec_pushAt(v, ptr, n);
1318 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001319}
1320
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001321static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001322{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001323 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001324 if (i >= v->len) return BC_VEC_INVALID_IDX;
1325 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1326}
1327
Gavin Howard01055ba2018-11-03 11:00:21 -06001328static void bc_num_setToZero(BcNum *n, size_t scale)
1329{
1330 n->len = 0;
1331 n->neg = false;
1332 n->rdx = scale;
1333}
1334
1335static void bc_num_zero(BcNum *n)
1336{
1337 bc_num_setToZero(n, 0);
1338}
1339
1340static void bc_num_one(BcNum *n)
1341{
1342 bc_num_setToZero(n, 0);
1343 n->len = 1;
1344 n->num[0] = 1;
1345}
1346
Denys Vlasenko3129f702018-12-09 12:04:44 +01001347// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001348static void bc_num_init(BcNum *n, size_t req)
1349{
1350 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001351 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001352 n->num = xmalloc(req);
1353 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001354 n->rdx = 0;
1355 n->len = 0;
1356 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001357}
1358
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001359static void bc_num_init_DEF_SIZE(BcNum *n)
1360{
1361 bc_num_init(n, BC_NUM_DEF_SIZE);
1362}
1363
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001364static void bc_num_expand(BcNum *n, size_t req)
1365{
1366 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1367 if (req > n->cap) {
1368 n->num = xrealloc(n->num, req);
1369 n->cap = req;
1370 }
1371}
1372
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001373static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001374{
1375 free(((BcNum *) num)->num);
1376}
1377
1378static void bc_num_copy(BcNum *d, BcNum *s)
1379{
1380 if (d != s) {
1381 bc_num_expand(d, s->cap);
1382 d->len = s->len;
1383 d->neg = s->neg;
1384 d->rdx = s->rdx;
1385 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1386 }
1387}
1388
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001389static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001390{
1391 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001392 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001393
Denys Vlasenko1557b762018-12-22 21:37:46 +01001394 result = 0;
1395 i = n->len;
1396 while (i > n->rdx) {
1397 unsigned long prev = result;
1398 result = result * 10 + n->num[--i];
1399 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1400 // 0x1ff00000 * 10 is 0x13f600000,
1401 // or 0x3f600000 truncated to 32 bits. Which is larger.
1402 // However, (N*10)/8 < N check is always correct.
1403 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001404 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001405 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001406 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001407
Denys Vlasenko29301232018-12-11 15:29:32 +01001408 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001409}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001410#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1411
1412static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1413{
1414 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1415
1416 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1417}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001418#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001419
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001420#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1421# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1422#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1423# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1424#endif
1425// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1426// would not hit realloc() code path - not good if num[] is not malloced
1427
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001428static void bc_num_ulong2num(BcNum *n, unsigned long val)
1429{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001430 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001431
1432 bc_num_zero(n);
1433
1434 if (val == 0) return;
1435
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001436 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001437
1438 ptr = n->num;
1439 for (;;) {
1440 n->len++;
1441 *ptr++ = val % 10;
1442 val /= 10;
1443 if (val == 0) break;
1444 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001445}
1446
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001447static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001448{
1449 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001450 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001451 a[i] -= b[i];
1452 for (j = i; a[j] < 0;) {
1453 a[j++] += 10;
1454 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001455 }
1456 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001457}
1458
1459static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1460{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001461 size_t i = len;
1462 for (;;) {
1463 int c;
1464 if (i == 0)
1465 return 0;
1466 i--;
1467 c = a[i] - b[i];
1468 if (c != 0) {
1469 i++;
1470 if (c < 0)
1471 return -i;
1472 return i;
1473 }
1474 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001475}
1476
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001477#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1478#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1479#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1480//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1481static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1482{
1483 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1484}
1485//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1486static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1487{
1488 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1489}
1490
Gavin Howard01055ba2018-11-03 11:00:21 -06001491static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1492{
1493 size_t i, min, a_int, b_int, diff;
1494 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001495 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001496 ssize_t cmp;
1497
1498 if (a == b) return 0;
1499 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1500 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001501
1502 if (a->neg != b->neg) // signs of a and b differ
1503 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1504 return (int)b->neg - (int)a->neg;
1505 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001506
1507 a_int = BC_NUM_INT(a);
1508 b_int = BC_NUM_INT(b);
1509 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001510
Denys Vlasenko00841372019-11-23 17:25:21 +01001511 if (a_int != 0) {
1512 if (neg) return - (ssize_t) a_int;
1513 return (ssize_t) a_int;
1514 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001515
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001516 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001517 if (a_max) {
1518 min = b->rdx;
1519 diff = a->rdx - b->rdx;
1520 max_num = a->num + diff;
1521 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001522 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1523 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001524 min = a->rdx;
1525 diff = b->rdx - a->rdx;
1526 max_num = b->num + diff;
1527 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001528 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001529 }
1530
1531 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001532 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001533
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001534 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001535 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001536 }
1537
1538 return 0;
1539}
1540
1541static void bc_num_truncate(BcNum *n, size_t places)
1542{
1543 if (places == 0) return;
1544
1545 n->rdx -= places;
1546
1547 if (n->len != 0) {
1548 n->len -= places;
1549 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1550 }
1551}
1552
1553static void bc_num_extend(BcNum *n, size_t places)
1554{
1555 size_t len = n->len + places;
1556
1557 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001558 if (n->cap < len) bc_num_expand(n, len);
1559
1560 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1561 memset(n->num, 0, sizeof(BcDig) * places);
1562
1563 n->len += places;
1564 n->rdx += places;
1565 }
1566}
1567
1568static void bc_num_clean(BcNum *n)
1569{
1570 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1571 if (n->len == 0)
1572 n->neg = false;
1573 else if (n->len < n->rdx)
1574 n->len = n->rdx;
1575}
1576
1577static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1578{
1579 if (n->rdx < scale)
1580 bc_num_extend(n, scale - n->rdx);
1581 else
1582 bc_num_truncate(n, n->rdx - scale);
1583
1584 bc_num_clean(n);
1585 if (n->len != 0) n->neg = !neg1 != !neg2;
1586}
1587
1588static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1589 BcNum *restrict b)
1590{
1591 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001592 b->len = n->len - idx;
1593 a->len = idx;
1594 a->rdx = b->rdx = 0;
1595
1596 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1597 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001598 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001599 bc_num_zero(b);
1600 bc_num_copy(a, n);
1601 }
1602
1603 bc_num_clean(a);
1604 bc_num_clean(b);
1605}
1606
Denys Vlasenko29301232018-12-11 15:29:32 +01001607static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001608{
Denys Vlasenko29301232018-12-11 15:29:32 +01001609 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001610
1611 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1612 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1613 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001614 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001615 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001616
1617 if (n->rdx >= places)
1618 n->rdx -= places;
1619 else {
1620 bc_num_extend(n, places - n->rdx);
1621 n->rdx = 0;
1622 }
1623
1624 bc_num_clean(n);
1625
Denys Vlasenko29301232018-12-11 15:29:32 +01001626 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001627}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001628#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001629
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001630typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1631
1632static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1633 BcNumBinaryOp op, size_t req)
1634{
1635 BcStatus s;
1636 BcNum num2, *ptr_a, *ptr_b;
1637 bool init = false;
1638
1639 if (c == a) {
1640 ptr_a = &num2;
1641 memcpy(ptr_a, c, sizeof(BcNum));
1642 init = true;
1643 } else
1644 ptr_a = a;
1645
1646 if (c == b) {
1647 ptr_b = &num2;
1648 if (c != a) {
1649 memcpy(ptr_b, c, sizeof(BcNum));
1650 init = true;
1651 }
1652 } else
1653 ptr_b = b;
1654
1655 if (init)
1656 bc_num_init(c, req);
1657 else
1658 bc_num_expand(c, req);
1659
1660 s = BC_STATUS_SUCCESS;
1661 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1662
1663 if (init) bc_num_free(&num2);
1664
1665 RETURN_STATUS(s);
1666}
1667#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1668
1669static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1670static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1671static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1672static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1673static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1674static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1675
1676static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1677{
1678 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1679 (void) scale;
1680 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1681}
1682
1683static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1684{
1685 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1686 (void) scale;
1687 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1688}
1689
1690static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1691{
1692 size_t req = BC_NUM_MREQ(a, b, scale);
1693 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1694}
1695
1696static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1697{
1698 size_t req = BC_NUM_MREQ(a, b, scale);
1699 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1700}
1701
1702static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1703{
1704 size_t req = BC_NUM_MREQ(a, b, scale);
1705 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1706}
1707
1708static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1709{
1710 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1711}
1712
Denys Vlasenko10bde142018-12-27 18:23:58 +01001713static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001714 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1715};
1716#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1717#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1718#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1719#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1720#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1721#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1722
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001723static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001724{
1725 BcNum one;
1726 BcDig num[2];
1727
1728 one.cap = 2;
1729 one.num = num;
1730 bc_num_one(&one);
1731
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001732 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001733}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001734#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001735
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001736static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001737{
1738 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1739 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001740 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001741
1742 // Because this function doesn't need to use scale (per the bc spec),
1743 // I am hijacking it to say whether it's doing an add or a subtract.
1744
1745 if (a->len == 0) {
1746 bc_num_copy(c, b);
1747 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001748 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001749 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001750 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001751 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001752 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001753 }
1754
1755 c->neg = a->neg;
1756 c->rdx = BC_MAX(a->rdx, b->rdx);
1757 min_rdx = BC_MIN(a->rdx, b->rdx);
1758 c->len = 0;
1759
1760 if (a->rdx > b->rdx) {
1761 diff = a->rdx - b->rdx;
1762 ptr = a->num;
1763 ptr_a = a->num + diff;
1764 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001765 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001766 diff = b->rdx - a->rdx;
1767 ptr = b->num;
1768 ptr_a = a->num;
1769 ptr_b = b->num + diff;
1770 }
1771
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001772 ptr_c = c->num;
1773 for (i = 0; i < diff; ++i, ++c->len)
1774 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001775
1776 ptr_c += diff;
1777 a_int = BC_NUM_INT(a);
1778 b_int = BC_NUM_INT(b);
1779
1780 if (a_int > b_int) {
1781 min_int = b_int;
1782 max = a_int;
1783 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001784 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001785 min_int = a_int;
1786 max = b_int;
1787 ptr = ptr_b;
1788 }
1789
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001790 carry = 0;
1791 for (i = 0; i < min_rdx + min_int; ++i) {
1792 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001793 carry = in / 10;
1794 ptr_c[i] = (BcDig)(in % 10);
1795 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001796 for (; i < max + min_rdx; ++i) {
1797 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001798 carry = in / 10;
1799 ptr_c[i] = (BcDig)(in % 10);
1800 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001801 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001802
1803 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1804
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001805 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001806}
1807
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001808static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001809{
Gavin Howard01055ba2018-11-03 11:00:21 -06001810 ssize_t cmp;
1811 BcNum *minuend, *subtrahend;
1812 size_t start;
1813 bool aneg, bneg, neg;
1814
1815 // Because this function doesn't need to use scale (per the bc spec),
1816 // I am hijacking it to say whether it's doing an add or a subtract.
1817
1818 if (a->len == 0) {
1819 bc_num_copy(c, b);
1820 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001821 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001823 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001824 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001825 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001826 }
1827
1828 aneg = a->neg;
1829 bneg = b->neg;
1830 a->neg = b->neg = false;
1831
1832 cmp = bc_num_cmp(a, b);
1833
1834 a->neg = aneg;
1835 b->neg = bneg;
1836
1837 if (cmp == 0) {
1838 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001839 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001840 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 neg = a->neg;
1843 minuend = a;
1844 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001845 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001846 neg = b->neg;
1847 if (sub) neg = !neg;
1848 minuend = b;
1849 subtrahend = a;
1850 }
1851
1852 bc_num_copy(c, minuend);
1853 c->neg = neg;
1854
1855 if (c->rdx < subtrahend->rdx) {
1856 bc_num_extend(c, subtrahend->rdx - c->rdx);
1857 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001858 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001859 start = c->rdx - subtrahend->rdx;
1860
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001861 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001862
1863 bc_num_clean(c);
1864
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001865 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001866}
1867
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001868static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001869 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001870#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001871{
1872 BcStatus s;
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01001873 size_t max, max2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001875 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001876
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 if (a->len == 0 || b->len == 0) {
1878 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001879 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001880 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001881 aone = BC_NUM_ONE(a);
1882 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001883 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001884 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001885 }
1886
Denys Vlasenkocc4303f2021-01-06 14:00:53 +01001887 if (a->len < BC_NUM_KARATSUBA_LEN
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001888 || b->len < BC_NUM_KARATSUBA_LEN
Denys Vlasenkocc4303f2021-01-06 14:00:53 +01001889 /* || a->len + b->len < BC_NUM_KARATSUBA_LEN - redundant check */
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001890 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001891 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001892
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 bc_num_expand(c, a->len + b->len + 1);
1894
1895 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001896 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001897
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001898 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001899 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001900 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001901 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001902 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001903 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 carry = in / 10;
1905 c->num[i + j] = (BcDig)(in % 10);
1906 }
1907
1908 c->num[i + j] += (BcDig) carry;
1909 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001910
Denys Vlasenko14767602018-12-27 22:52:13 +01001911#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001912 // a=2^1000000
1913 // a*a <- without check below, this will not be interruptible
1914 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001915#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 }
1917
1918 c->len = len;
1919
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001920 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001921 }
1922
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01001923 max = BC_MAX(a->len, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001924 bc_num_init(&l1, max);
1925 bc_num_init(&h1, max);
1926 bc_num_init(&l2, max);
1927 bc_num_init(&h2, max);
1928 bc_num_init(&m1, max);
1929 bc_num_init(&m2, max);
1930 bc_num_init(&z0, max);
1931 bc_num_init(&z1, max);
1932 bc_num_init(&z2, max);
1933 bc_num_init(&temp, max + max);
1934
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01001935 max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001936 bc_num_split(a, max2, &l1, &h1);
1937 bc_num_split(b, max2, &l2, &h2);
1938
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001939 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001940 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001941 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001942 if (s) goto err;
1943
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001944 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001945 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001946 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001947 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001948 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 if (s) goto err;
1950
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001951 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001952 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001953 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001954 if (s) goto err;
1955
Denys Vlasenko29301232018-12-11 15:29:32 +01001956 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001957 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001958 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001960 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001961 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001962 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001963 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001964 bc_num_free(&temp);
1965 bc_num_free(&z2);
1966 bc_num_free(&z1);
1967 bc_num_free(&z0);
1968 bc_num_free(&m2);
1969 bc_num_free(&m1);
1970 bc_num_free(&h2);
1971 bc_num_free(&l2);
1972 bc_num_free(&h1);
1973 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001974 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001975}
1976
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001977static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001978{
1979 BcStatus s;
1980 BcNum cpa, cpb;
1981 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1982
1983 scale = BC_MAX(scale, a->rdx);
1984 scale = BC_MAX(scale, b->rdx);
1985 scale = BC_MIN(a->rdx + b->rdx, scale);
1986 maxrdx = BC_MAX(maxrdx, scale);
1987
1988 bc_num_init(&cpa, a->len);
1989 bc_num_init(&cpb, b->len);
1990
1991 bc_num_copy(&cpa, a);
1992 bc_num_copy(&cpb, b);
1993 cpa.neg = cpb.neg = false;
1994
Denys Vlasenko29301232018-12-11 15:29:32 +01001995 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001996 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001997 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001998 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001999 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06002000 if (s) goto err;
2001
2002 maxrdx += scale;
2003 bc_num_expand(c, c->len + maxrdx);
2004
2005 if (c->len < maxrdx) {
2006 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2007 c->len += maxrdx;
2008 }
2009
2010 c->rdx = maxrdx;
2011 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002012 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 bc_num_free(&cpb);
2014 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002015 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002016}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002017#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002018
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002019static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002020{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002021 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06002022 size_t len, end, i;
2023 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06002024
2025 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002026 RETURN_STATUS(bc_error("divide by zero"));
2027 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002028 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002029 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002030 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002031 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002032 bc_num_copy(c, a);
2033 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002034 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002035 }
2036
2037 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2038 bc_num_copy(&cp, a);
2039 len = b->len;
2040
2041 if (len > cp.len) {
2042 bc_num_expand(&cp, len + 2);
2043 bc_num_extend(&cp, len - cp.len);
2044 }
2045
2046 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2047 cp.rdx -= b->rdx;
2048 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2049
2050 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002051 for (;;) {
2052 if (len == 0) break;
2053 len--;
2054 if (b->num[len] != 0)
2055 break;
2056 }
2057 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002058 }
2059
2060 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2061
2062 // We want an extra zero in front to make things simpler.
2063 cp.num[cp.len++] = 0;
2064 end = cp.len - len;
2065
2066 bc_num_expand(c, cp.len);
2067
2068 bc_num_zero(c);
2069 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2070 c->rdx = cp.rdx;
2071 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002072
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002073 s = BC_STATUS_SUCCESS;
2074 for (i = end - 1; i < end; --i) {
2075 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002076 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002077 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2078 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002079 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01002080#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01002081 // a=2^100000
2082 // scale=40000
2083 // 1/a <- without check below, this will not be interruptible
2084 if (G_interrupt) {
2085 s = BC_STATUS_FAILURE;
2086 break;
2087 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002088#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002089 }
2090
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002091 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002092 bc_num_free(&cp);
2093
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002094 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002095}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002096#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002097
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002098static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002099 BcNum *restrict d, size_t scale, size_t ts)
2100{
2101 BcStatus s;
2102 BcNum temp;
2103 bool neg;
2104
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002105 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002106 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002107
2108 if (a->len == 0) {
2109 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002110 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002111 }
2112
2113 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002114 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002115 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002116
2117 if (scale != 0) scale = ts;
2118
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002119 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002121 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 if (s) goto err;
2123
2124 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2125
2126 neg = d->neg;
2127 bc_num_retireMul(d, ts, a->neg, b->neg);
2128 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002129 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002130 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002131 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002132}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002133#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002134
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002135static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002136{
2137 BcStatus s;
2138 BcNum c1;
2139 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2140
2141 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002142 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002143 bc_num_free(&c1);
2144
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002145 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002146}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002147#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002148
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002149static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002150{
2151 BcStatus s = BC_STATUS_SUCCESS;
2152 BcNum copy;
2153 unsigned long pow;
2154 size_t i, powrdx, resrdx;
Denys Vlasenkoe5958f72021-06-12 12:19:20 +02002155 size_t a_rdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002156 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002157
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002158 // GNU bc does not allow 2^2.0 - we do
2159 for (i = 0; i < b->rdx; i++)
2160 if (b->num[i] != 0)
2161 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002162
2163 if (b->len == 0) {
2164 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002165 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002167 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002168 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002169 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002170 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002171 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002172 if (!b->neg)
2173 bc_num_copy(c, a);
2174 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002175 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002176 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002177 }
2178
2179 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002180 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002181 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002182 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002183
2184 bc_num_init(&copy, a->len);
2185 bc_num_copy(&copy, a);
Denys Vlasenkoe5958f72021-06-12 12:19:20 +02002186 a_rdx = a->rdx; // pull it into a CPU register (hopefully)
2187 // a is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002188
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002189 if (!neg) {
Denys Vlasenkoe5958f72021-06-12 12:19:20 +02002190 unsigned long new_scale;
2191 if (a_rdx > scale)
2192 scale = a_rdx;
2193 new_scale = a_rdx * pow;
2194 // Don't fall for multiplication overflow. Example:
2195 // 0.01^2147483648 a_rdx:2 pow:0x80000000, 32bit mul is 0.
2196//not that it matters with current algorithm, it would OOM on such large powers,
2197//but it can be improved to detect zero results etc. Example: with scale=0,
2198//result of 0.01^N for any N>1 is 0: 0.01^2 = 0.0001 ~= 0.00 (trunc to scale)
2199//then this would matter:
2200 // if (new_scale >= pow) is false, we had overflow, correct
2201 // "new_scale" value is larger than ULONG_MAX, thus larger
2202 // than any possible current value of "scale",
2203 // thus "scale = new_scale" should not be done:
2204 if (new_scale >= pow)
2205 if (new_scale < scale)
2206 scale = new_scale;
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002207 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002208
Denys Vlasenkoe5958f72021-06-12 12:19:20 +02002209 for (powrdx = a_rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002210 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002211 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002212 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002213 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002214 //if (G_interrupt) {
2215 // s = BC_STATUS_FAILURE;
2216 // goto err;
2217 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002218 }
2219
Gavin Howard01055ba2018-11-03 11:00:21 -06002220 bc_num_copy(c, &copy);
2221
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002222 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002223 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002224 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002225 if (s) goto err;
2226
2227 if (pow & 1) {
2228 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002229 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002230 if (s) goto err;
2231 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002232 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002233 //if (G_interrupt) {
2234 // s = BC_STATUS_FAILURE;
2235 // goto err;
2236 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002237 }
2238
2239 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002240 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002241 if (s) goto err;
2242 }
2243
Gavin Howard01055ba2018-11-03 11:00:21 -06002244 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2245
2246 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002247 for (i = 0; i < c->len; ++i)
2248 if (c->num[i] != 0)
2249 goto skip;
2250 bc_num_setToZero(c, scale);
2251 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002252
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002253 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002254 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002255 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002256}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002257#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002258
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002259static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002260{
2261 BcStatus s;
2262 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002263 BcDig half_digs[1];
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002264 size_t pow, len, digs, digs1, resrdx, req, times;
2265 ssize_t cmp, cmp1, cmp2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002266
2267 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2268 bc_num_expand(b, req);
2269
2270 if (a->len == 0) {
2271 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002272 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002273 }
2274 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002275 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002276 }
2277 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002278 bc_num_one(b);
2279 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002280 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002281 }
2282
2283 scale = BC_MAX(scale, a->rdx) + 1;
2284 len = a->len + scale;
2285
2286 bc_num_init(&num1, len);
2287 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002288
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002289 half.cap = ARRAY_SIZE(half_digs);
2290 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002291 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002292 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002293 half.rdx = 1;
2294
2295 bc_num_init(&f, len);
2296 bc_num_init(&fprime, len);
2297
2298 x0 = &num1;
2299 x1 = &num2;
2300
2301 bc_num_one(x0);
2302 pow = BC_NUM_INT(a);
2303
2304 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002305 if (pow & 1)
2306 x0->num[0] = 2;
2307 else
2308 x0->num[0] = 6;
2309
2310 pow -= 2 - (pow & 1);
2311
2312 bc_num_extend(x0, pow);
2313
2314 // Make sure to move the radix back.
2315 x0->rdx -= pow;
2316 }
2317
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002318 x0->rdx = digs = digs1 = times = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002319 resrdx = scale + 2;
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002320 len = x0->len + resrdx - 1;
2321 cmp = 1;
2322 cmp1 = cmp2 = SSIZE_MAX;
2323 do {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002324 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002325 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002326 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002327 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002328 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002329 if (s) goto err;
2330
2331 cmp = bc_num_cmp(x1, x0);
2332 digs = x1->len - (unsigned long long) llabs(cmp);
2333
2334 if (cmp == cmp2 && digs == digs1)
2335 times += 1;
2336 else
2337 times = 0;
2338
2339 resrdx += times > 4;
2340
2341 cmp2 = cmp1;
2342 cmp1 = cmp;
2343 digs1 = digs;
2344
2345 temp = x0;
2346 x0 = x1;
2347 x1 = temp;
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002348 } while (cmp != 0 || digs < len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002349
Gavin Howard01055ba2018-11-03 11:00:21 -06002350 bc_num_copy(b, x0);
2351 scale -= 1;
Denys Vlasenko40f9fe22020-12-29 16:54:37 +01002352 if (b->rdx > scale)
2353 bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002354 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002355 bc_num_free(&fprime);
2356 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002357 bc_num_free(&num2);
2358 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002359 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002360}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002361#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002362
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002363static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002364 size_t scale)
2365{
2366 BcStatus s;
2367 BcNum num2, *ptr_a;
2368 bool init = false;
2369 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2370
2371 if (c == a) {
2372 memcpy(&num2, c, sizeof(BcNum));
2373 ptr_a = &num2;
2374 bc_num_init(c, len);
2375 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002376 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002377 ptr_a = a;
2378 bc_num_expand(c, len);
2379 }
2380
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002381 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002382
2383 if (init) bc_num_free(&num2);
2384
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002385 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002386}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002387#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002388
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002389#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002390static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002391{
2392 BcStatus s;
2393 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002394 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002395
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002396 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002397 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002398 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002399 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002400 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002401 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002402
2403 bc_num_expand(d, c->len);
2404 bc_num_init(&base, c->len);
2405 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002406 bc_num_init(&temp, b->len);
2407
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002408 two.cap = ARRAY_SIZE(two_digs);
2409 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002410 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002411 two_digs[0] = 2;
2412
Gavin Howard01055ba2018-11-03 11:00:21 -06002413 bc_num_one(d);
2414
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002415 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002416 if (s) goto err;
2417 bc_num_copy(&exp, b);
2418
2419 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002420 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002421 if (s) goto err;
2422
2423 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002424 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002425 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002426 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002427 if (s) goto err;
2428 }
2429
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002430 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002431 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002432 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002433 if (s) goto err;
2434 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002435 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002436 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002437 bc_num_free(&exp);
2438 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002439 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002440}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002441#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002442#endif // ENABLE_DC
2443
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002444static FAST_FUNC void bc_string_free(void *string)
2445{
2446 free(*(char**)string);
2447}
2448
Gavin Howard01055ba2018-11-03 11:00:21 -06002449static void bc_func_init(BcFunc *f)
2450{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002451 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002452 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2453 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002454 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2455 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002456 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002457}
2458
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002459static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002460{
2461 BcFunc *f = (BcFunc *) func;
2462 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002463 IF_BC(bc_vec_free(&f->labels);)
2464 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002465 IF_BC(bc_vec_free(&f->strs);)
2466 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002467}
2468
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002469static void bc_array_expand(BcVec *a, size_t len);
2470
Gavin Howard01055ba2018-11-03 11:00:21 -06002471static void bc_array_init(BcVec *a, bool nums)
2472{
2473 if (nums)
2474 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2475 else
2476 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2477 bc_array_expand(a, 1);
2478}
2479
Gavin Howard01055ba2018-11-03 11:00:21 -06002480static void bc_array_expand(BcVec *a, size_t len)
2481{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002482 if (a->dtor == bc_num_free
2483 // && a->size == sizeof(BcNum) - always true
2484 ) {
2485 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002486 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002487 bc_num_init_DEF_SIZE(&n);
2488 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002489 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002490 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002491 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002492 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002493 bc_array_init(&v, true);
2494 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002495 }
2496 }
2497}
2498
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002499static void bc_array_copy(BcVec *d, const BcVec *s)
2500{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002501 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002502 size_t i;
2503
2504 bc_vec_pop_all(d);
2505 bc_vec_expand(d, s->cap);
2506 d->len = s->len;
2507
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002508 dnum = (void*)d->v;
2509 snum = (void*)s->v;
2510 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002511 bc_num_init(dnum, snum->len);
2512 bc_num_copy(dnum, snum);
2513 }
2514}
2515
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002516#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002517static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002518{
2519 d->t = src->t;
2520
2521 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002522 case XC_RESULT_TEMP:
2523 case XC_RESULT_IBASE:
2524 case XC_RESULT_SCALE:
2525 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002526 bc_num_init(&d->d.n, src->d.n.len);
2527 bc_num_copy(&d->d.n, &src->d.n);
2528 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002529 case XC_RESULT_VAR:
2530 case XC_RESULT_ARRAY:
2531 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002532 d->d.id.name = xstrdup(src->d.id.name);
2533 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002534 case XC_RESULT_CONSTANT:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002535 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002536 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2537 break;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002538 default: // placate compiler
2539 // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen
2540 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002541 }
2542}
2543#endif // ENABLE_DC
2544
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002545static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002546{
2547 BcResult *r = (BcResult *) result;
2548
2549 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002550 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01002551 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002552 case XC_RESULT_IBASE:
2553 case XC_RESULT_SCALE:
2554 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002555 bc_num_free(&r->d.n);
2556 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002557 case XC_RESULT_VAR:
2558 case XC_RESULT_ARRAY:
2559 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002560 free(r->d.id.name);
2561 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002562 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002563 // Do nothing.
2564 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002565 }
2566}
2567
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002568static int bad_input_byte(char c)
2569{
2570 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2571 || c > 0x7e
2572 ) {
2573 bc_error_fmt("illegal character 0x%02x", c);
2574 return 1;
2575 }
2576 return 0;
2577}
2578
Denys Vlasenko10bde142018-12-27 18:23:58 +01002579static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002580{
2581 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002582 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002583 fflush_and_check();
2584
Denys Vlasenko14767602018-12-27 22:52:13 +01002585#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002586 if (G_interrupt) { // ^C was pressed
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002587 if (fp != stdin) {
2588 // ^C while running a script (bc SCRIPT): die.
2589 // We do not return to interactive prompt:
2590 // user might be running us from a shell,
2591 // and SCRIPT might be intended to terminate
2592 // (e.g. contain a "halt" stmt).
2593 // ^C dropping user into a bc prompt instead of
2594 // the shell would be unexpected.
2595 xfunc_die();
2596 }
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002597 // There was ^C while running calculations
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002598 G_interrupt = 0;
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002599 // GNU bc says "interrupted execution." (to stdout, not stderr)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002600 // GNU dc says "Interrupt!"
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002601 puts("\ninterrupted execution");
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002602 }
2603
2604# if ENABLE_FEATURE_EDITING
2605 if (G_ttyin && fp == stdin) {
2606 int n, i;
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01002607 if (!G.line_input_state)
2608 G.line_input_state = new_line_input_t(DO_HISTORY);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002609# define line_buf bb_common_bufsiz1
2610 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2611 if (n <= 0) { // read errors or EOF, or ^D, or ^C
Denys Vlasenko0197fbf2021-01-04 14:41:20 +01002612 //GNU bc prints this on ^C:
2613 //if (n == 0) // ^C
2614 // puts("(interrupt) Exiting bc.");
2615 bc_vec_pushZeroByte(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002616 return;
2617 }
2618 i = 0;
2619 for (;;) {
2620 char c = line_buf[i++];
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002621 if (c == '\0') break;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002622 if (bad_input_byte(c)) goto again;
2623 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002624 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002625# undef line_buf
2626 } else
2627# endif
2628#endif
2629 {
2630 int c;
2631 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002632
2633 do {
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002634 get_char:
Denys Vlasenko14767602018-12-27 22:52:13 +01002635#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002636 if (G_interrupt) {
2637 // ^C was pressed: ignore entire line, get another one
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002638 goto again;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002639 }
2640#endif
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002641 c = fgetc(fp);
2642 if (c == '\0')
2643 goto get_char;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002644 if (c == EOF) {
2645 if (ferror(fp))
James Byrne69374872019-07-02 11:35:03 +02002646 bb_simple_perror_msg_and_die("input error");
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002647 // Note: EOF does not append '\n'
2648 break;
2649 }
2650 bad_chars |= bad_input_byte(c);
2651 bc_vec_pushByte(vec, (char)c);
2652 } while (c != '\n');
2653
2654 if (bad_chars) {
2655 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002656 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002657 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002658 goto again;
2659 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002660 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002661 }
2662 bc_vec_pushZeroByte(vec);
2663 }
2664}
2665
2666//
2667// Parsing routines
2668//
2669
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002670// "Input numbers may contain the characters 0-9 and A-Z.
2671// (Note: They must be capitals. Lower case letters are variable names.)
2672// Single digit numbers always have the value of the digit regardless of
2673// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2674// all input digits greater or equal to ibase to the value of ibase-1.
2675// This makes the number ZZZ always be the largest 3 digit number of the
2676// input base."
2677static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002678{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002679 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002680 for (;;) {
2681 BcDig c = *val++;
2682 if (c == '\0')
2683 break;
2684 if (c == '.') {
2685 if (radix) return false;
2686 radix = true;
2687 continue;
2688 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002689 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002690 return false;
2691 }
2692 return true;
2693}
2694
2695// Note: n is already "bc_num_zero()"ed,
2696// leading zeroes in "val" are removed
2697static void bc_num_parseDecimal(BcNum *n, const char *val)
2698{
2699 size_t len, i;
2700 const char *ptr;
2701
2702 len = strlen(val);
2703 if (len == 0)
2704 return;
2705
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002706 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002707
2708 ptr = strchr(val, '.');
2709
2710 n->rdx = 0;
2711 if (ptr != NULL)
2712 n->rdx = (size_t)((val + len) - (ptr + 1));
2713
2714 for (i = 0; val[i]; ++i) {
2715 if (val[i] != '0' && val[i] != '.') {
2716 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002717 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002718 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002719 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002720 if (c > 9) { // A-Z => 10-36
2721 n->len = 2;
2722 c -= ('A' - '9' - 1);
2723 n->num[1] = c/10;
2724 c = c%10;
2725 }
2726 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002727 break;
2728 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002729 i = len - 1;
2730 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002731 char c = val[i] - '0';
2732 if (c > 9) // A-Z => 9
2733 c = 9;
2734 n->num[n->len] = c;
2735 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002736 skip_dot:
2737 if (i == 0) break;
2738 if (val[--i] == '.') goto skip_dot;
2739 }
2740 break;
2741 }
2742 }
2743 // if for() exits without hitting if(), the value is entirely zero
2744}
2745
2746// Note: n is already "bc_num_zero()"ed,
2747// leading zeroes in "val" are removed
2748static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2749{
2750 BcStatus s;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002751 BcNum mult, result;
2752 BcNum temp;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002753 BcNum base;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002754 BcDig temp_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002755 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002756 size_t digits;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002757
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002758 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002759
2760 temp.cap = ARRAY_SIZE(temp_digs);
2761 temp.num = temp_digs;
2762
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002763 base.cap = ARRAY_SIZE(base_digs);
2764 base.num = base_digs;
2765 bc_num_ulong2num(&base, base_t);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002766 base_t--;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002767
2768 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002769 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002770 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002771
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002772 c = *val++;
2773 if (c == '\0') goto int_err;
2774 if (c == '.') break;
2775
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002776 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2777 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002778
2779 s = zbc_num_mul(n, &base, &mult, 0);
2780 if (s) goto int_err;
2781 bc_num_ulong2num(&temp, v);
2782 s = zbc_num_add(&mult, &temp, n, 0);
2783 if (s) goto int_err;
2784 }
2785
2786 bc_num_init(&result, base.len);
2787 //bc_num_zero(&result); - already is
2788 bc_num_one(&mult);
2789
2790 digits = 0;
2791 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002792 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002793 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002794
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002795 c = *val++;
2796 if (c == '\0') break;
2797 digits++;
2798
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002799 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2800 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002801
2802 s = zbc_num_mul(&result, &base, &result, 0);
2803 if (s) goto err;
2804 bc_num_ulong2num(&temp, v);
2805 s = zbc_num_add(&result, &temp, &result, 0);
2806 if (s) goto err;
2807 s = zbc_num_mul(&mult, &base, &mult, 0);
2808 if (s) goto err;
2809 }
2810
2811 s = zbc_num_div(&result, &mult, &result, digits);
2812 if (s) goto err;
2813 s = zbc_num_add(n, &result, n, digits);
2814 if (s) goto err;
2815
2816 if (n->len != 0) {
2817 if (n->rdx < digits)
2818 bc_num_extend(n, digits - n->rdx);
2819 } else
2820 bc_num_zero(n);
2821 err:
2822 bc_num_free(&result);
2823 int_err:
2824 bc_num_free(&mult);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002825}
2826
Denys Vlasenko10bde142018-12-27 18:23:58 +01002827static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002828{
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002829 size_t i;
2830
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002831 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002832 RETURN_STATUS(bc_error("bad number string"));
2833
2834 bc_num_zero(n);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002835 while (*val == '0')
2836 val++;
2837 for (i = 0; ; ++i) {
2838 if (val[i] == '\0')
2839 RETURN_STATUS(BC_STATUS_SUCCESS);
2840 if (val[i] != '.' && val[i] != '0')
2841 break;
2842 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002843
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002844 if (base_t == 10 || val[1] == '\0')
2845 // Decimal, or single-digit number
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002846 bc_num_parseDecimal(n, val);
2847 else
2848 bc_num_parseBase(n, val, base_t);
2849
2850 RETURN_STATUS(BC_STATUS_SUCCESS);
2851}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002852#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002853
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002854// p->lex_inbuf points to the current string to be parsed.
2855// if p->lex_inbuf points to '\0', it's either EOF or it points after
2856// last processed line's terminating '\n' (and more reading needs to be done
2857// to get next character).
2858//
2859// If you are in a situation where that is a possibility, call peek_inbuf().
2860// If necessary, it performs more reading and changes p->lex_inbuf,
2861// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2862// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2863// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2864//
2865// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2866// it returns current char and advances the pointer (if not EOF).
2867// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2868//
2869// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2870// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2871// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002872//
2873// bc cases to test interactively:
2874// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2875// 1 #comment/* - prints "1<newline>" at once
2876// 1 #comment" - prints "1<newline>" at once
2877// 1\#comment - error at once (\ is not a line continuation)
2878// 1 + /*"*/2 - prints "3<newline>" at once
2879// 1 + /*#*/2 - prints "3<newline>" at once
2880// "str\" - prints "str\" at once
2881// "str#" - prints "str#" at once
2882// "str/*" - prints "str/*" at once
2883// "str#\ - waits for second line
2884// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002885static char peek_inbuf(void)
2886{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002887 if (*G.prs.lex_inbuf == '\0'
2888 && G.prs.lex_input_fp
2889 ) {
2890 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2891 G.prs.lex_inbuf = G.input_buffer.v;
2892 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2893 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002894 }
2895 return *G.prs.lex_inbuf;
2896}
2897static char eat_inbuf(void)
2898{
2899 char c = peek_inbuf();
2900 if (c) G.prs.lex_inbuf++;
2901 return c;
2902}
2903
Denys Vlasenko10bde142018-12-27 18:23:58 +01002904static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002905{
2906 BcParse *p = &G.prs;
2907 char c;
2908
2909 // Try: echo -n '#foo' | bc
2910 p->lex = XC_LEX_WHITESPACE;
2911
Denys Vlasenko2747f612018-12-31 18:48:10 +01002912 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002913 // '\0' which isn't the EOF can only be seen after '\n'.
2914 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2915 p->lex_inbuf++;
2916}
2917
Denys Vlasenko10bde142018-12-27 18:23:58 +01002918static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002919{
2920 BcParse *p = &G.prs;
2921
2922 p->lex = XC_LEX_WHITESPACE;
2923 for (;;) {
2924 // We depend here on input being done in whole lines:
2925 // '\0' which isn't the EOF can only be seen after '\n'.
2926 char c = *p->lex_inbuf;
2927 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2928 break;
2929 if (!isspace(c))
2930 break;
2931 p->lex_inbuf++;
2932 }
2933}
2934
Denys Vlasenko10bde142018-12-27 18:23:58 +01002935static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002936{
2937 BcParse *p = &G.prs;
2938 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002939 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002940
2941 bc_vec_pop_all(&p->lex_strnumbuf);
2942 bc_vec_pushByte(&p->lex_strnumbuf, last);
2943
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002944// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002945// (Note: They must be capitals. Lower case letters are variable names.)
2946// Single digit numbers always have the value of the digit regardless of
2947// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2948// all input digits greater or equal to ibase to the value of ibase-1.
2949// This makes the number ZZZ always be the largest 3 digit number of the
2950// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002951// dc only allows A-F, the rules about single-char and multi-char are the same.
2952 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002953 pt = (last == '.');
2954 p->lex = XC_LEX_NUMBER;
2955 for (;;) {
2956 // We depend here on input being done in whole lines:
2957 // '\0' which isn't the EOF can only be seen after '\n'.
2958 char c = *p->lex_inbuf;
2959 check_c:
2960 if (c == '\0')
2961 break;
2962 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2963 p->lex_inbuf += 2;
2964 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01002965 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002966 c = peek_inbuf(); // force next line to be read
2967 goto check_c;
2968 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002969 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002970 if (c != '.') break;
2971 // if '.' was already seen, stop on second one:
2972 if (pt) break;
2973 pt = true;
2974 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002975 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002976 last = c;
2977 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2978 p->lex_inbuf++;
2979 }
2980 if (last == '.') // remove trailing '.' if any
2981 bc_vec_pop(&p->lex_strnumbuf);
2982 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2983
2984 G.err_line = G.prs.lex_line;
2985 RETURN_STATUS(BC_STATUS_SUCCESS);
2986}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002987#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002988
Denys Vlasenko10bde142018-12-27 18:23:58 +01002989static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002990{
2991 BcParse *p = &G.prs;
2992 size_t i;
2993 const char *buf;
2994
2995 p->lex = XC_LEX_NAME;
2996
2997 // Since names can't cross lines with \<newline>,
2998 // we depend on the fact that whole line is in the buffer
2999 i = 0;
3000 buf = p->lex_inbuf - 1;
3001 for (;;) {
3002 char c = buf[i];
3003 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
3004 i++;
3005 }
3006
3007#if 0 // We do not protect against people with gigabyte-long names
3008 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3009 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3010 if (i > BC_MAX_STRING)
3011 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
3012 }
3013#endif
3014 bc_vec_string(&p->lex_strnumbuf, i, buf);
3015
3016 // Increment the index. We minus 1 because it has already been incremented.
3017 p->lex_inbuf += i - 1;
3018
3019 //return BC_STATUS_SUCCESS;
3020}
3021
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003022IF_BC(static BC_STATUS zbc_lex_token(void);)
3023IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003024#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
3025#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003026
Denys Vlasenko10bde142018-12-27 18:23:58 +01003027static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003028{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003029 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003030 BcStatus s;
3031
Denys Vlasenko22314682019-01-01 21:50:14 +01003032 G.err_line = p->lex_line;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003033 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01003034//why?
3035// if (p->lex_last == XC_LEX_EOF)
3036// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
3038 // Loop until failure or we don't have whitespace. This
3039 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003040 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003041 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003042 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003043 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003044 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003045 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01003046 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01003047 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003048 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003049 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003050 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
3051 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01003052 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003053 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003054 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003055 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003056 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003057 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003058 } while (!s && p->lex == XC_LEX_WHITESPACE);
3059 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06003060
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003061 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003062}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003063#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003064
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003065#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003066static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003067{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003068 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01003069 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01003070 RETURN_STATUS(BC_STATUS_SUCCESS);
3071}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003072#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003073
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003074static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003075{
3076 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003077 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003078 if (s) RETURN_STATUS(s);
3079 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003080 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01003081 RETURN_STATUS(s);
3082}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003083#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003084
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003085static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003086{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003087 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003088 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003089 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003090 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003091
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003092 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3093 const char *keyword8 = bc_lex_kws[i].name8;
3094 unsigned j = 0;
3095 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3096 j++;
3097 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003098 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003099 if (keyword8[j] != '\0')
3100 continue;
3101 match:
3102 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003103 if (isalnum(buf[j]) || buf[j]=='_')
3104 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003105 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003106 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003107 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3108 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003109 }
3110
3111 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003112 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003113 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003114 }
3115
Denys Vlasenko10bde142018-12-27 18:23:58 +01003116 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003117 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003119 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003120 // Prevent this:
3121 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003122 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003123 // '
3124 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003125 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003126 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003127
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003128 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003129}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003130#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003131
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003132static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003133{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003134 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003135
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003136 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003137 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003138 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003139 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003140 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003141 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003142 }
3143 if (c == '"')
3144 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003145 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003146 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003147 dbg_lex("++p->lex_line=%zd", p->lex_line);
3148 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003149 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3150 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003151 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003152 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3153 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003154
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003155 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003156 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003157}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003158#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003159
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003160static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003161{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003162 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003163 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003164 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003165 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003166 with_and_without >>= 8; // store "with" value
3167 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003168 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003169}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003170#define parse_lex_by_checking_eq_sign(with, without) \
3171 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003172
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003173static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003174{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003175 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003176
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003177 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003178 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003179 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003180 char c;
3181
3182 p->lex_inbuf++;
3183 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003184 check_star:
3185 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003186 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003187 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003188 if (c == '/')
3189 break;
3190 goto check_star;
3191 }
3192 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003193 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003194 }
Denys Vlasenko22314682019-01-01 21:50:14 +01003195 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003196 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003197 dbg_lex("++p->lex_line=%zd", p->lex_line);
3198 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003200 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003201
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003202 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003203 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003204}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003205#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003206
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003207#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003208static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003209{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003210 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003211 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003212 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003213 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003214
3215 // This is the workhorse of the lexer.
3216 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003217// case '\0': // probably never reached
3218// p->lex_inbuf--;
3219// p->lex = XC_LEX_EOF;
3220// break;
3221 case '\n':
3222 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003223 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003224 p->lex = XC_LEX_NLINE;
3225 break;
3226 case '\t':
3227 case '\v':
3228 case '\f':
3229 case '\r':
3230 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003231 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003232 break;
3233 case '!':
3234 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3235 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3236 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003237 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003238 }
3239 break;
3240 case '"':
3241 s = zbc_lex_string();
3242 break;
3243 case '#':
3244 s = zbc_POSIX_does_not_allow("'#' script comments");
3245 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003246 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003247 break;
3248 case '%':
3249 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3250 break;
3251 case '&':
3252 c2 = *p->lex_inbuf;
3253 if (c2 == '&') {
3254 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3255 if (s) RETURN_STATUS(s);
3256 p->lex_inbuf++;
3257 p->lex = BC_LEX_OP_BOOL_AND;
3258 } else {
3259 p->lex = XC_LEX_INVALID;
3260 s = bc_error_bad_character('&');
3261 }
3262 break;
3263 case '(':
3264 case ')':
3265 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3266 break;
3267 case '*':
3268 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3269 break;
3270 case '+':
3271 c2 = *p->lex_inbuf;
3272 if (c2 == '+') {
3273 p->lex_inbuf++;
3274 p->lex = BC_LEX_OP_INC;
3275 } else
3276 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3277 break;
3278 case ',':
3279 p->lex = BC_LEX_COMMA;
3280 break;
3281 case '-':
3282 c2 = *p->lex_inbuf;
3283 if (c2 == '-') {
3284 p->lex_inbuf++;
3285 p->lex = BC_LEX_OP_DEC;
3286 } else
3287 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3288 break;
3289 case '.':
3290 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003291 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003292 else {
3293 p->lex = BC_LEX_KEY_LAST;
3294 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3295 }
3296 break;
3297 case '/':
3298 c2 = *p->lex_inbuf;
3299 if (c2 == '*')
3300 s = zbc_lex_comment();
3301 else
3302 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3303 break;
3304 case '0':
3305 case '1':
3306 case '2':
3307 case '3':
3308 case '4':
3309 case '5':
3310 case '6':
3311 case '7':
3312 case '8':
3313 case '9':
3314 case 'A':
3315 case 'B':
3316 case 'C':
3317 case 'D':
3318 case 'E':
3319 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003320 case 'G':
3321 case 'H':
3322 case 'I':
3323 case 'J':
3324 case 'K':
3325 case 'L':
3326 case 'M':
3327 case 'N':
3328 case 'O':
3329 case 'P':
3330 case 'Q':
3331 case 'R':
3332 case 'S':
3333 case 'T':
3334 case 'U':
3335 case 'V':
3336 case 'W':
3337 case 'X':
3338 case 'Y':
3339 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003340 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003341 break;
3342 case ';':
3343 p->lex = BC_LEX_SCOLON;
3344 break;
3345 case '<':
3346 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3347 break;
3348 case '=':
3349 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3350 break;
3351 case '>':
3352 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3353 break;
3354 case '[':
3355 case ']':
3356 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3357 break;
3358 case '\\':
3359 if (*p->lex_inbuf == '\n') {
3360 p->lex = XC_LEX_WHITESPACE;
3361 p->lex_inbuf++;
3362 } else
3363 s = bc_error_bad_character(c);
3364 break;
3365 case '^':
3366 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3367 break;
3368 case 'a':
3369 case 'b':
3370 case 'c':
3371 case 'd':
3372 case 'e':
3373 case 'f':
3374 case 'g':
3375 case 'h':
3376 case 'i':
3377 case 'j':
3378 case 'k':
3379 case 'l':
3380 case 'm':
3381 case 'n':
3382 case 'o':
3383 case 'p':
3384 case 'q':
3385 case 'r':
3386 case 's':
3387 case 't':
3388 case 'u':
3389 case 'v':
3390 case 'w':
3391 case 'x':
3392 case 'y':
3393 case 'z':
3394 s = zbc_lex_identifier();
3395 break;
3396 case '{':
3397 case '}':
3398 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3399 break;
3400 case '|':
3401 c2 = *p->lex_inbuf;
3402 if (c2 == '|') {
3403 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3404 if (s) RETURN_STATUS(s);
3405 p->lex_inbuf++;
3406 p->lex = BC_LEX_OP_BOOL_OR;
3407 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003408 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003409 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003410 }
3411 break;
3412 default:
3413 p->lex = XC_LEX_INVALID;
3414 s = bc_error_bad_character(c);
3415 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003416 }
3417
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003418 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003419}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003420#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003421#endif // ENABLE_BC
3422
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003423#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003424static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003425{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003426 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003427 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003428 xc_lex_whitespace(); // eats whitespace (but not newline)
3429 p->lex_inbuf++; // xc_lex_name() expects this
3430 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003431 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003432 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003433 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003434 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3435 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003436 }
3437
Denys Vlasenko29301232018-12-11 15:29:32 +01003438 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003439}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003440#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003441
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003442static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003443{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003444 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003445 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003446
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003447 p->lex = XC_LEX_STR;
3448 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003449
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003450 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003451 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003452 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003453 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003454 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003455 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003456 if (c == '[') depth++;
3457 if (c == ']')
3458 if (--depth == 0)
3459 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003460 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003461 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003462 dbg_lex("++p->lex_line=%zd", p->lex_line);
3463 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003464 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3465 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003466 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003467 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003468 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003469
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003470 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003471 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003472}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003473#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003474
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003475#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003476static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003477{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003478 static const //BcLexType - should be this type, but narrower type saves size:
3479 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003480 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003481 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 +01003482 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3483 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003484 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003485 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003486
Denys Vlasenko10bde142018-12-27 18:23:58 +01003487 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003488 BcStatus s;
3489 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003490 size_t i;
3491
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003492 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003493 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003494 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 }
3496
Denys Vlasenko81293c82018-12-24 01:53:55 +01003497 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003498 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003499 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003500 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003501 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003502 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003503 }
3504
3505 // This is the workhorse of the lexer.
3506 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003507// case '\0': // probably never reached
3508// p->lex = XC_LEX_EOF;
3509// break;
3510 case '\n':
3511 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3512 // (and "case '\n':" is not just empty here)
3513 // only to allow interactive dc have a way to exit
3514 // "parse" stage of "parse,execute" loop
3515 // on <enter>, not on _next_ token (which would mean
3516 // commands are not executed on pressing <enter>).
3517 // IOW: typing "1p<enter>" should print "1" _at once_,
3518 // not after some more input.
3519 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003520 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003521 p->lex = XC_LEX_NLINE;
3522 break;
3523 case '\t':
3524 case '\v':
3525 case '\f':
3526 case '\r':
3527 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003528 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003529 break;
3530 case '!':
3531 c2 = *p->lex_inbuf;
3532 if (c2 == '=')
3533 p->lex = XC_LEX_OP_REL_NE;
3534 else if (c2 == '<')
3535 p->lex = XC_LEX_OP_REL_LE;
3536 else if (c2 == '>')
3537 p->lex = XC_LEX_OP_REL_GE;
3538 else
3539 RETURN_STATUS(bc_error_bad_character(c));
3540 p->lex_inbuf++;
3541 break;
3542 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003543 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003544 break;
3545 case '.':
3546 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003547 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003548 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003549 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003550 break;
3551 case '0':
3552 case '1':
3553 case '2':
3554 case '3':
3555 case '4':
3556 case '5':
3557 case '6':
3558 case '7':
3559 case '8':
3560 case '9':
3561 case 'A':
3562 case 'B':
3563 case 'C':
3564 case 'D':
3565 case 'E':
3566 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003567 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003568 break;
3569 case '[':
3570 s = zdc_lex_string();
3571 break;
3572 default:
3573 p->lex = XC_LEX_INVALID;
3574 s = bc_error_bad_character(c);
3575 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003576 }
3577
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003578 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003579}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003580#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003581#endif // ENABLE_DC
3582
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003583static void xc_parse_push(unsigned i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003584{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003585 BcVec *code = &G.prs.func->code;
3586 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003587 bc_vec_pushByte(code, (uint8_t)i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003588}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003589
Denys Vlasenko10bde142018-12-27 18:23:58 +01003590static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003591{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003592#if 1
3593 BcVec *code = &G.prs.func->code;
3594 size_t pos = code->len;
3595 size_t len = strlen(name) + 1;
3596
3597 bc_vec_expand(code, pos + len);
3598 strcpy(code->v + pos, name);
3599 code->len = pos + len;
3600#else
3601 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003602 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003603 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003604 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003605#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003606}
3607
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003608// Indexes < 0xfc are encoded verbatim, else first byte is
3609// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3610// followed by that many bytes, lsb first.
3611// (The above describes 32-bit case).
3612#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3613
Denys Vlasenko53799502019-01-25 14:24:03 +01003614static void bc_vec_pushIndex(BcVec *v, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003615{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003616 size_t mask;
3617 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003618
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003619 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003620 if (idx < SMALL_INDEX_LIMIT) {
Denys Vlasenko53799502019-01-25 14:24:03 +01003621 bc_vec_pushByte(v, idx);
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003622 return;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003623 }
3624
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003625 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3626 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003627 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003628 if (idx & mask) break;
3629 mask >>= 8;
3630 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003631 }
3632 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003633
Denys Vlasenko53799502019-01-25 14:24:03 +01003634 bc_vec_pushByte(v, (SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003635
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003636 do {
Denys Vlasenko53799502019-01-25 14:24:03 +01003637 bc_vec_pushByte(v, (unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003638 idx >>= 8;
Denys Vlasenkoae6c44e2019-01-02 16:30:24 +01003639 } while (idx != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003640}
3641
Denys Vlasenko53799502019-01-25 14:24:03 +01003642static void xc_parse_pushIndex(size_t idx)
3643{
3644 bc_vec_pushIndex(&G.prs.func->code, idx);
3645}
3646
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003647static void xc_parse_pushInst_and_Index(unsigned inst, size_t idx)
3648{
3649 xc_parse_push(inst);
3650 xc_parse_pushIndex(idx);
3651}
3652
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003653#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003654static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003655{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003656 xc_parse_pushInst_and_Index(BC_INST_JUMP, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003657}
3658
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003659static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003660{
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003661 xc_parse_pushInst_and_Index(BC_INST_JUMP_ZERO, idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003662}
3663
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003664static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003665{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003666 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003667 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003668
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003669 xc_parse_pushInst_and_Index(XC_INST_STR, p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003670 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003671
Denys Vlasenko10bde142018-12-27 18:23:58 +01003672 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003673}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003674#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003675#endif
3676
Denys Vlasenko10bde142018-12-27 18:23:58 +01003677static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003678{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003679 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003680 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003681#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003682 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003683#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003684 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003685#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003686 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003687#endif
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003688 xc_parse_pushInst_and_Index(XC_INST_NUM, idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003689}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003690
Denys Vlasenko10bde142018-12-27 18:23:58 +01003691static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003692{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003693 G.prs.func = xc_program_func(G.prs.fidx);
3694 G.prs.lex_inbuf = text;
3695 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3696 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003697}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003698#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003699
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003700// Called when parsing or execution detects a failure,
3701// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003702static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003703{
3704 BcFunc *f;
3705 BcInstPtr *ip;
3706
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003707 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003708 bc_vec_pop_all(&G.prog.results);
3709
Denys Vlasenko10bde142018-12-27 18:23:58 +01003710 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003711 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003712 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003713}
3714
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003715// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003716// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003717static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003718{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003719 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003720 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003721 bc_func_free(p->func);
3722 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003723
Denys Vlasenko65e10462018-12-19 15:13:14 +01003724 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003725 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003726 }
3727
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003728 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003729 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003730
Denys Vlasenko503faf92018-12-20 16:24:18 +01003731 IF_BC(bc_vec_pop_all(&p->exits);)
3732 IF_BC(bc_vec_pop_all(&p->conds);)
3733 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003734
Denys Vlasenko10bde142018-12-27 18:23:58 +01003735 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003736}
3737
Denys Vlasenko10bde142018-12-27 18:23:58 +01003738static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003739{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003740 IF_BC(bc_vec_free(&G.prs.exits);)
3741 IF_BC(bc_vec_free(&G.prs.conds);)
3742 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003743 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003744}
3745
Denys Vlasenko10bde142018-12-27 18:23:58 +01003746static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003747{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003748 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003749 memset(p, 0, sizeof(BcParse));
3750
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003751 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003752 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3753 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3754 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003755
Denys Vlasenkofa210792018-12-19 19:35:40 +01003756 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003757 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003758}
3759
Denys Vlasenko10bde142018-12-27 18:23:58 +01003760static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003761{
Denys Vlasenko04715442018-12-21 00:10:26 +01003762 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003763 BcFunc f;
3764 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003765 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003766 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003767 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003768}
3769
3770#if ENABLE_BC
3771
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003772// Note: takes ownership of 'name' (must be malloced)
3773static size_t bc_program_addFunc(char *name)
3774{
3775 size_t idx;
3776 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003777 int inserted;
3778
3779 entry.name = name;
3780 entry.idx = G.prog.fns.len;
3781
3782 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3783 if (!inserted) free(name);
3784
3785 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3786 idx = entry_ptr->idx;
3787
3788 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003789 // There is already a function with this name.
3790 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003791 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003792 bc_func_free(func);
3793 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003794 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003795 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003796 }
3797
3798 return idx;
3799}
3800
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003801#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003802// We can calculate the conversion between tokens and exprs by subtracting the
3803// position of the first operator in the lex enum and adding the position of the
3804// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003805#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003806
Denys Vlasenko132d7c02019-01-08 19:29:35 +01003807static BC_STATUS zbc_parse_expr(uint8_t flags);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003808#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003809
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003810static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003811#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003812
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003813static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003814{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003815 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003816}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003817#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003818
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003819static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003820{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003821 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003822 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3823 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003824 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003825 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003826 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003827 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003828
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003829 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003830}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003831#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003832
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003833static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003834{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003835 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003836 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3837 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003838
3839 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003840 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003841 if (t == BC_LEX_LPAREN) break;
3842
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003843 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003844 if (l >= r && (l != r || !left)) break;
3845
Denys Vlasenko10bde142018-12-27 18:23:58 +01003846 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003848 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003849 }
3850
3851 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003852}
3853
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003854static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003855{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003856 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003857 BcLexType top;
3858
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003859 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003860 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003861 top = BC_PARSE_TOP_OP(p);
3862
3863 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003864 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003865
3866 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003867 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003868
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003869 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003870 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003871 top = BC_PARSE_TOP_OP(p);
3872 }
3873
3874 bc_vec_pop(&p->ops);
3875
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003876 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003877}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003878#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003879
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003880static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003881{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003882 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003883 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 size_t nparams;
3885
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003886 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003887 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3888
Denys Vlasenko10bde142018-12-27 18:23:58 +01003889 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003890 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003891
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003892 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003893 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003894 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003895 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003896 if (s) RETURN_STATUS(s);
3897 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003898 if (p->lex != BC_LEX_COMMA) {
3899 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003900 break;
3901 RETURN_STATUS(bc_error_bad_token());
3902 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003903 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003904 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 }
3906 }
3907
Denys Vlasenkocfc25462019-01-09 11:17:19 +01003908 xc_parse_pushInst_and_Index(BC_INST_CALL, nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003909
Denys Vlasenko26819db2018-12-12 16:08:46 +01003910 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003911}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003912#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003913
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003914// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003915static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003916{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003917 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003918 BcStatus s;
3919 BcId entry, *entry_ptr;
3920 size_t idx;
3921
3922 entry.name = name;
3923
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003924 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003925 if (s) goto err;
3926
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003927 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003928 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003929 goto err;
3930 }
3931
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003932 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003933
3934 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003935 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003936 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003937 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003938 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003939 free(name);
3940
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003941 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003942 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003943
Denys Vlasenko10bde142018-12-27 18:23:58 +01003944 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003945 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003946 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003947 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003948}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003949#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003951static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003952{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003953 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003954 BcStatus s;
3955 char *name;
3956
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003957 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003958 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003959 if (s) goto err;
3960
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003961 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003962 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003963 if (s) goto err;
3964
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003965 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003966 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003967 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003968 goto err;
3969 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003970 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003971 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003972 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003973 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003974 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003975 if (s) goto err;
3976 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003977 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003978 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003979 xc_parse_push(*type);
3980 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003981 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003982 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003983 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003984 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003985 goto err;
3986 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003987 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003988 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003989 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003990 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003991 xc_parse_push(XC_INST_VAR);
3992 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003993 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003994 }
3995
Denys Vlasenko26819db2018-12-12 16:08:46 +01003996 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003997 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003998 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003999 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004000}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004001#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004002
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004003static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004004{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004005 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004006 BcStatus s;
4007
Denys Vlasenko10bde142018-12-27 18:23:58 +01004008 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004009 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004010 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
Denys Vlasenko10bde142018-12-27 18:23:58 +01004012 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004013 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004014 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004015
Denys Vlasenko10bde142018-12-27 18:23:58 +01004016 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004017
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004018 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004019}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004020#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004021
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004022static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06004023{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004024 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004025 BcStatus s;
4026
Denys Vlasenko10bde142018-12-27 18:23:58 +01004027 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004028 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004029 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004030
4031 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
4032
Denys Vlasenko10bde142018-12-27 18:23:58 +01004033 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004034 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004035
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004036 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004037 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004038
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004039 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004040
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004041 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004042 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004043
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004044 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004045}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004046#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004047
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004048static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004049{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004050 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004051 BcStatus s;
4052
Denys Vlasenko10bde142018-12-27 18:23:58 +01004053 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004054 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004056 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004057 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004058 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004059 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004060 }
4061
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004062 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06004063 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4064
Denys Vlasenko10bde142018-12-27 18:23:58 +01004065 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004066 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004067
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004068 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004069 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004070 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01004071 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004072 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004073
Denys Vlasenko10bde142018-12-27 18:23:58 +01004074 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004075}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004076#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004077
Denys Vlasenko22314682019-01-01 21:50:14 +01004078static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004079{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004080 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004081 BcStatus s;
4082 BcLexType type;
4083 char inst;
4084 BcInst etype = *prev;
4085
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004086 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4087 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4088 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004089 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004090 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004091 xc_parse_push(inst);
4092 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004093 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004094 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004095
Denys Vlasenko10bde142018-12-27 18:23:58 +01004096 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004097 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004098 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004099
4100 // Because we parse the next part of the expression
4101 // right here, we need to increment this.
Denys Vlasenko22314682019-01-01 21:50:14 +01004102 *nexs = *nexs + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004103
4104 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004105 case XC_LEX_NAME:
4106 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
4107 break;
4108 case BC_LEX_KEY_IBASE:
4109 case BC_LEX_KEY_LAST:
4110 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004111 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4112 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004113 break;
4114 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004115 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004116 if (s) RETURN_STATUS(s);
4117 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004118 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004119 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01004120 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004121 break;
4122 default:
4123 s = bc_error_bad_token();
4124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004125 }
4126
Denys Vlasenko10bde142018-12-27 18:23:58 +01004127 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004128 }
4129
Denys Vlasenko26819db2018-12-12 16:08:46 +01004130 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004131}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004132#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004133
Denys Vlasenko22314682019-01-01 21:50:14 +01004134static int bc_parse_inst_isLeaf(BcInst p)
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004135{
4136 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4137 || p == BC_INST_INC_POST
4138 || p == BC_INST_DEC_POST
4139 ;
4140}
Denys Vlasenko22314682019-01-01 21:50:14 +01004141#define BC_PARSE_LEAF(prev, bin_last, rparen) \
4142 (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev)))
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004143
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004144static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
Denys Vlasenko22314682019-01-01 21:50:14 +01004145 bool rparen, bool bin_last, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004146{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004147 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004148 BcStatus s;
4149 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004150
Denys Vlasenko10bde142018-12-27 18:23:58 +01004151 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004152 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004153
Denys Vlasenko22314682019-01-01 21:50:14 +01004154 type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004155 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156
4157 // We can just push onto the op stack because this is the largest
4158 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004159 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004160 bc_vec_push(&p->ops, &type);
4161 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004162 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004163
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004164 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004165}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004166#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004167
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004168static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004169{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004170 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004171 BcStatus s;
4172 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004173
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004174 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004175 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004176 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004177 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004178 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004179 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004180 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004181 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004182 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004183 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004184 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004185 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004186 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004187 }
4188
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004189 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004190}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004191#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004192
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004193static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004194{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004195 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004196 BcStatus s;
4197 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004198
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004199 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004200 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004201 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004202
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004203 t = p->lex;
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004204 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004205 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004206 else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004207//TODO: if (p->func->voidfunc) ERROR
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004208 s = zbc_parse_expr(0);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004209 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004210
Denys Vlasenko96b5ec12019-01-03 23:34:36 +01004211 if (t != BC_LEX_LPAREN // "return EXPR", no ()
4212 || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b"
4213 ) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004214 s = zbc_POSIX_requires("parentheses around return expressions");
4215 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004216 }
4217
Denys Vlasenko10bde142018-12-27 18:23:58 +01004218 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004219 }
4220
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004221 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004222 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004223}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004224#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004225
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004226static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004227{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004228 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004229 size_t *label = bc_vec_item(&p->func->labels, idx);
4230 *label = p->func->code.len;
4231}
4232
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004233static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004234{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004235 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004236 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004237 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004238
4239 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004240 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004241 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004242 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004243
Denys Vlasenko10bde142018-12-27 18:23:58 +01004244 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004245 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004246 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004247 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004248 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004249
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004250 // Encode "if zero, jump to ..."
4251 // Pushed value (destination of the jump) is uninitialized,
4252 // will be rewritten to be address of "end of if()" or of "else".
4253 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004254 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004255
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004256 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004257 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004258
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004259 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4260 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004261 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004262
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004263 // Encode "after then_stmt, jump to end of if()"
4264 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4265 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004266 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004267
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004268 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 +01004269 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004270
Denys Vlasenko15850832018-12-16 21:40:54 +01004271 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004272
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004273 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004274 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004275 }
4276
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004277 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 +01004278 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004279
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004280 dbg_lex_done("%s:%d done", __func__, __LINE__);
4281 RETURN_STATUS(s);
4282}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004283#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004284
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004285static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004286{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004287 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004288 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004289 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004290 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004291
Denys Vlasenko10bde142018-12-27 18:23:58 +01004292 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004293 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004294 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004295 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004296 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004297
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004298 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004299 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004300 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004301
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004302 bc_vec_push(&p->exits, &ip_idx);
4303 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004305 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004306 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004307 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004308
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004309 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004311 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004312 if (s) RETURN_STATUS(s);
4313
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004314 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004315 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004316
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004317 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004318 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004319
4320 bc_vec_pop(&p->exits);
4321 bc_vec_pop(&p->conds);
4322
4323 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004324}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004325#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004326
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004327static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004328{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004329 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004330 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004331 size_t cond_idx, exit_idx, body_idx, update_idx;
4332
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004333 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004334 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004335 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004336 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004337 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004338 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004339
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004340 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004341 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004342 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004343 if (s) RETURN_STATUS(s);
4344 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004345 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4346 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004347 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004348
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004349 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004350 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004351 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004352
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004353 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004354 update_idx = cond_idx + 1;
4355 body_idx = update_idx + 1;
4356 exit_idx = body_idx + 1;
4357
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004358 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004359 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004360 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004361 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004362 // This is safe to set because the current token is a semicolon,
4363 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004364 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004365 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004366 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004367 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004368 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004369
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004370 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004371
Denys Vlasenko10bde142018-12-27 18:23:58 +01004372 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004373 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004374
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004375 bc_parse_pushJUMP_ZERO(exit_idx);
4376 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004377
Gavin Howard01055ba2018-11-03 11:00:21 -06004378 bc_vec_push(&p->conds, &update_idx);
4379 bc_vec_push(&p->func->labels, &p->func->code.len);
4380
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004381 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004382 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004383 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004384 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004385 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004386 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004387 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4388 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004389 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004390
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004391 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004392 bc_vec_push(&p->func->labels, &p->func->code.len);
4393
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004394 bc_vec_push(&p->exits, &exit_idx);
4395 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004396
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004397 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004398 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004399
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004400 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004401 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004402
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004403 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004404 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004405
4406 bc_vec_pop(&p->exits);
4407 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004408
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004409 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004410}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004411#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004412
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004413static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004414{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004415 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004416 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004417
Gavin Howard01055ba2018-11-03 11:00:21 -06004418 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004419 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4420 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004421 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004422 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004423 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004424 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004425 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004426
Denys Vlasenko10bde142018-12-27 18:23:58 +01004427 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004428}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004429#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004430
Denys Vlasenko53799502019-01-25 14:24:03 +01004431static BC_STATUS zbc_func_insert(BcFunc *f, char *name, BcType type)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004432{
4433 BcId *autoid;
4434 BcId a;
4435 size_t i;
4436
4437 autoid = (void*)f->autos.v;
4438 for (i = 0; i < f->autos.len; i++, autoid++) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004439 if (strcmp(name, autoid->name) == 0
Denys Vlasenko53799502019-01-25 14:24:03 +01004440 && type == (BcType) autoid->idx
Denys Vlasenko22314682019-01-01 21:50:14 +01004441 ) {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004442 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko22314682019-01-01 21:50:14 +01004443 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004444 }
4445
Denys Vlasenko53799502019-01-25 14:24:03 +01004446 a.idx = type;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004447 a.name = name;
4448
4449 bc_vec_push(&f->autos, &a);
4450
4451 RETURN_STATUS(BC_STATUS_SUCCESS);
4452}
4453#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4454
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004455static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004456{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004457 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004458 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01004459 bool comma, voidfunc;
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 char *name;
4461
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004462 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004463 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004464 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004465 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004466 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004467
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004468 // To be maximally both POSIX and GNU-compatible,
4469 // "void" is not treated as a normal keyword:
4470 // you can have variable named "void", and even a function
4471 // named "void": "define void() { return 6; }" is ok.
4472 // _Only_ "define void f() ..." syntax treats "void"
4473 // specially.
4474 voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004475
Denys Vlasenko10bde142018-12-27 18:23:58 +01004476 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004477 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004478
4479 voidfunc = (voidfunc && p->lex == XC_LEX_NAME);
4480 if (voidfunc) {
4481 s = zxc_lex_next();
4482 if (s) RETURN_STATUS(s);
4483 }
4484
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004485 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko22314682019-01-01 21:50:14 +01004486 RETURN_STATUS(bc_error_bad_function_definition());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004487
4488 p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v));
4489 p->func = xc_program_func(p->fidx);
4490 p->func->voidfunc = voidfunc;
4491
Denys Vlasenko10bde142018-12-27 18:23:58 +01004492 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004493 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004494
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004495 comma = false;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004496 while (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004497 BcType t = BC_TYPE_VAR;
4498
4499 if (p->lex == XC_LEX_OP_MULTIPLY) {
4500 t = BC_TYPE_REF;
4501 s = zxc_lex_next();
4502 if (s) RETURN_STATUS(s);
4503 s = zbc_POSIX_does_not_allow("references");
4504 if (s) RETURN_STATUS(s);
4505 }
4506
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004507 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004508 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004509
4510 ++p->func->nparams;
4511
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004512 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004513 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004514 if (s) goto err;
4515
Denys Vlasenko53799502019-01-25 14:24:03 +01004516 if (p->lex == BC_LEX_LBRACKET) {
4517 if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004518 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004519 if (s) goto err;
4520
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004521 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004522 s = bc_error_bad_function_definition();
Gavin Howard01055ba2018-11-03 11:00:21 -06004523 goto err;
4524 }
4525
Denys Vlasenko10bde142018-12-27 18:23:58 +01004526 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004527 if (s) goto err;
4528 }
Denys Vlasenko53799502019-01-25 14:24:03 +01004529 else if (t == BC_TYPE_REF) {
4530 s = bc_error_at("vars can't be references");
4531 goto err;
4532 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004533
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004534 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004535 if (comma) {
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 }
4539
Denys Vlasenko53799502019-01-25 14:24:03 +01004540 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 if (s) goto err;
4542 }
4543
Denys Vlasenko22314682019-01-01 21:50:14 +01004544 if (comma) RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004545
Denys Vlasenko10bde142018-12-27 18:23:58 +01004546 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004547 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004548
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004549 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004550 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004551 if (s) RETURN_STATUS(s);
4552 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004553
Denys Vlasenko202dd192018-12-16 17:30:35 +01004554 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004555 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004556 if (s) RETURN_STATUS(s);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01004557 // GNU bc requires a {} block even if function body has single stmt, enforce this
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004558 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004559 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004560
4561 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004562 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004563 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004564 if (s) RETURN_STATUS(s);
4565
Denys Vlasenko10bde142018-12-27 18:23:58 +01004566 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004567
4568 // Subsequent code generation is into main program
4569 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004570 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004571
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004572 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004573 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004574 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004575 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004577 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004579#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004580
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004581static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004582{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004583 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 char *name;
4586
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004587 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004588 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004589 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004590
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004591 for (;;) {
Denys Vlasenko53799502019-01-25 14:24:03 +01004592 BcType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004593
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004594 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004595 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004596
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004597 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004598 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004599 if (s) goto err;
4600
Denys Vlasenko53799502019-01-25 14:24:03 +01004601 t = BC_TYPE_VAR;
4602 if (p->lex == BC_LEX_LBRACKET) {
4603 t = BC_TYPE_ARRAY;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004604 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004605 if (s) goto err;
4606
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004607 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004608 s = bc_error_at("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 goto err;
4610 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004611 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 if (s) goto err;
4613 }
4614
Denys Vlasenko53799502019-01-25 14:24:03 +01004615 s = zbc_func_insert(p->func, name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004617
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004618 if (p->lex == XC_LEX_NLINE
4619 || p->lex == BC_LEX_SCOLON
4620 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004621 ) {
4622 break;
4623 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004624 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko22314682019-01-01 21:50:14 +01004625 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004626 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004627 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 }
4629
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004630 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004631 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004632 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004633 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004634 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004635 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004636}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004637#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004638
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004639#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004640static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004641{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004642 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004643 BcStatus s = BC_STATUS_SUCCESS;
4644
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004645 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004646
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004647 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004648 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004649 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004650 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004651 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004652 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004653 RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004654 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004655
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004656 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004657 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4658 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004659 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004660 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004661 } while (p->lex == XC_LEX_NLINE);
4662 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004663 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004664 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004665 if (s) RETURN_STATUS(s);
4666 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004667 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004668 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004669 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004670 if (s) RETURN_STATUS(s);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004671 // Check that next token is a correct stmt delimiter -
4672 // disallows "print 1 print 2" and such.
4673 if (p->lex == BC_LEX_RBRACE)
4674 break;
4675 if (p->lex != BC_LEX_SCOLON
4676 && p->lex != XC_LEX_NLINE
4677 ) {
4678 RETURN_STATUS(bc_error_at("bad statement terminator"));
4679 }
4680 s = zxc_lex_next();
4681 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004682 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004683 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004684 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4685 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 }
4687
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004688 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4689 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004690 case XC_LEX_OP_MINUS:
4691 case BC_LEX_OP_INC:
4692 case BC_LEX_OP_DEC:
4693 case BC_LEX_OP_BOOL_NOT:
4694 case BC_LEX_LPAREN:
4695 case XC_LEX_NAME:
4696 case XC_LEX_NUMBER:
4697 case BC_LEX_KEY_IBASE:
4698 case BC_LEX_KEY_LAST:
4699 case BC_LEX_KEY_LENGTH:
4700 case BC_LEX_KEY_OBASE:
4701 case BC_LEX_KEY_READ:
4702 case BC_LEX_KEY_SCALE:
4703 case BC_LEX_KEY_SQRT:
4704 s = zbc_parse_expr(BC_PARSE_PRINT);
4705 break;
4706 case XC_LEX_STR:
4707 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004708 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004709 break;
4710 case BC_LEX_KEY_BREAK:
4711 case BC_LEX_KEY_CONTINUE:
4712 s = zbc_parse_break_or_continue(p->lex);
4713 break;
4714 case BC_LEX_KEY_FOR:
4715 s = zbc_parse_for();
4716 break;
4717 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004718 xc_parse_push(BC_INST_HALT);
4719 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004720 break;
4721 case BC_LEX_KEY_IF:
4722 s = zbc_parse_if();
4723 break;
4724 case BC_LEX_KEY_LIMITS:
4725 // "limits" is a compile-time command,
4726 // the output is produced at _parse time_.
4727 printf(
4728 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4729 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4730 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4731 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
Denys Vlasenkoe05ec6e2019-01-04 16:26:19 +01004732 // "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" - GNU bc does not show this
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004733 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4734 "Number of vars = "BC_MAX_VARS_STR "\n"
4735 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004736 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004737 break;
4738 case BC_LEX_KEY_PRINT:
4739 s = zbc_parse_print();
4740 break;
4741 case BC_LEX_KEY_QUIT:
4742 // "quit" is a compile-time command. For example,
4743 // "if (0 == 1) quit" terminates when parsing the statement,
4744 // not when it is executed
4745 QUIT_OR_RETURN_TO_MAIN;
4746 case BC_LEX_KEY_RETURN:
4747 if (!p->in_funcdef)
4748 RETURN_STATUS(bc_error("'return' not in a function"));
4749 s = zbc_parse_return();
4750 break;
4751 case BC_LEX_KEY_WHILE:
4752 s = zbc_parse_while();
4753 break;
4754 default:
4755 s = bc_error_bad_token();
4756 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004757 }
4758
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004759 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004760 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004761}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004762#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004763
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004764static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004765{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004766 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004767 BcStatus s;
4768
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004769 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01004770//why?
4771// if (p->lex == XC_LEX_EOF)
4772// s = bc_error("end of file");
4773// else
4774 if (p->lex == BC_LEX_KEY_DEFINE) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004775 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004776 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004777 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004778 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004779 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004780 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004781
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004782 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004783 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004784}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004785#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004786
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004787#undef zbc_parse_expr
4788static BC_STATUS zbc_parse_expr(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004789{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004790 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004791 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004792 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004793 unsigned nparens, nrelops;
Denys Vlasenko22314682019-01-01 21:50:14 +01004794 bool paren_first, rprn, assign, bin_last, incdec;
Gavin Howard01055ba2018-11-03 11:00:21 -06004795
Denys Vlasenko17df8822018-12-14 23:00:24 +01004796 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004797 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004798 nparens = nrelops = 0;
Denys Vlasenko22314682019-01-01 21:50:14 +01004799 rprn = assign = incdec = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004800 bin_last = true;
4801
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004802 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004803 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004804 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004805 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004806
4807 if (!lex_allowed_in_bc_expr(t))
4808 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004809
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004810 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004811 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004812 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004813 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004814 case BC_LEX_OP_INC:
4815 case BC_LEX_OP_DEC:
4816 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004817 if (incdec) RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko22314682019-01-01 21:50:14 +01004818 s = zbc_parse_incdec(&prev, &nexprs, flags);
4819 incdec = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004820 rprn = bin_last = false;
4821 //get_token = false; - already is
4822 break;
4823 case XC_LEX_OP_MINUS:
4824 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004825 s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004826 rprn = false;
4827 //get_token = false; - already is
4828 bin_last = (prev == XC_INST_MINUS);
Denys Vlasenko22314682019-01-01 21:50:14 +01004829 if (bin_last) incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004830 break;
4831 case BC_LEX_OP_ASSIGN_POWER:
4832 case BC_LEX_OP_ASSIGN_MULTIPLY:
4833 case BC_LEX_OP_ASSIGN_DIVIDE:
4834 case BC_LEX_OP_ASSIGN_MODULUS:
4835 case BC_LEX_OP_ASSIGN_PLUS:
4836 case BC_LEX_OP_ASSIGN_MINUS:
4837 case BC_LEX_OP_ASSIGN:
4838 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4839 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4840 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4841 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4842 ) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004843 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004844 }
4845 // Fallthrough.
4846 case XC_LEX_OP_POWER:
4847 case XC_LEX_OP_MULTIPLY:
4848 case XC_LEX_OP_DIVIDE:
4849 case XC_LEX_OP_MODULUS:
4850 case XC_LEX_OP_PLUS:
4851 case XC_LEX_OP_REL_EQ:
4852 case XC_LEX_OP_REL_LE:
4853 case XC_LEX_OP_REL_GE:
4854 case XC_LEX_OP_REL_NE:
4855 case XC_LEX_OP_REL_LT:
4856 case XC_LEX_OP_REL_GT:
4857 case BC_LEX_OP_BOOL_NOT:
4858 case BC_LEX_OP_BOOL_OR:
4859 case BC_LEX_OP_BOOL_AND:
4860 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004861 if (t == BC_LEX_OP_BOOL_NOT) {
4862 if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004863 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko22314682019-01-01 21:50:14 +01004864 } else if (prev == XC_INST_BOOL_NOT) {
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004865 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004866 }
Denys Vlasenko22314682019-01-01 21:50:14 +01004867
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004868 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4869 prev = BC_TOKEN_2_INST(t);
4870 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko22314682019-01-01 21:50:14 +01004871 rprn = incdec = false;
4872 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004873 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4874 break;
4875 case BC_LEX_LPAREN:
4876 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004877 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004878 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004879 bc_vec_push(&p->ops, &t);
4880 nparens++;
4881 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004882 rprn = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004883 break;
4884 case BC_LEX_RPAREN:
4885 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenkoa1698a12019-01-08 19:32:38 +01004886//why?
4887// if (p->lex_last == BC_LEX_LPAREN) {
4888// RETURN_STATUS(bc_error_at("empty expression"));
4889// }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004890 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004891 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004892 if (nparens == 0) {
4893 goto exit_loop;
4894 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004895 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4896 nparens--;
4897 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004898 rprn = true;
4899 bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004900 break;
4901 case XC_LEX_NAME:
4902 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004903 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004904 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004905 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko22314682019-01-01 21:50:14 +01004906 rprn = (prev == BC_INST_CALL);
4907 bin_last = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004908 //get_token = false; - already is
4909 nexprs++;
4910 break;
4911 case XC_LEX_NUMBER:
4912 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004913 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004914 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004915 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004916 prev = XC_INST_NUM;
4917 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004918 rprn = bin_last = false;
4919 nexprs++;
4920 break;
4921 case BC_LEX_KEY_IBASE:
4922 case BC_LEX_KEY_LAST:
4923 case BC_LEX_KEY_OBASE:
4924 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004925 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004926 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004927 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004928 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004929 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004930 rprn = bin_last = false;
4931 nexprs++;
4932 break;
4933 case BC_LEX_KEY_LENGTH:
4934 case BC_LEX_KEY_SQRT:
4935 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004936 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004937 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004938 s = zbc_parse_builtin(t, flags, &prev);
4939 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004940 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004941 nexprs++;
4942 break;
4943 case BC_LEX_KEY_READ:
4944 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004945 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004946 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004947 s = zbc_parse_read();
4948 prev = XC_INST_READ;
4949 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004950 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004951 nexprs++;
4952 break;
4953 case BC_LEX_KEY_SCALE:
4954 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004955 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004956 RETURN_STATUS(bc_error_bad_expression());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004957 s = zbc_parse_scale(&prev, flags);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004958 //get_token = false; - already is
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004959 rprn = bin_last = false;
4960 nexprs++;
4961 break;
4962 default:
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004963 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004964 }
4965
Denys Vlasenkobb116032018-12-25 01:16:37 +01004966 if (s || G_interrupt) // error, or ^C: stop parsing
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004967 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004968 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004969 s = zxc_lex_next();
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004970 if (s) RETURN_STATUS(s);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004971 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004972 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004973 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004974
4975 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004976 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004977 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004978
4979 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004980 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenko10bde142018-12-27 18:23:58 +01004982 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004983
Denys Vlasenko69560f42018-12-24 14:14:23 +01004984 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004985 bc_vec_pop(&p->ops);
4986 }
4987
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004988 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004989 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06004990
Gavin Howard01055ba2018-11-03 11:00:21 -06004991 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004992 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004993 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004994 if (s) RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004995 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004996 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004997 s = zbc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko132d7c02019-01-08 19:29:35 +01004998 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 }
5000
5001 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01005002 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01005003 xc_parse_push(XC_INST_PRINT);
5004 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005005 }
5006
Denys Vlasenko17df8822018-12-14 23:00:24 +01005007 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko132d7c02019-01-08 19:29:35 +01005008 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005009}
Denys Vlasenko132d7c02019-01-08 19:29:35 +01005010#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005011
Gavin Howard01055ba2018-11-03 11:00:21 -06005012#endif // ENABLE_BC
5013
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005014#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005015
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005016static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005017{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005018 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06005019 BcStatus 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);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005023 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005024
Denys Vlasenko10bde142018-12-27 18:23:58 +01005025 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005026
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005027 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005028}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005029#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005030
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005031static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005032{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005033 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005034 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01005035 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005036
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005037 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06005038
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005039 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkocfc25462019-01-09 11:17:19 +01005040 xc_parse_pushInst_and_Index(XC_INST_STR, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005041 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005042
Brian Foleyb64470b2019-09-05 10:50:13 +02005043 // Add an empty function so that if zdc_program_execStr ever needs to
5044 // parse the string into code (from the 'x' command) there's somewhere
5045 // to store the bytecode.
Denys Vlasenko10bde142018-12-27 18:23:58 +01005046 xc_program_add_fn();
5047 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005048
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005049 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06005050}
5051
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005052static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06005053{
5054 BcStatus s;
5055
Denys Vlasenko10bde142018-12-27 18:23:58 +01005056 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005058 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005059 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005060 }
5061
5062 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005063 xc_parse_push(DC_INST_SWAP);
5064 xc_parse_push(XC_INST_ASSIGN);
5065 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 }
5067
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005068 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005069}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005070#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005071
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005072static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005073{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005074 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06005075 BcStatus s;
5076
Denys Vlasenko10bde142018-12-27 18:23:58 +01005077 xc_parse_push(inst);
5078 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06005079
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005080 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005081 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005082
Denys Vlasenko10bde142018-12-27 18:23:58 +01005083 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005084 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005085
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005086 // Note that 'else' part can not be on the next line:
5087 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
5088 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005089 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005090 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005091 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005092 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005093 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005094 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005095 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005096
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005097 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005098}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005099#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005100
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005101static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005102{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005103 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005104 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005105 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06005106
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005107 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005108 s = BC_STATUS_SUCCESS;
5109 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005110 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005111 case XC_LEX_OP_REL_EQ:
5112 case XC_LEX_OP_REL_LE:
5113 case XC_LEX_OP_REL_GE:
5114 case XC_LEX_OP_REL_NE:
5115 case XC_LEX_OP_REL_LT:
5116 case XC_LEX_OP_REL_GT:
5117 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
5118 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
5119 get_token = false;
5120 break;
5121 case DC_LEX_SCOLON:
5122 case DC_LEX_COLON:
5123 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
5124 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
5125 break;
5126 case XC_LEX_STR:
5127 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
5128 dc_parse_string();
5129 break;
5130 case XC_LEX_NEG:
5131 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005132 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005133 if (s) RETURN_STATUS(s);
5134 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005135 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005136 xc_parse_pushNUM();
5137 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005138 break;
5139 case XC_LEX_NUMBER:
5140 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005141 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005142 break;
5143 case DC_LEX_READ:
5144 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005145 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005146 break;
5147 case DC_LEX_OP_ASSIGN:
5148 case DC_LEX_STORE_PUSH:
5149 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
5150 assign = (t == DC_LEX_OP_ASSIGN);
5151 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
5152 s = zdc_parse_mem(inst, true, assign);
5153 break;
5154 case DC_LEX_LOAD:
5155 case DC_LEX_LOAD_POP:
5156 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
5157 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
5158 s = zdc_parse_mem(inst, true, false);
5159 break;
5160 case DC_LEX_STORE_IBASE:
5161 case DC_LEX_STORE_SCALE:
5162 case DC_LEX_STORE_OBASE:
5163 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5164 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5165 s = zdc_parse_mem(inst, false, true);
5166 break;
5167 default:
5168 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5169 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005170 }
5171
Denys Vlasenko10bde142018-12-27 18:23:58 +01005172 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005173
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005174 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005175 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005176}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005177#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005178
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005179static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005180{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005181 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005182 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005183
Denys Vlasenko6842c602019-01-04 05:41:47 +01005184 if (p->lex == XC_LEX_NLINE)
5185 RETURN_STATUS(zxc_lex_next());
5186
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005187 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005188 if (i >= 0) {
5189 BcInst inst = dc_LEX_to_INST[i];
5190 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005191 xc_parse_push(inst);
5192 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005193 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005194 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005195 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005196}
5197#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5198
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005199static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005200{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005201 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005202 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5203 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005204 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005205 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005206 }
5207
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005208 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005209 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005210}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005211#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005212
Gavin Howard01055ba2018-11-03 11:00:21 -06005213#endif // ENABLE_DC
5214
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005215//
5216// Execution engine
5217//
5218
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005219#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5220#define BC_PROG_NUM(r, n) \
5221 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5222
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005223#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5224#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5225
Denys Vlasenko53799502019-01-25 14:24:03 +01005226static size_t xc_program_index(char *code, size_t *bgn)
5227{
5228 unsigned char *bytes = (void*)(code + *bgn);
5229 unsigned amt;
5230 unsigned i;
5231 size_t res;
5232
5233 amt = *bytes++;
5234 if (amt < SMALL_INDEX_LIMIT) {
5235 *bgn += 1;
5236 return amt;
5237 }
5238 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
5239 *bgn += amt + 1;
5240
5241 res = 0;
5242 i = 0;
5243 do {
5244 res |= (size_t)(*bytes++) << i;
5245 i += 8;
5246 } while (--amt != 0);
5247
5248 return res;
5249}
5250
5251static char *xc_program_name(char *code, size_t *bgn)
5252{
5253 code += *bgn;
5254 *bgn += strlen(code) + 1;
5255
5256 return xstrdup(code);
5257}
5258
5259static BcVec* xc_program_dereference(BcVec *vec)
5260{
5261 BcVec *v;
5262 size_t vidx, nidx, i = 0;
5263
5264 //assert(vec->size == sizeof(uint8_t));
5265
5266 vidx = xc_program_index(vec->v, &i);
5267 nidx = xc_program_index(vec->v, &i);
5268
5269 v = bc_vec_item(&G.prog.arrs, vidx);
5270 v = bc_vec_item(v, nidx);
5271
5272 //assert(v->size != sizeof(uint8_t));
5273
5274 return v;
5275}
5276
5277static BcVec* xc_program_search(char *id, BcType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06005278{
Gavin Howard01055ba2018-11-03 11:00:21 -06005279 BcId e, *ptr;
5280 BcVec *v, *map;
5281 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005282 int new;
Denys Vlasenko53799502019-01-25 14:24:03 +01005283 bool var = (type == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005284
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005285 v = var ? &G.prog.vars : &G.prog.arrs;
5286 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005287
5288 e.name = id;
5289 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005290 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005291
5292 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005293 BcVec v2;
5294 bc_array_init(&v2, var);
5295 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005296 }
5297
5298 ptr = bc_vec_item(map, i);
5299 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005300 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005301}
5302
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005303// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005304static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005305{
Gavin Howard01055ba2018-11-03 11:00:21 -06005306 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005307 case XC_RESULT_STR:
5308 case XC_RESULT_TEMP:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005309 IF_BC(case BC_RESULT_VOID:)
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005310 case XC_RESULT_IBASE:
5311 case XC_RESULT_SCALE:
5312 case XC_RESULT_OBASE:
5313 *num = &r->d.n;
5314 break;
5315 case XC_RESULT_CONSTANT: {
5316 BcStatus s;
5317 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005318 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005319
Denys Vlasenko10bde142018-12-27 18:23:58 +01005320 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005321 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005322
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005323 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005324
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005325 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005326 if (s) {
5327 bc_num_free(&r->d.n);
5328 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005329 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005330 *num = &r->d.n;
5331 r->t = XC_RESULT_TEMP;
5332 break;
5333 }
5334 case XC_RESULT_VAR:
5335 case XC_RESULT_ARRAY:
5336 case XC_RESULT_ARRAY_ELEM: {
Denys Vlasenko53799502019-01-25 14:24:03 +01005337 BcType type = (r->t == XC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY;
5338 BcVec *v = xc_program_search(r->d.id.name, type);
5339 void *p = bc_vec_top(v);
5340
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005341 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko53799502019-01-25 14:24:03 +01005342 size_t idx = r->d.id.idx;
5343
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005344 v = p;
Denys Vlasenko53799502019-01-25 14:24:03 +01005345 if (v->size == sizeof(uint8_t))
5346 v = xc_program_dereference(v);
5347 //assert(v->size == sizeof(BcNum));
5348 if (v->len <= idx)
5349 bc_array_expand(v, idx + 1);
5350 *num = bc_vec_item(v, idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005351 } else {
5352 *num = p;
5353 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005354 break;
5355 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005356#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005357 case BC_RESULT_LAST:
5358 *num = &G.prog.last;
5359 break;
5360 case BC_RESULT_ONE:
5361 *num = &G.prog.one;
5362 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005363#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005364#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005365 default:
5366 // Testing the theory that dc does not reach LAST/ONE
5367 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005368#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005369 }
5370
Denys Vlasenko29301232018-12-11 15:29:32 +01005371 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005372}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005373#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005374
Denys Vlasenko10bde142018-12-27 18:23:58 +01005375static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005376 BcResult **r, BcNum **rn, bool assign)
5377{
5378 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005379 BcResultType lt, rt;
5380
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005381 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005382 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005383
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005384 *r = bc_vec_item_rev(&G.prog.results, 0);
5385 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005386
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005387 s = zxc_program_num(*l, ln);
5388 if (s) RETURN_STATUS(s);
5389 s = zxc_program_num(*r, rn);
5390 if (s) RETURN_STATUS(s);
5391
Gavin Howard01055ba2018-11-03 11:00:21 -06005392 lt = (*l)->t;
5393 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005394
5395 // We run this again under these conditions in case any vector has been
5396 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005397 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005398 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005399 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005400 }
5401
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005402 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005403 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005404 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005405 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005406
Denys Vlasenko29301232018-12-11 15:29:32 +01005407 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005408}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005409#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005410
Denys Vlasenko10bde142018-12-27 18:23:58 +01005411static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005412{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005413 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005414 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005415 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005416}
5417
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005418// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005419static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005420{
5421 BcStatus s;
5422
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005423 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005424 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005425 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005426
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005427 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005428 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005429
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005430 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005431 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005432
Denys Vlasenko29301232018-12-11 15:29:32 +01005433 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005434}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005435#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005436
Denys Vlasenko10bde142018-12-27 18:23:58 +01005437static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005438{
5439 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005440 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005441}
5442
Denys Vlasenko10bde142018-12-27 18:23:58 +01005443static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005444{
5445 BcStatus s;
5446 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005447 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005448
Denys Vlasenko10bde142018-12-27 18:23:58 +01005449 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005450 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005451 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005452
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005453 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005454 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 -06005455 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005456 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005457
Denys Vlasenko259137d2018-12-11 19:42:05 +01005458 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005459 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005460 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005461 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005462}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005463#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005464
Denys Vlasenko10bde142018-12-27 18:23:58 +01005465static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005466{
5467 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005468 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005469 BcVec buf;
5470 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005471 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005472
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005473 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005474 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005475
Denys Vlasenko10bde142018-12-27 18:23:58 +01005476 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005477 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005478
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005479 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005480 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005481 //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 +01005482
Denys Vlasenko10bde142018-12-27 18:23:58 +01005483 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005484 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005485 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005486 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005487 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005488 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005489 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005490 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005491 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005492 s = bc_error_at("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005493 goto exec_err;
5494 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005495 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005496
5497 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005498 ip.inst_idx = 0;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005499 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005500
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005501 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005502 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005503 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005504 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005505 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005506}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005507#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005508
Denys Vlasenko10bde142018-12-27 18:23:58 +01005509static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005510{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005511#if ENABLE_DC
Denys Vlasenko266bec82019-01-02 05:03:53 +01005512 if (!str[0] && IS_DC) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005513 // Example: echo '[]ap' | dc
5514 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005515 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005516 return;
5517 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005518#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005519 while (*str) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005520 char c = *str++;
5521 if (c == '\\') {
5522 static const char esc[] ALIGN1 = "nabfrt""e\\";
5523 char *n;
5524
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005525 c = *str++;
Brian Foley10509a72019-09-05 10:53:21 +02005526 n = strchr(esc, c); // note: if c is NUL, n = \0 at end of esc
5527 if (!n || !c) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005528 // Just print the backslash and following character
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005529 bb_putchar('\\');
5530 ++G.prog.nchars;
Brian Foley10509a72019-09-05 10:53:21 +02005531 // But if we're at the end of the string, stop
5532 if (!c) break;
Denys Vlasenko266bec82019-01-02 05:03:53 +01005533 } else {
5534 if (n - esc == 0) // "\n" ?
5535 G.prog.nchars = SIZE_MAX;
5536 c = "\n\a\b\f\r\t""\\\\""\\"[n - esc];
5537 // n a b f r t e \ \<end of line>
Gavin Howard01055ba2018-11-03 11:00:21 -06005538 }
5539 }
Denys Vlasenko266bec82019-01-02 05:03:53 +01005540 putchar(c);
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005541 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005542 }
5543}
5544
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005545static void bc_num_printNewline(void)
5546{
5547 if (G.prog.nchars == G.prog.len - 1) {
5548 bb_putchar('\\');
5549 bb_putchar('\n');
5550 G.prog.nchars = 0;
5551 }
5552}
5553
5554#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005555static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005556{
5557 (void) radix;
5558 bb_putchar((char) num);
5559 G.prog.nchars += width;
5560}
5561#endif
5562
5563static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5564{
5565 size_t exp, pow;
5566
5567 bc_num_printNewline();
5568 bb_putchar(radix ? '.' : ' ');
5569 ++G.prog.nchars;
5570
5571 bc_num_printNewline();
5572 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5573 continue;
5574
5575 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5576 size_t dig;
5577 bc_num_printNewline();
5578 dig = num / pow;
5579 num -= dig * pow;
5580 bb_putchar(((char) dig) + '0');
5581 }
5582}
5583
5584static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5585{
5586 if (radix) {
5587 bc_num_printNewline();
5588 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005589 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005590 }
5591
5592 bc_num_printNewline();
5593 bb_putchar(bb_hexdigits_upcase[num]);
5594 G.prog.nchars += width;
5595}
5596
5597static void bc_num_printDecimal(BcNum *n)
5598{
5599 size_t i, rdx = n->rdx - 1;
5600
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005601 if (n->neg) {
5602 bb_putchar('-');
5603 G.prog.nchars++;
5604 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005605
5606 for (i = n->len - 1; i < n->len; --i)
5607 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5608}
5609
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005610typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5611
Denys Vlasenko10bde142018-12-27 18:23:58 +01005612static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005613{
5614 BcStatus s;
5615 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005616 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005617 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005618 BcNum intp, fracp, digit, frac_len;
5619 unsigned long dig, *ptr;
5620 size_t i;
5621 bool radix;
5622
5623 if (n->len == 0) {
5624 print(0, width, false);
5625 RETURN_STATUS(BC_STATUS_SUCCESS);
5626 }
5627
5628 bc_vec_init(&stack, sizeof(long), NULL);
5629 bc_num_init(&intp, n->len);
5630 bc_num_init(&fracp, n->rdx);
5631 bc_num_init(&digit, width);
5632 bc_num_init(&frac_len, BC_NUM_INT(n));
5633 bc_num_copy(&intp, n);
5634 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005635 base.cap = ARRAY_SIZE(base_digs);
5636 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005637 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005638
5639 bc_num_truncate(&intp, intp.rdx);
5640 s = zbc_num_sub(n, &intp, &fracp, 0);
5641 if (s) goto err;
5642
5643 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005644 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005645 if (s) goto err;
5646 s = zbc_num_ulong(&digit, &dig);
5647 if (s) goto err;
5648 bc_vec_push(&stack, &dig);
5649 }
5650
5651 for (i = 0; i < stack.len; ++i) {
5652 ptr = bc_vec_item_rev(&stack, i);
5653 print(*ptr, width, false);
5654 }
5655
5656 if (!n->rdx) goto err;
5657
5658 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005659 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005660 if (s) goto err;
5661 s = zbc_num_ulong(&fracp, &dig);
5662 if (s) goto err;
5663 bc_num_ulong2num(&intp, dig);
5664 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5665 if (s) goto err;
5666 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005667 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005668 if (s) goto err;
5669 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005670 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005671 bc_num_free(&frac_len);
5672 bc_num_free(&digit);
5673 bc_num_free(&fracp);
5674 bc_num_free(&intp);
5675 bc_vec_free(&stack);
5676 RETURN_STATUS(s);
5677}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005678#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005679
Denys Vlasenko10bde142018-12-27 18:23:58 +01005680static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005681{
5682 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005683 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005684 BcNumDigitOp print;
5685 bool neg = n->neg;
5686
5687 if (neg) {
5688 bb_putchar('-');
5689 G.prog.nchars++;
5690 }
5691
5692 n->neg = false;
5693
Denys Vlasenko680ccd32018-12-31 19:42:13 +01005694 if (G.prog.ob_t <= 16) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005695 width = 1;
5696 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005697 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005698 unsigned i = G.prog.ob_t - 1;
5699 width = 0;
5700 for (;;) {
5701 width++;
5702 i /= 10;
5703 if (i == 0)
5704 break;
5705 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005706 print = bc_num_printDigits;
5707 }
5708
Denys Vlasenko10bde142018-12-27 18:23:58 +01005709 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005710 n->neg = neg;
5711
5712 RETURN_STATUS(s);
5713}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005714#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005715
Denys Vlasenko10bde142018-12-27 18:23:58 +01005716static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005717{
5718 BcStatus s = BC_STATUS_SUCCESS;
5719
5720 bc_num_printNewline();
5721
5722 if (n->len == 0) {
5723 bb_putchar('0');
5724 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005725 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005726 bc_num_printDecimal(n);
5727 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005728 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005729
5730 if (newline) {
5731 bb_putchar('\n');
5732 G.prog.nchars = 0;
5733 }
5734
5735 RETURN_STATUS(s);
5736}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005737#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005738
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005739#if !ENABLE_DC
5740// for bc, idx is always 0
5741#define xc_program_print(inst, idx) \
5742 xc_program_print(inst)
5743#endif
5744static BC_STATUS xc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005745{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005746 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005747 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005748 BcNum *num;
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005749 IF_NOT_DC(size_t idx = 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005750
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005751 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005752 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005753
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005754 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005755#if ENABLE_BC
5756 if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID)
5757 // void function's result on stack, ignore
5758 RETURN_STATUS(BC_STATUS_SUCCESS);
5759#endif
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005760 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005761 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005762
5763 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005764 s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005765#if ENABLE_BC
5766 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5767#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005768 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005769 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005770
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005771 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005772 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005773
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005774 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko266bec82019-01-02 05:03:53 +01005775 char *nl;
5776 G.prog.nchars += printf("%s", str);
5777 nl = strrchr(str, '\n');
5778 if (nl)
5779 G.prog.nchars = strlen(nl + 1);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005780 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005781 xc_program_printString(str);
Denys Vlasenko266bec82019-01-02 05:03:53 +01005782 if (inst == XC_INST_PRINT)
5783 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005784 }
5785 }
5786
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005787 if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005788
Denys Vlasenko29301232018-12-11 15:29:32 +01005789 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005790}
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005791#define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005792
Denys Vlasenko10bde142018-12-27 18:23:58 +01005793static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005794{
5795 BcStatus s;
5796 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005797 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005798
Denys Vlasenko10bde142018-12-27 18:23:58 +01005799 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005800 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005801
5802 bc_num_init(&res.d.n, num->len);
5803 bc_num_copy(&res.d.n, num);
5804 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5805
Denys Vlasenko10bde142018-12-27 18:23:58 +01005806 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005807
Denys Vlasenko29301232018-12-11 15:29:32 +01005808 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005809}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005810#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005811
Denys Vlasenko10bde142018-12-27 18:23:58 +01005812static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005813{
5814 BcStatus s;
5815 BcResult *opd1, *opd2, res;
5816 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005817 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005818
Denys Vlasenko10bde142018-12-27 18:23:58 +01005819 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005820 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005821
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005822 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005823
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005824 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005825 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005826 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005827 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005828 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005829 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005830 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005831 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005832 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005833 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005834 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005835 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005836 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005837 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005838 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005839 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005840 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005841 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005842 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005843 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005844 cond = (cond > 0);
5845 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005846 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005847 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005848 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005849 }
5850 }
5851
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005852 if (cond) bc_num_one(&res.d.n);
5853 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005854
Denys Vlasenko10bde142018-12-27 18:23:58 +01005855 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005856
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005857 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005858}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005859#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005860
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005861#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005862static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005863{
5864 BcNum n2;
5865 BcResult res;
5866
5867 memset(&n2, 0, sizeof(BcNum));
5868 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005869 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005870
5871 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005872 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005873 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005874 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005875 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005876 }
5877
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005878 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 bc_vec_push(v, &n2);
5880
Denys Vlasenko29301232018-12-11 15:29:32 +01005881 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005882}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005883#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005884#endif // ENABLE_DC
5885
Denys Vlasenko53799502019-01-25 14:24:03 +01005886static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, BcType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005887{
5888 BcStatus s;
5889 BcResult *ptr, r;
Denys Vlasenko53799502019-01-25 14:24:03 +01005890 BcVec *vec;
Gavin Howard01055ba2018-11-03 11:00:21 -06005891 BcNum *n;
Denys Vlasenko53799502019-01-25 14:24:03 +01005892 bool var = (t == BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005893
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005894 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005895 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005896
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005897 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko53799502019-01-25 14:24:03 +01005898 if ((ptr->t == XC_RESULT_ARRAY) == var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005899 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005900 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005901
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005902#if ENABLE_DC
Denys Vlasenko53799502019-01-25 14:24:03 +01005903 if (ptr->t == XC_RESULT_STR) {
5904 if (!var)
5905 RETURN_STATUS(bc_error_variable_is_wrong_type());
5906 RETURN_STATUS(zdc_program_assignStr(ptr, vec, true));
5907 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005908#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005909
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005910 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005911 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
5913 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko53799502019-01-25 14:24:03 +01005914 vec = xc_program_search(name, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005915
5916 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005917 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005918 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005919 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01005920 BcVec *v = (BcVec*) n;
5921 bool ref, ref_size;
Gavin Howard01055ba2018-11-03 11:00:21 -06005922
Denys Vlasenko53799502019-01-25 14:24:03 +01005923 ref = (v->size == sizeof(BcVec) && t != BC_TYPE_ARRAY);
5924 ref_size = (v->size == sizeof(uint8_t));
5925
5926 if (ref || (ref_size && t == BC_TYPE_REF)) {
5927 bc_vec_init(&r.d.v, sizeof(uint8_t), NULL);
5928 if (ref) {
5929 size_t vidx, idx;
5930 BcId id;
5931
5932 id.name = ptr->d.id.name;
5933 v = xc_program_search(ptr->d.id.name, BC_TYPE_REF);
5934
5935 // Make sure the pointer was not invalidated.
5936 vec = xc_program_search(name, t);
5937
5938 vidx = bc_map_find_exact(&G.prog.arr_map, &id);
5939 //assert(vidx != BC_VEC_INVALID_IDX);
5940 vidx = ((BcId*) bc_vec_item(&G.prog.arr_map, vidx))->idx;
5941 idx = v->len - 1;
5942
5943 bc_vec_pushIndex(&r.d.v, vidx);
5944 bc_vec_pushIndex(&r.d.v, idx);
5945 }
5946 // If we get here, we are copying a ref to a ref.
5947 else bc_vec_npush(&r.d.v, v->len, v->v);
5948
5949 // We need to return early.
5950 goto ret;
5951 }
5952
5953 if (ref_size && t != BC_TYPE_REF)
5954 v = xc_program_dereference(v);
5955
5956 bc_array_init(&r.d.v, true);
5957 bc_array_copy(&r.d.v, v);
5958 }
5959 ret:
5960 bc_vec_push(vec, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005961 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005962
Denys Vlasenko29301232018-12-11 15:29:32 +01005963 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005964}
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01005965#define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005966
Denys Vlasenko10bde142018-12-27 18:23:58 +01005967static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005968{
5969 BcStatus s;
5970 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005971 BcNum *l, *r;
5972 bool assign = (inst == XC_INST_ASSIGN);
5973 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005974
Denys Vlasenko10bde142018-12-27 18:23:58 +01005975 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005976 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005977
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005978 ib = left->t == XC_RESULT_IBASE;
5979 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005980
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005981#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005982 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005983 BcVec *v;
5984
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005985 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005986 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko53799502019-01-25 14:24:03 +01005987 v = xc_program_search(left->d.id.name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005988
Denys Vlasenkofa210792018-12-19 19:35:40 +01005989 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005990 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005991#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005992
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005993 if (left->t == XC_RESULT_CONSTANT
5994 || left->t == XC_RESULT_TEMP
5995 IF_BC(|| left->t == BC_RESULT_VOID)
5996 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005997 RETURN_STATUS(bc_error_bad_assignment());
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01005998 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005999
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006000#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006001 if (assign)
6002 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006003 else {
6004 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006005 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 +01006006 }
6007 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006008#else
Gavin Howard01055ba2018-11-03 11:00:21 -06006009 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006010#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006012 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006013 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006014 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
6015 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
6016 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006017 };
Gavin Howard01055ba2018-11-03 11:00:21 -06006018 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01006019 size_t max;
6020 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06006021
Denys Vlasenko29301232018-12-11 15:29:32 +01006022 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006023 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006024 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06006025 if (sc) {
6026 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006027 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01006028 } else {
6029 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006030 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06006031 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006032 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006033 }
6034
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006035 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006036 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06006037
6038 *ptr = (size_t) val;
6039 s = BC_STATUS_SUCCESS;
6040 }
6041
6042 bc_num_init(&res.d.n, l->len);
6043 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006044 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006045
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006046 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006047}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006048#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006049
Denys Vlasenko416ce762018-12-02 20:57:17 +01006050#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01006051#define xc_program_pushVar(code, bgn, pop, copy) \
6052 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01006053// for bc, 'pop' and 'copy' are always false
6054#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006055static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006056 bool pop, bool copy)
6057{
Gavin Howard01055ba2018-11-03 11:00:21 -06006058 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006059 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006060
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006061 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06006062 r.d.id.name = name;
6063
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006064#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006065 if (pop || copy) {
Denys Vlasenko53799502019-01-25 14:24:03 +01006066 BcVec *v = xc_program_search(name, BC_TYPE_VAR);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006067 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006068
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006069 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006070 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006071 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006072 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006073
6074 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006075 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006076 bc_num_init_DEF_SIZE(&r.d.n);
6077 bc_num_copy(&r.d.n, num);
6078 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006079 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01006080 r.d.id.idx = num->rdx;
6081 }
6082
6083 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084 }
6085#endif // ENABLE_DC
6086
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006087 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006088
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006089 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006090}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006091#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006092
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006093static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006094{
6095 BcStatus s = BC_STATUS_SUCCESS;
6096 BcResult r;
6097 BcNum *num;
6098
Denys Vlasenko10bde142018-12-27 18:23:58 +01006099 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006100
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006101 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006102 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006103 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006104 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06006105 BcResult *operand;
6106 unsigned long temp;
6107
Denys Vlasenko10bde142018-12-27 18:23:58 +01006108 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006109 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006110 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006111 if (s) goto err;
6112
6113 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006114 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006115 goto err;
6116 }
6117
6118 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006119 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006120 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006121 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006122 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006123 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006124}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006125#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006126
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006127#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006128static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006129{
6130 BcStatus s;
6131 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006132 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006133 char inst2 = inst;
6134
Denys Vlasenko10bde142018-12-27 18:23:58 +01006135 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006136 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006137
6138 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006139 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006140 bc_num_init(&copy.d.n, num->len);
6141 bc_num_copy(&copy.d.n, num);
6142 }
6143
6144 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006145 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
6146 ? BC_INST_ASSIGN_PLUS
6147 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006149 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006150 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006151 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006152
6153 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006154 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006155 }
6156
Denys Vlasenko29301232018-12-11 15:29:32 +01006157 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006159#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006160
Denys Vlasenko29301232018-12-11 15:29:32 +01006161static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006162{
Gavin Howard01055ba2018-11-03 11:00:21 -06006163 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006164 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 BcId *a;
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006166 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006167
Denys Vlasenko10bde142018-12-27 18:23:58 +01006168 nparams = xc_program_index(code, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006169 ip.func = xc_program_index(code, idx);
6170 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006171
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006172 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006173 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006174 }
6175 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006176 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006177 }
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006178 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006179
6180 for (i = 0; i < nparams; ++i) {
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006181 BcResult *arg;
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006182 BcStatus s;
Denys Vlasenko53799502019-01-25 14:24:03 +01006183 bool arr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006184
6185 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006186 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006187
Denys Vlasenko53799502019-01-25 14:24:03 +01006188 arr = (a->idx == BC_TYPE_ARRAY || a->idx == BC_TYPE_REF);
6189
6190 if (arr != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006191 // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions)
6192 ) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006193 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko19c3eb02019-01-04 00:05:07 +01006194 }
Denys Vlasenko53799502019-01-25 14:24:03 +01006195 s = zxc_program_popResultAndCopyToVar(a->name, (BcType) a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006196 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006197 }
6198
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006199 a = bc_vec_item(&func->autos, i);
6200 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006201 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006202
Denys Vlasenko53799502019-01-25 14:24:03 +01006203 v = xc_program_search(a->name, (BcType) a->idx);
6204 if (a->idx == BC_TYPE_VAR) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006205 BcNum n2;
6206 bc_num_init_DEF_SIZE(&n2);
6207 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006208 } else {
Denys Vlasenko53799502019-01-25 14:24:03 +01006209 //assert(a->idx == BC_TYPE_ARRAY);
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006210 BcVec v2;
6211 bc_array_init(&v2, true);
6212 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006213 }
6214 }
6215
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006216 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006217
Denys Vlasenko29301232018-12-11 15:29:32 +01006218 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006219}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006220#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006221
Denys Vlasenko29301232018-12-11 15:29:32 +01006222static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006223{
Gavin Howard01055ba2018-11-03 11:00:21 -06006224 BcResult res;
6225 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006226 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006227 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006228 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006229
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006230 f = xc_program_func(ip->func);
6231
6232 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006233 if (inst == XC_INST_RET) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006234 // bc needs this for e.g. RESULT_CONSTANT ("return 5")
6235 // because bc constants are per-function.
6236 // TODO: maybe avoid if value is already RESULT_TEMP?
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006237 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006238 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006239 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006240
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006241 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006242 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006243 bc_num_init(&res.d.n, num->len);
6244 bc_num_copy(&res.d.n, num);
Denys Vlasenko377cc972019-01-04 00:34:52 +01006245 bc_vec_pop(&G.prog.results);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006246 } else {
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006247 if (f->voidfunc)
6248 res.t = BC_RESULT_VOID;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006249 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006250 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006251 }
Denys Vlasenko02c3d7a2019-01-04 00:21:29 +01006252 bc_vec_push(&G.prog.results, &res);
6253
6254 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255
6256 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006257 a = (void*)f->autos.v;
6258 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006259 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006260 v = xc_program_search(a->name, (BcType) a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006261 bc_vec_pop(v);
6262 }
6263
Denys Vlasenko29301232018-12-11 15:29:32 +01006264 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006266#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006267#endif // ENABLE_BC
6268
Denys Vlasenko10bde142018-12-27 18:23:58 +01006269static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006270{
6271 return (unsigned long) n->rdx;
6272}
6273
Denys Vlasenko10bde142018-12-27 18:23:58 +01006274static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006275{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006276 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenkoace81cd2021-02-26 14:05:28 +01006278 if (n->rdx != len)
6279 // length(100): rdx 0 len 3, return 3
6280 // length(0.01-0.01): rdx 2 len 0, return 2
6281 // dc: 0.01 0.01 - Zp: rdx 2 len 0, return 1
6282 return len != 0 ? len : (IS_BC ? n->rdx : 1);
6283
6284 // length(0): return 1
6285 // length(0.000nnn): count nnn
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006286 for (;;) {
6287 if (len == 0) break;
6288 len--;
6289 if (n->num[len] != 0) break;
6290 }
Denys Vlasenkoace81cd2021-02-26 14:05:28 +01006291 return len + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06006292}
6293
Denys Vlasenko10bde142018-12-27 18:23:58 +01006294static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006295{
6296 BcStatus s;
6297 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006298 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006300 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006301
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006302 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006303 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006304 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006305
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006306 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006307 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006308
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006309#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006310 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006311 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006312#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006313
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006314 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006316 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006317 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006318#if ENABLE_BC
Denys Vlasenkoace81cd2021-02-26 14:05:28 +01006319 else if (len && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006320 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006321 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006322#endif
6323#if ENABLE_DC
Denys Vlasenkoace81cd2021-02-26 14:05:28 +01006324 else if (len && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006325 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006326 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006327
Denys Vlasenko10bde142018-12-27 18:23:58 +01006328 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006329 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006330 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006331#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006332 else {
Denys Vlasenkoace81cd2021-02-26 14:05:28 +01006333//TODO: length(.00) and scale(.00) should return 2, they return 1 and 0 now
6334//(don't forget to check that dc Z and X commands do not break)
Denys Vlasenko10bde142018-12-27 18:23:58 +01006335 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006336 }
6337
Denys Vlasenko10bde142018-12-27 18:23:58 +01006338 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006339
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006340 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006342#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006343
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006344#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006345static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006346{
6347 BcStatus s;
6348 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006349 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006350
Denys Vlasenko10bde142018-12-27 18:23:58 +01006351 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006352 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006353
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006354 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006355 bc_num_init(&res2.d.n, n2->len);
6356
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006357 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006358 if (s) goto err;
6359
Denys Vlasenko10bde142018-12-27 18:23:58 +01006360 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006361 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006362 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006364 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006365 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006366 bc_num_free(&res2.d.n);
6367 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006368 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006369}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006370#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006371
Denys Vlasenkofa210792018-12-19 19:35:40 +01006372static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006373{
6374 BcStatus s;
6375 BcResult *r1, *r2, *r3, res;
6376 BcNum *n1, *n2, *n3;
6377
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006378 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006379 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006380 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006381 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006382
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006383 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006384 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006385 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006386 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006387 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006388
6389 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006390 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006392 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006393 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006394 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006395 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006396 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006397 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006398 }
6399 }
6400
6401 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006402 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 if (s) goto err;
6404
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006406 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006407
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006408 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006409 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006410 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006411 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006412}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006413#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006414
Denys Vlasenkofa210792018-12-19 19:35:40 +01006415static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006416{
Gavin Howard01055ba2018-11-03 11:00:21 -06006417 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006418 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006419
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006420 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006421
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006422 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006423 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006424 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006425}
6426
Denys Vlasenkofa210792018-12-19 19:35:40 +01006427static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006428{
6429 BcStatus s;
6430 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006431 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006432 char **strs;
6433 char *str;
6434 char c;
6435 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006436
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006437 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006438 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006439
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006440 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006441 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006442 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443
6444 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006445 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006446 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006447 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006448
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006449 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006450 bc_num_copy(&n, num);
6451 bc_num_truncate(&n, n.rdx);
6452
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006453 strmb.cap = ARRAY_SIZE(strmb_digs);
6454 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006455 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006456
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006457 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006458 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006459 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006460 if (s) goto num_err;
6461
6462 c = (char) val;
6463
6464 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006465 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006466 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006467 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006468 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006469 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006470 }
6471
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006472 strs = (void*)G.prog.strs.v;
6473 for (idx = 0; idx < G.prog.strs.len; idx++) {
6474 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6475 goto dup;
6476 }
6477 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006478 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006479 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006480 //str[1] = '\0'; - already is
Brian Foleyb64470b2019-09-05 10:50:13 +02006481 idx = bc_vec_push(&G.prog.strs, &str);
6482 // Add an empty function so that if zdc_program_execStr ever needs to
6483 // parse the string into code (from the 'x' command) there's somewhere
6484 // to store the bytecode.
6485 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006486 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006487 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006488 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006489 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006490
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006491 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006492 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006493 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006494 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006495}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006496#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006497
Denys Vlasenkofa210792018-12-19 19:35:40 +01006498static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006499{
6500 BcStatus s;
6501 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006502 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006504
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006505 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006506 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006507 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006508
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006509 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006510 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511
Denys Vlasenko57734c92018-12-17 21:14:05 +01006512 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006513 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006514 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006515 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006516 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006517 str = *xc_program_str(idx);
Ron Yorstoncad3fc72021-02-03 20:47:14 +01006518 fputs_stdout(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 }
6520
Denys Vlasenko29301232018-12-11 15:29:32 +01006521 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006522}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006523#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006524
Denys Vlasenkofa210792018-12-19 19:35:40 +01006525static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006526{
6527 BcStatus s;
6528 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006529 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006530 unsigned long val;
6531
Denys Vlasenko10bde142018-12-27 18:23:58 +01006532 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006533 if (s) RETURN_STATUS(s);
6534 s = zbc_num_ulong(num, &val);
6535 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006537 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006538
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006539 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006540 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006541 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006542 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006543 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006544
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006545 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006546
Denys Vlasenko29301232018-12-11 15:29:32 +01006547 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006548}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006549#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006550
Denys Vlasenkofa210792018-12-19 19:35:40 +01006551static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006552{
6553 BcStatus s = BC_STATUS_SUCCESS;
6554 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006555 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006556 BcInstPtr ip;
6557 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006558
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006559 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006560 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006561
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006562 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006563
6564 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006565 BcNum *n = n; // for compiler
6566 bool exec;
6567 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006568 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006569 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006570
Denys Vlasenkof706a182018-12-26 20:02:09 +01006571 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006572 (*bgn) += 1;
6573 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006574 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006575
6576 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006577 name = then_name;
6578 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006579 exec = true;
6580 name = else_name;
6581 }
6582
6583 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006584 BcVec *v;
Denys Vlasenko53799502019-01-25 14:24:03 +01006585 v = xc_program_search(name, BC_TYPE_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06006586 n = bc_vec_top(v);
6587 }
6588
6589 free(then_name);
6590 free(else_name);
6591
6592 if (!exec) goto exit;
6593 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006594 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 goto exit;
6596 }
6597
6598 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006599 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006600 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006601 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006602 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006603 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006604 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006605 if (s || !BC_PROG_STR(n)) goto exit;
6606 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006607 } else
Brian Foley74548792019-09-05 10:46:22 +02006608 goto exit_nopop;
Gavin Howard01055ba2018-11-03 11:00:21 -06006609 }
6610
6611 fidx = sidx + BC_PROG_REQ_FUNCS;
6612
Denys Vlasenko10bde142018-12-27 18:23:58 +01006613 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006614
6615 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006616 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006617 char *str;
6618
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006619 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006620 xc_parse_create(fidx);
6621 str = *xc_program_str(sidx);
6622 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006623 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006624
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006625 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006626 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006627 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006628 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006629 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006630 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006631 G.prs = sv_parse; // struct copy
6632 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006633 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006634 bc_vec_pop_all(&f->code);
6635 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006636 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006637 }
6638
Denys Vlasenko24e41942018-12-21 23:01:26 +01006639 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006640 ip.func = fidx;
6641
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006642 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006643 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006644
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006645 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006646 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006647 bc_vec_pop(&G.prog.results);
Brian Foley74548792019-09-05 10:46:22 +02006648 exit_nopop:
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006649 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006650}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006651#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006652#endif // ENABLE_DC
6653
Denys Vlasenko10bde142018-12-27 18:23:58 +01006654static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006655{
Gavin Howard01055ba2018-11-03 11:00:21 -06006656 BcResult res;
6657 unsigned long val;
6658
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006659 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006660 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006661 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006662 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006663 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006664 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006665 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006666
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006667 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006668 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006669 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006670}
6671
Denys Vlasenko10bde142018-12-27 18:23:58 +01006672static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006673{
Gavin Howard01055ba2018-11-03 11:00:21 -06006674 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006675 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006676 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006677 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006678
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006679 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006680 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6681 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006682 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006683 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006684
Denys Vlasenko24e41942018-12-21 23:01:26 +01006685 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 -06006686 switch (inst) {
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006687 case XC_INST_RET:
6688 if (IS_DC) { // end of '?' reached
6689 bc_vec_pop(&G.prog.exestack);
6690 goto read_updated_ip;
6691 }
6692 // bc: fall through
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006693#if ENABLE_BC
Denys Vlasenko1db367a2019-01-04 06:18:00 +01006694 case BC_INST_RET0:
6695 dbg_exec("BC_INST_RET[0]:");
6696 s = zbc_program_return(inst);
6697 goto read_updated_ip;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006698 case BC_INST_JUMP_ZERO: {
6699 BcNum *num;
6700 bool zero;
6701 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006702 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006703 if (s) RETURN_STATUS(s);
6704 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6705 bc_vec_pop(&G.prog.results);
6706 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006707 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006708 break;
6709 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006710 // else: fall through
6711 }
6712 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006713 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006714 size_t *addr = bc_vec_item(&func->labels, idx);
6715 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6716 ip->inst_idx = *addr;
6717 break;
6718 }
6719 case BC_INST_CALL:
6720 dbg_exec("BC_INST_CALL:");
6721 s = zbc_program_call(code, &ip->inst_idx);
6722 goto read_updated_ip;
6723 case BC_INST_INC_PRE:
6724 case BC_INST_DEC_PRE:
6725 case BC_INST_INC_POST:
6726 case BC_INST_DEC_POST:
6727 dbg_exec("BC_INST_INCDEC:");
6728 s = zbc_program_incdec(inst);
6729 break;
6730 case BC_INST_HALT:
6731 dbg_exec("BC_INST_HALT:");
6732 QUIT_OR_RETURN_TO_MAIN;
6733 break;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006734 case XC_INST_BOOL_OR:
6735 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006736#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006737 case XC_INST_REL_EQ:
6738 case XC_INST_REL_LE:
6739 case XC_INST_REL_GE:
6740 case XC_INST_REL_NE:
6741 case XC_INST_REL_LT:
6742 case XC_INST_REL_GT:
6743 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006744 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006745 break;
6746 case XC_INST_READ:
6747 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006748 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006749 goto read_updated_ip;
6750 case XC_INST_VAR:
6751 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006752 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006753 break;
6754 case XC_INST_ARRAY_ELEM:
6755 case XC_INST_ARRAY:
6756 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6757 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6758 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006759#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006760 case BC_INST_LAST:
6761 dbg_exec("BC_INST_LAST:");
6762 r.t = BC_RESULT_LAST;
6763 bc_vec_push(&G.prog.results, &r);
6764 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006765#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006766 case XC_INST_IBASE:
6767 case XC_INST_OBASE:
6768 case XC_INST_SCALE:
6769 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006770 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006771 break;
6772 case XC_INST_SCALE_FUNC:
6773 case XC_INST_LENGTH:
6774 case XC_INST_SQRT:
6775 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006776 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006777 break;
6778 case XC_INST_NUM:
6779 dbg_exec("XC_INST_NUM:");
6780 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006781 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006782 bc_vec_push(&G.prog.results, &r);
6783 break;
6784 case XC_INST_POP:
6785 dbg_exec("XC_INST_POP:");
6786 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6787 s = bc_error_stack_has_too_few_elements();
6788 else
6789 bc_vec_pop(&G.prog.results);
6790 break;
6791 case XC_INST_PRINT:
6792 case XC_INST_PRINT_POP:
6793 case XC_INST_PRINT_STR:
Denys Vlasenko54f5c1d2019-01-04 13:58:46 +01006794 dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006795 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006796 break;
6797 case XC_INST_STR:
6798 dbg_exec("XC_INST_STR:");
6799 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006800 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006801 bc_vec_push(&G.prog.results, &r);
6802 break;
6803 case XC_INST_POWER:
6804 case XC_INST_MULTIPLY:
6805 case XC_INST_DIVIDE:
6806 case XC_INST_MODULUS:
6807 case XC_INST_PLUS:
6808 case XC_INST_MINUS:
6809 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006810 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006811 break;
6812 case XC_INST_BOOL_NOT: {
6813 BcNum *num;
6814 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006815 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006816 if (s) RETURN_STATUS(s);
6817 bc_num_init_DEF_SIZE(&r.d.n);
6818 if (bc_num_cmp(num, &G.prog.zero) == 0)
6819 bc_num_one(&r.d.n);
6820 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006821 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006822 break;
6823 }
6824 case XC_INST_NEG:
6825 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006826 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006827 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006828#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006829 case BC_INST_ASSIGN_POWER:
6830 case BC_INST_ASSIGN_MULTIPLY:
6831 case BC_INST_ASSIGN_DIVIDE:
6832 case BC_INST_ASSIGN_MODULUS:
6833 case BC_INST_ASSIGN_PLUS:
6834 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006835#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006836 case XC_INST_ASSIGN:
6837 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006838 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006839 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006840#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006841 case DC_INST_POP_EXEC:
6842 dbg_exec("DC_INST_POP_EXEC:");
6843 bc_vec_pop(&G.prog.exestack);
6844 goto read_updated_ip;
6845 case DC_INST_MODEXP:
6846 dbg_exec("DC_INST_MODEXP:");
6847 s = zdc_program_modexp();
6848 break;
6849 case DC_INST_DIVMOD:
6850 dbg_exec("DC_INST_DIVMOD:");
6851 s = zdc_program_divmod();
6852 break;
6853 case DC_INST_EXECUTE:
6854 case DC_INST_EXEC_COND:
6855 dbg_exec("DC_INST_EXEC[_COND]:");
6856 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6857 goto read_updated_ip;
6858 case DC_INST_PRINT_STACK: {
6859 size_t idx;
6860 dbg_exec("DC_INST_PRINT_STACK:");
6861 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006862 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006863 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006864 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006865 break;
6866 }
6867 case DC_INST_CLEAR_STACK:
6868 dbg_exec("DC_INST_CLEAR_STACK:");
6869 bc_vec_pop_all(&G.prog.results);
6870 break;
6871 case DC_INST_STACK_LEN:
6872 dbg_exec("DC_INST_STACK_LEN:");
6873 dc_program_stackLen();
6874 break;
6875 case DC_INST_DUPLICATE:
6876 dbg_exec("DC_INST_DUPLICATE:");
6877 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6878 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6879 ptr = bc_vec_top(&G.prog.results);
6880 dc_result_copy(&r, ptr);
6881 bc_vec_push(&G.prog.results, &r);
6882 break;
6883 case DC_INST_SWAP: {
6884 BcResult *ptr2;
6885 dbg_exec("DC_INST_SWAP:");
6886 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6887 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6888 ptr = bc_vec_item_rev(&G.prog.results, 0);
6889 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6890 memcpy(&r, ptr, sizeof(BcResult));
6891 memcpy(ptr, ptr2, sizeof(BcResult));
6892 memcpy(ptr2, &r, sizeof(BcResult));
6893 break;
6894 }
6895 case DC_INST_ASCIIFY:
6896 dbg_exec("DC_INST_ASCIIFY:");
6897 s = zdc_program_asciify();
6898 break;
6899 case DC_INST_PRINT_STREAM:
6900 dbg_exec("DC_INST_PRINT_STREAM:");
6901 s = zdc_program_printStream();
6902 break;
6903 case DC_INST_LOAD:
6904 case DC_INST_PUSH_VAR: {
6905 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006906 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006907 break;
6908 }
6909 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006910 char *name = xc_program_name(code, &ip->inst_idx);
Denys Vlasenko53799502019-01-25 14:24:03 +01006911 s = zxc_program_popResultAndCopyToVar(name, BC_TYPE_VAR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006912 free(name);
6913 break;
6914 }
6915 case DC_INST_QUIT:
6916 dbg_exec("DC_INST_QUIT:");
6917 if (G.prog.exestack.len <= 2)
6918 QUIT_OR_RETURN_TO_MAIN;
6919 bc_vec_npop(&G.prog.exestack, 2);
6920 goto read_updated_ip;
6921 case DC_INST_NQUIT:
6922 dbg_exec("DC_INST_NQUIT:");
6923 s = zdc_program_nquit();
6924 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006925#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006926 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006927 // Instruction stack has changed, read new pointers
6928 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006929 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006930 code = func->code.v;
6931 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006932 }
6933
Denys Vlasenkod38af482018-12-04 19:11:02 +01006934 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006935 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006936 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006937 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006938
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006939 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006940 }
6941
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006942 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006943}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006944#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006945
Denys Vlasenko10bde142018-12-27 18:23:58 +01006946static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006947{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006948 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006949
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006950 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006951 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006953
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006954 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006955 BcInstPtr *ip;
6956 BcFunc *f;
6957
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006958 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006959 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006960#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006961 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006962 if (s) goto err;
6963
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006964 // Check that next token is a correct stmt delimiter -
6965 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006966 if (G.prs.lex != BC_LEX_SCOLON
6967 && G.prs.lex != XC_LEX_NLINE
6968 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006969 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01006970 bc_error_at("bad statement terminator");
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006971 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006972 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006973 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006974 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006975#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006976 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006977#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006978 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006979#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006980 }
6981 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006982 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006983 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006984 RETURN_STATUS(BC_STATUS_FAILURE);
6985 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006986
Denys Vlasenko39287e02018-12-22 02:23:08 +01006987 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006988 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006989 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006990 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006991 break;
6992 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006993
6994 ip = (void*)G.prog.exestack.v;
6995#if SANITY_CHECKS
6996 if (G.prog.exestack.len != 1) // should have only main's IP
James Byrne69374872019-07-02 11:35:03 +02006997 bb_simple_error_msg_and_die("BUG:call stack");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006998 if (ip->func != BC_PROG_MAIN)
James Byrne69374872019-07-02 11:35:03 +02006999 bb_simple_error_msg_and_die("BUG:not MAIN");
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01007000#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01007001 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007002 // bc discards strings, constants and code after each
7003 // top-level statement in the "main program".
7004 // This prevents "yes 1 | bc" from growing its memory
7005 // without bound. This can be done because data stack
7006 // is empty and thus can't hold any references to
7007 // strings or constants, there is no generated code
7008 // which can hold references (after we discard one
7009 // we just executed). Code of functions can have references,
7010 // but bc stores function strings/constants in per-function
7011 // storage.
7012 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01007013#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01007014 if (G.prog.results.len != 0) // should be empty
James Byrne69374872019-07-02 11:35:03 +02007015 bb_simple_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01007016#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007017 IF_BC(bc_vec_pop_all(&f->strs);)
7018 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkofc7aa7a2019-01-08 18:08:48 +01007019 // We are at SCOLON/NLINE, skip it:
7020 s = zxc_lex_next();
7021 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01007022 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01007023 if (G.prog.results.len == 0
7024 && G.prog.vars.len == 0
7025 ) {
7026 // If stack is empty and no registers exist (TODO: or they are all empty),
7027 // we can get rid of accumulated strings and constants.
7028 // In this example dc process should not grow
7029 // its memory consumption with time:
7030 // yes 1pc | dc
7031 IF_DC(bc_vec_pop_all(&G.prog.strs);)
7032 IF_DC(bc_vec_pop_all(&G.prog.consts);)
7033 }
7034 // The code is discarded always (below), thus this example
7035 // should also not grow its memory consumption with time,
7036 // even though its data stack is not empty:
7037 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007038 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01007039 // We drop generated and executed code for both bc and dc:
7040 bc_vec_pop_all(&f->code);
7041 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06007042 }
Denys Vlasenko6842c602019-01-04 05:41:47 +01007043
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007044 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007045 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007046}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007047#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007048
Denys Vlasenko10bde142018-12-27 18:23:58 +01007049static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06007050{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007051 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01007052 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01007053 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007054 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007055
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01007056 G.prs.lex_filename = filename;
7057 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01007058 G.err_line = G.prs.lex_line = 1;
Denys Vlasenko22314682019-01-01 21:50:14 +01007059 dbg_lex("p->lex_line reset to 1");
Gavin Howard01055ba2018-11-03 11:00:21 -06007060
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007061 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007062 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007063 // We do not stop looping on errors here if reading stdin.
7064 // Example: start interactive bc and enter "return".
7065 // It should say "'return' not in a function"
7066 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01007067 } while (G.prs.lex_input_fp == stdin);
7068 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007069 RETURN_STATUS(s);
7070}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007071#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007072
Denys Vlasenko10bde142018-12-27 18:23:58 +01007073static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007074{
7075 BcStatus s;
7076 FILE *fp;
7077
7078 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01007079 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007080 fclose(fp);
7081
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007082 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007083}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007084#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007085
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007086#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01007087static void bc_vm_info(void)
7088{
7089 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07007090 "Adapted from https://github.com/gavinhoward/bc\n"
7091 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01007092 , applet_name);
7093}
7094
7095static void bc_args(char **argv)
7096{
7097 unsigned opts;
7098 int i;
7099
7100 GETOPT_RESET();
7101#if ENABLE_FEATURE_BC_LONG_OPTIONS
7102 opts = option_mask32 |= getopt32long(argv, "wvsqli",
7103 "warn\0" No_argument "w"
7104 "version\0" No_argument "v"
7105 "standard\0" No_argument "s"
7106 "quiet\0" No_argument "q"
7107 "mathlib\0" No_argument "l"
7108 "interactive\0" No_argument "i"
7109 );
7110#else
7111 opts = option_mask32 |= getopt32(argv, "wvsqli");
7112#endif
7113 if (getenv("POSIXLY_CORRECT"))
7114 option_mask32 |= BC_FLAG_S;
7115
7116 if (opts & BC_FLAG_V) {
7117 bc_vm_info();
7118 exit(0);
7119 }
7120
7121 for (i = optind; argv[i]; ++i)
7122 bc_vec_push(&G.files, argv + i);
7123}
7124
7125static void bc_vm_envArgs(void)
7126{
7127 BcVec v;
7128 char *buf;
7129 char *env_args = getenv("BC_ENV_ARGS");
7130
7131 if (!env_args) return;
7132
7133 G.env_args = xstrdup(env_args);
7134 buf = G.env_args;
7135
7136 bc_vec_init(&v, sizeof(char *), NULL);
7137
7138 while (*(buf = skip_whitespace(buf)) != '\0') {
7139 bc_vec_push(&v, &buf);
7140 buf = skip_non_whitespace(buf);
7141 if (!*buf)
7142 break;
7143 *buf++ = '\0';
7144 }
7145
7146 // NULL terminate, and pass argv[] so that first arg is argv[1]
7147 if (sizeof(int) == sizeof(char*)) {
7148 bc_vec_push(&v, &const_int_0);
7149 } else {
7150 static char *const nullptr = NULL;
7151 bc_vec_push(&v, &nullptr);
7152 }
7153 bc_args(((char **)v.v) - 1);
7154
7155 bc_vec_free(&v);
7156}
7157
7158static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007159 "scale=20"
7160"\n" "define e(x){"
7161"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007162////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
7163//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
7164//e(-.998896): GNU:.36828580434569428695
7165// above code:.36828580434569428696
7166// actual value:.3682858043456942869594...
7167// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007168"\n" "b=ibase"
7169"\n" "ibase=A"
7170"\n" "if(x<0){"
7171"\n" "n=1"
7172"\n" "x=-x"
7173"\n" "}"
7174"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007175"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007176"\n" "scale=scale(x)+1"
7177"\n" "while(x>1){"
7178"\n" "d+=1"
7179"\n" "x/=2"
7180"\n" "scale+=1"
7181"\n" "}"
7182"\n" "scale=r"
7183"\n" "r=x+1"
7184"\n" "p=x"
7185"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007186"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007187"\n" "p*=x"
7188"\n" "f*=i"
7189"\n" "v=p/f"
7190"\n" "r+=v"
7191"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007192"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007193"\n" "scale=s"
7194"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007195"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007196"\n" "return(r/1)"
7197"\n" "}"
7198"\n" "define l(x){"
7199"\n" "auto b,s,r,p,a,q,i,v"
7200"\n" "b=ibase"
7201"\n" "ibase=A"
7202"\n" "if(x<=0){"
7203"\n" "r=(1-10^scale)/1"
7204"\n" "ibase=b"
7205"\n" "return(r)"
7206"\n" "}"
7207"\n" "s=scale"
7208"\n" "scale+=6"
7209"\n" "p=2"
7210"\n" "while(x>=2){"
7211"\n" "p*=2"
7212"\n" "x=sqrt(x)"
7213"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007214"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007215"\n" "p*=2"
7216"\n" "x=sqrt(x)"
7217"\n" "}"
7218"\n" "r=a=(x-1)/(x+1)"
7219"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007220"\n" "v=1"
7221"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007222"\n" "a*=q"
7223"\n" "v=a/i"
7224"\n" "r+=v"
7225"\n" "}"
7226"\n" "r*=p"
7227"\n" "scale=s"
7228"\n" "ibase=b"
7229"\n" "return(r/1)"
7230"\n" "}"
7231"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007232"\n" "auto b,s,r,a,q,i"
7233"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007234"\n" "b=ibase"
7235"\n" "ibase=A"
7236"\n" "s=scale"
7237"\n" "scale=1.1*s+2"
7238"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007239"\n" "scale=0"
7240"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007241"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007242"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007243"\n" "scale=s+2"
7244"\n" "r=a=x"
7245"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007246"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007247"\n" "a*=q/(i*(i-1))"
7248"\n" "r+=a"
7249"\n" "}"
7250"\n" "scale=s"
7251"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007252"\n" "return(r/1)"
7253"\n" "}"
7254"\n" "define c(x){"
7255"\n" "auto b,s"
7256"\n" "b=ibase"
7257"\n" "ibase=A"
7258"\n" "s=scale"
7259"\n" "scale*=1.2"
7260"\n" "x=s(2*a(1)+x)"
7261"\n" "scale=s"
7262"\n" "ibase=b"
7263"\n" "return(x/1)"
7264"\n" "}"
7265"\n" "define a(x){"
7266"\n" "auto b,s,r,n,a,m,t,f,i,u"
7267"\n" "b=ibase"
7268"\n" "ibase=A"
7269"\n" "n=1"
7270"\n" "if(x<0){"
7271"\n" "n=-1"
7272"\n" "x=-x"
7273"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007274"\n" "if(scale<65){"
7275"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7276"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007277"\n" "}"
7278"\n" "s=scale"
7279"\n" "if(x>.2){"
7280"\n" "scale+=5"
7281"\n" "a=a(.2)"
7282"\n" "}"
7283"\n" "scale=s+3"
7284"\n" "while(x>.2){"
7285"\n" "m+=1"
7286"\n" "x=(x-.2)/(1+.2*x)"
7287"\n" "}"
7288"\n" "r=u=x"
7289"\n" "f=-x*x"
7290"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007291"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007292"\n" "u*=f"
7293"\n" "t=u/i"
7294"\n" "r+=t"
7295"\n" "}"
7296"\n" "scale=s"
7297"\n" "ibase=b"
7298"\n" "return((m*a+r)/n)"
7299"\n" "}"
7300"\n" "define j(n,x){"
7301"\n" "auto b,s,o,a,i,v,f"
7302"\n" "b=ibase"
7303"\n" "ibase=A"
7304"\n" "s=scale"
7305"\n" "scale=0"
7306"\n" "n/=1"
7307"\n" "if(n<0){"
7308"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007309"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007310"\n" "}"
7311"\n" "a=1"
7312"\n" "for(i=2;i<=n;++i)a*=i"
7313"\n" "scale=1.5*s"
7314"\n" "a=(x^n)/2^n/a"
7315"\n" "r=v=1"
7316"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007317"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007318"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007319"\n" "v=v*f/i/(n+i)"
7320"\n" "r+=v"
7321"\n" "}"
7322"\n" "scale=s"
7323"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007324"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007325"\n" "return(a*r/1)"
7326"\n" "}"
7327};
7328#endif // ENABLE_BC
7329
Denys Vlasenko10bde142018-12-27 18:23:58 +01007330static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007331{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007332 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007333 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007334 size_t i;
7335
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007336#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007337 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007338 // We know that internal library is not buggy,
7339 // thus error checking is normally disabled.
7340# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007341 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007342 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007343 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007344#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007345
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007346 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007347 fname = (void*)G.files.v;
7348 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007349 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007350 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7351 // Debug config, non-interactive mode:
7352 // return all the way back to main.
7353 // Non-debug builds do not come here
7354 // in non-interactive mode, they exit.
7355 RETURN_STATUS(s);
7356 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007357 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007358
Denys Vlasenko91cde952018-12-10 20:56:08 +01007359 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007360 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007361
Denys Vlasenko19f11072018-12-12 22:48:19 +01007362 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007363}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007364#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007365
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007366#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007367static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007368{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007369 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007370 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007371 bc_vec_free(&G.prog.vars);
7372 bc_vec_free(&G.prog.var_map);
7373 bc_vec_free(&G.prog.arrs);
7374 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007375 IF_DC(bc_vec_free(&G.prog.strs);)
7376 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007377 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007378 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007379 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007380 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007381 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007382 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007383}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007384#endif
7385
Denys Vlasenko10bde142018-12-27 18:23:58 +01007386static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007387{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007388 BcInstPtr ip;
7389
Denys Vlasenko82269122018-12-14 16:30:56 +01007390 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007391 memset(&ip, 0, sizeof(BcInstPtr));
7392
Denys Vlasenko82269122018-12-14 16:30:56 +01007393 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007394 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007395 G.prog.ob_t = 10;
7396
Denys Vlasenko503faf92018-12-20 16:24:18 +01007397 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7398 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007399
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007400 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007401 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007402
Denys Vlasenko503faf92018-12-20 16:24:18 +01007403 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7404 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007405
7406 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007407 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007408
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007409 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007410 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007411 // a valid function name (and be short)
7412 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007413 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007414 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007415 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007416 xc_program_add_fn();
7417 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007418 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007419
7420 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007421 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007422
7423 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007424 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007425
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007426 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7427 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007428 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007429 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7430 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007431
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007432 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007433}
Gavin Howard01055ba2018-11-03 11:00:21 -06007434
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01007435static unsigned xc_vm_envLen(const char *var)
7436{
7437 char *lenv;
7438 unsigned len;
7439
7440 lenv = getenv(var);
7441 len = BC_NUM_PRINT_WIDTH;
Denys Vlasenko29a90432020-12-29 18:50:56 +01007442 if (lenv) {
7443 len = bb_strtou(lenv, NULL, 10);
7444 if (len == 0 || len > INT_MAX)
7445 len = INT_MAX;
7446 if (errno)
7447 len = BC_NUM_PRINT_WIDTH;
7448 }
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01007449
Denys Vlasenko29a90432020-12-29 18:50:56 +01007450 // dc (GNU bc 1.07.1) 1.4.1 seems to use width
7451 // 1 char wider than bc from the same package.
7452 // Both default width, and xC_LINE_LENGTH=N are wider:
7453 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7454 // |1234\ |
7455 // |56 |
7456 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7457 // |123\ |
7458 // |456 |
7459 // Do the same, but it might be a bug in GNU package
7460 if (IS_BC)
7461 len--;
7462
7463 if (len < 2)
7464 len = IS_BC ? BC_NUM_PRINT_WIDTH - 1 : BC_NUM_PRINT_WIDTH;
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01007465
7466 return len;
7467}
7468
Denys Vlasenko10bde142018-12-27 18:23:58 +01007469static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007470{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007471 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007472 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007473
Denys Vlasenko10bde142018-12-27 18:23:58 +01007474 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007475 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007476 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007477
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007478//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7479//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007480 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007481#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007482 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007483 // With SA_RESTART, most system calls will restart
7484 // (IOW: they won't fail with EINTR).
7485 // In particular, this means ^C won't cause
7486 // stdout to get into "error state" if SIGINT hits
7487 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007488 //
7489 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007490 // will only be handled after [Enter] since read()
7491 // from stdin is not interrupted by ^C either,
7492 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007493 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007494 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7495
7496 // Without SA_RESTART, this exhibits a bug:
7497 // "while (1) print 1" and try ^C-ing it.
7498 // Intermittently, instead of returning to input line,
7499 // you'll get "output error: Interrupted system call"
7500 // and exit.
7501 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007502#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007503 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007504 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007505 return 0; // "not a tty"
7506}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007507
Denys Vlasenko10bde142018-12-27 18:23:58 +01007508static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007509{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007510 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007511#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007512 if (G_exiting) // it was actually "halt" or "quit"
7513 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007514
7515 bc_vec_free(&G.files);
7516 xc_program_free();
7517 xc_parse_free();
7518 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007519# if ENABLE_FEATURE_EDITING
7520 free_line_input_t(G.line_input_state);
7521# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007522 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007523#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007524 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007525 return st;
7526}
7527
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007528#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007529int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007530int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007531{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007532 int is_tty;
7533
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007534 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007535
Denys Vlasenko10bde142018-12-27 18:23:58 +01007536 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007537
7538 bc_args(argv);
7539
7540 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7541 bc_vm_info();
7542
Denys Vlasenko10bde142018-12-27 18:23:58 +01007543 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007544}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007545#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007546
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007547#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007548int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007549int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007550{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007551 int noscript;
7552
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007553 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007554
Denys Vlasenko10bde142018-12-27 18:23:58 +01007555 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007556
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007557 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7558 noscript = BC_FLAG_I;
7559 for (;;) {
7560 int n = getopt(argc, argv, "e:f:x");
7561 if (n <= 0)
7562 break;
7563 switch (n) {
7564 case 'e':
7565 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007566 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007567 if (n) return n;
7568 break;
7569 case 'f':
7570 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007571 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007572 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007573 break;
7574 case 'x':
7575 option_mask32 |= DC_FLAG_X;
7576 break;
7577 default:
7578 bb_show_usage();
7579 }
7580 }
7581 argv += optind;
7582
7583 while (*argv) {
7584 noscript = 0;
7585 bc_vec_push(&G.files, argv++);
7586 }
7587
7588 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7589
Denys Vlasenko10bde142018-12-27 18:23:58 +01007590 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007591}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007592#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007593
Denys Vlasenko14767602018-12-27 22:52:13 +01007594#endif // DC_BIG