Debian Codegraph @

terps/alan2/inter.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 inter.c
4
5 Interpreter unit for Alan interpreter Arun
6
7 \*----------------------------------------------------------------------*/
8
9
10 #include <stdio.h>
11
12 #include "types.h"
13 #include "main.h"
14 #include "parse.h"
15 #include "exe.h"
16 #include "stack.h"
17 #include "sysdep.h"
18
19 #include "inter.h"
20
21 #ifdef GLK
22 #include "glkio.h"
23 #endif
24
25 /* PRIVATE DATA */
26
27 static int pc;
28
29
30 #ifdef _PROTOTYPES_
31 static void if_(
32 Aword v
33 )
34 #else
35 static void if_(v)
36 Aword v;
37 #endif
38 {
39 int lev = 1;
40 Aword i;
41
42 if (!v) {
43 /* Skip to next ELSE or ENDIF on same level */
44 while (TRUE) {
45 i = memory[pc++];
46 if (I_CLASS(i) == (Aword)C_STMOP)
47 switch (I_OP(i)) {
48 case I_ELSE:
49 if (lev == 1) return;
50 break;
51 case I_IF:
52 lev++;
53 break;
54 case I_ENDIF:
55 lev--;
56 if (lev == 0) return;
57 break;
58 }
59 }
60 }
61 }
62
63
64 #ifdef _PROTOTYPES_
65 static void else_(void)
66 #else
67 static void else_()
68 #endif
69 {
70 int lev = 1;
71 Aword i;
72
73 while (TRUE) {
74 /* Skip to ENDIF on the same level */
75 i = memory[pc++];
76 if (I_CLASS(i) == (Aword)C_STMOP)
77 switch (I_OP(i)) {
78 case I_ENDIF:
79 lev--;
80 if (lev == 0) return;
81 break;
82 case I_IF:
83 lev++;
84 break;
85 }
86 }
87 }
88
89
90 #ifdef _PROTOTYPES_
91 static void depstart(void)
92 #else
93 static void depstart(void)
94 #endif
95 {
96 /* A DEPSTART was executed so skip across the redundant DEPCASE to
97 start at the first expression */
98 pc++;
99 }
100
101
102 #ifdef _PROTOTYPES_
103 static void swap(void)
104 #else
105 static void swap()
106 #endif
107 {
108 Aptr v1 = pop();
109 Aptr v2 = pop();
110
111 push(v1);
112 push(v2);
113 }
114
115
116
117 #ifdef _PROTOTYPES_
118 static void depexec(
119 Aword v
120 )
121 #else
122 static void depexec(v)
123 Aword v;
124 #endif
125 {
126 int lev = 1;
127 Aword i;
128
129 if (!v)
130 /* The expression was not true, skip to next CASE on the same
131 level which could be a DEPCASE or DEPELSE */
132 while (TRUE) {
133 i = memory[pc++];
134 if (I_CLASS(i) == (Aword)C_STMOP)
135 switch (I_OP(i)) {
136 case I_DEPSTART:
137 lev++;
138 break;
139 case I_DEPEND:
140 if (lev == 1) return;
141 lev--;
142 break;
143 case I_DEPCASE:
144 case I_DEPELSE:
145 if (lev == 1) return;
146 break;
147 }
148 }
149 }
150
151
152 #ifdef _PROTOTYPES_
153 static void depcase(void)
154 #else
155 static void depcase()
156 #endif
157 {
158 int lev = 1;
159 Aword i;
160
161 /* Skip to end of DEPENDING block (next DEPEND on same level) because
162 we have just executed a DEPCASE/DEPELSE statement as a result of a DEPCASE
163 catching */
164
165 while (TRUE) {
166 i = memory[pc++];
167 if (I_CLASS(i) == (Aword)C_STMOP)
168 switch (I_OP(i)) {
169 case I_DEPSTART:
170 lev++;
171 break;
172 case I_DEPEND:
173 lev--;
174 if (lev == 0) return;
175 break;
176 }
177 }
178 }
179
180
181 #ifdef _PROTOTYPES_
182 void interpret(Aaddr adr)
183 #else
184 void interpret(adr)
185 Aaddr adr;
186 #endif
187 {
188 Aaddr oldpc;
189 Aword i;
190
191 if (stpflg) printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++");
192
193 oldpc = pc;
194 pc = adr;
195 while(TRUE) {
196 if (stpflg) printf("\n%4x: ", pc);
197 if (pc > memTop)
198 syserr("Interpreting outside program.");
199
200 i = memory[pc++];
201
202 switch (I_CLASS(i)) {
203 case C_CONST:
204 if (stpflg) printf("PUSH \t%5ld", I_OP(i));
205 push(I_OP(i));
206 break;
207 case C_CURVAR:
208 switch (I_OP(i)) {
209 case V_PARAM:
210 if (stpflg) printf("PARAM \t%5ld\t\t(%ld)", top(), params[top()-1].code);
211 push(params[pop()-1].code);
212 break;
213 case V_CURLOC:
214 if (stpflg) printf("CURLOC \t\t\t(%d)", cur.loc);
215 push(cur.loc);
216 break;
217 case V_CURACT:
218 if (stpflg) printf("CURACT \t\t\t(%d)", cur.act);
219 push(cur.act);
220 break;
221 case V_CURVRB:
222 if (stpflg) printf("CURVRB \t\t\t(%d)", cur.vrb);
223 push(cur.vrb);
224 break;
225 case V_SCORE:
226 if (stpflg) printf("CURSCORE \t\t\t(%d)", cur.score);
227 push(cur.score);
228 break;
229 default:
230 syserr("Unknown CURVAR instruction.");
231 break;
232 }
233 break;
234
235 case C_STMOP:
236 switch (I_OP(i)) {
237 case I_PRINT: {
238 Aptr fpos, len;
239 fpos = pop();
240 len = pop();
241 if (stpflg) {
242 printf("PRINT \t%5ld, %5ld\t\"", fpos, len);
243 col = 34; /* To format it better! */
244 }
245 print(fpos, len);
246 if (stpflg)
247 printf("\"");
248 break;
249 }
250 case I_SYSTEM: {
251 Aptr fpos, len;
252 fpos = pop();
253 len = pop();
254 if (stpflg) {
255 printf("SYSTEM \t%5ld, %5ld\t\"", fpos, len);
256 col = 34; /* To format it better! */
257 }
258 sys(fpos, len);
259 break;
260 }
261 case I_GETSTR: {
262 Aptr fpos, len;
263 fpos = pop();
264 len = pop();
265 if (stpflg)
266 printf("GETSTR\t%5ld, %5ld", fpos, len);
267 getstr(fpos, len);
268 if (stpflg)
269 printf("\t(%ld)", top());
270 break;
271 }
272 case I_QUIT: {
273 if (stpflg)
274 printf("QUIT");
275 quit();
276 break;
277 }
278 case I_LOOK: {
279 if (stpflg)
280 printf("LOOK");
281 look();
282 break;
283 }
284 case I_SAVE: {
285 if (stpflg)
286 printf("SAVE");
287 save();
288 break;
289 }
290 case I_RESTORE: {
291 if (stpflg)
292 printf("RESTORE");
293 restore();
294 break;
295 }
296 case I_RESTART: {
297 if (stpflg)
298 printf("RESTART");
299 restart();
300 break;
301 }
302 case I_LIST: {
303 Aptr cnt;
304 cnt = pop();
305 if (stpflg)
306 printf("LIST \t%5ld", cnt);
307 list(cnt);
308 break;
309 }
310 case I_EMPTY: {
311 Aptr cnt, whr;
312 cnt = pop();
313 whr = pop();
314 if (stpflg)
315 printf("EMPTY \t%5ld, %5ld", cnt, whr);
316 empty(cnt, whr);
317 break;
318 }
319 case I_SCORE: {
320 Aptr sc;
321 sc = pop();
322 if (stpflg)
323 printf("SCORE \t%5ld\t\t(%ld)", sc, scores[sc-1]);
324 score(sc);
325 break;
326 }
327 case I_VISITS: {
328 Aptr v;
329 v = pop();
330 if (stpflg)
331 printf("VISITS \t%5ld", v);
332 visits(v);
333 break;
334 }
335 case I_SCHEDULE: {
336 Aptr evt, whr, aft;
337 evt = pop();
338 whr = pop();
339 aft = pop();
340 if (stpflg)
341 printf("SCHEDULE \t%5ld, %5ld, %5ld", evt, whr, aft);
342 schedule(evt, whr, aft);
343 break;
344 }
345 case I_CANCEL: {
346 Aptr evt;
347 evt = pop();
348 if (stpflg)
349 printf("CANCEL \t%5ld", evt);
350 cancl(evt);
351 break;
352 }
353 case I_MAKE: {
354 Aptr id, atr, val;
355 id = pop();
356 atr = pop();
357 val = pop();
358 if (stpflg) {
359 printf("MAKE \t%5ld, %5ld, ", id, atr);
360 if (val) printf("TRUE"); else printf("FALSE");
361 }
362 make(id, atr, val);
363 break;
364 }
365 case I_SET: {
366 Aptr id, atr, val;
367 id = pop();
368 atr = pop();
369 val = pop();
370 if (stpflg) {
371 printf("SET \t%5ld, %5ld, %5ld", id, atr, val);
372 }
373 set(id, atr, val);
374 break;
375 }
376 case I_STRSET: {
377 Aptr id, atr, str;
378 id = pop();
379 atr = pop();
380 str = pop();
381 if (stpflg) {
382 printf("STRSET\t%5ld, %5ld, %5ld", id, atr, str);
383 }
384 setstr(id, atr, str);
385 break;
386 }
387 case I_INCR: {
388 Aptr id, atr, step;
389 id = pop();
390 atr = pop();
391 step = pop();
392 if (stpflg) {
393 printf("INCR\t%5ld, %5ld, %5ld", id, atr, step);
394 }
395 incr(id, atr, step);
396 break;
397 }
398 case I_DECR: {
399 Aptr id, atr, step;
400 id = pop();
401 atr = pop();
402 step = pop();
403 if (stpflg) {
404 printf("DECR\t%5ld, %5ld, %5ld", id, atr, step);
405 }
406 decr(id, atr, step);
407 break;
408 }
409 case I_ATTRIBUTE: {
410 Aptr id, atr;
411 id = pop();
412 atr = pop();
413 if (stpflg)
414 printf("ATTRIBUTE %5ld, %5ld", id, atr);
415 push(attribute(id, atr));
416 if (stpflg)
417 printf("\t(%ld)", top());
418 break;
419 }
420 case I_STRATTR: {
421 Aptr id, atr;
422 id = pop();
423 atr = pop();
424 if (stpflg)
425 printf("STRATTR \t%5ld, %5ld", id, atr);
426 push(strattr(id, atr));
427 if (stpflg)
428 printf("\t(%ld)", top());
429 break;
430 }
431 case I_LOCATE: {
432 Aptr id, whr;
433 id = pop();
434 whr = pop();
435 if (stpflg)
436 printf("LOCATE \t%5ld, %5ld", id, whr);
437 locate(id, whr);
438 break;
439 }
440 case I_WHERE: {
441 Aptr id;
442 id = pop();
443 if (stpflg)
444 printf("WHERE \t%5ld", id);
445 push(where(id));
446 if (stpflg)
447 printf("\t\t(%ld)", top());
448 break;
449 }
450 case I_HERE: {
451 Aptr id;
452 id = pop();
453 if (stpflg)
454 printf("HERE \t%5ld", id);
455 push(isHere(id));
456 if (stpflg)
457 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
458 break;
459 }
460 case I_NEAR: {
461 Aptr id;
462 id = pop();
463 if (stpflg)
464 printf("NEAR \t%5ld", id);
465 push(isNear(id));
466 if (stpflg)
467 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
468 break;
469 }
470 case I_USE: {
471 Aptr act, scr;
472 act = pop();
473 scr = pop();
474 if (stpflg)
475 printf("USE \t%5ld, %5ld", act, scr);
476 use(act, scr);
477 break;
478 }
479 case I_IN: {
480 Aptr obj, cnt;
481 obj = pop();
482 cnt = pop();
483 if (stpflg)
484 printf("IN \t%5ld, %5ld ", obj, cnt);
485 push(in(obj, cnt));
486 if (stpflg)
487 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
488 break;
489 }
490 case I_DESCRIBE: {
491 Aptr id;
492 id = pop();
493 if (stpflg) {
494 printf("DESCRIBE \t%5ld\t", id);
495 col = 34; /* To format it better! */
496 }
497 describe(id);
498 break;
499 }
500 case I_SAY: {
501 Aptr id;
502 id = pop();
503 if (stpflg)
504 printf("SAY \t%5ld\t\t\"", id);
505 say(id);
506 if (stpflg)
507 printf("\"");
508 break;
509 }
510 case I_SAYINT: {
511 Aptr val;
512 val = pop();
513 if (stpflg)
514 printf("SAYINT\t%5ld\t\t\"", val);
515 sayint(val);
516 if (stpflg)
517 printf("\"");
518 break;
519 }
520 case I_SAYSTR: {
521 Aptr adr;
522 adr = pop();
523 if (stpflg)
524 printf("SAYSTR\t%5ld\t\t\"", adr);
525 saystr((char *)adr);
526 if (stpflg)
527 printf("\"");
528 break;
529 }
530 case I_IF: {
531 Aptr v;
532 v = pop();
533 if (stpflg) {
534 printf("IF \t");
535 if (v) printf(" TRUE"); else printf("FALSE");
536 }
537 if_(v);
538 break;
539 }
540 case I_ELSE: {
541 if (stpflg)
542 printf("ELSE");
543 else_();
544 break;
545 }
546 case I_ENDIF: {
547 if (stpflg)
548 printf("ENDIF");
549 break;
550 }
551 case I_AND: {
552 Aptr lh, rh;
553 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
554 swap();
555 rh = pop();
556 lh = pop();
557 if (stpflg) {
558 printf("AND \t");
559 if (lh) printf("TRUE, "); else printf("FALSE, ");
560 if (rh) printf("TRUE"); else printf("FALSE");
561 }
562 push(lh && rh);
563 if (stpflg)
564 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
565 break;
566 }
567 case I_OR: {
568 Aptr lh, rh;
569 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
570 swap();
571 rh = pop();
572 lh = pop();
573 if (stpflg) {
574 printf("OR \t");
575 if (lh) printf("TRUE, "); else printf("FALSE, ");
576 if (rh) printf("TRUE"); else printf("FALSE");
577 }
578 push(lh || rh);
579 if (stpflg)
580 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
581 break;
582 }
583 case I_NE: {
584 Aptr lh, rh;
585 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
586 swap();
587 rh = pop();
588 lh = pop();
589 if (stpflg)
590 printf("NE \t%5ld, %5ld", lh, rh);
591 push(lh != rh);
592 if (stpflg)
593 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
594 break;
595 }
596 case I_EQ: {
597 Aptr lh, rh;
598 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
599 swap();
600 rh = pop();
601 lh = pop();
602 if (stpflg)
603 printf("EQ \t%5ld, %5ld", lh, rh);
604 push(lh == rh);
605 if (stpflg)
606 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
607 break;
608 }
609 case I_STREQ: {
610 Aptr lh, rh;
611 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
612 swap();
613 rh = pop();
614 lh = pop();
615 if (stpflg)
616 printf("STREQ \t%5ld, %5ld", lh, rh);
617 push(streq((char *)lh, (char *)rh));
618 if (stpflg)
619 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
620 break;
621 }
622 case I_STREXACT: {
623 Aptr lh, rh;
624 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
625 swap();
626 rh = pop();
627 lh = pop();
628 if (stpflg)
629 printf("STREXACT \t%5ld, %5ld", lh, rh);
630 push(strcmp((char *)lh, (char *)rh) == 0);
631 if (stpflg)
632 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
633 free((void *)lh);
634 free((void *)rh);
635 break;
636 }
637 case I_LE: {
638 Aint lh, rh;
639 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
640 swap();
641 rh = pop();
642 lh = pop();
643 if (stpflg)
644 printf("LE \t%5ld, %5ld", lh, rh);
645 push(lh <= rh);
646 if (stpflg)
647 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
648 break;
649 }
650 case I_GE: {
651 Aint lh, rh;
652 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
653 swap();
654 rh = pop();
655 lh = pop();
656 if (stpflg)
657 printf("GE \t%5ld, %5ld", lh, rh);
658 push(lh >= rh);
659 if (stpflg)
660 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
661 break;
662 }
663 case I_LT: {
664 Aint lh, rh;
665 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
666 swap();
667 rh = pop();
668 lh = pop();
669 if (stpflg)
670 printf("LT \t%5ld, %5ld", lh, rh);
671 push((signed int)lh < (signed int)rh);
672 if (stpflg)
673 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
674 break;
675 }
676 case I_GT: {
677 Aint lh, rh;
678 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
679 swap();
680 rh = pop();
681 lh = pop();
682 if (stpflg)
683 printf("GT \t%5ld, %5ld", lh, rh);
684 push(lh > rh);
685 if (stpflg)
686 if (top()) printf("\t(TRUE)"); else printf("\t(FALSE)");
687 break;
688 }
689 case I_PLUS: {
690 Aint lh, rh;
691 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
692 swap();
693 rh = pop();
694 lh = pop();
695 if (stpflg)
696 printf("PLUS \t%5ld, %5ld", lh, rh);
697 push(lh + rh);
698 if (stpflg)
699 printf("\t(%ld)", top());
700 break;
701 }
702 case I_MINUS: {
703 Aint lh, rh;
704 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
705 swap();
706 rh = pop();
707 lh = pop();
708 if (stpflg)
709 printf("MINUS \t%5ld, %5ld", lh, rh);
710 push(lh - rh);
711 if (stpflg)
712 printf("\t(%ld)", top());
713 break;
714 }
715 case I_MULT: {
716 Aint lh, rh;
717 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
718 swap();
719 rh = pop();
720 lh = pop();
721 if (stpflg)
722 printf("MULT \t%5ld, %5ld", lh, rh);
723 push(lh * rh);
724 if (stpflg)
725 printf("\t(%ld)", top());
726 break;
727 }
728 case I_DIV: {
729 Aint lh, rh;
730 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
731 swap();
732 rh = pop();
733 lh = pop();
734 if (stpflg)
735 printf("DIV \t%5ld, %5ld", lh, rh);
736 push(lh / rh);
737 if (stpflg)
738 printf("\t(%ld)", top());
739 break;
740 }
741 case I_NOT: {
742 Aptr val;
743 val = pop();
744 if (stpflg) {
745 printf("NOT \t");
746 if (val) printf("TRUE"); else printf("FALSE");
747 }
748 push(!val);
749 if (stpflg)
750 if (top()) printf("\t\t(TRUE)"); else printf("\t\t(FALSE)");
751 break;
752 }
753 case I_MAX: {
754 Aptr atr, whr;
755 atr = pop();
756 whr = pop();
757 if (stpflg)
758 printf("MAX \t%5ld, %5ld", atr, whr);
759 push(agrmax(atr, whr));
760 if (stpflg)
761 printf("\t(%ld)", top());
762 break;
763 }
764 case I_SUM: {
765 Aptr atr, whr;
766 atr = pop();
767 whr = pop();
768 if (stpflg)
769 printf("SUM \t%5ld, %5ld", atr, whr);
770 push(agrsum(atr, whr));
771 if (stpflg)
772 printf("\t(%ld)", top());
773 break;
774 }
775 case I_COUNT: {
776 Aptr whr;
777 whr = pop();
778 if (stpflg)
779 printf("COUNT \t%5ld", whr);
780 push(agrcount(whr));
781 if (stpflg)
782 printf("\t(%ld)", top());
783 break;
784 }
785 case I_RND: {
786 Aptr from, to;
787 from = pop();
788 to = pop();
789 if (stpflg)
790 printf("RANDOM \t%5ld, %5ld", from, to);
791 push(rnd(from, to));
792 if (stpflg)
793 printf("\t(%ld)", top());
794 break;
795 }
796 case I_BTW: {
797 Aint low, high, val;
798 high = pop();
799 low = pop();
800 val = pop();
801 if (stpflg)
802 printf("BETWEEN \t%5ld, %5ld, %5ld", val, low, high);
803 push(btw(val, low, high));
804 if (stpflg)
805 printf("\t(%ld)", top());
806 break;
807 }
808 case I_CONTAINS: {
809 Aptr string, substring;
810 substring = pop();
811 string = pop();
812 if (stpflg)
813 printf("CONTAINS \t%5ld, %5ld", string, substring);
814 push(contains(string, substring));
815 if (stpflg)
816 printf("\t(%ld)", top());
817 break;
818 }
819
820 case I_DEPSTART:
821 if (stpflg)
822 printf("DEPSTART");
823 depstart();
824 break;
825
826 case I_DEPCASE:
827 if (stpflg)
828 printf("DEPCASE");
829 depcase();
830 break;
831
832 case I_DEPEXEC: {
833 Aptr v;
834 v = pop();
835 if (stpflg) {
836 printf("DEPEXEC \t");
837 if (v) printf(" TRUE"); else printf("FALSE");
838 }
839 depexec(v);
840 break;
841 }
842
843 case I_DEPELSE:
844 if (stpflg)
845 printf("DEPELSE");
846 depcase();
847 break;
848
849 case I_DEPEND:
850 if (stpflg)
851 printf("DEPEND");
852 break;
853
854 case I_RETURN:
855 if (stpflg)
856 printf("RETURN\n--------------------------------------------------\n");
857 pc = oldpc;
858 return;
859
860 default:
861 syserr("Unknown STMOP instruction.");
862 break;
863 }
864 if (fail) {
865 pc = oldpc;
866 return;
867 }
868 break;
869
870 default:
871 syserr("Unknown instruction class.");
872 break;
873 }
874 }
875 }