aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/scan.cpp
blob: dcfcc6f6031837bf0917a4211077ae2f869b81b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/alan3/scan.h"
#include "glk/alan3/lists.h"
#include "glk/alan3/dictionary.h"
#include "glk/alan3/debug.h"
#include "glk/alan3/exe.h"
#include "glk/alan3/glkio.h"
#include "glk/alan3/inter.h"
#include "glk/alan3/literal.h"
#include "glk/alan3/memory.h"
#include "glk/alan3/msg.h"
#include "glk/alan3/options.h"
#include "glk/alan3/output.h"
#include "glk/alan3/params.h"
#include "glk/alan3/readline.h"
#include "glk/alan3/syserr.h"
#include "glk/alan3/term.h"
#include "glk/alan3/word.h"

namespace Glk {
namespace Alan3 {

/* PUBLIC DATA */
bool continued = FALSE;


/* PRIVATE DATA */
static char buf[1000]; /* The input buffer */
static char isobuf[1000]; /* The input buffer in ISO */
static bool eol = TRUE; /* Looking at End of line? Yes, initially */
static char *token = NULL;


/*======================================================================*/
void forceNewPlayerInput() {
	setEndOfArray(&playerWords[currentWordIndex]);
}


/*----------------------------------------------------------------------*/
static void unknown(char tok[]) {
	char *str = strdup(tok);
	Parameter *messageParameters = newParameterArray();

	addParameterForString(messageParameters, str);
	printMessageWithParameters(M_UNKNOWN_WORD, messageParameters);
	deallocate(messageParameters);
	free(str);
	abortPlayerCommand();
}


/*----------------------------------------------------------------------*/
static int number(char tok[]) {
	int i;

	(void)sscanf(tok, "%d", &i);
	return i;
}


/*----------------------------------------------------------------------*/
static int lookup(char wrd[]) {
	int i;

	for (i = 0; !isEndOfArray(&dictionary[i]); i++) {
		if (compareStrings(wrd, (char *) pointerTo(dictionary[i].string)) == 0) {
			return (i);
		}
	}
	unknown(wrd);
	return (int)EOD;
}


/*----------------------------------------------------------------------*/
static bool isWordCharacter(int ch) {
	return isISOLetter(ch) || isdigit(ch) || ch == '\'' || ch == '-' || ch == '_';
}

/*----------------------------------------------------------------------*/
static char *gettoken(char *txtBuf) {
	static char *marker;
	static char oldch;

	if (txtBuf == NULL)
		*marker = oldch;
	else
		marker = txtBuf;
	while (*marker != '\0' && isSpace(*marker) && *marker != '\n')
		marker++;
	txtBuf = marker;
	if (isISOLetter(*marker))
		while (*marker && isWordCharacter(*marker))
			marker++;
	else if (isdigit((int)*marker))
		while (isdigit((int)*marker))
			marker++;
	else if (*marker == '\"') {
		marker++;
		while (*marker != '\"')
			marker++;
		marker++;
	} else if (*marker == '\0' || *marker == '\n' || *marker == ';')
		return NULL;
	else
		marker++;
	oldch = *marker;
	*marker = '\0';
	return txtBuf;
}


/*----------------------------------------------------------------------*/
// TODO replace dependency to exe.c with injection of quitGame() and undo()
static void getLine(void) {
	para();
	do {
		statusline();
		if (header->prompt) {
			anyOutput = FALSE;
			interpret(header->prompt);
			if (anyOutput)
				printAndLog(" ");
			needSpace = FALSE;
		} else
			printAndLog("> ");

		if (!readline(buf)) {
			newline();
			quitGame();
		}

		getPageSize();
		anyOutput = FALSE;
		if (transcriptOption || logOption) {
			// TODO: Refactor out the logging to log.c?
			g_vm->glk_put_string_stream(logFile, buf);
			g_vm->glk_put_char_stream(logFile, '\n');
		}
		/* If the player input an empty command he forfeited his command */
#ifdef TODO
		if (strlen(buf) == 0) {
			clearWordList(playerWords);
			longjmp(forfeitLabel, 0);
		}
#else
		syserr("TODO: empty command");
#endif

		strcpy(isobuf, buf);
		token = gettoken(isobuf);
		if (token != NULL) {
			if (strcmp("debug", token) == 0 && header->debug) {
				debugOption = TRUE;
				debug(FALSE, 0, 0);
				token = NULL;
			} else if (strcmp("undo", token) == 0) {
				token = gettoken(NULL);
				if (token != NULL) /* More tokens? */
					error(M_WHAT);
				undo();
			}
		}
	} while (token == NULL);
	eol = FALSE;
}



/*======================================================================*/
void scan(void) {
	int i;
	int w;

	if (continued) {
		/* Player used '.' to separate commands. Read next */
		para();
		token = gettoken(NULL); /* Or did he just finish the command with a full stop? */
		if (token == NULL)
			getLine();
		continued = FALSE;
	} else
		getLine();

	freeLiterals();
	playerWords[0].code = 0; // TODO This means what?
	i = 0;
	do {
		ensureSpaceForPlayerWords(i + 1);
		playerWords[i].start = token;
		playerWords[i].end = strchr(token, '\0');
		if (isISOLetter(token[0])) {
			w = lookup(token);
			if (!isNoise(w))
				playerWords[i++].code = w;
		} else if (isdigit((int)token[0]) || token[0] == '\"') {
			if (isdigit((int)token[0])) {
				createIntegerLiteral(number(token));
			} else {
				char *unquotedString = strdup(token);
				unquotedString[strlen(token) - 1] = '\0';
				createStringLiteral(&unquotedString[1]);
				free(unquotedString);
			}
			playerWords[i++].code = dictionarySize + litCount; /* Word outside dictionary = literal */
		} else if (token[0] == ',') {
			playerWords[i++].code = conjWord;
		} else if (token[0] == '.') {
			continued = TRUE;
			setEndOfArray(&playerWords[i]);
			eol = TRUE;
			break;
		} else
			unknown(token);
		setEndOfArray(&playerWords[i]);
		eol = (token = gettoken(NULL)) == NULL;
	} while (!eol);
}

} // End of namespace Alan3
} // End of namespace Glk