summaryrefslogtreecommitdiff
path: root/src/symtab.c
diff options
context:
space:
mode:
authorkx <kx@radix-linux.su>2026-09-30 21:14:58 +0300
committerkx <kx@radix-linux.su>2026-09-30 21:14:58 +0300
commit5b1c65152f77e03a4800fceae32d16efe2dadc9c (patch)
treecaffe4b2235503cccfedfb772dc266cb311840b3 /src/symtab.c
parent8b354d2b9f2640d90705abd324a413a0d8fa3867 (diff)
downloadzubr-trunk.tar.xz
Version 4.1.0HEAD4.1.0trunk
Diffstat (limited to 'src/symtab.c')
-rw-r--r--src/symtab.c260
1 files changed, 260 insertions, 0 deletions
diff --git a/src/symtab.c b/src/symtab.c
new file mode 100644
index 0000000..9a8d0d6
--- /dev/null
+++ b/src/symtab.c
@@ -0,0 +1,260 @@
+
+/***************************************************************
+ SYMTAB.C
+
+ This file containts SYMBOL TABLE routines of ZUBR.
+
+ PART OF : ZUBR - Parsers generator for multiple syntax
+ language compilers .
+
+ COMPILE : .
+
+ NOTE : NONE .
+
+ Copyright (C) 1995 - 2026 by Andrey V.Kosteltsev.
+ All Rights Reserved.
+ ***************************************************************/
+/*
+ This file contant RUSSIAN letters( code-page: UTF-8 )
+ ***************************************************************/
+
+#include <defs.h>
+
+#ifndef __NO_COMPILE
+
+
+/* TABLE_SIZE is the number of entries in the symbol table. */
+/* TABLE_SIZE must be a power of two. */
+
+#define TABLE_SIZE 2048
+
+bucket **symbol_table;
+bucket *first_symbol;
+bucket *last_symbol;
+
+int hash( __mpu_char16_t *name )
+/***************************************************************
+
+ Description : find unique number in symbol table
+
+ Concepts : excellent !
+
+ Use Global Variable:
+
+ Use Functions :
+
+ Parameters : char *name
+
+ Return : int k;
+
+ ***************************************************************/
+{
+ register __mpu_char16_t *s;
+ register int c, k;
+
+ if( !name || *name == 0 )
+ {
+ done( 2 );
+ }
+ s = name;
+ k = *s;
+ while( (c = *++s) ) k = (31*k + c) & (TABLE_SIZE - 1);
+
+ return( k );
+
+} /******* End of hash( char *name ) *************************/
+
+
+bucket * make_bucket( __mpu_char16_t *name )
+/***************************************************************
+
+ Description : allocate memory for symbol
+
+ Concepts :
+
+ Use Global Variable:
+
+ Use Functions :
+
+ Parameters : char *name
+
+ Return : bucket *bp
+
+ ***************************************************************/
+{
+ register bucket *bp;
+
+ if( !name )
+ {
+ done( 2 );
+ }
+ bp = (bucket *)MALLOC( sizeof( bucket ) );
+ if( bp == 0 ) no_space();
+
+ bp->link = 0;
+ bp->next = 0;
+
+ bp->name = (__mpu_char16_t *)MALLOC( (mpu_str16len( name ) + 1) *
+ sizeof(__mpu_char16_t) );
+ if( bp->name == 0 ) no_space();
+ mpu_str16cpy( bp->name, name );
+
+ bp->tag = 0;
+ bp->value = UNDEFINED;
+ bp->index = 0;
+ bp->prec = 0;
+ bp->class = UNKNOWN;
+ bp->assoc = TOKEN;
+
+ return( bp );
+
+} /******* End of make_bucket( char *name ) ******************/
+
+
+bucket * lookup( __mpu_char16_t *name )
+/***************************************************************
+
+ Description : find symbol in symbol table
+ if symbol not found,
+ then (for its) allocate memory (use make_bucket ())
+ and include in symbol table
+
+ Concepts :
+
+ Use Global Variable: bucket *last_symbol; | this file
+
+ Use Functions :
+
+ Parameters : char *name
+
+ Return : bucket *bp
+
+ ***************************************************************/
+{
+ register bucket *bp, **bpp;
+
+ bpp = symbol_table + hash( name );
+ bp = *bpp;
+
+ while( bp )
+ {
+ if( mpu_str16cmp( name, bp->name ) == 0 ) return( bp );
+ bpp = &bp->link; /* link для связи цепочки по
+ одному индексу в symbol_table[] */
+ bp = *bpp;
+ /*
+ См.:
+ David Gries, Compiler Construction
+ for Digital Computers,
+ Cornell University, 1971.
+
+ Д.Грис, Конструирование компиляторов для цифровых
+ вычислительных машин,
+ Пер. с англ., Е.Б.Докшитской и др.,
+ Под ред. Ю.М.Баяковского, М.: Мир, 1975.
+
+ п. 9.3.2. Метод цепочек, стр. 252.
+ */
+ }
+
+ *bpp = bp = make_bucket( name ); /* make and insert into symbol_table */
+ last_symbol->next = bp;
+ last_symbol = bp;
+
+ return( bp );
+
+} /******* End of lookup( char *name ) ***********************/
+
+
+void create_symbol_table( void )
+/***************************************************************
+
+ Description : allocate memory for symbol_table
+ and make symbol u"error"
+
+ Concepts :
+
+ Use Global Variable: bucket *first_symbol; | this file
+ bucket *last_symbol; | this file
+
+ Use Functions :
+
+ Parameters : [void]
+
+ Return : [void]
+
+ ***************************************************************/
+{
+ register int i;
+ register bucket *bp;
+
+ symbol_table = (bucket **)MALLOC( TABLE_SIZE*sizeof( bucket * ) );
+ if( symbol_table == 0 ) no_space();
+ for( i = 0; i < TABLE_SIZE; i++ ) symbol_table[i] = 0;
+
+ bp = make_bucket( (__mpu_char16_t *)MPU_UCS2( "error" ) );
+ bp->index = 1;
+ bp->class = TERM;
+
+ first_symbol = bp;
+ last_symbol = bp;
+
+ symbol_table[hash( (__mpu_char16_t *)MPU_UCS2( "error" ) )] = bp;
+
+} /******* End of create_symbol_table( void ) ****************/
+
+
+void free_symbol_table( void )
+/***************************************************************
+
+ Description : free memory for symbol_table
+
+ Concepts :
+
+ Use Global Variable:
+
+ Use Functions :
+
+ Parameters : [void]
+
+ Return : [void]
+
+ ***************************************************************/
+{
+ FREE( symbol_table );
+ symbol_table = 0;
+
+} /******* End of free_symbol_table( void ) ******************/
+
+
+void free_symbols( void )
+/***************************************************************
+
+ Description : free memory for all symbols in list
+
+ Concepts :
+
+ Use Global Variable: bucket *first_symbol; | this file
+
+ Use Functions :
+
+ Parameters : [void]
+
+ Return : [void]
+
+ ***************************************************************/
+{
+ register bucket *p, *q;
+
+ for( p = first_symbol; p; p = q )
+ {
+ q = p->next;
+ /* TODO ADD FREE(name) and other */
+ FREE( p );
+ }
+
+} /******* End of free_symbols( void ) ***********************/
+
+#endif /* __NO_COMPILE */
+
+/****************** END OF FILE SYMTAB.C *********************/