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