From 6c25664a7eae5b5c7368675f53baaa59c90715f5 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 30 Jan 2006 13:25:26 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/Makefile.cl | 2 +- ase/awk/hash.c | 158 +++++++++++++++++++++++++++++++++++++++++++- ase/awk/hash.h | 20 ++++-- ase/awk/parse.c | 11 ++- ase/awk/sa.h | 11 +-- 5 files changed, 188 insertions(+), 14 deletions(-) diff --git a/ase/awk/Makefile.cl b/ase/awk/Makefile.cl index 7936f7de..d4d4a587 100644 --- a/ase/awk/Makefile.cl +++ b/ase/awk/Makefile.cl @@ -1,4 +1,4 @@ -SRCS = awk.c tree.c tab.c parse.c run.c sa.c +SRCS = awk.c tree.c tab.c hash.c parse.c run.c sa.c OBJS = $(SRCS:.c=.obj) OUT = xpawk.lib diff --git a/ase/awk/hash.c b/ase/awk/hash.c index aa719fb8..2ae56303 100644 --- a/ase/awk/hash.c +++ b/ase/awk/hash.c @@ -1,5 +1,161 @@ /* - * $Id: hash.c,v 1.1 2005-11-07 16:02:44 bacon Exp $ + * $Id: hash.c,v 1.2 2006-01-30 13:25:26 bacon Exp $ */ #include + +// TODO: improve the entire hash routines. +// support automatic bucket resizing and rehashing, etc. + +static xp_size_t __hash (const xp_char_t* key); + +xp_awk_hash_t* xp_awk_hash_open (xp_awk_hash_t* hash, xp_size_t capa) +{ + if (hash == XP_NULL) { + hash = (xp_awk_hash_t*) xp_malloc (xp_sizeof(xp_awk_hash_t)); + if (hash == XP_NULL) return XP_NULL; + hash->__dynamic = xp_true; + } + else hash->__dynamic = xp_false; + + hash->buck = (xp_awk_pair_t**) xp_malloc (xp_sizeof(xp_awk_pair_t*) * capa); + if (hash->buck == XP_NULL) { + if (hash->__dynamic) xp_free (hash); + return XP_NULL; + } + + hash->capa = capa; + hash->size = 0; + while (capa > 0) hash->buck[--capa] = XP_NULL; + + return hash; +} + +void xp_awk_hash_close (xp_awk_hash_t* hash) +{ + xp_awk_hash_clear (hash); + xp_free (hash->buck); + if (hash->__dynamic) xp_free (hash); +} + +void xp_awk_hash_clear (xp_awk_hash_t* hash) +{ + xp_size_t i; + xp_awk_pair_t* pair, * next; + + for (i = 0; i < hash->capa; i++) { + pair = hash->buck[i]; + + while (pair != XP_NULL) { + next = pair->next; + xp_free (pair); + hash->size--; + pair = next; + } + + hash->buck[i] = XP_NULL; + } +} + +xp_awk_pair_t* xp_awk_hash_get (xp_awk_hash_t* hash, const xp_char_t* key) +{ + xp_awk_pair_t* pair; + xp_size_t hc; + + hc = __hash(key) % hash->capa; + pair = hash->buck[hc]; + + while (pair != XP_NULL) { + if (xp_strcmp(pair->key,key) == 0) return pair; + pair = pair->next; + } + + return XP_NULL; +} + +xp_awk_pair_t* xp_awk_hash_put (xp_awk_hash_t* hash, const xp_char_t* key, void* value) +{ + xp_awk_pair_t* pair; + xp_size_t hc; + + hc = __hash(key) % hash->capa; + pair = hash->buck[hc]; + + while (pair != XP_NULL) { + if (xp_strcmp(pair->key,key) == 0) { + pair->value = value; + return pair; + } + pair = pair->next; + } + + pair = (xp_awk_pair_t*) xp_malloc (xp_sizeof(xp_awk_pair_t)); + if (pair == XP_NULL) return XP_NULL; + + pair->key = key; + pair->value = value; + pair->next = hash->buck[hc]; + hash->buck[hc] = pair; + hash->size++; + + return pair; +} + +xp_awk_pair_t* xp_awk_hash_set (xp_awk_hash_t* hash, const xp_char_t* key, void* value) +{ + xp_awk_pair_t* pair; + xp_size_t hc; + + hc = __hash(key) % hash->capa; + pair = hash->buck[hc]; + + while (pair != XP_NULL) { + if (xp_strcmp(pair->key,key) == 0) { + pair->value = value; + return pair; + } + pair = pair->next; + } + + return XP_NULL; +} + +int xp_awk_hash_remove (xp_awk_hash_t* hash, const xp_char_t* key) +{ + xp_awk_pair_t* pair, * prev; + xp_size_t hc; + + hc = __hash(key) % hash->capa; + pair = hash->buck[hc]; + prev = XP_NULL; + + while (pair != XP_NULL) { + if (xp_strcmp(pair->key,key) == 0) { + if (prev == XP_NULL) + hash->buck[hc] = pair->next; + else prev->next = pair->next; + + xp_free (pair); + hash->size--; + return 0; + } + + prev = pair; + pair = pair->next; + } + + return -1; +} + +static xp_size_t __hash (const xp_char_t* key) +{ + xp_size_t n = 0, i; + + while (*key != XP_CHAR('\0')) { + xp_byte_t* bp = (xp_byte_t*)key; + for (i = 0; i < xp_sizeof(*key); i++) n = n * 31 + *bp++; + key++; + } + + return n; +} diff --git a/ase/awk/hash.h b/ase/awk/hash.h index cac66720..5c60d29d 100644 --- a/ase/awk/hash.h +++ b/ase/awk/hash.h @@ -1,5 +1,5 @@ /* - * $Id: hash.h,v 1.2 2006-01-29 18:28:14 bacon Exp $ + * $Id: hash.h,v 1.3 2006-01-30 13:25:26 bacon Exp $ */ #ifndef _XP_AWK_HASH_H_ @@ -13,18 +13,20 @@ #endif typedef struct xp_awk_hash_t xp_awk_hash_t; -typedef struct xp_awk_hashdatum_t xp_awk_hashdatum_t; +typedef struct xp_awk_pair_t xp_awk_pair_t; -struct xp_awk_hashdatum_t +struct xp_awk_pair_t { - xp_char_t* key; + const xp_char_t* key; void* value; + xp_awk_pair_t* next; }; struct xp_awk_hash_t { xp_size_t size; xp_size_t capa; + xp_awk_pair_t** buck; xp_bool_t __dynamic; }; @@ -32,6 +34,16 @@ struct xp_awk_hash_t extern "C" { #endif +xp_awk_hash_t* xp_awk_hash_open (xp_awk_hash_t* hash, xp_size_t capa); +void xp_awk_hash_close (xp_awk_hash_t* hash); + +void xp_awk_hash_clear (xp_awk_hash_t* hash); + +xp_awk_pair_t* xp_awk_hash_get (xp_awk_hash_t* hash, const xp_char_t* key); +xp_awk_pair_t* xp_awk_hash_put (xp_awk_hash_t* hash, const xp_char_t* key, void* value); +xp_awk_pair_t* xp_awk_hash_set (xp_awk_hash_t* hash, const xp_char_t* key, void* value); + +int xp_awk_hash_remove (xp_awk_hash_t* hash, const xp_char_t* key); #ifdef __cplusplus } diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 1e4e2b15..b92edbbe 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.37 2006-01-29 18:28:14 bacon Exp $ + * $Id: parse.c,v 1.38 2006-01-30 13:25:26 bacon Exp $ */ #include @@ -120,6 +120,8 @@ static xp_long_t __str_to_long (const xp_char_t* name); static INLINE xp_size_t __add_func_name (xp_awk_t* awk, const xp_char_t* name); static INLINE xp_size_t __find_func_name (xp_awk_t* awk, const xp_char_t* name); +static INLINE int __remove_func_name (xp_awk_t* awk, xp_size_t index); + static INLINE xp_size_t __find_func_arg (xp_awk_t* awk, const xp_char_t* name); static INLINE xp_size_t __find_variable (xp_awk_t* awk, const xp_char_t* name); @@ -195,16 +197,19 @@ int xp_awk_parse (xp_awk_t* awk) GET_TOKEN (awk); while (1) { + xp_awk_node_t* unit; + if (MATCH(awk,TOKEN_EOF)) break; - if (__parse_progunit(awk) == XP_NULL) { + unit = __parse_progunit(awk); + if (unit == XP_NULL) { // TODO: cleanup the parse tree created so far.... // function tables also etc... xp_printf (XP_TEXT("error - %d\n"), awk->errnum); return -1; } -xp_awk_prnpt(node); +xp_awk_prnpt(unit); } diff --git a/ase/awk/sa.h b/ase/awk/sa.h index 598c0654..e4cb2c35 100644 --- a/ase/awk/sa.h +++ b/ase/awk/sa.h @@ -1,5 +1,5 @@ /* - * $Id: sa.h,v 1.8 2006-01-29 18:28:14 bacon Exp $ + * $Id: sa.h,v 1.9 2006-01-30 13:25:26 bacon Exp $ */ #ifndef _XP_AWK_SA_H_ @@ -53,10 +53,11 @@ #define xp_true (0 == 0) #define xp_false (0 != 0) -typedef int xp_bool_t; -typedef wchar_t xp_char_t; -typedef wint_t xp_cint_t; -typedef size_t xp_size_t; +typedef unsigned char xp_byte_t; +typedef int xp_bool_t; +typedef wchar_t xp_char_t; +typedef wint_t xp_cint_t; +typedef size_t xp_size_t; #if defined(_WIN32) || defined(vms) || defined(__vms) typedef long xp_ssize_t;