-
Notifications
You must be signed in to change notification settings - Fork 35
/
expressp.c
2306 lines (2024 loc) · 82 KB
/
expressp.c
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ------------------------------------------------------------------------- */
/* "expressp" : The expression parser */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
/* --- Interface to lexer -------------------------------------------------- */
static char separators_to_operators[103];
static char conditionals_to_operators[7];
static char token_type_allowable[301];
#define NOT_AN_OPERATOR (char) 0x7e
static void make_lexical_interface_tables(void)
{ int i;
for (i=0;i<103;i++)
separators_to_operators[i] = NOT_AN_OPERATOR;
for (i=0;i<NUM_OPERATORS;i++)
if (operators[i].token_type == SEP_TT)
separators_to_operators[operators[i].token_value] = i;
for (i=0;i<7;i++) /* 7 being the size of keyword_group "conditions" */
conditionals_to_operators[i] = NOT_AN_OPERATOR;
for (i=0;i<NUM_OPERATORS;i++)
if (operators[i].token_type == CND_TT)
conditionals_to_operators[operators[i].token_value] = i;
for (i=0;i<301;i++) token_type_allowable[i] = 0;
token_type_allowable[VARIABLE_TT] = 1;
token_type_allowable[SYSFUN_TT] = 1;
token_type_allowable[DQ_TT] = 1;
token_type_allowable[DICTWORD_TT] = 1;
token_type_allowable[SUBOPEN_TT] = 1;
token_type_allowable[SUBCLOSE_TT] = 1;
token_type_allowable[SMALL_NUMBER_TT] = 1;
token_type_allowable[LARGE_NUMBER_TT] = 1;
token_type_allowable[ACTION_TT] = 1;
token_type_allowable[SYSTEM_CONSTANT_TT] = 1;
token_type_allowable[OP_TT] = 1;
}
static token_data current_token, previous_token, heldback_token;
static int comma_allowed, arrow_allowed, superclass_allowed,
bare_prop_allowed,
array_init_ambiguity, action_ambiguity,
etoken_count, inserting_token, bracket_level;
int system_function_usage[NUMBER_SYSTEM_FUNCTIONS];
static void check_system_constant_available(int);
static int get_next_etoken(void)
{ int v, symbol = 0, mark_symbol_as_used = FALSE,
initial_bracket_level = bracket_level;
etoken_count++;
if (inserting_token)
{ current_token = heldback_token;
inserting_token = FALSE;
}
else
{ get_next_token();
current_token.text = token_text;
current_token.value = token_value;
current_token.type = token_type;
current_token.marker = 0;
current_token.symindex = -1;
current_token.symtype = 0;
current_token.symflags = -1;
}
switch(current_token.type)
{ case LOCAL_VARIABLE_TT:
current_token.type = VARIABLE_TT;
variables[current_token.value].usage = TRUE;
break;
case DQ_TT:
current_token.marker = STRING_MV;
break;
case SQ_TT:
{ int32 unicode = text_to_unicode(token_text);
if (token_text[textual_form_length] == 0)
{
if (!glulx_mode) {
current_token.value = unicode_to_zscii(unicode);
if (current_token.value == 5)
{ unicode_char_error("Character can be printed \
but not used as a value:", unicode);
current_token.value = '?';
}
if (current_token.value >= 0x100)
current_token.type = LARGE_NUMBER_TT;
else current_token.type = SMALL_NUMBER_TT;
}
else {
current_token.value = unicode;
if (current_token.value >= 0x8000
|| current_token.value < -0x8000)
current_token.type = LARGE_NUMBER_TT;
else current_token.type = SMALL_NUMBER_TT;
}
}
else
{ current_token.type = DICTWORD_TT;
current_token.marker = DWORD_MV;
}
}
break;
case SYMBOL_TT:
ReceiveSymbol:
symbol = current_token.value;
mark_symbol_as_used = TRUE;
v = symbols[symbol].value;
current_token.symindex = symbol;
current_token.symtype = symbols[symbol].type;
current_token.symflags = symbols[symbol].flags;
switch(symbols[symbol].type)
{ case ROUTINE_T:
/* Replaced functions must always be backpatched
because there could be another definition coming. */
if (symbols[symbol].flags & REPLACE_SFLAG)
{ current_token.marker = SYMBOL_MV;
v = symbol;
break;
}
current_token.marker = IROUTINE_MV;
break;
case GLOBAL_VARIABLE_T:
current_token.marker = VARIABLE_MV;
break;
case OBJECT_T:
case CLASS_T:
/* All objects must be backpatched in Glulx. */
if (glulx_mode)
current_token.marker = OBJECT_MV;
break;
case ARRAY_T:
current_token.marker = ARRAY_MV;
break;
case STATIC_ARRAY_T:
current_token.marker = STATIC_ARRAY_MV;
break;
case INDIVIDUAL_PROPERTY_T:
break;
case CONSTANT_T:
if (symbols[symbol].flags & (UNKNOWN_SFLAG + CHANGE_SFLAG))
{ current_token.marker = SYMBOL_MV;
v = symbol;
}
else current_token.marker = 0;
break;
case LABEL_T:
error_named("Label name used as value:", token_text);
break;
default:
current_token.marker = 0;
break;
}
if (symbols[symbol].flags & SYSTEM_SFLAG)
current_token.marker = 0;
current_token.value = v;
if (!glulx_mode) {
if (((current_token.marker != 0)
&& (current_token.marker != VARIABLE_MV))
|| (v < 0) || (v > 255))
current_token.type = LARGE_NUMBER_TT;
else current_token.type = SMALL_NUMBER_TT;
}
else {
if (((current_token.marker != 0)
&& (current_token.marker != VARIABLE_MV))
|| (v < -0x8000) || (v >= 0x8000))
current_token.type = LARGE_NUMBER_TT;
else current_token.type = SMALL_NUMBER_TT;
}
if (symbols[symbol].type == GLOBAL_VARIABLE_T)
{ current_token.type = VARIABLE_TT;
variables[current_token.value].usage = TRUE;
}
break;
case NUMBER_TT:
if (!glulx_mode) {
if (current_token.value >= 256)
current_token.type = LARGE_NUMBER_TT;
else
current_token.type = SMALL_NUMBER_TT;
}
else {
if (current_token.value < -0x8000
|| current_token.value >= 0x8000)
current_token.type = LARGE_NUMBER_TT;
else
current_token.type = SMALL_NUMBER_TT;
}
break;
case SEP_TT:
switch(current_token.value)
{ case ARROW_SEP:
if (!arrow_allowed)
current_token.type = ENDEXP_TT;
break;
case COMMA_SEP:
if ((bracket_level==0) && (!comma_allowed))
current_token.type = ENDEXP_TT;
break;
case SUPERCLASS_SEP:
if ((bracket_level==0) && (!superclass_allowed))
current_token.type = ENDEXP_TT;
break;
case GREATER_SEP:
get_next_token();
if ((token_type == SEP_TT)
&&((token_value == SEMICOLON_SEP)
|| (token_value == GREATER_SEP)))
current_token.type = ENDEXP_TT;
put_token_back();
break;
case OPENB_SEP:
bracket_level++;
if (expr_trace_level>=3)
{ printf("Previous token type = %d\n",previous_token.type);
printf("Previous token val = %d\n",previous_token.value);
}
if ((previous_token.type == OP_TT)
|| (previous_token.type == SUBOPEN_TT)
|| (previous_token.type == ENDEXP_TT)
|| (array_init_ambiguity)
|| ((bracket_level == 1) && (action_ambiguity)))
current_token.type = SUBOPEN_TT;
else
{ inserting_token = TRUE;
heldback_token = current_token;
current_token.text = "<call>";
bracket_level--;
}
break;
case CLOSEB_SEP:
bracket_level--;
if (bracket_level < 0)
current_token.type = ENDEXP_TT;
else current_token.type = SUBCLOSE_TT;
break;
case SEMICOLON_SEP:
current_token.type = ENDEXP_TT; break;
case MINUS_SEP:
if ((previous_token.type == OP_TT)
|| (previous_token.type == SUBOPEN_TT)
|| (previous_token.type == ENDEXP_TT))
current_token.value = UNARY_MINUS_SEP;
break;
case INC_SEP:
if ((previous_token.type == VARIABLE_TT)
|| (previous_token.type == SUBCLOSE_TT)
|| (previous_token.type == LARGE_NUMBER_TT)
|| (previous_token.type == SMALL_NUMBER_TT))
current_token.value = POST_INC_SEP;
break;
case DEC_SEP:
if ((previous_token.type == VARIABLE_TT)
|| (previous_token.type == SUBCLOSE_TT)
|| (previous_token.type == LARGE_NUMBER_TT)
|| (previous_token.type == SMALL_NUMBER_TT))
current_token.value = POST_DEC_SEP;
break;
case HASHHASH_SEP:
token_text = current_token.text + 2;
ActionUsedAsConstant:
current_token.type = ACTION_TT;
current_token.text = token_text;
current_token.value = 0;
current_token.marker = ACTION_MV;
break;
case HASHADOLLAR_SEP:
obsolete_warning("'#a$Act' is now superseded by '##Act'");
token_text = current_token.text + 3;
goto ActionUsedAsConstant;
case HASHGDOLLAR_SEP:
/* This form generates the position of a global variable
in the global variables array. So Glob is the same as
#globals_array --> #g$Glob */
current_token.text += 3;
current_token.type = SYMBOL_TT;
symbol = get_symbol_index(current_token.text);
if (symbol < 0 || symbols[symbol].type != GLOBAL_VARIABLE_T) {
ebf_error(
"global variable name after '#g$'",
current_token.text);
current_token.value = 0;
current_token.type = SMALL_NUMBER_TT;
current_token.marker = 0;
break;
}
mark_symbol_as_used = TRUE;
current_token.value = symbols[symbol].value - MAX_LOCAL_VARIABLES;
current_token.marker = 0;
if (!glulx_mode) {
if (current_token.value >= 0x100)
current_token.type = LARGE_NUMBER_TT;
else current_token.type = SMALL_NUMBER_TT;
}
else {
if (current_token.value >= 0x8000
|| current_token.value < -0x8000)
current_token.type = LARGE_NUMBER_TT;
else current_token.type = SMALL_NUMBER_TT;
}
break;
case HASHNDOLLAR_SEP:
/* This form is still needed for constants like #n$a (the
dictionary address of the word "a"), since 'a' means
the ASCII value of 'a' */
if (strlen(token_text) > 4)
obsolete_warning(
"'#n$word' is now superseded by ''word''");
current_token.type = DICTWORD_TT;
current_token.value = 0;
current_token.text = token_text + 3;
current_token.marker = DWORD_MV;
break;
case HASHRDOLLAR_SEP:
/* This form -- #r$Routinename, to return the routine's */
/* packed address -- is needed far less often in Inform 6, */
/* where just giving the name Routine returns the packed */
/* address. But it's used in a lot of Inform 5 code. */
obsolete_warning(
"'#r$Routine' can now be written just 'Routine'");
current_token.text += 3;
current_token.type = SYMBOL_TT;
current_token.value = symbol_index(current_token.text, -1, NULL);
goto ReceiveSymbol;
case HASHWDOLLAR_SEP:
error("The obsolete '#w$word' construct has been removed");
break;
case HASH_SEP:
system_constants.enabled = TRUE;
get_next_token();
system_constants.enabled = FALSE;
if (token_type != SYSTEM_CONSTANT_TT)
{ ebf_curtoken_error(
"'r$', 'n$', 'g$' or internal Inform constant name after '#'");
break;
}
else
{
check_system_constant_available(token_value);
current_token.type = token_type;
current_token.value = token_value;
current_token.text = token_text;
current_token.marker = INCON_MV;
}
break;
}
break;
case CND_TT:
v = conditionals_to_operators[current_token.value];
if (v != NOT_AN_OPERATOR)
{ current_token.type = OP_TT; current_token.value = v;
}
break;
}
if (current_token.type == SEP_TT)
{ v = separators_to_operators[current_token.value];
if (v != NOT_AN_OPERATOR)
{ if ((veneer_mode)
|| ((v!=MESSAGE_OP) && (v!=MPROP_NUM_OP)))
{ current_token.type = OP_TT; current_token.value = v;
if (array_init_ambiguity &&
((v==MINUS_OP) || (v==UNARY_MINUS_OP)) &&
(initial_bracket_level == 0) &&
(etoken_count != 1))
warning("Without bracketing, the minus sign '-' is ambiguous");
}
}
}
/* A feature of Inform making it annoyingly hard to parse left-to-right
is that there is no clear delimiter for expressions; that is, the
legal syntax often includes sequences of expressions with no
intervening markers such as commas. We therefore need to use some
internal context to determine whether an end is in sight... */
if (token_type_allowable[current_token.type]==0)
{ if (expr_trace_level >= 3)
{ printf("Discarding as not allowable: '%s' ", current_token.text);
describe_token(¤t_token);
printf("\n");
}
current_token.type = ENDEXP_TT;
}
else
if ((!((initial_bracket_level > 0)
|| (previous_token.type == ENDEXP_TT)
|| ((previous_token.type == OP_TT)
&& (operators[previous_token.value].usage != POST_U))
|| (previous_token.type == SYSFUN_TT)))
&& ((current_token.type != OP_TT)
|| (operators[current_token.value].usage == PRE_U)))
{ if (expr_trace_level >= 3)
{ printf("Discarding as no longer part: '%s' ", current_token.text);
describe_token(¤t_token);
printf("\n");
}
current_token.type = ENDEXP_TT;
}
else
{ if (mark_symbol_as_used) symbols[symbol].flags |= USED_SFLAG;
if (expr_trace_level >= 3)
{ printf("Expr token = '%s' ", current_token.text);
describe_token(¤t_token);
printf("\n");
}
}
if ((previous_token.type == ENDEXP_TT)
&& (current_token.type == ENDEXP_TT)) return FALSE;
previous_token = current_token;
return TRUE;
}
/* --- Operator precedences and error values-------------------------------- */
#define LOWER_P 101
#define EQUAL_P 102
#define GREATER_P 103
#define BYPREC -1 /* Compare the precedence of two operators */
#define NOVAL_E 1 /* Missing operand error */
#define CLOSEB_E 2 /* Unexpected close bracket */
#define NOOP_E 3 /* Missing operator error */
#define OPENB_E 4 /* Expression ends with an open bracket */
#define ASSOC_E 5 /* Associativity illegal error */
const int prec_table[49] = {
/* a ....... ( ) end op:pre op:bin op:post term */
/* b ( */ LOWER_P, NOOP_E, LOWER_P, LOWER_P, LOWER_P, NOOP_E, NOOP_E,
/* . ) */ EQUAL_P, GREATER_P, CLOSEB_E, GREATER_P, GREATER_P, GREATER_P, GREATER_P,
/* . end */ OPENB_E, GREATER_P, NOVAL_E, GREATER_P, GREATER_P, GREATER_P, GREATER_P,
/* . op:pre */ LOWER_P, NOOP_E, LOWER_P, BYPREC, BYPREC, NOOP_E, NOOP_E,
/* . op:bin */ LOWER_P, GREATER_P, LOWER_P, BYPREC, BYPREC, BYPREC, GREATER_P,
/* . op:post */ LOWER_P, GREATER_P, LOWER_P, BYPREC, BYPREC, BYPREC, GREATER_P,
/* . term */ LOWER_P, NOOP_E, LOWER_P, LOWER_P, LOWER_P, NOOP_E, NOOP_E
};
static int find_prec(const token_data *a, const token_data *b)
{
/* We are comparing the precedence of tokens a and b
(where a occurs to the left of b). If the expression is correct,
the only possible values are GREATER_P, LOWER_P or EQUAL_P;
if it is malformed then one of the *_E results.
Note that this routine is not symmetrical and that the relation
is not trichotomous.
If a and b are equal (and aren't brackets), then
a LOWER_P a if a right-associative
a GREATER_P a if a left-associative
*/
int ai, bi, j, l1, l2;
/* Select a column and row in prec_table, based on the type of
a and b. If a/b is an operator, we have to distinguish three
columns/rows depending on whether the operator is prefix,
postfix, or neither.
*/
switch(a->type)
{ case SUBOPEN_TT: ai=0; break;
case SUBCLOSE_TT: ai=1; break;
case ENDEXP_TT: ai=2; break;
case OP_TT:
if (operators[a->value].usage == PRE_U)
ai=3;
else if (operators[a->value].usage == POST_U)
ai=5;
else
ai=4;
break;
default: ai=6; break;
}
switch(b->type)
{ case SUBOPEN_TT: bi=0; break;
case SUBCLOSE_TT: bi=1; break;
case ENDEXP_TT: bi=2; break;
case OP_TT:
if (operators[b->value].usage == PRE_U)
bi=3;
else if (operators[b->value].usage == POST_U)
bi=5;
else
bi=4;
break;
default: bi=6; break;
}
j = prec_table[ai+7*bi];
if (j != BYPREC) return j;
/* BYPREC is the (a=OP, b=OP) cases. We must compare the precedence of the
two operators.
(We've already eliminated invalid cases like (a++ --b).)
*/
l1 = operators[a->value].precedence;
l2 = operators[b->value].precedence;
if (operators[b->value].usage == PRE_U) return LOWER_P;
if (operators[a->value].usage == POST_U) return GREATER_P;
/* Anomalous rule to resolve the function call precedence, which is
different on the right from on the left, e.g., in:
alpha.beta(gamma)
beta(gamma).alpha
*/
if ((l1 == 11) && (l2 > 11)) return GREATER_P;
if (l1 < l2) return LOWER_P;
if (l1 > l2) return GREATER_P;
switch(operators[a->value].associativity)
{ case L_A: return GREATER_P;
case R_A: return LOWER_P;
case 0: return ASSOC_E;
}
return GREATER_P;
}
/* --- Converting token to operand ----------------------------------------- */
/* List used to generate gameinfo.dbg.
Must match the switch statement below. */
int z_system_constant_list[] =
{ adjectives_table_SC,
actions_table_SC,
classes_table_SC,
identifiers_table_SC,
preactions_table_SC,
largest_object_SC,
strings_offset_SC,
code_offset_SC,
actual_largest_object_SC,
static_memory_offset_SC,
array_names_offset_SC,
readable_memory_offset_SC,
cpv__start_SC,
cpv__end_SC,
ipv__start_SC,
ipv__end_SC,
array__start_SC,
array__end_SC,
highest_attribute_number_SC,
attribute_names_array_SC,
highest_property_number_SC,
property_names_array_SC,
highest_action_number_SC,
action_names_array_SC,
highest_fake_action_number_SC,
fake_action_names_array_SC,
highest_routine_number_SC,
routine_names_array_SC,
routines_array_SC,
routine_flags_array_SC,
highest_global_number_SC,
global_names_array_SC,
globals_array_SC,
global_flags_array_SC,
highest_array_number_SC,
array_names_array_SC,
array_flags_array_SC,
highest_constant_number_SC,
constant_names_array_SC,
highest_class_number_SC,
class_objects_array_SC,
highest_object_number_SC,
dictionary_table_SC,
grammar_table_SC,
-1 };
static void check_system_constant_available(int t)
{
if (OMIT_SYMBOL_TABLE) {
/* Certain system constants refer to the symbol table, which
is meaningless if OMIT_SYMBOL_TABLE is set. */
switch(t)
{
case identifiers_table_SC:
case attribute_names_array_SC:
case property_names_array_SC:
case action_names_array_SC:
case fake_action_names_array_SC:
case array_names_offset_SC:
case global_names_array_SC:
case routine_names_array_SC:
case constant_names_array_SC:
error_named("OMIT_SYMBOL_TABLE omits system constant", system_constants.keywords[t]);
default:
break;
}
}
}
static int32 value_of_system_constant_z(int t)
{
switch(t)
{ case adjectives_table_SC:
return adjectives_offset;
case actions_table_SC:
return actions_offset;
case classes_table_SC:
return class_numbers_offset;
case identifiers_table_SC:
return identifier_names_offset;
case preactions_table_SC:
return preactions_offset;
case largest_object_SC:
return 256 + no_objects - 1;
case strings_offset_SC:
return strings_offset/scale_factor;
case code_offset_SC:
return code_offset/scale_factor;
case actual_largest_object_SC:
return no_objects;
case static_memory_offset_SC:
return static_memory_offset;
case array_names_offset_SC:
return array_names_offset;
case readable_memory_offset_SC:
return Write_Code_At;
case cpv__start_SC:
return prop_values_offset;
case cpv__end_SC:
return class_numbers_offset;
case ipv__start_SC:
return individuals_offset;
case ipv__end_SC:
return variables_offset;
case array__start_SC:
return arrays_offset;
case array__end_SC:
return static_memory_offset;
case dictionary_table_SC:
return dictionary_offset;
case grammar_table_SC:
return static_memory_offset;
case highest_attribute_number_SC:
return no_attributes-1;
case attribute_names_array_SC:
return attribute_names_offset;
case highest_property_number_SC:
return no_individual_properties-1;
case property_names_array_SC:
return identifier_names_offset + 2;
case highest_action_number_SC:
return no_actions-1;
case action_names_array_SC:
return action_names_offset;
case highest_fake_action_number_SC:
return lowest_fake_action() + no_fake_actions-1;
case fake_action_names_array_SC:
return fake_action_names_offset;
case highest_routine_number_SC:
return no_named_routines-1;
case routine_names_array_SC:
return routine_names_offset;
case routines_array_SC:
return routines_array_offset;
case routine_flags_array_SC:
return routine_flags_array_offset;
case highest_global_number_SC:
return 16 + no_globals-1;
case global_names_array_SC:
return global_names_offset;
case globals_array_SC:
return variables_offset;
case global_flags_array_SC:
return global_flags_array_offset;
case highest_array_number_SC:
return no_arrays-1;
case array_names_array_SC:
return array_names_offset;
case array_flags_array_SC:
return array_flags_array_offset;
case highest_constant_number_SC:
return no_named_constants-1;
case constant_names_array_SC:
return constant_names_offset;
case highest_class_number_SC:
return no_classes-1;
case class_objects_array_SC:
return class_numbers_offset;
case highest_object_number_SC:
return no_objects-1;
case highest_meta_action_number_SC:
if (!GRAMMAR_META_FLAG)
error_named("Must set $GRAMMAR_META_FLAG to use option:", system_constants.keywords[t]);
return (no_meta_actions-1) & 0xFFFF;
}
error_named("System constant not implemented in Z-code",
system_constants.keywords[t]);
return(0);
}
/* List used to generate gameinfo.dbg.
Must match the switch statement below. */
int glulx_system_constant_list[] =
{ classes_table_SC,
identifiers_table_SC,
array_names_offset_SC,
cpv__start_SC,
cpv__end_SC,
dictionary_table_SC,
dynam_string_table_SC,
grammar_table_SC,
actions_table_SC,
globals_array_SC,
highest_class_number_SC,
highest_object_number_SC,
-1 };
static int32 value_of_system_constant_g(int t)
{
switch (t) {
case classes_table_SC:
return Write_RAM_At + class_numbers_offset;
case identifiers_table_SC:
return Write_RAM_At + identifier_names_offset;
case array_names_offset_SC:
return Write_RAM_At + array_names_offset;
case cpv__start_SC:
return prop_defaults_offset;
case cpv__end_SC:
return Write_RAM_At + class_numbers_offset;
case dictionary_table_SC:
return dictionary_offset;
case dynam_string_table_SC:
return abbreviations_offset;
case grammar_table_SC:
return grammar_table_offset;
case actions_table_SC:
return actions_offset;
case globals_array_SC:
return variables_offset;
case highest_class_number_SC:
return no_classes-1;
case highest_object_number_SC:
return no_objects-1;
case highest_action_number_SC:
return no_actions-1;
case action_names_array_SC:
return Write_RAM_At + action_names_offset;
case lowest_fake_action_number_SC:
return lowest_fake_action();
case highest_fake_action_number_SC:
return lowest_fake_action() + no_fake_actions-1;
case fake_action_names_array_SC:
return Write_RAM_At + fake_action_names_offset;
case highest_meta_action_number_SC:
if (!GRAMMAR_META_FLAG)
error_named("Must set $GRAMMAR_META_FLAG to use option:", system_constants.keywords[t]);
return no_meta_actions-1;
}
error_named("System constant not implemented in Glulx",
system_constants.keywords[t]);
return 0;
}
extern int32 value_of_system_constant(int t)
{
if (!glulx_mode)
return value_of_system_constant_z(t);
else
return value_of_system_constant_g(t);
}
extern char *name_of_system_constant(int t)
{
if (t < 0 || t >= NO_SYSTEM_CONSTANTS) {
return "???";
}
return system_constants.keywords[t];
}
static int evaluate_term(const token_data *t, assembly_operand *o)
{
/* If the given token is a constant, evaluate it into the operand.
For now, the identifiers are considered variables.
Returns FALSE if it fails to understand type. */
int32 v;
o->marker = t->marker;
o->symindex = t->symindex;
switch(t->type)
{ case LARGE_NUMBER_TT:
v = t->value;
if (!glulx_mode) {
if (v < 0) v = v + 0x10000;
o->type = LONG_CONSTANT_OT;
o->value = v;
}
else {
o->value = v;
o->type = CONSTANT_OT;
}
return(TRUE);
case SMALL_NUMBER_TT:
v = t->value;
if (!glulx_mode) {
if (v < 0) v = v + 0x10000;
o->type = SHORT_CONSTANT_OT;
o->value = v;
}
else {
o->value = v;
set_constant_ot(o);
}
return(TRUE);
case DICTWORD_TT:
/* Find the dictionary address, adding to dictionary if absent */
if (!glulx_mode)
o->type = LONG_CONSTANT_OT;
else
o->type = CONSTANT_OT;
o->value = dictionary_add(t->text, NOUN_DFLAG, 0, 0);
return(TRUE);
case DQ_TT:
/* Create as a static string */
if (!glulx_mode)
o->type = LONG_CONSTANT_OT;
else
o->type = CONSTANT_OT;
o->value = compile_string(t->text, STRCTX_GAME);
return(TRUE);
case VARIABLE_TT:
if (!glulx_mode) {
o->type = VARIABLE_OT;
}
else {
if (t->value >= MAX_LOCAL_VARIABLES) {
o->type = GLOBALVAR_OT;
}
else {
/* This includes "local variable zero", which is really
the stack-pointer magic variable. */
o->type = LOCALVAR_OT;
}
}
o->value = t->value;
return(TRUE);
case SYSFUN_TT:
if (!glulx_mode) {
o->type = VARIABLE_OT;
o->value = t->value + 256;
}
else {
o->type = SYSFUN_OT;
o->value = t->value;
}
system_function_usage[t->value] = 1;
return(TRUE);
case ACTION_TT:
*o = action_of_name(t->text);
return(TRUE);
case SYSTEM_CONSTANT_TT:
/* Certain system constants depend only on the
version number and need no backpatching, as they
are known in advance. We can therefore evaluate
them immediately. */
if (!glulx_mode) {
o->type = LONG_CONSTANT_OT;
switch(t->value)
{
case version_number_SC:
o->type = SHORT_CONSTANT_OT;
o->marker = 0;
v = version_number; break;
case dict_par1_SC:
o->type = SHORT_CONSTANT_OT;
o->marker = 0;
v = (version_number==3)?4:6; break;
case dict_par2_SC:
o->type = SHORT_CONSTANT_OT;
o->marker = 0;
v = (version_number==3)?5:7; break;
case dict_par3_SC:
o->type = SHORT_CONSTANT_OT;
o->marker = 0;
if (ZCODE_LESS_DICT_DATA)
error("#dict_par3 is unavailable when ZCODE_LESS_DICT_DATA is set");
v = (version_number==3)?6:8; break;
case lowest_attribute_number_SC:
case lowest_action_number_SC:
case lowest_routine_number_SC:
case lowest_array_number_SC:
case lowest_constant_number_SC:
case lowest_class_number_SC:
o->type = SHORT_CONSTANT_OT; o->marker = 0; v = 0; break;
case lowest_object_number_SC:
case lowest_property_number_SC:
o->type = SHORT_CONSTANT_OT; o->marker = 0; v = 1; break;
case lowest_global_number_SC:
o->type = SHORT_CONSTANT_OT; o->marker = 0; v = 16; break;
case lowest_fake_action_number_SC:
o->type = LONG_CONSTANT_OT; o->marker = 0;
v = lowest_fake_action(); break;
case oddeven_packing_SC:
o->type = SHORT_CONSTANT_OT; o->marker = 0;
v = oddeven_packing_switch; break;
default:
v = t->value;
o->marker = INCON_MV;
break;
}
o->value = v;
}
else {
o->type = CONSTANT_OT;
switch(t->value)
{
/* The three dict_par flags point at the lower byte
of the flag field, because the library is written
to expect one-byte fields, even though the compiler
generates a dictionary with room for two. */
case dict_par1_SC:
o->marker = 0;
o->value = DICT_ENTRY_FLAG_POS+1;
set_constant_ot(o);
return TRUE;
case dict_par2_SC:
o->marker = 0;
o->value = DICT_ENTRY_FLAG_POS+3;
set_constant_ot(o);
return TRUE;
case dict_par3_SC:
o->marker = 0;
o->value = DICT_ENTRY_FLAG_POS+5;