diff --git a/ase/include/ase/awk/awk.h b/ase/include/ase/awk/awk.h index 7c886f25..be47d5f1 100644 --- a/ase/include/ase/awk/awk.h +++ b/ase/include/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h 245 2008-07-15 05:56:32Z baconevi $ + * $Id: awk.h 268 2008-07-19 08:03:49Z baconevi $ * * {License} */ @@ -9,7 +9,7 @@ /** * @file awk.h - * @brief Define an AWK interpreter + * @brief Defines an AWK interpreter * * This file defines most of the data types and functions required to embed * a AWK interpreter engine. @@ -586,36 +586,65 @@ struct ase_awk_val_ref_t extern "C" { #endif +/** @brief represents the nil value */ extern ase_awk_val_t* ase_awk_val_nil; + +/** @brief represents an empty string */ extern ase_awk_val_t* ase_awk_val_zls; + +/** @brief represents a numeric value -1 */ extern ase_awk_val_t* ase_awk_val_negone; + +/** @brief represents a numeric value 0 */ extern ase_awk_val_t* ase_awk_val_zero; + +/** @brief represents a numeric value 1 */ extern ase_awk_val_t* ase_awk_val_one; /** - * @brief opens an awk interpreter + * @brief creates an instance of ase_awk_t + * @param mmgr the pointer to a memory manager + * @return a pointer to an ase_awk_t instance on success, ASE_NULL on failure */ -ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns); +/*ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns);*/ +ase_awk_t* ase_awk_open (const ase_mmgr_t* mmgr); /** - * @brief opens an awk interpreter + * @brief destroys an instance of ase_awk_t + * @param awk the pointer to an ase_awk_t instance to destory + * @return 0 on success, -1 on failure */ int ase_awk_close (ase_awk_t* awk); /** * @brief clears an awk interpreter + * @param awk the pointer to an ase_awk_t instance to clear + * @return 0 on success, -1 on failure */ int ase_awk_clear (ase_awk_t* awk); /** - * @brief associats the user-specified data with an interpreter + * @brief associates the user-specified data with an interpreter + * @param awk the pointer to an ase_awk_t instance + * @param data the pointer to a user-specified data + * @return n/a */ void ase_awk_setassocdata (ase_awk_t* awk, void* data); + /** - * @brief returns the user-specified data associated with an interpreter + * @brief returns the user-specified data associated with an interpreter + * @param awk the pointer to an ase_awk_t instance + * @return The pointer to the user-specified data through ase_awk_setassocdata. + * is returned. ASE_NULL is returned if ase_awk_setassocdata is never + * called. */ void* ase_awk_getassocdata (ase_awk_t* awk); +/** + * @brief returns the pointer to the memory manager in use + * @param awk the pointer to an ase_awk_t instance + * @return the pointer to the memory manager set through ase_awk_open + */ ase_mmgr_t* ase_awk_getmmgr (ase_awk_t* awk); const ase_char_t* ase_awk_geterrstr (ase_awk_t* awk, int num); diff --git a/ase/lib/awk/awk.c b/ase/lib/awk/awk.c index 5a34065e..1e8fe130 100644 --- a/ase/lib/awk/awk.c +++ b/ase/lib/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c 232 2008-06-28 09:38:00Z baconevi $ + * $Id: awk.c 268 2008-07-19 08:03:49Z baconevi $ * * {License} */ @@ -24,6 +24,7 @@ static void free_bfn (void* awk, void* afn); ase_awk_seterror ((awk), (code), (line), &errarg, 1); \ } while (0) +#if 0 ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns) { ase_awk_t* awk; @@ -244,6 +245,209 @@ ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns) return awk; } +#endif +ase_awk_t* ase_awk_open (const ase_mmgr_t* mmgr) +{ + ase_awk_t* awk; + + ASE_ASSERT (mmgr != ASE_NULL); + ASE_ASSERT (mmgr->malloc != ASE_NULL); + ASE_ASSERT (mmgr->free != ASE_NULL); + + /* use ASE_MALLOC instead of ASE_AWK_MALLOC because + * the awk object has not been initialized yet */ + awk = ASE_MALLOC (mmgr, ASE_SIZEOF(ase_awk_t)); + if (awk == ASE_NULL) return ASE_NULL; + + /* it uses the built-in ase_awk_memset because awk is not + * fully initialized yet */ + ase_memset (awk, 0, ASE_SIZEOF(ase_awk_t)); + awk->prmfns.mmgr = *mmgr; + //ase_memcpy (&awk->prmfns, prmfns, ASE_SIZEOF(awk->prmfns)); + + if (ase_str_open ( + &awk->token.name, 128, &awk->prmfns.mmgr) == ASE_NULL) + { + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + awk->wtab = ase_map_open ( + awk, 512, 70, free_word, ASE_NULL, &awk->prmfns.mmgr); + if (awk->wtab == ASE_NULL) + { + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + awk->rwtab = ase_map_open ( + awk, 512, 70, free_word, ASE_NULL, &awk->prmfns.mmgr); + if (awk->rwtab == ASE_NULL) + { + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + /* TODO: initial map size?? */ + awk->tree.afns = ase_map_open ( + awk, 512, 70, free_afn, ASE_NULL, &awk->prmfns.mmgr); + if (awk->tree.afns == ASE_NULL) + { + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + awk->parse.afns = ase_map_open ( + awk, 256, 70, ASE_NULL, ASE_NULL, &awk->prmfns.mmgr); + if (awk->parse.afns == ASE_NULL) + { + ase_map_close (awk->tree.afns); + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + awk->parse.named = ase_map_open ( + awk, 256, 70, ASE_NULL, ASE_NULL, &awk->prmfns.mmgr); + if (awk->parse.named == ASE_NULL) + { + ase_map_close (awk->parse.afns); + ase_map_close (awk->tree.afns); + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + if (ase_awk_tab_open (&awk->parse.globals, awk) == ASE_NULL) + { + ase_map_close (awk->parse.named); + ase_map_close (awk->parse.afns); + ase_map_close (awk->tree.afns); + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + if (ase_awk_tab_open (&awk->parse.locals, awk) == ASE_NULL) + { + ase_awk_tab_close (&awk->parse.globals); + ase_map_close (awk->parse.named); + ase_map_close (awk->parse.afns); + ase_map_close (awk->tree.afns); + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + if (ase_awk_tab_open (&awk->parse.params, awk) == ASE_NULL) + { + ase_awk_tab_close (&awk->parse.locals); + ase_awk_tab_close (&awk->parse.globals); + ase_map_close (awk->parse.named); + ase_map_close (awk->parse.afns); + ase_map_close (awk->tree.afns); + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + awk->option = 0; + awk->errnum = ASE_AWK_ENOERR; + awk->errlin = 0; + awk->stopall = ASE_FALSE; + + awk->parse.nlocals_max = 0; + + awk->tree.nglobals = 0; + awk->tree.nbglobals = 0; + awk->tree.begin = ASE_NULL; + awk->tree.begin_tail = ASE_NULL; + awk->tree.end = ASE_NULL; + awk->tree.end_tail = ASE_NULL; + awk->tree.chain = ASE_NULL; + awk->tree.chain_tail = ASE_NULL; + awk->tree.chain_size = 0; + + awk->token.prev.type = 0; + awk->token.prev.line = 0; + awk->token.prev.column = 0; + awk->token.type = 0; + awk->token.line = 0; + awk->token.column = 0; + + awk->src.lex.curc = ASE_CHAR_EOF; + awk->src.lex.ungotc_count = 0; + awk->src.lex.line = 1; + awk->src.lex.column = 1; + awk->src.shared.buf_pos = 0; + awk->src.shared.buf_len = 0; + + awk->bfn.sys = ASE_NULL; + /*awk->bfn.user = ASE_NULL;*/ + awk->bfn.user = ase_map_open ( + awk, 512, 70, free_bfn, ASE_NULL, &awk->prmfns.mmgr); + if (awk->bfn.user == ASE_NULL) + { + ase_awk_tab_close (&awk->parse.params); + ase_awk_tab_close (&awk->parse.locals); + ase_awk_tab_close (&awk->parse.globals); + ase_map_close (awk->parse.named); + ase_map_close (awk->parse.afns); + ase_map_close (awk->tree.afns); + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + awk->parse.depth.cur.block = 0; + awk->parse.depth.cur.loop = 0; + awk->parse.depth.cur.expr = 0; + + ase_awk_setmaxdepth (awk, ASE_AWK_DEPTH_BLOCK_PARSE, 0); + ase_awk_setmaxdepth (awk, ASE_AWK_DEPTH_BLOCK_RUN, 0); + ase_awk_setmaxdepth (awk, ASE_AWK_DEPTH_EXPR_PARSE, 0); + ase_awk_setmaxdepth (awk, ASE_AWK_DEPTH_EXPR_RUN, 0); + ase_awk_setmaxdepth (awk, ASE_AWK_DEPTH_REX_BUILD, 0); + ase_awk_setmaxdepth (awk, ASE_AWK_DEPTH_REX_MATCH, 0); + + awk->assoc_data = ASE_NULL; + + if (ase_awk_initglobals (awk) == -1) + { + ase_map_close (awk->bfn.user); + ase_awk_tab_close (&awk->parse.params); + ase_awk_tab_close (&awk->parse.locals); + ase_awk_tab_close (&awk->parse.globals); + ase_map_close (awk->parse.named); + ase_map_close (awk->parse.afns); + ase_map_close (awk->tree.afns); + ase_map_close (awk->rwtab); + ase_map_close (awk->wtab); + ase_str_close (&awk->token.name); + ASE_AWK_FREE (awk, awk); + return ASE_NULL; + } + + return awk; +} static void free_word (void* owner, void* ptr) {