xref: /freebsd/contrib/bc/src/parse.c (revision 2938ecc85c29202824e83d65af5c3a4fb7b3e5fb)
1 /*
2  * *****************************************************************************
3  *
4  * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
5  *
6  * All rights reserved.
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 	BcFunc *f = BC_IS_BC ? p->func : bc_vec_item(&p->prog->fns, BC_PROG_MAIN);
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 = f->strs.len;
75 		bc_vec_push(&f->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 #ifndef NDEBUG
82 	f = BC_IS_BC ? p->func : bc_vec_item(&p->prog->fns, BC_PROG_MAIN);
83 	assert(f->strs.len > idx);
84 #endif // NDEBUG
85 
86 	bc_parse_update(p, BC_INST_STR, idx);
87 
88 	BC_SIG_UNLOCK;
89 }
90 
91 static void bc_parse_addNum(BcParse *p, const char *string) {
92 
93 	BcFunc *f = BC_IS_BC ? p->func : bc_vec_item(&p->prog->fns, BC_PROG_MAIN);
94 	size_t idx;
95 	BcConst c;
96 
97 	if (bc_parse_one[0] == string[0] && bc_parse_one[1] == string[1]) {
98 		bc_parse_push(p, BC_INST_ONE);
99 		return;
100 	}
101 
102 	idx = f->consts.len;
103 
104 	BC_SIG_LOCK;
105 
106 	c.val = bc_vm_strdup(string);
107 	c.base = BC_NUM_BIGDIG_MAX;
108 
109 	bc_num_clear(&c.num);
110 	bc_vec_push(&f->consts, &c);
111 
112 	bc_parse_update(p, BC_INST_NUM, idx);
113 
114 	BC_SIG_UNLOCK;
115 }
116 
117 void bc_parse_number(BcParse *p) {
118 
119 #if BC_ENABLE_EXTRA_MATH
120 	char *exp = strchr(p->l.str.v, 'e');
121 	size_t idx = SIZE_MAX;
122 
123 	if (exp != NULL) {
124 		idx = ((size_t) (exp - p->l.str.v));
125 		*exp = 0;
126 	}
127 #endif // BC_ENABLE_EXTRA_MATH
128 
129 	bc_parse_addNum(p, p->l.str.v);
130 
131 #if BC_ENABLE_EXTRA_MATH
132 	if (exp != NULL) {
133 
134 		bool neg;
135 
136 		neg = (*((char*) bc_vec_item(&p->l.str, idx + 1)) == BC_LEX_NEG_CHAR);
137 
138 		bc_parse_addNum(p, bc_vec_item(&p->l.str, idx + 1 + neg));
139 		bc_parse_push(p, BC_INST_LSHIFT + neg);
140 	}
141 #endif // BC_ENABLE_EXTRA_MATH
142 }
143 
144 void bc_parse_text(BcParse *p, const char *text) {
145 	// Make sure the pointer isn't invalidated.
146 	p->func = bc_vec_item(&p->prog->fns, p->fidx);
147 	bc_lex_text(&p->l, text);
148 }
149 
150 void bc_parse_reset(BcParse *p) {
151 
152 	BC_SIG_ASSERT_LOCKED;
153 
154 	if (p->fidx != BC_PROG_MAIN) {
155 		bc_func_reset(p->func);
156 		bc_parse_updateFunc(p, BC_PROG_MAIN);
157 	}
158 
159 	p->l.i = p->l.len;
160 	p->l.t = BC_LEX_EOF;
161 	p->auto_part = false;
162 
163 #if BC_ENABLED
164 	if (BC_IS_BC) {
165 		bc_vec_npop(&p->flags, p->flags.len - 1);
166 		bc_vec_npop(&p->exits, p->exits.len);
167 		bc_vec_npop(&p->conds, p->conds.len);
168 		bc_vec_npop(&p->ops, p->ops.len);
169 	}
170 #endif // BC_ENABLED
171 
172 	bc_program_reset(p->prog);
173 
174 	if (BC_ERR(vm.status)) BC_VM_JMP;
175 }
176 
177 void bc_parse_free(BcParse *p) {
178 
179 	BC_SIG_ASSERT_LOCKED;
180 
181 	assert(p != NULL);
182 
183 #if BC_ENABLED
184 	if (BC_IS_BC) {
185 		bc_vec_free(&p->flags);
186 		bc_vec_free(&p->exits);
187 		bc_vec_free(&p->conds);
188 		bc_vec_free(&p->ops);
189 		bc_vec_free(&p->buf);
190 	}
191 #endif // BC_ENABLED
192 
193 	bc_lex_free(&p->l);
194 }
195 
196 void bc_parse_init(BcParse *p, BcProgram *prog, size_t func) {
197 
198 #if BC_ENABLED
199 	uint16_t flag = 0;
200 #endif // BC_ENABLED
201 
202 	BC_SIG_ASSERT_LOCKED;
203 
204 	assert(p != NULL && prog != NULL);
205 
206 #if BC_ENABLED
207 	if (BC_IS_BC) {
208 		bc_vec_init(&p->flags, sizeof(uint16_t), NULL);
209 		bc_vec_push(&p->flags, &flag);
210 		bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
211 		bc_vec_init(&p->conds, sizeof(size_t), NULL);
212 		bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
213 		bc_vec_init(&p->buf, sizeof(char), NULL);
214 	}
215 #endif // BC_ENABLED
216 
217 	bc_lex_init(&p->l);
218 
219 	p->prog = prog;
220 	p->auto_part = false;
221 	bc_parse_updateFunc(p, func);
222 }
223