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