Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 1 | /* |
| 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 Howard | 0dbafb5 | 2018-02-15 10:03:30 -0700 | [diff] [blame] | 32 | * A special license exemption is granted to the Toybox project to use this |
| 33 | * source under the following BSD 0-Clause License: |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 34 | * |
| 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 Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 52 | #ifndef BC_DATA_H |
| 53 | #define BC_DATA_H |
| 54 | |
| 55 | #include <stdbool.h> |
| 56 | |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 57 | #include <bc.h> |
| 58 | #include <vector.h> |
| 59 | #include <num.h> |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 60 | |
| 61 | #define BC_PROGRAM_MAX_STMTS (128) |
| 62 | |
| 63 | #define BC_PROGRAM_DEF_SIZE (16) |
| 64 | |
Gavin Howard | 6fc7769 | 2018-01-24 18:05:21 -0700 | [diff] [blame] | 65 | typedef enum BcTempType { |
| 66 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 67 | BC_TEMP_NUM, |
| 68 | BC_TEMP_NAME, |
Gavin Howard | 6fc7769 | 2018-01-24 18:05:21 -0700 | [diff] [blame] | 69 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 70 | BC_TEMP_SCALE, |
| 71 | BC_TEMP_IBASE, |
| 72 | BC_TEMP_OBASE, |
| 73 | BC_TEMP_LAST, |
Gavin Howard | 6fc7769 | 2018-01-24 18:05:21 -0700 | [diff] [blame] | 74 | |
| 75 | } BcTempType; |
| 76 | |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 77 | typedef enum BcExprType { |
| 78 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 79 | BC_EXPR_INC_PRE, |
| 80 | BC_EXPR_DEC_PRE, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 81 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 82 | BC_EXPR_INC_POST, |
| 83 | BC_EXPR_DEC_POST, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 84 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 85 | BC_EXPR_NEGATE, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 86 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 87 | BC_EXPR_POWER, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 88 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 89 | BC_EXPR_MULTIPLY, |
| 90 | BC_EXPR_DIVIDE, |
| 91 | BC_EXPR_MODULUS, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 92 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 93 | BC_EXPR_PLUS, |
| 94 | BC_EXPR_MINUS, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 95 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 96 | 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 Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 103 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 104 | 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 Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 110 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 111 | BC_EXPR_BOOL_NOT, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 112 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 113 | BC_EXPR_BOOL_OR, |
| 114 | BC_EXPR_BOOL_AND, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 115 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 116 | BC_EXPR_NUMBER, |
| 117 | BC_EXPR_VAR, |
| 118 | BC_EXPR_ARRAY_ELEM, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 119 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 120 | BC_EXPR_FUNC_CALL, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 121 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 122 | 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 Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 130 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 131 | BC_EXPR_PRINT, |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 132 | |
| 133 | } BcExprType; |
| 134 | |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 135 | typedef struct BcEntry { |
| 136 | |
| 137 | char* name; |
| 138 | size_t idx; |
| 139 | |
| 140 | } BcEntry; |
| 141 | |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 142 | typedef struct BcAuto { |
| 143 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 144 | char* name; |
| 145 | bool var; |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 146 | |
| 147 | } BcAuto; |
| 148 | |
Gavin Howard | e4cdf56 | 2018-01-23 13:42:39 -0700 | [diff] [blame] | 149 | typedef struct BcLocal { |
| 150 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 151 | const char* name; |
| 152 | bool var; |
Gavin Howard | e4cdf56 | 2018-01-23 13:42:39 -0700 | [diff] [blame] | 153 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 154 | union { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 155 | BcNum num; |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 156 | struct { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 157 | BcNum* array; |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 158 | uint32_t num_elems; |
| 159 | }; |
| 160 | }; |
Gavin Howard | e4cdf56 | 2018-01-23 13:42:39 -0700 | [diff] [blame] | 161 | |
| 162 | } BcLocal; |
| 163 | |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 164 | typedef struct BcTemp { |
| 165 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 166 | BcTempType type; |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 167 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 168 | union { |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 169 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 170 | BcNum num; |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 171 | const char* name; |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 172 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 173 | }; |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 174 | |
| 175 | } BcTemp; |
| 176 | |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 177 | typedef struct BcFunc { |
| 178 | |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 179 | BcVec code; |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 180 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 181 | BcVec labels; |
| 182 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 183 | BcAuto* params; |
| 184 | uint32_t num_params; |
| 185 | uint32_t param_cap; |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 186 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 187 | BcAuto* autos; |
| 188 | uint32_t num_autos; |
| 189 | uint32_t auto_cap; |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 190 | |
| 191 | } BcFunc; |
| 192 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 193 | typedef BcNum BcVar; |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 194 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 195 | typedef BcVec BcArray; |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 196 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 197 | typedef enum BcResultType { |
Gavin Howard | 602178d | 2018-02-09 12:03:28 -0700 | [diff] [blame] | 198 | |
| 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 Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 211 | } BcResultType; |
Gavin Howard | 602178d | 2018-02-09 12:03:28 -0700 | [diff] [blame] | 212 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 213 | typedef struct BcResult { |
Gavin Howard | 602178d | 2018-02-09 12:03:28 -0700 | [diff] [blame] | 214 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 215 | BcResultType type; |
| 216 | BcNum num; |
Gavin Howard | 602178d | 2018-02-09 12:03:28 -0700 | [diff] [blame] | 217 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 218 | } BcResult; |
Gavin Howard | 602178d | 2018-02-09 12:03:28 -0700 | [diff] [blame] | 219 | |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 220 | typedef struct BcInstPtr { |
| 221 | |
| 222 | size_t func; |
| 223 | size_t idx; |
| 224 | |
| 225 | } BcInstPtr; |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 226 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 227 | BcStatus bc_func_init(BcFunc* func); |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 228 | BcStatus bc_func_insertParam(BcFunc* func, char* name, bool var); |
| 229 | BcStatus bc_func_insertAuto(BcFunc* func, char* name, bool var); |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 230 | void bc_func_free(void* func); |
| 231 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 232 | BcStatus bc_var_init(BcVar* var); |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 233 | void bc_var_free(void* var); |
| 234 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 235 | BcStatus bc_array_init(BcArray* array); |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 236 | void bc_array_free(void* array); |
| 237 | |
Gavin Howard | e4cdf56 | 2018-01-23 13:42:39 -0700 | [diff] [blame] | 238 | BcStatus bc_local_initVar(BcLocal* local, const char* name, const char* num); |
| 239 | BcStatus bc_local_initArray(BcLocal* local, const char* name, uint32_t nelems); |
| 240 | void bc_local_free(void* local); |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 241 | |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 242 | BcStatus bc_temp_initNum(BcTemp* temp, const char* val); |
| 243 | BcStatus bc_temp_initName(BcTemp* temp, const char* name); |
Gavin Howard | 6fc7769 | 2018-01-24 18:05:21 -0700 | [diff] [blame] | 244 | BcStatus bc_temp_init(BcTemp* temp, BcTempType type); |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 245 | void bc_temp_free(void* temp); |
| 246 | |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 247 | void bc_string_free(void* string); |
| 248 | |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 249 | int bc_entry_cmp(void* entry1, void*entry2); |
| 250 | void bc_entry_free(void* entry); |
| 251 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 252 | void bc_result_free(void* num); |
Gavin Howard | 91072cf | 2018-02-08 21:07:51 -0700 | [diff] [blame] | 253 | |
Gavin Howard | 0a8772b | 2018-02-12 20:16:59 -0700 | [diff] [blame] | 254 | void bc_constant_free(void* constant); |
| 255 | |
Gavin Howard | 9b4c562 | 2018-01-17 12:53:42 -0700 | [diff] [blame] | 256 | #endif // BC_DATA_H |