xref: /freebsd/contrib/bc/src/parse.c (revision 23210c9f42af94dc6bcdae3996d8a3d010dd6bfe)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2021 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 <parse.h>
44 #include <program.h>
45 #include <vm.h>
46 
47 void bc_parse_updateFunc(BcParse *p, size_t fidx) {
48 	p->fidx = fidx;
49 	p->func = bc_vec_item(&p->prog->fns, fidx);
50 }
51 
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));
54 }
55 
56 /**
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.
62  */
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);
67 }
68 
69 void bc_parse_addString(BcParse *p) {
70 
71 	size_t idx;
72 
73 	idx = bc_program_addString(p->prog, p->l.str.v, p->fidx);
74 
75 	// Push the string info.
76 	bc_parse_update(p, BC_INST_STR, p->fidx);
77 	bc_parse_pushIndex(p, idx);
78 }
79 
80 static void bc_parse_addNum(BcParse *p, const char *string) {
81 
82 	BcVec *consts = &p->func->consts;
83 	size_t idx;
84 	BcConst *c;
85 	BcVec *slabs;
86 
87 	BC_SIG_ASSERT_LOCKED;
88 
89 	// Special case 0.
90 	if (bc_parse_zero[0] == string[0] && bc_parse_zero[1] == string[1]) {
91 		bc_parse_push(p, BC_INST_ZERO);
92 		return;
93 	}
94 
95 	// Special case 1.
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 	// Get the index.
102 	idx = consts->len;
103 
104 	// Get the right slab.
105 	slabs = p->fidx == BC_PROG_MAIN || p->fidx == BC_PROG_READ ?
106 	        &vm.main_const_slab : &vm.other_slabs;
107 
108 	// Push an empty constant.
109 	c = bc_vec_pushEmpty(consts);
110 
111 	// Set the fields.
112 	c->val = bc_slabvec_strdup(slabs, string);
113 	c->base = BC_NUM_BIGDIG_MAX;
114 
115 	// We need this to be able to tell that the number has not been allocated.
116 	bc_num_clear(&c->num);
117 
118 	bc_parse_update(p, BC_INST_NUM, idx);
119 }
120 
121 void bc_parse_number(BcParse *p) {
122 
123 #if BC_ENABLE_EXTRA_MATH
124 	char *exp = strchr(p->l.str.v, 'e');
125 	size_t idx = SIZE_MAX;
126 
127 	// Do we have a number in scientific notation? If so, add a nul byte where
128 	// the e is.
129 	if (exp != NULL) {
130 		idx = ((size_t) (exp - p->l.str.v));
131 		*exp = 0;
132 	}
133 #endif // BC_ENABLE_EXTRA_MATH
134 
135 	bc_parse_addNum(p, p->l.str.v);
136 
137 #if BC_ENABLE_EXTRA_MATH
138 	// If we have a number in scientific notation...
139 	if (exp != NULL) {
140 
141 		bool neg;
142 
143 		// Figure out if the exponent is negative.
144 		neg = (*((char*) bc_vec_item(&p->l.str, idx + 1)) == BC_LEX_NEG_CHAR);
145 
146 		// Add the number and instruction.
147 		bc_parse_addNum(p, bc_vec_item(&p->l.str, idx + 1 + neg));
148 		bc_parse_push(p, BC_INST_LSHIFT + neg);
149 	}
150 #endif // BC_ENABLE_EXTRA_MATH
151 }
152 
153 void bc_parse_text(BcParse *p, const char *text, bool is_stdin, bool is_exprs) {
154 
155 	BC_SIG_LOCK;
156 
157 	// Make sure the pointer isn't invalidated.
158 	p->func = bc_vec_item(&p->prog->fns, p->fidx);
159 	bc_lex_text(&p->l, text, is_stdin, is_exprs);
160 
161 	BC_SIG_UNLOCK;
162 }
163 
164 void bc_parse_reset(BcParse *p) {
165 
166 	BC_SIG_ASSERT_LOCKED;
167 
168 	// Reset the function if it isn't main and switch to main.
169 	if (p->fidx != BC_PROG_MAIN) {
170 		bc_func_reset(p->func);
171 		bc_parse_updateFunc(p, BC_PROG_MAIN);
172 	}
173 
174 	// Reset the lexer.
175 	p->l.i = p->l.len;
176 	p->l.t = BC_LEX_EOF;
177 
178 #if BC_ENABLED
179 	if (BC_IS_BC) {
180 
181 		// Get rid of the bc parser state.
182 		p->auto_part = false;
183 		bc_vec_npop(&p->flags, p->flags.len - 1);
184 		bc_vec_popAll(&p->exits);
185 		bc_vec_popAll(&p->conds);
186 		bc_vec_popAll(&p->ops);
187 	}
188 #endif // BC_ENABLED
189 
190 	// Reset the program. This might clear the error.
191 	bc_program_reset(p->prog);
192 
193 	// Jump if there is an error.
194 	if (BC_ERR(vm.status)) BC_JMP;
195 }
196 
197 #ifndef NDEBUG
198 void bc_parse_free(BcParse *p) {
199 
200 	BC_SIG_ASSERT_LOCKED;
201 
202 	assert(p != NULL);
203 
204 #if BC_ENABLED
205 	if (BC_IS_BC) {
206 		bc_vec_free(&p->flags);
207 		bc_vec_free(&p->exits);
208 		bc_vec_free(&p->conds);
209 		bc_vec_free(&p->ops);
210 		bc_vec_free(&p->buf);
211 	}
212 #endif // BC_ENABLED
213 
214 	bc_lex_free(&p->l);
215 }
216 #endif // NDEBUG
217 
218 void bc_parse_init(BcParse *p, BcProgram *prog, size_t func) {
219 
220 #if BC_ENABLED
221 	uint16_t flag = 0;
222 #endif // BC_ENABLED
223 
224 	BC_SIG_ASSERT_LOCKED;
225 
226 	assert(p != NULL && prog != NULL);
227 
228 #if BC_ENABLED
229 	if (BC_IS_BC) {
230 
231 		// We always want at least one flag set on the flags stack.
232 		bc_vec_init(&p->flags, sizeof(uint16_t), BC_DTOR_NONE);
233 		bc_vec_push(&p->flags, &flag);
234 
235 		bc_vec_init(&p->exits, sizeof(BcInstPtr), BC_DTOR_NONE);
236 		bc_vec_init(&p->conds, sizeof(size_t), BC_DTOR_NONE);
237 		bc_vec_init(&p->ops, sizeof(BcLexType), BC_DTOR_NONE);
238 		bc_vec_init(&p->buf, sizeof(char), BC_DTOR_NONE);
239 
240 		p->auto_part = false;
241 	}
242 #endif // BC_ENABLED
243 
244 	bc_lex_init(&p->l);
245 
246 	// Set up the function.
247 	p->prog = prog;
248 	bc_parse_updateFunc(p, func);
249 }
250