aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/archetype/semantic.cpp
blob: c13b770e808ec572ac3d55f8e78fb4ccf2944f2a (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
/* 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/archetype/semantic.h"
#include "glk/archetype/archetype.h"
#include "glk/archetype/error.h"
#include "glk/archetype/id_table.h"

namespace Glk {
namespace Archetype {

typedef int *IntegerPtr;

int classify_as(progfile &f, int id_number, ClassifyType interpretation, void *ptr_to_data) {
	IdRecPtr the_id_ptr;
    String error_string;
	int result = 0;

	if (!index_ident(id_number, the_id_ptr)) {
		error_message(f, "Attempt to classify unencountered identifier");
	} else {
		//with the_id_ptr^ do begin
		if (the_id_ptr->id_kind == interpretation)
			result = the_id_ptr->id_integer;

		// If the existing id_kind is the DefaultClassification, we're allowed to
		// change it; otherwise there's a conflict
		else if (the_id_ptr->id_kind == DefaultClassification) {
			the_id_ptr->id_kind    = interpretation;
			the_id_ptr->id_integer = the_id_ptr->id_index;

			switch (the_id_ptr->id_kind) {
			case TYPE_ID:
				append_to_xarray(g_vm->Type_List, ptr_to_data);
				append_to_xarray(g_vm->Type_ID_List, (void *)the_id_ptr->id_name);
				the_id_ptr->id_integer = g_vm->Type_List.size();
				break;

			case OBJECT_ID:
				if (ptr_to_data == nullptr) {
					the_id_ptr->id_integer = 0;
				} else {
					// Object_List may have grown by unnamed objects between calls to classify_as.
					// Fill in the intervening spaces with "null"
					while (g_vm->Object_ID_List.size() < g_vm->Object_List.size())
						append_to_xarray(g_vm->Object_ID_List, (void *)g_vm->NullStr);

					append_to_xarray(g_vm->Object_List, ptr_to_data);
					append_to_xarray(g_vm->Object_ID_List, (void *)the_id_ptr->id_name);
					the_id_ptr->id_integer = g_vm->Object_List.size();
				}
				break;

			case ATTRIBUTE_ID:
				append_to_xarray(g_vm->Attribute_ID_List, (void *)the_id_ptr->id_name);
				the_id_ptr->id_integer = g_vm->Attribute_ID_List.size();
				break;

			default:
				break;
			}
		} else {
			error_string = String::format("Identifier type conflict: \"%s\" already declared as ",
				the_id_ptr->id_name->c_str());

			switch (the_id_ptr->id_kind) {
			case TYPE_ID:
				error_string = error_string + "a type";
				break;
			case OBJECT_ID:
				error_string = error_string + "an object";
				break;
			case ATTRIBUTE_ID:
				error_string = error_string + "an attribute";
				break;
			case ENUMERATE_ID:
				error_string = error_string + "a keyword";
				break;
			default:
				break;
			}

			error_message(f, error_string);
			the_id_ptr->id_integer = 0;
		}

		result = the_id_ptr->id_integer;
	}

	return result;
}

void get_meaning(int id_number, ClassifyType &meaning, int &number) {
	IdRecPtr the_id_ptr;

	if (!index_ident(id_number, the_id_ptr)) {
		error("Internal error:  attempt to find meaning of unencountered identifier");
	} else {
		meaning = the_id_ptr->id_kind;
		number = the_id_ptr->id_integer;
	}
}

void add_undefined(int the_ID) {
	NodePtr np;
	IntegerPtr ip;

	np = find_item(g_vm->Overlooked, the_ID);
	if (np != nullptr) {
		++*((IntegerPtr)np->data);
	} else {
		np = new NodeType();
		np->key = the_ID;
		ip = new int();
		*ip = 1;			// TODO: Should this be 0-based?
		np->data = ip;
		insert_item(g_vm->Overlooked, np);
	}
}

bool display_undefined() {
	NodePtr np = nullptr;
	IntegerPtr ip;
	IdRecPtr id_rec;
	bool exists = false;

	while (iterate_list(g_vm->Overlooked, np)) {
		if (!exists) {
			debugN("The following identifiers were not explicitly defined.");
			exists = true;
		}

		ip = (IntegerPtr)np->data;
		debugN("Used %d", *ip);
		if (*ip == 1)
			debugN(" time:   ");
		else
			debugN(" times:  ");

		if (index_ident(np->key, id_rec))
			debug("%s", id_rec->id_name->c_str());
		else
			debug("<unknown identifier>");

		delete ip;
	}

	dispose_list(g_vm->Overlooked);

	return exists;
}

bool verify_expr(progfile &f, ExprTree the_expr) {
	bool success = true;

	switch (the_expr->_kind) {
	case OPER:
		switch (the_expr->_data._oper.op_name) {
		case OP_DOT:
			if (the_expr->_data._oper.right->_kind != IDENT) {
				error_message(f, "Right side of dot must be an identifier");
				success = false;
			}
			else if (the_expr->_data._oper.right->_data._ident.ident_kind != ATTRIBUTE_ID) {
				the_expr->_data._oper.right->_data._ident.ident_int = classify_as(f,
					the_expr->_data._oper.right->_data._ident.ident_int, ATTRIBUTE_ID, nullptr);
			}

			the_expr->_data._oper.right->_data._ident.ident_kind = ATTRIBUTE_ID;
			if (the_expr->_data._oper.right->_data._ident.ident_int == 0)
				success = false;
			// fallthrough
			// FIXME: is this fallthrough intentional?

		case OP_ASSIGN:
		case OP_C_CONCAT:
		case OP_C_MULTIPLY:
		case OP_C_DIVIDE:
		case OP_C_PLUS:
		case OP_C_MINUS:
			if (the_expr->_data._oper.left->_kind == IDENT) {
				get_meaning(the_expr->_data._oper.left->_data._ident.ident_int,
					the_expr->_data._oper.left->_data._ident.ident_kind, the_expr->_data._oper.left->_data._ident.ident_int);

				if (the_expr->_data._oper.left->_data._ident.ident_kind != ATTRIBUTE_ID) {
					error_message(f, "Left side of assignment is not an attribute");
					success = false;
				}
			}
			else if (!(the_expr->_data._oper.left->_kind == OPER &&
				the_expr->_data._oper.left->_data._oper.op_name == OP_DOT)) {
				error_message(f, "Left side of assignment must reference an attribute");
				success = false;
			}
			break;

		default:
			break;
		}

		if (success) {
			if (Binary[the_expr->_data._oper.op_name])
				success = verify_expr(f, the_expr->_data._oper.left);
		}
		if (success)
			success = verify_expr(f, the_expr->_data._oper.right);
		break;

	default:
		break;
	}

	return success;
}

} // End of namespace Archetype
} // End of namespace Glk