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 | 112304b | 2018-12-04 14:30:01 -0700 | [diff] [blame^] | 65 | #if BC_ENABLE_SIGNALS |
| 66 | #define BC_SIGINT (bcg.sig) |
| 67 | #else // BC_ENABLE_SIGNALS |
| 68 | #define BC_SIGINT (0) |
| 69 | #endif // BC_ENABLE_SIGNALS |
| 70 | |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 71 | typedef struct BcVm { |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 72 | |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 73 | BcParse prs; |
| 74 | BcProgram prog; |
| 75 | |
Gavin Howard | 6bd1700 | 2018-10-08 13:40:13 -0600 | [diff] [blame] | 76 | // ** Exclude start. ** |
Gavin Howard | ed2641c | 2018-10-31 11:10:15 -0600 | [diff] [blame] | 77 | uint32_t flags; |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 78 | BcVec files; |
Gavin Howard | ed2641c | 2018-10-31 11:10:15 -0600 | [diff] [blame] | 79 | |
| 80 | // ** Busybox exclude start. ** |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 81 | BcVec exprs; |
Gavin Howard | ed2641c | 2018-10-31 11:10:15 -0600 | [diff] [blame] | 82 | // ** Busybox exclude end. ** |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 83 | |
Gavin Howard | 99efb10 | 2018-10-08 22:27:25 -0600 | [diff] [blame] | 84 | char *env_args; |
Gavin Howard | af2cafd | 2018-10-05 12:36:27 -0600 | [diff] [blame] | 85 | // ** Exclude end. ** |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 86 | |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 87 | } BcVm; |
| 88 | |
| 89 | // ** Exclude start. ** |
| 90 | typedef struct BcGlobals { |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 91 | |
Gavin Howard | 9727b6f | 2018-11-14 15:24:43 -0700 | [diff] [blame] | 92 | long i; |
Gavin Howard | ca893d7 | 2018-09-28 14:15:16 -0600 | [diff] [blame] | 93 | long ttyin; |
Gavin Howard | 9727b6f | 2018-11-14 15:24:43 -0700 | [diff] [blame] | 94 | long s; |
| 95 | long w; |
| 96 | long x; |
Gavin Howard | 118fc65 | 2018-10-03 16:45:29 -0600 | [diff] [blame] | 97 | |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 98 | // ** Busybox exclude start. ** |
Gavin Howard | f4983b8 | 2018-09-27 10:44:21 -0600 | [diff] [blame] | 99 | const char *name; |
Gavin Howard | 07f0d10 | 2018-12-04 14:28:03 -0700 | [diff] [blame] | 100 | const char *help; |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 101 | // ** Busybox exclude end. ** |
Gavin Howard | 07f0d10 | 2018-12-04 14:28:03 -0700 | [diff] [blame] | 102 | |
Gavin Howard | 02c856d | 2018-11-01 23:52:16 -0600 | [diff] [blame] | 103 | #if BC_ENABLE_SIGNALS |
Gavin Howard | 112304b | 2018-12-04 14:30:01 -0700 | [diff] [blame^] | 104 | unsigned long sig; |
Gavin Howard | 118fc65 | 2018-10-03 16:45:29 -0600 | [diff] [blame] | 105 | const char *sig_msg; |
Gavin Howard | 02c856d | 2018-11-01 23:52:16 -0600 | [diff] [blame] | 106 | #endif // BC_ENABLE_SIGNALS |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 107 | |
Gavin Howard | 5ec7573 | 2018-12-03 12:11:28 -0700 | [diff] [blame] | 108 | BcParseInit init; |
| 109 | BcParseExpr exp; |
| 110 | char sbgn; |
| 111 | char send; |
| 112 | |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 113 | } BcGlobals; |
| 114 | // ** Exclude end. ** |
| 115 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 116 | #if BC_ENABLED |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 117 | BcStatus bc_vm_posixError(BcStatus s, const char *file, |
Gavin Howard | 6365b24 | 2018-10-08 10:34:26 -0600 | [diff] [blame] | 118 | size_t line, const char *msg); |
Gavin Howard | f8f232b | 2018-10-11 17:29:47 -0600 | [diff] [blame] | 119 | #endif // BC_ENABLED |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 120 | |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 121 | // ** Exclude start. ** |
| 122 | void bc_vm_info(const char* const help); |
| 123 | BcStatus bc_vm_run(int argc, char *argv[], const char *env_len); |
| 124 | // ** Exclude end. ** |
| 125 | |
| 126 | // ** Busybox exclude start. ** |
| 127 | |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 128 | void bc_vm_exit(BcStatus s); |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 129 | void bc_vm_printf(const char *fmt, ...); |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 130 | void bc_vm_puts(const char *str, FILE *restrict f); |
| 131 | void bc_vm_putchar(int c); |
| 132 | void bc_vm_fflush(FILE *restrict f); |
| 133 | |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 134 | // ** Exclude start. ** |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 135 | void* bc_vm_malloc(size_t n); |
| 136 | void* bc_vm_realloc(void *ptr, size_t n); |
| 137 | char* bc_vm_strdup(const char *str); |
| 138 | |
Gavin Howard | 0f31f5b | 2018-12-03 11:15:46 -0700 | [diff] [blame] | 139 | BcStatus bc_vm_error(BcStatus s, const char *file, size_t line); |
| 140 | // ** Exclude end. ** |
| 141 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 142 | #if BC_ENABLED |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 143 | // ** Exclude start. ** |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 144 | extern const char bc_name[]; |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 145 | // ** Exclude end. ** |
| 146 | extern const char bc_sig_msg[]; |
| 147 | #endif // BC_ENABLED |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 148 | |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 149 | // ** Exclude start. ** |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 150 | #if DC_ENABLED |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 151 | extern const char dc_name[]; |
| 152 | extern const char dc_sig_msg[]; |
| 153 | #endif // DC_ENABLED |
| 154 | |
| 155 | extern const char bc_copyright[]; |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 156 | |
| 157 | extern const char bc_lib[]; |
| 158 | extern const char *bc_lib_name; |
| 159 | |
| 160 | extern const char bc_err_fmt[]; |
Gavin Howard | a218a39 | 2018-10-25 09:13:54 -0600 | [diff] [blame] | 161 | extern const char bc_warn_fmt[]; |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 162 | extern const char bc_err_line[]; |
| 163 | extern const char *bc_errs[]; |
Gavin Howard | a218a39 | 2018-10-25 09:13:54 -0600 | [diff] [blame] | 164 | extern const uint8_t bc_err_ids[]; |
| 165 | extern const char *bc_err_msgs[]; |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 166 | // ** Exclude end. ** |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 167 | |
Gavin Howard | f89006a | 2018-10-29 13:01:51 -0600 | [diff] [blame] | 168 | // ** Busybox exclude end. ** |
| 169 | |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 170 | extern BcGlobals bcg; |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 171 | |
| 172 | #endif // BC_VM_H |