c1f410ce34b9d47f1513c957c6ac5af927aaf18d
[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 - 2020                                 */
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     int32 global_symbol;
297     const char *global_name;
298     debug_location_beginning beginning_debug_location =
299         get_token_location_beginning();
300
301     directive_keywords.enabled = FALSE;
302     get_next_token();
303     i = token_value;
304     global_symbol = i;
305     global_name = token_text;
306
307     if (!glulx_mode) {
308         if ((token_type==SYMBOL_TT) && (stypes[i]==GLOBAL_VARIABLE_T)
309             && (svals[i] >= LOWEST_SYSTEM_VAR_NUMBER))
310             goto RedefinitionOfSystemVar;
311     }
312     else {
313         if ((token_type==SYMBOL_TT) && (stypes[i]==GLOBAL_VARIABLE_T))
314             goto RedefinitionOfSystemVar;
315     }
316
317     if (token_type != SYMBOL_TT)
318     {   discard_token_location(beginning_debug_location);
319         if (array_flag)
320             ebf_error("new array name", token_text);
321         else ebf_error("new global variable name", token_text);
322         panic_mode_error_recovery(); return;
323     }
324
325     if (!(sflags[i] & UNKNOWN_SFLAG))
326     {   discard_token_location(beginning_debug_location);
327         if (array_flag)
328             ebf_symbol_error("new array name", token_text, typename(stypes[i]), slines[i]);
329         else ebf_symbol_error("new global variable name", token_text, typename(stypes[i]), slines[i]);
330         panic_mode_error_recovery(); return;
331     }
332
333     if ((!array_flag) && (sflags[i] & USED_SFLAG))
334         error_named("Variable must be defined before use:", token_text);
335
336     directive_keywords.enabled = TRUE;
337     get_next_token();
338     directive_keywords.enabled = FALSE;
339     if ((token_type==DIR_KEYWORD_TT)&&(token_value==STATIC_DK)) {
340         if (array_flag) {
341             is_static = TRUE;
342         }
343         else {
344             error("Global variables cannot be static");
345         }
346     }
347     else {
348         put_token_back();
349     }
350     
351     if (array_flag)
352     {   if (!is_static) {
353             if (!glulx_mode)
354                 assign_symbol(i, dynamic_array_area_size, ARRAY_T);
355             else
356                 assign_symbol(i, 
357                     dynamic_array_area_size - 4*MAX_GLOBAL_VARIABLES, ARRAY_T);
358         }
359         else {
360             assign_symbol(i, static_array_area_size, STATIC_ARRAY_T);
361         }
362         if (no_arrays == MAX_ARRAYS)
363             memoryerror("MAX_ARRAYS", MAX_ARRAYS);
364         array_symbols[no_arrays] = i;
365     }
366     else
367     {   if (!glulx_mode && no_globals==233)
368         {   discard_token_location(beginning_debug_location);
369             error("All 233 global variables already declared");
370             panic_mode_error_recovery();
371             return;
372         }
373         if (glulx_mode && no_globals==MAX_GLOBAL_VARIABLES)
374         {   discard_token_location(beginning_debug_location);
375             memoryerror("MAX_GLOBAL_VARIABLES", MAX_GLOBAL_VARIABLES);
376             panic_mode_error_recovery();
377             return;
378         }
379
380         variable_tokens[MAX_LOCAL_VARIABLES+no_globals] = i;
381         assign_symbol(i, MAX_LOCAL_VARIABLES+no_globals, GLOBAL_VARIABLE_T);
382         variable_tokens[svals[i]] = i;
383
384         if (name_only) import_symbol(i);
385         else global_initial_value[no_globals++]=0;
386     }
387
388     directive_keywords.enabled = TRUE;
389
390     RedefinitionOfSystemVar:
391
392     if (name_only)
393     {   discard_token_location(beginning_debug_location);
394         return;
395     }
396
397     get_next_token();
398
399     if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
400     {   if (array_flag)
401         {   discard_token_location(beginning_debug_location);
402             ebf_error("array definition", token_text);
403         }
404         put_token_back();
405         if (debugfile_switch && !array_flag)
406         {   debug_file_printf("<global-variable>");
407             debug_file_printf("<identifier>%s</identifier>", global_name);
408             debug_file_printf("<address>");
409             write_debug_global_backpatch(svals[global_symbol]);
410             debug_file_printf("</address>");
411             write_debug_locations
412                 (get_token_location_end(beginning_debug_location));
413             debug_file_printf("</global-variable>");
414         }
415         return;
416     }
417
418     if (!array_flag)
419     {
420         /* is_static is always false in this case */
421         if ((token_type == SEP_TT) && (token_value == SETEQUALS_SEP))
422         {   AO = parse_expression(CONSTANT_CONTEXT);
423             if (!glulx_mode) {
424                 if (AO.marker != 0)
425                     backpatch_zmachine(AO.marker, DYNAMIC_ARRAY_ZA,
426                         2*(no_globals-1));
427             }
428             else {
429             if (AO.marker != 0)
430                 backpatch_zmachine(AO.marker, GLOBALVAR_ZA,
431                 4*(no_globals-1));
432             }
433             global_initial_value[no_globals-1] = AO.value;
434             if (debugfile_switch)
435             {   debug_file_printf("<global-variable>");
436                 debug_file_printf("<identifier>%s</identifier>", global_name);
437                 debug_file_printf("<address>");
438                 write_debug_global_backpatch(svals[global_symbol]);
439                 debug_file_printf("</address>");
440                 write_debug_locations
441                     (get_token_location_end(beginning_debug_location));
442                 debug_file_printf("</global-variable>");
443             }
444             return;
445         }
446
447         obsolete_warning("more modern to use 'Array', not 'Global'");
448
449         if (!glulx_mode) {
450             backpatch_zmachine(ARRAY_MV, DYNAMIC_ARRAY_ZA, 2*(no_globals-1));
451             global_initial_value[no_globals-1]
452                 = dynamic_array_area_size+variables_offset;
453         }
454         else {
455             backpatch_zmachine(ARRAY_MV, GLOBALVAR_ZA, 4*(no_globals-1));
456             global_initial_value[no_globals-1]
457                 = dynamic_array_area_size - 4*MAX_GLOBAL_VARIABLES;
458         }
459     }
460
461     array_type = BYTE_ARRAY; data_type = UNSPECIFIED_AI;
462
463          if ((!array_flag) &&
464              ((token_type==DIR_KEYWORD_TT)&&(token_value==DATA_DK)))
465                  data_type=NULLS_AI;
466     else if ((!array_flag) &&
467              ((token_type==DIR_KEYWORD_TT)&&(token_value==INITIAL_DK)))
468                  data_type=DATA_AI;
469     else if ((!array_flag) &&
470              ((token_type==DIR_KEYWORD_TT)&&(token_value==INITSTR_DK)))
471                  data_type=ASCII_AI;
472
473     else if ((token_type==SEP_TT)&&(token_value==ARROW_SEP))
474              array_type = BYTE_ARRAY;
475     else if ((token_type==SEP_TT)&&(token_value==DARROW_SEP))
476              array_type = WORD_ARRAY;
477     else if ((token_type==DIR_KEYWORD_TT)&&(token_value==STRING_DK))
478              array_type = STRING_ARRAY;
479     else if ((token_type==DIR_KEYWORD_TT)&&(token_value==TABLE_DK))
480              array_type = TABLE_ARRAY;
481     else if ((token_type==DIR_KEYWORD_TT)&&(token_value==BUFFER_DK))
482              array_type = BUFFER_ARRAY;
483     else
484     {   discard_token_location(beginning_debug_location);
485         if (array_flag)
486             ebf_error
487               ("'->', '-->', 'string', 'table' or 'buffer'", token_text);
488         else
489             ebf_error
490               ("'=', '->', '-->', 'string', 'table' or 'buffer'", token_text);
491         panic_mode_error_recovery();
492         return;
493     }
494
495     array_entry_size=1;
496     if ((array_type==WORD_ARRAY) || (array_type==TABLE_ARRAY))
497         array_entry_size=WORDSIZE;
498
499     get_next_token();
500     if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
501     {   discard_token_location(beginning_debug_location);
502         error("No array size or initial values given");
503         put_token_back();
504         return;
505     }
506
507     switch(data_type)
508     {   case UNSPECIFIED_AI:
509             if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
510                 data_type = BRACKET_AI;
511             else
512             {   data_type = NULLS_AI;
513                 if (token_type == DQ_TT) data_type = ASCII_AI;
514                 get_next_token();
515                 if (!((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)))
516                     data_type = DATA_AI;
517                 put_token_back();
518                 put_token_back();
519             }
520             break;
521         case NULLS_AI: obsolete_warning("use '->' instead of 'data'"); break;
522         case DATA_AI:  obsolete_warning("use '->' instead of 'initial'"); break;
523         case ASCII_AI: obsolete_warning("use '->' instead of 'initstr'"); break;
524     }
525
526     /*  Leave room to write the array size in later, if string/table array   */
527     
528     int extraspace = 0;
529     if ((array_type==STRING_ARRAY) || (array_type==TABLE_ARRAY))
530         extraspace += array_entry_size;
531     if (array_type==BUFFER_ARRAY)
532         extraspace += WORDSIZE;
533
534     int orig_area_size;
535     
536     if (!is_static) {
537         orig_area_size = dynamic_array_area_size;
538         array_base = dynamic_array_area_size;
539         dynamic_array_area_size += extraspace;
540     }
541     else {
542         orig_area_size = static_array_area_size;
543         array_base = static_array_area_size;
544         static_array_area_size += extraspace;
545     }
546
547     array_types[no_arrays] = array_type;
548     array_locs[no_arrays] = is_static;
549
550     switch(data_type)
551     {
552         case NULLS_AI:
553
554             AO = parse_expression(CONSTANT_CONTEXT);
555
556             CalculatedArraySize:
557
558             if (AO.marker != 0)
559             {   error("Array sizes must be known now, not defined later");
560                 break;
561             }
562
563             if (!glulx_mode) {
564                 if ((AO.value <= 0) || (AO.value >= 32768))
565                 {   error("An array must have between 1 and 32767 entries");
566                     AO.value = 1;
567                 }
568             }
569             else {
570                 if (AO.value <= 0 || (AO.value & 0x80000000))
571                 {   error("An array may not have 0 or fewer entries");
572                     AO.value = 1;
573                 }
574             }
575
576             {   for (i=0; i<AO.value; i++) array_entry(i, is_static, zero_operand);
577             }
578             break;
579
580         case DATA_AI:
581
582             /*  In this case the array is initialised to the sequence of
583                 constant values supplied on the same line                    */
584
585             i=0;
586             do
587             {   get_next_token();
588                 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
589                     break;
590
591                 if ((token_type == SEP_TT)
592                     && ((token_value == OPEN_SQUARE_SEP)
593                         || (token_value == CLOSE_SQUARE_SEP)))
594                 {   discard_token_location(beginning_debug_location);
595                     error("Missing ';' to end the initial array values "
596                           "before \"[\" or \"]\"");
597                     return;
598                 }
599                 put_token_back();
600
601                 AO = parse_expression(ARRAY_CONTEXT);
602
603                 if (i == 0)
604                 {   get_next_token();
605                     put_token_back();
606                     if ((token_type == SEP_TT)
607                         && (token_value == SEMICOLON_SEP))
608                     {   data_type = NULLS_AI;
609                         goto CalculatedArraySize;
610                     }
611                 }
612
613                 array_entry(i, is_static, AO);
614                 i++;
615             } while (TRUE);
616             put_token_back();
617             break;
618
619         case ASCII_AI:
620
621             /*  In this case the array is initialised to the ASCII values of
622                 the characters of a given "quoted string"                    */
623
624             get_next_token();
625             if (token_type != DQ_TT)
626             {   ebf_error("literal text in double-quotes", token_text);
627                 token_text = "error";
628             }
629
630             {   assembly_operand chars;
631
632                 int j;
633                 INITAO(&chars);
634                 for (i=0,j=0; token_text[j]!=0; i++,j+=textual_form_length)
635                 {
636                     int32 unicode; int zscii;
637                     unicode = text_to_unicode(token_text+j);
638                     if (glulx_mode)
639                     {
640                         if (array_entry_size == 1 && (unicode < 0 || unicode >= 256))
641                         {
642                             error("Unicode characters beyond Latin-1 cannot be used in a byte array");
643                         }
644                         else
645                         {
646                             chars.value = unicode;
647                         }
648                     }
649                     else  /* Z-code */
650                     {                          
651                         zscii = unicode_to_zscii(unicode);
652                         if ((zscii != 5) && (zscii < 0x100)) chars.value = zscii;
653                         else
654                         {   unicode_char_error("Character can only be used if declared in \
655 advance as part of 'Zcharacter table':", unicode);
656                             chars.value = '?';
657                         }
658                     }
659                     chars.marker = 0;
660                     set_constant_ot(&chars);
661                     array_entry(i, is_static, chars);
662                 }
663             }
664             break;
665
666         case BRACKET_AI:
667
668             /*  In this case the array is initialised to the sequence of
669                 constant values given over a whole range of compiler-lines,
670                 between square brackets [ and ]                              */
671
672             i = 0;
673             while (TRUE)
674             {   get_next_token();
675                 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
676                     continue;
677                 if ((token_type == SEP_TT) && (token_value == CLOSE_SQUARE_SEP))
678                     break;
679                 if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
680                 {   /*  Minimal error recovery: we assume that a ] has
681                         been missed, and the programmer is now starting
682                         a new routine                                        */
683
684                     ebf_error("']'", token_text);
685                     put_token_back(); break;
686                 }
687                 put_token_back();
688                 array_entry(i, is_static, parse_expression(ARRAY_CONTEXT));
689                 i++;
690             }
691     }
692
693     finish_array(i, is_static);
694
695     if (debugfile_switch)
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         int32 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 /* ========================================================================= */