• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * *****************************************************************************
3  *
4  * Copyright (c) 2018-2019 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  * Definitions for program data.
33  *
34  */
35 
36 #ifndef BC_LANG_H
37 #define BC_LANG_H
38 
39 #include <stdbool.h>
40 
41 #include <status.h>
42 #include <vector.h>
43 #include <num.h>
44 
45 #if BC_ENABLED
46 #define BC_INST_IS_ASSIGN(i) \
47 	((i) == BC_INST_ASSIGN || (i) == BC_INST_ASSIGN_NO_VAL)
48 #define BC_INST_USE_VAL(i) ((i) <= BC_INST_ASSIGN)
49 #else // BC_ENABLED
50 #define BC_INST_IS_ASSIGN(i) ((i) == BC_INST_ASSIGN_NO_VAL)
51 #define BC_INST_USE_VAL(i) (false)
52 #endif // BC_ENABLED
53 
54 typedef enum BcInst {
55 
56 #if BC_ENABLED
57 	BC_INST_INC_POST = 0,
58 	BC_INST_DEC_POST,
59 	BC_INST_INC_PRE,
60 	BC_INST_DEC_PRE,
61 #endif // BC_ENABLED
62 
63 	BC_INST_NEG,
64 	BC_INST_BOOL_NOT,
65 #if BC_ENABLE_EXTRA_MATH
66 	BC_INST_TRUNC,
67 #endif // BC_ENABLE_EXTRA_MATH
68 
69 	BC_INST_POWER,
70 	BC_INST_MULTIPLY,
71 	BC_INST_DIVIDE,
72 	BC_INST_MODULUS,
73 	BC_INST_PLUS,
74 	BC_INST_MINUS,
75 
76 #if BC_ENABLE_EXTRA_MATH
77 	BC_INST_PLACES,
78 
79 	BC_INST_LSHIFT,
80 	BC_INST_RSHIFT,
81 #endif // BC_ENABLE_EXTRA_MATH
82 
83 	BC_INST_REL_EQ,
84 	BC_INST_REL_LE,
85 	BC_INST_REL_GE,
86 	BC_INST_REL_NE,
87 	BC_INST_REL_LT,
88 	BC_INST_REL_GT,
89 
90 	BC_INST_BOOL_OR,
91 	BC_INST_BOOL_AND,
92 
93 #if BC_ENABLED
94 	BC_INST_ASSIGN_POWER,
95 	BC_INST_ASSIGN_MULTIPLY,
96 	BC_INST_ASSIGN_DIVIDE,
97 	BC_INST_ASSIGN_MODULUS,
98 	BC_INST_ASSIGN_PLUS,
99 	BC_INST_ASSIGN_MINUS,
100 #if BC_ENABLE_EXTRA_MATH
101 	BC_INST_ASSIGN_PLACES,
102 	BC_INST_ASSIGN_LSHIFT,
103 	BC_INST_ASSIGN_RSHIFT,
104 #endif // BC_ENABLE_EXTRA_MATH
105 	BC_INST_ASSIGN,
106 
107 	BC_INST_INC_NO_VAL,
108 	BC_INST_DEC_NO_VAL,
109 
110 	BC_INST_ASSIGN_POWER_NO_VAL,
111 	BC_INST_ASSIGN_MULTIPLY_NO_VAL,
112 	BC_INST_ASSIGN_DIVIDE_NO_VAL,
113 	BC_INST_ASSIGN_MODULUS_NO_VAL,
114 	BC_INST_ASSIGN_PLUS_NO_VAL,
115 	BC_INST_ASSIGN_MINUS_NO_VAL,
116 #if BC_ENABLE_EXTRA_MATH
117 	BC_INST_ASSIGN_PLACES_NO_VAL,
118 	BC_INST_ASSIGN_LSHIFT_NO_VAL,
119 	BC_INST_ASSIGN_RSHIFT_NO_VAL,
120 #endif // BC_ENABLE_EXTRA_MATH
121 #endif // BC_ENABLED
122 	BC_INST_ASSIGN_NO_VAL,
123 
124 	BC_INST_NUM,
125 	BC_INST_VAR,
126 	BC_INST_ARRAY_ELEM,
127 #if BC_ENABLED
128 	BC_INST_ARRAY,
129 #endif // BC_ENABLED
130 
131 	BC_INST_ONE,
132 
133 #if BC_ENABLED
134 	BC_INST_LAST,
135 #endif // BC_ENABLED
136 	BC_INST_IBASE,
137 	BC_INST_OBASE,
138 	BC_INST_SCALE,
139 	BC_INST_LENGTH,
140 	BC_INST_SCALE_FUNC,
141 	BC_INST_SQRT,
142 	BC_INST_ABS,
143 	BC_INST_READ,
144 	BC_INST_MAXIBASE,
145 	BC_INST_MAXOBASE,
146 	BC_INST_MAXSCALE,
147 
148 	BC_INST_PRINT,
149 	BC_INST_PRINT_POP,
150 	BC_INST_STR,
151 	BC_INST_PRINT_STR,
152 
153 #if BC_ENABLED
154 	BC_INST_JUMP,
155 	BC_INST_JUMP_ZERO,
156 
157 	BC_INST_CALL,
158 
159 	BC_INST_RET,
160 	BC_INST_RET0,
161 	BC_INST_RET_VOID,
162 
163 	BC_INST_HALT,
164 #endif // BC_ENABLED
165 
166 	BC_INST_POP,
167 
168 #if DC_ENABLED
169 	BC_INST_POP_EXEC,
170 	BC_INST_MODEXP,
171 	BC_INST_DIVMOD,
172 
173 	BC_INST_EXECUTE,
174 	BC_INST_EXEC_COND,
175 
176 	BC_INST_ASCIIFY,
177 	BC_INST_PRINT_STREAM,
178 
179 	BC_INST_PRINT_STACK,
180 	BC_INST_CLEAR_STACK,
181 	BC_INST_STACK_LEN,
182 	BC_INST_DUPLICATE,
183 	BC_INST_SWAP,
184 
185 	BC_INST_LOAD,
186 	BC_INST_PUSH_VAR,
187 	BC_INST_PUSH_TO_VAR,
188 
189 	BC_INST_QUIT,
190 	BC_INST_NQUIT,
191 #endif // DC_ENABLED
192 
193 	BC_INST_INVALID = UCHAR_MAX,
194 
195 } BcInst;
196 
197 typedef struct BcId {
198 	char *name;
199 	size_t idx;
200 } BcId;
201 
202 typedef struct BcLoc {
203 	size_t loc;
204 	size_t idx;
205 } BcLoc;
206 
207 typedef struct BcConst {
208 	char *val;
209 	BcBigDig base;
210 	BcNum num;
211 } BcConst;
212 
213 typedef struct BcFunc {
214 
215 	BcVec code;
216 #if BC_ENABLED
217 	BcVec labels;
218 	BcVec autos;
219 	size_t nparams;
220 #endif // BC_ENABLED
221 
222 	BcVec strs;
223 	BcVec consts;
224 
225 	const char *name;
226 #if BC_ENABLED
227 	bool voidfn;
228 #endif // BC_ENABLED
229 
230 } BcFunc;
231 
232 typedef enum BcResultType {
233 
234 	BC_RESULT_VAR,
235 	BC_RESULT_ARRAY_ELEM,
236 #if BC_ENABLED
237 	BC_RESULT_ARRAY,
238 #endif // BC_ENABLED
239 
240 	BC_RESULT_STR,
241 
242 	BC_RESULT_CONSTANT,
243 	BC_RESULT_TEMP,
244 
245 	BC_RESULT_ONE,
246 
247 #if BC_ENABLED
248 	BC_RESULT_LAST,
249 	BC_RESULT_VOID,
250 #endif // BC_ENABLED
251 	BC_RESULT_IBASE,
252 	BC_RESULT_OBASE,
253 	BC_RESULT_SCALE,
254 
255 } BcResultType;
256 
257 typedef union BcResultData {
258 	BcNum n;
259 	BcVec v;
260 	BcLoc loc;
261 } BcResultData;
262 
263 typedef struct BcResult {
264 	BcResultType t;
265 	BcResultData d;
266 } BcResult;
267 
268 typedef struct BcInstPtr {
269 	size_t func;
270 	size_t idx;
271 	size_t len;
272 } BcInstPtr;
273 
274 typedef enum BcType {
275 	BC_TYPE_VAR,
276 	BC_TYPE_ARRAY,
277 #if BC_ENABLED
278 	BC_TYPE_REF,
279 #endif // BC_ENABLED
280 } BcType;
281 
282 struct BcProgram;
283 
284 void bc_func_init(BcFunc *f, const char* name);
285 BcStatus bc_func_insert(BcFunc *f, struct BcProgram* p, char* name,
286                         BcType type, size_t line);
287 void bc_func_reset(BcFunc *f);
288 void bc_func_free(void *func);
289 
290 void bc_array_init(BcVec *a, bool nums);
291 void bc_array_copy(BcVec *d, const BcVec *s);
292 
293 void bc_string_free(void *string);
294 void bc_const_free(void *constant);
295 void bc_id_free(void *id);
296 void bc_result_copy(BcResult *d, BcResult *src);
297 void bc_result_free(void *result);
298 
299 void bc_array_expand(BcVec *a, size_t len);
300 int bc_id_cmp(const BcId *e1, const BcId *e2);
301 
302 #if BC_DEBUG_CODE
303 extern const char* bc_inst_names[];
304 #endif // BC_DEBUG_CODE
305 
306 extern const char bc_func_main[];
307 extern const char bc_func_read[];
308 
309 #endif // BC_LANG_H
310