Debian Codegraph @

terps/alan3/scan.c

raw symbols SCIP
1 /*----------------------------------------------------------------------
2
3 scan.c
4
5 Player command line scanne unit for Alan interpreter ARUN
6
7 ----------------------------------------------------------------------*/
8
9 #include "scan.h"
10
11
12 /* IMPORTS */
13 #include "lists.h"
14 #include "dictionary.h"
15 #include "output.h"
16 #include "memory.h"
17 #include "params.h"
18 #include "term.h"
19 #include "options.h"
20 #include "exe.h"
21 #include "word.h"
22 #include "literal.h"
23 #include "debug.h"
24 #include "msg.h"
25 #include "inter.h"
26 #include "converter.h"
27
28
29 #ifdef USE_READLINE
30 #include "readline.h"
31 #endif
32
33
34
35 /* PRIVATE CONSTANTS */
36
37
38 /* PUBLIC DATA */
39 bool continued = false;
40
41
42 /* PRIVATE DATA */
43 static char input_buffer[1000]; /* The input buffer */
44 static bool eol = true; /* Looking at End of line? Yes, initially */
45 static char *token = NULL;
46
47
48 /*======================================================================*/
49 void forceNewPlayerInput() {
50 setEndOfArray(&playerWords[currentWordIndex]);
51 }
52
53
54 /*----------------------------------------------------------------------*/
55 static void unknown(char token[]) {
56 Parameter *messageParameters = newParameterArray();
57
58 addParameterForString(messageParameters, token);
59 printMessageWithParameters(M_UNKNOWN_WORD, messageParameters);
60 deallocate(messageParameters);
61 abortPlayerCommand();
62 }
63
64
65 /*----------------------------------------------------------------------*/
66 static int number(char token[]) {
67 int i;
68
69 sscanf(token, "%d", &i);
70 return i;
71 }
72
73
74 /*----------------------------------------------------------------------*/
75 static int lookup(char wrd[]) {
76 int i;
77
78 // TODO: Why do we start at 0, is there a word code == 0?
79 for (i = 0; !isEndOfArray(&dictionary[i]); i++) {
80 if (equalStrings(wrd, (char *) pointerTo(dictionary[i].string))) {
81 return i;
82 }
83 }
84 return EOF;
85 }
86
87
88 /*----------------------------------------------------------------------*/
89 static bool isWordCharacter(int ch) {
90 return isLetter(ch) || isdigit(ch) || ch == '\'' || ch == '-' || ch == '_';
91 }
92
93 /*----------------------------------------------------------------------*/
94 static void readWord(char **markerp) {
95 while (**markerp && isWordCharacter(**markerp))
96 (*markerp)++;
97 }
98
99 /*----------------------------------------------------------------------*/
100 static void readNumber(char **markerp) {
101 while (isdigit((int)**markerp))
102 (*markerp)++;
103 }
104
105 /*----------------------------------------------------------------------*/
106 static void readString(char **markerp) {
107 (*markerp)++;
108 while (**markerp != '\"')
109 (*markerp)++;
110 (*markerp)++;
111 }
112
113
114 /*----------------------------------------------------------------------*/
115 static char *gettoken(char *buf) {
116 static char *marker;
117 static char oldch;
118
119 if (buf == NULL)
120 *marker = oldch;
121 else
122 marker = buf;
123 while (*marker != '\0' && isSpace(*marker) && *marker != '\n')
124 marker++;
125 buf = marker;
126 if (isLetter(*marker))
127 readWord(&marker);
128 else if (isdigit((int)*marker))
129 readNumber(&marker);
130 else if (*marker == '\"') {
131 readString(&marker);
132 } else if (*marker == '\0' || *marker == '\n' || *marker == ';')
133 // End of input, either actually or by a comment start
134 return NULL;
135 else
136 marker++;
137 oldch = *marker;
138 *marker = '\0';
139 return buf;
140 }
141
142
143 static void printPrompt() {
144 if (header->prompt) {
145 anyOutput = false;
146 interpret(header->prompt);
147 if (anyOutput)
148 printAndLog(" ");
149 needSpace = false;
150 } else
151 printAndLog("> ");
152 }
153
154
155 /*----------------------------------------------------------------------*/
156 // TODO replace dependency to exe.c with injection of quitGame() and undo()
157 static void getLine(void) {
158 para();
159 do {
160 statusline();
161 printPrompt();
162
163 #ifdef USE_READLINE
164 if (!readline(input_buffer))
165 #else
166 fflush(stdout);
167 if (fgets(buf, LISTLEN, stdin) == NULL)
168 #endif
169 {
170 newline();
171 quitGame();
172 }
173
174 getPageSize();
175 anyOutput = false;
176 if (commandLogOption || transcriptOption) {
177 /* Command log ("solution") needs to be in external encoding even for GLK terps */
178 char *converted = ensureExternalEncoding(input_buffer);
179 if (commandLogOption) {
180 #ifdef HAVE_GLK
181 glk_put_string_stream(commandLogFile, converted);
182 glk_put_char_stream(commandLogFile, '\n');
183 #else
184 fprintf(commandLogFile, "%s\n", converted);
185 fflush(commandLogFile);
186 #endif
187 }
188 if (transcriptOption) {
189 #ifdef HAVE_GLK
190 glk_put_string_stream(transcriptFile, converted);
191 glk_put_char_stream(transcriptFile, '\n');
192 #else
193 fprintf(transcriptFile, "%s\n", converted);
194 fflush(commandLogFile);
195 #endif
196 }
197 free(converted);
198 }
199
200 /* If the player inputs an empty command he forfeited his command */
201 if (strlen(input_buffer) == 0) {
202 clearWordList(playerWords);
203 longjmp(forfeitLabel, 0);
204 }
205
206 token = gettoken(input_buffer);
207 if (token != NULL) {
208 if (strcmp("debug", token) == 0 && header->debug) {
209 debugOption = true;
210 debug(false, 0, 0);
211 token = NULL;
212 } else if (strcmp("undo", token) == 0) {
213 token = gettoken(NULL);
214 if (token != NULL) /* More tokens? */
215 error(M_WHAT);
216 undo();
217 }
218 }
219 } while (token == NULL);
220 eol = false;
221 }
222
223
224 /*----------------------------------------------------------------------*/
225 static int try_elision(char *apostrophe, int *_i) {
226 int i = *_i, w;
227 // Handle elisions (contractions) with apostrophe,
228 // e.g. Italian "l'acqua", and split that into two
229 // words
230
231 // First cut after the apostrophe
232 int previous_char = apostrophe[1];
233 apostrophe[1] = '\0';
234
235 // Now try that word
236 w = lookup(token);
237 apostrophe[1] = previous_char;
238 if (w == EOF) {
239 // No cigar, so give up
240 unknown(token);
241 }
242
243 // We found a word, save it
244 if (!isNoise(w))
245 playerWords[i++].code = w;
246 // Then restore and point to next part
247 token = &apostrophe[1];
248 w = lookup(token);
249 if (w == EOF) {
250 // No cigar, so give up
251 unknown(token);
252 }
253
254 // Prepare to store this word
255 ensureSpaceForPlayerWords(i+1);
256 playerWords[i].start = token;
257 playerWords[i].end = strchr(token, '\0');
258
259 *_i = i;
260 return w;
261 }
262
263
264 /*----------------------------------------------------------------------*/
265 static int handle_literal(int i) {
266 if (isdigit((int)token[0])) {
267 createIntegerLiteral(number(token));
268 } else {
269 char *unquotedString = strdup(token);
270 unquotedString[strlen(token) - 1] = '\0';
271 createStringLiteral(&unquotedString[1]);
272 free(unquotedString);
273 }
274 playerWords[i++].code = dictionarySize + litCount; /* Word outside dictionary = literal */
275 return i;
276 }
277
278
279 /*----------------------------------------------------------------------*/
280 static int handle_word(int i) {
281 int w = lookup(token);
282 if (w == EOF) {
283 char *apostrophe = strchr(token, '\'');
284 if (apostrophe == NULL) {
285 unknown(token);
286 } else {
287 w = try_elision(apostrophe, &i);
288 }
289 }
290 if (!isNoise(w))
291 playerWords[i++].code = w;
292 return(i);
293 }
294
295
296 /*======================================================================*/
297 void scan(void) {
298 int i;
299
300 if (continued) {
301 /* Player used '.' to separate commands. Read next */
302 para();
303 token = gettoken(NULL);
304 if (token == NULL) /* Or did he just finish the command with a full stop? */
305 getLine();
306 continued = false;
307 } else
308 getLine();
309
310 freeLiterals();
311 playerWords[0].code = 0; // TODO This means what?
312 i = 0;
313 do {
314 ensureSpaceForPlayerWords(i+1);
315 playerWords[i].start = token;
316 playerWords[i].end = strchr(token, '\0');
317
318 if (isLetter(token[0])) {
319 i = handle_word(i);
320 } else if (isdigit((int)token[0]) || token[0] == '\"') {
321 i = handle_literal(i);
322 } else if (token[0] == ',') {
323 playerWords[i++].code = conjWord;
324 } else if (token[0] == '.') {
325 continued = true;
326 setEndOfArray(&playerWords[i]);
327 eol = true;
328 break;
329 } else
330 unknown(token);
331 setEndOfArray(&playerWords[i]);
332 eol = (token = gettoken(NULL)) == NULL;
333 } while (!eol);
334 }