2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 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.
50 void bc_parse_updateFunc(BcParse *p, size_t fidx) {
52 p->func = bc_vec_item(&p->prog->fns, fidx);
55 inline void bc_parse_pushName(const BcParse *p, char *name, bool var) {
56 bc_parse_pushIndex(p, bc_program_search(p->prog, name, var));
59 static void bc_parse_update(BcParse *p, uchar inst, size_t idx) {
60 bc_parse_updateFunc(p, p->fidx);
61 bc_parse_push(p, inst);
62 bc_parse_pushIndex(p, idx);
65 void bc_parse_addString(BcParse *p) {
67 BcVec *strs = BC_IS_BC ? &p->func->strs : p->prog->strs;
73 const char *str = bc_vm_strdup(p->l.str.v);
75 bc_vec_push(strs, &str);
78 else idx = bc_program_insertFunc(p->prog, p->l.str.v) - BC_PROG_REQ_FUNCS;
81 bc_parse_update(p, BC_INST_STR, idx);
86 static void bc_parse_addNum(BcParse *p, const char *string) {
88 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);
96 if (bc_parse_one[0] == string[0] && bc_parse_one[1] == string[1]) {
97 bc_parse_push(p, BC_INST_ONE);
105 c.val = bc_vm_strdup(string);
106 c.base = BC_NUM_BIGDIG_MAX;
108 bc_num_clear(&c.num);
109 bc_vec_push(consts, &c);
111 bc_parse_update(p, BC_INST_NUM, idx);
116 void bc_parse_number(BcParse *p) {
118 #if BC_ENABLE_EXTRA_MATH
119 char *exp = strchr(p->l.str.v, 'e');
120 size_t idx = SIZE_MAX;
123 idx = ((size_t) (exp - p->l.str.v));
126 #endif // BC_ENABLE_EXTRA_MATH
128 bc_parse_addNum(p, p->l.str.v);
130 #if BC_ENABLE_EXTRA_MATH
135 neg = (*((char*) bc_vec_item(&p->l.str, idx + 1)) == BC_LEX_NEG_CHAR);
137 bc_parse_addNum(p, bc_vec_item(&p->l.str, idx + 1 + neg));
138 bc_parse_push(p, BC_INST_LSHIFT + neg);
140 #endif // BC_ENABLE_EXTRA_MATH
143 void bc_parse_text(BcParse *p, const char *text) {
144 // Make sure the pointer isn't invalidated.
145 p->func = bc_vec_item(&p->prog->fns, p->fidx);
146 bc_lex_text(&p->l, text);
149 void bc_parse_reset(BcParse *p) {
151 BC_SIG_ASSERT_LOCKED;
153 if (p->fidx != BC_PROG_MAIN) {
154 bc_func_reset(p->func);
155 bc_parse_updateFunc(p, BC_PROG_MAIN);
160 p->auto_part = false;
164 bc_vec_npop(&p->flags, p->flags.len - 1);
165 bc_vec_npop(&p->exits, p->exits.len);
166 bc_vec_npop(&p->conds, p->conds.len);
167 bc_vec_npop(&p->ops, p->ops.len);
171 bc_program_reset(p->prog);
173 if (BC_ERR(vm.status)) BC_VM_JMP;
176 void bc_parse_free(BcParse *p) {
178 BC_SIG_ASSERT_LOCKED;
184 bc_vec_free(&p->flags);
185 bc_vec_free(&p->exits);
186 bc_vec_free(&p->conds);
187 bc_vec_free(&p->ops);
188 bc_vec_free(&p->buf);
195 void bc_parse_init(BcParse *p, BcProgram *prog, size_t func) {
201 BC_SIG_ASSERT_LOCKED;
203 assert(p != NULL && prog != NULL);
207 bc_vec_init(&p->flags, sizeof(uint16_t), NULL);
208 bc_vec_push(&p->flags, &flag);
209 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
210 bc_vec_init(&p->conds, sizeof(size_t), NULL);
211 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
212 bc_vec_init(&p->buf, sizeof(char), NULL);
219 p->auto_part = false;
220 bc_parse_updateFunc(p, func);