Debian Codegraph @

terps/alan3/inter.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 inter.c
4
5 Interpreter unit for Alan interpreter Arun
6
7 \*----------------------------------------------------------------------*/
8 #include "inter.h"
9
10
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include "current.h"
15 #include "exe.h"
16 #include "syserr.h"
17 #include "debug.h"
18 #include "options.h"
19 #include "save.h"
20 #include "memory.h"
21 #include "output.h"
22 #include "score.h"
23 #include "params.h"
24 #include "instance.h"
25 #include "Container.h"
26 #include "Location.h"
27 #include "compatibility.h"
28
29 #ifdef HAVE_GLK
30 #define MAP_STDIO_TO_GLK
31 #include "glkio.h"
32 #endif
33
34
35 bool stopAtNextLine = false;
36 bool fail = false;
37
38
39 /* PRIVATE DATA */
40
41 static int pc;
42 static Stack stack = NULL;
43
44 static void (*interpreterMock)(Aaddr adr) = NULL;
45
46
47 /*======================================================================*/
48 void setInterpreterMock(void (*mock)(Aaddr adr)) {
49 interpreterMock = mock;
50 }
51
52
53 /*======================================================================*/
54 void setInterpreterStack(Stack theStack)
55 {
56 stack = theStack;
57 }
58
59 /* Indicates if the result "field" has been printed, so that we know
60 * if we need to advance over it */
61 static bool resultTraced = false;
62
63 /* Format for all trace lines:
64 - address
65 - instruction name
66 - 3 parameters
67 - result field
68 - stack dump
69
70 All traceInstruction-functions are responsible to move to beginning
71 of result field. The single traceResult() function will print there,
72 and outputs (like print and system) will move over it if necessary, as
73 will the stack dump function.
74 */
75 static char *format = "%-11s %7s %7s %7s %7s";
76 /*----------------------------------------------------------------------*/
77 static void traceInstruction0(char *name) {
78 if (!traceInstructionOption)
79 return;
80
81 printf(format, name, "", "", "", "");
82
83 }
84
85 /*----------------------------------------------------------------------*/
86 static void traceInstruction1(char *name, Aword p1) {
87 char buf1[100];
88
89 if (!traceInstructionOption)
90 return;
91
92 sprintf(buf1, "%6d ", p1);
93
94 printf(format, name, buf1, "", "", "");
95 }
96
97 /*----------------------------------------------------------------------*/
98 static void traceInstruction1s(char *name, char *p1) {
99 if (!traceInstructionOption)
100 return;
101
102 printf(format, name, p1, "", "", "");
103 }
104
105 /*----------------------------------------------------------------------*/
106 static void traceInstruction1R(char *name, Aword p1, Aword result) {
107 char buf1[100];
108
109 if (!traceInstructionOption)
110 return;
111
112 sprintf(buf1, "%6d ", p1);
113
114 printf(format, name, buf1, "", "", "");
115 printf("=%-7d ", result);
116 resultTraced = true;
117 }
118
119 /*----------------------------------------------------------------------*/
120 static void traceInstruction2(char *name, Aword p1, Aword p2) {
121 char buf1[100];
122 char buf2[100];
123
124 if (!traceInstructionOption)
125 return;
126
127 sprintf(buf1, "%6d,", p1);
128 sprintf(buf2, "%6d ", p2);
129
130 printf(format, name, buf1, buf2, "", "");
131 }
132
133
134 /*----------------------------------------------------------------------*/
135 static void traceInstruction2s(char *name, char *p1, char *p2) {
136 if (!traceInstructionOption)
137 return;
138 printf(format, name, p1, p2, "", "");
139 }
140
141
142 /*----------------------------------------------------------------------*/
143 static void traceInstruction2ds(char *name, Aword p1, char *p2) {
144 char buf1[100];
145
146 if (!traceInstructionOption)
147 return;
148
149 sprintf(buf1, "%6d ", p1);
150 printf(format, name, buf1, p2, "", "");
151 }
152
153 /*----------------------------------------------------------------------*/
154 static void traceInstruction2sd(char *name, char *p1, Aword p2) {
155 char buf2[100];
156
157 if (!traceInstructionOption)
158 return;
159
160 sprintf(buf2, "%6d ", p2);
161 printf(format, name, p1, buf2, "", "");
162 }
163
164 /*----------------------------------------------------------------------*/
165 static void traceInstruction3(char *name, Aword p1, Aword p2, Aword p3) {
166 char buf1[100];
167 char buf2[100];
168 char buf3[100];
169
170 if (!traceInstructionOption)
171 return;
172
173 sprintf(buf1, "%6d,", p1);
174 sprintf(buf2, "%6d,", p2);
175 sprintf(buf3, "%6d ", p3);
176
177 printf(format, name, buf1, buf2, buf3, "");
178 }
179
180
181 /*----------------------------------------------------------------------*/
182 static void traceInstruction3dds(char *name, Aword p1, Aword p2, char *p3) {
183 char buf1[100];
184 char buf2[100];
185
186 if (!traceInstructionOption)
187 return;
188
189 sprintf(buf1, "%6d,", p1);
190 sprintf(buf2, "%6d,", p2);
191
192 printf(format, name, buf1, buf2, p3, "");
193 }
194
195
196 /*----------------------------------------------------------------------*/
197 static void traceInstruction5(char *name, Aword p1, Aword p2, Aword p3, Aword p4, Aword p5) {
198 char buf1[100];
199 char buf2[100];
200 char buf3[100];
201 char buf4[100];
202 char buf5[100];
203
204 if (!traceInstructionOption)
205 return;
206
207 sprintf(buf1, "%6d,", p1);
208 sprintf(buf2, "%6d,", p2);
209 sprintf(buf3, "%6d,", p3);
210 sprintf(buf4, "%6d,", p4);
211 sprintf(buf5, "%6d ", p5);
212
213 printf(format, name, buf1, buf2, buf3, buf4, buf5);
214 }
215
216
217 /*----------------------------------------------------------------------*/
218 static void moveToStackTraceField() {
219 if (!resultTraced) {
220 printf("%11s", ""); /* Move past result field */
221 resultTraced = true;
222 }
223 }
224
225 /*----------------------------------------------------------------------*/
226 static void traceStack(Stack theStack) {
227 moveToStackTraceField();
228 dumpStack(theStack);
229 }
230
231
232 /*----------------------------------------------------------------------*/
233 static void traceOutput(char *leading) {
234 moveToStackTraceField();
235 printf("%s", leading);
236 }
237
238 /*----------------------------------------------------------------------*/
239 static void traceEndOfOutput(char *terminator) {
240 printf("%s", terminator);
241 if (traceStackOption) {
242 printf("\n ");
243 printf(format, "", "", "", "", "");
244 resultTraced = false;
245 moveToStackTraceField();
246 }
247 }
248
249 /*----------------------------------------------------------------------*/
250 static void tracePC(int pc) {
251 if (traceInstructionOption)
252 printf("\n%4x: ", pc);
253 resultTraced = false;
254 }
255
256 static bool skipStackDump = false; /* Need to be able to skip it for some outputs */
257
258 /*----------------------------------------------------------------------*/
259 static void traceSkip() {
260 if (traceInstructionOption) {
261 printf("\n :");
262 skipStackDump = true;
263 }
264 }
265
266 /*----------------------------------------------------------------------*/
267 static void interpretIf(Aword v)
268 {
269 int lev = 1;
270 Aword i;
271
272 if (!v) {
273 /* Skip to next ELSE or ENDIF on same level */
274 traceSkip();
275 while (true) {
276 i = memory[pc++];
277 if (I_CLASS(i) == (Aword)C_STMOP)
278 switch (I_OP(i)) {
279 case I_ELSE:
280 if (lev == 1) {
281 tracePC(pc);
282 traceInstruction0("ELSE");
283 return;
284 }
285 break;
286 case I_IF:
287 lev++;
288 break;
289 case I_ENDIF:
290 lev--;
291 if (lev == 0) {
292 tracePC(pc);
293 traceInstruction0("ENDIF");
294 return;
295 }
296 break;
297 }
298 }
299 }
300 }
301
302
303 /*----------------------------------------------------------------------*/
304 static void interpretElse(void)
305 {
306 int lev = 1;
307 Aword i;
308
309 traceSkip();
310 while (true) {
311 /* Skip to ENDIF on the same level */
312 i = memory[pc++];
313 if (I_CLASS(i) == (Aword)C_STMOP)
314 switch (I_OP(i)) {
315 case I_ENDIF:
316 lev--;
317 if (lev == 0) return;
318 break;
319 case I_IF:
320 lev++;
321 break;
322 }
323 }
324 }
325
326
327 /*----------------------------------------------------------------------*/
328 static void goToLOOPEND(void) {
329 int level = 1;
330 int i;
331
332 traceSkip();
333 while (true) {
334 /* Skip past LOOPEND on the same level */
335 i = memory[pc];
336 if (I_CLASS(i) == (Aword)C_STMOP)
337 switch (I_OP(i)) {
338 case I_LOOPEND:
339 level--;
340 if (level == 0)
341 return;
342 break;
343 case I_LOOP:
344 level++;
345 break;
346 }
347 pc++;
348 }
349 }
350
351
352 /*----------------------------------------------------------------------*/
353 static void jumpBackToStartOfMatchingLOOP(void) {
354 int level = 1;
355 int i;
356
357 traceSkip();
358 pc--; /* Ignore the instruction we're on */
359 while (true) {
360 /* Skip back past LOOP on the same level */
361 i = memory[--pc];
362 if (I_CLASS(i) == (Aword)C_STMOP)
363 switch (I_OP(i)) {
364 case I_LOOPEND:
365 level++;
366 break;
367 case I_LOOP:
368 level--;
369 if (level == 0) {
370 return;
371 }
372 break;
373 }
374 }
375 }
376
377
378 /*----------------------------------------------------------------------*/
379 static void nextLoop(void)
380 {
381 goToLOOPEND();
382 }
383
384
385 /*----------------------------------------------------------------------*/
386 static void endLoop(Aint index, Aint limit)
387 {
388 if (index < limit) {
389 index++;
390 push(stack, limit);
391 push(stack, index);
392 jumpBackToStartOfMatchingLOOP();
393 tracePC(pc);
394 traceInstruction0("LOOP");
395 pc++;
396 }
397 }
398
399
400 /*----------------------------------------------------------------------*/
401 static void stackDup(void)
402 {
403 push(stack, top(stack));
404 }
405
406
407 /*----------------------------------------------------------------------*/
408 static void depexec(Aword v)
409 {
410 int lev = 1;
411 Aword i;
412 char *instructionString = "DEPELSE";
413
414 if (!v) {
415 /* The expression was not true, skip to next CASE on the same
416 level which could be a DEPCASE or DEPELSE */
417 if (traceInstructionOption) printf("\n : ");
418 while (true) {
419 i = memory[pc++];
420 if (I_CLASS(i) == (Aword)C_STMOP)
421 switch (I_OP(i)) {
422 case I_DEPEND:
423 lev++;
424 break;
425 case I_ENDDEP:
426 if (lev == 1) {
427 pc--;
428 if (traceInstructionOption)
429 printf("\n%4x: ENDDEP", pc);
430 return;
431 } else
432 lev--;
433 break;
434 case I_DEPCASE:
435 instructionString = "DEPCASE";
436 case I_DEPELSE:
437 if (lev == 1) {
438 if (traceInstructionOption)
439 printf("\n%4x: %s", pc, instructionString);
440 return;
441 }
442 break;
443 }
444 }
445 }
446 }
447
448
449 /*----------------------------------------------------------------------*/
450 static void depcase(void)
451 {
452 int lev = 1;
453 Aword i;
454
455 /*
456 We have just executed a DEPCASE/DEPELSE clause as a result of a
457 DEPCASE catching so skip to end of DEPENDING block (next DEPEND
458 on same level) then return.
459 */
460
461 if (traceInstructionOption) printf("\n : ");
462 while (true) {
463 i = memory[pc++];
464 if (I_CLASS(i) == (Aword)C_STMOP)
465 switch (I_OP(i)) {
466 case I_DEPEND:
467 lev++;
468 break;
469 case I_ENDDEP:
470 lev--;
471 if (lev == 0) {
472 pc--;
473 return;
474 }
475 break;
476 }
477 }
478 }
479
480
481 /*----------------------------------------------------------------------*/
482 static char *booleanValue(Abool value) {
483 if (value) return "TRUE";
484 else return "FALSE";
485 }
486
487 /*----------------------------------------------------------------------*/
488 static char *stringValue(Aptr address) {
489 static char string[1000];
490
491 sprintf(string, "0x%lx (\"%s\")\t\t", (unsigned long) address, (char *)fromAptr(address));
492 return string;
493 }
494
495 /*----------------------------------------------------------------------*/
496 static char *pointerValue(Aptr address) {
497 static char string[100];
498
499 sprintf(string, "@%lx ",(unsigned long) address);
500 return string;
501 }
502
503 /*----------------------------------------------------------------------*/
504 static void traceStringTopValue() {
505 if (traceInstructionOption) {
506 printf("=%10s", stringValue(top(stack)));
507 resultTraced = true;
508 }
509 }
510
511 /*----------------------------------------------------------------------*/
512 static void traceBooleanTopValue() {
513 if (traceInstructionOption) {
514 printf("=%-10s", top(stack)?"TRUE":"FALSE");
515 resultTraced = true;
516 }
517 }
518
519 /*----------------------------------------------------------------------*/
520 static void traceIntegerTopValue() {
521 if (traceInstructionOption) {
522 printf("=%-10d", top(stack));
523 resultTraced = true;
524 }
525 }
526
527 /*----------------------------------------------------------------------*/
528 static void tracePointerTopValue() {
529 if (traceInstructionOption) {
530 printf("=%-10s", pointerValue(top(stack)));
531 resultTraced = true;
532 }
533 }
534
535 /*----------------------------------------------------------------------*/
536 static void traceInstanceTopValue() {
537 if (traceInstructionOption) {
538 printf("=%ld ('", (long)top(stack));
539 traceSay(top(stack));
540 printf("')");
541 resultTraced = true;
542 if (traceStackOption) {
543 /* TODO Move to stack field on next line */
544 printf("\n ");
545 printf(format, "", "", "", "");
546 printf("%10s", "");
547 }
548 }
549 }
550
551
552 /*----------------------------------------------------------------------*/
553 static char *transitivityFlag(ATrans value) {
554 switch (value) {
555 case TRANSITIVE:
556 return "Trans.";
557 case DIRECT:
558 return "Direct";
559 case INDIRECT:
560 return "Indir.";
561 }
562 syserr("Unexpected transitivity");
563 return "ERROR";
564 }
565
566 /*----------------------------------------------------------------------*/
567 static char *printForm(SayForm form) {
568 switch (form) {
569 case SAY_SIMPLE: return "-";
570 case SAY_INDEFINITE: return "An";
571 case SAY_DEFINITE: return "The";
572 case SAY_NEGATIVE: return "No";
573 case SAY_PRONOUN: return "It";
574 }
575 return "**Unknown!!***";
576 }
577
578
579 static Aaddr invocation[1000];
580 int recursionDepth = 0;
581
582 /*----------------------------------------------------------------------*/
583 static void checkForRecursion(Aaddr adr) {
584 int i;
585
586 for (i = 0; i < recursionDepth; i++)
587 if (invocation[i] == adr)
588 apperr("Interpreter recursion.");
589 invocation[recursionDepth++] = adr;
590 if (recursionDepth > 1000)
591 syserr("Interpreter call stack too deep.");
592 }
593
594
595 /*----------------------------------------------------------------------*/
596 static bool stillOnSameLine(Aint line, Aint file) {
597 return line != current.sourceLine || file != current.sourceFile;
598 }
599
600 /*======================================================================*/
601 void interpret(Aaddr adr)
602 {
603 Aaddr oldpc;
604 Aword i;
605
606 /* Check for mock implementation */
607 if (interpreterMock != NULL) {
608 interpreterMock(adr);
609 return;
610 }
611
612 /* Sanity checks: */
613 if (adr == 0) syserr("Interpreting at address 0.");
614 checkForRecursion(adr);
615
616 if (traceInstructionOption)
617 printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++");
618
619 oldpc = pc;
620 pc = adr;
621 while(true) {
622 if (pc > memTop)
623 syserr("Interpreting outside program.");
624
625 i = memory[pc++];
626
627 switch (I_CLASS(i)) {
628 case C_CONST:
629 if (tracePushOption) {
630 tracePC(pc-1);
631 traceInstruction1("PUSH", I_OP(i));
632 }
633 push(stack, I_OP(i));
634 if (tracePushOption && traceStackOption)
635 traceStack(stack);
636 break;
637 case C_CURVAR:
638 tracePC(pc-1);
639 switch (I_OP(i)) {
640 case V_PARAM:
641 traceInstruction1("PARAM", top(stack));
642 push(stack, globalParameters[pop(stack)-1].instance);
643 traceIntegerTopValue();
644 break;
645 case V_CURLOC:
646 traceInstruction0("CURLOC");
647 push(stack, current.location);
648 traceIntegerTopValue();
649 break;
650 case V_CURACT:
651 traceInstruction0("CURACT");
652 push(stack, current.actor);
653 traceIntegerTopValue();
654 break;
655 case V_CURVRB:
656 traceInstruction0("CURVRB");
657 push(stack, current.verb);
658 traceIntegerTopValue();
659 break;
660 case V_CURRENT_INSTANCE:
661 traceInstruction0("CURINS");
662 push(stack, current.instance);
663 traceIntegerTopValue();
664 break;
665 case V_SCORE:
666 traceInstruction0("CURSCORE");
667 push(stack, current.score);
668 traceIntegerTopValue();
669 break;
670 case V_MAX_INSTANCE: {
671 int instanceMax = isPreBeta3(header->version)?header->instanceMax:header->instanceMax-1;
672 traceInstruction0("MAXINSTANCE");
673 push(stack, instanceMax);
674 traceIntegerTopValue();
675 break;
676 }
677 default:
678 syserr("Unknown CURVAR instruction.");
679 break;
680 }
681 if (traceStackOption)
682 traceStack(stack);
683 break;
684
685 case C_STMOP:
686 tracePC(pc-1);
687 switch (I_OP(i)) {
688
689 case I_DUP:
690 traceInstruction0("DUP");
691 stackDup();
692 break;
693
694 case I_DUPSTR:
695 traceInstruction0("DUPSTR");
696 push(stack, toAptr(strdup((char*)fromAptr(top(stack)))));
697 break;
698
699 case I_POP: {
700 Aptr top = pop(stack);
701 traceInstruction1("POP", top);
702 break;
703 }
704
705 case I_LINE: {
706 Aint line = pop(stack);
707 Aint file = pop(stack);
708 traceInstruction2("LINE", file, line);
709 if (traceStackOption)
710 traceStack(stack);
711 skipStackDump = true;
712 if (line != 0) {
713 bool atNext = stopAtNextLine && line != current.sourceLine;
714 bool atBreakpoint = breakpointIndex(file, line) != -1;
715 if (traceSourceOption && stillOnSameLine(line, file)) {
716 if (col != 1 || traceInstructionOption)
717 printf("\n");
718 showSourceLine(file, line);
719 if (!traceInstructionOption)
720 printf("\n");
721 }
722 current.sourceLine = line;
723 current.sourceFile = file;
724 if (atNext || atBreakpoint) {
725 stopAtNextLine = false;
726 debug(true, line, file);
727 }
728 }
729 break;
730 }
731
732 case I_PRINT: {
733 Aint fpos = pop(stack);
734 Aint len = pop(stack);
735 if (traceInstructionOption) {
736 traceInstruction2("PRINT", fpos, len);
737 col = 41; /* To break lines better! */
738 traceOutput("\"");
739 }
740 print(fpos, len);
741 if (traceInstructionOption) {
742 traceEndOfOutput("\"");
743 }
744 break;
745 }
746
747 case I_STYLE: {
748 Aint style = pop(stack);
749 traceInstruction1("STYLE", style);
750 setStyle(style);
751 break;
752 }
753
754 case I_SYSTEM: {
755 Aint fpos = pop(stack);
756 Aint len = pop(stack);
757 if (traceInstructionOption) {
758 traceInstruction2("SYSTEM", fpos, len);
759 col = 34; /* To format possible output better! */
760 traceOutput("\"");
761 }
762 sys(fpos, len);
763 if (traceInstructionOption)
764 printf("\"\t\t\t\t\t\t");
765 break;
766 }
767
768 case I_GETSTR: {
769 Aint fpos = pop(stack);
770 Aint len = pop(stack);
771 traceInstruction2("GETSTR", fpos, len);
772 push(stack, toAptr(getStringFromFile(fpos, len)));
773 traceStringTopValue();
774 break;
775 }
776
777 case I_QUIT: {
778 traceInstruction0("QUIT");
779 quitGame();
780 break;
781 }
782 case I_LOOK: {
783 traceInstruction0("LOOK");
784 look();
785 break;
786 }
787 case I_SAVE: {
788 traceInstruction0("SAVE");
789 save();
790 break;
791 }
792 case I_RESTORE: {
793 traceInstruction0("RESTORE");
794 restore();
795 break;
796 }
797 case I_RESTART: {
798 traceInstruction0("RESTART");
799 restartGame();
800 break;
801 }
802
803 case I_SCORE: {
804 Aint sc = pop(stack);
805 traceInstruction1R("SCORE", sc, scores[sc-1]);
806 score(sc);
807 break;
808 }
809 case I_VISITS: {
810 Aint v = pop(stack);
811 traceInstruction1("VISITS", v);
812 visits(v);
813 break;
814 }
815
816 case I_LIST: {
817 Aint cnt = pop(stack);
818 traceInstruction1("LIST", cnt);
819 list(cnt);
820 break;
821 }
822 case I_EMPTY: {
823 Aint cnt = pop(stack);
824 Aint whr = pop(stack);
825 traceInstruction2("EMPTY", cnt, whr);
826 empty(cnt, whr);
827 break;
828 }
829 case I_SCHEDULE: {
830 Aint event = pop(stack);
831 Aint where = pop(stack);
832 Aint after = pop(stack);
833 traceInstruction3("SCHEDULE", event, where, after);
834 schedule(event, where, after);
835 break;
836 }
837 case I_CANCEL: {
838 Aint event = pop(stack);
839 traceInstruction1("CANCEL", event);
840 cancelEvent(event);
841 break;
842 }
843 case I_MAKE: {
844 Aint atr = pop(stack);
845 Aid id = pop(stack);
846 Abool val = pop(stack);
847 traceInstruction3dds("MAKE", id, atr, booleanValue(val));
848 setInstanceAttribute(id, atr, val);
849 break;
850 }
851 case I_SET: {
852 Aint atr = pop(stack);
853 Aid id = pop(stack);
854 Aptr val = pop(stack);
855 traceInstruction3("SET", id, atr, val);
856 setInstanceAttribute(id, atr, val);
857 break;
858 }
859 case I_SETSTR: {
860 Aint atr = pop(stack);
861 Aid id = pop(stack);
862 Aptr str = pop(stack);
863 traceInstruction3dds("SETSTR", id, atr, stringValue(str));
864 setInstanceStringAttribute(id, atr, fromAptr(str));
865 break;
866 }
867 case I_SETSET: {
868 Aint atr = pop(stack);
869 Aid id = pop(stack);
870 Aptr set = pop(stack);
871 traceInstruction3dds("SETSET", id, atr, pointerValue(set));
872 setInstanceSetAttribute(id, atr, set);
873 break;
874 }
875 case I_NEWSET: {
876 Set *set = newSet(0);
877 traceInstruction0("NEWSET");
878 push(stack, toAptr(set));
879 tracePointerTopValue();
880 break;
881 }
882 case I_UNION: {
883 Aptr set2 = pop(stack);
884 Aptr set1 = pop(stack);
885 traceInstruction2("UNION", set1, set2);
886 push(stack, toAptr(setUnion((Set *)fromAptr(set1), (Set *)fromAptr(set2))));
887 tracePointerTopValue();
888 freeSet((Set *)fromAptr(set1));
889 freeSet((Set *)fromAptr(set2));
890 break;
891 }
892 case I_INCR: {
893 Aint step = pop(stack);
894 traceInstruction1("INCR", step);
895 push(stack, pop(stack) + step);
896 traceIntegerTopValue();
897 break;
898 }
899 case I_DECR: {
900 Aint step = pop(stack);
901 traceInstruction1("DECR", step);
902 push(stack, pop(stack) - step);
903 traceIntegerTopValue();
904 break;
905 }
906 case I_INCLUDE: {
907 Aint member = pop(stack);
908 traceInstruction1("INCLUDE", member);
909 addToSet((Set *)fromAptr(top(stack)), member);
910 break;
911 }
912 case I_EXCLUDE: {
913 Aint member = pop(stack);
914 traceInstruction1("EXCLUDE", member);
915 removeFromSet((Set *)fromAptr(top(stack)), member);
916 break;
917 }
918 case I_SETSIZE: {
919 Aptr ptr = pop(stack);
920 Set *set = (Set *)fromAptr(ptr);
921 traceInstruction1s("SETSIZE", pointerValue(ptr));
922 push(stack, setSize(set));
923 traceIntegerTopValue();
924 break;
925 }
926 case I_SETMEMB: {
927 Aptr ptr = pop(stack);
928 Set *set = (Set *)fromAptr(ptr);
929 Aint index = pop(stack);
930 traceInstruction2sd("SETMEMB", pointerValue(ptr), index);
931 push(stack, getSetMember(set, index));
932 traceIntegerTopValue();
933 break;
934 }
935 case I_CONTSIZE: {
936 Abool transitivity = pop(stack);
937 Aint container = pop(stack);
938 traceInstruction2ds("CONTSIZE", container, transitivityFlag(transitivity));
939 push(stack, containerSize(container, transitivity));
940 if (traceInstructionOption)
941 traceIntegerTopValue();
942 break;
943 }
944 case I_CONTMEMB: {
945 Abool transitivity = pop(stack);
946 Aint container = pop(stack);
947 Aint index = pop(stack);
948 traceInstruction3dds("CONTMEMB", container, index, transitivityFlag(transitivity));
949 push(stack, getContainerMember(container, index, transitivity));
950 traceIntegerTopValue();
951 break;
952 }
953 case I_ATTRIBUTE: {
954 Aint atr = pop(stack);
955 Aid id = pop(stack);
956 traceInstruction2("ATTRIBUTE", id, atr);
957 push(stack, getInstanceAttribute(id, atr));
958 traceIntegerTopValue();
959 break;
960 }
961 case I_ATTRSTR: {
962 Aint atr = pop(stack);
963 Aid id = pop(stack);
964 traceInstruction2("ATTRSTR", id, atr);
965 push(stack, toAptr(getInstanceStringAttribute(id, atr)));
966 traceStringTopValue();
967 break;
968 }
969 case I_ATTRSET: {
970 Aint atr = pop(stack);
971 Aid id = pop(stack);
972 traceInstruction2("ATTRSET", id, atr);
973 push(stack, toAptr(getInstanceSetAttribute(id, atr)));
974 tracePointerTopValue();
975 break;
976 }
977 case I_SHOW: {
978 Aint image = pop(stack);
979 Aint align = pop(stack);
980 traceInstruction2("SHOW", image, align);
981 showImage(image, align);
982 break;
983 }
984 case I_PLAY: {
985 Aint sound = pop(stack);
986 traceInstruction1("PLAY", sound);
987 playSound(sound);
988 break;
989 }
990 case I_LOCATE: {
991 Aid id = pop(stack);
992 Aint whr = pop(stack);
993 traceInstruction2("LOCATE", id, whr);
994 locate(id, whr);
995 break;
996 }
997 case I_WHERE: {
998 Abool transitivity = pop(stack);
999 Aid id = pop(stack);
1000 traceInstruction2ds("WHERE", id, transitivityFlag(transitivity));
1001 push(stack, where(id, transitivity));
1002 traceInstanceTopValue();
1003 break;
1004 }
1005 case I_LOCATION: {
1006 Aid id = pop(stack);
1007 traceInstruction1("LOCATION", id);
1008 push(stack, locationOf(id));
1009 traceInstanceTopValue();
1010 break;
1011 }
1012 case I_HERE: {
1013 Abool transitivity = pop(stack);
1014 Aid id = pop(stack);
1015 traceInstruction2ds("HERE", id, transitivityFlag(transitivity));
1016 push(stack, isHere(id, transitivity));
1017 traceBooleanTopValue();
1018 break;
1019 }
1020 case I_NEARBY: {
1021 Abool transitivity = pop(stack);
1022 Aid id = pop(stack);
1023 traceInstruction2ds("NEARBY", id, transitivityFlag(transitivity));
1024 push(stack, isNearby(id, transitivity));
1025 traceBooleanTopValue();
1026 break;
1027 }
1028 case I_NEAR: {
1029 Abool transitivity = pop(stack);
1030 Aint other = pop(stack);
1031 Aid id = pop(stack);
1032 traceInstruction3dds("NEAR", id, other, transitivityFlag(transitivity));
1033 push(stack, isNear(id, other, transitivity));
1034 traceBooleanTopValue();
1035 break;
1036 }
1037 case I_AT: {
1038 Abool transitivity = pop(stack);
1039 Aint other = pop(stack);
1040 Aint instance = pop(stack);
1041 traceInstruction3dds("AT", instance, other, transitivityFlag(transitivity));
1042 push(stack, isAt(instance, other, transitivity));
1043 traceBooleanTopValue();
1044 break;
1045 }
1046 case I_IN: {
1047 Abool transitivity = pop(stack);
1048 Aint cnt = pop(stack);
1049 Aint obj = pop(stack);
1050 traceInstruction3dds("IN", obj, cnt, transitivityFlag(transitivity));
1051 push(stack, isIn(obj, cnt, transitivity));
1052 traceBooleanTopValue();
1053 break;
1054 }
1055 case I_INSET: {
1056 Aptr set = pop(stack);
1057 Aword element = pop(stack);
1058 traceInstruction2("INSET", element, set);
1059 push(stack, inSet((Set*)fromAptr(set), element));
1060 freeSet((Set *)fromAptr(set));
1061 traceBooleanTopValue();
1062 break;
1063 }
1064 case I_USE: {
1065 Aid act = pop(stack);
1066 Aint scr = pop(stack);
1067 traceInstruction2("USE", act, scr);
1068 use(act, scr);
1069 break;
1070 }
1071 case I_STOP: {
1072 Aid actor = pop(stack);
1073 traceInstruction1("STOP", actor);
1074 stop(actor);
1075 break;
1076 }
1077 case I_DESCRIBE: {
1078 Aid id = pop(stack);
1079 if (traceInstructionOption) {
1080 traceInstruction1("DESCRIBE", id);
1081 col = 41; /* To format output better! */
1082 }
1083 describe(id);
1084 break;
1085 }
1086 case I_SAY: {
1087 Aint form = pop(stack);
1088 Aid id = pop(stack);
1089 traceInstruction2sd("SAY", printForm(form), id);
1090 if (form == SAY_SIMPLE)
1091 say(id);
1092 else
1093 sayForm(id, form);
1094 if (traceInstructionOption)
1095 traceEndOfOutput("\"");
1096 break;
1097 }
1098 case I_SAYINT: {
1099 Aword val = pop(stack);
1100 traceInstruction1("SAYINT", val);
1101 sayInteger(val);
1102 if (traceInstructionOption)
1103 traceEndOfOutput("\"");
1104 break;
1105 }
1106 case I_SAYSTR: {
1107 Aptr adr = pop(stack);
1108 traceInstruction1("SAYSTR", adr);
1109 sayString((char *)fromAptr(adr));
1110 if (traceInstructionOption)
1111 traceEndOfOutput("\"");
1112 break;
1113 }
1114 case I_IF: {
1115 Aword v = pop(stack);
1116 traceInstruction1s("IF", booleanValue(v));
1117 interpretIf(v);
1118 break;
1119 }
1120 case I_ELSE: {
1121 traceInstruction0("ELSE");
1122 interpretElse();
1123 break;
1124 }
1125 case I_ENDIF: {
1126 traceInstruction0("ENDIF");
1127 break;
1128 }
1129 case I_AND: {
1130 Aword rh = pop(stack);
1131 Aword lh = pop(stack);
1132 traceInstruction2s("AND", booleanValue(lh), booleanValue(rh));
1133 push(stack, lh && rh);
1134 traceBooleanTopValue();
1135 break;
1136 }
1137 case I_OR: {
1138 Aword rh = pop(stack);
1139 Aword lh = pop(stack);
1140 traceInstruction2s("OR", booleanValue(lh), booleanValue(rh));
1141 push(stack, lh || rh);
1142 traceBooleanTopValue();
1143 break;
1144 }
1145 case I_NE: {
1146 Aword rh = pop(stack);
1147 Aword lh = pop(stack);
1148 traceInstruction2("NE", lh, rh);
1149 push(stack, lh != rh);
1150 traceBooleanTopValue();
1151 break;
1152 }
1153 case I_EQ: {
1154 Aword rh = pop(stack);
1155 Aword lh = pop(stack);
1156 traceInstruction2("EQ", lh, rh);
1157 push(stack, lh == rh);
1158 traceBooleanTopValue();
1159 break;
1160 }
1161 case I_STREQ: {
1162 Aptr rh = pop(stack);
1163 Aptr lh = pop(stack);
1164 traceInstruction2("STREQ", lh, rh);
1165 push(stack, streq((char *)fromAptr(lh), (char *)fromAptr(rh)));
1166 traceBooleanTopValue();
1167 deallocate(fromAptr(lh));
1168 deallocate(fromAptr(rh));
1169 break;
1170 }
1171 case I_STREXACT: {
1172 Aptr rh = pop(stack);
1173 Aptr lh = pop(stack);
1174 traceInstruction2("STREXACT", lh, rh);
1175 push(stack, strcmp((char *)fromAptr(lh), (char *)fromAptr(rh)) == 0);
1176 traceBooleanTopValue();
1177 deallocate(fromAptr(lh));
1178 deallocate(fromAptr(rh));
1179 break;
1180 }
1181 case I_LE: {
1182 Aint rh = pop(stack);
1183 Aint lh = pop(stack);
1184 traceInstruction2("LE", lh, rh);
1185 push(stack, lh <= rh);
1186 traceBooleanTopValue();
1187 break;
1188 }
1189 case I_GE: {
1190 Aint rh = pop(stack);
1191 Aint lh = pop(stack);
1192 traceInstruction2("GE", lh, rh);
1193 push(stack, lh >= rh);
1194 traceBooleanTopValue();
1195 break;
1196 }
1197 case I_LT: {
1198 Aint rh = pop(stack);
1199 Aint lh = pop(stack);
1200 traceInstruction2("LT", lh, rh);
1201 push(stack, lh < rh);
1202 traceBooleanTopValue();
1203 break;
1204 }
1205 case I_GT: {
1206 Aint rh = pop(stack);
1207 Aint lh = pop(stack);
1208 traceInstruction2("GT", lh, rh);
1209 push(stack, lh > rh);
1210 traceBooleanTopValue();
1211 break;
1212 }
1213 case I_PLUS: {
1214 Aint rh = pop(stack);
1215 Aint lh = pop(stack);
1216 traceInstruction2("PLUS", lh, rh);
1217 push(stack, lh + rh);
1218 traceIntegerTopValue();
1219 break;
1220 }
1221 case I_MINUS: {
1222 Aint rh = pop(stack);
1223 Aint lh = pop(stack);
1224 traceInstruction2("MINUS", lh, rh);
1225 push(stack, lh - rh);
1226 traceIntegerTopValue();
1227 break;
1228 }
1229 case I_MULT: {
1230 Aint rh = pop(stack);
1231 Aint lh = pop(stack);
1232 traceInstruction2("MULT", lh, rh);
1233 push(stack, lh * rh);
1234 traceIntegerTopValue();
1235 break;
1236 }
1237 case I_DIV: {
1238 Aint rh = pop(stack);
1239 Aint lh = pop(stack);
1240 traceInstruction2("DIV", lh, rh);
1241 if (rh == 0)
1242 apperr("Division by zero");
1243 push(stack, lh / rh);
1244 traceIntegerTopValue();
1245 break;
1246 }
1247 case I_NOT: {
1248 Aword val = pop(stack);
1249 traceInstruction1s("NOT", booleanValue(val));
1250 push(stack, !val);
1251 traceBooleanTopValue();
1252 break;
1253 }
1254 case I_RND: {
1255 Aint from = pop(stack);
1256 Aint to = pop(stack);
1257 traceInstruction2("RANDOM", from, to);
1258 push(stack, randomInteger(from, to));
1259 traceIntegerTopValue();
1260 break;
1261 }
1262 case I_BTW: {
1263 Aint high = pop(stack);
1264 Aint low = pop(stack);
1265 Aint val = pop(stack);
1266 traceInstruction3("BETWEEN", val, low, high);
1267 push(stack, between(val, low, high));
1268 traceIntegerTopValue();
1269 break;
1270 }
1271
1272 /*------------------------------------------------------------* \
1273 String functions
1274 \*------------------------------------------------------------*/
1275 case I_CONCAT: {
1276 Aptr s2 = pop(stack);
1277 Aptr s1 = pop(stack);
1278 traceInstruction2s("CONCAT", pointerValue(s1), pointerValue(s2));
1279 push(stack, concat(s1, s2));
1280 traceStringTopValue();
1281 deallocate(fromAptr(s1));
1282 deallocate(fromAptr(s2));
1283 break;
1284 }
1285
1286 case I_CONTAINS: {
1287 Aptr substring = pop(stack);
1288 Aptr string = pop(stack);
1289 traceInstruction2s("CONTAINS", pointerValue(string), pointerValue(substring));
1290 push(stack, contains(string, substring));
1291 traceIntegerTopValue();
1292 deallocate(fromAptr(string));
1293 deallocate(fromAptr(substring));
1294 break;
1295 }
1296
1297 case I_STRIP: {
1298 Aint atr = pop(stack);
1299 Aid id = pop(stack);
1300 Aint words = pop(stack);
1301 Aint count = pop(stack);
1302 Aint first = pop(stack);
1303 traceInstruction5("STRIP", first, count, words, id, atr);
1304 push(stack, strip(first, count, words, id, atr));
1305 traceStringTopValue();
1306 break;
1307 }
1308
1309 /*------------------------------------------------------------
1310 Aggregation
1311 ------------------------------------------------------------*/
1312 case I_MIN:
1313 case I_SUM:
1314 case I_MAX: {
1315 Aint attribute = pop(stack);
1316 Aint loopIndex = pop(stack);
1317 Aint limit = pop(stack);
1318 Aint aggregate = pop(stack);
1319 switch (I_OP(i)) {
1320 case I_MAX:
1321 traceInstruction1("MAX", attribute);
1322 if (aggregate < attribute)
1323 push(stack, attribute);
1324 else
1325 push(stack, aggregate);
1326 break;
1327 case I_MIN:
1328 traceInstruction1("MIN", attribute);
1329 if (aggregate > attribute)
1330 push(stack, attribute);
1331 else
1332 push(stack, aggregate);
1333 break;
1334 case I_SUM:
1335 traceInstruction1("SUM", attribute);
1336 push(stack, aggregate + attribute);
1337 break;
1338 }
1339 traceIntegerTopValue();
1340 push(stack, limit);
1341 push(stack, loopIndex);
1342 break;
1343 }
1344 case I_COUNT: {
1345 Aint loopIndex = pop(stack);
1346 Aint limit = pop(stack);
1347 traceInstruction0("COUNT");
1348 push(stack, pop(stack) + 1);
1349 traceIntegerTopValue();
1350 push(stack, limit);
1351 push(stack, loopIndex);
1352 break;
1353 }
1354 case I_TRANSCRIPT: {
1355 Aint on_or_off = pop(stack);
1356 traceInstruction0("TRANSCRIPT");
1357 if (on_or_off)
1358 startTranscript();
1359 else
1360 stopTranscript();
1361 break;
1362 }
1363
1364 /*------------------------------------------------------------
1365 Depending On
1366 ------------------------------------------------------------*/
1367 case I_DEPEND:
1368 traceInstruction0("DEPEND");
1369 break;
1370
1371 case I_DEPCASE:
1372 traceInstruction0("DEPCASE");
1373 depcase();
1374 break;
1375
1376 case I_DEPEXEC: {
1377 traceInstruction0("DEPEXEC");
1378 traceBooleanTopValue();
1379 depexec(pop(stack));
1380 break;
1381 }
1382
1383 case I_DEPELSE:
1384 traceInstruction0("DEPELSE");
1385 depcase();
1386 break;
1387
1388 case I_ENDDEP:
1389 traceInstruction0("ENDDEP");
1390 pop(stack);
1391 break;
1392
1393 case I_ISA: {
1394 Aid rh = pop(stack);
1395 Aid lh = pop(stack);
1396 traceInstruction2("ISA", lh, rh);
1397 push(stack, isA(lh, rh));
1398 traceBooleanTopValue();
1399 break;
1400 }
1401
1402 case I_FRAME: {
1403 Aint size = pop(stack);
1404 traceInstruction1("FRAME", size);
1405 newFrame(stack, size);
1406 break;
1407 }
1408
1409 case I_GETLOCAL: {
1410 Aint framesBelow = pop(stack);
1411 Aint variableNumber = pop(stack);
1412 traceInstruction2("GETLOCAL", framesBelow, variableNumber);
1413 push(stack, getLocal(stack, framesBelow, variableNumber));
1414 traceIntegerTopValue();
1415 break;
1416 }
1417
1418 case I_SETLOCAL: {
1419 Aint framesBelow = pop(stack);
1420 Aint variableNumber = pop(stack);
1421 Aint value = pop(stack);
1422 traceInstruction3("SETLOCAL", framesBelow, variableNumber, value);
1423 setLocal(stack, framesBelow, variableNumber, value);
1424 break;
1425 }
1426
1427 case I_ENDFRAME: {
1428 traceInstruction0("ENDFRAME");
1429 endFrame(stack);
1430 break;
1431 }
1432
1433 case I_LOOP: {
1434 Aint index = pop(stack);
1435 Aint limit = pop(stack);
1436 traceInstruction0("LOOP");
1437 push(stack, limit);
1438 push(stack, index);
1439 if (index > limit)
1440 goToLOOPEND();
1441 break;
1442 }
1443
1444 case I_LOOPNEXT: {
1445 traceInstruction0("LOOPNEXT");
1446 nextLoop();
1447 break;
1448 }
1449
1450 case I_LOOPEND: {
1451 Aint index = pop(stack);
1452 Aint limit = pop(stack);
1453 traceInstruction0("LOOPEND");
1454 endLoop(index, limit);
1455 break;
1456 }
1457
1458 case I_RETURN:
1459 traceInstruction0("RETURN");
1460 if (traceInstructionOption)
1461 printf("\n--------------------------------------------------\n");
1462 pc = oldpc;
1463 goto exitInterpreter;
1464
1465 default:
1466 syserr("Unknown STMOP instruction.");
1467 break;
1468 }
1469 if (fail) {
1470 pc = oldpc;
1471 goto exitInterpreter;
1472 }
1473 if (traceStackOption) {
1474 if (!skipStackDump)
1475 traceStack(stack);
1476 skipStackDump = false;
1477 }
1478 break;
1479
1480 default:
1481 syserr("Unknown instruction class.");
1482 break;
1483 }
1484 }
1485 exitInterpreter:
1486 recursionDepth--;
1487
1488 }
1489
1490 /*======================================================================*/
1491 Aword evaluate(Aaddr adr) {
1492 interpret(adr);
1493 return pop(stack);
1494 }