forked from EricEve/adv3lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.t
611 lines (533 loc) · 15.7 KB
/
debug.t
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#charset "us-ascii"
#include "advlite.h"
/*
* ************************************************************************
* debug.t This module forms part of the adv3Lite library, and defines a
* number of commands that can be used for debugging purposes.
*
* (c) 2012-13 Eric Eve (but based partly on code borrowed from the Mercury
* library (c) Michael J. Roberts).
*
*
*/
/* We only include any of the code in this module in debug builds */
#ifdef __DEBUG
DebugCtl: object
/*
* Debug mode options. Each debug function has an associated ID key,
* which is just a string identifying it. This is a lookup table
* that keeps a true/nil value for each key, saying whether the
* function is enabled or disabled. This lets the developer turn
* debugging displays on and off individually, so that you don't have
* to look at piles of debug output not relevant to the task you're
* currently working on.
*/
enabled = static (new LookupTable(32, 64))
/* list of all debugging options */
all = ['spelling', 'messages', 'actions', 'doers']
/* show the current status */
status()
{
"Debugging options:\n";
local opts = all.sort(SortAsc);
foreach (local opt in opts)
"\t<<opt>> = <<enabled[opt] ? 'on' : 'off'>>\n";
}
/* LookupTable used to avoid duplicate debug message reports */
messageIDs = static (new LookupTable(32, 64))
;
/*
* Debug options. This is the general verb for performing various
* debugging operations while running the game. The Debug Action parses
* the options string to carry out the command.
*/
VerbRule(Debug)
'debug' literalDobj
: VerbProduction
action = Debug
verbPhrase = 'debug/debugging'
missingQ = 'which debug option do you want to set'
;
/* The Debug Action with various options */
DefineSystemAction(Debug)
execAction(cmd)
{
gLiteral = cmd.dobj.name.toLower;
switch(gLiteral)
{
case 'status':
DebugCtl.status();
break;
case 'off':
case 'stop':
foreach(local opt in DebugCtl.all)
DebugCtl.enabled[opt] = nil;
DebugCtl.status();
break;
default:
if(DebugCtl.all.indexOf(gLiteral))
{
DebugCtl.enabled[gLiteral] = !DebugCtl.enabled[gLiteral];
DebugCtl.status();
}
else
"That is not a valid option. The valid DEBUG options are DEBUG
MESSAGES, DEBUG SPELLING, DEBUG ACTIONS, DEBUG DOERS,
DEBUG OFF or DEBUG STOP (to turn off all options) or
just DEBUG by itself to break into the debugger. ";
break;
}
}
;
/* DEBUG without any options simply breaks into the debugger, as in adv3 */
DefineSystemAction(DebugI)
execAction(cmd)
{
/* if the debugger is present, break into it */
if (t3DebugTrace(T3DebugCheck))
t3DebugTrace(T3DebugBreak);
else
DMsg(debugger not present, 'Debugger not present. ');
}
;
VerbRule(DebugI)
'debug'
: VerbProduction
action = DebugI
verbPhrase = 'debug/debugging'
missingQ = 'which debug option do you want to set'
;
/*
* The actionTab object holds a table providing the names (as strings)
* corresponding to the various Action objects, for use with the DEBUG ACTIONS
* option.
*/
actionTab: PreinitObject
/*
* To return the string val corresponding to the Action val, simply look
* it up in out ctab table
*/
symbolToVal(val)
{
return ctab[val];
}
/* A LookupTable of Actions and their corresponding string names */
ctab = [* -> '???']
execute()
{
/*
* Populate our ctab table by going through the global symbol table at
* preinit and storing the value and associated name of every Action
* object.
*/
t3GetGlobalSymbols().forEachAssoc( new function(key, value)
{
if(dataType(value) == TypeObject && value.ofKind(Action))
ctab[value] = key;
});
}
;
/*
* The Purloin Action allows a game author to take any object in the game
* while testing
*/
DefineTAction(Purloin)
againRepeatsParse = true
/* The PURLOIN action requires universal scope */
addExtraScopeItems(whichRole?)
{
makeScopeUniversal();
}
beforeAction() { }
afterAction() { }
turnSequence() { }
/* The Purloin action should work even on a hidden item */
unhides = true
;
/*
* The GONEAR action allows the game author to move the player character to
* anywhere on the map, while testing.
*/
DefineTAction(GoNear)
againRepeatsParse = true
/* The GONEAR action requires universal scope */
addExtraScopeItems(whichRole?)
{
makeScopeUniversal();
}
beforeAction() { }
afterAction() { }
turnSequence() { }
/* The GoNear action should work even on a hidden item */
unhides = true
;
/*
* The FIAT LUX Action can be used to light up the player character (thus
* bringing light to a dark location). Repeating the FIAT LUX action removes
* the light from the player character
*/
DefineIAction(FiatLux)
execAction(cmd)
{
gPlayerChar.isLit = !gPlayerChar.isLit;
DMsg(fiat lux, '{I} suddenly {1} glowing. ', gPlayerChar.isLit ? 'start'
: 'stop' );
}
beforeAction() { }
turnSequence() { }
;
/* The EVALUATE action allows any expression to be evaluated */
DefineLiteralAction(Evaluate)
exec(cmd)
{
try
{
/*
* Try using the Compiler object to evaluate the expression
* contained in the name property of the direct object of this
* command (i.e. the string literal it was executed upon).
*/
local res = Compiler.eval(stripQuotesFrom(cmd.dobj.name));
/* Display a string version of the result */
say(toString(res));
}
/*
* If the attempt to evaluate the expression caused a compiler error,
* display the exception message.
*/
catch (CompilerException cex)
{
cex.displayException();
}
/*
* If the attempt to evaluate the expression caused any other kind of
* error, display the exception message.
*/
catch (Exception ex)
{
ex.displayException();
}
}
includeInUndo = true
afterAction() {}
beforeAction() { }
turnSequence() { }
;
/* An object to store class and object names */
symTab: PreinitObject
symbolToVal(val)
{
return ctab[val];
}
ctab = [* -> '???']
/*
* Store a string equivalent of the name of every identifier defined in the
* game (and the library)
*/
execute()
{
t3GetGlobalSymbols().forEachAssoc( new function(key, value)
{
// if(dataType(value) == TypeObject && value.isClass)
// ctab[value] = key;
//
// if(dataType(value) == TypeObject && (value.ofKind(Region)))
// ctab[value] = key;
//
// if(defined(Actor) && dataType(value) == TypeObject &&
// (value.ofKind(ActorState) || value.ofKind(AgendaItem)))
// ctab[value] = key;
ctab[value] = key;
});
}
;
/* Take a string and return the object whose programmatic name it refers to */
symToVal(val)
{
return t3GetGlobalSymbols()[val];
}
/* Take a value and return the string representation of its programmatic name */
valToSym(val)
{
local str;
switch(dataType(val))
{
case TypeSString:
return val;
case TypeInt:
return toString(val);
case TypeObject:
case TypeEnum:
case TypeProp:
return symTab.ctab[val];
case TypeNil:
return 'nil';
case TypeTrue:
return 'true';
case TypeList:
str = '[';
for(local cur in val, local i=1, local len=val.length;; i++)
{
str += valToSym(cur);
if(i < len)
str += ', ';
}
str += ']';
return str;
}
return '?';
}
/*
* Provide TadsObject with an objToString() method so that the EVALUATE
* command can display some kind of name of the object via the toString()
* function
*/
modify TadsObject
objToString()
{
/* If this object is a class, return the name of the class */
if(isClass)
return symTab.symbolToVal(self);
local str = '';
/*
* If the object has a name property, start the string with this
* object's name
*/
if(name != nil)
str = name + ' ';
/*
* Otherwise if we have an identifier for this object stored in our
* symbol table, use that
*/
else if(symTab.symbolToVal(self) != '???')
str = symTab.symbolToVal(self) + ' ';
/* Append this object's superclass list in parentheses*/
str += '(' + getSuperclassList + ')';
/* Return the result */
return str;
}
;
/*
* Adaptation for use with adv3Lite of the tests extension based on work by
* Ben Cressy, Eric Eve, and N.R.Turner
*/
/*
* To use this facility, define Test objects like so:
*
*. foo: Test
*. testName = 'foo'
*. testList =
*. [
*. 'x me',
*. 'i'
*. ]
*. ;
*
*. bar: Test
*. testName = 'bar'
*. testList =
*. [
*. 'look',
*. 'listen'
*. ]
*. ;
*
*. allTests: Test
*. testName = 'all'
*. testList =
*. [
*. 'test foo',
*. 'test bar'
*. ]
* ;
*
* Alternatively, use the template structure to create your test objects more
* conveniently:
*
*. someTest: Test 'foo' ['x me', 'i'];
*
* Unless you're planning to refer to the Test object in some other part of
* your code, you can save a bit of typing by making it an anonymous object:
*
*. Test 'foo' ['x me', 'i'];
*
*/
/*
* The 'list tests' and 'list tests fully' commands can be used to list your
* test scripts from within the running game.
*/
DefineSystemAction(ListTests)
execAction(cmd)
{
if(allTests.lst.length == 0)
{
DMsg(no test scripts, 'There are no test scripts defined in this
game. ');
exit;
}
fully = cmd.verbProd.fully;
foreach(local testObj in allTests.lst)
{
"<<testObj.testName>>";
if(gAction.fully)
{
": ";
foreach(local txt in testObj.testList)
"<<txt>>/";
}
"\n";
}
}
fully = nil
;
VerbRule(ListTests)
('list' | 'l') 'tests' (| 'fully' -> fully)
: VerbProduction
action = ListTests
verbPhrase = 'list/listing test scripts'
;
/*
* The 'test X' command can be used with any Test object defined in the source
* code:
*/
DefineLiteralAction(DoTest)
/*
* We override exec() rather than exeAction() here, since we want to skip
* all the normal turn sequence routines such as before and after
* notifications and advancing the turn count.
*/
exec(cmd)
{
local target = cmd.dobj.name.toLower();
local script = allTests.valWhich({x: x.testName.toLower == target});
if (script)
script.run();
else
DMsg(test sequence not found, 'Test sequence not found. ');
}
/* Do nothing after the main action */
afterAction() { }
turnSequence() { }
;
VerbRule(Test)
'test' literalDobj
: VerbProduction
action = DoTest
verbPhrase = 'test/testing (what)'
missingQ = 'which sequence do you want to test'
;
/*
* A Test object can be used to create a series of testing commands in your
* game, for example:
*
*. Test 'foo' ['x me', 'i', 'wear uniform'] [uniform];
*
* Would cause the uniform to be moved into the player character's inventory
* and then the commands X ME and then I and WEAR UNIFORM to be executed in
* response to TEST FOO.
*/
class Test: object
/* The name of this test */
testName = 'nil'
/* The list commands to be executed when running this test. */
testList = [ 'z' ]
/*
* The location to move the player character to before running the test
* script
*/
location = nil
/*
* Flag: Do we want to report any change of location by looking around in
* the new one? By default we will.
*/
reportMove = true
/*
* The objects to move into the player character's inventory before
* running the test script.
*/
testHolding = []
/*
* Flag: do we want to report on what items were added to inventory? By
* default we do.
*/
reportHolding = true
/* Move everything in the testHolding list into the actor's inventory */
getHolding()
{
testHolding.forEach({x: x.moveInto(gActor)});
/*
* If we want to report on the effect of moving additional items into
* the player character's inventory, and if we specified any items to
* move, report that the actor is now holding those items.
*/
if(reportHolding && testHolding.length > 0)
DMsg(debug test now holding, '{I} {am} {now} holding {1}.\n',
makeListStr(testHolding, &theName));
}
/*
* Run this test by passing the commands in testList through
* Parser.parse().
*/
run()
{
"Testing sequence: \"<<testName>>\".\n";
/*
* If a location is specified, first move the actor into that
* location.
*/
if (location && gActor.location != location)
{
gActor.moveInto(location);
/* If we want to report the move, show the new room description */
if(reportMove)
gActor.getOutermostRoom.lookAroundWithin();
}
/* Move any required objects into the actor's inventory */
getHolding();
/* Preparse and execute each command in the list */
testList.forEach(new function(x) {
/* Display the command to be executed */
"<b>><<x>></b>\n";
/* Preparse the command */
x = StringPreParser.runAll(x, rmcCommand);
/*
* Execute the preparsed command if it is not nil after preparsing
*/
if(x)
Parser.parse(x);
});
}
;
/*
* The allTests object contains a list of Test objects for listing via the
* LIST TESTS command, and for finding the test that corresponds to a
* particular testName.
*/
allTests: object
lst()
{
if (lst_ == nil)
initLst();
return lst_;
}
initLst()
{
lst_ = new Vector(50);
local obj = firstObj();
while (obj != nil)
{
if(obj.ofKind(Test))
lst_.append(obj);
obj = nextObj(obj);
}
lst_ = lst_.toList();
}
valWhich(cond)
{
if (lst_ == nil)
initLst();
return lst_.valWhich(cond);
}
lst_ = nil
;
#endif // __DEBUG