This commit is contained in:
hyung-hwan 2008-09-05 04:58:08 +00:00
parent fd2eb3f012
commit f90fffc853
20 changed files with 487 additions and 363 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: str.h 352 2008-08-31 10:55:59Z baconevi $ * $Id: str.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -21,6 +21,8 @@ typedef struct ase_str_t ase_str_t;
struct ase_str_t struct ase_str_t
{ {
ase_mmgr_t* mmgr; ase_mmgr_t* mmgr;
ase_sizer_t sizer;
ase_char_t* buf; ase_char_t* buf;
ase_size_t size; ase_size_t size;
ase_size_t capa; ase_size_t capa;
@ -216,6 +218,58 @@ int ase_str_yield (
int new_capa /* new capacity in number of characters */ int new_capa /* new capacity in number of characters */
); );
/*
* NAME: get the sizer
*
* DESCRIPTION:
* The ase_str_getsizer() function returns the sizer specified.
*
* RETURNS: a sizer function set or ASE_NULL if no sizer is set.
*/
ase_sizer_t ase_str_getsizer (
ase_str_t* str /* a dynamic string */
);
/*
* NAME: specify a sizer
*
* DESCRIPTION:
* The ase_str_setsizer() function specify a new sizer for a dynamic string.
* With no sizer specified, the dynamic string doubles the current buffer
* when it needs to increase its size.
*/
void ase_str_setsizer (
ase_str_t* str /* a dynamic string */,
ase_sizer_t sizer /* a sizer function */
);
/*
* NAME: get capacity
*
* DESCRIPTION:
* The ase_str_getcapa() function returns the current capacity.
*
* RETURNS: the current capacity in number of characters.
*/
ase_size_t ase_str_getcapa (
ase_str_t* str /* a dynamic string */
);
/*
* NAME: set new capacity
*
* DESCRIPTION:
* The ase_str_setcapa() function set new capacity. If the new capacity
* is smaller than the old, the overflowing characters are removed from
* from the buffer.
*
* RETURNS: -1 on failure, a new capacity on success
*/
ase_size_t ase_str_setcapa (
ase_str_t* str /* a dynamic string */,
ase_size_t capa /* a new capacity */
);
void ase_str_clear (ase_str_t* str); void ase_str_clear (ase_str_t* str);
void ase_str_swap (ase_str_t* str, ase_str_t* str2); void ase_str_swap (ase_str_t* str, ase_str_t* str2);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: types.h 354 2008-08-31 10:57:24Z baconevi $ * $Id: types.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -345,26 +345,6 @@ struct ase_mmgr_t
void* data; void* data;
}; };
/*
struct ase_ccls_t
{
ase_isccls_t is_upper;
ase_isccls_t is_lower;
ase_isccls_t is_alpha;
ase_isccls_t is_digit;
ase_isccls_t is_xdigit;
ase_isccls_t is_alnum;
ase_isccls_t is_space;
ase_isccls_t is_print;
ase_isccls_t is_graph;
ase_isccls_t is_cntrl;
ase_isccls_t is_punct;
ase_toccls_t to_upper;
ase_toccls_t to_lower;
void* data;
};
*/
struct ase_ccls_t struct ase_ccls_t
{ {
ase_bool_t (*is) (void* data, ase_cint_t c, int type); ase_bool_t (*is) (void* data, ase_cint_t c, int type);
@ -372,4 +352,14 @@ struct ase_ccls_t
void* data; void* data;
}; };
/*
* NAME: determine the size of data
*
* DESCRIPTION:
* The ase_sizer_t is a generic type used by many other modules usually to
* get the new size for resizing data structure.
*/
typedef ase_size_t (*ase_sizer_t) (void* data);
#endif #endif

View File

@ -1,5 +1,5 @@
pkginclude_HEADERS = helper.h ctype.h getopt.h http.h main.h stdio.h pkginclude_HEADERS = http.h main.h stdio.h
pkgincludedir= $(includedir)/ase/utl pkgincludedir= $(includedir)/ase/utl

View File

