1
/* Copyright (C) 1995 Bjoern Beutel. */
3
/* Description. =============================================================*/
5
/* This module administrates the name and atoms for each symbol. */
7
/* Includes. ================================================================*/
17
#include "malaga_files.h"
20
/* Variables. ===============================================================*/
22
static struct /* This is the symbol table. */
24
int_t symbol_count; /* Number of symbols in this table. */
26
symbol_entry_t *symbols; /* The names and atoms of all symbols. */
27
symbol_t *symbols_name; /* All symbols sorted by their names. */
28
symbol_t *symbols_atoms; /* All symbols sorted by their atom lists. */
31
symbol_t *values; /* Contains the lists of atomic symbols. */
34
char_t *strings; /* Contains the symbol names. */
37
/* Functions. ===============================================================*/
40
get_symbol_name( symbol_t symbol )
41
/* Return the name of SYMBOL. */
43
return symbol_table.strings + symbol_table.symbols[ symbol ].name;
46
/*---------------------------------------------------------------------------*/
49
get_atoms( symbol_t symbol )
50
/* Return the atom list of SYMBOL. */
52
return symbol_table.values + symbol_table.symbols[ symbol ].atoms;
55
/*---------------------------------------------------------------------------*/
58
find_symbol( string_t name )
59
/* Find a symbol NAME in the symbol table and return its code.
60
* If there is no symbol NAME, report an error. */
62
int_t lower, upper, middle, result;
65
/* We do a binary search in SYMBOLS_NAME. */
67
upper = symbol_table.symbol_count - 1;
68
while (lower <= upper)
70
middle = (lower + upper) / 2;
71
symbol = symbol_table.symbols_name[ middle ];
72
result = strcmp_no_case( name, get_symbol_name( symbol ) );
80
complain( "Unknown symbol \"%s\".", name );
83
/*---------------------------------------------------------------------------*/
86
find_multi_symbol( value_t atoms )
87
/* Find a symbol by its atoms in the symbol table and return its code.
88
* If there is no multi-symbol for ATOMS, report an error. */
90
int_t lower, upper, middle, result;
93
/* We do a binary search in SYMBOLS_ATOMS. */
95
upper = symbol_table.symbol_count - 1;
96
while (lower <= upper)
98
middle = (lower + upper) / 2;
99
symbol = symbol_table.symbols_atoms[ middle ];
100
result = compare_atom_lists( atoms, get_atoms( symbol ) );
108
complain( "No multi symbol for this atom list." );
111
/*---------------------------------------------------------------------------*/
115
/* Return the number of symbols defined. */
117
return symbol_table.symbol_count;
120
/*---------------------------------------------------------------------------*/
123
compare_symbols_name( const void *symbol1, const void *symbol2 )
124
/* Return -1 if name( SYMBOL1 ) < name( SYMBOL2 )
125
* 0 if name( SYMBOL1 ) == name( SYMBOL2 )
126
* 1 if name( SYMBOL1 ) > name( SYMBOL2 ). */
128
return strcmp_no_case( get_symbol_name( *(symbol_t *) symbol1 ),
129
get_symbol_name( *(symbol_t *) symbol2 ) );
132
/*---------------------------------------------------------------------------*/
135
compare_symbols_atoms( const void *symbol1, const void *symbol2 )
136
/* Return -1 if atoms( SYMBOL1 ) < atoms( SYMBOL2 )
137
* 0 if atoms( SYMBOL1 ) == atoms( SYMBOL2 )
138
* 1 if atoms( SYMBOL1 ) > atoms( SYMBOL2 ). */
140
return compare_atom_lists( get_atoms( *(symbol_t *) symbol1 ),
141
get_atoms( *(symbol_t *) symbol2 ) );
144
/*---------------------------------------------------------------------------*/
147
init_symbols( string_t file_name )
148
/* Initialise this module. Read SYMBOL_TABLE from file FILE_NAME. */
151
symbol_header_t header;
154
stream = open_stream( file_name, "rb" );
155
read_vector( &header, sizeof( header ), 1, stream, file_name );
156
check_header( &header.common_header, file_name,
157
SYMBOL_FILE, MIN_SYMBOL_CODE_VERSION, SYMBOL_CODE_VERSION );
159
symbol_table.symbol_count = header.symbol_count;
160
symbol_table.symbols = read_new_vector( sizeof( symbol_entry_t ),
163
symbol_table.values_size = header.values_size;
164
symbol_table.values = read_new_vector( sizeof( cell_t ), header.values_size,
166
symbol_table.strings_size = header.strings_size;
167
symbol_table.strings = read_new_vector( sizeof( char_t ),
170
close_stream( &stream, file_name );
172
/* Build a list of all symbols sorted by their names
173
* and a list of all symbols sorted by their atom lists. */
174
symbol_table.symbols_name = new_vector( sizeof( symbol_t ),
175
header.symbol_count );
176
symbol_table.symbols_atoms = new_vector( sizeof( symbol_t ),
177
header.symbol_count );
178
for (i = 0; i < header.symbol_count; i++)
180
symbol_table.symbols_name[i] = i;
181
symbol_table.symbols_atoms[i] = i;
183
qsort( symbol_table.symbols_name, header.symbol_count,
184
sizeof( symbol_t ), compare_symbols_name );
185
qsort( symbol_table.symbols_atoms, header.symbol_count,
186
sizeof( symbol_t ), compare_symbols_atoms );
188
values_get_symbol_name = get_symbol_name;
189
values_get_atoms = get_atoms;
192
/*---------------------------------------------------------------------------*/
195
terminate_symbols( void )
196
/* Terminate this module. */
198
free_mem( &symbol_table.symbols );
199
free_mem( &symbol_table.symbols_name );
200
free_mem( &symbol_table.symbols_atoms );
201
free_mem( &symbol_table.values );
202
free_mem( &symbol_table.strings );
205
/* End of file. =============================================================*/