Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [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 | * Constant data for bc. |
| 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 23 | #include <lex.h> |
| 24 | #include <parse.h> |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 25 | #include <bc.h> |
Gavin Howard | d2a0525 | 2018-09-27 14:00:40 -0600 | [diff] [blame] | 26 | #include <num.h> |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 27 | |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 28 | #ifdef BC_ENABLED |
Gavin Howard | 29204d6 | 2018-09-28 09:44:35 -0600 | [diff] [blame] | 29 | // ** Exclude start. ** |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 30 | const char bc_name[] = "bc"; |
Gavin Howard | 29204d6 | 2018-09-28 09:44:35 -0600 | [diff] [blame] | 31 | // ** Exclude end. ** |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 32 | const char bc_sig_msg[] = "\ninterrupt (type \"quit\" to exit)\n"; |
| 33 | #endif // BC_ENABLED |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 34 | |
Gavin Howard | a95501a | 2018-10-03 16:13:34 -0600 | [diff] [blame] | 35 | #ifdef DC_ENABLED |
| 36 | const char dc_name[] = "dc"; |
| 37 | const char dc_sig_msg[] = "\ninturrupt (type \"q\" to exit)\n"; |
| 38 | #endif // DC_ENABLED |
| 39 | |
| 40 | const char bc_copyright[] = |
Gavin Howard | cdcae37 | 2018-09-26 16:49:04 -0600 | [diff] [blame] | 41 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 42 | "Report bugs at: https://github.com/gavinhoward/bc\n\n" |
Gavin Howard | 4104403 | 2018-09-27 10:49:51 -0600 | [diff] [blame] | 43 | "This is free software with ABSOLUTELY NO WARRANTY.\n"; |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 44 | |
Gavin Howard | 4294179 | 2018-10-03 11:20:22 -0600 | [diff] [blame] | 45 | const char bc_err_fmt[] = "\n%s error: %s\n"; |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 46 | const char bc_err_line[] = ":%zu\n\n"; |
Gavin Howard | 79ce08b | 2018-06-21 16:21:55 -0600 | [diff] [blame] | 47 | |
| 48 | const char *bc_errs[] = { |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 49 | "VM", |
| 50 | "Lex", |
| 51 | "Parse", |
| 52 | "Math", |
| 53 | "Runtime", |
| 54 | "POSIX", |
Gavin Howard | 9a65a4e | 2018-03-15 18:26:57 -0600 | [diff] [blame] | 55 | }; |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 56 | |
Gavin Howard | 79ce08b | 2018-06-21 16:21:55 -0600 | [diff] [blame] | 57 | const uint8_t bc_err_indices[] = { |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 58 | BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM, |
| 59 | BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, |
Gavin Howard | 8412ba8 | 2018-10-04 13:03:24 -0600 | [diff] [blame] | 60 | #ifdef DC_ENABLED |
| 61 | BC_ERR_IDX_LEX, |
| 62 | #endif // DC_ENABLED |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 63 | BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, |
| 64 | BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, |
| 65 | BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, |
| 66 | BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 67 | #ifdef DC_ENABLED |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 68 | BC_ERR_IDX_MATH, |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 69 | #endif // DC_ENABLED |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 70 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
| 71 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
| 72 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
| 73 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
Gavin Howard | 044644b | 2018-10-02 14:42:45 -0600 | [diff] [blame] | 74 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 75 | BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, |
| 76 | BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, |
| 77 | BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, |
| 78 | BC_ERR_IDX_VEC, BC_ERR_IDX_VEC, |
| 79 | BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | const char *bc_err_descs[] = { |
Gavin Howard | 0790bca | 2018-09-22 19:44:35 -0600 | [diff] [blame] | 83 | |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 84 | NULL, |
| 85 | "memory allocation error", |
| 86 | "I/O error", |
| 87 | "file is not text:", |
Gavin Howard | e77b83d | 2018-03-29 14:24:26 -0600 | [diff] [blame] | 88 | |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 89 | "bad character", |
| 90 | "string end could not be found", |
| 91 | "comment end could not be found", |
| 92 | "end of file", |
Gavin Howard | 8412ba8 | 2018-10-04 13:03:24 -0600 | [diff] [blame] | 93 | #ifdef DC_ENABLED |
| 94 | "extended register", |
| 95 | #endif // DC_ENABLED |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 96 | |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 97 | "bad token", |
| 98 | "bad expression", |
Gavin Howard | 6d632e9 | 2018-09-28 11:26:23 -0600 | [diff] [blame] | 99 | "empty expression", |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 100 | "bad print statement", |
| 101 | "bad function definition", |
| 102 | "bad assignment: left side must be scale, ibase, " |
| 103 | "obase, last, var, or array element", |
| 104 | "no auto variable found", |
| 105 | "function parameter or auto var has the same name as another", |
| 106 | "block end could not be found", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 107 | |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 108 | "negative number", |
| 109 | "non integer number", |
| 110 | "overflow", |
| 111 | "divide by zero", |
| 112 | "negative square root", |
| 113 | "bad number string", |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 114 | #ifdef DC_ENABLED |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 115 | "modulus overflowed base", |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 116 | #endif // DC_ENABLED |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 117 | |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 118 | "could not open file:", |
| 119 | "mismatched parameters", |
| 120 | "undefined function", |
| 121 | "file is not executable:", |
| 122 | "could not install signal handler", |
| 123 | "bad scale; must be [0, BC_SCALE_MAX]", |
| 124 | "bad ibase; must be [2, 16]", |
| 125 | "bad obase; must be [2, BC_BASE_MAX]", |
| 126 | "number too long: must be [1, BC_NUM_MAX]", |
| 127 | "name too long: must be [1, BC_NAME_MAX]", |
| 128 | "string too long: must be [1, BC_STRING_MAX]", |
| 129 | "array too long; must be [1, BC_DIM_MAX]", |
| 130 | "bad read() expression", |
| 131 | "read() call inside of a read() call", |
| 132 | "variable is wrong type", |
| 133 | "signal caught", |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 134 | "stack has too few elements", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 135 | |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 136 | "POSIX only allows one character names; the following is bad:", |
| 137 | "POSIX does not allow '#' script comments", |
| 138 | "POSIX does not allow the following keyword:", |
| 139 | "POSIX does not allow a period ('.') as a shortcut for the last result", |
| 140 | "POSIX requires parentheses around return expressions", |
| 141 | "POSIX does not allow boolean operators; the following is bad:", |
| 142 | "POSIX does not allow comparison operators outside if or loops", |
| 143 | "POSIX requires exactly one comparison operator per condition", |
| 144 | "POSIX does not allow an empty init expression in a for loop", |
| 145 | "POSIX does not allow an empty condition expression in a for loop", |
| 146 | "POSIX does not allow an empty update expression in a for loop", |
| 147 | "POSIX requires the left brace be on the same line as the function header", |
Gavin Howard | a049214 | 2018-08-29 15:28:35 -0600 | [diff] [blame] | 148 | |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 149 | "index is out of bounds", |
| 150 | "item already exists", |
Gavin Howard | c4fab67 | 2018-09-28 18:58:45 -0600 | [diff] [blame] | 151 | |
Gavin Howard | 5f0cf2c | 2018-08-31 20:22:34 -0600 | [diff] [blame] | 152 | #ifndef NDEBUG |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 153 | "quit request not honored", |
| 154 | "limits request not honored", |
Gavin Howard | c3a6680 | 2018-09-04 16:53:26 -0600 | [diff] [blame] | 155 | #endif // NDEBUG |
Gavin Howard | 0790bca | 2018-09-22 19:44:35 -0600 | [diff] [blame] | 156 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Gavin Howard | 3bc436f | 2018-09-28 14:02:34 -0600 | [diff] [blame] | 159 | const char bc_func_main[] = "(main)"; |
| 160 | const char bc_func_read[] = "(read)"; |
Gavin Howard | ff66e29 | 2018-03-28 12:47:58 -0600 | [diff] [blame] | 161 | |
Gavin Howard | e0eec66 | 2018-03-29 17:53:20 -0600 | [diff] [blame] | 162 | #ifndef NDEBUG |
Gavin Howard | 3bc436f | 2018-09-28 14:02:34 -0600 | [diff] [blame] | 163 | const char bc_inst_chars[] = |
Gavin Howard | 044644b | 2018-10-02 14:42:45 -0600 | [diff] [blame] | 164 | "edED_^*/%+-=;?~<>!|&`{}@[],NVMACaI.LlrOqpQsSJjPR$HovxzcZdfghkK"; |
Gavin Howard | e0eec66 | 2018-03-29 17:53:20 -0600 | [diff] [blame] | 165 | #endif // NDEBUG |
Gavin Howard | 101b5e6 | 2018-03-26 07:46:27 -0600 | [diff] [blame] | 166 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 167 | #ifdef BC_ENABLED |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 168 | const BcLexKeyword bc_lex_kws[20] = { |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 169 | BC_LEX_KW_ENTRY("auto", 4, true), |
| 170 | BC_LEX_KW_ENTRY("break", 5, true), |
| 171 | BC_LEX_KW_ENTRY("continue", 8, false), |
| 172 | BC_LEX_KW_ENTRY("define", 6, true), |
| 173 | BC_LEX_KW_ENTRY("else", 4, false), |
| 174 | BC_LEX_KW_ENTRY("for", 3, true), |
| 175 | BC_LEX_KW_ENTRY("halt", 4, false), |
| 176 | BC_LEX_KW_ENTRY("ibase", 5, true), |
| 177 | BC_LEX_KW_ENTRY("if", 2, true), |
| 178 | BC_LEX_KW_ENTRY("last", 4, false), |
| 179 | BC_LEX_KW_ENTRY("length", 6, true), |
| 180 | BC_LEX_KW_ENTRY("limits", 6, false), |
| 181 | BC_LEX_KW_ENTRY("obase", 5, true), |
| 182 | BC_LEX_KW_ENTRY("print", 5, false), |
| 183 | BC_LEX_KW_ENTRY("quit", 4, true), |
| 184 | BC_LEX_KW_ENTRY("read", 4, false), |
| 185 | BC_LEX_KW_ENTRY("return", 6, true), |
| 186 | BC_LEX_KW_ENTRY("scale", 5, true), |
| 187 | BC_LEX_KW_ENTRY("sqrt", 4, true), |
| 188 | BC_LEX_KW_ENTRY("while", 5, true), |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 191 | // This is an array that corresponds to token types. An entry is |
| 192 | // true if the token is valid in an expression, false otherwise. |
Gavin Howard | 9520b20 | 2018-09-28 12:04:14 -0600 | [diff] [blame] | 193 | const bool bc_parse_exprs[] = { |
Gavin Howard | 65a0c77 | 2018-09-28 12:50:18 -0600 | [diff] [blame] | 194 | false, false, true, true, true, true, true, true, true, true, true, true, |
| 195 | true, true, true, true, true, true, true, true, true, true, true, true, |
| 196 | true, true, true, false, false, true, true, false, false, false, false, |
| 197 | false, false, false, true, true, false, false, false, false, false, false, |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 198 | false, true, false, true, true, true, true, false, false, true, false, true, |
Gavin Howard | 65a0c77 | 2018-09-28 12:50:18 -0600 | [diff] [blame] | 199 | true, false, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | // This is an array of data for operators that correspond to token types. |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 203 | const BcOp bc_parse_ops[] = { |
Gavin Howard | 73527d2 | 2018-09-25 17:43:34 -0600 | [diff] [blame] | 204 | { 0, false }, { 0, false }, |
| 205 | { 1, false }, |
| 206 | { 2, false }, |
| 207 | { 3, true }, { 3, true }, { 3, true }, |
| 208 | { 4, true }, { 4, true }, |
| 209 | { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true }, |
| 210 | { 1, false }, |
| 211 | { 7, true }, { 7, true }, |
| 212 | { 5, false }, { 5, false }, { 5, false }, { 5, false }, { 5, false }, |
| 213 | { 5, false }, { 5, false }, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
Gavin Howard | 29204d6 | 2018-09-28 09:44:35 -0600 | [diff] [blame] | 216 | // These identify what tokens can come after expressions in certain cases. |
| 217 | const BcParseNext bc_parse_next_expr = |
| 218 | BC_PARSE_NEXT(3, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE); |
| 219 | const BcParseNext bc_parse_next_param = |
| 220 | BC_PARSE_NEXT(2, BC_LEX_RPAREN, BC_LEX_COMMA); |
| 221 | const BcParseNext bc_parse_next_print = |
| 222 | BC_PARSE_NEXT(3, BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON); |
| 223 | const BcParseNext bc_parse_next_cond = BC_PARSE_NEXT(1, BC_LEX_RPAREN); |
| 224 | const BcParseNext bc_parse_next_elem = BC_PARSE_NEXT(1, BC_LEX_RBRACKET); |
| 225 | const BcParseNext bc_parse_next_for = BC_PARSE_NEXT(1, BC_LEX_SCOLON); |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 226 | #endif // BC_ENABLED |
Gavin Howard | 29204d6 | 2018-09-28 09:44:35 -0600 | [diff] [blame] | 227 | |
| 228 | // This one is needed for dc too. |
| 229 | const BcParseNext bc_parse_next_read = BC_PARSE_NEXT(1, BC_LEX_NLINE); |
| 230 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 231 | #ifdef DC_ENABLED |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 232 | const uint8_t dc_inst_noperands[] = { |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 233 | 1, 1, 1, 1, 1, |
| 234 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 235 | 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 236 | 0, 0, 1, 0, 0, |
| 237 | 1, 0, 0, 0, 1, 0, 0, 1, |
| 238 | 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, |
| 239 | 3, 2, 1, 0, 0, 0, 1, 2, 0, 1, 0, 1, |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | const uint8_t dc_inst_nresults[] = { |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 243 | 1, 1, 1, 1, 1, |
| 244 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 245 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 246 | 1, 1, 1, 1, 0, |
| 247 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 248 | 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, |
| 249 | 1, 2, 0, 0, 0, 1, 2, 2, 1, 0, 0, 0, |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 250 | }; |
| 251 | |
Gavin Howard | 8412ba8 | 2018-10-04 13:03:24 -0600 | [diff] [blame] | 252 | const BcLexType dc_lex_regs[] = { |
| 253 | BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, |
| 254 | BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, |
| 255 | BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_STORE,BC_LEX_STORE_PUSH, |
| 256 | }; |
| 257 | |
| 258 | const size_t dc_lex_regs_len = sizeof(dc_lex_regs) / sizeof(BcLexType); |
| 259 | |
Gavin Howard | 2b9aa8a | 2018-09-28 12:49:41 -0600 | [diff] [blame] | 260 | const BcLexType dc_lex_tokens[] = { |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 261 | BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, |
Gavin Howard | c246baf | 2018-09-28 09:49:49 -0600 | [diff] [blame] | 262 | BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, |
| 263 | BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, |
| 264 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 265 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 266 | BC_LEX_INVALID, BC_LEX_INVALID, |
Gavin Howard | 8412ba8 | 2018-10-04 13:03:24 -0600 | [diff] [blame] | 267 | BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_INVALID, BC_LEX_INVALID, |
| 268 | BC_LEX_INVALID, BC_LEX_KEY_READ, BC_LEX_INVALID, BC_LEX_INVALID, |
| 269 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_ELSE, |
| 270 | BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID, BC_LEX_KEY_IBASE, |
| 271 | BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD, BC_LEX_INVALID, |
| 272 | BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_POP, BC_LEX_NQUIT, |
| 273 | BC_LEX_POP, BC_LEX_STORE, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 274 | BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID, BC_LEX_KEY_LENGTH, |
Gavin Howard | c246baf | 2018-09-28 09:49:49 -0600 | [diff] [blame] | 275 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_OP_POWER, |
Gavin Howard | 6252584 | 2018-10-01 12:41:50 -0600 | [diff] [blame] | 276 | BC_LEX_NEG, BC_LEX_INVALID, |
Gavin Howard | 3a714ae | 2018-09-29 19:56:47 -0600 | [diff] [blame] | 277 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, |
Gavin Howard | c246baf | 2018-09-28 09:49:49 -0600 | [diff] [blame] | 278 | BC_LEX_INVALID, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID, |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 279 | BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD_POP, |
Gavin Howard | 634b600 | 2018-09-28 19:52:17 -0600 | [diff] [blame] | 280 | BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT, |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 281 | BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, |
Gavin Howard | c246baf | 2018-09-28 09:49:49 -0600 | [diff] [blame] | 282 | BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, |
| 283 | BC_LEX_INVALID, BC_LEX_STACK_LEVEL, |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 284 | BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD, |
Gavin Howard | c246baf | 2018-09-28 09:49:49 -0600 | [diff] [blame] | 285 | BC_LEX_INVALID |
| 286 | }; |
Gavin Howard | 88cbe15 | 2018-09-28 14:03:57 -0600 | [diff] [blame] | 287 | |
| 288 | const BcInst dc_parse_insts[] = { |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 289 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
Gavin Howard | c079d55 | 2018-10-01 12:38:54 -0600 | [diff] [blame] | 290 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
Gavin Howard | 88cbe15 | 2018-09-28 14:03:57 -0600 | [diff] [blame] | 291 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 292 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 293 | BC_INST_INVALID, BC_INST_INVALID, |
Gavin Howard | 634b600 | 2018-09-28 19:52:17 -0600 | [diff] [blame] | 294 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
Gavin Howard | 88cbe15 | 2018-09-28 14:03:57 -0600 | [diff] [blame] | 295 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 296 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_ASSIGN, |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 297 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID, |
| 298 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 299 | BC_INST_INVALID, BC_INST_INVALID, |
Gavin Howard | 88cbe15 | 2018-09-28 14:03:57 -0600 | [diff] [blame] | 300 | BC_INST_STR, BC_INST_INVALID, BC_INST_INVALID, |
| 301 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 302 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 303 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
Gavin Howard | 1ce5ea9 | 2018-10-04 12:03:18 -0600 | [diff] [blame] | 304 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
Gavin Howard | 88cbe15 | 2018-09-28 14:03:57 -0600 | [diff] [blame] | 305 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
Gavin Howard | 8412ba8 | 2018-10-04 13:03:24 -0600 | [diff] [blame] | 306 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
Gavin Howard | 849e9bd | 2018-10-04 14:26:36 -0600 | [diff] [blame] | 307 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
Gavin Howard | 634b600 | 2018-09-28 19:52:17 -0600 | [diff] [blame] | 308 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
Gavin Howard | 88cbe15 | 2018-09-28 14:03:57 -0600 | [diff] [blame] | 309 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 310 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_PRINT, |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 311 | BC_INST_INVALID, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
Gavin Howard | 88cbe15 | 2018-09-28 14:03:57 -0600 | [diff] [blame] | 312 | }; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 313 | #endif // DC_ENABLED |
Gavin Howard | c246baf | 2018-09-28 09:49:49 -0600 | [diff] [blame] | 314 | |
Gavin Howard | 29204d6 | 2018-09-28 09:44:35 -0600 | [diff] [blame] | 315 | const char bc_num_hex_digits[] = "0123456789ABCDEF"; |
| 316 | |
Gavin Howard | ce8fb79 | 2018-09-04 15:08:36 -0600 | [diff] [blame] | 317 | const BcNumBinaryOp bc_program_ops[] = { |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 318 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_rem, bc_num_add, bc_num_sub, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
Gavin Howard | ff66e29 | 2018-03-28 12:47:58 -0600 | [diff] [blame] | 321 | const char bc_program_stdin_name[] = "<stdin>"; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 322 | const char bc_program_ready_msg[] = "ready for more input\n"; |