blob: 1b02e26fd649853031888e1b262b149b77ab654b [file] [log] [blame]
Gavin Howardf456d372018-03-10 20:11:41 -07001/*
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 Howardf456d372018-03-10 20:11:41 -070023#include <lex.h>
24#include <parse.h>
Gavin Howardd2a05252018-09-27 14:00:40 -060025#include <num.h>
Gavin Howardf456d372018-03-10 20:11:41 -070026
Gavin Howard29204d62018-09-28 09:44:35 -060027// ** Exclude start. **
Gavin Howard4ffe5a92018-09-26 20:58:31 -060028const char bc_name[] = "bc";
29const char dc_name[] = "dc";
Gavin Howard29204d62018-09-28 09:44:35 -060030// ** Exclude end. **
Gavin Howard4ffe5a92018-09-26 20:58:31 -060031
Gavin Howard86fb8d32018-09-25 18:00:06 -060032const char bc_header[] =
Gavin Howard73527d22018-09-25 17:43:34 -060033 "bc 1.0\n"
Gavin Howardcdcae372018-09-26 16:49:04 -060034 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Gavin Howard73527d22018-09-25 17:43:34 -060035 "Report bugs at: https://github.com/gavinhoward/bc\n\n"
Gavin Howard41044032018-09-27 10:49:51 -060036 "This is free software with ABSOLUTELY NO WARRANTY.\n";
Gavin Howardf456d372018-03-10 20:11:41 -070037
Gavin Howard79ce08b2018-06-21 16:21:55 -060038const char bc_err_fmt[] = "\n%s error: %s\n\n";
Gavin Howard60183832018-09-04 22:43:44 -060039const char bc_err_line[] = ":%d\n\n";
Gavin Howard79ce08b2018-06-21 16:21:55 -060040
41const char *bc_errs[] = {
Gavin Howard73527d22018-09-25 17:43:34 -060042 "VM",
43 "Lex",
44 "Parse",
45 "Math",
46 "Runtime",
47 "POSIX",
Gavin Howard9a65a4e2018-03-15 18:26:57 -060048};
Gavin Howardf456d372018-03-10 20:11:41 -070049
Gavin Howard79ce08b2018-06-21 16:21:55 -060050const uint8_t bc_err_indices[] = {
Gavin Howard73527d22018-09-25 17:43:34 -060051 BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM,
52 BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX,
53 BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
54 BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
55 BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH,
56 BC_ERR_IDX_MATH, BC_ERR_IDX_MATH,
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_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
60 BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
61 BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
62 BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
63 BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
64 BC_ERR_IDX_VEC, BC_ERR_IDX_VEC,
65 BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM,
Gavin Howardf456d372018-03-10 20:11:41 -070066};
67
68const char *bc_err_descs[] = {
Gavin Howard0790bca2018-09-22 19:44:35 -060069
Gavin Howard73527d22018-09-25 17:43:34 -060070 NULL,
71 "memory allocation error",
72 "I/O error",
73 "file is not text:",
Gavin Howarde77b83d2018-03-29 14:24:26 -060074
Gavin Howard73527d22018-09-25 17:43:34 -060075 "bad character",
76 "string end could not be found",
77 "comment end could not be found",
78 "end of file",
Gavin Howardf456d372018-03-10 20:11:41 -070079
Gavin Howard73527d22018-09-25 17:43:34 -060080 "bad token",
81 "bad expression",
Gavin Howard6d632e92018-09-28 11:26:23 -060082 "empty expression",
Gavin Howard73527d22018-09-25 17:43:34 -060083 "bad print statement",
84 "bad function definition",
85 "bad assignment: left side must be scale, ibase, "
86 "obase, last, var, or array element",
87 "no auto variable found",
88 "function parameter or auto var has the same name as another",
89 "block end could not be found",
Gavin Howardf456d372018-03-10 20:11:41 -070090
Gavin Howard73527d22018-09-25 17:43:34 -060091 "negative number",
92 "non integer number",
93 "overflow",
94 "divide by zero",
95 "negative square root",
96 "bad number string",
Gavin Howardf456d372018-03-10 20:11:41 -070097
Gavin Howard73527d22018-09-25 17:43:34 -060098 "could not open file:",
99 "mismatched parameters",
100 "undefined function",
101 "file is not executable:",
102 "could not install signal handler",
103 "bad scale; must be [0, BC_SCALE_MAX]",
104 "bad ibase; must be [2, 16]",
105 "bad obase; must be [2, BC_BASE_MAX]",
106 "number too long: must be [1, BC_NUM_MAX]",
107 "name too long: must be [1, BC_NAME_MAX]",
108 "string too long: must be [1, BC_STRING_MAX]",
109 "array too long; must be [1, BC_DIM_MAX]",
110 "bad read() expression",
111 "read() call inside of a read() call",
112 "variable is wrong type",
113 "signal caught",
Gavin Howardc935ff12018-09-29 02:18:45 -0600114#ifdef DC_CONFIG
115 "stack has too few elements",
116#endif // DC_CONFIG
Gavin Howardf456d372018-03-10 20:11:41 -0700117
Gavin Howard73527d22018-09-25 17:43:34 -0600118 "POSIX only allows one character names; the following is bad:",
119 "POSIX does not allow '#' script comments",
120 "POSIX does not allow the following keyword:",
121 "POSIX does not allow a period ('.') as a shortcut for the last result",
122 "POSIX requires parentheses around return expressions",
123 "POSIX does not allow boolean operators; the following is bad:",
124 "POSIX does not allow comparison operators outside if or loops",
125 "POSIX requires exactly one comparison operator per condition",
126 "POSIX does not allow an empty init expression in a for loop",
127 "POSIX does not allow an empty condition expression in a for loop",
128 "POSIX does not allow an empty update expression in a for loop",
129 "POSIX requires the left brace be on the same line as the function header",
Gavin Howarda0492142018-08-29 15:28:35 -0600130
Gavin Howard73527d22018-09-25 17:43:34 -0600131 "index is out of bounds",
132 "item already exists",
Gavin Howardc4fab672018-09-28 18:58:45 -0600133
Gavin Howard5f0cf2c2018-08-31 20:22:34 -0600134#ifndef NDEBUG
Gavin Howard73527d22018-09-25 17:43:34 -0600135 "quit request not honored",
136 "limits request not honored",
Gavin Howardc3a66802018-09-04 16:53:26 -0600137#endif // NDEBUG
Gavin Howard0790bca2018-09-22 19:44:35 -0600138
Gavin Howardf456d372018-03-10 20:11:41 -0700139};
140
Gavin Howard67598a72018-06-21 15:04:07 -0600141const char bc_sig_msg[34] = "\ninterrupt (type \"quit\" to exit)\n";
Gavin Howardf456d372018-03-10 20:11:41 -0700142
Gavin Howard3bc436f2018-09-28 14:02:34 -0600143const char bc_func_main[] = "(main)";
144const char bc_func_read[] = "(read)";
Gavin Howardff66e292018-03-28 12:47:58 -0600145
Gavin Howarde0eec662018-03-29 17:53:20 -0600146#ifndef NDEBUG
Gavin Howard3bc436f2018-09-28 14:02:34 -0600147const char bc_inst_chars[] =
Gavin Howard73527d22018-09-25 17:43:34 -0600148 "edED_^*/%+-=;?~<>!|&`{}@[],NVMACaI.LlrOqpQsSJjPR$H";
Gavin Howarde0eec662018-03-29 17:53:20 -0600149#endif // NDEBUG
Gavin Howard101b5e62018-03-26 07:46:27 -0600150
Gavin Howard634b6002018-09-28 19:52:17 -0600151#ifdef DC_CONFIG
152const uint8_t bc_inst_noperands[] = {
Gavin Howard88cbe152018-09-28 14:03:57 -0600153 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,
154 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
Gavin Howard634b6002018-09-28 19:52:17 -0600155 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 3, 2, 1, 1, 0, 0, 0, 1, 2,
Gavin Howard88cbe152018-09-28 14:03:57 -0600156};
157
Gavin Howard634b6002018-09-28 19:52:17 -0600158const uint8_t bc_inst_nresults[] = {
159 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
160 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
161 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 2, 0, 0, 0, 0, 1, 2, 2,
162};
163#endif // DC_CONFIG
164
Gavin Howard29204d62018-09-28 09:44:35 -0600165#ifdef BC_CONFIG
Gavin Howard63738202018-09-26 15:34:20 -0600166const BcLexKeyword bc_lex_kws[20] = {
Gavin Howard73527d22018-09-25 17:43:34 -0600167 BC_LEX_KW_ENTRY("auto", 4, true),
168 BC_LEX_KW_ENTRY("break", 5, true),
169 BC_LEX_KW_ENTRY("continue", 8, false),
170 BC_LEX_KW_ENTRY("define", 6, true),
171 BC_LEX_KW_ENTRY("else", 4, false),
172 BC_LEX_KW_ENTRY("for", 3, true),
173 BC_LEX_KW_ENTRY("halt", 4, false),
174 BC_LEX_KW_ENTRY("ibase", 5, true),
175 BC_LEX_KW_ENTRY("if", 2, true),
176 BC_LEX_KW_ENTRY("last", 4, false),
177 BC_LEX_KW_ENTRY("length", 6, true),
178 BC_LEX_KW_ENTRY("limits", 6, false),
179 BC_LEX_KW_ENTRY("obase", 5, true),
180 BC_LEX_KW_ENTRY("print", 5, false),
181 BC_LEX_KW_ENTRY("quit", 4, true),
182 BC_LEX_KW_ENTRY("read", 4, false),
183 BC_LEX_KW_ENTRY("return", 6, true),
184 BC_LEX_KW_ENTRY("scale", 5, true),
185 BC_LEX_KW_ENTRY("sqrt", 4, true),
186 BC_LEX_KW_ENTRY("while", 5, true),
Gavin Howardf456d372018-03-10 20:11:41 -0700187};
188
Gavin Howardf456d372018-03-10 20:11:41 -0700189// This is an array that corresponds to token types. An entry is
190// true if the token is valid in an expression, false otherwise.
Gavin Howard9520b202018-09-28 12:04:14 -0600191const bool bc_parse_exprs[] = {
Gavin Howard65a0c772018-09-28 12:50:18 -0600192 false, false, true, true, true, true, true, true, true, true, true, true,
193 true, true, true, true, true, true, true, true, true, true, true, true,
194 true, true, true, false, false, true, true, false, false, false, false,
195 false, false, false, true, true, false, false, false, false, false, false,
196 false, true, false, true,true, true, true, false, false, true, false, true,
197 true, false,
Gavin Howardf456d372018-03-10 20:11:41 -0700198};
199
200// This is an array of data for operators that correspond to token types.
Gavin Howardf456d372018-03-10 20:11:41 -0700201const BcOp bc_parse_ops[] = {
Gavin Howard73527d22018-09-25 17:43:34 -0600202 { 0, false }, { 0, false },
203 { 1, false },
204 { 2, false },
205 { 3, true }, { 3, true }, { 3, true },
206 { 4, true }, { 4, true },
207 { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true },
208 { 1, false },
209 { 7, true }, { 7, true },
210 { 5, false }, { 5, false }, { 5, false }, { 5, false }, { 5, false },
211 { 5, false }, { 5, false },
Gavin Howardf456d372018-03-10 20:11:41 -0700212};
213
Gavin Howard29204d62018-09-28 09:44:35 -0600214// These identify what tokens can come after expressions in certain cases.
215const BcParseNext bc_parse_next_expr =
216 BC_PARSE_NEXT(3, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE);
217const BcParseNext bc_parse_next_param =
218 BC_PARSE_NEXT(2, BC_LEX_RPAREN, BC_LEX_COMMA);
219const BcParseNext bc_parse_next_print =
220 BC_PARSE_NEXT(3, BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON);
221const BcParseNext bc_parse_next_cond = BC_PARSE_NEXT(1, BC_LEX_RPAREN);
222const BcParseNext bc_parse_next_elem = BC_PARSE_NEXT(1, BC_LEX_RBRACKET);
223const BcParseNext bc_parse_next_for = BC_PARSE_NEXT(1, BC_LEX_SCOLON);
224#endif // BC_CONFIG
225
226// This one is needed for dc too.
227const BcParseNext bc_parse_next_read = BC_PARSE_NEXT(1, BC_LEX_NLINE);
228
Gavin Howardc246baf2018-09-28 09:49:49 -0600229#ifdef DC_CONFIG
Gavin Howard2b9aa8a2018-09-28 12:49:41 -0600230const BcLexType dc_lex_tokens[] = {
Gavin Howardc246baf2018-09-28 09:49:49 -0600231 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_OP_REL_GT,
232 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
233 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
234 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
235 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
236 BC_LEX_INVALID, BC_LEX_INVALID,
237 BC_LEX_COLON, BC_LEX_COLON, BC_LEX_OP_REL_LT, BC_LEX_OP_REL_EQ,
238 BC_LEX_OP_REL_GT, BC_LEX_KEY_READ, BC_LEX_INVALID,
239 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
240 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_OP_REL_EQ, BC_LEX_INVALID,
241 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD,
Gavin Howard634b6002018-09-28 19:52:17 -0600242 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
Gavin Howardc246baf2018-09-28 09:49:49 -0600243 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE, BC_LEX_INVALID, BC_LEX_INVALID,
244 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
245 BC_LEX_KEY_LENGTH,
246 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_OP_POWER,
247 BC_LEX_OP_NEG, BC_LEX_INVALID,
248 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
249 BC_LEX_INVALID, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
Gavin Howardc935ff12018-09-29 02:18:45 -0600250 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD_POP,
Gavin Howard634b6002018-09-28 19:52:17 -0600251 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
Gavin Howardc935ff12018-09-29 02:18:45 -0600252 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_STORE_PUSH, BC_LEX_INVALID,
Gavin Howardc246baf2018-09-28 09:49:49 -0600253 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
254 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
255 BC_LEX_OP_REL_GE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
256 BC_LEX_INVALID
257};
Gavin Howard88cbe152018-09-28 14:03:57 -0600258
259const BcInst dc_parse_insts[] = {
260 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
261 BC_INST_NEG, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
262 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
263 BC_INST_REL_EQ, BC_INST_REL_LE, BC_INST_REL_GE, BC_INST_REL_NE,
264 BC_INST_REL_LT, BC_INST_REL_GT,
Gavin Howard634b6002018-09-28 19:52:17 -0600265 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
Gavin Howard88cbe152018-09-28 14:03:57 -0600266 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
267 BC_INST_INVALID, BC_INST_INVALID, BC_INST_ASSIGN,
268 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
269 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
270 BC_INST_ARRAY_ELEM, BC_INST_INVALID,
271 BC_INST_STR, BC_INST_INVALID, BC_INST_INVALID,
272 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
273 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
274 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
Gavin Howardc935ff12018-09-29 02:18:45 -0600275 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_READ,
Gavin Howard88cbe152018-09-28 14:03:57 -0600276 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
Gavin Howardc935ff12018-09-29 02:18:45 -0600277 BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, BC_INST_ASCIIFY,
278 BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
Gavin Howard634b6002018-09-28 19:52:17 -0600279 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
Gavin Howard88cbe152018-09-28 14:03:57 -0600280 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
281 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_PRINT,
Gavin Howardc935ff12018-09-29 02:18:45 -0600282 BC_INST_INVALID, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
Gavin Howard88cbe152018-09-28 14:03:57 -0600283};
Gavin Howardc246baf2018-09-28 09:49:49 -0600284#endif // DC_CONFIG
285
Gavin Howard29204d62018-09-28 09:44:35 -0600286const char bc_num_hex_digits[] = "0123456789ABCDEF";
287
Gavin Howardce8fb792018-09-04 15:08:36 -0600288const BcNumBinaryOp bc_program_ops[] = {
Gavin Howard73527d22018-09-25 17:43:34 -0600289 bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub,
Gavin Howardf456d372018-03-10 20:11:41 -0700290};
291
Gavin Howardff66e292018-03-28 12:47:58 -0600292const char bc_program_stdin_name[] = "<stdin>";
Gavin Howard63738202018-09-26 15:34:20 -0600293const char bc_program_ready_msg[] = "ready for more input\n";