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.
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));
56 static void bc_parse_update(BcParse *p, uchar inst, size_t idx) {
57 bc_parse_updateFunc(p, p->fidx);
58 bc_parse_push(p, inst);
59 bc_parse_pushIndex(p, idx);
62 void bc_parse_addString(BcParse *p) {
64 BcVec *strs = BC_IS_BC ? &p->func->strs : p->prog->strs;
70 const char *str = bc_vm_strdup(p->l.str.v);
72 bc_vec_push(strs, &str);
75 else idx = bc_program_insertFunc(p->prog, p->l.str.v) - BC_PROG_REQ_FUNCS;
78 bc_parse_update(p, BC_INST_STR, idx);
83 static void bc_parse_addNum(BcParse *p, const char *string) {
85 BcVec *consts = &p->func->consts;
89 if (bc_parse_zero[0] == string[0] && bc_parse_zero[1] == string[1]) {
90 bc_parse_push(p, BC_INST_ZERO);
93 if (bc_parse_one[0] == string[0] && bc_parse_one[1] == string[1]) {
94 bc_parse_push(p, BC_INST_ONE);
102 c.val = bc_vm_strdup(string);
103 c.base = BC_NUM_BIGDIG_MAX;
105 bc_num_clear(&c.num);
106 bc_vec_push(consts, &c);
108 bc_parse_update(p, BC_INST_NUM, idx);
113 void bc_parse_number(BcParse *p) {
115 #if BC_ENABLE_EXTRA_MATH
116 char *exp = strchr(p->l.str.v, 'e');
117 size_t idx = SIZE_MAX;
120 idx = ((size_t) (exp - p->l.str.v));
123 #endif // BC_ENABLE_EXTRA_MATH
125 bc_parse_addNum(p, p->l.str.v);
127 #if BC_ENABLE_EXTRA_MATH
132 neg = (*((char*) bc_vec_item(&p->l.str, idx + 1)) == BC_LEX_NEG_CHAR);
134 bc_parse_addNum(p, bc_vec_item(&p->l.str, idx + 1 + neg));
135 bc_parse_push(p, BC_INST_LSHIFT + neg);
137 #endif // BC_ENABLE_EXTRA_MATH
140 void bc_parse_text(BcParse *p, const char *text) {
141 // Make sure the pointer isn't invalidated.
142 p->func = bc_vec_item(&p->prog->fns, p->fidx);
143 bc_lex_text(&p->l, text);
146 void bc_parse_reset(BcParse *p) {
148 BC_SIG_ASSERT_LOCKED;
150 if (p->fidx != BC_PROG_MAIN) {
151 bc_func_reset(p->func);
152 bc_parse_updateFunc(p, BC_PROG_MAIN);
157 p->auto_part = false;
161 bc_vec_npop(&p->flags, p->flags.len - 1);
162 bc_vec_npop(&p->exits, p->exits.len);
163 bc_vec_npop(&p->conds, p->conds.len);
164 bc_vec_npop(&p->ops, p->ops.len);
168 bc_program_reset(p->prog);
170 if (BC_ERR(vm.status)) BC_VM_JMP;
173 void bc_parse_free(BcParse *p) {
175 BC_SIG_ASSERT_LOCKED;
181 bc_vec_free(&p->flags);
182 bc_vec_free(&p->exits);
183 bc_vec_free(&p->conds);
184 bc_vec_free(&p->ops);
185 bc_vec_free(&p->buf);
192 void bc_parse_init(BcParse *p, BcProgram *prog, size_t func) {
198 BC_SIG_ASSERT_LOCKED;
200 assert(p != NULL && prog != NULL);
204 bc_vec_init(&p->flags, sizeof(uint16_t), NULL);
205 bc_vec_push(&p->flags, &flag);
206 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
207 bc_vec_init(&p->conds, sizeof(size_t), NULL);
208 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
209 bc_vec_init(&p->buf, sizeof(char), NULL);
216 p->auto_part = false;
217 bc_parse_updateFunc(p, func);