1 /* 2 * ***************************************************************************** 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2018-2020 Gavin D. Howard and contributors. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * 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. 17 * 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. 29 * 30 * ***************************************************************************** 31 * 32 * Code common to the parsers. 33 * 34 */ 35 36 #include <assert.h> 37 #include <stddef.h> 38 #include <stdlib.h> 39 #include <string.h> 40 41 #include <limits.h> 42 43 #include <status.h> 44 #include <vector.h> 45 #include <lex.h> 46 #include <parse.h> 47 #include <program.h> 48 #include <vm.h> 49 50 void bc_parse_updateFunc(BcParse *p, size_t fidx) { 51 p->fidx = fidx; 52 p->func = bc_vec_item(&p->prog->fns, fidx); 53 } 54 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)); 57 } 58 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); 63 } 64 65 void bc_parse_addString(BcParse *p) { 66 67 BcVec *strs = BC_IS_BC ? &p->func->strs : p->prog->strs; 68 size_t idx; 69 70 BC_SIG_LOCK; 71 72 if (BC_IS_BC) { 73 const char *str = bc_vm_strdup(p->l.str.v); 74 idx = strs->len; 75 bc_vec_push(strs, &str); 76 } 77 #if DC_ENABLED 78 else idx = bc_program_insertFunc(p->prog, p->l.str.v) - BC_PROG_REQ_FUNCS; 79 #endif // DC_ENABLED 80 81 bc_parse_update(p, BC_INST_STR, idx); 82 83 BC_SIG_UNLOCK; 84 } 85 86 static void bc_parse_addNum(BcParse *p, const char *string) { 87 88 BcVec *consts = &p->func->consts; 89 size_t idx; 90 BcConst c; 91 92 if (bc_parse_zero[0] == string[0] && bc_parse_zero[1] == string[1]) { 93 bc_parse_push(p, BC_INST_ZERO); 94 return; 95 } 96 if (bc_parse_one[0] == string[0] && bc_parse_one[1] == string[1]) { 97 bc_parse_push(p, BC_INST_ONE); 98 return; 99 } 100 101 idx = consts->len; 102 103 BC_SIG_LOCK; 104 105 c.val = bc_vm_strdup(string); 106 c.base = BC_NUM_BIGDIG_MAX; 107 108 bc_num_clear(&c.num); 109 bc_vec_push(consts, &c); 110 111 bc_parse_update(p, BC_INST_NUM, idx); 112 113 BC_SIG_UNLOCK; 114 } 115 116 void bc_parse_number(BcParse *p) { 117 118 #if BC_ENABLE_EXTRA_MATH 119 char *exp = strchr(p->l.str.v, 'e'); 120 size_t idx = SIZE_MAX; 121 122 if (exp != NULL) { 123 idx = ((size_t) (exp - p->l.str.v)); 124 *exp = 0; 125 } 126 #endif // BC_ENABLE_EXTRA_MATH 127 128 bc_parse_addNum(p, p->l.str.v); 129 130 #if BC_ENABLE_EXTRA_MATH 131 if (exp != NULL) { 132 133 bool neg; 134 135 neg = (*((char*) bc_vec_item(&p->l.str, idx + 1)) == BC_LEX_NEG_CHAR); 136 137 bc_parse_addNum(p, bc_vec_item(&p->l.str, idx + 1 + neg)); 138 bc_parse_push(p, BC_INST_LSHIFT + neg); 139 } 140 #endif // BC_ENABLE_EXTRA_MATH 141 } 142 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); 147 } 148 149 void bc_parse_reset(BcParse *p) { 150 151 BC_SIG_ASSERT_LOCKED; 152 153 if (p->fidx != BC_PROG_MAIN) { 154 bc_func_reset(p->func); 155 bc_parse_updateFunc(p, BC_PROG_MAIN); 156 } 157 158 p->l.i = p->l.len; 159 p->l.t = BC_LEX_EOF; 160 p->auto_part = false; 161 162 #if BC_ENABLED 163 if (BC_IS_BC) { 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); 168 } 169 #endif // BC_ENABLED 170 171 bc_program_reset(p->prog); 172 173 if (BC_ERR(vm.status)) BC_VM_JMP; 174 } 175 176 void bc_parse_free(BcParse *p) { 177 178 BC_SIG_ASSERT_LOCKED; 179 180 assert(p != NULL); 181 182 #if BC_ENABLED 183 if (BC_IS_BC) { 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); 189 } 190 #endif // BC_ENABLED 191 192 bc_lex_free(&p->l); 193 } 194 195 void bc_parse_init(BcParse *p, BcProgram *prog, size_t func) { 196 197 #if BC_ENABLED 198 uint16_t flag = 0; 199 #endif // BC_ENABLED 200 201 BC_SIG_ASSERT_LOCKED; 202 203 assert(p != NULL && prog != NULL); 204 205 #if BC_ENABLED 206 if (BC_IS_BC) { 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); 213 } 214 #endif // BC_ENABLED 215 216 bc_lex_init(&p->l); 217 218 p->prog = prog; 219 p->auto_part = false; 220 bc_parse_updateFunc(p, func); 221 } 222