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