This commit is contained in:
hyung-hwan 2008-08-29 08:21:25 +00:00
parent 616ddad7d7
commit 2390cda730
10 changed files with 115 additions and 72 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h 337 2008-08-20 09:17:25Z baconevi $
* $Id: awk.h 349 2008-08-28 14:21:25Z baconevi $
*
* {License}
*/
@ -592,8 +592,9 @@ extern ase_awk_val_t* ase_awk_val_one;
*/
ase_awk_t* ase_awk_open (
ase_mmgr_t* mmgr /* memory manager */,
ase_size_t extension /* size of extension area in bytes */,
void (*initializer) (ase_awk_t*) /* extension area initializer */
ase_size_t ext /* size of extension area in bytes */,
void (*init) (ase_awk_t*, void*) /* extension initializer */,
void* init_data /* the second argument to the extension initializer */
);
/*

View File

@ -93,7 +93,8 @@ extern "C" {
ase_dll_t* ase_dll_open (
ase_mmgr_t* mmgr /* memory manager */ ,
ase_size_t ext /* size of extension area in bytes */,
void (*init) (ase_dll_t*) /* extension initializer */
void (*init) (ase_dll_t*, void*) /* extension initializer */,
void* init_data /* the second argument to the extension initializer */
);
/*

View File

@ -1,5 +1,5 @@
/*
* $Id: map.h 348 2008-08-28 10:29:53Z baconevi $
* $Id: map.h 349 2008-08-28 14:21:25Z baconevi $
*
* {License}
*/
@ -116,8 +116,9 @@ extern "C" {
ase_map_t* ase_map_open (
ase_mmgr_t* mmgr,
ase_size_t ext,
void (*init) (ase_map_t*),
ase_size_t init_capa /* initial capacity */,
void (*init) (ase_map_t*, void*),
void* init_arg,
ase_size_t capa /* initial capacity */,
unsigned int load_factor /* load factor */
);

View File

@ -92,7 +92,8 @@ extern "C" {
ase_sll_t* ase_sll_open (
ase_mmgr_t* mmgr /* memory manager */ ,
ase_size_t ext /* size of extension area in bytes */,
void (*init) (ase_sll_t*) /* extension initializer */
void (*init) (ase_sll_t*, void*) /* extension initializer */,
void* init_data /* the second argument to the extension initializer */
);
/*

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c 348 2008-08-28 10:29:53Z baconevi $
* $Id: awk.c 349 2008-08-28 14:21:25Z baconevi $
*
* {License}
*/
@ -26,15 +26,13 @@ static void free_bfn (void* awk, void* afn);
static void init_map (ase_map_t* map, void* arg)
{
awk_t** p = getextension (map);
*p = awk;
ase_awk_t** p = ase_map_getextension (map);
*p = arg;
}
ase_awk_t* ase_awk_open (
ase_mmgr_t* mmgr,
ase_size_t ext,
void (*init) (ase_awk_t*, void*),
void* init_arg)
ase_mmgr_t* mmgr, ase_size_t ext,
void (*init) (ase_awk_t*, void*), void* init_data)
{
ase_awk_t* awk;
@ -60,7 +58,9 @@ ase_awk_t* ase_awk_open (
return ASE_NULL;
}
awk->wtab = ase_map_open (awk, 512, 70, free_word, ASE_NULL, mmgr);
/*awk->wtab = ase_map_open (awk, 512, 70, free_word, ASE_NULL, mmgr);*/
awk->wtab = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->wtab == ASE_NULL)
{
ase_str_close (&awk->token.name);
@ -68,7 +68,9 @@ ase_awk_t* ase_awk_open (
return ASE_NULL;
}
awk->rwtab = ase_map_open (awk, 512, 70, free_word, ASE_NULL, mmgr);
/*awk->rwtab = ase_map_open (awk, 512, 70, free_word, ASE_NULL, mmgr);*/
awk->rwtab = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->rwtab == ASE_NULL)
{
ase_map_close (awk->wtab);
@ -81,7 +83,7 @@ ase_awk_t* ase_awk_open (
/*awk->tree.afns = ase_map_open (awk, 512, 70, free_afn, ASE_NULL, mmgr);*/
awk->tree.afns = ase_map_open (
mmgr, ASE_SIZEOF(awk), init_map, awk, 512, 70);
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->tree.afns == ASE_NULL)
{
ase_map_close (awk->rwtab);
@ -91,7 +93,9 @@ ase_awk_t* ase_awk_open (
return ASE_NULL;
}
awk->parse.afns = ase_map_open (awk, 256, 70, ASE_NULL, ASE_NULL, mmgr);
/*awk->parse.afns = ase_map_open (awk, 256, 70, ASE_NULL, ASE_NULL, mmgr);*/
awk->parse.afns = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 256, 70);
if (awk->parse.afns == ASE_NULL)
{
ase_map_close (awk->tree.afns);
@ -102,7 +106,9 @@ ase_awk_t* ase_awk_open (
return ASE_NULL;
}
awk->parse.named = ase_map_open (awk, 256, 70, ASE_NULL, ASE_NULL, mmgr);
/*awk->parse.named = ase_map_open (awk, 256, 70, ASE_NULL, ASE_NULL, mmgr);*/
awk->parse.named = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 256, 70);
if (awk->parse.named == ASE_NULL)
{
ase_map_close (awk->parse.afns);
@ -186,7 +192,9 @@ ase_awk_t* ase_awk_open (
awk->bfn.sys = ASE_NULL;
/*awk->bfn.user = ASE_NULL;*/
awk->bfn.user = ase_map_open (awk, 512, 70, free_bfn, ASE_NULL, mmgr);
/*awk->bfn.user = ase_map_open (awk, 512, 70, free_bfn, ASE_NULL, mmgr);*/
awk->bfn.user = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->bfn.user == ASE_NULL)
{
ase_awk_tab_close (&awk->parse.params);
@ -231,7 +239,7 @@ ase_awk_t* ase_awk_open (
return ASE_NULL;
}
if (init) init (awk);
if (init) init (awk, init_data);
return awk;
}
@ -433,13 +441,13 @@ int ase_awk_getword (ase_awk_t* awk,
const ase_char_t* okw, ase_size_t olen,
const ase_char_t** nkw, ase_size_t* nlen)
{
ase_pair_t* p;
ase_map_pair_t* p;
p = ase_map_get (awk->wtab, okw, olen);
if (p == ASE_NULL) return -1;
*nkw = ((ase_cstr_t*)p->val)->ptr;
*nlen = ((ase_cstr_t*)p->val)->len;
*nkw = ((ase_cstr_t*)p->vptr)->ptr;
*nlen = ((ase_cstr_t*)p->vptr)->len;
return 0;
}
@ -452,7 +460,7 @@ int ase_awk_setword (ase_awk_t* awk,
if (nkw == ASE_NULL || nlen == 0)
{
ase_pair_t* p;
ase_map_pair_t* p;
if (okw == ASE_NULL || olen == 0)
{
@ -466,7 +474,7 @@ int ase_awk_setword (ase_awk_t* awk,
p = ase_map_get (awk->wtab, okw, olen);
if (p != ASE_NULL)
{
ase_cstr_t* s = (ase_cstr_t*)p->val;
ase_cstr_t* s = (ase_cstr_t*)p->vptr;
ase_map_remove (awk->rwtab, s->ptr, s->len);
ase_map_remove (awk->wtab, okw, olen);
return 0;
@ -484,6 +492,7 @@ int ase_awk_setword (ase_awk_t* awk,
}
/* set the word */
#if 0
vn = (ase_cstr_t*) ASE_AWK_ALLOC (
awk, ASE_SIZEOF(ase_cstr_t)+((nlen+1)*ASE_SIZEOF(*nkw)));
if (vn == ASE_NULL)
@ -507,7 +516,7 @@ int ase_awk_setword (ase_awk_t* awk,
vo->ptr = (const ase_char_t*)(vo + 1);
ase_strncpy ((ase_char_t*)vo->ptr, okw, olen);
if (ase_map_put (awk->wtab, okw, olen, vn) == ASE_NULL)
if (ase_map_put (awk->wtab, (void*)okw, olen, vn) == ASE_NULL)
{
ASE_AWK_FREE (awk, vo);
ASE_AWK_FREE (awk, vn);
@ -522,6 +531,22 @@ int ase_awk_setword (ase_awk_t* awk,
SETERR (awk, ASE_AWK_ENOMEM);
return -1;
}
#endif
if (ase_map_put (awk->wtab, (void*)okw, olen, nkw, nlen) == ASE_NULL)
{
ASE_AWK_FREE (awk, vo);
ASE_AWK_FREE (awk, vn);
SETERR (awk, ASE_AWK_ENOMEM);
return -1;
}
if (ase_map_put (awk->rwtab, nkw, nlen, okw, olen) == ASE_NULL)
{
ase_map_remove (awk->wtab, okw, olen);
ASE_AWK_FREE (awk, vo);
SETERR (awk, ASE_AWK_ENOMEM);
return -1;
}
return 0;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c 337 2008-08-20 09:17:25Z baconevi $
* $Id: parse.c 349 2008-08-28 14:21:25Z baconevi $
*
* {License}
*/
@ -226,7 +226,7 @@ static int is_plain_var (ase_awk_nde_t* nde);
static int is_var (ase_awk_nde_t* nde);
static int deparse (ase_awk_t* awk);
static int deparse_func (ase_pair_t* pair, void* arg);
static int deparse_func (ase_map_pair_t* pair, void* arg);
static int put_char (ase_awk_t* awk, ase_char_t c);
static int flush_out (ase_awk_t* awk);
@ -458,7 +458,7 @@ const ase_char_t* ase_awk_getglobalname (
const ase_char_t* ase_awk_getkw (ase_awk_t* awk, const ase_char_t* kw)
{
ase_pair_t* p;
ase_map_pair_t* p;
ASE_ASSERT (kw != ASE_NULL);
@ -539,7 +539,7 @@ static int parse (ase_awk_t* awk)
if ((awk->option & ASE_AWK_EXPLICIT) &&
!(awk->option & ASE_AWK_IMPLICIT))
{
ase_pair_t* p;
ase_map_pair_t* p;
ase_size_t buckno;
p = ase_map_getfirstpair (awk->parse.afns, &buckno);
@ -819,7 +819,7 @@ static ase_awk_nde_t* parse_function (ase_awk_t* awk)
ase_awk_nde_t* body;
ase_awk_afn_t* afn;
ase_size_t nargs, g;
ase_pair_t* pair;
ase_map_pair_t* pair;
int n;
/* eat up the keyword 'function' and get the next token */
@ -1423,13 +1423,13 @@ static void trans_global (
*/
if (index < awk->tree.nbglobals)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
pair = ase_map_get (awk->wtab, word->ptr, word->len);
if (pair != ASE_NULL)
{
word->ptr = ((ase_cstr_t*)(pair->val))->ptr;
word->len = ((ase_cstr_t*)(pair->val))->len;
word->ptr = ((ase_cstr_t*)(pair->vptr))->ptr;
word->len = ((ase_cstr_t*)(pair->vptr))->len;
}
}
}
@ -5376,7 +5376,7 @@ static int classify_ident (
ase_awk_t* awk, const ase_char_t* name, ase_size_t len)
{
kwent_t* kwp;
ase_pair_t* pair;
ase_map_pair_t* pair;
for (kwp = kwtab; kwp->name != ASE_NULL; kwp++)
{
@ -5389,8 +5389,8 @@ static int classify_ident (
pair = ase_map_get (awk->wtab, kwp->name, kwp->name_len);
if (pair != ASE_NULL)
{
k = ((ase_cstr_t*)(pair->val))->ptr;
l = ((ase_cstr_t*)(pair->val))->len;
k = ((ase_cstr_t*)(pair->vptr))->ptr;
l = ((ase_cstr_t*)(pair->vptr))->len;
}
else
{
@ -5462,6 +5462,7 @@ struct deparse_func_t
ase_awk_t* awk;
ase_char_t* tmp;
ase_size_t tmp_len;
int ret;
};
static int deparse (ase_awk_t* awk)
@ -5589,8 +5590,10 @@ static int deparse (ase_awk_t* awk)
df.awk = awk;
df.tmp = tmp;
df.tmp_len = ASE_COUNTOF(tmp);
df.ret = 0;
if (ase_map_walk (awk->tree.afns, deparse_func, &df) == -1)
ase_map_walk (awk->tree.afns, deparse_func, &df);
if (df.ret == -1)
{
EXIT_DEPARSE ();
}
@ -5688,18 +5691,24 @@ exit_deparse:
return n;
}
static int deparse_func (ase_pair_t* pair, void* arg)
#define PUTSRCSTR(x,str) \
if (ase_awk_putsrcstr (x->awk, str) == -1) { \
x->ret = -1; \
return ASE_MAP_WALK_STOP; \
}
static int deparse_func (ase_map_pair_t* pair, void* arg)
{
struct deparse_func_t* df = (struct deparse_func_t*)arg;
ase_awk_afn_t* afn = (ase_awk_afn_t*)pair->val;
ase_awk_afn_t* afn = (ase_awk_afn_t*)pair->vptr;
ase_size_t i, n;
ASE_ASSERT (ase_strxncmp (ASE_PAIR_KEYPTR(pair), ASE_PAIR_KEYLEN(pair), afn->name, afn->name_len) == 0);
if (ase_awk_putsrcstr(df->awk,ase_awk_getkw(df->awk,ASE_T("function"))) == -1) return -1;
if (ase_awk_putsrcstr (df->awk, ASE_T(" ")) == -1) return -1;
if (ase_awk_putsrcstr (df->awk, afn->name) == -1) return -1;
if (ase_awk_putsrcstr (df->awk, ASE_T(" (")) == -1) return -1;
PUTSRCSTR (df, ase_awk_getkw(df->awk,ASE_T("function")));
PUTSRCSTR (df, ASE_T(" "));
PUTSRCSTR (df, afn->name);
PUTSRCSTR (df, ASE_T(" ("));
for (i = 0; i < afn->nargs; )
{
@ -5707,11 +5716,12 @@ static int deparse_func (ase_pair_t* pair, void* arg)
ASE_T("__p"), df->tmp, df->tmp_len);
ASE_ASSERT (n != (ase_size_t)-1);
if (ase_awk_putsrcstrx (df->awk, df->tmp, n) == -1) return -1;
if (i >= afn->nargs) break;
if (ase_awk_putsrcstr (df->awk, ASE_T(", ")) == -1) return -1;
PUTSRCSTR (df, ASE_T(", "));
}
if (ase_awk_putsrcstr (df->awk, ASE_T(")")) == -1) return -1;
PUTSRCSTR (df, ASE_T(")"));
if (df->awk->option & ASE_AWK_CRLF)
{
if (put_char (df->awk, ASE_T('\r')) == -1) return -1;
@ -5725,7 +5735,7 @@ static int deparse_func (ase_pair_t* pair, void* arg)
}
if (put_char (df->awk, ASE_T('\n')) == -1) return -1;
return 0;
return ASE_MAP_WALK_FORWARD;
}
static int put_char (ase_awk_t* awk, ase_char_t c)

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 337 2008-08-20 09:17:25Z baconevi $
* $Id: run.c 349 2008-08-28 14:21:25Z baconevi $
*
* {License}
*/
@ -2189,7 +2189,7 @@ struct __foreach_walker_t
ase_awk_nde_t* body;
};
static int __walk_foreach (ase_pair_t* pair, void* arg)
static int __walk_foreach (ase_map_pair_t* pair, void* arg)
{
struct __foreach_walker_t* w = (struct __foreach_walker_t*)arg;
ase_awk_val_t* str;
@ -2441,7 +2441,7 @@ static int run_delete (ase_awk_run_t* run, ase_awk_nde_delete_t* nde)
if (var->type == ASE_AWK_NDE_NAMED ||
var->type == ASE_AWK_NDE_NAMEDIDX)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
ASE_ASSERTX (
(var->type == ASE_AWK_NDE_NAMED && var->idx == ASE_NULL) ||
@ -3324,7 +3324,7 @@ static ase_awk_val_t* do_assignment_scalar (
if (var->type == ASE_AWK_NDE_NAMED)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
int n;
pair = ase_map_get (
@ -3428,7 +3428,7 @@ static ase_awk_val_t* do_assignment_map (
if (var->type == ASE_AWK_NDE_NAMEDIDX)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
pair = ase_map_get (
run->named, var->id.name, var->id.name_len);
map = (pair == ASE_NULL)?
@ -5379,7 +5379,7 @@ static ase_awk_val_t* eval_afn_intrinsic (
{
ase_awk_nde_call_t* call = (ase_awk_nde_call_t*)nde;
ase_awk_afn_t* afn;
ase_pair_t* pair;
ase_map_pair_t* pair;
pair = ase_map_get (run->awk->tree.afns,
call->what.afn.name.ptr, call->what.afn.name.len);
@ -5735,7 +5735,7 @@ static int get_reference (
if (nde->type == ASE_AWK_NDE_NAMED)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
pair = ase_map_get (
run->named, tgt->id.name, tgt->id.name_len);
@ -5780,7 +5780,7 @@ static int get_reference (
if (nde->type == ASE_AWK_NDE_NAMEDIDX)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
pair = ase_map_get (
run->named, tgt->id.name, tgt->id.name_len);
@ -5869,7 +5869,7 @@ static int get_reference (
static ase_awk_val_t** get_reference_indexed (
ase_awk_run_t* run, ase_awk_nde_var_t* nde, ase_awk_val_t** val)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
ase_char_t* str;
ase_size_t len;
ase_char_t idxbuf[IDXBUFSIZE];
@ -5994,7 +5994,7 @@ static ase_awk_val_t* eval_rex (ase_awk_run_t* run, ase_awk_nde_t* nde)
static ase_awk_val_t* eval_named (ase_awk_run_t* run, ase_awk_nde_t* nde)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
pair = ase_map_get (run->named,
((ase_awk_nde_var_t*)nde)->id.name,
@ -6021,7 +6021,7 @@ static ase_awk_val_t* eval_arg (ase_awk_run_t* run, ase_awk_nde_t* nde)
static ase_awk_val_t* eval_indexed (
ase_awk_run_t* run, ase_awk_nde_var_t* nde, ase_awk_val_t** val)
{
ase_pair_t* pair;
ase_map_pair_t* pair;
ase_char_t* str;
ase_size_t len;
ase_char_t idxbuf[IDXBUFSIZE];
@ -6069,7 +6069,7 @@ static ase_awk_val_t* eval_indexed (
static ase_awk_val_t* eval_namedidx (ase_awk_run_t* run, ase_awk_nde_t* nde)
{
ase_awk_nde_var_t* tgt = (ase_awk_nde_var_t*)nde;
ase_pair_t* pair;
ase_map_pair_t* pair;
pair = ase_map_get (run->named, tgt->id.name, tgt->id.name_len);
if (pair == ASE_NULL)

View File

@ -8,7 +8,8 @@
#include "mem.h"
ase_dll_t* ase_dll_open (
ase_mmgr_t* mmgr, ase_size_t ext, void (*init) (ase_dll_t*))
ase_mmgr_t* mmgr, ase_size_t ext,
void (*init) (ase_dll_t*, void*), void* init_data)
{
ase_dll_t* dll;
@ -28,7 +29,7 @@ ase_dll_t* ase_dll_open (
ASE_MEMSET (dll, 0, ASE_SIZEOF(ase_dll_t) + ext);
dll->mmgr = mmgr;
if (init) init (dll);
if (init) init (dll, init_data);
return dll;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: map.c 348 2008-08-28 10:29:53Z baconevi $
* $Id: map.c 349 2008-08-28 14:21:25Z baconevi $
*
* {License}
*/
@ -197,8 +197,9 @@ static pair_t* change_pair_val (
}
map_t* ase_map_open (
mmgr_t* mmgr, size_t ext, void (*init) (map_t*),
size_t init_capa, unsigned int load_factor)
mmgr_t* mmgr, size_t ext,
void (*init) (map_t*, void*), void* init_data,
size_t capa, unsigned int load_factor)
{
map_t* map;
@ -212,7 +213,7 @@ map_t* ase_map_open (
if (mmgr == ASE_NULL) return ASE_NULL;
}
ASE_ASSERTX (init_capa >= 0,
ASE_ASSERTX (capa >= 0,
"The initial capacity should be greater than 0");
ASE_ASSERTX (load_factor >= 0 && load_factor <= 100,
@ -221,7 +222,7 @@ map_t* ase_map_open (
map = ASE_MMGR_ALLOC (mmgr, SIZEOF(map_t)+SIZEOF(priv_t)+ext);
if (map == ASE_NULL) return ASE_NULL;
PRIV(map)->bucket = ASE_MMGR_ALLOC (mmgr, init_capa*SIZEOF(pair_t*));
PRIV(map)->bucket = ASE_MMGR_ALLOC (mmgr, capa*SIZEOF(pair_t*));
if (PRIV(map)->bucket == ASE_NULL)
{
ASE_MMGR_FREE (mmgr, map);
@ -232,13 +233,13 @@ map_t* ase_map_open (
map->mmgr = mmgr;
map->size = 0;
map->capa = init_capa;
map->capa = capa;
PRIV(map)->load_factor = load_factor;
map->hasher = hash_key;
map->comper = comp_key;
if (init) init (map);
if (init) init (map, init_data);
return map;
}

View File

@ -24,7 +24,9 @@
#define size_t ase_size_t
#define mmgr_t ase_mmgr_t
sll_t* ase_sll_open (mmgr_t* mmgr, size_t ext, void (*init) (sll_t*))
sll_t* ase_sll_open (
mmgr_t* mmgr, size_t ext,
void (*init) (sll_t*, void*), void* init_data)
{
sll_t* sll;
@ -44,7 +46,7 @@ sll_t* ase_sll_open (mmgr_t* mmgr, size_t ext, void (*init) (sll_t*))
ASE_MEMSET (sll, 0, ASE_SIZEOF(sll_t) + ext);
sll->mmgr = mmgr;
if (init) init (sll);
if (init) init (sll, init_data);
return sll;
}