blob: 418c6fdb3ecc1929a464f36a45f03c866385149c [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 Howard8f4b2072018-08-02 22:41:29 -060023#include <bc.h>
Gavin Howard29493062018-03-20 19:57:37 -060024#include <status.h>
Gavin Howardf456d372018-03-10 20:11:41 -070025#include <lex.h>
26#include <parse.h>
27
Gavin Howard86fb8d32018-09-25 18:00:06 -060028const char bc_header[] =
Gavin Howard73527d22018-09-25 17:43:34 -060029 "bc 1.0\n"
30 "bc copyright (c) 2018 Gavin D. Howard and contributors\n"
31 "Report bugs at: https://github.com/gavinhoward/bc\n\n"
32 "This is free software with ABSOLUTELY NO WARRANTY.\n\n";
Gavin Howardf456d372018-03-10 20:11:41 -070033
Gavin Howard79ce08b2018-06-21 16:21:55 -060034const char bc_err_fmt[] = "\n%s error: %s\n\n";
Gavin Howard60183832018-09-04 22:43:44 -060035const char bc_err_line[] = ":%d\n\n";
Gavin Howard79ce08b2018-06-21 16:21:55 -060036
37const char *bc_errs[] = {
Gavin Howard73527d22018-09-25 17:43:34 -060038 "VM",
39 "Lex",
40 "Parse",
41 "Math",
42 "Runtime",
43 "POSIX",
Gavin Howard9a65a4e2018-03-15 18:26:57 -060044};
Gavin Howardf456d372018-03-10 20:11:41 -070045
Gavin Howard79ce08b2018-06-21 16:21:55 -060046const uint8_t bc_err_indices[] = {
Gavin Howard73527d22018-09-25 17:43:34 -060047 BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM,
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,
50 BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
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,
56 BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
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,
60 BC_ERR_IDX_VEC, BC_ERR_IDX_VEC,
61 BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM,
Gavin Howardf456d372018-03-10 20:11:41 -070062};
63
64const char *bc_err_descs[] = {
Gavin Howard0790bca2018-09-22 19:44:35 -060065
Gavin Howard73527d22018-09-25 17:43:34 -060066 NULL,
67 "memory allocation error",
68 "I/O error",
69 "file is not text:",
Gavin Howarde77b83d2018-03-29 14:24:26 -060070
Gavin Howard73527d22018-09-25 17:43:34 -060071 "bad character",
72 "string end could not be found",
73 "comment end could not be found",
74 "end of file",
Gavin Howardf456d372018-03-10 20:11:41 -070075
Gavin Howard73527d22018-09-25 17:43:34 -060076 "bad token",
77 "bad expression",
78 "bad print statement",
79 "bad function definition",
80 "bad assignment: left side must be scale, ibase, "
81 "obase, last, var, or array element",
82 "no auto variable found",
83 "function parameter or auto var has the same name as another",
84 "block end could not be found",
Gavin Howardf456d372018-03-10 20:11:41 -070085
Gavin Howard73527d22018-09-25 17:43:34 -060086 "negative number",
87 "non integer number",
88 "overflow",
89 "divide by zero",
90 "negative square root",
91 "bad number string",
Gavin Howardf456d372018-03-10 20:11:41 -070092
Gavin Howard73527d22018-09-25 17:43:34 -060093 "could not open file:",
94 "mismatched parameters",
95 "undefined function",
96 "file is not executable:",
97 "could not install signal handler",
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]",
101 "number too long: must be [1, BC_NUM_MAX]",
102 "name too long: must be [1, BC_NAME_MAX]",
103 "string too long: must be [1, BC_STRING_MAX]",
104 "array too long; must be [1, BC_DIM_MAX]",
105 "bad read() expression",
106 "read() call inside of a read() call",
107 "variable is wrong type",
108 "signal caught",
Gavin Howardf456d372018-03-10 20:11:41 -0700109
Gavin Howard73527d22018-09-25 17:43:34 -0600110 "POSIX only allows one character names; the following is bad:",
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",
115 "POSIX does not allow boolean operators; the following is bad:",
116 "POSIX does not allow comparison operators outside if or loops",
117 "POSIX requires exactly one comparison operator per condition",
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 Howarda0492142018-08-29 15:28:35 -0600122
Gavin Howard73527d22018-09-25 17:43:34 -0600123 "index is out of bounds",
124 "item already exists",
Gavin Howard5f0cf2c2018-08-31 20:22:34 -0600125#ifndef NDEBUG
Gavin Howard73527d22018-09-25 17:43:34 -0600126 "quit request not honored",
127 "limits request not honored",
Gavin Howardc3a66802018-09-04 16:53:26 -0600128#endif // NDEBUG
Gavin Howard0790bca2018-09-22 19:44:35 -0600129
Gavin Howardf456d372018-03-10 20:11:41 -0700130};
131
Gavin Howard67598a72018-06-21 15:04:07 -0600132const char bc_sig_msg[34] = "\ninterrupt (type \"quit\" to exit)\n";
Gavin Howardf456d372018-03-10 20:11:41 -0700133
Gavin Howardf4a74ee2018-09-24 15:32:52 -0600134const char bc_lang_func_main[] = "(main)";
135const char bc_lang_func_read[] = "(read)";
Gavin Howardff66e292018-03-28 12:47:58 -0600136
Gavin Howarde0eec662018-03-29 17:53:20 -0600137#ifndef NDEBUG
Gavin Howardff66e292018-03-28 12:47:58 -0600138const char bc_lang_inst_chars[] =
Gavin Howard73527d22018-09-25 17:43:34 -0600139 "edED_^*/%+-=;?~<>!|&`{}@[],NVMACaI.LlrOqpQsSJjPR$H";
Gavin Howarde0eec662018-03-29 17:53:20 -0600140#endif // NDEBUG
Gavin Howard101b5e62018-03-26 07:46:27 -0600141
Gavin Howard63738202018-09-26 15:34:20 -0600142const BcLexKeyword bc_lex_kws[20] = {
Gavin Howard73527d22018-09-25 17:43:34 -0600143 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 Howardf456d372018-03-10 20:11:41 -0700163};
164
165const 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.
169const bool bc_parse_token_exprs[] = {
Gavin Howard73527d22018-09-25 17:43:34 -0600170 true, true, true, true, true, true, true, true, true, true, true, true, true,
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,
174 true, true, true, false, false, true, false, true, true, false, false, false,
Gavin Howardf456d372018-03-10 20:11:41 -0700175};
176
Gavin Howarde56e9182018-09-12 17:22:09 -0600177// These are to identify what tokens can
178// come after expressions in certain cases.
Gavin Howard4c2ec1b2018-09-24 19:57:40 -0600179const BcParseNext bc_parse_next_expr =
Gavin Howard63738202018-09-26 15:34:20 -0600180 BC_PARSE_NEXT(3, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE);
Gavin Howard4c2ec1b2018-09-24 19:57:40 -0600181const BcParseNext bc_parse_next_param =
Gavin Howard63738202018-09-26 15:34:20 -0600182 BC_PARSE_NEXT(2, BC_LEX_RPAREN, BC_LEX_COMMA);
Gavin Howard4c2ec1b2018-09-24 19:57:40 -0600183const BcParseNext bc_parse_next_print =
Gavin Howard63738202018-09-26 15:34:20 -0600184 BC_PARSE_NEXT(3, BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON);
185const BcParseNext bc_parse_next_cond = BC_PARSE_NEXT(1, BC_LEX_RPAREN);
186const BcParseNext bc_parse_next_elem = BC_PARSE_NEXT(1, BC_LEX_RBRACKET);
187const BcParseNext bc_parse_next_for = BC_PARSE_NEXT(1, BC_LEX_SCOLON);
188const BcParseNext bc_parse_next_read = BC_PARSE_NEXT(1, BC_LEX_NLINE);
Gavin Howard79be14a2018-09-12 16:39:27 -0600189
Gavin Howardf456d372018-03-10 20:11:41 -0700190// This is an array of data for operators that correspond to token types.
Gavin Howardf456d372018-03-10 20:11:41 -0700191const BcOp bc_parse_ops[] = {
Gavin Howard73527d22018-09-25 17:43:34 -0600192 { 0, false }, { 0, false },
193 { 1, false },
194 { 2, false },
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 },
198 { 1, false },
199 { 7, true }, { 7, true },
200 { 5, false }, { 5, false }, { 5, false }, { 5, false }, { 5, false },
201 { 5, false }, { 5, false },
Gavin Howardf456d372018-03-10 20:11:41 -0700202};
203
Gavin Howardce8fb792018-09-04 15:08:36 -0600204const BcNumBinaryOp bc_program_ops[] = {
Gavin Howard73527d22018-09-25 17:43:34 -0600205 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 -0700206};
207
Gavin Howardff66e292018-03-28 12:47:58 -0600208const char bc_program_stdin_name[] = "<stdin>";
Gavin Howard63738202018-09-26 15:34:20 -0600209const char bc_program_ready_msg[] = "ready for more input\n";