qse/ase/stx/symbol.c

124 lines
2.8 KiB
C
Raw Normal View History

2005-05-17 16:18:56 +00:00
/*
2005-05-19 16:41:10 +00:00
* $Id: symbol.c,v 1.3 2005-05-19 16:41:10 bacon Exp $
2005-05-17 16:18:56 +00:00
*/
#include <xp/stx/symbol.h>
#include <xp/stx/object.h>
#include <xp/stx/misc.h>
2005-05-18 04:01:51 +00:00
#define SYMLINK_SIZE 2
#define SYMLINK_LINK 0
#define SYMLINK_SYMBOL 1
2005-05-17 16:18:56 +00:00
2005-05-18 04:01:51 +00:00
xp_stx_word_t xp_stx_new_symlink (xp_stx_t* stx, xp_stx_word_t sym)
2005-05-17 16:18:56 +00:00
{
xp_stx_word_t x;
2005-05-18 04:01:51 +00:00
x = xp_stx_alloc_object (stx, SYMLINK_SIZE);
XP_STX_CLASS(stx,x) = stx->class_symlink;
/*XP_STX_AT(stx,x,SYMLINK_LINK) = stx->nil;*/
XP_STX_AT(stx,x,SYMLINK_SYMBOL) = sym;
2005-05-17 16:18:56 +00:00
return x;
}
xp_stx_word_t xp_stx_new_symbol (xp_stx_t* stx, const xp_stx_char_t* name)
{
xp_stx_word_t x, hash, table, link, next;
table = stx->symbol_table;
hash = xp_stx_strhash(name) % XP_STX_SIZE(stx,table);
link = XP_STX_AT(stx,table,hash);
if (link == stx->nil) {
x = xp_stx_alloc_string_object (stx, name);
XP_STX_CLASS(stx,x) = stx->class_symbol;
2005-05-18 04:01:51 +00:00
XP_STX_AT(stx,table,hash) = xp_stx_new_symlink(stx,x);
2005-05-17 16:18:56 +00:00
}
else {
do {
2005-05-18 04:01:51 +00:00
x = XP_STX_AT(stx,link,SYMLINK_SYMBOL);
2005-05-19 16:41:10 +00:00
xp_stx_assert (XP_STX_CLASS(stx,x) == stx->class_symbol);
2005-05-17 16:18:56 +00:00
if (xp_stx_strxcmp (
&XP_STX_CHARAT(stx,x,0),
XP_STX_SIZE(stx,x), name) == 0) return x;
2005-05-18 04:01:51 +00:00
next = XP_STX_AT(stx,link,SYMLINK_LINK);
2005-05-17 16:18:56 +00:00
if (next == stx->nil) {
x = xp_stx_alloc_string_object (stx, name);
XP_STX_CLASS(stx,x) = stx->class_symbol;
2005-05-18 04:01:51 +00:00
XP_STX_AT(stx,link,SYMLINK_LINK) =
xp_stx_new_symlink(stx,x);
2005-05-17 16:18:56 +00:00
break;
}
link = next;
} while (1);
}
return x;
}
xp_stx_word_t xp_stx_new_symbol_pp (
xp_stx_t* stx, const xp_stx_char_t* name,
const xp_stx_char_t* prefix, const xp_stx_char_t* postfix)
{
xp_stx_word_t x, hash, table, link, next;
table = stx->symbol_table;
hash = xp_stx_strhash(name) % XP_STX_SIZE(stx,table);
link = XP_STX_AT(stx,table,hash);
if (link == stx->nil) {
x = xp_stx_allocn_string_object (stx, prefix, name, postfix);
XP_STX_CLASS(stx,x) = stx->class_symbol;
2005-05-18 04:01:51 +00:00
XP_STX_AT(stx,table,hash) = xp_stx_new_symlink(stx,x);
2005-05-17 16:18:56 +00:00
}
else {
do {
2005-05-18 04:01:51 +00:00
x = XP_STX_AT(stx,link,SYMLINK_SYMBOL);
2005-05-19 16:41:10 +00:00
xp_stx_assert (XP_STX_CLASS(stx,x) == stx->class_symbol);
2005-05-17 16:18:56 +00:00
if (xp_stx_strxcmp (
&XP_STX_CHARAT(stx,x,0),
XP_STX_SIZE(stx,x), name) == 0) return x;
2005-05-18 04:01:51 +00:00
next = XP_STX_AT(stx,link,SYMLINK_LINK);
2005-05-17 16:18:56 +00:00
if (next == stx->nil) {
x = xp_stx_allocn_string_object (stx, prefix, name, postfix);
XP_STX_CLASS(stx,x) = stx->class_symbol;
2005-05-18 04:01:51 +00:00
XP_STX_AT(stx,link,SYMLINK_LINK) =
xp_stx_new_symlink(stx,x);
2005-05-17 16:18:56 +00:00
break;
}
link = next;
} while (1);
}
return x;
}
2005-05-18 04:01:51 +00:00
void xp_stx_traverse_symbol_table (
xp_stx_t* stx, void (*func) (xp_stx_t*,xp_stx_word_t))
{
xp_stx_word_t link;
xp_stx_word_t size;
xp_stx_word_t table;
table = stx->symbol_table;
size = XP_STX_SIZE(stx,table);
while (size-- > 0) {
link = XP_STX_AT(stx,table,size);
while (link != stx->nil) {
func (stx,XP_STX_AT(stx,link,SYMLINK_SYMBOL));
link = XP_STX_AT(stx,link,SYMLINK_LINK);
}
}
}