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 | 8f4b207 | 2018-08-02 22:41:29 -0600 | [diff] [blame] | 23 | #include <bc.h> |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 24 | #include <status.h> |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 25 | #include <lex.h> |
| 26 | #include <parse.h> |
| 27 | |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 28 | const char bc_header[] = |
Gavin Howard | fb4d4d0 | 2018-07-05 18:36:21 -0600 | [diff] [blame] | 29 | "bc 1.0\n" |
Gavin Howard | 9d8285d | 2018-03-12 11:56:15 -0600 | [diff] [blame] | 30 | "bc copyright (c) 2018 Gavin D. Howard and contributors\n" |
Gavin Howard | a280bde | 2018-03-13 13:49:12 -0600 | [diff] [blame] | 31 | "Report bugs at: https://github.com/gavinhoward/bc\n\n" |
| 32 | "This is free software with ABSOLUTELY NO WARRANTY.\n\n"; |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 33 | |
Gavin Howard | 79ce08b | 2018-06-21 16:21:55 -0600 | [diff] [blame] | 34 | const char bc_err_fmt[] = "\n%s error: %s\n\n"; |
Gavin Howard | 6018383 | 2018-09-04 22:43:44 -0600 | [diff] [blame] | 35 | const char bc_err_line[] = ":%d\n\n"; |
Gavin Howard | 79ce08b | 2018-06-21 16:21:55 -0600 | [diff] [blame] | 36 | |
| 37 | const char *bc_errs[] = { |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 38 | "bc", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 39 | "Lex", |
Gavin Howard | 9a65a4e | 2018-03-15 18:26:57 -0600 | [diff] [blame] | 40 | "Parse", |
| 41 | "Math", |
| 42 | "Runtime", |
| 43 | "POSIX", |
Gavin Howard | 9a65a4e | 2018-03-15 18:26:57 -0600 | [diff] [blame] | 44 | }; |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 45 | |
Gavin Howard | 79ce08b | 2018-06-21 16:21:55 -0600 | [diff] [blame] | 46 | const uint8_t bc_err_indices[] = { |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 47 | BC_ERR_IDX_BC, BC_ERR_IDX_BC, BC_ERR_IDX_BC, BC_ERR_IDX_BC, |
| 48 | BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, |
| 49 | BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, |
Gavin Howard | bd9b292 | 2018-09-07 16:31:48 -0600 | [diff] [blame] | 50 | BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 51 | BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, |
| 52 | BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, |
| 53 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
| 54 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
| 55 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
Gavin Howard | cf7882d | 2018-09-10 15:45:13 -0600 | [diff] [blame] | 56 | BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 57 | BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, |
| 58 | BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, |
| 59 | BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, |
Gavin Howard | a049214 | 2018-08-29 15:28:35 -0600 | [diff] [blame] | 60 | BC_ERR_IDX_VEC, BC_ERR_IDX_VEC, |
| 61 | BC_ERR_IDX_BC, BC_ERR_IDX_BC, BC_ERR_IDX_BC, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | const char *bc_err_descs[] = { |
Gavin Howard | 0790bca | 2018-09-22 19:44:35 -0600 | [diff] [blame^] | 65 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 66 | NULL, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 67 | "memory allocation error", |
| 68 | "I/O error", |
Gavin Howard | 9697ea8 | 2018-09-20 11:15:37 -0600 | [diff] [blame] | 69 | "file is not text:", |
Gavin Howard | e77b83d | 2018-03-29 14:24:26 -0600 | [diff] [blame] | 70 | |
| 71 | "bad character", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 72 | "string end could not be found", |
| 73 | "comment end could not be found", |
| 74 | "end of file", |
| 75 | |
Gavin Howard | bdb2f92 | 2018-03-15 09:25:18 -0600 | [diff] [blame] | 76 | "bad token", |
| 77 | "bad expression", |
| 78 | "bad print statement", |
| 79 | "bad function definition", |
| 80 | "bad assignment: left must be scale, ibase, " |
| 81 | "obase, last, var, or array element", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 82 | "no auto variable found", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 83 | "function parameter or auto var has the same name as another", |
Gavin Howard | bd9b292 | 2018-09-07 16:31:48 -0600 | [diff] [blame] | 84 | "block end could not be found", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 85 | |
| 86 | "negative number", |
| 87 | "non integer number", |
| 88 | "overflow", |
| 89 | "divide by zero", |
| 90 | "negative square root", |
Gavin Howard | bdb2f92 | 2018-03-15 09:25:18 -0600 | [diff] [blame] | 91 | "bad number string", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 92 | |
Gavin Howard | 9697ea8 | 2018-09-20 11:15:37 -0600 | [diff] [blame] | 93 | "could not open file:", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 94 | "mismatched parameters", |
| 95 | "undefined function", |
Gavin Howard | 9697ea8 | 2018-09-20 11:15:37 -0600 | [diff] [blame] | 96 | "file is not executable:", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 97 | "could not install signal handler", |
Gavin Howard | 293aec9 | 2018-03-13 12:29:18 -0600 | [diff] [blame] | 98 | "bad scale; must be [0, BC_SCALE_MAX]", |
| 99 | "bad ibase; must be [2, 16]", |
| 100 | "bad obase; must be [2, BC_BASE_MAX]", |
Gavin Howard | cf7882d | 2018-09-10 15:45:13 -0600 | [diff] [blame] | 101 | "number too long: must be [1, BC_NUM_MAX]", |
| 102 | "name too long: must be [1, BC_NAME_MAX]", |
Gavin Howard | bdb2f92 | 2018-03-15 09:25:18 -0600 | [diff] [blame] | 103 | "string too long: must be [1, BC_STRING_MAX]", |
| 104 | "array too long; must be [1, BC_DIM_MAX]", |
| 105 | "bad read() expression", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 106 | "read() call inside of a read() call", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 107 | "variable is wrong type", |
Gavin Howard | a049214 | 2018-08-29 15:28:35 -0600 | [diff] [blame] | 108 | "signal caught", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 109 | |
Gavin Howard | bdb2f92 | 2018-03-15 09:25:18 -0600 | [diff] [blame] | 110 | "POSIX only allows one character names; the following is bad:", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 111 | "POSIX does not allow '#' script comments", |
| 112 | "POSIX does not allow the following keyword:", |
| 113 | "POSIX does not allow a period ('.') as a shortcut for the last result", |
| 114 | "POSIX requires parentheses around return expressions", |
Gavin Howard | bdb2f92 | 2018-03-15 09:25:18 -0600 | [diff] [blame] | 115 | "POSIX does not allow boolean operators; the following is bad:", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 116 | "POSIX does not allow comparison operators outside if or loops", |
Gavin Howard | 5aff504 | 2018-03-23 08:08:19 -0600 | [diff] [blame] | 117 | "POSIX requires exactly one comparison operator per condition", |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 118 | "POSIX does not allow an empty init expression in a for loop", |
| 119 | "POSIX does not allow an empty condition expression in a for loop", |
| 120 | "POSIX does not allow an empty update expression in a for loop", |
| 121 | "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] | 122 | |
| 123 | "index is out of bounds", |
| 124 | "item already exists", |
Gavin Howard | 5f0cf2c | 2018-08-31 20:22:34 -0600 | [diff] [blame] | 125 | #ifndef NDEBUG |
Gavin Howard | a049214 | 2018-08-29 15:28:35 -0600 | [diff] [blame] | 126 | "quit request not honored", |
| 127 | "limits request not honored", |
Gavin Howard | c3a6680 | 2018-09-04 16:53:26 -0600 | [diff] [blame] | 128 | #endif // NDEBUG |
Gavin Howard | 0790bca | 2018-09-22 19:44:35 -0600 | [diff] [blame^] | 129 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Gavin Howard | 67598a7 | 2018-06-21 15:04:07 -0600 | [diff] [blame] | 132 | const char bc_sig_msg[34] = "\ninterrupt (type \"quit\" to exit)\n"; |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 133 | |
Gavin Howard | ff66e29 | 2018-03-28 12:47:58 -0600 | [diff] [blame] | 134 | const char bc_lang_func_main[7] = "(main)"; |
| 135 | const char bc_lang_func_read[7] = "(read)"; |
| 136 | |
Gavin Howard | e0eec66 | 2018-03-29 17:53:20 -0600 | [diff] [blame] | 137 | #ifndef NDEBUG |
Gavin Howard | ff66e29 | 2018-03-28 12:47:58 -0600 | [diff] [blame] | 138 | const char bc_lang_inst_chars[] = |
Gavin Howard | 375263e | 2018-09-04 15:10:18 -0600 | [diff] [blame] | 139 | "edED_^*/%+-=;?~<>!|&`{}@[],NVMACaI.LlrOqpQsSJjPR$H"; |
Gavin Howard | e0eec66 | 2018-03-29 17:53:20 -0600 | [diff] [blame] | 140 | #endif // NDEBUG |
Gavin Howard | 101b5e6 | 2018-03-26 07:46:27 -0600 | [diff] [blame] | 141 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 142 | const BcLexKeyword bc_lex_keywords[20] = { |
Gavin Howard | 665a993 | 2018-03-26 13:04:58 -0600 | [diff] [blame] | 143 | BC_LEX_KW_ENTRY("auto", 4, true), |
| 144 | BC_LEX_KW_ENTRY("break", 5, true), |
| 145 | BC_LEX_KW_ENTRY("continue", 8, false), |
| 146 | BC_LEX_KW_ENTRY("define", 6, true), |
| 147 | BC_LEX_KW_ENTRY("else", 4, false), |
| 148 | BC_LEX_KW_ENTRY("for", 3, true), |
| 149 | BC_LEX_KW_ENTRY("halt", 4, false), |
| 150 | BC_LEX_KW_ENTRY("ibase", 5, true), |
| 151 | BC_LEX_KW_ENTRY("if", 2, true), |
| 152 | BC_LEX_KW_ENTRY("last", 4, false), |
| 153 | BC_LEX_KW_ENTRY("length", 6, true), |
| 154 | BC_LEX_KW_ENTRY("limits", 6, false), |
| 155 | BC_LEX_KW_ENTRY("obase", 5, true), |
| 156 | BC_LEX_KW_ENTRY("print", 5, false), |
| 157 | BC_LEX_KW_ENTRY("quit", 4, true), |
| 158 | BC_LEX_KW_ENTRY("read", 4, false), |
| 159 | BC_LEX_KW_ENTRY("return", 6, true), |
| 160 | BC_LEX_KW_ENTRY("scale", 5, true), |
| 161 | BC_LEX_KW_ENTRY("sqrt", 4, true), |
| 162 | BC_LEX_KW_ENTRY("while", 5, true), |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | const char bc_num_hex_digits[] = "0123456789ABCDEF"; |
| 166 | |
| 167 | // This is an array that corresponds to token types. An entry is |
| 168 | // true if the token is valid in an expression, false otherwise. |
| 169 | const bool bc_parse_token_exprs[] = { |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 170 | true, true, true, true, true, true, true, true, true, true, true, true, true, |
Gavin Howard | 5cc6581 | 2018-06-21 15:08:45 -0600 | [diff] [blame] | 171 | true, true, true, true, true, true, true, true, true, true, true, true, false, |
| 172 | false, true, true, false, false, false, false, false, false, false, true, |
| 173 | true, false, false, false, false, false, false, false, true, false, true, |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 174 | true, true, true, false, false, true, false, true, true, false, false, false, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Gavin Howard | e56e918 | 2018-09-12 17:22:09 -0600 | [diff] [blame] | 177 | // These are to identify what tokens can |
| 178 | // come after expressions in certain cases. |
Gavin Howard | 79be14a | 2018-09-12 16:39:27 -0600 | [diff] [blame] | 179 | const BcNext bc_parse_next_expr = |
| 180 | BC_PARSE_NEXT(3, BC_LEX_NEWLINE, BC_LEX_SEMICOLON, BC_LEX_RIGHT_BRACE); |
| 181 | const BcNext bc_parse_next_param = |
| 182 | BC_PARSE_NEXT(2, BC_LEX_RIGHT_PAREN, BC_LEX_COMMA); |
| 183 | const BcNext bc_parse_next_print = |
| 184 | BC_PARSE_NEXT(3, BC_LEX_COMMA, BC_LEX_NEWLINE, BC_LEX_SEMICOLON); |
| 185 | const BcNext bc_parse_next_cond = BC_PARSE_NEXT(1, BC_LEX_RIGHT_PAREN); |
| 186 | const BcNext bc_parse_next_elem = BC_PARSE_NEXT(1, BC_LEX_RIGHT_BRACKET); |
| 187 | const BcNext bc_parse_next_for = BC_PARSE_NEXT(1, BC_LEX_SEMICOLON); |
Gavin Howard | 0749948 | 2018-09-14 08:58:59 -0600 | [diff] [blame] | 188 | const BcNext bc_parse_next_read = BC_PARSE_NEXT(1, BC_LEX_NEWLINE); |
Gavin Howard | 79be14a | 2018-09-12 16:39:27 -0600 | [diff] [blame] | 189 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 190 | // 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] | 191 | const BcOp bc_parse_ops[] = { |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 192 | { 0, false }, { 0, false }, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 193 | { 1, false }, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 194 | { 2, false }, |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 195 | { 3, true }, { 3, true }, { 3, true }, |
| 196 | { 4, true }, { 4, true }, |
| 197 | { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true }, |
Gavin Howard | 2444f27 | 2018-09-11 12:25:53 -0600 | [diff] [blame] | 198 | { 1, false }, |
| 199 | { 7, true }, { 7, true }, |
Gavin Howard | 39b6408 | 2018-09-02 00:10:18 -0600 | [diff] [blame] | 200 | { 5, false }, { 5, false }, { 5, false }, { 5, false }, { 5, false }, |
| 201 | { 5, false }, { 5, false }, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Gavin Howard | ce8fb79 | 2018-09-04 15:08:36 -0600 | [diff] [blame] | 204 | const BcNumBinaryOp bc_program_ops[] = { |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 205 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
Gavin Howard | ff66e29 | 2018-03-28 12:47:58 -0600 | [diff] [blame] | 208 | const char bc_program_stdin_name[] = "<stdin>"; |
Gavin Howard | 67598a7 | 2018-06-21 15:04:07 -0600 | [diff] [blame] | 209 | const char bc_program_ready_prompt[] = "ready for more input\n"; |