Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 1 | /* |
| 2 | * ***************************************************************************** |
| 3 | * |
| 4 | * Copyright 2018 Gavin D. Howard |
| 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
| 17 | * ***************************************************************************** |
| 18 | * |
| 19 | * Definitions for bc's VM. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifndef BC_VM_H |
| 24 | #define BC_VM_H |
| 25 | |
Gavin Howard | 411f732 | 2018-09-26 17:21:19 -0600 | [diff] [blame] | 26 | #include <stddef.h> |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 27 | #include <limits.h> |
| 28 | |
| 29 | #include <status.h> |
| 30 | #include <parse.h> |
| 31 | #include <program.h> |
| 32 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 33 | #if !BC_ENABLED && !DC_ENABLED |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 34 | #error Must define BC_ENABLED, DC_ENABLED, or both |
Gavin Howard | a0ccea6 | 2018-09-26 21:02:22 -0600 | [diff] [blame] | 35 | #endif |
| 36 | |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 37 | // ** Exclude start. ** |
| 38 | #define VERSION_STR(V) #V |
Gavin Howard | bc2d23d | 2018-10-03 16:24:08 -0600 | [diff] [blame] | 39 | #define VERSION_STR2(V) VERSION_STR(V) |
| 40 | #define BC_VERSION VERSION_STR2(VERSION) |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 41 | // ** Exclude end. ** |
| 42 | |
Gavin Howard | f4983b8 | 2018-09-27 10:44:21 -0600 | [diff] [blame] | 43 | #define BC_FLAG_X (1<<0) |
| 44 | #define BC_FLAG_W (1<<1) |
Gavin Howard | b03e668 | 2018-10-31 12:17:36 -0600 | [diff] [blame] | 45 | #define BC_FLAG_V (1<<2) |
| 46 | #define BC_FLAG_S (1<<3) |
| 47 | #define BC_FLAG_Q (1<<4) |
| 48 | #define BC_FLAG_L (1<<5) |
| 49 | #define BC_FLAG_I (1<<6) |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 50 | |
| 51 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 52 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 53 | |
| 54 | #define BC_MAX_OBASE ((unsigned long) 999) |
| 55 | #define BC_MAX_DIM ((unsigned long) INT_MAX) |
| 56 | #define BC_MAX_SCALE ((unsigned long) UINT_MAX) |
| 57 | #define BC_MAX_STRING ((unsigned long) UINT_MAX - 1) |
| 58 | #define BC_MAX_NAME BC_MAX_STRING |
Gavin Howard | 911d7ef | 2018-10-17 09:48:41 -0600 | [diff] [blame] | 59 | #define BC_MAX_NUM BC_MAX_STRING |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 60 | #define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 61 | #define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 62 | |
Gavin Howard | f878d33 | 2018-12-03 12:58:38 -0700 | [diff] [blame] | 63 | #define BC_IS_BC (BC_ENABLED && (!DC_ENABLED || bcg.name[0] == 'b')) |
| 64 | |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 65 | typedef struct BcVm { |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 66 | |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 67 | BcParse prs; |
| 68 | BcProgram prog; |
| 69 | |
Gavin Howard | 6bd1700 | 2018-10-08 13:40:13 -0600 | [diff] [blame] | 70 | // ** Exclude start. ** |
Gavin Howard | ed2641c | 2018-10-31 11:10:15 -0600 | [diff] [blame] | 71 | uint32_t flags; |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 72 | BcVec files; |
Gavin Howard | ed2641c | 2018-10-31 11:10:15 -0600 | [diff] [blame] | 73 | |
| 74 | // ** Busybox exclude start. ** |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 75 | BcVec exprs; |
Gavin Howard | ed2641c | 2018-10-31 11:10:15 -0600 | [diff] [blame] | 76 | // ** Busybox exclude end. ** |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 77 | |
Gavin Howard | 99efb10 | 2018-10-08 22:27:25 -0600 | [diff] [blame] | 78 | char *env_args; |
Gavin Howard | af2cafd | 2018-10-05 12:36:27 -0600 | [diff] [blame] | 79 | // ** Exclude end. ** |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 80 | |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 81 | } BcVm; |
| 82 | |
| 83 | // ** Exclude start. ** |
| 84 | typedef struct BcGlobals { |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 85 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 86 | unsigned long sig; |
| 87 | unsigned long sigc; |
| 88 | unsigned long signe; |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 89 | |
Gavin Howard | 9727b6f | 2018-11-14 15:24:43 -0700 | [diff] [blame] | 90 | long i; |
Gavin Howard | ca893d7 | 2018-09-28 14:15:16 -0600 | [diff] [blame] | 91 | long ttyin; |
Gavin Howard | 9727b6f | 2018-11-14 15:24:43 -0700 | [diff] [blame] | 92 | long s; |
| 93 | long w; |
| 94 | long x; |
Gavin Howard | 118fc65 | 2018-10-03 16:45:29 -0600 | [diff] [blame] | 95 | |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 96 | // ** Busybox exclude start. ** |
Gavin Howard | f4983b8 | 2018-09-27 10:44:21 -0600 | [diff] [blame] | 97 | const char *name; |
Gavin Howard | 07f0d10 | 2018-12-04 14:28:03 -0700 | [diff] [blame^] | 98 | const char *help; |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 99 | // ** Busybox exclude end. ** |
Gavin Howard | 07f0d10 | 2018-12-04 14:28:03 -0700 | [diff] [blame^] | 100 | |
Gavin Howard | 02c856d | 2018-11-01 23:52:16 -0600 | [diff] [blame] | 101 | #if BC_ENABLE_SIGNALS |
Gavin Howard | 118fc65 | 2018-10-03 16:45:29 -0600 | [diff] [blame] | 102 | const char *sig_msg; |
Gavin Howard | 02c856d | 2018-11-01 23:52:16 -0600 | [diff] [blame] | 103 | #endif // BC_ENABLE_SIGNALS |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 104 | |
Gavin Howard | 5ec7573 | 2018-12-03 12:11:28 -0700 | [diff] [blame] | 105 | BcParseInit init; |
| 106 | BcParseExpr exp; |
| 107 | char sbgn; |
| 108 | char send; |
| 109 | |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 110 | } BcGlobals; |
| 111 | // ** Exclude end. ** |
| 112 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 113 | #if BC_ENABLED |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 114 | BcStatus bc_vm_posixError(BcStatus s, const char *file, |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 115 | size_t line, const char *msg); |
Gavin Howard | f8f232b | 2018-10-11 17:29:47 -0600 | [diff] [blame] | 116 | #endif // BC_ENABLED |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 117 | |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 118 | // ** Exclude start. ** |
| 119 | void bc_vm_info(const char* const help); |
| 120 | BcStatus bc_vm_run(int argc, char *argv[], const char *env_len); |
| 121 | // ** Exclude end. ** |
| 122 | |
| 123 | // ** Busybox exclude start. ** |
| 124 | |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 125 | void bc_vm_exit(BcStatus s); |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 126 | void bc_vm_printf(const char *fmt, ...); |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 127 | void bc_vm_puts(const char *str, FILE *restrict f); |
| 128 | void bc_vm_putchar(int c); |
| 129 | void bc_vm_fflush(FILE *restrict f); |
| 130 | |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 131 | // ** Exclude start. ** |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 132 | void* bc_vm_malloc(size_t n); |
| 133 | void* bc_vm_realloc(void *ptr, size_t n); |
| 134 | char* bc_vm_strdup(const char *str); |
| 135 | |
Gavin Howard | 0f31f5b | 2018-12-03 11:15:46 -0700 | [diff] [blame] | 136 | BcStatus bc_vm_error(BcStatus s, const char *file, size_t line); |
| 137 | // ** Exclude end. ** |
| 138 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 139 | #if BC_ENABLED |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 140 | // ** Exclude start. ** |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 141 | extern const char bc_name[]; |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 142 | // ** Exclude end. ** |
| 143 | extern const char bc_sig_msg[]; |
| 144 | #endif // BC_ENABLED |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 145 | |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 146 | // ** Exclude start. ** |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 147 | #if DC_ENABLED |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 148 | extern const char dc_name[]; |
| 149 | extern const char dc_sig_msg[]; |
| 150 | #endif // DC_ENABLED |
| 151 | |
| 152 | extern const char bc_copyright[]; |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 153 | |
| 154 | extern const char bc_lib[]; |
| 155 | extern const char *bc_lib_name; |
| 156 | |
| 157 | extern const char bc_err_fmt[]; |
Gavin Howard | a218a39 | 2018-10-25 09:13:54 -0600 | [diff] [blame] | 158 | extern const char bc_warn_fmt[]; |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 159 | extern const char bc_err_line[]; |
| 160 | extern const char *bc_errs[]; |
Gavin Howard | a218a39 | 2018-10-25 09:13:54 -0600 | [diff] [blame] | 161 | extern const uint8_t bc_err_ids[]; |
| 162 | extern const char *bc_err_msgs[]; |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 163 | // ** Exclude end. ** |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 164 | |
Gavin Howard | f89006a | 2018-10-29 13:01:51 -0600 | [diff] [blame] | 165 | // ** Busybox exclude end. ** |
| 166 | |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 167 | extern BcGlobals bcg; |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 168 | |
| 169 | #endif // BC_VM_H |