~ubuntu-branches/ubuntu/vivid/inform/vivid

« back to all changes in this revision

Viewing changes to src/symbols.c

  • Committer: Bazaar Package Importer
  • Author(s): Jan Christoph Nordholz
  • Date: 2008-05-26 22:09:44 UTC
  • mfrom: (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526220944-ba7phz0d1k4vo7wx
Tags: 6.31.1+dfsg-1
* Remove a considerable number of files from the package
  due to unacceptable licensing terms.
* Repair library symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ------------------------------------------------------------------------- */
2
 
/*   "symbols" :  The symbols table; creating stock of reserved words        */
3
 
/*                                                                           */
4
 
/*   Part of Inform 6.30                                                     */
5
 
/*   copyright (c) Graham Nelson 1993 - 2004                                 */
6
 
/*                                                                           */
7
 
/* ------------------------------------------------------------------------- */
8
 
 
9
 
#include "header.h"
10
 
 
11
 
/* ------------------------------------------------------------------------- */
12
 
/*   This section of Inform is a service detached from the rest.             */
13
 
/*   Only two variables are accessible from the outside:                     */
14
 
/* ------------------------------------------------------------------------- */
15
 
 
16
 
int no_symbols;                        /* Total number of symbols defined    */
17
 
int no_named_constants;                         /* Copied into story file    */
18
 
 
19
 
/* ------------------------------------------------------------------------- */
20
 
/*   Plus four arrays.  Each symbol has its own index n (an int32) and       */
21
 
/*                                                                           */
22
 
/*       svals[n]   is its value (must be 32 bits wide, i.e. an int32, tho'  */
23
 
/*                  it is used to hold an unsigned 16 bit Z-machine value)   */
24
 
/*       sflags[n]  holds flags (see "header.h" for a list)                  */
25
 
/*       stypes[n]  is the "type", distinguishing between the data type of   */
26
 
/*                  different kinds of constants/variables.                  */
27
 
/*                  (See the "typename()" below.)                            */
28
 
/*       symbs[n]   (needs to be cast to (char *) to be used) is the name    */
29
 
/*                  of the symbol, in the same case form as when created.    */
30
 
/*       slines[n]  is the source line on which the symbol value was first   */
31
 
/*                  assigned                                                 */
32
 
/*                                                                           */
33
 
/*   Comparison is case insensitive.                                         */
34
 
/*   Note that local variable names are not entered into the symbols table,  */
35
 
/*   as their numbers and scope are too limited for this to be efficient.    */
36
 
/* ------------------------------------------------------------------------- */
37
 
/*   Caveat editor: some array types are set up to work even on machines     */
38
 
/*   where sizeof(int32 *) differs from, e.g., sizeof(char *): so do not     */
39
 
/*   alter the types unless you understand what is going on!                 */
40
 
/* ------------------------------------------------------------------------- */
41
 
 
42
 
  int32  **symbs;
43
 
  int32  *svals;
44
 
  int    *smarks;            /* Glulx-only */
45
 
  int32  *slines;
46
 
  int    *sflags;
47
 
#ifdef VAX
48
 
  char   *stypes;            /* In VAX C, insanely, "signed char" is illegal */
49
 
#else
50
 
  signed char *stypes;
51
 
#endif
52
 
 
53
 
/* ------------------------------------------------------------------------- */
54
 
/*   Memory to hold the text of symbol names: note that this memory is       */
55
 
/*   allocated as needed in chunks of size SYMBOLS_CHUNK_SIZE.               */
56
 
/* ------------------------------------------------------------------------- */
57
 
 
58
 
static uchar *symbols_free_space,       /* Next byte free to hold new names  */
59
 
           *symbols_ceiling;            /* Pointer to the end of the current
60
 
                                           allocation of memory for names    */
61
 
 
62
 
static char** symbol_name_space_chunks; /* For chunks of memory used to hold
63
 
                                           the name strings of symbols       */
64
 
static int no_symbol_name_space_chunks;
65
 
 
66
 
/* ------------------------------------------------------------------------- */
67
 
/*   The symbols table is "hash-coded" into a disjoint union of linked       */
68
 
/*   lists, so that for any symbol i, next_entry[i] is either -1 (meaning    */
69
 
/*   that it's the last in its list) or the next in the list.                */
70
 
/*                                                                           */
71
 
/*   Each list contains, in alphabetical order, all the symbols which share  */
72
 
/*   the same "hash code" (a numerical function of the text of the symbol    */
73
 
/*   name, designed with the aim that roughly equal numbers of symbols are   */
74
 
/*   given each possible hash code).  The hash codes are 0 to HASH_TAB_SIZE  */
75
 
/*   (which is a memory setting) minus 1: start_of_list[h] gives the first   */
76
 
/*   symbol with hash code h, or -1 if no symbol exists with hash code h.    */
77
 
/*                                                                           */
78
 
/*   Note that the running time of the symbol search algorithm is about      */
79
 
/*                                                                           */
80
 
/*       O ( n^2 / HASH_TAB_SIZE )                                           */
81
 
/*                                                                           */
82
 
/*   (where n is the number of symbols in the program) so that it is a good  */
83
 
/*   idea to choose HASH_TAB_SIZE as large as conveniently possible.         */
84
 
/* ------------------------------------------------------------------------- */
85
 
 
86
 
static int   *next_entry;
87
 
static int32 *start_of_list;
88
 
 
89
 
/* ------------------------------------------------------------------------- */
90
 
/*   Initialisation.                                                         */
91
 
/* ------------------------------------------------------------------------- */
92
 
 
93
 
static void init_symbol_banks(void)
94
 
{   int i;
95
 
    for (i=0; i<HASH_TAB_SIZE; i++) start_of_list[i] = -1;
96
 
}
97
 
 
98
 
/* ------------------------------------------------------------------------- */
99
 
/*   The hash coding we use is quite standard; the variable hashcode is      */
100
 
/*   expected to overflow a good deal.  (The aim is to produce a number      */
101
 
/*   so that similar names do not produce the same number.)  Note that       */
102
 
/*   30011 is prime.  It doesn't matter if the unsigned int to int cast      */
103
 
/*   behaves differently on different ports.                                 */
104
 
/* ------------------------------------------------------------------------- */
105
 
 
106
 
int case_conversion_grid[128];
107
 
 
108
 
static void make_case_conversion_grid(void)
109
 
{
110
 
    /*  Assumes that A to Z are contiguous in the host OS character set:
111
 
        true for ASCII but not for EBCDIC, for instance.                     */
112
 
 
113
 
    int i;
114
 
    for (i=0; i<128; i++) case_conversion_grid[i] = i;
115
 
    for (i=0; i<26; i++) case_conversion_grid['A'+i]='a'+i;
116
 
}
117
 
 
118
 
extern int hash_code_from_string(char *p)
119
 
{   uint32 hashcode=0;
120
 
    for (; *p; p++) hashcode=hashcode*30011 + case_conversion_grid[*p];
121
 
    return (int) (hashcode % HASH_TAB_SIZE);
122
 
}
123
 
 
124
 
extern int strcmpcis(char *p, char *q)
125
 
{
126
 
    /*  Case insensitive strcmp  */
127
 
 
128
 
    int i, j, pc, qc;
129
 
    for (i=0;p[i] != 0;i++)
130
 
    {   pc = p[i]; if (isupper(pc)) pc = tolower(pc);
131
 
        qc = q[i]; if (isupper(qc)) qc = tolower(qc);
132
 
        j = pc - qc;
133
 
        if (j!=0) return j;
134
 
    }
135
 
    qc = q[i]; if (isupper(qc)) qc = tolower(qc);
136
 
    return -qc;
137
 
}
138
 
 
139
 
/* ------------------------------------------------------------------------- */
140
 
/*   Symbol finding/creating.                                                */
141
 
/* ------------------------------------------------------------------------- */
142
 
 
143
 
extern int symbol_index(char *p, int hashcode)
144
 
{
145
 
    /*  Return the index in the symbs/svals/sflags/stypes arrays of symbol
146
 
        "p", creating a new symbol with that name if it isn't already there.
147
 
 
148
 
        New symbols are created with fundamental type UNKNOWN_CONSTANT_FT,
149
 
        value 0x100 (a 2-byte quantity in Z-machine terms) and type
150
 
        CONSTANT_T.
151
 
 
152
 
        The string "p" is undamaged.                                         */
153
 
 
154
 
    int32 new_entry, this, last; char *r;
155
 
 
156
 
    if (hashcode == -1) hashcode = hash_code_from_string(p);
157
 
 
158
 
    this = start_of_list[hashcode]; last = -1;
159
 
 
160
 
    do
161
 
    {   if (this == -1) break;
162
 
 
163
 
        r = (char *)symbs[this];
164
 
        new_entry = strcmpcis(r, p);
165
 
        if (new_entry == 0) return this;
166
 
        if (new_entry > 0) break;
167
 
 
168
 
        last = this;
169
 
        this = next_entry[this];
170
 
    } while (this != -1);
171
 
 
172
 
    if (no_symbols >= MAX_SYMBOLS)
173
 
        memoryerror("MAX_SYMBOLS", MAX_SYMBOLS);
174
 
 
175
 
    if (last == -1)
176
 
    {   next_entry[no_symbols]=start_of_list[hashcode];
177
 
        start_of_list[hashcode]=no_symbols;
178
 
    }
179
 
    else
180
 
    {   next_entry[no_symbols]=this;
181
 
        next_entry[last]=no_symbols;
182
 
    }
183
 
 
184
 
    if (symbols_free_space+strlen(p)+1 >= symbols_ceiling)
185
 
    {   symbols_free_space
186
 
            = my_malloc(SYMBOLS_CHUNK_SIZE, "symbol names chunk");
187
 
        symbols_ceiling = symbols_free_space + SYMBOLS_CHUNK_SIZE;
188
 
        symbol_name_space_chunks[no_symbol_name_space_chunks++]
189
 
            = (char *) symbols_free_space;
190
 
    }
191
 
 
192
 
    strcpy((char *) symbols_free_space, p);
193
 
    symbs[no_symbols] = (int32 *) symbols_free_space;
194
 
    symbols_free_space += strlen((char *)symbols_free_space) + 1;
195
 
 
196
 
    svals[no_symbols]   =  0x100; /* ###-wrong? Would this fix the
197
 
                                     unbound-symbol-causes-asm-error? */
198
 
    sflags[no_symbols]  =  UNKNOWN_SFLAG;
199
 
    stypes[no_symbols]  =  CONSTANT_T;
200
 
    slines[no_symbols]  =  ErrorReport.line_number
201
 
                           + 0x10000*ErrorReport.file_number;
202
 
 
203
 
    return(no_symbols++);
204
 
}
205
 
 
206
 
/* ------------------------------------------------------------------------- */
207
 
/*   Printing diagnostics                                                    */
208
 
/* ------------------------------------------------------------------------- */
209
 
 
210
 
extern char *typename(int type)
211
 
{   switch(type)
212
 
    {
213
 
        /*  These are the possible symbol types.  Note that local variables
214
 
            do not reside in the symbol table (for scope and efficiency
215
 
            reasons) and actions have their own name-space (via routine
216
 
            names with "Sub" appended).                                      */
217
 
 
218
 
        case ROUTINE_T:             return("Routine");
219
 
        case LABEL_T:               return("Label");
220
 
        case GLOBAL_VARIABLE_T:     return("Global variable");
221
 
        case ARRAY_T:               return("Array");
222
 
        case CONSTANT_T:            return("Defined constant");
223
 
        case ATTRIBUTE_T:           return("Attribute");
224
 
        case PROPERTY_T:            return("Property");
225
 
        case INDIVIDUAL_PROPERTY_T: return("Individual property");
226
 
        case OBJECT_T:              return("Object");
227
 
        case CLASS_T:               return("Class");
228
 
        case FAKE_ACTION_T:         return("Fake action");
229
 
 
230
 
        default:                   return("(Unknown type)");
231
 
    }
232
 
}
233
 
 
234
 
static void describe_flags(int flags)
235
 
{   if (flags & UNKNOWN_SFLAG)  printf("(?) ");
236
 
    if (flags & USED_SFLAG)     printf("(used) ");
237
 
    if (flags & REPLACE_SFLAG)  printf("(Replaced) ");
238
 
    if (flags & DEFCON_SFLAG)   printf("(Defaulted) ");
239
 
    if (flags & STUB_SFLAG)     printf("(Stubbed) ");
240
 
    if (flags & CHANGE_SFLAG)   printf("(value will change) ");
241
 
    if (flags & IMPORT_SFLAG)   printf("(Imported) ");
242
 
    if (flags & EXPORT_SFLAG)   printf("(Exported) ");
243
 
    if (flags & SYSTEM_SFLAG)   printf("(System) ");
244
 
    if (flags & INSF_SFLAG)     printf("(created in sys file) ");
245
 
    if (flags & UERROR_SFLAG)   printf("('Unknown' error issued) ");
246
 
    if (flags & ALIASED_SFLAG)  printf("(aliased) ");
247
 
    if (flags & ACTION_SFLAG)   printf("(Action name) ");
248
 
    if (flags & REDEFINABLE_SFLAG) printf("(Redefinable) ");
249
 
}
250
 
 
251
 
extern void describe_symbol(int k)
252
 
{   printf("%4d  %-16s  %2d:%04d  %04x  %s  ",
253
 
        k, (char *) (symbs[k]), slines[k]/0x10000, slines[k]%0x10000,
254
 
        svals[k], typename(stypes[k]));
255
 
    describe_flags(sflags[k]);
256
 
}
257
 
 
258
 
extern void list_symbols(int level)
259
 
{   int k;
260
 
    for (k=0; k<no_symbols; k++)
261
 
    {   if ((level==2) ||
262
 
            ((sflags[k] & (SYSTEM_SFLAG + UNKNOWN_SFLAG + INSF_SFLAG)) == 0))
263
 
        {   describe_symbol(k); printf("\n");
264
 
        }
265
 
    }
266
 
}
267
 
 
268
 
extern void issue_unused_warnings(void)
269
 
{   int32 i;
270
 
 
271
 
    if (module_switch) return;
272
 
 
273
 
    /*  Update any ad-hoc variables that might help the library  */
274
 
    if (glulx_mode)
275
 
    {   global_initial_value[10]=statusline_flag;
276
 
    }
277
 
    /*  Now back to mark anything necessary as used  */
278
 
 
279
 
    i = symbol_index("Main", -1);
280
 
    if (!(sflags[i] & UNKNOWN_SFLAG)) sflags[i] |= USED_SFLAG;
281
 
 
282
 
    for (i=0;i<no_symbols;i++)
283
 
    {   if (((sflags[i]
284
 
             & (SYSTEM_SFLAG + UNKNOWN_SFLAG + EXPORT_SFLAG
285
 
                + INSF_SFLAG + USED_SFLAG + REPLACE_SFLAG)) == 0)
286
 
             && (stypes[i] != OBJECT_T))
287
 
            dbnu_warning(typename(stypes[i]), (char *) symbs[i], slines[i]);
288
 
    }
289
 
}
290
 
 
291
 
/* ------------------------------------------------------------------------- */
292
 
/*   These are arrays used only during story file (never module) creation,   */
293
 
/*   and not allocated until then.                                           */
294
 
 
295
 
       int32 *individual_name_strings; /* Packed addresses of Z-encoded
296
 
                                          strings of the names of the
297
 
                                          properties: this is an array
298
 
                                          indexed by the property ID         */
299
 
       int32 *action_name_strings;     /* Ditto for actions                  */
300
 
       int32 *attribute_name_strings;  /* Ditto for attributes               */
301
 
       int32 *array_name_strings;      /* Ditto for arrays                   */
302
 
 
303
 
extern void write_the_identifier_names(void)
304
 
{   int i, j, k, t, null_value; char idname_string[256];
305
 
    static char unknown_attribute[20] = "<unknown attribute>";
306
 
 
307
 
    for (i=0; i<no_individual_properties; i++)
308
 
        individual_name_strings[i] = 0;
309
 
 
310
 
    if (module_switch) return;
311
 
 
312
 
    veneer_mode = TRUE;
313
 
 
314
 
    null_value = compile_string(unknown_attribute, FALSE, FALSE);
315
 
    for (i=0; i<NUM_ATTR_BYTES*8; i++) attribute_name_strings[i] = null_value;
316
 
 
317
 
    for (i=0; i<no_symbols; i++)
318
 
    {   t=stypes[i];
319
 
        if ((t == INDIVIDUAL_PROPERTY_T) || (t == PROPERTY_T))
320
 
        {   if (sflags[i] & ALIASED_SFLAG)
321
 
            {   if (individual_name_strings[svals[i]] == 0)
322
 
                {   sprintf(idname_string, "%s", (char *) symbs[i]);
323
 
 
324
 
                    for (j=i+1, k=0; (j<no_symbols && k<3); j++)
325
 
                    {   if ((stypes[j] == stypes[i])
326
 
                            && (svals[j] == svals[i]))
327
 
                        {   sprintf(idname_string+strlen(idname_string),
328
 
                                "/%s", (char *) symbs[j]);
329
 
                            k++;
330
 
                        }
331
 
                    }
332
 
 
333
 
                    if (debugfile_switch)
334
 
                    {   write_debug_byte(PROP_DBR);
335
 
                        write_debug_byte(svals[i]/256);
336
 
                        write_debug_byte(svals[i]%256);
337
 
                        write_debug_string(idname_string);
338
 
                    }
339
 
 
340
 
                    individual_name_strings[svals[i]]
341
 
                        = compile_string(idname_string, FALSE, FALSE);
342
 
                }
343
 
            }
344
 
            else
345
 
            {   sprintf(idname_string, "%s", (char *) symbs[i]);
346
 
 
347
 
                if (debugfile_switch)
348
 
                {   write_debug_byte(PROP_DBR);
349
 
                    write_debug_byte(svals[i]/256);
350
 
                    write_debug_byte(svals[i]%256);
351
 
                    write_debug_string(idname_string);
352
 
                }
353
 
 
354
 
                individual_name_strings[svals[i]]
355
 
                    = compile_string(idname_string, FALSE, FALSE);
356
 
            }
357
 
        }
358
 
        if (t == ATTRIBUTE_T)
359
 
        {   if (sflags[i] & ALIASED_SFLAG)
360
 
            {   if (attribute_name_strings[svals[i]] == null_value)
361
 
                {   sprintf(idname_string, "%s", (char *) symbs[i]);
362
 
 
363
 
                    for (j=i+1, k=0; (j<no_symbols && k<3); j++)
364
 
                    {   if ((stypes[j] == stypes[i])
365
 
                            && (svals[j] == svals[i]))
366
 
                        {   sprintf(idname_string+strlen(idname_string),
367
 
                                "/%s", (char *) symbs[j]);
368
 
                            k++;
369
 
                        }
370
 
                    }
371
 
 
372
 
                    if (debugfile_switch)
373
 
                    {   write_debug_byte(ATTR_DBR);
374
 
                        write_debug_byte(svals[i]/256);
375
 
                        write_debug_byte(svals[i]%256);
376
 
                        write_debug_string(idname_string);
377
 
                    }
378
 
 
379
 
                    attribute_name_strings[svals[i]]
380
 
                        = compile_string(idname_string, FALSE, FALSE);
381
 
                }
382
 
            }
383
 
            else
384
 
            {   sprintf(idname_string, "%s", (char *) symbs[i]);
385
 
 
386
 
                if (debugfile_switch)
387
 
                {   write_debug_byte(ATTR_DBR);
388
 
                    write_debug_byte(svals[i]/256);
389
 
                    write_debug_byte(svals[i]%256);
390
 
                    write_debug_string(idname_string);
391
 
                }
392
 
 
393
 
                attribute_name_strings[svals[i]]
394
 
                    = compile_string(idname_string, FALSE, FALSE);
395
 
            }
396
 
        }
397
 
        if (sflags[i] & ACTION_SFLAG)
398
 
        {   sprintf(idname_string, "%s", (char *) symbs[i]);
399
 
            idname_string[strlen(idname_string)-3] = 0;
400
 
 
401
 
            if (debugfile_switch)
402
 
            {   write_debug_byte(ACTION_DBR);
403
 
                write_debug_byte(svals[i]/256);
404
 
                write_debug_byte(svals[i]%256);
405
 
                write_debug_string(idname_string);
406
 
            }
407
 
 
408
 
            action_name_strings[svals[i]]
409
 
                = compile_string(idname_string, FALSE, FALSE);
410
 
        }
411
 
    }
412
 
 
413
 
    for (i=0; i<no_symbols; i++)
414
 
    {   if (stypes[i] == FAKE_ACTION_T)
415
 
        {   sprintf(idname_string, "%s", (char *) symbs[i]);
416
 
            idname_string[strlen(idname_string)-3] = 0;
417
 
 
418
 
            if (debugfile_switch)
419
 
            {   write_debug_byte(ACTION_DBR);
420
 
                write_debug_byte(svals[i]/256);
421
 
                write_debug_byte(svals[i]%256);
422
 
                write_debug_string(idname_string);
423
 
            }
424
 
 
425
 
            action_name_strings[svals[i]
426
 
                    - ((grammar_version_number==1)?256:4096) + no_actions]
427
 
                = compile_string(idname_string, FALSE, FALSE);
428
 
        }
429
 
    }
430
 
 
431
 
    for (j=0; j<no_arrays; j++)
432
 
    {   i = array_symbols[j];
433
 
        sprintf(idname_string, "%s", (char *) symbs[i]);
434
 
 
435
 
        if (debugfile_switch)
436
 
        {   write_debug_byte(ARRAY_DBR);
437
 
            write_debug_byte(svals[i]/256);
438
 
            write_debug_byte(svals[i]%256);
439
 
            write_debug_string(idname_string);
440
 
        }
441
 
 
442
 
        array_name_strings[j]
443
 
            = compile_string(idname_string, FALSE, FALSE);
444
 
    }
445
 
  if (define_INFIX_switch)
446
 
  { for (i=0; i<no_symbols; i++)
447
 
    {   if (stypes[i] == GLOBAL_VARIABLE_T)
448
 
        {   sprintf(idname_string, "%s", (char *) symbs[i]);
449
 
            array_name_strings[no_arrays + svals[i] -16]
450
 
                = compile_string(idname_string, FALSE, FALSE);
451
 
        }
452
 
    }
453
 
 
454
 
    for (i=0; i<no_named_routines; i++)
455
 
    {   sprintf(idname_string, "%s", (char *) symbs[named_routine_symbols[i]]);
456
 
            array_name_strings[no_arrays + no_globals + i]
457
 
                = compile_string(idname_string, FALSE, FALSE);
458
 
    }
459
 
 
460
 
    for (i=0, no_named_constants=0; i<no_symbols; i++)
461
 
    {   if (((stypes[i] == OBJECT_T) || (stypes[i] == CLASS_T)
462
 
            || (stypes[i] == CONSTANT_T))
463
 
            && ((sflags[i] & (UNKNOWN_SFLAG+ACTION_SFLAG))==0))
464
 
        {   sprintf(idname_string, "%s", (char *) symbs[i]);
465
 
            array_name_strings[no_arrays + no_globals + no_named_routines
466
 
                + no_named_constants++]
467
 
                = compile_string(idname_string, FALSE, FALSE);
468
 
        }
469
 
    }
470
 
  }
471
 
 
472
 
    veneer_mode = FALSE;
473
 
}
474
 
/* ------------------------------------------------------------------------- */
475
 
/*   Creating symbols                                                        */
476
 
/* ------------------------------------------------------------------------- */
477
 
 
478
 
static void assign_symbol_base(int index, int32 value, int type)
479
 
{   svals[index]  = value;
480
 
    stypes[index] = type;
481
 
    if (sflags[index] & UNKNOWN_SFLAG)
482
 
    {   sflags[index] &= (~UNKNOWN_SFLAG);
483
 
        if (is_systemfile()) sflags[index] |= INSF_SFLAG;
484
 
        slines[index] = ErrorReport.line_number
485
 
                        + 0x10000*ErrorReport.file_number;
486
 
    }
487
 
}
488
 
 
489
 
extern void assign_symbol(int index, int32 value, int type)
490
 
{
491
 
    if (!glulx_mode) {
492
 
        assign_symbol_base(index, value, type);
493
 
    }
494
 
    else {
495
 
        smarks[index] = 0;
496
 
        assign_symbol_base(index, value, type);
497
 
    }
498
 
}
499
 
 
500
 
extern void assign_marked_symbol(int index, int marker, int32 value, int type)
501
 
{
502
 
    if (!glulx_mode) {
503
 
        assign_symbol_base(index, (int32)marker*0x10000 + (value % 0x10000),
504
 
            type);
505
 
    }
506
 
    else {
507
 
        smarks[index] = marker;
508
 
        assign_symbol_base(index, value, type);
509
 
    }
510
 
}
511
 
 
512
 
 
513
 
static void create_symbol(char *p, int value, int type)
514
 
{   int i = symbol_index(p, -1);
515
 
    svals[i] = value; stypes[i] = type; slines[i] = 0;
516
 
    sflags[i] = USED_SFLAG + SYSTEM_SFLAG;
517
 
}
518
 
 
519
 
static void create_rsymbol(char *p, int value, int type)
520
 
{   int i = symbol_index(p, -1);
521
 
    svals[i] = value; stypes[i] = type; slines[i] = 0;
522
 
    sflags[i] = USED_SFLAG + SYSTEM_SFLAG + REDEFINABLE_SFLAG;
523
 
}
524
 
 
525
 
static void stockup_symbols(void)
526
 
{
527
 
    if (!glulx_mode)
528
 
        create_symbol("TARGET_ZCODE", 0, CONSTANT_T);
529
 
    else 
530
 
        create_symbol("TARGET_GLULX", 0, CONSTANT_T);
531
 
 
532
 
    create_symbol("nothing",        0, OBJECT_T);
533
 
    create_symbol("name",           1, PROPERTY_T);
534
 
 
535
 
    create_symbol("true",           1, CONSTANT_T);
536
 
    create_symbol("false",          0, CONSTANT_T);
537
 
 
538
 
    /* Glulx defaults to GV2; Z-code to GV1 */
539
 
    if (!glulx_mode)
540
 
        create_rsymbol("Grammar__Version", 1, CONSTANT_T);
541
 
    else
542
 
        create_rsymbol("Grammar__Version", 2, CONSTANT_T);
543
 
    grammar_version_symbol = symbol_index("Grammar__Version", -1);
544
 
 
545
 
    if (module_switch)
546
 
        create_rsymbol("MODULE_MODE",0, CONSTANT_T);
547
 
 
548
 
    if (runtime_error_checking_switch)
549
 
        create_rsymbol("STRICT_MODE",0, CONSTANT_T);
550
 
 
551
 
    if (define_DEBUG_switch)
552
 
        create_rsymbol("DEBUG",      0, CONSTANT_T);
553
 
 
554
 
    if (define_USE_MODULES_switch)
555
 
        create_rsymbol("USE_MODULES",0, CONSTANT_T);
556
 
 
557
 
    if (define_INFIX_switch)
558
 
    {   create_rsymbol("INFIX",      0, CONSTANT_T);
559
 
        create_symbol("infix__watching", 0, ATTRIBUTE_T);
560
 
    }
561
 
 
562
 
    create_symbol("WORDSIZE",        WORDSIZE, CONSTANT_T);
563
 
    if (!glulx_mode) {
564
 
        create_symbol("DICT_WORD_SIZE", ((version_number==3)?4:6), CONSTANT_T);
565
 
        create_symbol("NUM_ATTR_BYTES", ((version_number==3)?4:6), CONSTANT_T);
566
 
    }
567
 
    else {
568
 
        create_symbol("DICT_WORD_SIZE",     DICT_WORD_SIZE, CONSTANT_T);
569
 
        create_symbol("NUM_ATTR_BYTES",     NUM_ATTR_BYTES, CONSTANT_T);
570
 
        create_symbol("INDIV_PROP_START",   INDIV_PROP_START, CONSTANT_T);
571
 
    }    
572
 
 
573
 
    if (!glulx_mode) {
574
 
        create_symbol("temp_global",  255, GLOBAL_VARIABLE_T);
575
 
        create_symbol("temp__global2", 254, GLOBAL_VARIABLE_T);
576
 
        create_symbol("temp__global3", 253, GLOBAL_VARIABLE_T);
577
 
        create_symbol("temp__global4", 252, GLOBAL_VARIABLE_T);
578
 
        create_symbol("self",         251, GLOBAL_VARIABLE_T);
579
 
        create_symbol("sender",       250, GLOBAL_VARIABLE_T);
580
 
        create_symbol("sw__var",      249, GLOBAL_VARIABLE_T);
581
 
        
582
 
        create_symbol("sys__glob0",     16, GLOBAL_VARIABLE_T);
583
 
        create_symbol("sys__glob1",     17, GLOBAL_VARIABLE_T);
584
 
        create_symbol("sys__glob2",     18, GLOBAL_VARIABLE_T);
585
 
        
586
 
        create_symbol("create",        64, INDIVIDUAL_PROPERTY_T);
587
 
        create_symbol("recreate",      65, INDIVIDUAL_PROPERTY_T);
588
 
        create_symbol("destroy",       66, INDIVIDUAL_PROPERTY_T);
589
 
        create_symbol("remaining",     67, INDIVIDUAL_PROPERTY_T);
590
 
        create_symbol("copy",          68, INDIVIDUAL_PROPERTY_T);
591
 
        create_symbol("call",          69, INDIVIDUAL_PROPERTY_T);
592
 
        create_symbol("print",         70, INDIVIDUAL_PROPERTY_T);
593
 
        create_symbol("print_to_array",71, INDIVIDUAL_PROPERTY_T);
594
 
    }
595
 
    else {
596
 
        /* In Glulx, these system globals are entered in order, not down 
597
 
           from 255. */
598
 
        create_symbol("temp_global",  MAX_LOCAL_VARIABLES+0, 
599
 
          GLOBAL_VARIABLE_T);
600
 
        create_symbol("temp__global2", MAX_LOCAL_VARIABLES+1, 
601
 
          GLOBAL_VARIABLE_T);
602
 
        create_symbol("temp__global3", MAX_LOCAL_VARIABLES+2, 
603
 
          GLOBAL_VARIABLE_T);
604
 
        create_symbol("temp__global4", MAX_LOCAL_VARIABLES+3, 
605
 
          GLOBAL_VARIABLE_T);
606
 
        create_symbol("self",         MAX_LOCAL_VARIABLES+4, 
607
 
          GLOBAL_VARIABLE_T);
608
 
        create_symbol("sender",       MAX_LOCAL_VARIABLES+5, 
609
 
          GLOBAL_VARIABLE_T);
610
 
        create_symbol("sw__var",      MAX_LOCAL_VARIABLES+6, 
611
 
          GLOBAL_VARIABLE_T);
612
 
 
613
 
        /* These are almost certainly meaningless, and can be removed. */
614
 
        create_symbol("sys__glob0",     MAX_LOCAL_VARIABLES+7, 
615
 
          GLOBAL_VARIABLE_T);
616
 
        create_symbol("sys__glob1",     MAX_LOCAL_VARIABLES+8, 
617
 
          GLOBAL_VARIABLE_T);
618
 
        create_symbol("sys__glob2",     MAX_LOCAL_VARIABLES+9, 
619
 
          GLOBAL_VARIABLE_T);
620
 
 
621
 
        /* value of statusline_flag to be written later */
622
 
        create_symbol("sys_statusline_flag",  MAX_LOCAL_VARIABLES+10, 
623
 
          GLOBAL_VARIABLE_T);
624
 
 
625
 
        /* These are created in order, but not necessarily at a fixed
626
 
           value. */
627
 
        create_symbol("create",        INDIV_PROP_START+0, 
628
 
          INDIVIDUAL_PROPERTY_T);
629
 
        create_symbol("recreate",      INDIV_PROP_START+1, 
630
 
          INDIVIDUAL_PROPERTY_T);
631
 
        create_symbol("destroy",       INDIV_PROP_START+2, 
632
 
          INDIVIDUAL_PROPERTY_T);
633
 
        create_symbol("remaining",     INDIV_PROP_START+3, 
634
 
          INDIVIDUAL_PROPERTY_T);
635
 
        create_symbol("copy",          INDIV_PROP_START+4, 
636
 
          INDIVIDUAL_PROPERTY_T);
637
 
        create_symbol("call",          INDIV_PROP_START+5, 
638
 
          INDIVIDUAL_PROPERTY_T);
639
 
        create_symbol("print",         INDIV_PROP_START+6, 
640
 
          INDIVIDUAL_PROPERTY_T);
641
 
        create_symbol("print_to_array",INDIV_PROP_START+7, 
642
 
          INDIVIDUAL_PROPERTY_T);
643
 
    }
644
 
}
645
 
 
646
 
/* ========================================================================= */
647
 
/*   Data structure management routines                                      */
648
 
/* ------------------------------------------------------------------------- */
649
 
 
650
 
extern void init_symbols_vars(void)
651
 
{
652
 
    symbs = NULL;
653
 
    svals = NULL;
654
 
    smarks = NULL;
655
 
    stypes = NULL;
656
 
    sflags = NULL;
657
 
    next_entry = NULL;
658
 
    start_of_list = NULL;
659
 
 
660
 
    symbol_name_space_chunks = NULL;
661
 
    no_symbol_name_space_chunks = 0;
662
 
    symbols_free_space=NULL;
663
 
    symbols_ceiling=symbols_free_space;
664
 
 
665
 
    no_symbols = 0;
666
 
 
667
 
    make_case_conversion_grid();
668
 
}
669
 
 
670
 
extern void symbols_begin_pass(void) { }
671
 
 
672
 
extern void symbols_allocate_arrays(void)
673
 
{
674
 
    symbs      = my_calloc(sizeof(char *),  MAX_SYMBOLS, "symbols");
675
 
    svals      = my_calloc(sizeof(int32),   MAX_SYMBOLS, "symbol values");
676
 
    if (glulx_mode)
677
 
        smarks = my_calloc(sizeof(int),     MAX_SYMBOLS, "symbol markers");
678
 
    slines     = my_calloc(sizeof(int32),   MAX_SYMBOLS, "symbol lines");
679
 
    stypes     = my_calloc(sizeof(char),    MAX_SYMBOLS, "symbol types");
680
 
    sflags     = my_calloc(sizeof(int),     MAX_SYMBOLS, "symbol flags");
681
 
    next_entry = my_calloc(sizeof(int),     MAX_SYMBOLS,
682
 
                     "symbol linked-list forward links");
683
 
    start_of_list = my_calloc(sizeof(int32), HASH_TAB_SIZE,
684
 
                     "hash code list beginnings");
685
 
 
686
 
    symbol_name_space_chunks
687
 
        = my_calloc(sizeof(char *), 100, "symbol names chunk addresses");
688
 
 
689
 
    init_symbol_banks();
690
 
    stockup_symbols();
691
 
 
692
 
    /*  Allocated during story file construction, not now  */
693
 
    individual_name_strings = NULL;
694
 
    attribute_name_strings = NULL;
695
 
    action_name_strings = NULL;
696
 
    array_name_strings = NULL;
697
 
}
698
 
 
699
 
extern void symbols_free_arrays(void)
700
 
{   int i;
701
 
 
702
 
    for (i=0; i<no_symbol_name_space_chunks; i++)
703
 
        my_free(&(symbol_name_space_chunks[i]),
704
 
            "symbol names chunk");
705
 
 
706
 
    my_free(&symbol_name_space_chunks, "symbol names chunk addresses");
707
 
 
708
 
    my_free(&symbs, "symbols");
709
 
    my_free(&svals, "symbol values");
710
 
    my_free(&smarks, "symbol markers");
711
 
    my_free(&slines, "symbol lines");
712
 
    my_free(&stypes, "symbol types");
713
 
    my_free(&sflags, "symbol flags");
714
 
    my_free(&next_entry, "symbol linked-list forward links");
715
 
    my_free(&start_of_list, "hash code list beginnings");
716
 
 
717
 
    if (individual_name_strings != NULL)
718
 
        my_free(&individual_name_strings, "property name strings");
719
 
    if (action_name_strings != NULL)
720
 
        my_free(&action_name_strings,     "action name strings");
721
 
    if (attribute_name_strings != NULL)
722
 
        my_free(&attribute_name_strings,  "attribute name strings");
723
 
    if (array_name_strings != NULL)
724
 
        my_free(&array_name_strings,      "array name strings");
725
 
}
726
 
 
727
 
/* ========================================================================= */