Debian Codegraph @

terps/hugo/source/heset.c

raw symbols SCIP
1 /*
2 HESET.C
3
4 contains:
5 RunSet
6 SetCompound
7
8 for the Hugo Engine
9
10 Copyright (c) 1995-2009 by Kent Tessman
11 */
12
13
14 #include "heheader.h"
15
16 /* Function prototypes: */
17 static unsigned int GetAnonymousFunction(long addr);
18 static int SetCompound();
19
20 #define MAX_GAME_TITLE 64
21 char game_title[MAX_GAME_TITLE] = "";
22
23 char arrexpr = 0; /* true when assigning array */
24 char multiprop = 0; /* true in multiple prop. assign. */
25
26 static int set_value = 0;
27
28
29 /* RUNSET
30
31 If gotvalue is passed as -1, then no value has already been
32 as the (potential) object, etc. comprising the first part of the
33 object.property, for example, to be set.
34 */
35
36 void RunSet(int gotvalue)
37 {
38 char inc = 0; /* increment/decrement */
39 char temparrexpr, propval = 0;
40 int a = 0, t = 0, obj = 0;
41 int newl = 0; /* new length */
42 int newp = 0; /* new property val */
43 unsigned int element = 0; /* of an array */
44
45 unsigned short n, m, v; /* must be 16 bits */
46
47 inobj = 0;
48
49 if (gotvalue!=-1)
50 {
51 obj = gotvalue;
52 t = SetCompound();
53 goto StoreVal;
54 }
55
56 t = MEM(codeptr);
57
58 switch (t)
59 {
60 case OBJECTNUM_T:
61 {
62 codeptr++;
63 obj = PeekWord(codeptr);
64 codeptr += 2;
65 t = SetCompound();
66 break;
67 }
68
69 case VAR_T:
70 {
71 a = MEM(codeptr + 1);
72
73 /* Check for ++, --, +=, etc. */
74 inc = IsIncrement(codeptr+2);
75
76 if (MEM(codeptr + 2)==EQUALS_T || inc)
77 {
78 if (a < MAXGLOBALS) SaveUndo(VAR_T, a, var[a], 0, 0);
79
80 /* anonymous function */
81 if (!inc && MEM(codeptr+3)==EOL_T)
82 {
83 var[a] = GetAnonymousFunction(codeptr+4);
84 return;
85 }
86
87 if (inc)
88 {
89 var[a] = (Increment(var[a], inc)) + incdec;
90
91 /* backward-compatibility tweak */
92 if ((game_version<23) && MEM(codeptr)!=CLOSE_BRACE_T) codeptr--;
93
94 codeptr++; /* eol */
95 }
96 else
97 {
98 codeptr += 3;
99 inexpr = 1;
100 SetupExpr();
101 inexpr = 0;
102 v = EvalExpr(0);
103 var[a] = v;
104 }
105
106 /* If a global variable */
107 if (a < MAXGLOBALS)
108 {
109 if (a==wordcount) words = var[wordcount];
110 }
111
112 return;
113 }
114
115 obj = var[a];
116 codeptr += 2;
117 t = SetCompound();
118
119 break;
120 }
121
122 case WORD_T: /* "word" */
123 {
124 codeptr += 2; /* skip "[" */
125 n = GetValue();
126 #if defined (DEBUGGER)
127 if ((debug_eval) && debug_eval_error)
128 return;
129 #endif
130 codeptr += 2; /* skip "] =" */
131 inexpr = 1;
132 SetupExpr();
133 inexpr = 0;
134
135 GetNextWord:
136 if (n >= MAXWORDS) n = MAXWORDS-1;
137 SaveUndo(WORD_T, n, wd[n], 0, 0);
138 wd[n] = EvalExpr(0);
139
140 if (MEM(codeptr)==COMMA_T)
141 {
142 codeptr++;
143 n++;
144 goto GetNextWord;
145 }
146
147 /* Have to (rather unfortunately) rebuild the entire
148 input buffer and word array here
149 */
150 strcpy(buffer, "");
151 t = 0;
152 for (a=1; a<=(int)MAXWORDS; a++)
153 {
154 if ((unsigned short)wd[a]!=UNKNOWN_WORD)
155 strcpy(buffer+t, GetWord(wd[a]));
156 else
157 itoa(parsed_number, buffer+t, 10);
158 word[a] = buffer + t;
159 t+=strlen(word[a])+1;
160 }
161
162 if (n>(unsigned)var[wordcount])
163 var[wordcount] = n;
164
165 return;
166 }
167
168 case ARRAYDATA_T:
169 case ARRAY_T:
170 {
171 char af_flag = false;
172 /* array[n]... */
173 if (t==ARRAYDATA_T)
174 {
175 m = PeekWord(codeptr + 1);
176 codeptr += 4; /* "[" */
177 n = GetValue();
178 #if defined (DEBUGGER)
179 if ((debug_eval) && debug_eval_error)
180 return;
181 #endif
182 codeptr++; /* "]" */
183 }
184
185 /* ...or array val[n] */
186 else
187 {
188 codeptr++;
189 m = GetValue();
190 #if defined (DEBUGGER)
191 if ((debug_eval) && debug_eval_error)
192 return;
193 #endif
194 codeptr++; /* "[" */
195 n = GetValue();
196 #if defined (DEBUGGER)
197 if ((debug_eval) && debug_eval_error)
198 return;
199 #endif
200 codeptr++; /* "]" */
201 }
202
203 if (game_version>=22)
204 {
205 /* Convert to word value */
206 m*=2;
207
208 if (game_version>=23)
209 /* Space for array length */
210 a = 2;
211 }
212
213 #if defined (DEBUGGER)
214 CheckinRange(m+a+n*2, debug_workspace, "array data");
215 #endif
216 /* Check for ++, --, +=, etc. */
217 inc = IsIncrement(codeptr);
218 if (inc)
219 {
220 defseg = arraytable;
221 v = PeekWord(m+a+n*2);
222 defseg = gameseg;
223 v = (Increment(v, inc)) + incdec;
224
225 codeptr++; /* eol */
226
227 element = m+a+n*2;
228
229 goto WriteArrayValue;
230 }
231
232 if (MEM(codeptr)==EQUALS_T)
233 {
234 codeptr++;
235
236 do
237 {
238 element = m+a+n*2;
239
240 temparrexpr = arrexpr;
241 arrexpr = true;
242
243 /* anonymous function */
244 if (!inc && MEM(codeptr)==EOL_T)
245 {
246 v = GetAnonymousFunction(codeptr+1);
247 af_flag = true;
248 }
249 else
250 {
251 v = GetValue();
252 }
253 #if defined (DEBUGGER)
254 if ((debug_eval) && debug_eval_error)
255 return;
256 #endif
257 if (arrexpr==false && MEM(codeptr-1)==76)
258 codeptr--;
259 arrexpr = temparrexpr;
260
261 if (!af_flag && (MEM(codeptr)==COMMA_T || MEM(codeptr)==CLOSE_BRACKET_T))
262 codeptr++;
263 WriteArrayValue:
264 defseg = arraytable;
265
266 /* Make sure the value to be written is within range */
267 if ((element>0) && (element < (unsigned)(dicttable-arraytable)*16))
268 {
269 SaveUndo(ARRAYDATA_T, m+a, n, PeekWord(element), 0);
270
271 PokeWord(element, (unsigned int)v);
272 }
273
274 defseg = gameseg;
275
276 if (inc || af_flag) return;
277
278 n++;
279 }
280 while (MEM(codeptr)!=EOL_T);
281
282 codeptr++;
283 return;
284 }
285
286 defseg = arraytable;
287 obj = PeekWord((unsigned int)(m+a + n*2));
288 defseg = gameseg;
289 t = SetCompound();
290
291 break;
292 }
293 }
294
295
296 StoreVal:
297
298 /* Now store the evaluated expression in the appropriate place... */
299
300 /*
301 t = 1: property
302 t = 2: attribute
303 t = 3: not attribute
304 t = 4: property reference
305 */
306
307 n = 1;
308
309 if (t==4)
310 {
311 inobj = true;
312 n = GetValue();
313
314 #if defined (DEBUGGER)
315 if ((debug_eval) && debug_eval_error)
316 return;
317 #endif
318 inobj = false;
319
320 LoopBack:
321 if (MEM(codeptr)==IS_T || MEM(codeptr)==DECIMAL_T)
322 {
323 obj = GetProp(obj, set_value, n, 0);
324 t = SetCompound();
325 goto LoopBack;
326 }
327 /* Don't set t = 1 if it changed above before going back
328 to LoopBack:
329 */
330 else if (t==4)
331 t = 1; /* Just a property */
332 }
333 else if (t==1)
334 {
335 while (MEM(codeptr)==IS_T || MEM(codeptr)==DECIMAL_T)
336 {
337 obj = GetProp(obj, set_value, n, 0);
338 t = SetCompound();
339 }
340 }
341
342 switch (t)
343 {
344 case 1:
345 {
346 incdec = 0;
347
348 if (MEM(codeptr) != EQUALS_T)
349 {
350 /* Check for ++, --, +=, etc. */
351 if (!(inc = IsIncrement(codeptr)))
352 {
353 #if defined (DEBUGGER)
354 if (debug_eval)
355 {
356 debug_eval_error = true;
357 return;
358 }
359 #endif
360 FatalError(ILLEGAL_OP_E);
361 }
362 else if (MEM(codeptr)==EOL_T)
363 {
364 goto GetNextPropVal;
365 }
366 }
367 else
368 codeptr++;
369
370 /* Property routine (anonymous function)... */
371 if (MEM(codeptr)==EOL_T)
372 {
373 /* m = skipptr to the end of the property
374 routine block (i.e., the next statement
375 following it)
376 */
377 m = PeekWord(codeptr + 1);
378
379 newl = PROP_ROUTINE;
380 newp =(unsigned int)(((codeptr + 4)+(codeptr + 4)%address_scale)/address_scale);
381 codeptr = (long)m*address_scale;
382 m = PropAddr(obj, set_value, 0);
383 }
384
385 /* ...or not */
386 else
387 {
388 GetNextPropVal:
389 inexpr = false;
390 temparrexpr = multiprop;
391 multiprop = true;
392 propval = true;
393 if (!inc) newp = GetValue();
394 #if defined (DEBUGGER)
395 if ((debug_eval) && debug_eval_error)
396 return;
397 #endif
398
399 if (!multiprop)
400 codeptr--;
401 multiprop = temparrexpr;
402
403 m = PropAddr(obj, set_value, 0);
404 if (m)
405 {
406 defseg = proptable;
407 newl = Peek((unsigned int)m + 1);
408 if (newl==PROP_ROUTINE) newl = 1;
409 }
410
411 /* Deal with setting built-in display object
412 properties
413 */
414 else if ((obj==display_object) && n==1)
415 {
416 if (set_value==title_caption)
417 {
418 strncpy(game_title, GetWord(newp), MAX_GAME_TITLE - 1);
419 hugo_setgametitle(game_title);
420 }
421 else if (set_value==needs_repaint)
422 {
423 display_needs_repaint = (char)newp;
424 }
425 }
426 #if defined (DEBUGGER)
427 /*
428 else if (runtime_warnings)
429 {
430 RuntimeWarning("Setting non-existent property");
431 }
432 */
433 #endif
434 }
435
436 /* Write property obj.z = newl words of newp */
437
438 if (m && (int)n <= 0)
439 {
440 #if defined (DEBUGGER)
441 RuntimeWarning("Property element <= 0");
442 #endif
443 if (inc) codeptr++;
444 }
445 else if (m && (int)n <= newl)
446 {
447 defseg = proptable;
448
449 #if defined (DEBUGGER)
450 CheckinRange((unsigned)n, (unsigned)Peek(m+1), "property element");
451 #endif
452 /* Check to make sure this property value is within range */
453 if ((unsigned)(m+2+(n-1)*2)<(unsigned)(eventtable-proptable)*16)
454 {
455 SaveUndo(PROP_T, obj, (unsigned int)set_value, n, PeekWord((unsigned int)(m+2+(n-1)*2)));
456
457 /* Save the (possibly changed) length) */
458 Poke((unsigned int)m + 1, (unsigned char)newl);
459
460 /* An assignment such as obj.prop++ or
461 obj.prop += ...
462 */
463 if (inc)
464 {
465 PokeWord((unsigned int)(m+2+(n-1)*2), Increment(PeekWord((unsigned int)(m+2+(n-1)*2)), inc) + incdec);
466 codeptr++; /* eol */
467 }
468
469 /* A regular obj.prop = ... assignment */
470 else
471 PokeWord((unsigned int)(m+2+(n-1)*2), newp);
472 }
473 }
474 else if (inc) codeptr++; /* eol */
475
476 defseg = gameseg;
477
478 if (inc) return;
479
480 if (propval && MEM(codeptr)==COMMA_T)
481 {n++;
482 codeptr++;
483 goto GetNextPropVal;}
484
485 if (propval) codeptr++;
486 if (MEM(codeptr)==EOL_T) codeptr++;
487 return;
488 }
489
490 case 2:
491 case 3:
492 {
493 ModifyAttribute:
494
495 #if defined (DEBUGGER)
496 CheckinRange((unsigned int)set_value, (unsigned)attributes, "attribute");
497 #endif
498 SaveUndo(ATTR_T, obj, (unsigned int)set_value, TestAttribute(obj, (unsigned int)set_value, 0), 0);
499
500 SetAttribute(obj, set_value, (t==2));
501 t = 2; /* reset after 'not' */
502
503 if (MEM(codeptr++)==EOL_T) return;
504
505 /* Allow multiple attributes, comma-separated */
506 if (MEM(codeptr)==COMMA_T)
507 codeptr++;
508
509 if (MEM(codeptr)==NOT_T)
510 {
511 t = 3;
512 codeptr++;
513 }
514
515 set_value = GetValue();
516 goto ModifyAttribute;
517 }
518
519 default:
520 {
521 #if defined (DEBUGGER)
522 if (debug_eval)
523 {
524 debug_eval_error = true;
525 return;
526 }
527 #endif
528 /* Not any sort of variable data type */
529 FatalError(ILLEGAL_OP_E);
530 }
531 }
532 }
533
534
535 /* GETANONYMOUSFUNCTION */
536
537 static unsigned int GetAnonymousFunction(long addr)
538 {
539 long skipaddr;
540 unsigned int af_addr;
541
542 skipaddr = PeekWord(addr);
543 /* The address of the anonymous function is the next address boundary,
544 calculated as:
545 (((addr+2)/address_scale+1)*address_scale)/address_scale */
546 af_addr =(unsigned int)((addr+2)/address_scale+1);
547 codeptr = (long)skipaddr*address_scale;
548 return af_addr;
549 }
550
551
552 /* SETCOMPOUND */
553
554 static int SetCompound()
555 {
556 if (Peek(codeptr)==DECIMAL_T) /* obj.property */
557 {
558 codeptr++;
559 inobj = 1;
560 set_value = GetValue(); /* the prop. # */
561 inobj = 0;
562
563 if (Peek(codeptr)==POUND_T) /* if obj.prop #... */
564 {
565 codeptr++;
566 return 4;
567 }
568 return 1;
569 }
570
571 if (Peek(codeptr)==IS_T) /* obj is ... */
572 {
573 inobj = 1;
574 if (Peek(codeptr+1)==NOT_T)
575 {
576 codeptr += 2;
577 set_value = GetValue(); /* the attr. # */
578 inobj = 0;
579 return 3;
580 }
581
582 codeptr++;
583 set_value = GetValue(); /* the attr. # */
584 inobj = 0;
585 return 2;
586 }
587
588 #if defined (DEBUGGER)
589 if (debug_eval)
590 debug_eval_error = true;
591 else
592 #endif
593
594 FatalError(ILLEGAL_OP_E);
595
596 return 0;
597 }
598