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