Debian Codegraph @

terps/bocfel/dict.cpp

raw symbols SCIP
1 // Copyright 2009-2023 Chris Spiegel.
2 //
3 // SPDX-License-Identifier: MIT
4
5 #include <array>
6 #include <cstdlib>
7 #include <cstring>
8 #include <vector>
9
10 #include "dict.h"
11 #include "memory.h"
12 #include "options.h"
13 #include "process.h"
14 #include "types.h"
15 #include "unicode.h"
16 #include "util.h"
17 #include "zterp.h"
18
19 struct Dictionary {
20 explicit Dictionary(uint16_t addr) :
21 m_addr(addr),
22 m_num_separators(user_byte(m_addr)),
23 m_entry_length(user_byte(m_addr + m_num_separators + 1)),
24 m_num_entries(as_signed(user_word(m_addr + m_num_separators + 2))),
25 m_base(m_addr + 1 + m_num_separators + 1 + 2) {
26 ZASSERT(m_entry_length >= (zversion <= 3 ? 4 : 6), "dictionary entry length (%d) too small", m_entry_length);
27 ZASSERT(m_base + (labs(m_num_entries) * m_entry_length) < memory_size, "reported dictionary length extends beyond memory size");
28
29 m_separators[ZSCII_SPACE] = true;
30 for (uint8_t i = 0; i < m_num_separators; i++) {
31 m_separators[byte(m_addr + 1 + i)] = true;
32 }
33 }
34
35 uint16_t find(const uint8_t *token, size_t len) const;
36
37 bool is_sep(uint8_t c) const {
38 return m_separators[c];
39 };
40
41 private:
42 uint16_t m_addr;
43 uint8_t m_num_separators;
44 std::array<bool, UINT8_MAX + 1> m_separators{};
45 uint8_t m_entry_length;
46 long m_num_entries;
47 uint16_t m_base;
48 };
49
50 // Encode the text at “s”, of length “len” (there is not necessarily a
51 // terminating null character), returning it.
52 //
53 // 1.1 of the standard revises the encoding for V1 and V2 games. I am
54 // not implementing the new rules for two basic reasons:
55 // 1) It apparently only affects three (unnecessary) dictionary words in
56 // the known V1-2 games.
57 // 2) Because of 1, it is not worth the effort to peek ahead and see
58 // what the next character is to determine whether to shift once or
59 // to lock.
60 //
61 // Z-character 0 is a space (§3.5.1), so theoretically a space should be
62 // encoded simply with a zero. However, Inform 6.32 encodes space
63 // (which has the value 32) as a 10-bit ZSCII code, which is the
64 // Z-characters 5, 6, 1, 0. Assume this is correct.
65 static std::array<uint8_t, 6> encode_string(const uint8_t *s, size_t len)
66 {
67 int n = 0;
68 const int max = zversion <= 3 ? 6 : 9;
69 const int shiftbase = zversion <= 2 ? 1 : 3;
70 std::array<uint8_t, 12> chars;
71
72 for (size_t i = 0; i < len && n < max; i++) {
73 int pos = atable_pos[s[i]];
74 if (pos >= 0) {
75 int shift = pos / 26;
76 int c = pos % 26;
77
78 if (shift > 0) {
79 chars[n++] = shiftbase + shift;
80 }
81 chars[n++] = c + 6;
82 } else {
83 chars[n++] = shiftbase + 2;
84 chars[n++] = 6;
85 chars[n++] = s[i] >> 5;
86 chars[n++] = s[i] & 0x1f;
87 }
88 }
89
90 while (n < max) {
91 chars[n++] = 5;
92 }
93
94 std::array<uint8_t, 6> encoded;
95 auto p = encoded.begin();
96
97 // §3.2:
98 // --first byte------- --second byte---
99 // 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
100 // bit --first-- --second--- --third--
101 for (int i = 0; i < max; i += 3) {
102 *p++ = (chars[i] << 2) | (chars[i + 1] >> 3);
103 *p++ = ((chars[i + 1] & 0x7) << 5) | chars[i + 2];
104 }
105
106 // §3.2: the MSB of the last encoded word must be set.
107 if (zversion <= 3) {
108 encoded[2] |= 0x80;
109 } else {
110 encoded[4] |= 0x80;
111 }
112
113 return encoded;
114 }
115
116 uint16_t Dictionary::find(const uint8_t *token, size_t len) const {
117 const uint8_t *ret = nullptr;
118 auto dict_compar = [](const void *a, const void *b) {
119 return std::memcmp(a, b, zversion <= 3 ? 4 : 6);
120 };
121
122 auto encoded = encode_string(token, len);
123
124 if (m_num_entries > 0) {
125 ret = static_cast<uint8_t *>(std::bsearch(encoded.data(), &memory[m_base], m_num_entries, m_entry_length, dict_compar));
126 } else {
127 for (long i = 0; i < -m_num_entries; i++) {
128 const uint8_t *entry = &memory[m_base + (i * m_entry_length)];
129
130 if (dict_compar(encoded.data(), entry) == 0) {
131 ret = entry;
132 break;
133 }
134 }
135 }
136
137 if (ret == nullptr) {
138 return 0;
139 }
140
141 return m_base + (ret - &memory[m_base]);
142 }
143
144 static uint16_t lookup_replacement(uint16_t original, const std::vector<uint8_t> &replacement, const Dictionary &dictionary)
145 {
146 uint16_t d = dictionary.find(replacement.data(), replacement.size());
147
148 if (d == 0) {
149 return original;
150 }
151
152 return d;
153 }
154
155 static void handle_token(const uint8_t *base, const uint8_t *token, size_t len, uint16_t parse, const Dictionary &dictionary, int found, bool ignore_unknown, bool start_of_sentence)
156 {
157 uint16_t d;
158
159 d = dictionary.find(token, len);
160
161 if (len == 1 && is_game(Game::Infocom1234) && start_of_sentence && !options.disable_abbreviations) {
162 const std::vector<uint8_t> examine = { 'e', 'x', 'a', 'm', 'i', 'n', 'e' };
163 const std::vector<uint8_t> again = { 'a', 'g', 'a', 'i', 'n' };
164 const std::vector<uint8_t> wait = { 'w', 'a', 'i', 't' };
165 const std::vector<uint8_t> oops = { 'o', 'o', 'p', 's' };
166
167 if (*token == 'x') {
168 d = lookup_replacement(d, examine, dictionary);
169 } else if (*token == 'g') {
170 d = lookup_replacement(d, again, dictionary);
171 } else if (*token == 'z') {
172 d = lookup_replacement(d, wait, dictionary);
173 } else if (*token == 'o') {
174 d = lookup_replacement(d, oops, dictionary);
175 }
176 }
177
178 if (ignore_unknown && d == 0) {
179 return;
180 }
181
182 parse = parse + 2 + (found * 4);
183
184 user_store_word(parse, d);
185
186 user_store_byte(parse + 2, len);
187
188 if (zversion <= 4) {
189 user_store_byte(parse + 3, token - base + 1);
190 } else {
191 user_store_byte(parse + 3, token - base + 2);
192 }
193 }
194
195 // The behavior of tokenize is described in §15 (under the read opcode)
196 // and §13.
197 //
198 // For the text buffer, byte 0 is ignored in both V3/4 and V5+.
199 // Byte 1 of V3/4 is the start of the string, while in V5+ it is the
200 // length of the string.
201 // Byte 2 of V5+ is the start of the string. V3/4 strings have a null
202 // terminator, while V5+ do not.
203 //
204 // For the parse buffer, byte 0 contains the maximum number of tokens
205 // that can be read.
206 // The number of tokens found is stored in byte 1.
207 // Each token is then represented by a 4-byte chunk with the following
208 // information:
209 // • The first two bytes are the byte address of the dictionary entry
210 // for the token, or 0 if the token was not found in the dictionary.
211 // • The next byte is the length of the token.
212 // • The final byte is the offset in the string of the token.
213 void tokenize(uint16_t text, uint16_t parse, uint16_t dictaddr, bool ignore_unknown)
214 {
215 const uint8_t *p;
216 uint32_t text_len = 0;
217 const int maxwords = user_byte(parse);
218 bool in_word = false;
219 int found = 0;
220 bool start_of_sentence = true;
221
222 if (dictaddr == 0) {
223 dictaddr = header.dictionary;
224 }
225
226 ZASSERT(dictaddr != 0, "attempt to tokenize without a valid dictionary");
227
228 Dictionary dictionary(dictaddr);
229
230 if (zversion >= 5) {
231 text_len = user_byte(text + 1);
232 } else {
233 while (user_byte(text + 1 + text_len) != 0) {
234 text_len++;
235 }
236 }
237
238 ZASSERT(text + 1 + (zversion >= 5) + text_len < memory_size, "attempt to tokenize out-of-bounds string");
239
240 const uint8_t *string = &memory[text + 1 + (zversion >= 5 ? 1 : 0)];
241
242 for (p = string; p - string < text_len && *p == ZSCII_SPACE; p++) {
243 }
244 const uint8_t *lastp = p;
245
246 text_len -= (p - string);
247
248 do {
249 if (!in_word && text_len != 0 && !dictionary.is_sep(*p)) {
250 in_word = true;
251 lastp = p;
252 }
253
254 if (text_len == 0 || dictionary.is_sep(*p)) {
255 if (in_word && found < maxwords) {
256 handle_token(string, lastp, p - lastp, parse, dictionary, found++, ignore_unknown, start_of_sentence);
257 start_of_sentence = false;
258 }
259
260 // §13.6.1: Separators (apart from a space) are tokens too.
261 if (text_len != 0 && *p != ZSCII_SPACE && found < maxwords) {
262 handle_token(string, p, 1, parse, dictionary, found++, ignore_unknown, start_of_sentence);
263 start_of_sentence = *p == ZSCII_PERIOD;
264 }
265
266 in_word = false;
267 }
268
269 p++;
270
271 } while (text_len-- > 0 && found < maxwords);
272
273 user_store_byte(parse + 1, found);
274 }
275
276 static void encode_text(uint32_t text, uint16_t len, uint16_t coded)
277 {
278 ZASSERT(text + len < memory_size, "reported text length extends beyond memory size");
279
280 auto encoded = encode_string(&memory[text], len);
281
282 for (int i = 0; i < 6; i++) {
283 user_store_byte(coded + i, encoded[i]);
284 }
285 }
286
287 void ztokenise()
288 {
289 if (znargs < 3) {
290 zargs[2] = 0;
291 }
292 if (znargs < 4) {
293 zargs[3] = 0;
294 }
295
296 tokenize(zargs[0], zargs[1], zargs[2], zargs[3] != 0);
297 }
298
299 void zencode_text()
300 {
301 encode_text(zargs[0] + zargs[2], zargs[1], zargs[3]);
302 }