@ -1,5 +1,5 @@
/* /*
* $Id: Awk.cpp 341 2008-08-20 10:58:19Z baconevi $ * $Id: Awk.cpp 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -417,7 +417,7 @@ int Awk::Argument::getIndexed (
// get the value from the map. // get the value from the map.
ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val; ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val;
pair_t* pair = ase_map_get (m->map, idxptr, idxlen); pair_t* pair = ase_map_search (m->map, idxptr, idxlen);
// the key is not found. it is not an error. val is just nil // the key is not found. it is not an error. val is just nil
if (pair == ASE_NULL) return 0; if (pair == ASE_NULL) return 0;
@ -461,7 +461,7 @@ int Awk::Argument::getIndexed (long_t idx, Argument& val) const
// get the value from the map. // get the value from the map.
ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val; ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val;
pair_t* pair = ase_map_get (m->map, ri, rl); pair_t* pair = ase_map_search (m->map, ri, rl);
// the key is not found. it is not an error. val is just nil // the key is not found. it is not an error. val is just nil
if (pair == ASE_NULL) return 0; if (pair == ASE_NULL) return 0;
@ -616,7 +616,7 @@ int Awk::Return::setIndexed (const char_t* idx, size_t iln, long_t v)
ase_awk_refupval (this->run->run, x2); ase_awk_refupval (this->run->run, x2);
pair_t* pair = ase_map_put ( pair_t* pair = ase_map_upsert (
((ase_awk_val_map_t*)x)->map, idx, iln, x2); ((ase_awk_val_map_t*)x)->map, idx, iln, x2);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -636,7 +636,7 @@ int Awk::Return::setIndexed (const char_t* idx, size_t iln, long_t v)
ase_awk_refupval (this->run->run, x2); ase_awk_refupval (this->run->run, x2);
pair_t* pair = ase_map_put ( pair_t* pair = ase_map_upsert (
((ase_awk_val_map_t*)this->val)->map, idx, iln, x2); ((ase_awk_val_map_t*)this->val)->map, idx, iln, x2);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -677,7 +677,7 @@ int Awk::Return::setIndexed (const char_t* idx, size_t iln, real_t v)
ase_awk_refupval (this->run->run, x2); ase_awk_refupval (this->run->run, x2);
pair_t* pair = ase_map_put ( pair_t* pair = ase_map_upsert (
((ase_awk_val_map_t*)x)->map, idx, iln, x2); ((ase_awk_val_map_t*)x)->map, idx, iln, x2);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -697,7 +697,7 @@ int Awk::Return::setIndexed (const char_t* idx, size_t iln, real_t v)
ase_awk_refupval (this->run->run, x2); ase_awk_refupval (this->run->run, x2);
pair_t* pair = ase_map_put ( pair_t* pair = ase_map_upsert (
((ase_awk_val_map_t*)this->val)->map, idx, iln, x2); ((ase_awk_val_map_t*)this->val)->map, idx, iln, x2);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -738,7 +738,7 @@ int Awk::Return::setIndexed (const char_t* idx, size_t iln, const char_t* str, s
ase_awk_refupval (this->run->run, x2); ase_awk_refupval (this->run->run, x2);
pair_t* pair = ase_map_put ( pair_t* pair = ase_map_upsert (
((ase_awk_val_map_t*)x)->map, idx, iln, x2); ((ase_awk_val_map_t*)x)->map, idx, iln, x2);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -758,7 +758,7 @@ int Awk::Return::setIndexed (const char_t* idx, size_t iln, const char_t* str, s
ase_awk_refupval (this->run->run, x2); ase_awk_refupval (this->run->run, x2);
pair_t* pair = ase_map_put ( pair_t* pair = ase_map_upsert (
((ase_awk_val_map_t*)this->val)->map, idx, iln, x2); ((ase_awk_val_map_t*)this->val)->map, idx, iln, x2);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -1464,7 +1464,7 @@ int Awk::addFunction (
return -1; return -1;
} }
pair_t* pair = ase_map_put (functionMap, name, nameLen, tmp); pair_t* pair = ase_map_upsert (functionMap, name, nameLen, tmp);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
ase_awk_delfunc (awk, name, nameLen); ase_awk_delfunc (awk, name, nameLen);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c 349 2008-08-28 14:21:25Z baconevi $ * $Id: awk.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -11,11 +11,8 @@
#include "awk.h" #include "awk.h"
static void free_word (void* awk, void* ptr);
static void free_afn (void* awk, void* afn);
static void free_bfn (void* awk, void* afn);
#define SETERR(awk,code) ase_awk_seterrnum(awk,code) #define SETERR(awk,code) ase_awk_seterrnum(awk,code)
#define SETERRARG(awk,code,line,arg,leng) \ #define SETERRARG(awk,code,line,arg,leng) \
do { \ do { \
ase_cstr_t errarg; \ ase_cstr_t errarg; \
@ -24,15 +21,27 @@ static void free_bfn (void* awk, void* afn);
ase_awk_seterror ((awk), (code), (line), &errarg, 1); \ ase_awk_seterror ((awk), (code), (line), &errarg, 1); \
} while (0) } while (0)
static void init_map (ase_map_t* map, void* arg) static void free_afn (ase_map_t* map, void* vptr, ase_size_t vlen)
{ {
ase_awk_t** p = ase_map_getextension (map); ase_awk_t* awk = *(ase_awk_t**)ase_map_getextension(map);
*p = arg; ase_awk_afn_t* f = (ase_awk_afn_t*)vptr;
/* f->name doesn't have to be freed */
/*ASE_AWK_FREE (awk, f->name);*/
ase_awk_clrpt (awk, f->body);
ASE_AWK_FREE (awk, f);
} }
ase_awk_t* ase_awk_open ( static void free_bfn (ase_map_t* map, void* vptr, ase_size_t vlen)
ase_mmgr_t* mmgr, ase_size_t ext, {
void (*init) (ase_awk_t*, void*), void* init_data) ase_awk_t* awk = *(ase_awk_t**)ase_map_getextension(map);
ase_awk_bfn_t* f = (ase_awk_bfn_t*)vptr;
ASE_AWK_FREE (awk, f);
}
ase_awk_t* ase_awk_open (ase_mmgr_t* mmgr, ase_size_t ext)
{ {
ase_awk_t* awk; ase_awk_t* awk;
@ -58,19 +67,18 @@ ase_awk_t* ase_awk_open (
return ASE_NULL; return ASE_NULL;
} }
/*awk->wtab = ase_map_open (awk, 512, 70, free_word, ASE_NULL, mmgr);*/ awk->wtab = ase_map_open (mmgr, sizeof(awk), 512, 70);
awk->wtab = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->wtab == ASE_NULL) if (awk->wtab == ASE_NULL)
{ {
ase_str_close (&awk->token.name); ase_str_close (&awk->token.name);
ASE_AWK_FREE (awk, awk); ASE_AWK_FREE (awk, awk);
return ASE_NULL; return ASE_NULL;
} }
*(ase_awk_t**)ase_map_getextension(awk->wtab) = awk;
ase_map_setcopier (awk->wtab, ASE_MAP_KEY, ASE_MAP_COPIER_INLINE);
ase_map_setcopier (awk->wtab, ASE_MAP_VAL, ASE_MAP_COPIER_INLINE);
/*awk->rwtab = ase_map_open (awk, 512, 70, free_word, ASE_NULL, mmgr);*/ awk->rwtab = ase_map_open (mmgr, sizeof(awk), 512, 70);
awk->rwtab = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->rwtab == ASE_NULL) if (awk->rwtab == ASE_NULL)
{ {
ase_map_close (awk->wtab); ase_map_close (awk->wtab);
@ -78,12 +86,13 @@ ase_awk_t* ase_awk_open (
ASE_AWK_FREE (awk, awk); ASE_AWK_FREE (awk, awk);
return ASE_NULL; return ASE_NULL;
} }
*(ase_awk_t**)ase_map_getextension(awk->rwtab) = awk;
ase_map_setcopier (awk->rwtab, ASE_MAP_KEY, ASE_MAP_COPIER_INLINE);
ase_map_setcopier (awk->rwtab, ASE_MAP_VAL, ASE_MAP_COPIER_INLINE);
/* TODO: initial map size?? */ /* TODO: initial map size?? */
/*awk->tree.afns = ase_map_open (awk, 512, 70, free_afn, ASE_NULL, mmgr);*/ /*awk->tree.afns = ase_map_open (awk, 512, 70, free_afn, ASE_NULL, mmgr);*/
awk->tree.afns = ase_map_open (mmgr, sizeof(awk), 512, 70);
awk->tree.afns = ase_map_open (
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->tree.afns == ASE_NULL) if (awk->tree.afns == ASE_NULL)
{ {
ase_map_close (awk->rwtab); ase_map_close (awk->rwtab);
@ -92,10 +101,11 @@ ase_awk_t* ase_awk_open (
ASE_AWK_FREE (awk, awk); ASE_AWK_FREE (awk, awk);
return ASE_NULL; return ASE_NULL;
} }
*(ase_awk_t**)ase_map_getextension(awk->tree.afns) = awk;
ase_map_setfreeer (awk->tree.afns, ASE_MAP_VAL, free_afn);
/*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 ( awk->parse.afns = ase_map_open (mmgr, sizeof(awk), 256, 70);
mmgr, sizeof(awk), init_map, awk, 256, 70);
if (awk->parse.afns == ASE_NULL) if (awk->parse.afns == ASE_NULL)
{ {
ase_map_close (awk->tree.afns); ase_map_close (awk->tree.afns);
@ -105,10 +115,10 @@ ase_awk_t* ase_awk_open (
ASE_AWK_FREE (awk, awk); ASE_AWK_FREE (awk, awk);
return ASE_NULL; return ASE_NULL;
} }
*(ase_awk_t**)ase_map_getextension(awk->parse.afns) = awk;
/*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 ( awk->parse.named = ase_map_open (mmgr, sizeof(awk), 256, 70);
mmgr, sizeof(awk), init_map, awk, 256, 70);
if (awk->parse.named == ASE_NULL) if (awk->parse.named == ASE_NULL)
{ {
ase_map_close (awk->parse.afns); ase_map_close (awk->parse.afns);
@ -119,6 +129,7 @@ ase_awk_t* ase_awk_open (
ASE_AWK_FREE (awk, awk); ASE_AWK_FREE (awk, awk);
return ASE_NULL; return ASE_NULL;
} }
*(ase_awk_t**)ase_map_getextension(awk->parse.named) = awk;
if (ase_awk_tab_open (&awk->parse.globals, awk) == ASE_NULL) if (ase_awk_tab_open (&awk->parse.globals, awk) == ASE_NULL)
{ {
@ -193,8 +204,7 @@ ase_awk_t* ase_awk_open (
awk->bfn.sys = ASE_NULL; awk->bfn.sys = ASE_NULL;
/*awk->bfn.user = 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 ( awk->bfn.user = ase_map_open (mmgr, sizeof(awk), 512, 70);
mmgr, sizeof(awk), init_map, awk, 512, 70);
if (awk->bfn.user == ASE_NULL) if (awk->bfn.user == ASE_NULL)
{ {
ase_awk_tab_close (&awk->parse.params); ase_awk_tab_close (&awk->parse.params);
@ -209,6 +219,8 @@ ase_awk_t* ase_awk_open (
ASE_AWK_FREE (awk, awk); ASE_AWK_FREE (awk, awk);
return ASE_NULL; return ASE_NULL;
} }
*(ase_awk_t**)ase_map_getextension(awk->bfn.user) = awk;
ase_map_setfreeer (awk->bfn.user, ASE_MAP_VAL, free_bfn);
awk->parse.depth.cur.block = 0; awk->parse.depth.cur.block = 0;
awk->parse.depth.cur.loop = 0; awk->parse.depth.cur.loop = 0;
@ -239,31 +251,9 @@ ase_awk_t* ase_awk_open (
return ASE_NULL; return ASE_NULL;
} }
if (init) init (awk, init_data);
return awk; return awk;
} }
static void free_word (void* owner, void* ptr)
{
ASE_AWK_FREE ((ase_awk_t*)owner, ptr);
}
static void free_afn (void* owner, void* afn)
{
ase_awk_afn_t* f = (ase_awk_afn_t*)afn;
/* f->name doesn't have to be freed */
/*ASE_AWK_FREE ((ase_awk_t*)owner, f->name);*/
ase_awk_clrpt ((ase_awk_t*)owner, f->body);
ASE_AWK_FREE ((ase_awk_t*)owner, f);
}
static void free_bfn (void* owner, void* bfn)
{
ase_awk_bfn_t* f = (ase_awk_bfn_t*)bfn;
ASE_AWK_FREE ((ase_awk_t*)owner, f);
}
int ase_awk_close (ase_awk_t* awk) int ase_awk_close (ase_awk_t* awk)
{ {
@ -443,7 +433,7 @@ int ase_awk_getword (ase_awk_t* awk,
{ {
ase_map_pair_t* p; ase_map_pair_t* p;
p = ase_map_get (awk->wtab, okw, olen); p = ase_map_search (awk->wtab, okw, olen);
if (p == ASE_NULL) return -1; if (p == ASE_NULL) return -1;
*nkw = ((ase_cstr_t*)p->vptr)->ptr; *nkw = ((ase_cstr_t*)p->vptr)->ptr;
@ -452,6 +442,28 @@ int ase_awk_getword (ase_awk_t* awk,
return 0; return 0;
} }
int ase_awk_unsetword (ase_awk_t* awk, const ase_char_t* kw, ase_size_t len)
{
ase_map_pair_t* p;
p = ase_map_search (awk->wtab, kw, ASE_NCHARS_TO_NBYTES(len));
if (p == ASE_NULL)
{
SETERRARG (awk, ASE_AWK_ENOENT, 0, kw, len);
return -1;
}
ase_map_delete (awk->rwtab, ASE_MAP_VPTR(p), ASE_MAP_VLEN(p));
ase_map_delete (awk->wtab, kw, ASE_NCHARS_TO_NBYTES(len));
return 0;
}
void ase_awk_unsetallwords (ase_awk_t* awk)
{
ase_map_clear (awk->wtab);
ase_map_clear (awk->rwtab);
}
int ase_awk_setword (ase_awk_t* awk, int ase_awk_setword (ase_awk_t* awk,
const ase_char_t* okw, ase_size_t olen, const ase_char_t* okw, ase_size_t olen,
const ase_char_t* nkw, ase_size_t nlen) const ase_char_t* nkw, ase_size_t nlen)
@ -465,25 +477,11 @@ int ase_awk_setword (ase_awk_t* awk,
if (okw == ASE_NULL || olen == 0) if (okw == ASE_NULL || olen == 0)
{ {
/* clear the entire table */ /* clear the entire table */
ase_map_clear (awk->wtab); ase_awk_unsetallwords (awk);
ase_map_clear (awk->rwtab);
return 0; return 0;
} }
/* delete the word */ return ase_awk_unsetword (awk, okw, olen);
p = ase_map_get (awk->wtab, okw, olen);
if (p != ASE_NULL)
{
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;
}
else
{
SETERRARG (awk, ASE_AWK_ENOENT, 0, okw, olen);
return -1;
}
} }
else if (okw == ASE_NULL || olen == 0) else if (okw == ASE_NULL || olen == 0)
{ {
@ -492,58 +490,19 @@ int ase_awk_setword (ase_awk_t* awk,
} }
/* set the word */ /* set the word */
#if 0 if (ase_map_upsert (awk->wtab,
vn = (ase_cstr_t*) ASE_AWK_ALLOC ( (ase_char_t*)okw, ASE_NCHARS_TO_NBYTES(olen),
awk, ASE_SIZEOF(ase_cstr_t)+((nlen+1)*ASE_SIZEOF(*nkw))); (ase_char_t*)nkw, ASE_NCHARS_TO_NBYTES(nlen)) == ASE_NULL)
if (vn == ASE_NULL)
{ {
SETERR (awk, ASE_AWK_ENOMEM); SETERR (awk, ASE_AWK_ENOMEM);
return -1; return -1;
} }
vn->len = nlen;
vn->ptr = (const ase_char_t*)(vn + 1);
ase_strncpy ((ase_char_t*)vn->ptr, nkw, nlen);
vo = (ase_cstr_t*) ASE_AWK_ALLOC (
awk, ASE_SIZEOF(ase_cstr_t)+((olen+1)*ASE_SIZEOF(*okw)));
if (vo == ASE_NULL)
{
ASE_AWK_FREE (awk, vn);
SETERR (awk, ASE_AWK_ENOMEM);
return -1;
}
vo->len = olen;
vo->ptr = (const ase_char_t*)(vo + 1);
ase_strncpy ((ase_char_t*)vo->ptr, okw, olen);
if (ase_map_put (awk->wtab, (void*)okw, olen, vn) == 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, vo) == ASE_NULL) if (ase_map_upsert (awk->rwtab,
(ase_char_t*)nkw, ASE_NCHARS_TO_NBYTES(nlen),
(ase_char_t*)okw, ASE_NCHARS_TO_NBYTES(olen)) == ASE_NULL)
{ {
ase_map_remove (awk->wtab, okw, olen); ase_map_delete (awk->wtab, okw, ASE_NCHARS_TO_NBYTES(olen));
ASE_AWK_FREE (awk, vo);
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); SETERR (awk, ASE_AWK_ENOMEM);
return -1; return -1;
} }

View File

@ -1,15 +1,11 @@
/* /*
* $Id: extio.h 115 2008-03-03 11:13:15Z baconevi $ * $Id: extio.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_EXTIO_H_ #ifndef _ASE_LIB_AWK_EXTIO_H_
#define _ASE_AWK_EXTIO_H_ #define _ASE_LIB_AWK_EXTIO_H_
#ifndef _ASE_AWK_AWK_H_
#error Never include this file directly. Include <ase/awk/awk.h> instead
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@ -1,5 +1,5 @@
/* /*
* $Id: func.c 337 2008-08-20 09:17:25Z baconevi $ * $Id: func.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -98,7 +98,7 @@ void* ase_awk_addfunc (
bfn->handler = handler; bfn->handler = handler;
if (ase_map_put (awk->bfn.user, name, name_len, bfn) == ASE_NULL) if (ase_map_upsert (awk->bfn.user, name, name_len, bfn) == ASE_NULL)
{ {
ASE_AWK_FREE (awk, bfn); ASE_AWK_FREE (awk, bfn);
ase_awk_seterrnum (awk, ASE_AWK_ENOMEM); ase_awk_seterrnum (awk, ASE_AWK_ENOMEM);
@ -144,12 +144,14 @@ ase_awk_bfn_t* ase_awk_getbfn (
if (bfn->valid != 0 && if (bfn->valid != 0 &&
(awk->option & bfn->valid) != bfn->valid) continue; (awk->option & bfn->valid) != bfn->valid) continue;
pair = ase_map_get (awk->wtab, bfn->name.ptr, bfn->name.len); pair = ase_map_search (
awk->wtab, bfn->name.ptr,
ASE_NCHARS_TO_NBYTES(bfn->name.len));
if (pair != ASE_NULL) if (pair != ASE_NULL)
{ {
/* found in the customized word table */ /* found in the customized word table */
k = ((ase_cstr_t*)(pair->val))->ptr; k = ASE_MAP_VPTR(pair);
l = ((ase_cstr_t*)(pair->val))->len; l = ASE_NBYTES_TO_NCHARS(ASE_MAP_VLEN(pair));
} }
else else
{ {
@ -164,21 +166,21 @@ ase_awk_bfn_t* ase_awk_getbfn (
* because I'm trying to support ase_awk_setword in * because I'm trying to support ase_awk_setword in
* a very flimsy way here. Would it be better to drop * a very flimsy way here. Would it be better to drop
* ase_awk_setword totally? */ * ase_awk_setword totally? */
pair = ase_map_get (awk->rwtab, name, len); pair = ase_map_search (awk->rwtab, name, ASE_NCHARS_TO_NBYTES(len));
if (pair != ASE_NULL) if (pair != ASE_NULL)
{ {
/* the current name is a target name for /* the current name is a target name for
* one of the original word. */ * one of the original word. */
k = ((ase_cstr_t*)(pair->val))->ptr; k = ASE_MAP_VPTR(pair);
l = ((ase_cstr_t*)(pair->val))->len; l = ASE_NBYTES_TO_NCHARS(ASE_MAP_VLEN(pair));
} }
else else
{ {
pair = ase_map_get (awk->wtab, name, len); pair = ase_map_search (awk->wtab, name, ASE_NCHARS_TO_NBYTES(len));
if (pair != ASE_NULL) if (pair != ASE_NULL)
{ {
k = ((ase_cstr_t*)(pair->val))->ptr; k = ASE_MAP_VPTR(pair);
l = ((ase_cstr_t*)(pair->val))->len; l = ASE_NBYTES_TO_NCHARS(ASE_MAP_VLEN(pair));
if (ase_strxncmp (name, len, k, l) != 0) if (ase_strxncmp (name, len, k, l) != 0)
{ {
@ -202,7 +204,7 @@ ase_awk_bfn_t* ase_awk_getbfn (
} }
/* END NOTE */ /* END NOTE */
pair = ase_map_get (awk->bfn.user, k, l); pair = ase_map_search (awk->bfn.user, k, ASE_NCHARS_TO_NBYTES(l));
if (pair == ASE_NULL) return ASE_NULL; if (pair == ASE_NULL) return ASE_NULL;
bfn = (ase_awk_bfn_t*)pair->val; bfn = (ase_awk_bfn_t*)pair->val;
@ -796,7 +798,7 @@ static int bfn_split (
* it is decremented if the assignement fails. */ * it is decremented if the assignement fails. */
ase_awk_refupval (run, t2); ase_awk_refupval (run, t2);
if (ase_map_putx ( if (ase_map_put (
((ase_awk_val_map_t*)t1)->map, ((ase_awk_val_map_t*)t1)->map,
key, key_len, t2, ASE_NULL) == -1) key, key_len, t2, ASE_NULL) == -1)
{ {
@ -1340,9 +1342,9 @@ static int bfn_sprintf (
{ {
ase_size_t nargs; ase_size_t nargs;
ase_awk_val_t* a0; ase_awk_val_t* a0;
ase_char_t* str0, * ptr;
ase_size_t len0, len;
ase_str_t out, fbu; ase_str_t out, fbu;
ase_cstr_t cs0;
ase_cstr_t x;
nargs = ase_awk_getnargs (run); nargs = ase_awk_getnargs (run);
ASE_ASSERT (nargs > 0); ASE_ASSERT (nargs > 0);
@ -1362,14 +1364,14 @@ static int bfn_sprintf (
a0 = ase_awk_getarg (run, 0); a0 = ase_awk_getarg (run, 0);
if (a0->type == ASE_AWK_VAL_STR) if (a0->type == ASE_AWK_VAL_STR)
{ {
str0 = ((ase_awk_val_str_t*)a0)->buf; cs0.ptr = ((ase_awk_val_str_t*)a0)->buf;
len0 = ((ase_awk_val_str_t*)a0)->len; cs0.len = ((ase_awk_val_str_t*)a0)->len;
} }
else else
{ {
str0 = ase_awk_valtostr ( cs0.ptr = ase_awk_valtostr (
run, a0, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len0); run, a0, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &cs0.len);
if (str0 == ASE_NULL) if (cs0.ptr == ASE_NULL)
{ {
ase_str_close (&fbu); ase_str_close (&fbu);
ase_str_close (&out); ase_str_close (&out);
@ -1377,9 +1379,9 @@ static int bfn_sprintf (
} }
} }
ptr = ase_awk_format (run, x.ptr = ase_awk_format (run,
&out, &fbu, str0, len0, nargs, ASE_NULL, &len); &out, &fbu, cs0.ptr, cs0.len, nargs, ASE_NULL, &x.len);
if (a0->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, str0); if (a0->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, cs0.ptr);
if (ptr == ASE_NULL) if (ptr == ASE_NULL)
{ {
ase_str_close (&fbu); ase_str_close (&fbu);
@ -1387,8 +1389,8 @@ static int bfn_sprintf (
return -1; return -1;
} }
/*a0 = ase_awk_makestrval_nodup (run, ptr, len);*/ /*a0 = ase_awk_makestrval_nodup (run, x.ptr, x.len);*/
a0 = ase_awk_makestrval (run, ptr, len); a0 = ase_awk_makestrval (run, x.ptr, x.len);
if (a0 == ASE_NULL) if (a0 == ASE_NULL)
{ {
ase_str_close (&fbu); ase_str_close (&fbu);
@ -1398,7 +1400,7 @@ static int bfn_sprintf (
} }
ase_str_close (&fbu); ase_str_close (&fbu);
/*ase_str_forfeit (&out);*/ /*ase_str_yield (&out, ASE_NULL, 0);*/
ase_str_close (&out); ase_str_close (&out);
ase_awk_setretval (run, a0); ase_awk_setretval (run, a0);
return 0; return 0;

View File

@ -1,15 +1,11 @@
/* /*
* $Id: func.h 115 2008-03-03 11:13:15Z baconevi $ * $Id: func.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_FUNC_H_ #ifndef _ASE_LIB_AWK_FUNC_H_
#define _ASE_AWK_FUNC_H_ #define _ASE_LIB_AWK_FUNC_H_
#ifndef _ASE_AWK_AWK_H_
#error Never include this file directly. Include <ase/awk/awk.h> instead
#endif
typedef struct ase_awk_bfn_t ase_awk_bfn_t; typedef struct ase_awk_bfn_t ase_awk_bfn_t;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: jni.c 337 2008-08-20 09:17:25Z baconevi $ * $Id: jni.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -2274,7 +2274,7 @@ JNIEXPORT jobject JNICALL Java_ase_awk_Argument_getindexed (JNIEnv* env, jobject
} }
else rptr = (ase_char_t*)ptr; else rptr = (ase_char_t*)ptr;
pair = ase_map_get (((ase_awk_val_map_t*)val)->map, rptr, len); pair = ase_map_search (((ase_awk_val_map_t*)val)->map, rptr, len);
if (ptr != rptr) ase_awk_free (awk, rptr); if (ptr != rptr) ase_awk_free (awk, rptr);
(*env)->ReleaseStringChars (env, index, ptr); (*env)->ReleaseStringChars (env, index, ptr);
@ -2463,7 +2463,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Return_setindexedintval (JNIEnv* env, jobjec
return; return;
} }
pair = ase_map_put ( pair = ase_map_upsert (
((ase_awk_val_map_t*)x)->map, aptr, len, x2); ((ase_awk_val_map_t*)x)->map, aptr, len, x2);
free_str (env, awk, index, jptr, aptr); free_str (env, awk, index, jptr, aptr);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -2497,7 +2497,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Return_setindexedintval (JNIEnv* env, jobjec
THROW_NOMEM_EXCEPTION (env); THROW_NOMEM_EXCEPTION (env);
return; return;
} }
pair = ase_map_put ( pair = ase_map_upsert (
((ase_awk_val_map_t*)val)->map, aptr, len, x2); ((ase_awk_val_map_t*)val)->map, aptr, len, x2);
free_str (env, awk, index, jptr, aptr); free_str (env, awk, index, jptr, aptr);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -2566,7 +2566,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Return_setindexedrealval (JNIEnv* env, jobje
return; return;
} }
pair = ase_map_put ( pair = ase_map_upsert (
((ase_awk_val_map_t*)x)->map, aptr, len, x2); ((ase_awk_val_map_t*)x)->map, aptr, len, x2);
free_str (env, awk, index, jptr, aptr); free_str (env, awk, index, jptr, aptr);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -2600,7 +2600,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Return_setindexedrealval (JNIEnv* env, jobje
THROW_NOMEM_EXCEPTION (env); THROW_NOMEM_EXCEPTION (env);
return; return;
} }
pair = ase_map_put ( pair = ase_map_upsert (
((ase_awk_val_map_t*)val)->map, aptr, len, x2); ((ase_awk_val_map_t*)val)->map, aptr, len, x2);
free_str (env, awk, index, jptr, aptr); free_str (env, awk, index, jptr, aptr);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -2675,7 +2675,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Return_setindexedstrval (JNIEnv* env, jobjec
THROW_NOMEM_EXCEPTION (env); THROW_NOMEM_EXCEPTION (env);
return; return;
} }
pair = ase_map_put ( pair = ase_map_upsert (
((ase_awk_val_map_t*)x)->map, aptr, len, x2); ((ase_awk_val_map_t*)x)->map, aptr, len, x2);
free_str (env, awk, index, jptr, aptr); free_str (env, awk, index, jptr, aptr);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -2715,7 +2715,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Return_setindexedstrval (JNIEnv* env, jobjec
THROW_NOMEM_EXCEPTION (env); THROW_NOMEM_EXCEPTION (env);
return; return;
} }
pair = ase_map_put ( pair = ase_map_upsert (
((ase_awk_val_map_t*)val)->map, aptr, len, x2); ((ase_awk_val_map_t*)val)->map, aptr, len, x2);
free_str (env, awk, index, jptr, aptr); free_str (env, awk, index, jptr, aptr);
if (pair == ASE_NULL) if (pair == ASE_NULL)

View File

@ -1,11 +1,11 @@
/* /*
* $Id: jni.h 115 2008-03-03 11:13:15Z baconevi $ * $Id: jni.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_JNI_H_ #ifndef _ASE_LIB_AWK_JNI_H_
#define _ASE_AWK_JNI_H_ #define _ASE_LIB_AWK_JNI_H_
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
#include <JavaVM/jni.h> #include <JavaVM/jni.h>

View File

@ -1,15 +1,11 @@
/* /*
* $Id: misc.h 115 2008-03-03 11:13:15Z baconevi $ * $Id: misc.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_MISC_H_ #ifndef _ASE_LIB_AWK_MISC_H_
#define _ASE_AWK_MISC_H_ #define _ASE_LIB_AWK_MISC_H_
#ifndef _ASE_AWK_AWK_H_
#error Never include this file directly. Include <ase/awk/awk.h> instead
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -1,12 +1,12 @@
/* /*
* $Id: parse.c 349 2008-08-28 14:21:25Z baconevi $ * $Id: parse.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#include "awk.h" #include "awk.h"
enum enum token_t
{ {
TOKEN_EOF, TOKEN_EOF,
TOKEN_NEWLINE, TOKEN_NEWLINE,
@ -240,6 +240,7 @@ struct kwent_t
int valid; /* the entry is valid when this option is set */ int valid; /* the entry is valid when this option is set */
}; };
/* this table should match the kw_t enums in parse.h */
static kwent_t kwtab[] = static kwent_t kwtab[] =
{ {
/* operators */ /* operators */
@ -456,16 +457,21 @@ const ase_char_t* ase_awk_getglobalname (
return awk->parse.globals.buf[idx].name.ptr; return awk->parse.globals.buf[idx].name.ptr;
} }
const ase_char_t* ase_awk_getkw (ase_awk_t* awk, const ase_char_t* kw) ase_cstr_t* ase_awk_getkw (ase_awk_t* awk, int id, ase_cstr_t* s)
{ {
ase_map_pair_t* p; ase_map_pair_t* p;
ASE_ASSERT (kw != ASE_NULL); s->ptr = kwtab[id].name;
s->len = kwtab[id].name_len;
p = ase_map_get (awk->wtab, kw, ase_strlen(kw)); p = ase_map_search (awk->wtab, s->ptr, ASE_NCHARS_TO_NBYTES(s->len));
if (p != ASE_NULL) return ((ase_cstr_t*)p->val)->ptr; if (p != ASE_NULL)
{
s->ptr = ASE_MAP_VPTR(p);
s->len = ASE_NBYTES_TO_NCHARS(ASE_MAP_VLEN(p));
}
return kw; return s;
} }
int ase_awk_parse (ase_awk_t* awk, ase_awk_srcios_t* srcios) int ase_awk_parse (ase_awk_t* awk, ase_awk_srcios_t* srcios)
@ -545,15 +551,16 @@ static int parse (ase_awk_t* awk)
p = ase_map_getfirstpair (awk->parse.afns, &buckno); p = ase_map_getfirstpair (awk->parse.afns, &buckno);
while (p != ASE_NULL) while (p != ASE_NULL)
{ {
if (ase_map_get (awk->tree.afns, if (ase_map_search (awk->tree.afns,
p->key.ptr, p->key.len) == ASE_NULL) ASE_MAP_KPTR(p), ASE_MAP_KLEN(p)) == ASE_NULL)
{ {
/* TODO: set better error no & line */ /* TODO: set better error no & line */
/* this line number might be truncated as /* this line number might be truncated as
* sizeof(line) could be > sizeof(void*) */ * sizeof(line) could be > sizeof(void*) */
SETERRARG (awk, ASE_AWK_EFNNONE, SETERRARG (awk, ASE_AWK_EFNNONE,
(ase_size_t)p->val, (ase_size_t)p->val,
p->key.ptr, p->key.len); ASE_MAP_KPTR(p),
ASE_NBYTES_TO_NCHARS(ASE_MAP_KELN(p)));
EXIT_PARSE(-1); EXIT_PARSE(-1);
} }
@ -844,7 +851,7 @@ static ase_awk_nde_t* parse_function (ase_awk_t* awk)
return ASE_NULL; return ASE_NULL;
} }
if (ase_map_get(awk->tree.afns, name, name_len) != ASE_NULL) if (ase_map_search (awk->tree.afns, name, name_len) != ASE_NULL)
{ {
/* the function is defined previously */ /* the function is defined previously */
SETERRARG ( SETERRARG (
@ -1075,7 +1082,7 @@ static ase_awk_nde_t* parse_function (ase_awk_t* awk)
afn->nargs = nargs; afn->nargs = nargs;
afn->body = body; afn->body = body;
n = ase_map_putx (awk->tree.afns, name_dup, name_len, afn, &pair); n = ase_map_upsertx (awk->tree.afns, name_dup, name_len, afn, &pair);
if (n < 0) if (n < 0)
{ {
ASE_AWK_FREE (awk, name_dup); ASE_AWK_FREE (awk, name_dup);
@ -1425,7 +1432,7 @@ static void trans_global (
{ {
ase_map_pair_t* pair; ase_map_pair_t* pair;
pair = ase_map_get (awk->wtab, word->ptr, word->len); pair = ase_map_search (awk->wtab, word->ptr, word->len);
if (pair != ASE_NULL) if (pair != ASE_NULL)
{ {
word->ptr = ((ase_cstr_t*)(pair->vptr))->ptr; word->ptr = ((ase_cstr_t*)(pair->vptr))->ptr;
@ -1470,7 +1477,7 @@ static int add_global (
} }
/* check if it conflict with a function name */ /* check if it conflict with a function name */
if (ase_map_get (awk->tree.afns, name, len) != ASE_NULL) if (ase_map_search (awk->tree.afns, name, len) != ASE_NULL)
{ {
SETERRARG ( SETERRARG (
awk, ASE_AWK_EAFNRED, line, awk, ASE_AWK_EAFNRED, line,
@ -1480,7 +1487,7 @@ static int add_global (
/* check if it conflict with a function name /* check if it conflict with a function name
* caught in the function call table */ * caught in the function call table */
if (ase_map_get (awk->parse.afns, name, len) != ASE_NULL) if (ase_map_search (awk->parse.afns, name, len) != ASE_NULL)
{ {
SETERRARG ( SETERRARG (
awk, ASE_AWK_EAFNRED, line, awk, ASE_AWK_EAFNRED, line,
@ -1660,7 +1667,7 @@ static ase_awk_t* collect_locals (
local, local_len) == 0); local, local_len) == 0);
} }
if (iscur || ase_map_get (awk->tree.afns, local, local_len) != ASE_NULL) if (iscur || ase_map_search (awk->tree.afns, local, local_len) != ASE_NULL)
{ {
SETERRARG ( SETERRARG (
awk, ASE_AWK_EAFNRED, awk->token.line, awk, ASE_AWK_EAFNRED, awk->token.line,
@ -1670,7 +1677,7 @@ static ase_awk_t* collect_locals (
/* check if it conflict with a function name /* check if it conflict with a function name
* caught in the function call table */ * caught in the function call table */
if (ase_map_get (awk->parse.afns, local, local_len) != ASE_NULL) if (ase_map_search (awk->parse.afns, local, local_len) != ASE_NULL)
{ {
SETERRARG ( SETERRARG (
awk, ASE_AWK_EAFNRED, awk->token.line, awk, ASE_AWK_EAFNRED, awk->token.line,
@ -3257,7 +3264,7 @@ static ase_awk_nde_t* parse_primary_ident (ase_awk_t* awk, ase_size_t line)
if (awk->option & ASE_AWK_IMPLICIT) if (awk->option & ASE_AWK_IMPLICIT)
{ {
if (ase_map_get (awk->parse.named, name_dup, name_len) != ASE_NULL) if (ase_map_search (awk->parse.named, name_dup, name_len) != ASE_NULL)
{ {
/* a function call conflicts with a named variable */ /* a function call conflicts with a named variable */
SETERRARG (awk, ASE_AWK_EVARRED, line, name_dup, name_len); SETERRARG (awk, ASE_AWK_EVARRED, line, name_dup, name_len);
@ -3308,14 +3315,14 @@ static ase_awk_nde_t* parse_primary_ident (ase_awk_t* awk, ase_size_t line)
name_dup, name_len) == 0); name_dup, name_len) == 0);
} }
if (iscur || ase_map_get (awk->tree.afns, name_dup, name_len) != ASE_NULL) if (iscur || ase_map_search (awk->tree.afns, name_dup, name_len) != ASE_NULL)
{ {
/* the function is defined previously */ /* the function is defined previously */
SETERRARG (awk, ASE_AWK_EAFNRED, line, name_dup, name_len); SETERRARG (awk, ASE_AWK_EAFNRED, line, name_dup, name_len);
goto exit_func; goto exit_func;
} }
if (ase_map_get (awk->parse.afns, name_dup, name_len) != ASE_NULL) if (ase_map_search (awk->parse.afns, name_dup, name_len) != ASE_NULL)
{ {
/* is it one of the function calls found so far? */ /* is it one of the function calls found so far? */
SETERRARG (awk, ASE_AWK_EAFNRED, line, name_dup, name_len); SETERRARG (awk, ASE_AWK_EAFNRED, line, name_dup, name_len);
@ -3334,7 +3341,7 @@ static ase_awk_nde_t* parse_primary_ident (ase_awk_t* awk, ase_size_t line)
nde->idx = ASE_NULL; nde->idx = ASE_NULL;
/* collect unique instances of a named variables for reference */ /* collect unique instances of a named variables for reference */
if (ase_map_put (awk->parse.named, if (ase_map_upsert (awk->parse.named,
name_dup, name_len, (void*)line) == ASE_NULL) name_dup, name_len, (void*)line) == ASE_NULL)
{ {
SETERRLIN (awk, ASE_AWK_ENOMEM, line); SETERRLIN (awk, ASE_AWK_ENOMEM, line);
@ -3489,14 +3496,14 @@ static ase_awk_nde_t* parse_hashidx (
name, name_len) == 0); name, name_len) == 0);
} }
if (iscur || ase_map_get (awk->tree.afns, name, name_len) != ASE_NULL) if (iscur || ase_map_search (awk->tree.afns, name, name_len) != ASE_NULL)
{ {
/* the function is defined previously */ /* the function is defined previously */
SETERRARG (awk, ASE_AWK_EAFNRED, line, name, name_len); SETERRARG (awk, ASE_AWK_EAFNRED, line, name, name_len);
goto exit_func; goto exit_func;
} }
if (ase_map_get (awk->parse.afns, name, name_len) != ASE_NULL) if (ase_map_search (awk->parse.afns, name, name_len) != ASE_NULL)
{ {
/* is it one of the function calls found so far? */ /* is it one of the function calls found so far? */
SETERRARG (awk, ASE_AWK_EAFNRED, line, name, name_len); SETERRARG (awk, ASE_AWK_EAFNRED, line, name, name_len);
@ -3648,7 +3655,7 @@ static ase_awk_nde_t* parse_fncall (
/* this line number might be truncated as /* this line number might be truncated as
* sizeof(line) could be > sizeof(void*) */ * sizeof(line) could be > sizeof(void*) */
if (ase_map_put (awk->parse.afns, if (ase_map_upsert (awk->parse.afns,
name, name_len, (void*)line) == ASE_NULL) name, name_len, (void*)line) == ASE_NULL)
{ {
ASE_AWK_FREE (awk, call); ASE_AWK_FREE (awk, call);
@ -5386,7 +5393,7 @@ static int classify_ident (
if (kwp->valid != 0 && if (kwp->valid != 0 &&
(awk->option & kwp->valid) != kwp->valid) continue; (awk->option & kwp->valid) != kwp->valid) continue;
pair = ase_map_get (awk->wtab, kwp->name, kwp->name_len); pair = ase_map_search (awk->wtab, kwp->name, kwp->name_len);
if (pair != ASE_NULL) if (pair != ASE_NULL)
{ {
k = ((ase_cstr_t*)(pair->vptr))->ptr; k = ((ase_cstr_t*)(pair->vptr))->ptr;
@ -5473,6 +5480,7 @@ static int deparse (ase_awk_t* awk)
struct deparse_func_t df; struct deparse_func_t df;
int n = 0; int n = 0;
ase_ssize_t op; ase_ssize_t op;
ase_cstr_t kw;
ASE_ASSERT (awk->src.ios.out != ASE_NULL); ASE_ASSERT (awk->src.ios.out != ASE_NULL);
@ -5512,7 +5520,9 @@ static int deparse (ase_awk_t* awk)
ase_size_t i, len; ase_size_t i, len;
ASE_ASSERT (awk->tree.nglobals > 0); ASE_ASSERT (awk->tree.nglobals > 0);
if (ase_awk_putsrcstr(awk,ase_awk_getkw(awk,ASE_T("global"))) == -1)
ase_awk_getkw (ase, KW_GLOBAL, &kw);
if (ase_awk_putsrcstrx(awk,kw.ptr,kw.len)) == -1)
{ {
EXIT_DEPARSE (); EXIT_DEPARSE ();
} }
@ -5691,51 +5701,68 @@ exit_deparse:
return n; return n;
} }
#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) static int deparse_func (ase_map_pair_t* pair, void* arg)
{ {
struct deparse_func_t* df = (struct deparse_func_t*)arg; struct deparse_func_t* df = (struct deparse_func_t*)arg;
/* CHECK: */
ase_awk_afn_t* afn = (ase_awk_afn_t*)pair->vptr; ase_awk_afn_t* afn = (ase_awk_afn_t*)pair->vptr;
ase_size_t i, n; ase_size_t i, n;
ase_cstr_t kw;
ASE_ASSERT (ase_strxncmp (ASE_PAIR_KEYPTR(pair), ASE_PAIR_KEYLEN(pair), afn->name, afn->name_len) == 0); ASE_ASSERT (ase_strxncmp (ASE_PAIR_KEYPTR(pair), ASE_PAIR_KEYLEN(pair), afn->name, afn->name_len) == 0);
PUTSRCSTR (df, ase_awk_getkw(df->awk,ASE_T("function"))); #define PUT_C(x,c) \
PUTSRCSTR (df, ASE_T(" ")); if (put_char(x->awk,c)==-1) { \
PUTSRCSTR (df, afn->name); x->ret = -1; return ASE_MAP_WALK_STOP; \
PUTSRCSTR (df, ASE_T(" (")); }
#define PUT_S(x,str) \
if (ase_awk_putsrcstr(x->awk,str) == -1) { \
x->ret = -1; return ASE_MAP_WALK_STOP; \
}
#define PUT_SX(x,str,len) \
if (ase_awk_putsrcstrx (x->awk, str, len) == -1) { \
x->ret = -1; return ASE_MAP_WALK_STOP; \
}
ase_awk_getkw (df->awk, KW_FUNCTION, &kw);
PUT_SX (df, kw->ptr, kw->len));
PUT_C (df, ASE_T(' '));
PUT_S (df, afn->name);
PUT_S (df, ASE_T(" ("));
for (i = 0; i < afn->nargs; ) for (i = 0; i < afn->nargs; )
{ {
n = ase_awk_longtostr (i++, 10, n = ase_awk_longtostr (i++, 10,
ASE_T("__p"), df->tmp, df->tmp_len); ASE_T("__p"), df->tmp, df->tmp_len);
ASE_ASSERT (n != (ase_size_t)-1); ASE_ASSERT (n != (ase_size_t)-1);
if (ase_awk_putsrcstrx (df->awk, df->tmp, n) == -1) return -1; PUT_SX (df, df->tmp, n);
if (i >= afn->nargs) break; if (i >= afn->nargs) break;
PUTSRCSTR (df, ASE_T(", ")); PUT_S (df, ASE_T(", "));
} }
PUTSRCSTR (df, ASE_T(")")); PUT_S (df, ASE_T(")"));
if (df->awk->option & ASE_AWK_CRLF) if (df->awk->option & ASE_AWK_CRLF) PUT_C (df, ASE_T('\r'));
{
if (put_char (df->awk, ASE_T('\r')) == -1) return -1; PUT_C (df, ASE_T('\n'));
}
if (put_char (df->awk, ASE_T('\n')) == -1) return -1;
if (ase_awk_prnpt (df->awk, afn->body) == -1) return -1; if (ase_awk_prnpt (df->awk, afn->body) == -1) return -1;
if (df->awk->option & ASE_AWK_CRLF) if (df->awk->option & ASE_AWK_CRLF)
{ {
if (put_char (df->awk, ASE_T('\r')) == -1) return -1; PUT_C (df, ASE_T('\r'));
} }
if (put_char (df->awk, ASE_T('\n')) == -1) return -1; PUT_C (df, ASE_T('\n'));
return ASE_MAP_WALK_FORWARD; return ASE_MAP_WALK_FORWARD;
#undef PUT_C
#undef PUT_S
#undef PUT_SX
} }
static int put_char (ase_awk_t* awk, ase_char_t c) static int put_char (ase_awk_t* awk, ase_char_t c)

View File

@ -1,15 +1,39 @@
/* /*
* $Id: parse.h 115 2008-03-03 11:13:15Z baconevi $ * $Id: parse.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_PARSE_H_ #ifndef _ASE_LIB_AWK_PARSE_H_
#define _ASE_AWK_PARSE_H_ #define _ASE_LIB_AWK_PARSE_H_
#ifndef _ASE_AWK_AWK_H_ /* these enums should match kwtab in parse.c */
#error Never include this file directly. Include <ase/awk/awk.h> instead enum kw_t
#endif {
KW_IN,
KW_BEGIN,
KW_END,
KW_FUNCTION,
KW_LOCAL,
KW_GLOBAL,
KW_IF,
KW_ELSE,
KW_WHILE,
KW_FOR,
KW_DO,
KW_BREAK,
KW_CONTINUE,
KW_RETURN,
KW_EXIT,
KW_NEXT,
KW_NEXTFILE,
KW_NEXTOFILE,
KW_DELETE,
KW_RESET,
KW_PRINT,
KW_PRINTF,
KW_GETLINE,
};
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -21,7 +45,8 @@ int ase_awk_putsrcstrx (
const ase_char_t* ase_awk_getglobalname ( const ase_char_t* ase_awk_getglobalname (
ase_awk_t* awk, ase_size_t idx, ase_size_t* len); ase_awk_t* awk, ase_size_t idx, ase_size_t* len);
const ase_char_t* ase_awk_getkw (ase_awk_t* awk, const ase_char_t* kw); ase_cstr_t* ase_awk_getkw (ase_awk_t* awk, int id, ase_cstr_t* s);
int ase_awk_initglobals (ase_awk_t* awk); int ase_awk_initglobals (ase_awk_t* awk);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: run.c 349 2008-08-28 14:21:25Z baconevi $ * $Id: run.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -1022,7 +1022,7 @@ static int build_runarg (
* it has successfully been assigned into ARGV. */ * it has successfully been assigned into ARGV. */
ase_awk_refupval (run, v_tmp); ase_awk_refupval (run, v_tmp);
if (ase_map_putx ( if (ase_map_upsertx (
((ase_awk_val_map_t*)v_argv)->map, ((ase_awk_val_map_t*)v_argv)->map,
key, key_len, v_tmp, ASE_NULL) == -1) key, key_len, v_tmp, ASE_NULL) == -1)
{ {
@ -2448,7 +2448,7 @@ static int run_delete (ase_awk_run_t* run, ase_awk_nde_delete_t* nde)
(var->type == ASE_AWK_NDE_NAMEDIDX && var->idx != ASE_NULL), (var->type == ASE_AWK_NDE_NAMEDIDX && var->idx != ASE_NULL),
"if a named variable has an index part and a named indexed variable doesn't have an index part, the program is definitely wrong"); "if a named variable has an index part and a named indexed variable doesn't have an index part, the program is definitely wrong");
pair = ase_map_get ( pair = ase_map_search (
run->named, var->id.name, var->id.name_len); run->named, var->id.name, var->id.name_len);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -2465,7 +2465,7 @@ static int run_delete (ase_awk_run_t* run, ase_awk_nde_delete_t* nde)
return -1; return -1;
} }
pair = ase_map_put (run->named, pair = ase_map_upsert (run->named,
var->id.name, var->id.name_len, tmp); var->id.name, var->id.name_len, tmp);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -3327,7 +3327,7 @@ static ase_awk_val_t* do_assignment_scalar (
ase_map_pair_t* pair; ase_map_pair_t* pair;
int n; int n;
pair = ase_map_get ( pair = ase_map_search (
run->named, var->id.name, var->id.name_len); run->named, var->id.name, var->id.name_len);
if (pair != ASE_NULL && if (pair != ASE_NULL &&
((ase_awk_val_t*)pair->val)->type == ASE_AWK_VAL_MAP) ((ase_awk_val_t*)pair->val)->type == ASE_AWK_VAL_MAP)
@ -3344,7 +3344,7 @@ static ase_awk_val_t* do_assignment_scalar (
return ASE_NULL; return ASE_NULL;
} }
n = ase_map_putx (run->named, n = ase_map_upsertx (run->named,
var->id.name, var->id.name_len, val, ASE_NULL); var->id.name, var->id.name_len, val, ASE_NULL);
if (n < 0) if (n < 0)
{ {
@ -3429,7 +3429,7 @@ static ase_awk_val_t* do_assignment_map (
if (var->type == ASE_AWK_NDE_NAMEDIDX) if (var->type == ASE_AWK_NDE_NAMEDIDX)
{ {
ase_map_pair_t* pair; ase_map_pair_t* pair;
pair = ase_map_get ( pair = ase_map_search (
run->named, var->id.name, var->id.name_len); run->named, var->id.name, var->id.name_len);
map = (pair == ASE_NULL)? map = (pair == ASE_NULL)?
(ase_awk_val_map_t*)ase_awk_val_nil: (ase_awk_val_map_t*)ase_awk_val_nil:
@ -3461,8 +3461,8 @@ static ase_awk_val_t* do_assignment_map (
{ {
/* doesn't have to decrease the reference count /* doesn't have to decrease the reference count
* of the previous value here as it is done by * of the previous value here as it is done by
* ase_map_put */ * ase_map_upsert */
if (ase_map_put (run->named, if (ase_map_upsert (run->named,
var->id.name, var->id.name_len, tmp) == ASE_NULL) var->id.name, var->id.name_len, tmp) == ASE_NULL)
{ {
ase_awk_refupval (run, tmp); ase_awk_refupval (run, tmp);
@ -3524,7 +3524,7 @@ static ase_awk_val_t* do_assignment_map (
str, (int)map->ref, (int)map->type); str, (int)map->ref, (int)map->type);
#endif #endif
n = ase_map_putx (map->map, str, len, val, ASE_NULL); n = ase_map_upsertx (map->map, str, len, val, ASE_NULL);
if (n < 0) if (n < 0)
{ {
if (str != idxbuf) ASE_AWK_FREE (run->awk, str); if (str != idxbuf) ASE_AWK_FREE (run->awk, str);
@ -3849,7 +3849,7 @@ static ase_awk_val_t* eval_binop_in (
ase_map_t* map; ase_map_t* map;
map = ((ase_awk_val_map_t*)rv)->map; map = ((ase_awk_val_map_t*)rv)->map;
res = (ase_map_get (map, str, len) == ASE_NULL)? res = (ase_map_search (map, str, len) == ASE_NULL)?
ase_awk_val_zero: ase_awk_val_one; ase_awk_val_zero: ase_awk_val_one;
if (str != idxbuf) ASE_AWK_FREE (run->awk, str); if (str != idxbuf) ASE_AWK_FREE (run->awk, str);
@ -5381,7 +5381,7 @@ static ase_awk_val_t* eval_afn_intrinsic (
ase_awk_afn_t* afn; ase_awk_afn_t* afn;
ase_map_pair_t* pair; ase_map_pair_t* pair;
pair = ase_map_get (run->awk->tree.afns, pair = ase_map_search (run->awk->tree.afns,
call->what.afn.name.ptr, call->what.afn.name.len); call->what.afn.name.ptr, call->what.afn.name.len);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -5737,14 +5737,14 @@ static int get_reference (
{ {
ase_map_pair_t* pair; ase_map_pair_t* pair;
pair = ase_map_get ( pair = ase_map_search (
run->named, tgt->id.name, tgt->id.name_len); run->named, tgt->id.name, tgt->id.name_len);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
/* it is bad that the named variable has to be /* it is bad that the named variable has to be
* created in the function named "__get_refernce". * created in the function named "__get_refernce".
* would there be any better ways to avoid this? */ * would there be any better ways to avoid this? */
pair = ase_map_put ( pair = ase_map_upsert (
run->named, tgt->id.name, run->named, tgt->id.name,
tgt->id.name_len, ase_awk_val_nil); tgt->id.name_len, ase_awk_val_nil);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -5782,11 +5782,11 @@ static int get_reference (
{ {
ase_map_pair_t* pair; ase_map_pair_t* pair;
pair = ase_map_get ( pair = ase_map_search (
run->named, tgt->id.name, tgt->id.name_len); run->named, tgt->id.name, tgt->id.name_len);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
pair = ase_map_put ( pair = ase_map_upsert (
run->named, tgt->id.name, run->named, tgt->id.name,
tgt->id.name_len, ase_awk_val_nil); tgt->id.name_len, ase_awk_val_nil);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -5908,10 +5908,10 @@ static ase_awk_val_t** get_reference_indexed (
if (str == ASE_NULL) return ASE_NULL; if (str == ASE_NULL) return ASE_NULL;
} }
pair = ase_map_get ((*(ase_awk_val_map_t**)val)->map, str, len); pair = ase_map_search ((*(ase_awk_val_map_t**)val)->map, str, len);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
pair = ase_map_put ( pair = ase_map_upsert (
(*(ase_awk_val_map_t**)val)->map, (*(ase_awk_val_map_t**)val)->map,
str, len, ase_awk_val_nil); str, len, ase_awk_val_nil);
if (pair == ASE_NULL) if (pair == ASE_NULL)
@ -5996,7 +5996,7 @@ static ase_awk_val_t* eval_named (ase_awk_run_t* run, ase_awk_nde_t* nde)
{ {
ase_map_pair_t* pair; ase_map_pair_t* pair;
pair = ase_map_get (run->named, pair = ase_map_search (run->named,
((ase_awk_nde_var_t*)nde)->id.name, ((ase_awk_nde_var_t*)nde)->id.name,
((ase_awk_nde_var_t*)nde)->id.name_len); ((ase_awk_nde_var_t*)nde)->id.name_len);
@ -6060,7 +6060,7 @@ static ase_awk_val_t* eval_indexed (
if (str == ASE_NULL) return ASE_NULL; if (str == ASE_NULL) return ASE_NULL;
} }
pair = ase_map_get ((*(ase_awk_val_map_t**)val)->map, str, len); pair = ase_map_search ((*(ase_awk_val_map_t**)val)->map, str, len);
if (str != idxbuf) ASE_AWK_FREE (run->awk, str); if (str != idxbuf) ASE_AWK_FREE (run->awk, str);
return (pair == ASE_NULL)? ase_awk_val_nil: (ase_awk_val_t*)pair->val; return (pair == ASE_NULL)? ase_awk_val_nil: (ase_awk_val_t*)pair->val;
@ -6071,10 +6071,10 @@ 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_awk_nde_var_t* tgt = (ase_awk_nde_var_t*)nde;
ase_map_pair_t* pair; ase_map_pair_t* pair;
pair = ase_map_get (run->named, tgt->id.name, tgt->id.name_len); pair = ase_map_search (run->named, tgt->id.name, tgt->id.name_len);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
pair = ase_map_put (run->named, pair = ase_map_upsert (run->named,
tgt->id.name, tgt->id.name_len, ase_awk_val_nil); tgt->id.name, tgt->id.name_len, ase_awk_val_nil);
if (pair == ASE_NULL) if (pair == ASE_NULL)
{ {
@ -6486,8 +6486,9 @@ static ase_char_t* idxnde_to_str (
{ {
/* multidimensional index */ /* multidimensional index */
ase_str_t idxstr; ase_str_t idxstr;
ase_cstr_t tmp;
if (ase_str_open (&idxstr, DEF_BUF_CAPA, MMGR(run)) == ASE_NULL) if (ase_str_init (MMGR(run) &idxstr, DEF_BUF_CAPA) == ASE_NULL)
{ {
ase_awk_setrunerror ( ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, nde->line, ASE_NULL, 0); run, ASE_AWK_ENOMEM, nde->line, ASE_NULL, 0);
@ -6499,7 +6500,7 @@ static ase_char_t* idxnde_to_str (
idx = eval_expression (run, nde); idx = eval_expression (run, nde);
if (idx == ASE_NULL) if (idx == ASE_NULL)
{ {
ase_str_close (&idxstr); ase_str_fini (&idxstr);
return ASE_NULL; return ASE_NULL;
} }
@ -6511,7 +6512,7 @@ static ase_char_t* idxnde_to_str (
run->global.subsep.len) == (ase_size_t)-1) run->global.subsep.len) == (ase_size_t)-1)
{ {
ase_awk_refdownval (run, idx); ase_awk_refdownval (run, idx);
ase_str_close (&idxstr); ase_str_fini (&idxstr);
ase_awk_setrunerror ( ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, nde->line, run, ASE_AWK_ENOMEM, nde->line,
@ -6524,7 +6525,7 @@ static ase_char_t* idxnde_to_str (
run, idx, 0, &idxstr, ASE_NULL) == ASE_NULL) run, idx, 0, &idxstr, ASE_NULL) == ASE_NULL)
{ {
ase_awk_refdownval (run, idx); ase_awk_refdownval (run, idx);
ase_str_close (&idxstr); ase_str_fini (&idxstr);
return ASE_NULL; return ASE_NULL;
} }
@ -6532,9 +6533,11 @@ static ase_char_t* idxnde_to_str (
nde = nde->next; nde = nde->next;
} }
str = ASE_STR_BUF(&idxstr); ase_str_yield (&idxstr, &tmp, 0);
*len = ASE_STR_LEN(&idxstr); str = tmp.ptr;
ase_str_forfeit (&idxstr); *len = tmp.len;
ase_str_fini (&idxstr);
} }
return str; return str;

View File

@ -1,15 +1,11 @@
/* /*
* $Id: run.h 115 2008-03-03 11:13:15Z baconevi $ * $Id: run.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_RUN_H_ #ifndef _ASE_LIB_AWK_RUN_H_
#define _ASE_AWK_RUN_H_ #define _ASE_LIB_AWK_RUN_H_
#ifndef _ASE_AWK_AWK_H_
#error Never include this file directly. Include <ase/awk/awk.h> instead
#endif
enum ase_awk_assop_type_t enum ase_awk_assop_type_t
{ {

View File

@ -1,11 +1,11 @@
/* /*
* $Id: tab.h 115 2008-03-03 11:13:15Z baconevi $ * $Id: tab.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_TAB_H_ #ifndef _ASE_LIB_AWK_TAB_H_
#define _ASE_AWK_TAB_H_ #define _ASE_LIB_AWK_TAB_H_
#ifndef _ASE_AWK_AWK_H_ #ifndef _ASE_AWK_AWK_H_
#error Never include this file directly. Include <ase/awk/awk.h> instead #error Never include this file directly. Include <ase/awk/awk.h> instead

View File

@ -1,5 +1,5 @@
/* /*
* $Id: tree.c 337 2008-08-20 09:17:25Z baconevi $ * $Id: tree.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -131,6 +131,8 @@ static int print_tabs (ase_awk_t* awk, int depth)
static int print_expression (ase_awk_t* awk, ase_awk_nde_t* nde) static int print_expression (ase_awk_t* awk, ase_awk_nde_t* nde)
{ {
ase_cstr_t kw;
switch (nde->type) switch (nde->type)
{ {
case ASE_AWK_NDE_GRP: case ASE_AWK_NDE_GRP:
@ -556,7 +558,8 @@ static int print_expression (ase_awk_t* awk, ase_awk_nde_t* nde)
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
} }
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("getline"))); ase_awk_getkw (awk, KW_GETLINE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
if (px->var != ASE_NULL) if (px->var != ASE_NULL)
{ {
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
@ -600,6 +603,7 @@ static int print_expression_list (ase_awk_t* awk, ase_awk_nde_t* tree)
static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth) static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
{ {
ase_size_t i; ase_size_t i;
ase_cstr_t kw;
switch (p->type) switch (p->type)
{ {
@ -623,7 +627,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
if (px->nlocals > 0) if (px->nlocals > 0)
{ {
PRINT_TABS (awk, depth + 1); PRINT_TABS (awk, depth + 1);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("local"))); ase_awk_getkw (awk, KW_LOCAL, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
for (i = 0; i < px->nlocals - 1; i++) for (i = 0; i < px->nlocals - 1; i++)
@ -657,7 +662,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
ase_awk_nde_if_t* px = (ase_awk_nde_if_t*)p; ase_awk_nde_if_t* px = (ase_awk_nde_if_t*)p;
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("if"))); ase_awk_getkw (awk, KW_IF, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" (")); PUT_SRCSTR (awk, ASE_T(" ("));
PRINT_EXPRESSION (awk, px->test); PRINT_EXPRESSION (awk, px->test);
PUT_SRCSTR (awk, ASE_T(")")); PUT_SRCSTR (awk, ASE_T(")"));
@ -672,7 +678,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
if (px->else_part != ASE_NULL) if (px->else_part != ASE_NULL)
{ {
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("else"))); ase_awk_getkw (awk, KW_ELSE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
if (px->else_part->type == ASE_AWK_NDE_BLK) if (px->else_part->type == ASE_AWK_NDE_BLK)
PRINT_STATEMENTS (awk, px->else_part, depth); PRINT_STATEMENTS (awk, px->else_part, depth);
@ -687,7 +694,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
ase_awk_nde_while_t* px = (ase_awk_nde_while_t*)p; ase_awk_nde_while_t* px = (ase_awk_nde_while_t*)p;
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("while"))); ase_awk_getkw (awk, KW_WHILE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" (")); PUT_SRCSTR (awk, ASE_T(" ("));
PRINT_EXPRESSION (awk, px->test); PRINT_EXPRESSION (awk, px->test);
PUT_SRCSTR (awk, ASE_T(")")); PUT_SRCSTR (awk, ASE_T(")"));
@ -708,7 +716,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
ase_awk_nde_while_t* px = (ase_awk_nde_while_t*)p; ase_awk_nde_while_t* px = (ase_awk_nde_while_t*)p;
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("do"))); ase_awk_getkw (awk, KW_DO, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
if (px->body->type == ASE_AWK_NDE_BLK) if (px->body->type == ASE_AWK_NDE_BLK)
{ {
@ -720,7 +729,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
} }
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("while"))); ase_awk_getkw (awk, KW_WHILE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" (")); PUT_SRCSTR (awk, ASE_T(" ("));
PRINT_EXPRESSION (awk, px->test); PRINT_EXPRESSION (awk, px->test);
PUT_SRCSTR (awk, ASE_T(");")); PUT_SRCSTR (awk, ASE_T(");"));
@ -733,7 +743,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
ase_awk_nde_for_t* px = (ase_awk_nde_for_t*)p; ase_awk_nde_for_t* px = (ase_awk_nde_for_t*)p;
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("for"))); ase_awk_getkw (awk, KW_FOR, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" (")); PUT_SRCSTR (awk, ASE_T(" ("));
if (px->init != ASE_NULL) if (px->init != ASE_NULL)
{ {
@ -768,7 +779,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
ase_awk_nde_foreach_t* px = (ase_awk_nde_foreach_t*)p; ase_awk_nde_foreach_t* px = (ase_awk_nde_foreach_t*)p;
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("for"))); ase_awk_getkw (awk, KW_FOR, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
PRINT_EXPRESSION (awk, px->test); PRINT_EXPRESSION (awk, px->test);
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
@ -786,7 +798,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
case ASE_AWK_NDE_BREAK: case ASE_AWK_NDE_BREAK:
{ {
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("break"))); ase_awk_getkw (awk, KW_BREAK, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(";")); PUT_SRCSTR (awk, ASE_T(";"));
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
break; break;
@ -795,7 +808,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
case ASE_AWK_NDE_CONTINUE: case ASE_AWK_NDE_CONTINUE:
{ {
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("continue"))); ase_awk_getkw (awk, KW_CONTINUE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(";")); PUT_SRCSTR (awk, ASE_T(";"));
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
break; break;
@ -806,13 +820,15 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
if (((ase_awk_nde_return_t*)p)->val == ASE_NULL) if (((ase_awk_nde_return_t*)p)->val == ASE_NULL)
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("return"))); ase_awk_getkw (awk, KW_RETURN, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(";")); PUT_SRCSTR (awk, ASE_T(";"));
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
} }
else else
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("return"))); ase_awk_getkw (awk, KW_RETURN, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
ASE_ASSERT (((ase_awk_nde_return_t*)p)->val->next == ASE_NULL); ASE_ASSERT (((ase_awk_nde_return_t*)p)->val->next == ASE_NULL);
@ -830,13 +846,15 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
if (px->val == ASE_NULL) if (px->val == ASE_NULL)
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("exit"))); ase_awk_getkw (awk, KW_EXIT, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(";")); PUT_SRCSTR (awk, ASE_T(";"));
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
} }
else else
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("exit"))); ase_awk_getkw (awk, KW_EXIT, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
ASE_ASSERT (px->val->next == ASE_NULL); ASE_ASSERT (px->val->next == ASE_NULL);
PRINT_EXPRESSION (awk, px->val); PRINT_EXPRESSION (awk, px->val);
@ -849,7 +867,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
case ASE_AWK_NDE_NEXT: case ASE_AWK_NDE_NEXT:
{ {
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("next"))); ase_awk_getkw (awk, KW_NEXT, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(";")); PUT_SRCSTR (awk, ASE_T(";"));
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
break; break;
@ -860,11 +879,13 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
if (((ase_awk_nde_nextfile_t*)p)->out) if (((ase_awk_nde_nextfile_t*)p)->out)
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("nextofile"))); ase_awk_getkw (awk, KW_NEXTOFILE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
} }
else else
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("nextfile"))); ase_awk_getkw (awk, KW_NEXTFILE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
} }
PUT_SRCSTR (awk, ASE_T(";")); PUT_SRCSTR (awk, ASE_T(";"));
PUT_NEWLINE (awk); PUT_NEWLINE (awk);
@ -874,7 +895,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
case ASE_AWK_NDE_DELETE: case ASE_AWK_NDE_DELETE:
{ {
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("delete"))); ase_awk_getkw (awk, KW_DELETE, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
ase_awk_prnpt (awk, ((ase_awk_nde_delete_t*)p)->var); ase_awk_prnpt (awk, ((ase_awk_nde_delete_t*)p)->var);
break; break;
@ -883,7 +905,8 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
case ASE_AWK_NDE_RESET: case ASE_AWK_NDE_RESET:
{ {
PRINT_TABS (awk, depth); PRINT_TABS (awk, depth);
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("reset"))); ase_awk_getkw (awk, KW_RESET, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
PUT_SRCSTR (awk, ASE_T(" ")); PUT_SRCSTR (awk, ASE_T(" "));
ase_awk_prnpt (awk, ((ase_awk_nde_reset_t*)p)->var); ase_awk_prnpt (awk, ((ase_awk_nde_reset_t*)p)->var);
break; break;
@ -898,11 +921,13 @@ static int print_statement (ase_awk_t* awk, ase_awk_nde_t* p, int depth)
if (p->type == ASE_AWK_NDE_PRINT) if (p->type == ASE_AWK_NDE_PRINT)
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("print"))); ase_awk_getkw (awk, KW_PRINT, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
} }
else else
{ {
PUT_SRCSTR (awk, ase_awk_getkw(awk,ASE_T("printf"))); ase_awk_getkw (awk, KW_PRINTF, &kw);
PUT_SRCSTRX (awk, kw.ptr, kw.len);
} }
if (px->args != ASE_NULL) if (px->args != ASE_NULL)

View File

@ -1,15 +1,11 @@
/* /*
* $Id: tree.h 237 2008-07-09 13:20:08Z baconevi $ * $Id: tree.h 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
#ifndef _ASE_AWK_TREE_H_ #ifndef _ASE_LIB_AWK_TREE_H_
#define _ASE_AWK_TREE_H_ #define _ASE_LIB_AWK_TREE_H_
#ifndef _ASE_AWK_AWK_H_
#error Never include this file directly. Include <ase/awk/awk.h> instead
#endif
enum ase_awk_nde_type_t enum ase_awk_nde_type_t
{ {

View File

@ -1,5 +1,5 @@
/* /*
* $Id: val.c 337 2008-08-20 09:17:25Z baconevi $ * $Id: val.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -927,15 +927,15 @@ static ase_char_t* val_real_to_str (
tmp_len = run->global.convfmt.len; tmp_len = run->global.convfmt.len;
} }
if (ase_str_open (&out, 256, run->awk->mmgr) == ASE_NULL) if (ase_str_init (&out, run->awk->mmgr, 256) == ASE_NULL)
{ {
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0); ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
return ASE_NULL; return ASE_NULL;
} }
if (ase_str_open (&fbu, 256, run->awk->mmgr) == ASE_NULL) if (ase_str_init (&fbu, run->awk->mmgr, 256) == ASE_NULL)
{ {
ase_str_close (&out); ase_str_fini (&out);
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0); ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
return ASE_NULL; return ASE_NULL;
} }
@ -944,15 +944,18 @@ static ase_char_t* val_real_to_str (
(ase_size_t)-1, (ase_awk_nde_t*)v, &tmp_len); (ase_size_t)-1, (ase_awk_nde_t*)v, &tmp_len);
if (tmp == ASE_NULL) if (tmp == ASE_NULL)
{ {
ase_str_close (&fbu); ase_str_fini (&fbu);
ase_str_close (&out); ase_str_fini (&out);
return ASE_NULL; return ASE_NULL;
} }
if (buf == ASE_NULL) if (buf == ASE_NULL)
{ {
ase_str_close (&fbu); ase_str_fini (&fbu);
ase_str_forfeit (&out);
ase_str_yield (&out, ASE_NULL, 0);
ase_str_fini (&out);
if (len != ASE_NULL) *len = tmp_len; if (len != ASE_NULL) *len = tmp_len;
} }
else if (opt & ASE_AWK_VALTOSTR_FIXED) else if (opt & ASE_AWK_VALTOSTR_FIXED)
@ -973,8 +976,8 @@ static ase_char_t* val_real_to_str (
tmp = (ase_char_t*)buf; tmp = (ase_char_t*)buf;
*len = tmp_len; *len = tmp_len;
ase_str_close (&fbu); ase_str_fini (&fbu);
ase_str_close (&out); ase_str_fini (&out);
} }
else else
{ {
@ -982,8 +985,8 @@ static ase_char_t* val_real_to_str (
if (ase_str_ncat (buf, tmp, tmp_len) == (ase_size_t)-1) if (ase_str_ncat (buf, tmp, tmp_len) == (ase_size_t)-1)
{ {
ase_str_close (&fbu); ase_str_fini (&fbu);
ase_str_close (&out); ase_str_fini (&out);
ase_awk_setrunerror ( ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0); run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
return ASE_NULL; return ASE_NULL;
@ -992,8 +995,8 @@ static ase_char_t* val_real_to_str (
tmp = ASE_STR_BUF(buf); tmp = ASE_STR_BUF(buf);
if (len != ASE_NULL) *len = ASE_STR_LEN(buf); if (len != ASE_NULL) *len = ASE_STR_LEN(buf);
ase_str_close (&fbu); ase_str_fini (&fbu);
ase_str_close (&out); ase_str_fini (&out);
} }
return tmp; return tmp;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: str_dyn.c 357 2008-08-31 11:17:01Z baconevi $ * $Id: str_dyn.c 363 2008-09-04 10:58:08Z baconevi $
* *
* {License} * {License}
*/ */
@ -32,7 +32,9 @@ void ase_str_close (ase_str_t* str)
ase_str_t* ase_str_init (ase_str_t* str, ase_mmgr_t* mmgr, ase_size_t capa) ase_str_t* ase_str_init (ase_str_t* str, ase_mmgr_t* mmgr, ase_size_t capa)
{ {
ASE_MEMSET (str, 0, ASE_SIZEOF(ase_str_t)); ASE_MEMSET (str, 0, ASE_SIZEOF(ase_str_t));
str->mmgr = mmgr; str->mmgr = mmgr;
str->sizer = ASE_NULL;
if (capa == 0) str->buf = ASE_NULL; if (capa == 0) str->buf = ASE_NULL;
else else
@ -80,6 +82,59 @@ int ase_str_yield (ase_str_t* str, ase_cstr_t* buf, int new_capa)
return 0; return 0;
} }
ase_sizer_t ase_str_getsizer (ase_str_t* str)
{
return str->sizer;
}
void ase_str_setsizer (ase_str_t* str, ase_sizer_t sizer)
{
str->sizer = sizer;
}
ase_size_t ase_str_getcapa (ase_str_t* str)
{
return str->capa;
}
ase_size_t ase_str_setcapa (ase_str_t* str, ase_size_t capa)
{
ase_char_t* tmp;
if (str->mmgr->realloc != ASE_NULL && str->buf != ASE_NULL)
{
tmp = (ase_char_t*) ASE_MMGR_REALLOC (
str->mmgr, str->buf,
ASE_SIZEOF(ase_char_t)*(capa+1));
if (tmp == ASE_NULL) return (ase_size_t)-1;
}
else
{
tmp = (ase_char_t*) ASE_MMGR_ALLOC (
str->mmgr, ASE_SIZEOF(ase_char_t)*(capa+1));
if (tmp == ASE_NULL) return (ase_size_t)-1;
if (str->buf != ASE_NULL)
{
ase_size_t ncopy = (str->size <= capa)? str->size: capa;
ASE_MEMCPY (tmp, str->buf,
ASE_SIZEOF(ase_char_t)*(ncopy+1));
ASE_MMGR_FREE (str->mmgr, str->buf);
}
}
if (capa < str->size)
{
str->size = capa;
tmp[capa] = ASE_T('\0');
}
str->capa = capa;
str->buf = tmp;
return str->capa;
}
void ase_str_clear (ase_str_t* str) void ase_str_clear (ase_str_t* str)
{ {
str->size = 0; str->size = 0;
@ -138,44 +193,45 @@ ase_size_t ase_str_cat (ase_str_t* str, const ase_char_t* s)
return ase_str_ncat (str, s, ase_strlen(s)); return ase_str_ncat (str, s, ase_strlen(s));
} }
ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len) ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len)
{ {
if (len > str->capa - str->size) if (len > str->capa - str->size)
{ {
ase_char_t* tmp; ase_size_t ncapa;
ase_size_t capa;
capa = str->size + len; if (str->sizer == ASE_NULL)
/* double the capa if necessary for concatenation */
if (capa < str->capa * 2) capa = str->capa * 2;
if (str->mmgr->realloc != ASE_NULL && str->buf != ASE_NULL)
{ {
tmp = (ase_char_t*) ASE_REALLOC ( /* increase the capacity by the length to add */
str->mmgr, str->buf, ncapa = str->size + len;
ASE_SIZEOF(ase_char_t)*(capa+1)); /* if the new capacity is less than the double,
if (tmp == ASE_NULL) return (ase_size_t)-1; * just double it */
if (ncapa < str->capa * 2) ncapa = str->capa * 2;
} }
else else
{ {
tmp = (ase_char_t*) ASE_MMGR_ALLOC ( /* let the user determine the new capacity */
str->mmgr, ASE_SIZEOF(ase_char_t)*(capa+1)); ncapa = str->sizer (str);
if (tmp == ASE_NULL) return (ase_size_t)-1;
if (str->buf != ASE_NULL)
{
ASE_MEMCPY (tmp, str->buf,
ASE_SIZEOF(ase_char_t)*(str->capa+1));
ASE_MMGR_FREE (str->mmgr, str->buf);
}
} }
str->capa = capa; if (ase_str_setcapa (str, ncapa) == (ase_size_t)-1)
str->buf = tmp; {
return (ase_size_t)-1;
}
} }
str->size += ase_strncpy (&str->buf[str->size], s, len); if (len > str->capa - str->size)
str->buf[str->size] = ASE_T('\0'); {
/* copy as many characters as the number of cells available */
len = str->capa - str->size;
}
if (len > 0)
{
ASE_MEMCPY (&str->buf[str->size], s, len*ASE_SIZEOF(*s));
str->size += len;
str->buf[str->size] = ASE_T('\0');
}
return str->size; return str->size;
} }