home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
POLYEDIT.LZH
/
ML
/
IDENT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-01-18
|
4KB
|
204 lines
/*
* 識別子の管理
*
* T.Koabayashi 1993.8.7
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "data.h"
#include "err.h"
#include "inlib.h"
IdentBuffer *IdentGlobalVar, *IdentLocalVar, *IdentFunction ;
/* 識別子バッファの確保 */
IdentBuffer *IdentAlloc( n, res )
int n ;
int res ;
{
int i ;
IdentBuffer *ret ;
ret = MemoryAlloc( sizeof( IdentBuffer ) + ( sizeof( char ) + sizeof( IdentName ) + sizeof(IdentTable)) * n );
if ( ret == NULL )
ParseFatal( "識別子管理バッファが確保できません" );
ret->flag = (char*)( ret + 1 );
ret->name = (IdentName*)( ret->flag + n );
ret->table = (IdentTable*)(ret->name + n);
ret->count = res ;
ret->max = n ;
ret->tablesize = 0;
for( i = 0 ; i < res ; i++ )
{
ret->flag[i] = IDENT_RESERVE ;
}
return ret ;
}
/* 識別子バッファの解放 */
void IdentFree( buf )
IdentBuffer *buf ;
{
MemoryFree( buf );
}
/* 識別子の登録 */
int IdentAppend( ibuf, name, sw )
IdentBuffer *ibuf ;
char *name ;
int sw ;
{
int i, n ;
assert( ibuf != NULL );
if ( ibuf->count < ibuf->max )
{
#if 0
n = ibuf->count ++ ;
for( i = 0 ; i < n ; i++ )
{
if ( ibuf->flag[i] != IDENT_RESERVE && strcmp( ibuf->name[i], name ) == 0 )
ParseError( "識別子 %s が2重定義されています", name );
}
ibuf->flag[n] = sw ;
strncpy( ibuf->name[n], name, IDENT_CHARS );
ibuf->name[n][IDENT_CHARS] = '\0' ;
return n ;
#else
IdentTable *table;
int i1, i2, j;
table = ibuf->table;
n = ibuf->count ++ ;
ibuf->flag[n] = sw ;
strncpy( ibuf->name[n], name, IDENT_CHARS );
ibuf->name[n][IDENT_CHARS] = '\0' ;
i1 = -1;
i2 = ibuf->tablesize;
while (i1 < i2-1) {
i = (i1+i2)/2;
j = strcmp(name, table[i].name);
if (j == 0) {
ParseError( "識別子 %s が2重定義されています", name );
} else if (j < 0) {
i2 = i;
} else {
i1 = i;
}
}
if (i2 < ibuf->tablesize) {
for (i = ibuf->tablesize; i > i2; --i) {
table[i].name = table[i-1].name;
table[i].id = table[i-1].id;
}
}
table[i2].name = ibuf->name[n];
table[i2].id = n;
ibuf->tablesize++;
return n;
#endif
}
else
{
ParseFatal( "識別子管理バッファがオーバーフローしました" );
return -1 ;
}
}
/* 識別子の検索 */
int IdentSearch( ibuf, name )
IdentBuffer *ibuf ;
char *name ;
{
int i, n ;
if ( ibuf == NULL )
return -1 ;
#if 0
n = ibuf->count ;
for( i = 0 ; i < n ; i++ )
{
if ( ibuf->flag[i] != IDENT_RESERVE && strcmp( ibuf->name[i], name ) == 0 )
return i ;
}
#else
{
IdentTable *table;
int i1, i2, j;
table = ibuf->table;
i1 = -1;
i2 = ibuf->tablesize;
while (i1 < i2-1) {
i = (i1+i2)/2;
j = strcmp(name, table[i].name);
if (j == 0) {
return table[i].id;
} else if (j < 0) {
i2 = i;
} else {
i1 = i;
}
}
}
#endif
return -1 ;
}
/* privete 識別子の無効化 */
void IdentPrivate( ibuf )
IdentBuffer *ibuf ;
{
int i, n ;
if ( ibuf == NULL )
return ;
n = ibuf->count ;
for( i = 0 ; i < n ; i++ )
{
if ( ibuf->flag[i] & IDENT_PRIVATE )
{
ibuf->flag[i] = IDENT_RESERVE ;
}
}
#if 1
{
int j = 0;
IdentTable *table = ibuf->table;
n = ibuf->tablesize;
for (i = 0; i < n; ++i) {
if (ibuf->flag[table[i].id] != IDENT_RESERVE) {
if (i != j) {
table[j].name = table[i].name;
table[j].id = table[i].id;
}
j++;
} else {
}
}
ibuf->tablesize = j;
}
#endif
}
/* flag の取得 */
int IdentFlag( ibuf, n )
IdentBuffer *ibuf ;
int n ;
{
assert( ibuf != NULL );
assert( n < ibuf->count );
return ibuf->flag[n] ;
}