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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
/* 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 <string>
#include "glk/alan2/types.h"
#include "glk/alan2/main.h"
#include "glk/alan2/parse.h"
#include "glk/alan2/exe.h"
#include "glk/alan2/stack.h"
#include "glk/alan2/sysdep.h"
#include "glk/alan2/inter.h"
#ifdef GLK
#include "glk/alan2/glkio.h"
#endif
namespace Glk {
namespace Alan2 {
/* PRIVATE DATA */
static int pc;
#ifdef _PROTOTYPES_
static void if_(
Aword v
)
#else
static void if_(v)
Aword v;
#endif
{
int lev = 1;
Aword i;
if (!v) {
/* Skip to next ELSE or ENDIF on same level */
while (TRUE) {
i = memory[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;
}
}
}
}
#ifdef _PROTOTYPES_
static void else_(void)
#else
static void else_()
#endif
{
int lev = 1;
Aword i;
while (TRUE) {
/* Skip to ENDIF on the same level */
i = memory[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;
}
}
}
#ifdef _PROTOTYPES_
static void depstart(void)
#else
static void depstart(void)
#endif
{
/* A DEPSTART was executed so skip across the redundant DEPCASE to
start at the first expression */
pc++;
}
#ifdef _PROTOTYPES_
static void swap(void)
#else
static void swap()
#endif
{
Aptr v1 = pop();
Aptr v2 = pop();
push(v1);
push(v2);
}
#ifdef _PROTOTYPES_
static void depexec(
Aword v
)
#else
static void depexec(v)
Aword v;
#endif
{
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 = memory[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;
}
}
}
#ifdef _PROTOTYPES_
static void depcase(void)
#else
static void depcase()
#endif
{
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 = memory[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;
}
}
}
#ifdef _PROTOTYPES_
void interpret(Aaddr adr)
#else
void interpret(adr)
Aaddr adr;
#endif
{
Aaddr oldpc;
Aword i;
if (stpflg) printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++");
oldpc = pc;
pc = adr;
while(TRUE) {
if (stpflg) printf("\n%4x: ", pc);
if (pc > memTop)
syserr("Interpreting outside program.");
i = memory[pc++];
switch (I_CLASS(i)) {
case C_CONST:
if (stpflg) printf("PUSH \t%5ld", I_OP(i));
push(I_OP(i));
break;
case C_CURVAR:
switch (I_OP(i)) {
case V_PARAM:
if (stpflg) printf("PARAM \t%5ld\t\t(%ld)", top(), params[top()-1].code);
push(params[pop()-1].code);
break;
case V_CURLOC:
if (stpflg) printf("CURLOC \t\t\t(%d)", cur.loc);
push(cur.loc);
break;
case V_CURACT:
if (stpflg) printf("CURACT \t\t\t(%d)", cur.act);
push(cur.act);
break;
case V_CURVRB:
if (stpflg) printf("CURVRB \t\t\t(%d)", cur.vrb);
push(cur.vrb);
break;
case V_SCORE:
if (stpflg) printf("CURSCORE \t\t\t(%d)", cur.score);
push(cur.score);
break;
default:
syserr("Unknown CURVAR instruction.");
break;
}
break;
case C_STMOP:
switch (I_OP(i)) {
case I_PRINT: {
Aptr fpos, len;
fpos = pop();
len = pop();
if (stpflg) {
printf("PRINT \t%5ld, %5ld\t\"", fpos, len);
col = 34; /* To format it better! */
}
print(fpos, len);
if (stpflg)
printf("\"");
break;
}
case I_SYSTEM: {
Aptr fpos, len;
fpos = pop();
len = pop();
if (stpflg) {
printf("SYSTEM \t%5ld, %5ld\t\"", fpos, len);
col = 34; /* To format it better! */
}
sys(fpos, len);
break;
}
case I_GETSTR: {
Aptr fpos, len;
fpos = pop();
len = pop();
if (stpflg)
printf("GETSTR\t%5ld, %5ld", fpos, len);
getstr(fpos, len);
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_QUIT: {
if (stpflg)
printf("QUIT");
quit();
break;
}
case I_LOOK: {
if (stpflg)
printf("LOOK");
look();
break;
}
case I_SAVE: {
if (stpflg)
printf("SAVE");
save();
break;
}
case I_RESTORE: {
if (stpflg)
printf("RESTORE");
restore();
break;
}
case I_RESTART: {
if (stpflg)
printf("RESTART");
restart();
break;
}
case I_LIST: {
Aptr cnt;
cnt = pop();
if (stpflg)
printf("LIST \t%5ld", cnt);
list(cnt);
break;
}
case I_EMPTY: {
Aptr cnt, whr;
cnt = pop();
whr = pop();
if (stpflg)
printf("EMPTY \t%5ld, %5ld", cnt, whr);
empty(cnt, whr);
break;
}
case I_SCORE: {
Aptr sc;
sc = pop();
if (stpflg)
printf("SCORE \t%5ld\t\t(%ld)", sc, scores[sc-1]);
score(sc);
break;
}
case I_VISITS: {
Aptr v;
v = pop();
if (stpflg)
printf("VISITS \t%5ld", v);
visits(v);
break;
}
case I_SCHEDULE: {
Aptr evt, whr, aft;
evt = pop();
whr = pop();
aft = pop();
if (stpflg)
printf("SCHEDULE \t%5ld, %5ld, %5ld", evt, whr, aft);
schedule(evt, whr, aft);
break;
}
case I_CANCEL: {
Aptr evt;
evt = pop();
if (stpflg)
printf("CANCEL \t%5ld", evt);
cancl(evt);
break;
}
case I_MAKE: {
Aptr id, atr, val;
id = pop();
atr = pop();
val = pop();
if (stpflg) {
printf("MAKE \t%5ld, %5ld, ", id, atr);
if (val) printf("TRUE"); else printf("FALSE");
}
make(id, atr, val);
break;
}
case I_SET: {
Aptr id, atr, val;
id = pop();
atr = pop();
val = pop();
if (stpflg) {
printf("SET \t%5ld, %5ld, %5ld", id, atr, val);
}
set(id, atr, val);
break;
}
case I_STRSET: {
Aptr id, atr, str;
id = pop();
atr = pop();
str = pop();
if (stpflg) {
printf("STRSET\t%5ld, %5ld, %5ld", id, atr, str);
}
setstr(id, atr, str);
break;
}
case I_INCR: {
Aptr id, atr, step;
id = pop();
atr = pop();
step = pop();
if (stpflg) {
printf("INCR\t%5ld, %5ld, %5ld", id, atr, step);
}
incr(id, atr, step);
break;
}
case I_DECR: {
Aptr id, atr, step;
id = pop();
atr = pop();
step = pop();
if (stpflg) {
printf("DECR\t%5ld, %5ld, %5ld", id, atr, step);
}
decr(id, atr, step);
break;
}
case I_ATTRIBUTE: {
Aptr id, atr;
id = pop();
atr = pop();
if (stpflg)
printf("ATTRIBUTE %5ld, %5ld", id, atr);
push(attribute(id, atr));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_STRATTR: {
Aptr id, atr;
id = pop();
atr = pop();
if (stpflg)
printf("STRATTR \t%5ld, %5ld", id, atr);
push(strattr(id, atr));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_LOCATE: {
Aptr id, whr;
id = pop();
whr = pop();
if (stpflg)
printf("LOCATE \t%5ld, %5ld", id, whr);
locate(id, whr);
break;
}
case I_WHERE: {
Aptr id;
id = pop();
if (stpflg)
printf("WHERE \t%5ld", id);
push(where(id));
if (stpflg)
printf("\t\t(%ld)", top());
break;
}
case I_HERE: {
Aptr id;
id = pop();
if (stpflg)
printf("HERE \t%5ld", id);
push(isHere(id));
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_NEAR: {
Aptr id;
id = pop();
if (stpflg)
printf("NEAR \t%5ld", id);
push(isNear(id));
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_USE: {
Aptr act, scr;
act = pop();
scr = pop();
if (stpflg)
printf("USE \t%5ld, %5ld", act, scr);
use(act, scr);
break;
}
case I_IN: {
Aptr obj, cnt;
obj = pop();
cnt = pop();
if (stpflg)
printf("IN \t%5ld, %5ld ", obj, cnt);
push(in(obj, cnt));
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_DESCRIBE: {
Aptr id;
id = pop();
if (stpflg) {
printf("DESCRIBE \t%5ld\t", id);
col = 34; /* To format it better! */
}
describe(id);
break;
}
case I_SAY: {
Aptr id;
id = pop();
if (stpflg)
printf("SAY \t%5ld\t\t\"", id);
say(id);
if (stpflg)
printf("\"");
break;
}
case I_SAYINT: {
Aptr val;
val = pop();
if (stpflg)
printf("SAYINT\t%5ld\t\t\"", val);
sayint(val);
if (stpflg)
printf("\"");
break;
}
case I_SAYSTR: {
Aptr adr;
adr = pop();
if (stpflg)
printf("SAYSTR\t%5ld\t\t\"", adr);
saystr((char *)adr);
if (stpflg)
printf("\"");
break;
}
case I_IF: {
Aptr v;
v = pop();
if (stpflg) {
printf("IF \t");
if (v) printf(" TRUE"); else printf("FALSE");
}
if_(v);
break;
}
case I_ELSE: {
if (stpflg)
printf("ELSE");
else_();
break;
}
case I_ENDIF: {
if (stpflg)
printf("ENDIF");
break;
}
case I_AND: {
Aptr lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg) {
printf("AND \t");
if (lh) printf("TRUE, "); else printf("FALSE, ");
if (rh) printf("TRUE"); else printf("FALSE");
}
push(lh && rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_OR: {
Aptr lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg) {
printf("OR \t");
if (lh) printf("TRUE, "); else printf("FALSE, ");
if (rh) printf("TRUE"); else printf("FALSE");
}
push(lh || rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_NE: {
Aptr lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("NE \t%5ld, %5ld", lh, rh);
push(lh != rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_EQ: {
Aptr lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("EQ \t%5ld, %5ld", lh, rh);
push(lh == rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_STREQ: {
Aptr lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("STREQ \t%5ld, %5ld", lh, rh);
push(streq((char *)lh, (char *)rh));
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_STREXACT: {
Aptr lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("STREXACT \t%5ld, %5ld", lh, rh);
push(strcmp((char *)lh, (char *)rh) == 0);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
free((void *)lh);
free((void *)rh);
break;
}
case I_LE: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("LE \t%5ld, %5ld", lh, rh);
push(lh <= rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_GE: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("GE \t%5ld, %5ld", lh, rh);
push(lh >= rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_LT: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("LT \t%5ld, %5ld", lh, rh);
push((signed int)lh < (signed int)rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_GT: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("GT \t%5ld, %5ld", lh, rh);
push(lh > rh);
if (stpflg)
if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
break;
}
case I_PLUS: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("PLUS \t%5ld, %5ld", lh, rh);
push(lh + rh);
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_MINUS: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("MINUS \t%5ld, %5ld", lh, rh);
push(lh - rh);
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_MULT: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("MULT \t%5ld, %5ld", lh, rh);
push(lh * rh);
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_DIV: {
Aint lh, rh;
if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
swap();
rh = pop();
lh = pop();
if (stpflg)
printf("DIV \t%5ld, %5ld", lh, rh);
push(lh / rh);
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_NOT: {
Aptr val;
val = pop();
if (stpflg) {
printf("NOT \t");
if (val) printf("TRUE"); else printf("FALSE");
}
push(!val);
if (stpflg)
if (top()) printf("\t\t(TRUE)"); else printf("\t\t(FALSE)");
break;
}
case I_MAX: {
Aptr atr, whr;
atr = pop();
whr = pop();
if (stpflg)
printf("MAX \t%5ld, %5ld", atr, whr);
push(agrmax(atr, whr));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_SUM: {
Aptr atr, whr;
atr = pop();
whr = pop();
if (stpflg)
printf("SUM \t%5ld, %5ld", atr, whr);
push(agrsum(atr, whr));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_COUNT: {
Aptr whr;
whr = pop();
if (stpflg)
printf("COUNT \t%5ld", whr);
push(agrcount(whr));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_RND: {
Aptr from, to;
from = pop();
to = pop();
if (stpflg)
printf("RANDOM \t%5ld, %5ld", from, to);
push(rnd(from, to));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_BTW: {
Aint low, high, val;
high = pop();
low = pop();
val = pop();
if (stpflg)
printf("BETWEEN \t%5ld, %5ld, %5ld", val, low, high);
push(btw(val, low, high));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_CONTAINS: {
Aptr string, substring;
substring = pop();
string = pop();
if (stpflg)
printf("CONTAINS \t%5ld, %5ld", string, substring);
push(contains(string, substring));
if (stpflg)
printf("\t(%ld)", top());
break;
}
case I_DEPSTART:
if (stpflg)
printf("DEPSTART");
depstart();
break;
case I_DEPCASE:
if (stpflg)
printf("DEPCASE");
depcase();
break;
case I_DEPEXEC: {
Aptr v;
v = pop();
if (stpflg) {
printf("DEPEXEC \t");
if (v) printf(" TRUE"); else printf("FALSE");
}
depexec(v);
break;
}
case I_DEPELSE:
if (stpflg)
printf("DEPELSE");
depcase();
break;
case I_DEPEND:
if (stpflg)
printf("DEPEND");
break;
case I_RETURN:
if (stpflg)
printf("RETURN\n--------------------------------------------------\n");
pc = oldpc;
return;
default:
syserr("Unknown STMOP instruction.");
break;
}
if (fail) {
pc = oldpc;
return;
}
break;
default:
syserr("Unknown instruction class.");
break;
}
}
}
} // End of namespace Alan2
} // End of namespace Glk
|