Update to Inform v6.35
[inform.git] / src / arrays.c
1 /* ------------------------------------------------------------------------- */
2 /*   "arrays" :  Parses array declarations and constructs arrays from them;  */
3 /*               likewise global variables, which are in some ways a         */
4 /*               simpler form of the same thing.                             */
5 /*                                                                           */
6 /*   Part of Inform 6.35                                                     */
7 /*   copyright (c) Graham Nelson 1993 - 2021                                 */
8 /*                                                                           */
9 /* Inform is free software: you can redistribute it and/or modify            */
10 /* it under the terms of the GNU General Public License as published by      */
11 /* the Free Software Foundation, either version 3 of the License, or         */
12 /* (at your option) any later version.                                       */
13 /*                                                                           */
14 /* Inform is distributed in the hope that it will be useful,                 */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              */
17 /* GNU General Public License for more details.                              */
18 /*                                                                           */
19 /* You should have received a copy of the GNU General Public License         */
20 /* along with Inform. If not, see https://gnu.org/licenses/                  */
21 /*                                                                           */
22 /* ------------------------------------------------------------------------- */
23
24 #include "header.h"
25
26 /* ------------------------------------------------------------------------- */
27 /*   Arrays defined below:                                                   */
28 /*                                                                           */
29 /*    int    dynamic_array_area[]         Initial values for the bytes of    */
30 /*                                        the dynamic array area             */
31 /*    int    static_array_area[]          Initial values for the bytes of    */
32 /*                                        the static array area              */
33 /*    int32  global_initial_value[n]      The initialised value of the nth   */
34 /*                                        global variable (counting 0 - 239) */
35 /*                                                                           */
36 /*   The "dynamic array area" is the Z-machine area holding the current      */
37 /*   values of the global variables (in 240x2 = 480 bytes) followed by any   */
38 /*   (dynamic) arrays which may be defined.  Owing to a poor choice of name  */
39 /*   some years ago, this is also called the "static data area", which is    */
40 /*   why the memory setting for its maximum extent is "MAX_STATIC_DATA".     */
41 /*                                                                           */
42 /*   In Glulx, that 240 is changed to MAX_GLOBAL_VAR_NUMBER, and we take     */
43 /*   correspondingly more space for the globals. This *really* ought to be   */
44 /*   split into two segments.                                                */
45 /*                                                                           */
46 /*   We can also store arrays (but not globals) into static memory (ROM).    */
47 /*   The storage for these goes, unsurprisingly, into static_array_area --   */
48 /*   a separate allocation of MAX_STATIC_DATA bytes.                         */
49 /* ------------------------------------------------------------------------- */
50 int     *dynamic_array_area;           /* See above                          */
51 int32   *global_initial_value;
52
53 int no_globals;                        /* Number of global variables used
54                                           by the programmer (Inform itself
55                                           uses the top seven -- but these do
56                                           not count)                         */
57                                        /* In Glulx, Inform uses the bottom 
58                                           ten.                               */
59
60 int dynamic_array_area_size;           /* Size in bytes                      */
61
62 int     *static_array_area;
63 int static_array_area_size;
64
65 int no_arrays;
66 int32   *array_symbols;
67 int     *array_sizes, *array_types, *array_locs;
68 /* array_sizes[N] gives the length of array N; array_types[N] is one of
69    the constants BYTE_ARRAY, WORD_ARRAY, etc; array_locs[N] is true for
70    static arrays, false for dynamic arrays.                                  */
71
72 static int array_entry_size,           /* 1 for byte array, 2 for word array */
73            array_base;                 /* Offset in dynamic array area of the
74                                           array being constructed.  During the
75                                           same time, dynamic_array_area_size
76                                           is the offset of the initial entry
77                                           in the array: so for "table" and
78                                           "string" arrays, these numbers are
79                                           different (by 2 and 1 bytes resp)  */
80
81                                        /* In Glulx, of course, that will be
82                                           4 instead of 2.                    */
83
84 extern void finish_array(int32 i, int is_static)
85 {
86   int *area;
87   int area_size;
88   
89   if (!is_static) {
90       area = dynamic_array_area;
91       area_size = dynamic_array_area_size;
92   }
93   else {
94       area = static_array_area;
95       area_size = static_array_area_size;
96   }
97
98   if (i == 0) {
99       error("An array must have at least one entry");
100   }
101   
102     /*  Write the array size into the 0th byte/word of the array, if it's
103         a "table" or "string" array                                          */
104   if (!glulx_mode) {
105
106     if (array_base != area_size)
107     {   if (area_size-array_base==2)
108         {   area[array_base]   = i/256;
109             area[array_base+1] = i%256;
110         }
111         else
112         {   if (i>=256)
113                 error("A 'string' array can have at most 256 entries");
114             area[array_base] = i;
115         }
116     }
117
118   }
119   else {
120     if (array_base != area_size)
121     {   if (area_size-array_base==4)
122         {   
123             area[array_base]   = (i >> 24) & 0xFF;
124             area[array_base+1] = (i >> 16) & 0xFF;
125             area[array_base+2] = (i >> 8) & 0xFF;
126             area[array_base+3] = (i) & 0xFF;
127         }
128         else
129         {   if (i>=256)
130                 error("A 'string' array can have at most 256 entries");
131             area[array_base] = i;
132         }
133     }
134     
135   }
136
137   /*  Move on the static/dynamic array size so that it now points to the
138       next available free space                                              */
139
140   if (!is_static) {
141       dynamic_array_area_size += i*array_entry_size;
142   }
143   else {
144       static_array_area_size += i*array_entry_size;
145   }
146
147 }
148
149 extern void array_entry(int32 i, int is_static, assembly_operand VAL)
150 {
151   int *area;
152   int area_size;
153   
154   if (!is_static) {
155       area = dynamic_array_area;
156       area_size = dynamic_array_area_size;
157   }
158   else {
159       area = static_array_area;
160       area_size = static_array_area_size;
161   }
162   
163   if (!glulx_mode) {
164     /*  Array entry i (initial entry has i=0) is set to Z-machine value j    */
165
166     if (area_size+(i+1)*array_entry_size > MAX_STATIC_DATA)
167         memoryerror("MAX_STATIC_DATA", MAX_STATIC_DATA);
168
169     if (array_entry_size==1)
170     {   area[area_size+i] = (VAL.value)%256;
171
172         if (VAL.marker != 0)
173            error("Entries in byte arrays and strings must be known constants");
174
175         /*  If the entry is too large for a byte array, issue a warning
176             and truncate the value                                           */
177         else
178         if (VAL.value >= 256)
179             warning("Entry in '->', 'string' or 'buffer' array not in range 0 to 255");
180     }
181     else
182     {
183         int32 addr = area_size + 2*i;
184         area[addr]   = (VAL.value)/256;
185         area[addr+1] = (VAL.value)%256;
186         if (VAL.marker != 0) {
187             if (!is_static) {
188                 backpatch_zmachine(VAL.marker, DYNAMIC_ARRAY_ZA,
189                     addr);
190             }
191             else {
192                 backpatch_zmachine(VAL.marker, STATIC_ARRAY_ZA,
193                     addr);
194             }
195         }
196     }
197   }
198   else {
199     /*  Array entry i (initial entry has i=0) is set to value j              */
200
201     if (area_size+(i+1)*array_entry_size > MAX_STATIC_DATA)
202         memoryerror("MAX_STATIC_DATA", MAX_STATIC_DATA);
203
204     if (array_entry_size==1)
205     {   area[area_size+i] = (VAL.value) & 0xFF;
206
207         if (VAL.marker != 0)
208            error("Entries in byte arrays and strings must be known constants");
209
210         /*  If the entry is too large for a byte array, issue a warning
211             and truncate the value                                           */
212         else
213         if (VAL.value >= 256)
214             warning("Entry in '->', 'string' or 'buffer' array not in range 0 to 255");
215     }
216     else if (array_entry_size==4)
217     {
218         int32 addr = area_size + 4*i;
219         area[addr]   = (VAL.value >> 24) & 0xFF;
220         area[addr+1] = (VAL.value >> 16) & 0xFF;
221         area[addr+2] = (VAL.value >> 8) & 0xFF;
222         area[addr+3] = (VAL.value) & 0xFF;
223         if (VAL.marker != 0) {
224             if (!is_static) {
225                 backpatch_zmachine(VAL.marker, DYNAMIC_ARRAY_ZA,
226                     addr - 4*MAX_GLOBAL_VARIABLES);
227             }
228             else {
229                 /* We can't use backpatch_zmachine() because that only applies to RAM. Instead we add an entry to staticarray_backpatch_table.
230                    A backpatch entry is five bytes: *_MV followed by the array offset (in static array area). */
231                 write_byte_to_memory_block(&staticarray_backpatch_table,
232                     staticarray_backpatch_size++,
233                     VAL.marker);
234                 write_byte_to_memory_block(&staticarray_backpatch_table,
235                     staticarray_backpatch_size++, ((addr >> 24) & 0xFF));
236                 write_byte_to_memory_block(&staticarray_backpatch_table,
237                     staticarray_backpatch_size++, ((addr >> 16) & 0xFF));
238                 write_byte_to_memory_block(&staticarray_backpatch_table,
239                     staticarray_backpatch_size++, ((addr >> 8) & 0xFF));
240                 write_byte_to_memory_block(&staticarray_backpatch_table,
241                     staticarray_backpatch_size++, (addr & 0xFF));
242             }
243         }
244     }
245     else
246     {
247         error("Somehow created an array of shorts");
248     }
249   }
250 }
251
252 /* ------------------------------------------------------------------------- */
253 /*   Global and Array directives.                                            */
254 /*                                                                           */
255 /*      Global <variablename> |                                              */
256 /*                            | = <value>                                    */
257 /*                            | <array specification>                        */
258 /*                                                                           */
259 /*      Array <arrayname> [static] <array specification>                     */
260 /*                                                                           */
261 /*   where an array specification is:                                        */
262 /*                                                                           */
263 /*      | ->       |  <number-of-entries>                                    */
264 /*      | -->      |  <entry-1> ... <entry-n>                                */
265 /*      | string   |  [ <entry-1> [;] <entry-2> ... <entry-n> ];             */
266 /*      | table                                                              */
267 /*      | buffer                                                             */
268 /*                                                                           */
269 /*   The "static" keyword (arrays only) places the array in static memory.   */
270 /*                                                                           */
271 /* ------------------------------------------------------------------------- */
272
273 extern void set_variable_value(int i, int32 v)
274 {   global_initial_value[i]=v;
275 }
276
277 /*  There are four ways to initialise arrays:                                */
278
279 #define UNSPECIFIED_AI  -1
280 #define NULLS_AI        0
281 #define DATA_AI         1
282 #define ASCII_AI        2
283 #define BRACKET_AI      3
284
285 extern void make_global(int array_flag, int name_only)
286 {
287     /*  array_flag is TRUE for an Array directive, FALSE for a Global;
288         name_only is only TRUE for parsing an imported variable name, so
289         array_flag is always FALSE in that case.                             */
290
291     int32 i;
292     int array_type, data_type;
293     int is_static = FALSE;
294     assembly_operand AO;
295     
296     int extraspace;
297
298     int32 global_symbol;
299     const char *global_name;
300     debug_location_beginning beginning_debug_location =
301         get_token_location_beginning();
302
303     directive_keywords.enabled = FALSE;
304     get_next_token();
305     i = token_value;
306     global_symbol = i;
307     global_name = token_text;
308
309     if (!glulx_mode) {
310         if ((token_type==SYMBOL_TT) && (stypes[i]==GLOBAL_VARIABLE_T)
311             && (svals[i] >= LOWEST_SYSTEM_VAR_NUMBER))
312             goto RedefinitionOfSystemVar;
313     }
314     else {
315         if ((token_type==SYMBOL_TT) && (stypes[i]==GLOBAL_VARIABLE_T))
316             goto RedefinitionOfSystemVar;
317     }
318
319     if (token_type != SYMBOL_TT)
320     {   discard_token_location(beginning_debug_location);
321         if (array_flag)
322             ebf_error("new array name", token_text);
323         else ebf_error("new global variable name", token_text);
324         panic_mode_error_recovery(); return;
325     }
326
327     if (!(sflags[i] & UNKNOWN_SFLAG))
328     {   discard_token_location(beginning_debug_location);
329         if (array_flag)
330             ebf_symbol_error("new array name", token_text, typename(stypes[i]), slines[i]);
331         else ebf_symbol_error("new global variable name", token_text, typename(stypes[i]), slines[i]);
332         panic_mode_error_recovery(); return;
333     }
334
335     if ((!array_flag) && (sflags[i] & USED_SFLAG))
336         error_named("Variable must be defined before use:", token_text);
337
338     directive_keywords.enabled = TRUE;
339     get_next_token();
340     directive_keywords.enabled = FALSE;
341     if ((token_type==DIR_KEYWORD_TT)&&(token_value==STATIC_DK)) {
342         if (array_flag) {
343             is_static = TRUE;
344         }
345         else {
346             error("Global variables cannot be static");
347         }
348     }
349     else {
350         put_token_back();
351     }
352     
353     if (array_flag)
354     {   if (!is_static) {
355             if (!glulx_mode)
356                 assign_symbol(i, dynamic_array_area_size, ARRAY_T);
357             else
358                 assign_symbol(i, 
359                     dynamic_array_area_size - 4*MAX_GLOBAL_VARIABLES, ARRAY_T);
360         }
361         else {
362             assign_symbol(i, static_array_area_size, STATIC_ARRAY_T);
363         }
364         if (no_arrays == MAX_ARRAYS)
365             memoryerror("MAX_ARRAYS", MAX_ARRAYS);
366         array_symbols[no_arrays] = i;
367     }
368     else
369     {   if (!glulx_mode && no_globals==233)
370         {   discard_token_location(beginning_debug_location);
371             error("All 233 global variables already declared");
372             panic_mode_error_recovery();
373             return;
374         }
375         if (glulx_mode && no_globals==MAX_GLOBAL_VARIABLES)
376         {   discard_token_location(beginning_debug_location);
377             memoryerror("MAX_GLOBAL_VARIABLES", MAX_GLOBAL_VARIABLES);
378             panic_mode_error_recovery();
379             return;
380         }
381
382         variable_tokens[MAX_LOCAL_VARIABLES+no_globals] = i;
383         assign_symbol(i, MAX_LOCAL_VARIABLES+no_globals, GLOBAL_VARIABLE_T);
384         variable_tokens[svals[i]] = i;
385
386         if (name_only) import_symbol(i);
387         else global_initial_value[no_globals++]=0;
388     }
389
390     directive_keywords.enabled = TRUE;
391
392     RedefinitionOfSystemVar:
393
394     if (name_only)
395     {   discard_token_location(beginning_debug_location);
396         return;
397     }
398
399     get_next_token();
400
401     if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
402     {   if (array_flag)
403         {   discard_token_location(beginning_debug_location);
404             ebf_error("array definition", token_text);
405         }
406         put_token_back();
407         if (debugfile_switch && !array_flag)
408         {   debug_file_printf("<global-variable>");
409             debug_file_printf("<identifier>%s</identifier>", global_name);
410             debug_file_printf("<address>");
411             write_debug_global_backpatch(svals[global_symbol]);
412             debug_file_printf("</address>");
413             write_debug_locations
414                 (get_token_location_end(beginning_debug_location));
415             debug_file_printf("</global-variable>");
416         }
417         return;
418     }
419
420     if (!array_flag)
421     {
422         /* is_static is always false in this case */
423         if ((token_type == SEP_TT) && (token_value == SETEQUALS_SEP))
424         {   AO = parse_expression(CONSTANT_CONTEXT);
425             if (!glulx_mode) {
426                 if (AO.marker != 0)
427                     backpatch_zmachine(AO.marker, DYNAMIC_ARRAY_ZA,
428                         2*(no_globals-1));
429             }
430             else {
431             if (AO.marker != 0)
432                 backpatch_zmachine(AO.marker, GLOBALVAR_ZA,
433                 4*(no_globals-1));
434             }
435             global_initial_value[no_globals-1] = AO.value;
436             if (debugfile_switch)
437             {   debug_file_printf("<global-variable>");
438                 debug_file_printf("<identifier>%s</identifier>", global_name);
439                 debug_file_printf("<address>");
440                 write_debug_global_backpatch(svals[global_symbol]);
441                 debug_file_printf("</address>");
442                 write_debug_locations
443                     (get_token_location_end(beginning_debug_location));
444                 debug_file_printf("</global-variable>");
445             }
446             return;
447         }
448
449         obsolete_warning("more modern to use 'Array', not 'Global'");
450
451         if (!glulx_mode) {
452             backpatch_zmachine(ARRAY_MV, DYNAMIC_ARRAY_ZA, 2*(no_globals-1));
453             global_initial_value[no_globals-1]
454                 = dynamic_array_area_size+variables_offset;
455         }
456         else {
457             backpatch_zmachine(ARRAY_MV, GLOBALVAR_ZA, 4*(no_globals-1));
458             global_initial_value[no_globals-1]
459                 = dynamic_array_area_size - 4*MAX_GLOBAL_VARIABLES;
460         }
461     }
462
463     array_type = BYTE_ARRAY; data_type = UNSPECIFIED_AI;
464
465          if ((!array_flag) &&
466              ((token_type==DIR_KEYWORD_TT)&&(token_value==DATA_DK)))
467                  data_type=NULLS_AI;
468     else if ((!array_flag) &&
469              ((token_type==DIR_KEYWORD_TT)&&(token_value==INITIAL_DK)))
470                  data_type=DATA_AI;
471     else if ((!array_flag) &&
472              ((token_type==DIR_KEYWORD_TT)&&(token_value==INITSTR_DK)))
473                  data_type=ASCII_AI;
474
475     else if ((token_type==SEP_TT)&&(token_value==ARROW_SEP))
476              array_type = BYTE_ARRAY;
477     else if ((token_type==SEP_TT)&&(token_value==DARROW_SEP))
478              array_type = WORD_ARRAY;
479     else if ((token_type==DIR_KEYWORD_TT)&&(token_value==STRING_DK))
480              array_type = STRING_ARRAY;
481     else if ((token_type==DIR_KEYWORD_TT)&&(token_value==TABLE_DK))
482              array_type = TABLE_ARRAY;
483     else if ((token_type==DIR_KEYWORD_TT)&&(token_value==BUFFER_DK))
484              array_type = BUFFER_ARRAY;
485     else
486     {   discard_token_location(beginning_debug_location);
487         if (array_flag)
488             ebf_error
489               ("'->', '-->', 'string', 'table' or 'buffer'", token_text);
490         else
491             ebf_error
492               ("'=', '->', '-->', 'string', 'table' or 'buffer'", token_text);
493         panic_mode_error_recovery();
494         return;
495     }
496
497     array_entry_size=1;
498     if ((array_type==WORD_ARRAY) || (array_type==TABLE_ARRAY))
499         array_entry_size=WORDSIZE;
500
501     get_next_token();
502     if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
503     {   discard_token_location(beginning_debug_location);
504         error("No array size or initial values given");
505         put_token_back();
506         return;
507     }
508
509     switch(data_type)
510     {   case UNSPECIFIED_AI:
511             if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
512                 data_type = BRACKET_AI;
513             else
514             {   data_type = NULLS_AI;
515                 if (token_type == DQ_TT) data_type = ASCII_AI;
516                 get_next_token();
517                 if (!((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)))
518                     data_type = DATA_AI;
519                 put_token_back();
520                 put_token_back();
521             }
522             break;
523         case NULLS_AI: obsolete_warning("use '->' instead of 'data'"); break;
524         case DATA_AI:  obsolete_warning("use '->' instead of 'initial'"); break;
525         case ASCII_AI: obsolete_warning("use '->' instead of 'initstr'"); break;
526     }
527
528     /*  Leave room to write the array size in later, if string/table array   */
529     
530     extraspace = 0;
531     if ((array_type==STRING_ARRAY) || (array_type==TABLE_ARRAY))
532         extraspace += array_entry_size;
533     if (array_type==BUFFER_ARRAY)
534         extraspace += WORDSIZE;
535     
536     if (!is_static) {
537         array_base = dynamic_array_area_size;
538         dynamic_array_area_size += extraspace;
539     }
540     else {
541         array_base = static_array_area_size;
542         static_array_area_size += extraspace;
543     }
544
545     array_types[no_arrays] = array_type;
546     array_locs[no_arrays] = is_static;
547
548     switch(data_type)
549     {
550         case NULLS_AI:
551
552             AO = parse_expression(CONSTANT_CONTEXT);
553
554             CalculatedArraySize:
555
556             if (AO.marker != 0)
557             {   error("Array sizes must be known now, not defined later");
558                 break;
559             }
560
561             if (!glulx_mode) {
562                 if ((AO.value <= 0) || (AO.value >= 32768))
563                 {   error("An array must have between 1 and 32767 entries");
564                     AO.value = 1;
565                 }
566             }
567             else {
568                 if (AO.value <= 0 || (AO.value & 0x80000000))
569                 {   error("An array may not have 0 or fewer entries");
570                     AO.value = 1;
571                 }
572             }
573
574             {   for (i=0; i<AO.value; i++) array_entry(i, is_static, zero_operand);
575             }
576             break;
577
578         case DATA_AI:
579
580             /*  In this case the array is initialised to the sequence of
581                 constant values supplied on the same line                    */
582
583             i=0;
584             do
585             {   get_next_token();
586                 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
587                     break;
588
589                 if ((token_type == SEP_TT)
590                     && ((token_value == OPEN_SQUARE_SEP)
591                         || (token_value == CLOSE_SQUARE_SEP)))
592                 {   discard_token_location(beginning_debug_location);
593                     error("Missing ';' to end the initial array values "
594                           "before \"[\" or \"]\"");
595                     return;
596                 }
597                 put_token_back();
598
599                 AO = parse_expression(ARRAY_CONTEXT);
600
601                 if (i == 0)
602                 {   get_next_token();
603                     put_token_back();
604                     if ((token_type == SEP_TT)
605                         && (token_value == SEMICOLON_SEP))
606                     {   data_type = NULLS_AI;
607                         goto CalculatedArraySize;
608                     }
609                 }
610
611                 array_entry(i, is_static, AO);
612                 i++;
613             } while (TRUE);
614             put_token_back();
615             break;
616
617         case ASCII_AI:
618
619             /*  In this case the array is initialised to the ASCII values of
620                 the characters of a given "quoted string"                    */
621
622             get_next_token();
623             if (token_type != DQ_TT)
624             {   ebf_error("literal text in double-quotes", token_text);
625                 token_text = "error";
626             }
627
628             {   assembly_operand chars;
629
630                 int j;
631                 INITAO(&chars);
632                 for (i=0,j=0; token_text[j]!=0; i++,j+=textual_form_length)
633                 {
634                     int32 unicode; int zscii;
635                     unicode = text_to_unicode(token_text+j);
636                     if (glulx_mode)
637                     {
638                         if (array_entry_size == 1 && (unicode < 0 || unicode >= 256))
639                         {
640                             error("Unicode characters beyond Latin-1 cannot be used in a byte array");
641                         }
642                         else
643                         {
644                             chars.value = unicode;
645                         }
646                     }
647                     else  /* Z-code */
648                     {                          
649                         zscii = unicode_to_zscii(unicode);
650                         if ((zscii != 5) && (zscii < 0x100)) chars.value = zscii;
651                         else
652                         {   unicode_char_error("Character can only be used if declared in \
653 advance as part of 'Zcharacter table':", unicode);
654                             chars.value = '?';
655                         }
656                     }
657                     chars.marker = 0;
658                     set_constant_ot(&chars);
659                     array_entry(i, is_static, chars);
660                 }
661             }
662             break;
663
664         case BRACKET_AI:
665
666             /*  In this case the array is initialised to the sequence of
667                 constant values given over a whole range of compiler-lines,
668                 between square brackets [ and ]                              */
669
670             i = 0;
671             while (TRUE)
672             {   get_next_token();
673                 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
674                     continue;
675                 if ((token_type == SEP_TT) && (token_value == CLOSE_SQUARE_SEP))
676                     break;
677                 if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
678                 {   /*  Minimal error recovery: we assume that a ] has
679                         been missed, and the programmer is now starting
680                         a new routine                                        */
681
682                     ebf_error("']'", token_text);
683                     put_token_back(); break;
684                 }
685                 put_token_back();
686                 array_entry(i, is_static, parse_expression(ARRAY_CONTEXT));
687                 i++;
688             }
689     }
690
691     finish_array(i, is_static);
692
693     if (debugfile_switch)
694     {
695         int32 new_area_size;
696         debug_file_printf("<array>");
697         debug_file_printf("<identifier>%s</identifier>", global_name);
698         debug_file_printf("<value>");
699         write_debug_array_backpatch(svals[global_symbol]);
700         debug_file_printf("</value>");
701         new_area_size = (!is_static ? dynamic_array_area_size : static_array_area_size);
702         debug_file_printf
703             ("<byte-count>%d</byte-count>",
704              new_area_size - array_base);
705         debug_file_printf
706             ("<bytes-per-element>%d</bytes-per-element>",
707              array_entry_size);
708         debug_file_printf
709             ("<zeroth-element-holds-length>%s</zeroth-element-holds-length>",
710              (array_type == STRING_ARRAY || array_type == TABLE_ARRAY) ?
711                  "true" : "false");
712         get_next_token();
713         write_debug_locations(get_token_location_end(beginning_debug_location));
714         put_token_back();
715         debug_file_printf("</array>");
716     }
717
718     if ((array_type==BYTE_ARRAY) || (array_type==WORD_ARRAY)) i--;
719     if (array_type==BUFFER_ARRAY) i+=WORDSIZE-1;
720     array_sizes[no_arrays++] = i;
721 }
722
723 extern int32 begin_table_array(void)
724 {
725     /*  The "box" statement needs to be able to construct table
726         arrays of strings like this. (Static data, but we create a dynamic
727         array for maximum backwards compatibility.) */
728
729     array_base = dynamic_array_area_size;
730     array_entry_size = WORDSIZE;
731
732     /*  Leave room to write the array size in later                          */
733
734     dynamic_array_area_size += array_entry_size;
735
736     if (!glulx_mode)
737         return array_base;
738     else
739         return array_base - WORDSIZE * MAX_GLOBAL_VARIABLES;
740 }
741
742 extern int32 begin_word_array(void)
743 {
744     /*  The "random(a, b, ...)" function needs to be able to construct
745         word arrays like this. (Static data, but we create a dynamic
746         array for maximum backwards compatibility.) */
747
748     array_base = dynamic_array_area_size;
749     array_entry_size = WORDSIZE;
750
751     if (!glulx_mode)
752         return array_base;
753     else
754         return array_base - WORDSIZE * MAX_GLOBAL_VARIABLES;
755 }
756
757 /* ========================================================================= */
758 /*   Data structure management routines                                      */
759 /* ------------------------------------------------------------------------- */
760
761 extern void init_arrays_vars(void)
762 {   dynamic_array_area = NULL;
763     static_array_area = NULL;
764     global_initial_value = NULL;
765     array_sizes = NULL; array_symbols = NULL; array_types = NULL;
766 }
767
768 extern void arrays_begin_pass(void)
769 {   no_arrays = 0; 
770     if (!glulx_mode)
771         no_globals=0; 
772     else
773         no_globals=11;
774     dynamic_array_area_size = WORDSIZE * MAX_GLOBAL_VARIABLES;
775     static_array_area_size = 0;
776 }
777
778 extern void arrays_allocate_arrays(void)
779 {   dynamic_array_area = my_calloc(sizeof(int), MAX_STATIC_DATA, 
780         "dynamic array data");
781     static_array_area = my_calloc(sizeof(int), MAX_STATIC_DATA, 
782         "static array data");
783     array_sizes = my_calloc(sizeof(int), MAX_ARRAYS, "array sizes");
784     array_types = my_calloc(sizeof(int), MAX_ARRAYS, "array types");
785     array_locs = my_calloc(sizeof(int), MAX_ARRAYS, "array locations");
786     array_symbols = my_calloc(sizeof(int32), MAX_ARRAYS, "array symbols");
787     global_initial_value = my_calloc(sizeof(int32), MAX_GLOBAL_VARIABLES, 
788         "global values");
789 }
790
791 extern void arrays_free_arrays(void)
792 {   my_free(&dynamic_array_area, "dynamic array data");
793     my_free(&static_array_area, "static array data");
794     my_free(&global_initial_value, "global values");
795     my_free(&array_sizes, "array sizes");
796     my_free(&array_types, "array types");
797     my_free(&array_locs, "array locations");
798     my_free(&array_symbols, "array sizes");
799 }
800
801 /* ========================================================================= */