-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSYSOBJ.PAS
298 lines (260 loc) · 7.91 KB
/
SYSOBJ.PAS
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{ SYSOBJ.PAS
Description:
Routines, types, and variables which comprise the "system" object
of Archetype.
}
unit sysobj;
interface
uses
misc, linklist, keywords, expr, wrap, intrptr,
parser, gamestat, heapsort;
procedure send_to_system(transport: shortint; var strmsg: string;
var result: result_type; var context: context_type);
implementation
type
sys_state_type = (IDLING,
INIT_SORTER, OPEN_SORTER, CLOSE_SORTER, NEXT_SORTED,
PLAYER_CMD, NORMALIZE, ABBR,
OPEN_PARSER, VERB_LIST, NOUN_LIST, CLOSE_PARSER,
INIT_PARSER, WHICH_OBJECT,
ROLL_CALL, PRESENT, PARSE, NEXT_OBJECT,
DEBUG_MESSAGES, DEBUG_EXPRESSIONS,
DEBUG_STATEMENTS,
DEBUG_MEMORY, FREE_MEMORY,
SAVE_STATE, LOAD_STATE);
const
StateLookup: array[IDLING..LOAD_STATE] of string =
('IDLING',
'INIT SORTER', 'OPEN SORTER', 'CLOSE SORTER',
'NEXT SORTED',
'PLAYER CMD', 'NORMALIZE', 'ABBR',
'OPEN PARSER', 'VERB LIST', 'NOUN LIST', 'CLOSE PARSER',
'INIT PARSER', 'WHICH OBJECT',
'ROLL CALL', 'PRESENT', 'PARSE', 'NEXT OBJECT',
'DEBUG MESSAGES', 'DEBUG EXPRESSIONS',
'DEBUG STATEMENTS',
'DEBUG MEMORY', 'FREE MEMORY',
'SAVE STATE', 'LOAD STATE');
{ Global variables which retain the state of the system object between
calls. }
var
sys_state : sys_state_type;
target_list : target_list_type;
function figure_state(var s : string) : boolean;
var st : sys_state_type;
begin
for st := IDLING to LOAD_STATE do
if StateLookup[st] = s then begin
sys_state := st;
figure_state := TRUE;
exit
end;
figure_state := FALSE
end;
{ send_to_system
Description:
Is the receiver of all "system calls" and the only object that receives
messages in the form of strings rather than message constants.
Notes:
Uses a global variable called sys_state to keep track of its state
between calls.
}
procedure send_to_system(transport: shortint; var strmsg: string;
var result: result_type; var context: context_type);
var
the_caller, the_message: integer;
obj_index: integer;
nomatch: string;
st: sys_state_type;
np: node_ptr;
p : pointer;
stfile: file;
begin
if transport = OP_SEND then
the_caller := context.self
else
the_caller := context.sender;
repeat
cleanup(result);
case sys_state of
IDLING: begin
if figure_state(strmsg) then
case sys_state of
PLAYER_CMD, ABBR, SAVE_STATE, LOAD_STATE,
OPEN_PARSER, OPEN_SORTER, WHICH_OBJECT:
exit; { come back again! }
INIT_SORTER: begin
reinit_heap;
sys_state := OPEN_SORTER;
exit
end;
INIT_PARSER: begin
new_parse_list;
sys_state := OPEN_PARSER;
exit
end;
end { case }
end;
PLAYER_CMD: begin
normalize_string(strmsg, Command);
sys_state := IDLING
end;
NORMALIZE: begin { last normalized command }
with result do begin
kind := STR_PTR;
acl_str := NewDynStr(Command)
end;
sys_state := IDLING
end;
ABBR: begin
with result do begin
kind := STR_PTR;
acl_str := NewDynStr(strmsg)
end;
if convert_to(NUMERIC, result) then
Abbreviate := result.acl_int
else begin
wraperr('Warning: non-numeric abbreviation message sent to system');
cleanup(result)
end;
sys_state := IDLING
end;
OPEN_PARSER: begin
if figure_state(strmsg) then
case sys_state of
CLOSE_PARSER:
sys_state := IDLING;
VERB_LIST: begin
target_list := PARSER_VERBLIST;
sys_state := OPEN_PARSER
end;
NOUN_LIST: begin
target_list := PARSER_NOUNLIST;
sys_state := OPEN_PARSER
end;
end { case }
else
add_parse_word(target_list, strmsg, the_caller);
exit
end;
OPEN_SORTER: begin
if figure_state(strmsg) then
case sys_state of
CLOSE_SORTER:
sys_state := IDLING;
end
else
drop_str_on_heap(strmsg);
exit
end;
NEXT_SORTED: begin
if not pop_heap(p) then
cleanup(result)
else with result do begin
kind := STR_PTR;
acl_str := p
end;
sys_state := IDLING
end;
WHICH_OBJECT: begin
obj_index := find_object(strmsg);
if obj_index <> 0 then
with result do begin
kind := IDENT;
ident_kind := OBJECT_ID;
ident_int := obj_index
end;
sys_state := IDLING
end;
ROLL_CALL: begin
dispose_list(Proximate);
new_list(Proximate);
sys_state := IDLING
end;
PRESENT: begin
new(np);
with np^ do begin
data := nil;
key := the_caller
end;
insert_item(Proximate, np);
sys_state := IDLING
end;
PARSE: begin
parse_sentence;
sys_state := IDLING
end;
NEXT_OBJECT: begin
if not pop_object(obj_index, nomatch) then
cleanup(result)
else if obj_index < 0 then
with result do begin
kind := STR_PTR;
acl_str := NewDynStr(nomatch)
end
else
with result do begin
kind := IDENT;
ident_kind := OBJECT_ID;
ident_int := obj_index
end;
sys_state := IDLING
end;
DEBUG_MESSAGES: begin
Debug := Debug XOR DEBUG_MSGS;
sys_state := IDLING
end;
DEBUG_EXPRESSIONS: begin
Debug := Debug XOR DEBUG_EXPR;
sys_state := IDLING
end;
DEBUG_STATEMENTS: begin
Debug := Debug XOR DEBUG_STMT;
sys_state := IDLING
end;
DEBUG_MEMORY: begin
wrapout('', TRUE); { get to beginning of line }
writeln('Maximum memory request: ', MaxAvail, ' bytes');
writeln('Actual free memory: ', MemAvail, ' bytes');
sys_state := IDLING
end;
FREE_MEMORY: begin
with result do begin
kind := NUMERIC;
acl_int := MemAvail
end;
sys_state := IDLING
end;
SAVE_STATE, LOAD_STATE: begin
assign(stfile, strmsg);
{$I-}
if sys_state = SAVE_STATE then
rewrite(stfile, 1)
else
reset(stfile, 1);
{$I+}
if IOResult <> 0 then begin
writeln('Error opening ', strmsg);
cleanup(result)
end
else begin
result.kind := RESERVED;
if sys_state = SAVE_STATE then begin
save_game_state(stfile, Object_List);
result.keyword := RW_TRUE
end
else if load_game_state(stfile, Object_List) then
result.keyword := RW_TRUE
else
result.keyword := RW_FALSE;
close(stfile)
end;
sys_state := IDLING
end;
end { case }
until sys_state = IDLING
end; { send_to_system }
begin
sys_state := IDLING
end. { unit sysobj }