fixed bugs in lda and changed awk for lda over tab.

- added ase_lda_rwalk
 - fixed crash when the keeper is not set
 - modified the awk parser to utilize lda.
This commit is contained in:
hyung-hwan 2008-10-12 08:10:25 +00:00
parent 7a05ceea75
commit 204f833d16
4 changed files with 160 additions and 63 deletions

View File

@ -19,8 +19,16 @@
****** ******
*/ */
enum ase_lda_walk_t
{
ASE_LDA_WALK_STOP = 0,
ASE_LDA_WALK_FORWARD = 1,
ASE_LDA_WALK_BACKWARD = 2
};
typedef struct ase_lda_t ase_lda_t; typedef struct ase_lda_t ase_lda_t;
typedef struct ase_lda_node_t ase_lda_node_t; typedef struct ase_lda_node_t ase_lda_node_t;
typedef enum ase_lda_walk_t ase_lda_walk_t;
#define ASE_LDA_COPIER_SIMPLE ase_lda_copysimple #define ASE_LDA_COPIER_SIMPLE ase_lda_copysimple
#define ASE_LDA_COPIER_INLINE ase_lda_copyinline #define ASE_LDA_COPIER_INLINE ase_lda_copyinline
@ -143,6 +151,11 @@ typedef ase_size_t (*ase_lda_sizer_t) (
); );
/******/ /******/
typedef ase_lda_walk_t (*ase_lda_walker_t) (
ase_lda_t* lda /* a linear dynamic array */,
ase_size_t index /* the index to the visited node */,
void* arg /* user-defined data */
);
/****s* ase.cmn.lda/ase_lda_t /****s* ase.cmn.lda/ase_lda_t
* NAME * NAME
@ -336,6 +349,15 @@ void ase_lda_setfreeer (
); );
/******/ /******/
ase_lda_keeper_t ase_lda_getkeeper (
ase_lda_t* lda
);
void ase_lda_setkeeper (
ase_lda_t* lda,
ase_lda_keeper_t keeper
);
ase_lda_sizer_t ase_lda_getsizer ( ase_lda_sizer_t ase_lda_getsizer (
ase_lda_t* lda ase_lda_t* lda
); );
@ -435,6 +457,19 @@ void ase_lda_clear (
ase_lda_t* lda ase_lda_t* lda
); );
void ase_lda_walk (
ase_lda_t* lda,
ase_lda_walker_t walker,
void* arg
);
void ase_lda_rwalk (
ase_lda_t* lda,
ase_lda_walker_t walker,
void* arg
);
void* ase_lda_copysimple ( void* ase_lda_copysimple (
ase_lda_t* lda /* a linear dynamic array */, ase_lda_t* lda /* a linear dynamic array */,
void* data /* the pointer to data */, void* data /* the pointer to data */,

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c 415 2008-10-10 11:16:31Z baconevi $ * $Id: parse.c 416 2008-10-11 14:10:25Z baconevi $
* *
* {License} * {License}
*/ */
@ -1421,79 +1421,77 @@ static void adjust_static_globals (ase_awk_t* awk)
} }
} }
typedef struct check_global_t check_global_t;
struct check_global_t
{
ase_cstr_t name;
ase_size_t index;
ase_lda_walk_t walk;
};
static ase_lda_walk_t check_global (ase_lda_t* lda, ase_size_t index, void* arg)
{
ase_cstr_t tmp;
ase_awk_t* awk = *(ase_awk_t**)ASE_LDA_EXTENSION(lda);
check_global_t* cg = (check_global_t*)arg;
tmp.ptr = ASE_LDA_DPTR(lda,index);
tmp.len = ASE_LDA_DLEN(lda,index);
if (index < awk->tree.nbglobals)
{
ase_map_pair_t* pair;
pair = ase_map_search (awk->wtab, tmp.ptr, tmp.len);
if (pair != ASE_NULL)
{
tmp.ptr = ((ase_cstr_t*)(pair->vptr))->ptr;
tmp.len = ((ase_cstr_t*)(pair->vptr))->len;
}
}
if (ase_strxncmp(tmp.ptr, tmp.len, cg->name.ptr, cg->name.len) == 0)
{
cg->index = index;
return ASE_LDA_WALK_STOP;
}
return cg->walk;
}
static ase_size_t get_global ( static ase_size_t get_global (
ase_awk_t* awk, const ase_char_t* name, ase_size_t len) ase_awk_t* awk, const ase_char_t* name, ase_size_t len)
{ {
ase_size_t i; check_global_t cg;
cg.name.ptr = name;
cg.name.len = len;
cg.index = ASE_LDA_INVALID;
cg.walk = ASE_LDA_WALK_BACKWARD;
/* return ase_lda_rsearch ( /* return ase_lda_rsearch (
awk->parse.globals, ASE_LDA_SIZE(awk->parse.globals), awk->parse.globals, ASE_LDA_SIZE(awk->parse.globals),
name, len); */ name, len); */
if (ASE_LDA_SIZE(awk->parse.globals) > 0) ase_lda_rwalk (awk->parse.globals, check_global, &cg);
{ return cg.index;
for (i = ASE_LDA_SIZE(awk->parse.globals); i-- > 0; )
{
if (ASE_LDA_NODE(awk->parse.globals,i))
{
ase_cstr_t tmp;
tmp.ptr = ASE_LDA_DPTR(awk->parse.globals,i);
tmp.len = ASE_LDA_DLEN(awk->parse.globals,i);
if (i < awk->tree.nbglobals)
{
ase_map_pair_t* pair;
pair = ase_map_search (awk->wtab, tmp.ptr, tmp.len);
if (pair != ASE_NULL)
{
tmp.ptr = ((ase_cstr_t*)(pair->vptr))->ptr;
tmp.len = ((ase_cstr_t*)(pair->vptr))->len;
}
}
if (ase_strxncmp(tmp.ptr, tmp.len, name, len) == 0) return i;
}
}
}
return ASE_LDA_INVALID;
} }
static ase_size_t find_global ( static ase_size_t find_global (
ase_awk_t* awk, const ase_char_t* name, ase_size_t len) ase_awk_t* awk, const ase_char_t* name, ase_size_t len)
{ {
ase_size_t i; check_global_t cg;
cg.name.ptr = name;
cg.name.len = len;
cg.index = ASE_LDA_INVALID;
cg.walk = ASE_LDA_WALK_FORWARD;
/* return ase_lda_search (awk->parse.globals, 0, name, len); */ /* return ase_lda_search (awk->parse.globals, 0, name, len); */
for (i = 0; i < ASE_LDA_SIZE(awk->parse.globals); i++) ase_lda_walk (awk->parse.globals, check_global, &cg);
{ return cg.index;
if (ASE_LDA_NODE(awk->parse.globals,i))
{
ase_cstr_t tmp;
tmp.ptr = ASE_LDA_DPTR(awk->parse.globals,i);
tmp.len = ASE_LDA_DLEN(awk->parse.globals,i);
if (i < awk->tree.nbglobals)
{
ase_map_pair_t* pair;
pair = ase_map_search (awk->wtab, tmp.ptr, tmp.len);
if (pair != ASE_NULL)
{
tmp.ptr = ((ase_cstr_t*)(pair->vptr))->ptr;
tmp.len = ((ase_cstr_t*)(pair->vptr))->len;
}
}
if (ase_strxncmp(tmp.ptr, tmp.len, name, len) == 0) return i;
}
}
return ASE_LDA_INVALID;
} }
static int add_global ( static int add_global (

View File

@ -13,6 +13,7 @@
#define freeer_t ase_lda_freeer_t #define freeer_t ase_lda_freeer_t
#define comper_t ase_lda_comper_t #define comper_t ase_lda_comper_t
#define sizer_t ase_lda_sizer_t #define sizer_t ase_lda_sizer_t
#define keeper_t ase_lda_keeper_t
#define walker_t ase_lda_walker_t #define walker_t ase_lda_walker_t
#define mmgr_t ase_mmgr_t #define mmgr_t ase_mmgr_t
@ -195,6 +196,16 @@ void ase_lda_setcomper (lda_t* lda, comper_t comper)
lda->comper = comper; lda->comper = comper;
} }
keeper_t ase_lda_getkeeper (lda_t* lda)
{
return lda->keeper;
}
void ase_lda_setkeeper (lda_t* lda, keeper_t keeper)
{
lda->keeper = keeper;
}
sizer_t ase_lda_getsizer (lda_t* lda) sizer_t ase_lda_getsizer (lda_t* lda)
{ {
return lda->sizer; return lda->sizer;
@ -353,7 +364,7 @@ size_t ase_lda_insert (lda_t* lda, size_t pos, void* dptr, size_t dlen)
} }
} }
if (pos >= lda->capa) if (pos >= lda->capa || lda->size >= lda->capa)
{ {
/* the buffer is not still enough after resizing */ /* the buffer is not still enough after resizing */
if (lda->freeer) if (lda->freeer)
@ -395,7 +406,7 @@ size_t ase_lda_update (lda_t* lda, size_t pos, void* dptr, size_t dlen)
if (dptr == DPTR(c) && dlen == DLEN(c)) if (dptr == DPTR(c) && dlen == DLEN(c))
{ {
/* updated to the same data */ /* updated to the same data */
lda->keeper (lda, dptr, dlen); if (lda->keeper) lda->keeper (lda, dptr, dlen);
} }
else else
{ {
@ -492,6 +503,59 @@ void ase_lda_clear (lda_t* lda)
lda->size = 0; lda->size = 0;
} }
void ase_lda_walk (lda_t* lda, walker_t walker, void* arg)
{
ase_lda_walk_t w = ASE_LDA_WALK_FORWARD;
size_t i = 0;
while (1)
{
if (lda->node[i] != ASE_NULL)
w = walker (lda, i, arg);
if (w == ASE_LDA_WALK_STOP) return;
if (w == ASE_LDA_WALK_FORWARD)
{
i++;
if (i >= lda->size) return;
}
if (w == ASE_LDA_WALK_BACKWARD)
{
if (i <= 0) return;
i--;
}
}
}
void ase_lda_rwalk (lda_t* lda, walker_t walker, void* arg)
{
ase_lda_walk_t w = ASE_LDA_WALK_BACKWARD;
size_t i;
if (lda->size <= 0) return;
i = lda->size - 1;
while (1)
{
if (lda->node[i] != ASE_NULL)
w = walker (lda, i, arg);
if (w == ASE_LDA_WALK_STOP) return;
if (w == ASE_LDA_WALK_FORWARD)
{
i++;
if (i >= lda->size) return;
}
if (w == ASE_LDA_WALK_BACKWARD)
{
if (i <= 0) return;
i--;
}
}
}
void* ase_lda_copysimple (lda_t* lda, void* dptr, size_t dlen) void* ase_lda_copysimple (lda_t* lda, void* dptr, size_t dlen)
{ {
return dptr; return dptr;