aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan2/interpreter.cpp
blob: b8906d2c6e1c09ff1c6d376ae2b68f0587ce3779 (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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/* 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/alan2/acode.h"
#include "glk/alan2/alan2.h"
#include "glk/alan2/execute.h"
#include "glk/alan2/interpreter.h"
#include "glk/alan2/saveload.h"
#include "glk/alan2/types.h"
#include "common/debug.h"

namespace Glk {
namespace Alan2 {

// TODO: Refactor these into debug flags
const bool trcflg = false;
const bool stpflg = false;

void Interpreter::if_(Aword v) {
	int lev = 1;
	Aword i;

	if (!v) {
		// Skip to next ELSE or ENDIF on same level
		while (true) {
			i = _vm->memory[_vm->pc++];
			if (I_CLASS(i) == (Aword)C_STMOP) {
				switch (I_OP(i)) {
				case I_ELSE:
					if (lev == 1)
						return;
					break;
				case I_IF:
					lev++;
					break;
				case I_ENDIF:
					lev--;
					if (lev == 0)
						return;
					break;
				default:
					break;
				}
			}
		}
	}
}

void Interpreter::else_() {
	int lev = 1;
	Aword i;

	while (true) {
		// Skip to ENDIF on the same level
		i = _vm->memory[_vm->pc++];
		if (I_CLASS(i) == (Aword)C_STMOP) {
			switch (I_OP(i)) {
			case I_ENDIF:
				lev--;
				if (lev == 0)
					return;
				break;
			case I_IF:
				lev++;
				break;
			default:
				break;
			}
		}
	}
}

void Interpreter::depstart() {
	// A DEPSTART was executed, so skip across the redundant DEPCASE to
	// start at the first expression
	_vm->pc++;
}

void Interpreter::swap() {
	Aptr v1 = _stack->pop();
	Aptr v2 = _stack->pop();

	_stack->push(v1);
	_stack->push(v2);
}

void Interpreter::depexec(Aword v) {
	int lev = 1;
	Aword i;

	if (!v) {
		// The expression was not true, skip to next CASE on the same
		//   level which could be a DEPCASE or DEPELSE
		while (true) {
			i = _vm->memory[_vm->pc++];
			if (I_CLASS(i) == (Aword)C_STMOP) {
				switch (I_OP(i)) {
				case I_DEPSTART:
					lev++;
					break;
				case I_DEPEND:
					if (lev == 1)
						return;
					lev--;
					break;
				case I_DEPCASE:
				case I_DEPELSE:
					if (lev == 1)
						return;
					break;
				default:
					break;
				}
			}
		}
	}
}

void Interpreter::depcase() {
	int lev = 1;
	Aword i;

	// Skip to end of DEPENDING block (next DEPEND on same level) because
	// we have just executed a DEPCASE/DEPELSE statement as a result of a DEPCASE
	// catching

	while (true) {
		i = _vm->memory[_vm->pc++];
		if (I_CLASS(i) == (Aword)C_STMOP) {
			switch (I_OP(i)) {
			case I_DEPSTART:
				lev++;
				break;
			case I_DEPEND:
				lev--;
				if (lev == 0) return;
				break;
			default:
				break;
			}
		}
	}
}

void Interpreter::curVar(Aword i) {
	switch (I_OP(i)) {
	case V_PARAM:
		if (stpflg)
			debug("PARAM \t%5ld\t\t(%d)", _stack->top(), _vm->params[_stack->top() - 1].code);
		_stack->push(_vm->params[_stack->pop() - 1].code);
		break;
	case V_CURLOC:
		if (stpflg)
			debug("CURLOC \t\t\t(%d)", _vm->cur.loc);
		_stack->push(_vm->cur.loc);
		break;
	case V_CURACT:
		if (stpflg)
			debug("CURACT \t\t\t(%d)", _vm->cur.act);
		_stack->push(_vm->cur.act);
		break;
	case V_CURVRB:
		if (stpflg)
			debug("CURVRB \t\t\t(%d)", _vm->cur.vrb);
		_stack->push(_vm->cur.vrb);
		break;
	case V_SCORE:
		if (stpflg)
			debug("CURSCORE \t\t\t(%d)", _vm->cur.score);
		_stack->push(_vm->cur.score);
		break;
	default:
		error("Unknown CURVAR instruction.");
	}
}

void Interpreter::adaptOldOpcodes(Aword i) {
	switch (I_OP(i)) {
	case I_AND:
	case I_OR:
	case I_NE:
	case I_EQ:
	case I_STREQ:
	case I_STREXACT:
	case I_LE:
	case I_GE:
	case I_LT:
	case I_GT:
	case I_PLUS:
	case I_MINUS:
	case I_MULT:
	case I_DIV:
		if (_vm->header->vers[0] == 2 && _vm->header->vers[1] == 7) // Check for 2.7 version
			swap();
	}
}

void Interpreter::stMop(Aword i, Aaddr oldpc) {
	Aptr arg1, arg2, arg3, lh, rh;

	adaptOldOpcodes(i);

	switch (I_OP(i)) {
	case I_PRINT:
		arg1 = _stack->pop();	// fpos
		arg2 = _stack->pop();	// len
		if (stpflg) {
			debug("PRINT \t%5ld, %5ld\t\"", arg1, arg2);
			_vm->col = 34;		// To format it better!
		}
		_execute->print(arg1, arg2);
		if (stpflg)
			debug("\"");
		break;
	case I_SYSTEM:
		arg1 = _stack->pop();	// fpos
		arg2 = _stack->pop();	// len
		if (stpflg) {
			debug("SYSTEM \t%5ld, %5ld\t\"", arg1, arg2);
			_vm->col = 34;		// To format it better!
		}
		_execute->sys(arg1, arg2);
		break;
	case I_GETSTR:
		arg1 = _stack->pop();	// fpos
		arg2 = _stack->pop();	// len
		if (stpflg)
			debug("GETSTR\t%5ld, %5ld", arg1, arg2);
		_execute->getstr(arg1, arg2);
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_QUIT:
		if (stpflg)
			debug("QUIT");
		_execute->quit();
		break;
	case I_LOOK:
		if (stpflg)
			debug("LOOK");
		_execute->look();
		break;
	case I_SAVE:
		if (stpflg)
			debug("SAVE");
		_saveLoad->save();
		break;
	case I_RESTORE:
		if (stpflg)
			debug("RESTORE");
		_saveLoad->restore();
		break;
	case I_RESTART:
		if (stpflg)
			debug("RESTART");
		_execute->restart();
		break;
	case I_LIST:
		arg1 = _stack->pop();	// cnt
		if (stpflg)
			debug("LIST \t%5ld", arg1);
		_execute->list(arg1);
		break;
	case I_EMPTY:
		arg1 = _stack->pop();	// cnt
		arg2 = _stack->pop();	// whr
		if (stpflg)
			debug("EMPTY \t%5ld, %5ld", arg1, arg2);
		_execute->empty(arg1, arg2);
		break;
	case I_SCORE:
		arg1 = _stack->pop();	// score
		if (stpflg)
			debug("SCORE \t%5ld\t\t(%d)", arg1, _vm->scores[arg1 - 1]);
		_execute->score(arg1);
		break;
	case I_VISITS:
		arg1 = _stack->pop();	// visits
		if (stpflg)
			debug("VISITS \t%5ld", arg1);
		_execute->visits(arg1);
		break;
	case I_SCHEDULE:
		arg1 = _stack->pop();	// evt
		arg2 = _stack->pop();	// whr
		arg3 = _stack->pop();	// aft
		if (stpflg)
			debug("SCHEDULE \t%5ld, %5ld, %5ld", arg1, arg2, arg3);
		_execute->schedule(arg1, arg2, arg3);
		break;
	case I_CANCEL:
		arg1 = _stack->pop();	// evt
		if (stpflg)
			debug("CANCEL \t%5ld", arg1);
		_execute->cancl(arg1);
		break;
	case I_MAKE:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// atr
		arg3 = _stack->pop();	// val
		if (stpflg) {
			debug("MAKE \t%5ld, %5ld, ", arg1, arg2);
			if (arg3)
				debug("TRUE");
			else
				debug("FALSE");
		}
		_execute->make(arg1, arg2, arg3);
		break;
	case I_SET:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// atr
		arg3 = _stack->pop();	// val
		if (stpflg)
			debug("SET \t%5ld, %5ld, %5ld", arg1, arg2, arg3);
		_execute->set(arg1, arg2, arg3);
		break;
	case I_STRSET:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// atr
		arg3 = _stack->pop();	// str
		if (stpflg)
			debug("STRSET\t%5ld, %5ld, %5ld", arg1, arg2, arg3);
		_execute->setstr(arg1, arg2, arg3);
		break;
	case I_INCR:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// atr
		arg3 = _stack->pop();	// step
		if (stpflg)
			debug("INCR\t%5ld, %5ld, %5ld", arg1, arg2, arg3);
		_execute->incr(arg1, arg2, arg3);
		break;
	case I_DECR:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// atr
		arg3 = _stack->pop();	// step
		if (stpflg)
			debug("DECR\t%5ld, %5ld, %5ld", arg1, arg2, arg3);
		_execute->decr(arg1, arg2, arg3);
		break;
	case I_ATTRIBUTE:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// atr
		if (stpflg)
			debug("ATTRIBUTE %5ld, %5ld", arg1, arg2);
		_stack->push(_execute->attribute(arg1, arg2));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_STRATTR:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// atr
		if (stpflg)
			debug("STRATTR \t%5ld, %5ld", arg1, arg2);
		_stack->push(_execute->strattr(arg1, arg2));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_LOCATE:
		arg1 = _stack->pop();	// id
		arg2 = _stack->pop();	// whr
		if (stpflg)
			debug("LOCATE \t%5ld, %5ld", arg1, arg2);
		_execute->locate(arg1, arg2);
		break;
	case I_WHERE:
		arg1 = _stack->pop();	// id
		if (stpflg)
			debug("WHERE \t%5ld", arg1);
		_stack->push(_execute->where(arg1));
		if (stpflg)
			debug("\t\t(%ld)", _stack->top());
		break;
	case I_HERE:
		arg1 = _stack->pop();	// id
		if (stpflg)
			debug("HERE \t%5ld", arg1);
		_stack->push(_execute->isHere(arg1));
		if (stpflg) {
			if (_stack->top())
				debug("\t(TRUE)");
			else
				debug("\t(FALSE)");
		}
		break;
	case I_NEAR:
		arg1 = _stack->pop();	// id
		if (stpflg)
			debug("NEAR \t%5ld", arg1);
		_stack->push(_execute->isNear(arg1));
		if (stpflg) {
			if (_stack->top())
				debug("\t(TRUE)");
			else
				debug("\t(FALSE)");
		}
		break;
	case I_USE:
		arg1 = _stack->pop();	// act
		arg2 = _stack->pop();	// scr
		if (stpflg)
			debug("USE \t%5ld, %5ld", arg1, arg2);
		_execute->use(arg1, arg2);
		break;
	case I_IN:
		arg1 = _stack->pop();	// obj
		arg2 = _stack->pop();	// cnt
		if (stpflg)
			debug("IN \t%5ld, %5ld ", arg1, arg2);
		_stack->push(_execute->in(arg1, arg2));
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_DESCRIBE:
		arg1 = _stack->pop();	// id
		if (stpflg) {
			debug("DESCRIBE \t%5ld\t", arg1);
			_vm->col = 34;		// To format it better!
		}
		_execute->describe(arg1);
		break;
	case I_SAY:
		arg1 = _stack->pop();	// id
		if (stpflg)
			debug("SAY \t%5ld\t\t\"", arg1);
		_execute->say(arg1);
		if (stpflg)
			debug("\"");
		break;
	case I_SAYINT:
		arg1 = _stack->pop();	// val
		if (stpflg)
			debug("SAYINT\t%5ld\t\t\"", arg1);
		_execute->sayint(arg1);
		if (stpflg)
			debug("\"");
		break;
	case I_SAYSTR:
		arg1 = _stack->pop();	// adr
		if (stpflg)
			debug("SAYSTR\t%5ld\t\t\"", arg1);
		_execute->saystr((char *)arg1);
		if (stpflg)
			debug("\"");
		break;
	case I_IF:
		arg1 = _stack->pop();	// v
		if (stpflg) {
			debug("IF \t");
			if (arg1) debug(" TRUE"); else debug("FALSE");
		}
		if_(arg1);
		break;
	case I_ELSE:
		if (stpflg)
			debug("ELSE");
		else_();
		break;
	case I_ENDIF:
		if (stpflg)
			debug("ENDIF");
		break;
	case I_AND:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg) {
			debug("AND \t");
			if (lh) debug("TRUE, "); else debug("FALSE, ");
			if (rh) debug("TRUE"); else debug("FALSE");
		}
		_stack->push(lh && rh);
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_OR:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg) {
			debug("OR \t");
			if (lh) debug("TRUE, "); else debug("FALSE, ");
			if (rh) debug("TRUE"); else debug("FALSE");
		}
		_stack->push(lh || rh);
		if (stpflg) {
			if (_stack->top())
				debug("\t(TRUE)");
			else
				debug("\t(FALSE)");
		}
		break;
	case I_NE:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("NE \t%5ld, %5ld", lh, rh);
		_stack->push(lh != rh);
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_EQ:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("EQ \t%5ld, %5ld", lh, rh);
		_stack->push(lh == rh);
		if (stpflg) {
			if (_stack->top())
				debug("\t(TRUE)");
			else
				debug("\t(FALSE)");
		}
		break;
	case I_STREQ:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("STREQ \t%5ld, %5ld", lh, rh);
		_stack->push(_execute->streq((char *)lh, (char *)rh));
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_STREXACT:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("STREXACT \t%5ld, %5ld", lh, rh);
		_stack->push(strcmp((char *)lh, (char *)rh) == 0);
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		free((void *)lh);
		free((void *)rh);
		break;
	case I_LE:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("LE \t%5ld, %5ld", lh, rh);
		_stack->push(lh <= rh);
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_GE:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("GE \t%5ld, %5ld", lh, rh);
		_stack->push(lh >= rh);
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_LT:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("LT \t%5ld, %5ld", lh, rh);
		_stack->push((signed int)lh < (signed int)rh);
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_GT:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("GT \t%5ld, %5ld", lh, rh);
		_stack->push(lh > rh);
		if (stpflg) {
			if (_stack->top()) debug("\t(TRUE)"); else debug("\t(FALSE)");
		}
		break;
	case I_PLUS:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("PLUS \t%5ld, %5ld", lh, rh);
		_stack->push(lh + rh);
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_MINUS:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("MINUS \t%5ld, %5ld", lh, rh);
		_stack->push(lh - rh);
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_MULT:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("MULT \t%5ld, %5ld", lh, rh);
		_stack->push(lh * rh);
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_DIV:
		rh = _stack->pop();
		lh = _stack->pop();
		if (stpflg)
			debug("DIV \t%5ld, %5ld", lh, rh);
		_stack->push(lh / rh);
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_NOT:
		arg1 = _stack->pop();	// val
		if (stpflg) {
			debug("NOT \t");
			if (arg1) debug("TRUE"); else debug("FALSE");
		}
		_stack->push(!arg1);
		if (stpflg) {
			if (_stack->top()) debug("\t\t(TRUE)"); else debug("\t\t(FALSE)");
		}
		break;
	case I_MAX:
		arg1 = _stack->pop();	// atr
		arg2 = _stack->pop();	// whr
		if (stpflg)
			debug("MAX \t%5ld, %5ld", arg1, arg2);
		_stack->push(_execute->agrmax(arg1, arg2));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_SUM:
		arg1 = _stack->pop();	// atr
		arg2 = _stack->pop();	// whr
		if (stpflg)
			debug("SUM \t%5ld, %5ld", arg1, arg2);
		_stack->push(_execute->agrsum(arg1, arg2));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_COUNT:
		arg1 = _stack->pop();	// whr
		if (stpflg)
			debug("COUNT \t%5ld", arg1);
		_stack->push(_execute->agrcount(arg1));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_RND:
		arg1 = _stack->pop();	// from
		arg2 = _stack->pop();	// to
		if (stpflg)
			debug("RANDOM \t%5ld, %5ld", arg1, arg2);
		_stack->push(_execute->rnd(arg1, arg2));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_BTW:
		arg1 = _stack->pop();	// high
		arg2 = _stack->pop();	// low
		arg3 = _stack->pop();	// val
		if (stpflg)
			debug("BETWEEN \t%5ld, %5ld, %5ld", arg1, arg2, arg3);
		_stack->push(_execute->btw(arg1, arg2, arg3));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_CONTAINS:
		arg1 = _stack->pop();	// substring
		arg2 = _stack->pop();	// string
		if (stpflg)
			debug("CONTAINS \t%5ld, %5ld", arg2, arg1);
		_stack->push(_execute->contains(arg2, arg1));
		if (stpflg)
			debug("\t(%ld)", _stack->top());
		break;
	case I_DEPSTART:
		if (stpflg)
			debug("DEPSTART");
		depstart();
		break;
	case I_DEPCASE:
		if (stpflg)
			debug("DEPCASE");
		depcase();
		break;
	case I_DEPEXEC:
		arg1 = _stack->pop();	// v
		if (stpflg) {
			debug("DEPEXEC \t");
			if (arg1) debug(" TRUE"); else debug("FALSE");
		}
		depexec(arg1);
		break;
	case I_DEPELSE:
		if (stpflg)
			debug("DEPELSE");
		depcase();
		break;
	case I_DEPEND:
		if (stpflg)
			debug("DEPEND");
		break;
	case I_RETURN:
		if (stpflg)
			debug("RETURN\n--------------------------------------------------\n");
		_vm->pc = oldpc;
		return;
	default:
		error("Unknown STMOP instruction.");
	}

	if (_vm->fail) {
		_vm->pc = oldpc;
		return;		// TODO: Return true/false, and stop execution if necessary
	}
}

void Interpreter::interpret(Aaddr adr) {
	Aaddr oldpc = _vm->pc;
	Aword i;
 	
	if (stpflg)
		debug("\n++++++++++++++++++++++++++++++++++++++++++++++++++");
 	
	_vm->pc = adr;

	while(true) {
		if (stpflg)
			debug("\n%4x: ", _vm->pc);

		if (_vm->pc > _vm->memTop)
			error("Interpreting outside program.");

		i = _vm->memory[_vm->pc++];
    
		switch (I_CLASS(i)) {
		case C_CONST:
			if (stpflg)
				debug("PUSH  \t%5d", I_OP(i));
			_stack->push(I_OP(i));
			break;
		case C_CURVAR:
			curVar(i);
			break;
		case C_STMOP: 
			stMop(i, oldpc);
			break;
	    default:
			error("Unknown instruction class.");
    }
  }
}

} // End of namespace Alan2
} // End of namespace Glk