Debian Codegraph @

terps/hugo/source/heexpr.c

raw symbols SCIP
1 /*
2 HEEXPR.C
3
4 Expression/value management functions:
5
6 EvalExpr
7 Precedence
8 GetValue
9 GetVal
10 Increment
11 IsIncrement
12 SetupExpr
13 TrimExpr
14
15 for the Hugo Engine
16
17 Copyright (c) 1995-2009 by Kent Tessman
18 */
19
20
21 #include "heheader.h"
22
23
24 /* Function prototypes: */
25 int GetVal(void);
26 int Precedence(int t);
27 void TrimExpr(int ptr);
28
29 #if defined (DEBUG_EXPR_EVAL)
30 void PrintExpr(void);
31 int exprt = true;
32 #endif
33
34
35 #define MAX_EVAL_ELEMENTS 256
36
37 int eval[MAX_EVAL_ELEMENTS]; /* expression components */
38 int evalcount; /* # of expr. components */
39 int var[MAXLOCALS+MAXGLOBALS]; /* variables */
40 int incdec; /* value is being incremented/dec. */
41 char getaddress = 0; /* true when finding &routine */
42 char inexpr = 0; /* true when in expression */
43 char inobj = 0; /* true when in object compound */
44
45 int last_precedence;
46
47
48 /* EVALEXPR
49
50 The new-and-improved expression evaluator. Evaluates the current
51 expression (or sub-expression therein) beginning at eval[p].
52 */
53
54 int EvalExpr(int p)
55 {
56 int n1, n2;
57 int oper;
58 short result = 0; /* must be 16 bits */
59
60 /* for precedence stacking/unstacking */
61 int next_prec, this_prec, temp_lp;
62
63 if (!evalcount) return 0; /* no expression */
64
65 do
66 {
67 if (eval[p]==1)
68 {
69 if (eval[p+1]==OPEN_BRACKET_T ||
70 eval[p+1]==OPEN_SQUARE_T)
71 {
72 eval[p] = 0;
73 eval[p+1] = EvalExpr(p+2);
74 TrimExpr(p+2);
75 }
76 else if (eval[p+1]==MINUS_T)
77 {
78 TrimExpr(p);
79 eval[p+1] = -eval[p+1];
80 }
81 }
82
83 if (evalcount<=p+2)
84 {
85 result = eval[p+1];
86 TrimExpr(p);
87 eval[p] = 0;
88 eval[p+1] = result;
89 goto ReturnResult;
90 }
91
92 n1 = eval[p+1];
93 oper = eval[p+3];
94
95 /* At this point, <n1> holds the first value, and <oper> holds
96 the token number of the operator.
97 */
98 if (eval[p+4]==1 && (eval[p+5]==OPEN_BRACKET_T
99 || eval[p+5]==OPEN_SQUARE_T))
100 {
101 eval[p+4] = 0;
102 eval[p+5] = EvalExpr(p+6);
103 TrimExpr(p+6);
104 }
105
106 n2 = eval[p+5];
107
108 if (evalcount > p+7)
109 {
110 if (eval[p+3]==CLOSE_BRACKET_T && eval[p+2]==1)
111 {
112 TrimExpr(p+2);
113 return eval[p+1];
114 }
115
116 /* eval[p+7] holds the next operator, i.e., the "*"
117 in: "x + y * z"
118
119 This way, we can check if the upcoming operator
120 takes precedence over the current one.
121 */
122 if ((next_prec = Precedence(eval[p+7]))
123 < (this_prec = Precedence(oper)))
124 {
125 if (next_prec >= last_precedence)
126 {
127 #if defined (DEBUG_PRECEDENCE)
128 sprintf(line, "Not preferring %s to %s because of previous level %d", token[eval[p+7]], token[oper], last_precedence);
129 Printout(line);
130 #endif
131 goto ReturnResult;
132 }
133
134 #if defined (DEBUG_PRECEDENCE)
135 sprintf(line, "Preferring %s to %s", token[eval[p+7]], token[oper]);
136 Printout(line);
137 #endif
138
139 temp_lp = last_precedence;
140 last_precedence = this_prec;
141 n2 = EvalExpr(p+4);
142 last_precedence = temp_lp;
143 }
144 }
145 else if (Precedence(oper)>=last_precedence)
146 {
147 goto ReturnResult;
148 }
149
150 #if defined (DEBUG_PRECEDENCE)
151 sprintf(line, "Solving %d %s %d", n1, token[oper], n2);
152 Printout(line);
153 #endif
154
155 switch (oper)
156 {
157 case DECIMAL_T:
158 {
159 result = GetProp(n1, n2, 1, 0);
160 break;
161 }
162
163 case EQUALS_T:
164 {
165 result = (n1==n2);
166 break;
167 }
168 case MINUS_T:
169 {
170 result = n1 - n2;
171 break;
172 }
173 case PLUS_T:
174 {
175 result = n1 + n2;
176 break;
177 }
178 case ASTERISK_T:
179 {
180 result = n1 * n2;
181 break;
182 }
183 case FORWARD_SLASH_T:
184 {
185 if (n2==0)
186 #if defined (DEBUGGER)
187 {
188 RuntimeWarning("Division by zero: invalid result");
189 result = 0;
190 }
191 #else
192 FatalError(DIVIDE_E);
193 #endif
194 result = n1 / n2;
195 break;
196 }
197 case PIPE_T:
198 {
199 result = n1 | n2;
200 break;
201 }
202 case GREATER_EQUAL_T:
203 {
204 result = (n1>=n2);
205 break;
206 }
207 case LESS_EQUAL_T:
208 {
209 result = (n1<=n2);
210 break;
211 }
212 case NOT_EQUAL_T:
213 {
214 result = (n1!=n2);
215 break;
216 }
217 case AMPERSAND_T:
218 {
219 result = n1 & n2;
220 break;
221 }
222 case GREATER_T:
223 {
224 result = (n1 > n2);
225 break;
226 }
227 case LESS_T:
228 {
229 result = (n1 < n2);
230 break;
231 }
232 case AND_T:
233 {
234 result = (n1 && n2);
235 break;
236 }
237 case OR_T:
238 {
239 result = (n1 || n2);
240 break;
241 }
242
243 default:
244 {
245 result = n1;
246 }
247 }
248
249 #if defined (DEBUGGER)
250 if ((debug_eval) && debug_eval_error) return 0;
251 #endif
252
253 TrimExpr(p+4); /* second value */
254 TrimExpr(p+2); /* operator */
255
256 eval[p] = 0;
257 eval[p+1] = result;
258
259
260 /* Keep looping while there are expression elements, or until there
261 is a ")", "]", or end of line
262 */
263 } while ((evalcount>p+2) && !(eval[p+2]==1 &&
264 (eval[p+3]==CLOSE_BRACKET_T || eval[p+3]==CLOSE_SQUARE_T ||
265 eval[p+3]==255)));
266
267 result = eval[p+1];
268
269 TrimExpr(p); /* first value */
270
271 ReturnResult:
272
273 #if defined (DEBUG_EXPR_EVAL)
274 if (p==0 && exprt)
275 {
276 sprintf(line, " = %d", result);
277 AP(line);
278 }
279 #endif
280 return result;
281 }
282
283
284 /* GETVAL
285
286 Called by GetValue(); does the actual dirty work of returning
287 a value from a simple data type.
288 */
289
290 int GetVal()
291 {
292 char a = 0;
293 char tempinexpr, tempgetaddress, tempinobj;
294 int i, j;
295 int tempret;
296
297 unsigned short routineaddr, arrayaddr; /* must be 16 bits */
298 short val = 0;
299
300 char inctype = 0;
301 int preincdec; /* pre-increment/decrement */
302
303 defseg = gameseg;
304
305 tempret = ret;
306 tempinexpr = inexpr;
307 inexpr = 0;
308
309 preincdec = incdec;
310 incdec = 0;
311
312 switch (MEM(codeptr))
313 {
314 case AMPERSAND_T: /* an address */
315 {codeptr++;
316 getaddress = true;
317 val = GetValue();
318 getaddress = false;
319 break;}
320
321 case ROUTINE_T:
322 case CALL_T:
323 {
324 if (MEM(codeptr)==ROUTINE_T)
325 {
326 if (tail_recursion==0 && MEM(codeptr-1)==RETURN_T)
327 {
328 /* We may be able to tail-recurse this return
329 statement if it's simply 'return Routine(...)'
330 */
331 tail_recursion = TAIL_RECURSION_ROUTINE;
332 }
333
334 routineaddr = PeekWord(++codeptr);
335 codeptr += 2;
336
337 if (getaddress)
338 {val = routineaddr;
339 getaddress = false;
340 break;}
341 }
342 else
343 {
344 codeptr++;
345 routineaddr = GetValue();
346 }
347
348 #if defined (DEBUGGER)
349 if (debug_eval)
350 {
351 debug_eval_error = true;
352 val = 0;
353 break;
354 }
355 #endif
356 val = CallRoutine(routineaddr);
357
358 break;
359 }
360
361 case OPEN_BRACKET_T:
362 {
363 codeptr++;
364 inexpr = 1;
365 tempgetaddress = getaddress;
366 getaddress = false;
367 SetupExpr();
368 inexpr = 0;
369 val = EvalExpr(0);
370 getaddress = tempgetaddress;
371 break;
372 }
373
374 case MINUS_T:
375 {
376 codeptr++;
377 j = inexpr; /* don't reuse tempinexpr */
378 inexpr = 1;
379 val = -GetValue();
380 inexpr = (char)j;
381 break;
382 }
383
384 case VALUE_T: /* integer 0 - 65535 */
385 case OBJECTNUM_T:
386 case DICTENTRY_T:
387 {
388 val = PeekWord(++codeptr);
389 codeptr += 2;
390 break;
391 }
392
393 case ATTR_T:
394 case PROP_T:
395 {
396 val = MEM(++codeptr);
397 codeptr++;
398 break;
399 }
400
401 case VAR_T: /* variable */
402 {
403 val = var[(i=MEM(++codeptr))];
404
405 if (game_version >= 22)
406 {
407 /* Pre-v2.4 included linelength and pagelength as
408 global variables after objectcount
409 */
410 if (i <= ((game_version>=24)?objectcount:objectcount+2))
411 {
412 if (i==wordcount) val = words;
413 else if (i==objectcount) val = objects;
414
415 /* i.e., pre-v2.4 only */
416 else if (i==objectcount+1)
417 {
418 #if defined (ACTUAL_LINELENGTH)
419 val = ACTUAL_LINELENGTH();
420 #else
421 val = SCREENWIDTH/charwidth;
422 #endif
423 }
424 else if (i==objectcount+2)
425 val = SCREENHEIGHT/lineheight;
426 }
427 }
428 codeptr++;
429
430 if (!inobj) inctype = IsIncrement(codeptr);
431
432 /* don't operate on, e.g., ++variable.property as
433 (++variable).property
434 */
435 if ((incdec || preincdec) && MEM(codeptr)!=DECIMAL_T)
436 {
437 if (i < MAXGLOBALS) SaveUndo(VAR_T, i, val, 0, 0);
438
439 if (inctype) val = Increment(val, inctype);
440
441 /* still a post-increment hanging around */
442 var[i] = (val+=preincdec) + incdec;
443
444 incdec = preincdec = 0;
445 }
446
447 break;
448 }
449
450 case TRUE_T:
451 val = 1;
452 codeptr++;
453 break;
454
455 case FALSE_T:
456 val = 0;
457 codeptr++;
458 break;
459
460 case TILDE_T:
461 codeptr++;
462 val = ~GetValue();
463 break;
464
465 case NOT_T:
466 codeptr++;
467 val = !GetValue();
468 break;
469
470 case ARRAYDATA_T:
471 case ARRAY_T:
472 {
473 unsigned int element;
474
475 if (MEM(codeptr)==ARRAY_T)
476 {
477 codeptr++;
478 arrayaddr = GetValue();
479 }
480 else
481 {
482 arrayaddr = PeekWord(++codeptr);
483 codeptr += 2;
484 }
485
486 if (MEM(codeptr)!=OPEN_SQUARE_T)
487 {val = arrayaddr;
488 break;}
489
490 if (game_version>=22)
491 {
492 /* convert to word value */
493 arrayaddr*=2;
494
495 if (game_version>=23)
496 /* space for array length */
497 a = 2;
498 }
499
500 /* check if this is array[] (i.e., array length) */
501 if (MEM(++codeptr)==CLOSE_SQUARE_T)
502 {
503 defseg = arraytable;
504 val = PeekWord(arrayaddr);
505 codeptr++;
506 break;
507 }
508
509 tempinobj = inobj;
510 inobj = 0;
511 j = GetValue();
512 inobj = tempinobj;
513
514 /* The array element we're after: */
515 element = arrayaddr+a + j*2;
516
517 defseg = arraytable;
518 #if defined (DEBUGGER)
519 CheckinRange(element, debug_workspace, "array data");
520 #endif
521 /* Check to make sure we've got a sane element number */
522 if ((element>0) && (element < (unsigned int)(dicttable-arraytable)*16))
523 val = PeekWord(element);
524 else
525 val = 0;
526 codeptr++;
527
528 if (!inobj) inctype = IsIncrement(codeptr);
529
530 /* Don't operate on the array on:
531
532 ++a[n].property
533 */
534 if ((incdec || preincdec) && MEM(codeptr)!=DECIMAL_T)
535 {
536 /* Same sanity check for element number */
537 if ((element>0) && (element < (unsigned)(dicttable-arraytable)*16))
538 {
539 if (inctype) val = Increment(val, inctype);
540
541 /* still a post-increment hanging around */
542 SaveUndo(ARRAYDATA_T, arrayaddr+a, j, val, 0);
543
544 PokeWord(element, (val+=preincdec) + incdec);
545
546 incdec = preincdec = 0;
547 }
548 }
549
550 break;
551 }
552
553 case RANDOM_T:
554 {
555 codeptr += 2; /* skip the "(" */
556 val = GetValue();
557 if (val!=0)
558 #if !defined (RANDOM)
559 val = (rand() % val)+1;
560 #else
561 val = (RANDOM() % val)+1;
562 #endif
563 if (MEM(codeptr)==2) codeptr++;
564 break;
565 }
566
567 case WORD_T:
568 {
569 codeptr += 2; /* skip the "[" */
570
571 if (MEM(codeptr)==CLOSE_SQUARE_T) /* words[] */
572 {
573 val = words;
574 break;
575 }
576
577 val = wd[GetValue()];
578 if (MEM(codeptr)==CLOSE_SQUARE_T) codeptr++;
579 break;
580 }
581
582 case CHILDREN_T:
583 {
584 codeptr += 2; /* skip the "(" */
585 val = GetValue();
586 if (MEM(codeptr)==CLOSE_BRACKET_T) codeptr++;
587 val = Children(val);
588 break;
589 }
590
591 case PARENT_T:
592 case SIBLING_T:
593 case CHILD_T:
594 case YOUNGEST_T:
595 case ELDEST_T:
596 case YOUNGER_T:
597 case ELDER_T:
598 {
599 i = MEM(codeptr);
600 codeptr += 2; /* skip the "(" */
601 val = GetValue();
602 if (MEM(codeptr)==CLOSE_BRACKET_T) codeptr++;
603
604 switch (i)
605 {
606 case PARENT_T:
607 val = Parent(val);
608 break;
609
610 case SIBLING_T:
611 case YOUNGER_T:
612 val = Sibling(val);
613 break;
614
615 case CHILD_T:
616 case ELDEST_T:
617 val = Child(val);
618 break;
619
620 case YOUNGEST_T:
621 val = Youngest(val);
622 break;
623
624 case ELDER_T:
625 val = Elder(val);
626 break;
627 }
628 break;
629 }
630
631 case SAVE_T:
632 val = RunSave();
633 codeptr++;
634 break;
635
636 case RESTORE_T:
637 val = RunRestore();
638 codeptr++;
639 break;
640
641 case SCRIPTON_T:
642 case SCRIPTOFF_T:
643 val = RunScriptSet();
644 codeptr++;
645 break;
646
647 case RESTART_T:
648 val = RunRestart();
649 codeptr++;
650 break;
651
652 case STRING_T:
653 val = RunString();
654 break;
655
656 case UNDO_T:
657 val = Undo();
658 codeptr++;
659 break;
660
661 case DICT_T:
662 val = Dict();
663 if (MEM(codeptr)==CLOSE_BRACKET_T) codeptr++;
664 break;
665
666 case RECORDON_T:
667 case RECORDOFF_T:
668 case PLAYBACK_T:
669 val = RecordCommands();
670 codeptr++;
671 break;
672
673 case READVAL_T:
674 {
675 val = 0;
676 if (ioblock)
677 {
678 int low, high;
679
680 if ((ioblock==1)
681 || (low = fgetc(io))==EOF
682 || (high = fgetc(io))==EOF)
683 /* fscanf() caused trouble for non-ASCII values
684 (fscanf(io, "%c%c", &a, &b)==EOF) */
685 {
686 ioerror = true;
687 retflag = true;
688 }
689 else
690 {
691 i = low + high*256;
692 #if !defined (MATH_16BIT)
693 if (i > 32767)
694 i -= 65536;
695 #endif
696 val = i;
697 }
698 }
699 codeptr++;
700 break;
701 }
702
703 case PARSE_T:
704 {
705 val = (short)PARSE_STRING_VAL;
706 codeptr++;
707 break;
708 }
709
710 case SERIAL_T:
711 {
712 val = (short)SERIAL_STRING_VAL;
713 codeptr++;
714 break;
715 }
716
717 case SYSTEM_T:
718 {
719 val = RunSystem();
720 codeptr++;
721 break;
722 }
723
724 default:
725 {
726 #if defined (DEBUGGER)
727 if (debug_eval)
728 debug_eval_error = true;
729 else
730 #endif
731
732 FatalError(EXPECT_VAL_E);
733
734 #if defined (DEBUGGER)
735 runtime_error = true;
736 codeptr++;
737 return 0;
738 #endif
739 }
740 }
741 defseg = gameseg;
742 ret = tempret;
743 inexpr = tempinexpr;
744
745 incdec = preincdec;
746
747 return val;
748 }
749
750
751 /* GETVALUE
752
753 Does any reckoning for more sophisticated constructions.
754 */
755
756 int GetValue()
757 {
758 char noself = 0;
759 int p, n;
760 char inctype; int preincdec;
761 int nattr = 0, attr;
762 unsigned int pa, val;
763 long tempptr;
764 short g; /* must be 16 bits */
765 int potential_tail_recursion = 0;
766
767 /* Check to see if this may be a valid tail-recursion */
768 if (tail_recursion==0 && MEM(codeptr-1)==RETURN_T)
769 {
770 /* We may be able to tail-recurse this return statement if
771 it's simply 'return object.property[.property...]'
772 */
773 potential_tail_recursion = TAIL_RECURSION_PROPERTY;
774 }
775
776 IsIncrement(codeptr); /* check for ++, -- */
777
778 tempptr = codeptr;
779 g = GetVal();
780
781 preincdec = incdec;
782 incdec = 0;
783
784 if (inobj==0)
785 {
786 switch (MEM(codeptr))
787 {
788 case DECIMAL_T: /* object.property */
789 {
790 DetermineProperty:
791 if (MEM(++codeptr)==DECIMAL_T) /* object..property */
792 {
793 noself = true;
794 codeptr++;
795 }
796
797 if (MEM(codeptr)==POUND_T) /* object.#property */
798 {
799 codeptr++;
800 inobj = true;
801 p = GetValue();
802 inobj = false;
803 pa = PropAddr(g, p, 0);
804 if (pa)
805 {
806 defseg = proptable;
807 g = Peek(pa + 1);
808 if (g==PROP_ROUTINE) g = 1;
809 defseg = gameseg;
810 }
811 else
812 g = 0;
813 }
814 else
815 {
816 inobj = true;
817 p = GetValue();
818 inobj = false;
819
820 if (MEM(codeptr) != POUND_T)
821 n = 1;
822
823 else /* object.property #x */
824 {
825 codeptr++;
826
827 /* Not GetValue(), since that might
828 botch "obj.property #n is attr"
829 */
830 n = GetVal();
831 }
832
833 /* We checked this at the start of the function, but
834 GetValue() for the property would've cleared it
835 */
836 tail_recursion = potential_tail_recursion;
837
838 val = GetProp(g, p, n, noself);
839
840 inctype = IsIncrement(codeptr);
841
842 /* Increment/decrement an object.property, although
843 only if this is the last property in, e.g.,
844 object.property.property...
845 */
846 if ((incdec || preincdec) && MEM(codeptr)!=DECIMAL_T)
847 {
848 SaveUndo(PROP_T, g, p, n, val);
849
850 if (inctype) val = Increment(val, inctype);
851
852 /* Still a post-increment hanging around */
853 pa = PropAddr(g, p, 0);
854 defseg = proptable;
855
856 /* Only change it if not a routine */
857 if (Peek(pa+1)!=PROP_ROUTINE)
858 PokeWord(pa+n*2, (val+=preincdec)+incdec);
859
860 defseg = gameseg;
861
862 incdec = preincdec = 0;
863 }
864 g = val;
865 }
866 if (MEM(codeptr)==IS_T) goto CheckAttribute;
867
868 break;
869 }
870
871 case IS_T:
872 {
873 CheckAttribute:
874 if (!inobj)
875 {
876 codeptr++;
877 if (MEM(codeptr)==NOT_T)
878 {
879 nattr = 1;
880 codeptr++;
881 }
882 attr = GetValue();
883 #if defined (DEBUGGER)
884 CheckinRange((unsigned)attr, (unsigned)attributes, "attribute");
885 #endif
886 g = TestAttribute(g, attr, nattr);
887
888 break;
889 }
890 }
891 }
892
893 switch (MEM(codeptr))
894 {
895 /* This comes here (again) in order to process
896 object.property1.property2...
897 */
898 case DECIMAL_T: goto DetermineProperty;
899
900 case NOT_T:
901 if (!inobj)
902 {nattr = 1;
903 codeptr++;}
904 /* fall through */
905 case IN_T:
906 {
907 if (!inobj)
908 {
909 codeptr++;
910 p = GetValue(); /* testing parent */
911 g = (p==Parent(g));
912 if (nattr)
913 g = !g;
914 }
915 }
916 }
917 } /* end of "if (inobj==0)" */
918
919 n = MEM(codeptr);
920
921 /* See if we have an implicit expression that needs to be
922 taken as a single value, i.e., "n + 1" where we've just
923 read n
924 */
925 if (((n>=MINUS_T && n<=PIPE_T) || n==AMPERSAND_T) &&
926 ((!inexpr)) && !inobj)
927 /*
928 #if !defined (DEBUGGER)
929 ((!inexpr)) && !inobj)
930 #else
931 ((!inexpr)) && !inobj && !debug_eval)
932 #endif
933 */
934 {
935 inexpr = 2;
936 codeptr = tempptr;
937 SetupExpr();
938 g = EvalExpr(0);
939 inexpr = 0;
940 }
941
942 /* Not a tail-recursive 'return object.property' */
943 if (tail_recursion_addr==0)
944 tail_recursion = 0;
945
946 return g;
947 }
948
949
950 /* INCREMENT
951
952 Actually performs the increment given below by IsIncrement.
953 */
954
955 int Increment(int a, char inctype)
956 {
957 short v; /* must be 16 bits */
958
959 v = a;
960
961 switch (inctype)
962 {
963 case MINUS_T: {v -= incdec; break;}
964 case PLUS_T: {v += incdec; break;}
965 case ASTERISK_T: {v *= incdec; break;}
966 case AMPERSAND_T: {v &= incdec; break;}
967 case PIPE_T: {v |= incdec; break;}
968 case FORWARD_SLASH_T:
969 {
970 #if defined (DEBUGGER)
971 if (incdec==0)
972 {
973 RuntimeWarning("Division by zero: invalid result");
974 v = 0;
975 }
976 else
977 #endif
978 v /= incdec;
979 break;
980 }
981 }
982
983 if (inctype!=1) incdec = 0;
984
985 return v;
986 }
987
988
989 /* ISINCREMENT
990
991 If an increment/decrement is next up (i.e. ++, --, or +=, *=, etc.),
992 then sets incdec equal to the increment/decrement and repositions
993 codeptr. Returns the token number of the operation, if any.
994 */
995
996 char IsIncrement(long addr)
997 {
998 unsigned char a, t = 0;
999
1000 incdec = 0;
1001
1002 switch (a = MEM(addr))
1003 {
1004 case MINUS_T:
1005 case PLUS_T:
1006 case ASTERISK_T:
1007 case FORWARD_SLASH_T:
1008 case AMPERSAND_T:
1009 case PIPE_T:
1010 {
1011 /* ++, -- */
1012 if ((a==MINUS_T || a==PLUS_T) && MEM(addr+1)==a)
1013 {
1014 codeptr = addr + 2;
1015 if (a==PLUS_T) incdec = 1;
1016 else incdec = -1;
1017 t = 1;
1018 break;
1019 }
1020
1021 /* +=, -=, etc. */
1022 else if (MEM(addr+1)==EQUALS_T)
1023 {
1024 codeptr = addr + 2;
1025 incdec = GetValue();
1026 t = a;
1027 }
1028 }
1029 }
1030
1031 #if defined (DEBUGGER)
1032 if (t && debug_eval)
1033 {
1034 debug_eval_error = true;
1035 sprintf(debug_line, "'%s%s' illegal in watch/assignment", token[a], token[MEM(addr+1)]);
1036 DebugMessageBox("Expression Error", debug_line);
1037 t = 0;
1038 }
1039 #endif
1040 return t;
1041 }
1042
1043
1044 /* PRECEDENCE
1045
1046 Returns the precedence ranking of the operator represented by
1047 token[t]. The lower the return value, the higher the rank in
1048 terms of processing order.
1049 */
1050
1051 int Precedence(int t)
1052 {
1053 switch (t)
1054 {
1055 case DECIMAL_T:
1056 return 1;
1057
1058 case ASTERISK_T:
1059 case FORWARD_SLASH_T:
1060 return 2;
1061
1062 case MINUS_T:
1063 case PLUS_T:
1064 return 3;
1065
1066 case PIPE_T:
1067 case TILDE_T:
1068 case AMPERSAND_T:
1069 return 4;
1070
1071 case EQUALS_T:
1072 case GREATER_EQUAL_T:
1073 case LESS_EQUAL_T:
1074 case NOT_EQUAL_T:
1075 case GREATER_T:
1076 case LESS_T:
1077 return 5;
1078
1079 default:
1080 return 6;
1081 }
1082 }
1083
1084
1085 /* PRINTEXPR
1086
1087 Prints the current expression during expression tracing.
1088 */
1089
1090 #if defined (DEBUG_EXPR_EVAL)
1091 void PrintExpr(void)
1092 {
1093 char e[261];
1094 int i, bracket = 0;
1095
1096 if (!evalcount) return;
1097
1098 strcpy(e, "( ");
1099 for (i=0; i<=evalcount; i+=2)
1100 {
1101 switch (eval[i])
1102 {
1103 case 0:
1104 {
1105 sprintf(line, "%d ", eval[i + 1]);
1106 strcat(e, line);
1107 break;
1108 }
1109 case 1:
1110 {
1111 if (eval[i+1]==OPEN_BRACKET_T) bracket++;
1112 if (eval[i+1]==CLOSE_BRACKET_T)
1113 {bracket--;
1114 if (bracket<0) goto ExitPrintExpr;}
1115
1116 if (token[eval[i+1]][0]=='~')
1117 strcat(e, "\\");
1118 if (eval[i+1] != 255)
1119 {sprintf(line, "%s ", token[eval[i+1]]);
1120 strcat(e, line);}
1121 break;
1122 }
1123 }
1124 }
1125
1126 ExitPrintExpr:
1127 strcat(e, ")\\;");
1128
1129 AP(e);
1130 }
1131 #endif
1132
1133
1134 /* SETUPEXPR
1135
1136 Reads the current expression from the current code position
1137 into eval[], using the following key:
1138
1139 if eval[n] is 0, eval[n+1] is a value
1140 if eval[n] is 1, eval[n+1] is a token
1141
1142 <inexpr> is used in various routines to keep track of
1143 whether or not we're currently reading an expression. If
1144 <inexpr> is 1, we're in an expression; if 2, we may have
1145 to step back one code position if encountering a closing
1146 parentheses.
1147 */
1148
1149 void SetupExpr(void)
1150 {
1151 char justgotvalue = 1;
1152 int j, t, bracket = 0;
1153 int tempret;
1154 int tempeval[MAX_EVAL_ELEMENTS];
1155 int tempevalcount;
1156
1157 last_precedence = 10;
1158
1159 tempret = ret;
1160 tempevalcount = 0;
1161
1162 inobj = false;
1163 if (!inexpr) inexpr = 1;
1164
1165 do
1166 {
1167 justgotvalue++;
1168
1169 switch (t = MEM(codeptr))
1170 {
1171 /* Various indications that we've hit the
1172 end of the expression:
1173 */
1174 case EOL_T:
1175 arrexpr = false;
1176 /* fall through */
1177 case COMMA_T:
1178 multiprop = false;
1179 /* fall through */
1180 case SEMICOLON_T:
1181 case CLOSE_SQUARE_T:
1182 case JUMP_T:
1183 {
1184 if (t==EOL_T || t==COMMA_T || t==JUMP_T)
1185 codeptr++;
1186 LeaveSetupExpr:
1187 for (j=0; j<tempevalcount; j++)
1188 eval[j] = tempeval[j];
1189 evalcount = tempevalcount;
1190
1191 eval[evalcount] = 1;
1192 eval[evalcount + 1] = 255;
1193
1194 #if defined (DEBUG_EXPR_EVAL)
1195 if (exprt) PrintExpr();
1196 #endif
1197
1198 ret = tempret;
1199 return;
1200 }
1201
1202
1203 /* Otherwise we have a value: */
1204
1205 case OPEN_BRACKET_T:
1206
1207 case MINUS_T:
1208 case PLUS_T:
1209 case TILDE_T:
1210 case AMPERSAND_T:
1211 case NOT_T:
1212
1213 case PARENT_T:
1214 case SIBLING_T:
1215 case CHILD_T:
1216 case YOUNGEST_T:
1217 case ELDEST_T:
1218 case YOUNGER_T:
1219 case ELDER_T:
1220 case CHILDREN_T:
1221 case RANDOM_T:
1222 case SYSTEM_T:
1223
1224 case PROP_T:
1225 case ATTR_T:
1226 case VAR_T:
1227 case DICTENTRY_T:
1228 case ROUTINE_T:
1229 case OBJECTNUM_T:
1230 case VALUE_T:
1231
1232 case ARRAYDATA_T:
1233 case ARRAY_T:
1234 case WORD_T:
1235
1236 case CALL_T:
1237
1238 case SAVE_T:
1239 case RESTORE_T:
1240 case SCRIPTON_T:
1241 case SCRIPTOFF_T:
1242 case RESTART_T:
1243 case UNDO_T:
1244 case READVAL_T:
1245
1246 case STRING_T:
1247 case DICT_T:
1248
1249 case PARSE_T:
1250 case SERIAL_T:
1251 {
1252 if ((t==AMPERSAND_T || t==MINUS_T ||
1253 t==PLUS_T) && justgotvalue==1)
1254 goto SomeSymbolorToken;
1255
1256 tempeval[tempevalcount] = 0;
1257 tempeval[tempevalcount + 1] = GetValue();
1258
1259 #if defined (DEBUGGER)
1260 if ((debug_eval) && debug_eval_error)
1261 return;
1262 #endif
1263
1264 tempevalcount += 2;
1265 if (tempevalcount > MAX_EVAL_ELEMENTS-2)
1266 FatalError(OVERFLOW_E);
1267
1268 justgotvalue = 0;
1269
1270 break;
1271 }
1272
1273 /* Logical constants */
1274 case TRUE_T:
1275 case FALSE_T:
1276 {
1277 tempeval[tempevalcount] = 0;
1278 if (Peek(codeptr)==TRUE_T)
1279 tempeval[tempevalcount + 1] = 1;
1280 else
1281 tempeval[tempevalcount + 1] = 0;
1282
1283 codeptr++;
1284
1285 tempevalcount += 2;
1286 if (tempevalcount > MAX_EVAL_ELEMENTS-2)
1287 FatalError(OVERFLOW_E);
1288
1289 break;
1290 }
1291
1292 /* Some symbol or token */
1293 default:
1294 {
1295 SomeSymbolorToken:
1296 tempeval[tempevalcount] = 1;
1297 tempeval[tempevalcount + 1] = MEM(codeptr++);
1298
1299 tempevalcount += 2;
1300 if (tempevalcount > MAX_EVAL_ELEMENTS-2)
1301 FatalError(OVERFLOW_E);
1302
1303 switch (MEM(codeptr-1))
1304 {
1305 case OPEN_BRACKET_T:
1306 {bracket++;
1307 break;}
1308 case CLOSE_BRACKET_T:
1309 {bracket--;
1310 justgotvalue = 0;
1311 if (inexpr==2)
1312 codeptr--;}
1313 }
1314 if (bracket < 0) goto LeaveSetupExpr;
1315
1316 break;
1317 }
1318 }
1319 }
1320 while (true); /* endless loop */
1321 }
1322
1323
1324 /* TRIMEXPR
1325
1326 Cuts off straggling components of eval[] after an expression or
1327 sub-expression has been successfully evaluated.
1328 */
1329
1330 void TrimExpr(int ptr)
1331 {
1332 int i;
1333
1334 for (i=ptr; i<=evalcount; i+=2)
1335 {
1336 eval[i] = eval[i+2];
1337 eval[i+1] = eval[i+3];
1338 }
1339 evalcount -= 2;
1340 }