blob: dd56a6e41dfefb394b43c2c9868104c60c318d8c [file] [log] [blame]
Gavin Howard5715b042018-02-12 16:11:42 -07001/*
2 * Copyright 2018 Contributors
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
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 *
Gavin Howard0dbafb52018-02-15 10:03:30 -070032 * A special license exemption is granted to the Toybox project to use this
33 * source under the following BSD 0-Clause License:
Gavin Howard5715b042018-02-12 16:11:42 -070034 *
35 * Permission to use, copy, modify, and/or distribute this software for any
36 * purpose with or without fee is hereby granted.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
39 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
40 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
41 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
42 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
43 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
44 * PERFORMANCE OF THIS SOFTWARE.
45 *
46 *******************************************************************************
47 *
48 * Definitions for program data.
49 *
50 */
51
Gavin Howard9b4c5622018-01-17 12:53:42 -070052#ifndef BC_DATA_H
53#define BC_DATA_H
54
55#include <stdbool.h>
56
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070057#include <bc.h>
58#include <vector.h>
59#include <num.h>
Gavin Howard9b4c5622018-01-17 12:53:42 -070060
61#define BC_PROGRAM_MAX_STMTS (128)
62
63#define BC_PROGRAM_DEF_SIZE (16)
64
Gavin Howard6fc77692018-01-24 18:05:21 -070065typedef enum BcTempType {
66
Gavin Howard4bc73ee2018-01-26 11:39:20 -070067 BC_TEMP_NUM,
68 BC_TEMP_NAME,
Gavin Howard6fc77692018-01-24 18:05:21 -070069
Gavin Howard4bc73ee2018-01-26 11:39:20 -070070 BC_TEMP_SCALE,
71 BC_TEMP_IBASE,
72 BC_TEMP_OBASE,
73 BC_TEMP_LAST,
Gavin Howard6fc77692018-01-24 18:05:21 -070074
75} BcTempType;
76
Gavin Howard9b4c5622018-01-17 12:53:42 -070077typedef enum BcExprType {
78
Gavin Howard4bc73ee2018-01-26 11:39:20 -070079 BC_EXPR_INC_PRE,
80 BC_EXPR_DEC_PRE,
Gavin Howard9b4c5622018-01-17 12:53:42 -070081
Gavin Howard4bc73ee2018-01-26 11:39:20 -070082 BC_EXPR_INC_POST,
83 BC_EXPR_DEC_POST,
Gavin Howard9b4c5622018-01-17 12:53:42 -070084
Gavin Howard4bc73ee2018-01-26 11:39:20 -070085 BC_EXPR_NEGATE,
Gavin Howard9b4c5622018-01-17 12:53:42 -070086
Gavin Howard4bc73ee2018-01-26 11:39:20 -070087 BC_EXPR_POWER,
Gavin Howard9b4c5622018-01-17 12:53:42 -070088
Gavin Howard4bc73ee2018-01-26 11:39:20 -070089 BC_EXPR_MULTIPLY,
90 BC_EXPR_DIVIDE,
91 BC_EXPR_MODULUS,
Gavin Howard9b4c5622018-01-17 12:53:42 -070092
Gavin Howard4bc73ee2018-01-26 11:39:20 -070093 BC_EXPR_PLUS,
94 BC_EXPR_MINUS,
Gavin Howard9b4c5622018-01-17 12:53:42 -070095
Gavin Howard4bc73ee2018-01-26 11:39:20 -070096 BC_EXPR_ASSIGN_POWER,
97 BC_EXPR_ASSIGN_MULTIPLY,
98 BC_EXPR_ASSIGN_DIVIDE,
99 BC_EXPR_ASSIGN_MODULUS,
100 BC_EXPR_ASSIGN_PLUS,
101 BC_EXPR_ASSIGN_MINUS,
102 BC_EXPR_ASSIGN,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700103
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700104 BC_EXPR_REL_EQUAL,
105 BC_EXPR_REL_LESS_EQ,
106 BC_EXPR_REL_GREATER_EQ,
107 BC_EXPR_REL_NOT_EQ,
108 BC_EXPR_REL_LESS,
109 BC_EXPR_REL_GREATER,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700110
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700111 BC_EXPR_BOOL_NOT,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700112
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700113 BC_EXPR_BOOL_OR,
114 BC_EXPR_BOOL_AND,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700115
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700116 BC_EXPR_NUMBER,
117 BC_EXPR_VAR,
118 BC_EXPR_ARRAY_ELEM,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700119
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700120 BC_EXPR_FUNC_CALL,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700121
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700122 BC_EXPR_SCALE_FUNC,
123 BC_EXPR_SCALE,
124 BC_EXPR_IBASE,
125 BC_EXPR_OBASE,
126 BC_EXPR_LAST,
127 BC_EXPR_LENGTH,
128 BC_EXPR_READ,
129 BC_EXPR_SQRT,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700130
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700131 BC_EXPR_PRINT,
Gavin Howard9b4c5622018-01-17 12:53:42 -0700132
133} BcExprType;
134
Gavin Howardd96bcae2018-02-07 19:16:19 -0700135typedef struct BcEntry {
136
137 char* name;
138 size_t idx;
139
140} BcEntry;
141
Gavin Howard9b4c5622018-01-17 12:53:42 -0700142typedef struct BcAuto {
143
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700144 char* name;
145 bool var;
Gavin Howard9b4c5622018-01-17 12:53:42 -0700146
147} BcAuto;
148
Gavin Howarde4cdf562018-01-23 13:42:39 -0700149typedef struct BcLocal {
150
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700151 const char* name;
152 bool var;
Gavin Howarde4cdf562018-01-23 13:42:39 -0700153
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700154 union {
Gavin Howardb5c77212018-02-14 17:12:34 -0700155 BcNum num;
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700156 struct {
Gavin Howardb5c77212018-02-14 17:12:34 -0700157 BcNum* array;
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700158 uint32_t num_elems;
159 };
160 };
Gavin Howarde4cdf562018-01-23 13:42:39 -0700161
162} BcLocal;
163
Gavin Howard88f39002018-01-24 16:53:07 -0700164typedef struct BcTemp {
165
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700166 BcTempType type;
Gavin Howard88f39002018-01-24 16:53:07 -0700167
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700168 union {
Gavin Howard88f39002018-01-24 16:53:07 -0700169
Gavin Howardb5c77212018-02-14 17:12:34 -0700170 BcNum num;
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700171 const char* name;
Gavin Howard88f39002018-01-24 16:53:07 -0700172
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700173 };
Gavin Howard88f39002018-01-24 16:53:07 -0700174
175} BcTemp;
176
Gavin Howard9b4c5622018-01-17 12:53:42 -0700177typedef struct BcFunc {
178
Gavin Howard5a8949e2018-02-05 16:46:06 -0700179 BcVec code;
Gavin Howard9b4c5622018-01-17 12:53:42 -0700180
Gavin Howarde46c6822018-02-08 20:05:39 -0700181 BcVec labels;
182
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700183 BcAuto* params;
184 uint32_t num_params;
185 uint32_t param_cap;
Gavin Howard9b4c5622018-01-17 12:53:42 -0700186
Gavin Howard4bc73ee2018-01-26 11:39:20 -0700187 BcAuto* autos;
188 uint32_t num_autos;
189 uint32_t auto_cap;
Gavin Howard9b4c5622018-01-17 12:53:42 -0700190
191} BcFunc;
192
Gavin Howardb5c77212018-02-14 17:12:34 -0700193typedef BcNum BcVar;
Gavin Howard9b4c5622018-01-17 12:53:42 -0700194
Gavin Howarde46c6822018-02-08 20:05:39 -0700195typedef BcVec BcArray;
Gavin Howard9b4c5622018-01-17 12:53:42 -0700196
Gavin Howardb5c77212018-02-14 17:12:34 -0700197typedef enum BcResultType {
Gavin Howard602178d2018-02-09 12:03:28 -0700198
199 BC_NUM_RESULT,
200
201 BC_NUM_CONSTANT,
202
203 BC_NUM_VAR,
204 BC_NUM_ARRAY,
205
206 BC_NUM_SCALE,
207 BC_NUM_LAST,
208 BC_NUM_IBASE,
209 BC_NUM_OBASE,
210
Gavin Howardb5c77212018-02-14 17:12:34 -0700211} BcResultType;
Gavin Howard602178d2018-02-09 12:03:28 -0700212
Gavin Howardb5c77212018-02-14 17:12:34 -0700213typedef struct BcResult {
Gavin Howard602178d2018-02-09 12:03:28 -0700214
Gavin Howardb5c77212018-02-14 17:12:34 -0700215 BcResultType type;
216 BcNum num;
Gavin Howard602178d2018-02-09 12:03:28 -0700217
Gavin Howardb5c77212018-02-14 17:12:34 -0700218} BcResult;
Gavin Howard602178d2018-02-09 12:03:28 -0700219
Gavin Howard5a8949e2018-02-05 16:46:06 -0700220typedef struct BcInstPtr {
221
222 size_t func;
223 size_t idx;
224
225} BcInstPtr;
Gavin Howard9b4c5622018-01-17 12:53:42 -0700226
Gavin Howarde46c6822018-02-08 20:05:39 -0700227BcStatus bc_func_init(BcFunc* func);
Gavin Howard9b4c5622018-01-17 12:53:42 -0700228BcStatus bc_func_insertParam(BcFunc* func, char* name, bool var);
229BcStatus bc_func_insertAuto(BcFunc* func, char* name, bool var);
Gavin Howard9b4c5622018-01-17 12:53:42 -0700230void bc_func_free(void* func);
231
Gavin Howarde46c6822018-02-08 20:05:39 -0700232BcStatus bc_var_init(BcVar* var);
Gavin Howard9b4c5622018-01-17 12:53:42 -0700233void bc_var_free(void* var);
234
Gavin Howarde46c6822018-02-08 20:05:39 -0700235BcStatus bc_array_init(BcArray* array);
Gavin Howard9b4c5622018-01-17 12:53:42 -0700236void bc_array_free(void* array);
237
Gavin Howarde4cdf562018-01-23 13:42:39 -0700238BcStatus bc_local_initVar(BcLocal* local, const char* name, const char* num);
239BcStatus bc_local_initArray(BcLocal* local, const char* name, uint32_t nelems);
240void bc_local_free(void* local);
Gavin Howard9b4c5622018-01-17 12:53:42 -0700241
Gavin Howard88f39002018-01-24 16:53:07 -0700242BcStatus bc_temp_initNum(BcTemp* temp, const char* val);
243BcStatus bc_temp_initName(BcTemp* temp, const char* name);
Gavin Howard6fc77692018-01-24 18:05:21 -0700244BcStatus bc_temp_init(BcTemp* temp, BcTempType type);
Gavin Howard88f39002018-01-24 16:53:07 -0700245void bc_temp_free(void* temp);
246
Gavin Howard5a8949e2018-02-05 16:46:06 -0700247void bc_string_free(void* string);
248
Gavin Howardd96bcae2018-02-07 19:16:19 -0700249int bc_entry_cmp(void* entry1, void*entry2);
250void bc_entry_free(void* entry);
251
Gavin Howardb5c77212018-02-14 17:12:34 -0700252void bc_result_free(void* num);
Gavin Howard91072cf2018-02-08 21:07:51 -0700253
Gavin Howard0a8772b2018-02-12 20:16:59 -0700254void bc_constant_free(void* constant);
255
Gavin Howard9b4c5622018-01-17 12:53:42 -0700256#endif // BC_DATA_H