2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Code common to the parsers.
47 void bc_parse_updateFunc(BcParse *p, size_t fidx) {
49 p->func = bc_vec_item(&p->prog->fns, fidx);
52 inline void bc_parse_pushName(const BcParse *p, char *name, bool var) {
53 bc_parse_pushIndex(p, bc_program_search(p->prog, name, var));
57 * Updates the function, then pushes the instruction and the index. This is a
58 * convenience function.
59 * @param p The parser.
60 * @param inst The instruction to push.
61 * @param idx The index to push.
63 static void bc_parse_update(BcParse *p, uchar inst, size_t idx) {
64 bc_parse_updateFunc(p, p->fidx);
65 bc_parse_push(p, inst);
66 bc_parse_pushIndex(p, idx);
69 void bc_parse_addString(BcParse *p) {
75 idx = bc_program_addString(p->prog, p->l.str.v, p->fidx);
77 // Push the string info.
78 bc_parse_update(p, BC_INST_STR, p->fidx);
79 bc_parse_pushIndex(p, idx);
84 static void bc_parse_addNum(BcParse *p, const char *string) {
86 BcVec *consts = &p->func->consts;
92 if (bc_parse_zero[0] == string[0] && bc_parse_zero[1] == string[1]) {
93 bc_parse_push(p, BC_INST_ZERO);
98 if (bc_parse_one[0] == string[0] && bc_parse_one[1] == string[1]) {
99 bc_parse_push(p, BC_INST_ONE);
108 // Get the right slab.
109 slabs = p->fidx == BC_PROG_MAIN || p->fidx == BC_PROG_READ ?
110 &vm.main_const_slab : &vm.other_slabs;
112 // Push an empty constant.
113 c = bc_vec_pushEmpty(consts);
116 c->val = bc_slabvec_strdup(slabs, string);
117 c->base = BC_NUM_BIGDIG_MAX;
119 // We need this to be able to tell that the number has not been allocated.
120 bc_num_clear(&c->num);
122 bc_parse_update(p, BC_INST_NUM, idx);
127 void bc_parse_number(BcParse *p) {
129 #if BC_ENABLE_EXTRA_MATH
130 char *exp = strchr(p->l.str.v, 'e');
131 size_t idx = SIZE_MAX;
133 // Do we have a number in scientific notation? If so, add a nul byte where
136 idx = ((size_t) (exp - p->l.str.v));
139 #endif // BC_ENABLE_EXTRA_MATH
141 bc_parse_addNum(p, p->l.str.v);
143 #if BC_ENABLE_EXTRA_MATH
144 // If we have a number in scientific notation...
149 // Figure out if the exponent is negative.
150 neg = (*((char*) bc_vec_item(&p->l.str, idx + 1)) == BC_LEX_NEG_CHAR);
152 // Add the number and instruction.
153 bc_parse_addNum(p, bc_vec_item(&p->l.str, idx + 1 + neg));
154 bc_parse_push(p, BC_INST_LSHIFT + neg);
156 #endif // BC_ENABLE_EXTRA_MATH
159 void bc_parse_text(BcParse *p, const char *text, bool is_stdin) {
161 // Make sure the pointer isn't invalidated.
162 p->func = bc_vec_item(&p->prog->fns, p->fidx);
163 bc_lex_text(&p->l, text, is_stdin);
166 void bc_parse_reset(BcParse *p) {
168 BC_SIG_ASSERT_LOCKED;
170 // Reset the function if it isn't main and switch to main.
171 if (p->fidx != BC_PROG_MAIN) {
172 bc_func_reset(p->func);
173 bc_parse_updateFunc(p, BC_PROG_MAIN);
183 // Get rid of the bc parser state.
184 p->auto_part = false;
185 bc_vec_npop(&p->flags, p->flags.len - 1);
186 bc_vec_popAll(&p->exits);
187 bc_vec_popAll(&p->conds);
188 bc_vec_popAll(&p->ops);
192 // Reset the program. This might clear the error.
193 bc_program_reset(p->prog);
195 // Jump if there is an error.
196 if (BC_ERR(vm.status)) BC_JMP;
200 void bc_parse_free(BcParse *p) {
202 BC_SIG_ASSERT_LOCKED;
208 bc_vec_free(&p->flags);
209 bc_vec_free(&p->exits);
210 bc_vec_free(&p->conds);
211 bc_vec_free(&p->ops);
212 bc_vec_free(&p->buf);
220 void bc_parse_init(BcParse *p, BcProgram *prog, size_t func) {
226 BC_SIG_ASSERT_LOCKED;
228 assert(p != NULL && prog != NULL);
233 // We always want at least one flag set on the flags stack.
234 bc_vec_init(&p->flags, sizeof(uint16_t), BC_DTOR_NONE);
235 bc_vec_push(&p->flags, &flag);
237 bc_vec_init(&p->exits, sizeof(BcInstPtr), BC_DTOR_NONE);
238 bc_vec_init(&p->conds, sizeof(size_t), BC_DTOR_NONE);
239 bc_vec_init(&p->ops, sizeof(BcLexType), BC_DTOR_NONE);
240 bc_vec_init(&p->buf, sizeof(char), BC_DTOR_NONE);
242 p->auto_part = false;
248 // Set up the function.
250 bc_parse_updateFunc(p, func);