2019-12-13 04:29:58 +00:00
/*
* $ Id $
*
2020-04-16 03:42:30 +00:00
Copyright ( c ) 2006 - 2020 Chung , Hyung - Hwan . All rights reserved .
2019-12-13 04:29:58 +00:00
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions
are met :
1. Redistributions of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
2. Redistributions in binary form must reproduce the above copyright
notice , this list of conditions and the following disclaimer in the
documentation and / or other materials provided with the distribution .
THIS SOFTWARE IS PROVIDED BY THE AUTHOR " AS IS " AND ANY EXPRESS OR
IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
# include "hawk-prv.h"
static void free_fun ( hawk_htb_t * map , void * vptr , hawk_oow_t vlen )
{
2020-04-16 03:42:30 +00:00
hawk_t * hawk = * ( hawk_t * * ) ( map + 1 ) ;
2019-12-13 04:29:58 +00:00
hawk_fun_t * f = ( hawk_fun_t * ) vptr ;
/* f->name doesn't have to be freed */
2020-04-16 03:42:30 +00:00
/*hawk_freemem (hawk, f->name);*/
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
if ( f - > argspec ) hawk_freemem ( hawk , f - > argspec ) ;
hawk_clrpt ( hawk , f - > body ) ;
hawk_freemem ( hawk , f ) ;
2019-12-13 04:29:58 +00:00
}
static void free_fnc ( hawk_htb_t * map , void * vptr , hawk_oow_t vlen )
{
2020-04-16 03:42:30 +00:00
hawk_t * hawk = * ( hawk_t * * ) ( map + 1 ) ;
2019-12-13 04:29:58 +00:00
hawk_fnc_t * f = ( hawk_fnc_t * ) vptr ;
2020-04-16 03:42:30 +00:00
hawk_freemem ( hawk , f ) ;
2019-12-13 04:29:58 +00:00
}
2019-12-14 06:11:30 +00:00
static int init_token ( hawk_t * hawk , hawk_tok_t * tok )
2019-12-13 04:29:58 +00:00
{
2019-12-14 06:11:30 +00:00
tok - > name = hawk_ooecs_open ( hawk_getgem ( hawk ) , 0 , 128 ) ;
2019-12-13 04:29:58 +00:00
if ( ! tok - > name ) return - 1 ;
2020-01-01 05:20:28 +00:00
2019-12-13 04:29:58 +00:00
tok - > type = 0 ;
tok - > loc . file = HAWK_NULL ;
tok - > loc . line = 0 ;
tok - > loc . colm = 0 ;
return 0 ;
}
static void fini_token ( hawk_tok_t * tok )
{
if ( tok - > name )
{
hawk_ooecs_close ( tok - > name ) ;
tok - > name = HAWK_NULL ;
}
}
static void clear_token ( hawk_tok_t * tok )
{
if ( tok - > name ) hawk_ooecs_clear ( tok - > name ) ;
tok - > type = 0 ;
tok - > loc . file = HAWK_NULL ;
tok - > loc . line = 0 ;
tok - > loc . colm = 0 ;
}
hawk_t * hawk_open ( hawk_mmgr_t * mmgr , hawk_oow_t xtnsize , hawk_cmgr_t * cmgr , const hawk_prm_t * prm , hawk_errnum_t * errnum )
{
2020-04-16 03:42:30 +00:00
hawk_t * hawk ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk = ( hawk_t * ) HAWK_MMGR_ALLOC ( mmgr , HAWK_SIZEOF ( hawk_t ) + xtnsize ) ;
if ( hawk )
2019-12-13 04:29:58 +00:00
{
int xret ;
2020-04-16 03:42:30 +00:00
xret = hawk_init ( hawk , mmgr , cmgr , prm ) ;
2019-12-13 04:29:58 +00:00
if ( xret < = - 1 )
{
2020-04-16 03:42:30 +00:00
if ( errnum ) * errnum = hawk_geterrnum ( hawk ) ;
HAWK_MMGR_FREE ( mmgr , hawk ) ;
hawk = HAWK_NULL ;
2019-12-13 04:29:58 +00:00
}
2020-04-16 03:42:30 +00:00
else HAWK_MEMSET ( hawk + 1 , 0 , xtnsize ) ;
2019-12-13 04:29:58 +00:00
}
else if ( errnum ) * errnum = HAWK_ENOMEM ;
2020-04-16 03:42:30 +00:00
return hawk ;
2019-12-13 04:29:58 +00:00
}
void hawk_close ( hawk_t * hawk )
{
hawk_fini ( hawk ) ;
HAWK_MMGR_FREE ( hawk_getmmgr ( hawk ) , hawk ) ;
}
2020-04-16 03:42:30 +00:00
int hawk_init ( hawk_t * hawk , hawk_mmgr_t * mmgr , hawk_cmgr_t * cmgr , const hawk_prm_t * prm )
2019-12-13 04:29:58 +00:00
{
static hawk_htb_style_t treefuncbs =
{
{
HAWK_HTB_COPIER_INLINE ,
HAWK_HTB_COPIER_DEFAULT
} ,
{
HAWK_HTB_FREEER_DEFAULT ,
free_fun
} ,
HAWK_HTB_COMPER_DEFAULT ,
HAWK_HTB_KEEPER_DEFAULT ,
HAWK_HTB_SIZER_DEFAULT ,
HAWK_HTB_HASHER_DEFAULT
} ;
static hawk_htb_style_t fncusercbs =
{
{
HAWK_HTB_COPIER_INLINE ,
HAWK_HTB_COPIER_DEFAULT
} ,
{
HAWK_HTB_FREEER_DEFAULT ,
free_fnc
} ,
HAWK_HTB_COMPER_DEFAULT ,
HAWK_HTB_KEEPER_DEFAULT ,
HAWK_HTB_SIZER_DEFAULT ,
HAWK_HTB_HASHER_DEFAULT
} ;
/* zero out the object */
2020-04-16 03:42:30 +00:00
HAWK_MEMSET ( hawk , 0 , HAWK_SIZEOF ( * hawk ) ) ;
2019-12-13 04:29:58 +00:00
/* remember the memory manager */
2020-04-16 03:42:30 +00:00
hawk - > _instsize = HAWK_SIZEOF ( * hawk ) ;
hawk - > _gem . mmgr = mmgr ;
hawk - > _gem . cmgr = cmgr ;
2019-12-13 04:29:58 +00:00
/* initialize error handling fields */
2020-04-16 03:42:30 +00:00
hawk - > _gem . errnum = HAWK_ENOERR ;
hawk - > _gem . errmsg [ 0 ] = ' \0 ' ;
hawk - > _gem . errloc . line = 0 ;
hawk - > _gem . errloc . colm = 0 ;
hawk - > _gem . errloc . file = HAWK_NULL ;
hawk - > errstr = hawk_dfl_errstr ;
hawk - > haltall = 0 ;
2019-12-13 04:29:58 +00:00
/* progagate the primitive functions */
2019-12-21 16:59:00 +00:00
HAWK_ASSERT ( prm ! = HAWK_NULL ) ;
HAWK_ASSERT ( prm - > math . pow ! = HAWK_NULL ) ;
HAWK_ASSERT ( prm - > math . mod ! = HAWK_NULL ) ;
2019-12-13 04:29:58 +00:00
if ( prm = = HAWK_NULL | |
prm - > math . pow = = HAWK_NULL | |
prm - > math . mod = = HAWK_NULL )
{
2020-04-16 03:42:30 +00:00
hawk_seterrnum ( hawk , HAWK_NULL , HAWK_EINVAL ) ;
2019-12-13 04:29:58 +00:00
goto oops ;
}
2020-04-16 03:42:30 +00:00
hawk - > prm = * prm ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
if ( init_token ( hawk , & hawk - > ptok ) < = - 1 | |
init_token ( hawk , & hawk - > tok ) < = - 1 | |
init_token ( hawk , & hawk - > ntok ) < = - 1 ) goto oops ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk - > opt . trait = HAWK_MODERN ;
2020-04-15 15:12:28 +00:00
# if defined(__WIN32__) || defined(_OS2) || defined(__DOS__)
2020-04-16 03:42:30 +00:00
hawk - > opt . trait | = HAWK_CRLF ;
2019-12-13 04:29:58 +00:00
# endif
2020-04-16 03:42:30 +00:00
hawk - > opt . rtx_stack_limit = HAWK_DFL_RTX_STACK_LIMIT ;
hawk - > opt . log_mask = HAWK_LOG_ALL_LEVELS | HAWK_LOG_ALL_TYPES ;
hawk - > opt . log_maxcapa = HAWK_DFL_LOG_MAXCAPA ;
hawk - > log . capa = HAWK_ALIGN_POW2 ( 1 , HAWK_LOG_CAPA_ALIGN ) ;
hawk - > log . ptr = hawk_allocmem ( hawk , ( hawk - > log . capa + 1 ) * HAWK_SIZEOF ( * hawk - > log . ptr ) ) ;
if ( ! hawk - > log . ptr ) goto oops ;
hawk - > tree . ngbls = 0 ;
hawk - > tree . ngbls_base = 0 ;
hawk - > tree . begin = HAWK_NULL ;
hawk - > tree . begin_tail = HAWK_NULL ;
hawk - > tree . end = HAWK_NULL ;
hawk - > tree . end_tail = HAWK_NULL ;
hawk - > tree . chain = HAWK_NULL ;
hawk - > tree . chain_tail = HAWK_NULL ;
hawk - > tree . chain_size = 0 ;
2019-12-13 04:29:58 +00:00
/* TODO: initial map size?? */
2020-04-16 03:42:30 +00:00
hawk - > tree . funs = hawk_htb_open ( hawk_getgem ( hawk ) , HAWK_SIZEOF ( hawk ) , 512 , 70 , HAWK_SIZEOF ( hawk_ooch_t ) , 1 ) ;
hawk - > parse . funs = hawk_htb_open ( hawk_getgem ( hawk ) , HAWK_SIZEOF ( hawk ) , 256 , 70 , HAWK_SIZEOF ( hawk_ooch_t ) , 1 ) ;
hawk - > parse . named = hawk_htb_open ( hawk_getgem ( hawk ) , HAWK_SIZEOF ( hawk ) , 256 , 70 , HAWK_SIZEOF ( hawk_ooch_t ) , 1 ) ;
hawk - > parse . gbls = hawk_arr_open ( hawk_getgem ( hawk ) , HAWK_SIZEOF ( hawk ) , 128 ) ;
hawk - > parse . lcls = hawk_arr_open ( hawk_getgem ( hawk ) , HAWK_SIZEOF ( hawk ) , 64 ) ;
hawk - > parse . params = hawk_arr_open ( hawk_getgem ( hawk ) , HAWK_SIZEOF ( hawk ) , 32 ) ;
hawk - > fnc . sys = HAWK_NULL ;
hawk - > fnc . user = hawk_htb_open ( hawk_getgem ( hawk ) , HAWK_SIZEOF ( hawk ) , 512 , 70 , HAWK_SIZEOF ( hawk_ooch_t ) , 1 ) ;
hawk - > modtab = hawk_rbt_open ( hawk_getgem ( hawk ) , 0 , HAWK_SIZEOF ( hawk_ooch_t ) , 1 ) ;
if ( hawk - > tree . funs = = HAWK_NULL | |
hawk - > parse . funs = = HAWK_NULL | |
hawk - > parse . named = = HAWK_NULL | |
hawk - > parse . gbls = = HAWK_NULL | |
hawk - > parse . lcls = = HAWK_NULL | |
hawk - > parse . params = = HAWK_NULL | |
hawk - > fnc . user = = HAWK_NULL | |
hawk - > modtab = = HAWK_NULL )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
hawk_seterrnum ( hawk , HAWK_NULL , HAWK_ENOMEM ) ;
2019-12-13 04:29:58 +00:00
goto oops ;
}
2020-04-16 03:42:30 +00:00
* ( hawk_t * * ) ( hawk - > tree . funs + 1 ) = hawk ;
hawk_htb_setstyle ( hawk - > tree . funs , & treefuncbs ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
* ( hawk_t * * ) ( hawk - > parse . funs + 1 ) = hawk ;
hawk_htb_setstyle ( hawk - > parse . funs , hawk_get_htb_style ( HAWK_HTB_STYLE_INLINE_KEY_COPIER ) ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
* ( hawk_t * * ) ( hawk - > parse . named + 1 ) = hawk ;
hawk_htb_setstyle ( hawk - > parse . named , hawk_get_htb_style ( HAWK_HTB_STYLE_INLINE_KEY_COPIER ) ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
* ( hawk_t * * ) ( hawk - > parse . gbls + 1 ) = hawk ;
hawk_arr_setscale ( hawk - > parse . gbls , HAWK_SIZEOF ( hawk_ooch_t ) ) ;
2020-04-24 06:49:19 +00:00
hawk_arr_setstyle ( hawk - > parse . gbls , hawk_get_arr_style ( HAWK_ARR_STYLE_INLINE_COPIER ) ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
* ( hawk_t * * ) ( hawk - > parse . lcls + 1 ) = hawk ;
hawk_arr_setscale ( hawk - > parse . lcls , HAWK_SIZEOF ( hawk_ooch_t ) ) ;
2020-04-24 06:49:19 +00:00
hawk_arr_setstyle ( hawk - > parse . lcls , hawk_get_arr_style ( HAWK_ARR_STYLE_INLINE_COPIER ) ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
* ( hawk_t * * ) ( hawk - > parse . params + 1 ) = hawk ;
hawk_arr_setscale ( hawk - > parse . params , HAWK_SIZEOF ( hawk_ooch_t ) ) ;
2020-04-24 06:49:19 +00:00
hawk_arr_setstyle ( hawk - > parse . params , hawk_get_arr_style ( HAWK_ARR_STYLE_INLINE_COPIER ) ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
* ( hawk_t * * ) ( hawk - > fnc . user + 1 ) = hawk ;
hawk_htb_setstyle ( hawk - > fnc . user , & fncusercbs ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk_rbt_setstyle ( hawk - > modtab , hawk_get_rbt_style ( HAWK_RBT_STYLE_INLINE_COPIERS ) ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
if ( hawk_initgbls ( hawk ) < = - 1 ) goto oops ;
2019-12-13 04:29:58 +00:00
return 0 ;
oops :
2020-04-16 03:42:30 +00:00
if ( hawk - > modtab ) hawk_rbt_close ( hawk - > modtab ) ;
if ( hawk - > fnc . user ) hawk_htb_close ( hawk - > fnc . user ) ;
if ( hawk - > parse . params ) hawk_arr_close ( hawk - > parse . params ) ;
if ( hawk - > parse . lcls ) hawk_arr_close ( hawk - > parse . lcls ) ;
if ( hawk - > parse . gbls ) hawk_arr_close ( hawk - > parse . gbls ) ;
if ( hawk - > parse . named ) hawk_htb_close ( hawk - > parse . named ) ;
if ( hawk - > parse . funs ) hawk_htb_close ( hawk - > parse . funs ) ;
if ( hawk - > tree . funs ) hawk_htb_close ( hawk - > tree . funs ) ;
fini_token ( & hawk - > ntok ) ;
fini_token ( & hawk - > tok ) ;
fini_token ( & hawk - > ptok ) ;
if ( hawk - > log . ptr ) hawk_freemem ( hawk , hawk - > log . ptr ) ;
hawk - > log . capa = 0 ;
2019-12-13 04:29:58 +00:00
return - 1 ;
}
2020-04-16 03:42:30 +00:00
void hawk_fini ( hawk_t * hawk )
2019-12-13 04:29:58 +00:00
{
hawk_ecb_t * ecb ;
int i ;
2020-04-16 03:42:30 +00:00
hawk_clear ( hawk ) ;
/*hawk_clrfnc (hawk);*/
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
if ( hawk - > log . len > 0 )
2019-12-14 16:18:35 +00:00
{
2020-04-16 03:42:30 +00:00
int shuterr = hawk - > shuterr ;
hawk - > shuterr = 1 ;
hawk - > prm . logwrite ( hawk , hawk - > log . last_mask , hawk - > log . ptr , hawk - > log . len ) ;
hawk - > shuterr = shuterr ;
2019-12-14 16:18:35 +00:00
}
2020-04-16 03:42:30 +00:00
for ( ecb = hawk - > ecb ; ecb ; ecb = ecb - > next )
if ( ecb - > close ) ecb - > close ( hawk ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk_rbt_close ( hawk - > modtab ) ;
hawk_htb_close ( hawk - > fnc . user ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk_arr_close ( hawk - > parse . params ) ;
hawk_arr_close ( hawk - > parse . lcls ) ;
hawk_arr_close ( hawk - > parse . gbls ) ;
hawk_htb_close ( hawk - > parse . named ) ;
hawk_htb_close ( hawk - > parse . funs ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk_htb_close ( hawk - > tree . funs ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
fini_token ( & hawk - > ntok ) ;
fini_token ( & hawk - > tok ) ;
fini_token ( & hawk - > ptok ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
if ( hawk - > parse . incl_hist . ptr ) hawk_freemem ( hawk , hawk - > parse . incl_hist . ptr ) ;
hawk_clearsionames ( hawk ) ;
2019-12-13 04:29:58 +00:00
/* destroy dynamically allocated options */
2020-04-16 03:42:30 +00:00
for ( i = 0 ; i < HAWK_COUNTOF ( hawk - > opt . mod ) ; i + + )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
if ( hawk - > opt . mod [ i ] . ptr ) hawk_freemem ( hawk , hawk - > opt . mod [ i ] . ptr ) ;
2019-12-13 04:29:58 +00:00
}
2019-12-14 16:18:35 +00:00
2020-04-16 03:42:30 +00:00
if ( hawk - > log . len > 0 )
2020-01-09 09:28:32 +00:00
{
2019-12-14 16:18:35 +00:00
/* flush pending log message that could be generated by the fini
* callbacks . however , the actual logging might not be produced at
* this point because one of the callbacks could arrange to stop
* logging */
2020-04-16 03:42:30 +00:00
int shuterr = hawk - > shuterr ;
hawk - > shuterr = 1 ;
hawk - > prm . logwrite ( hawk , hawk - > log . last_mask , hawk - > log . ptr , hawk - > log . len ) ;
hawk - > shuterr = shuterr ;
2019-12-14 16:18:35 +00:00
}
2020-04-16 03:42:30 +00:00
if ( hawk - > log . ptr )
2019-12-14 16:18:35 +00:00
{
2020-04-16 03:42:30 +00:00
hawk_freemem ( hawk , hawk - > log . ptr ) ;
hawk - > log . capa = 0 ;
hawk - > log . len = 0 ;
2019-12-14 16:18:35 +00:00
}
2019-12-13 04:29:58 +00:00
}
static hawk_rbt_walk_t unload_module ( hawk_rbt_t * rbt , hawk_rbt_pair_t * pair , void * ctx )
{
2020-04-16 03:42:30 +00:00
hawk_t * hawk = ( hawk_t * ) ctx ;
2019-12-13 04:29:58 +00:00
hawk_mod_data_t * md ;
2020-01-14 14:55:34 +00:00
md = HAWK_RBT_VPTR ( pair ) ;
2020-04-16 03:42:30 +00:00
if ( md - > mod . unload ) md - > mod . unload ( & md - > mod , hawk ) ;
if ( md - > handle ) hawk - > prm . modclose ( hawk , md - > handle ) ;
2019-12-13 04:29:58 +00:00
return HAWK_RBT_WALK_FORWARD ;
}
2020-04-16 03:42:30 +00:00
void hawk_clear ( hawk_t * hawk )
2019-12-13 04:29:58 +00:00
{
hawk_ecb_t * ecb ;
2020-04-16 03:42:30 +00:00
for ( ecb = hawk - > ecb ; ecb ; ecb = ecb - > next )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
if ( ecb - > clear ) ecb - > clear ( hawk ) ;
2019-12-13 04:29:58 +00:00
}
2020-04-16 03:42:30 +00:00
hawk - > haltall = 0 ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
clear_token ( & hawk - > tok ) ;
clear_token ( & hawk - > ntok ) ;
clear_token ( & hawk - > ptok ) ;
2019-12-13 04:29:58 +00:00
/* clear all loaded modules */
2020-04-16 03:42:30 +00:00
hawk_rbt_walk ( hawk - > modtab , unload_module , hawk ) ;
hawk_rbt_clear ( hawk - > modtab ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
HAWK_ASSERT ( HAWK_ARR_SIZE ( hawk - > parse . gbls ) = = hawk - > tree . ngbls ) ;
2019-12-13 04:29:58 +00:00
/* delete all non-builtin global variables */
hawk_arr_delete (
2020-04-16 03:42:30 +00:00
hawk - > parse . gbls , hawk - > tree . ngbls_base ,
HAWK_ARR_SIZE ( hawk - > parse . gbls ) - hawk - > tree . ngbls_base ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk_arr_clear ( hawk - > parse . lcls ) ;
hawk_arr_clear ( hawk - > parse . params ) ;
hawk_htb_clear ( hawk - > parse . named ) ;
hawk_htb_clear ( hawk - > parse . funs ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk - > parse . nlcls_max = 0 ;
hawk - > parse . depth . block = 0 ;
hawk - > parse . depth . loop = 0 ;
hawk - > parse . depth . expr = 0 ;
hawk - > parse . depth . incl = 0 ;
hawk - > parse . pragma . trait = ( hawk - > opt . trait & ( HAWK_IMPLICIT | HAWK_MULTILINESTR | HAWK_STRIPRECSPC | HAWK_STRIPSTRSPC ) ) ; /* implicit on if you didn't mask it off in hawk->opt.trait with hawk_setopt */
hawk - > parse . pragma . rtx_stack_limit = 0 ;
hawk - > parse . pragma . entry [ 0 ] = ' \0 ' ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk - > parse . incl_hist . count = 0 ;
2019-12-13 04:29:58 +00:00
/* clear parse trees */
2020-04-16 03:42:30 +00:00
/*hawk->tree.ngbls_base = 0;
hawk - > tree . ngbls = 0 ; */
hawk - > tree . ngbls = hawk - > tree . ngbls_base ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
hawk - > tree . cur_fun . ptr = HAWK_NULL ;
hawk - > tree . cur_fun . len = 0 ;
hawk_htb_clear ( hawk - > tree . funs ) ;
2019-12-13 04:29:58 +00:00
2020-04-16 03:42:30 +00:00
if ( hawk - > tree . begin )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
hawk_clrpt ( hawk , hawk - > tree . begin ) ;
hawk - > tree . begin = HAWK_NULL ;
hawk - > tree . begin_tail = HAWK_NULL ;
2019-12-13 04:29:58 +00:00
}
2020-04-16 03:42:30 +00:00
if ( hawk - > tree . end )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
hawk_clrpt ( hawk , hawk - > tree . end ) ;
hawk - > tree . end = HAWK_NULL ;
hawk - > tree . end_tail = HAWK_NULL ;
2019-12-13 04:29:58 +00:00
}
2020-04-16 03:42:30 +00:00
while ( hawk - > tree . chain )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
hawk_chain_t * next = hawk - > tree . chain - > next ;
if ( hawk - > tree . chain - > pattern ) hawk_clrpt ( hawk , hawk - > tree . chain - > pattern ) ;
if ( hawk - > tree . chain - > action ) hawk_clrpt ( hawk , hawk - > tree . chain - > action ) ;
hawk_freemem ( hawk , hawk - > tree . chain ) ;
hawk - > tree . chain = next ;
2019-12-13 04:29:58 +00:00
}
2020-04-16 03:42:30 +00:00
hawk - > tree . chain_tail = HAWK_NULL ;
hawk - > tree . chain_size = 0 ;
2019-12-13 04:29:58 +00:00
/* this table must not be cleared here as there can be a reference
* to an entry of this table from errinf . loc . file when hawk_parse ( )
* failed . this table is cleared in hawk_parse ( ) .
2020-04-16 03:42:30 +00:00
* hawk_claersionames ( hawk ) ;
2019-12-13 04:29:58 +00:00
*/
}
2020-04-15 15:12:28 +00:00
void hawk_getprm ( hawk_t * hawk , hawk_prm_t * prm )
2019-12-13 04:29:58 +00:00
{
2020-04-15 15:12:28 +00:00
* prm = hawk - > prm ;
2019-12-13 04:29:58 +00:00
}
2020-04-15 15:12:28 +00:00
void hawk_setprm ( hawk_t * hawk , const hawk_prm_t * prm )
2019-12-13 04:29:58 +00:00
{
2020-04-15 15:12:28 +00:00
hawk - > prm = * prm ;
2019-12-13 04:29:58 +00:00
}
2020-04-15 15:12:28 +00:00
static int dup_str_opt ( hawk_t * hawk , const void * value , hawk_oocs_t * tmp )
2019-12-13 04:29:58 +00:00
{
if ( value )
{
2020-04-15 15:12:28 +00:00
tmp - > ptr = hawk_dupoocstr ( hawk , value , & tmp - > len ) ;
if ( HAWK_UNLIKELY ( ! tmp - > ptr ) ) return - 1 ;
2019-12-13 04:29:58 +00:00
}
else
{
tmp - > ptr = HAWK_NULL ;
tmp - > len = 0 ;
}
return 0 ;
}
2019-12-14 16:05:10 +00:00
int hawk_setopt ( hawk_t * hawk , hawk_opt_t id , const void * value )
2019-12-13 04:29:58 +00:00
{
switch ( id )
{
2020-04-15 08:38:41 +00:00
case HAWK_OPT_TRAIT :
2019-12-14 16:05:10 +00:00
hawk - > opt . trait = * ( const int * ) value ;
2019-12-13 04:29:58 +00:00
return 0 ;
2020-04-15 15:12:28 +00:00
case HAWK_OPT_MODLIBDIRS :
2020-04-15 08:38:41 +00:00
case HAWK_OPT_MODPREFIX :
case HAWK_OPT_MODPOSTFIX :
2019-12-13 04:29:58 +00:00
{
hawk_oocs_t tmp ;
int idx ;
2019-12-14 16:05:10 +00:00
if ( dup_str_opt ( hawk , value , & tmp ) < = - 1 ) return - 1 ;
2019-12-13 04:29:58 +00:00
2020-04-15 15:12:28 +00:00
idx = id - HAWK_OPT_MODLIBDIRS ;
2019-12-14 16:05:10 +00:00
if ( hawk - > opt . mod [ idx ] . ptr ) hawk_freemem ( hawk , hawk - > opt . mod [ idx ] . ptr ) ;
2019-12-13 04:29:58 +00:00
2019-12-14 16:05:10 +00:00
hawk - > opt . mod [ idx ] = tmp ;
2019-12-13 04:29:58 +00:00
return 0 ;
}
2020-04-15 08:38:41 +00:00
case HAWK_OPT_INCLUDEDIRS :
2019-12-13 04:29:58 +00:00
{
hawk_oocs_t tmp ;
2019-12-14 16:05:10 +00:00
if ( dup_str_opt ( hawk , value , & tmp ) < = - 1 ) return - 1 ;
2020-04-15 15:12:28 +00:00
if ( hawk - > opt . includedirs . ptr ) hawk_freemem ( hawk , hawk - > opt . includedirs . ptr ) ;
hawk - > opt . includedirs = tmp ;
2019-12-13 04:29:58 +00:00
return 0 ;
}
2020-04-15 08:38:41 +00:00
case HAWK_OPT_DEPTH_INCLUDE :
case HAWK_OPT_DEPTH_BLOCK_PARSE :
case HAWK_OPT_DEPTH_BLOCK_RUN :
case HAWK_OPT_DEPTH_EXPR_PARSE :
case HAWK_OPT_DEPTH_EXPR_RUN :
case HAWK_OPT_DEPTH_REX_BUILD :
case HAWK_OPT_DEPTH_REX_MATCH :
hawk - > opt . depth . a [ id - HAWK_OPT_DEPTH_INCLUDE ] = * ( const hawk_oow_t * ) value ;
2019-12-13 04:29:58 +00:00
return 0 ;
2020-04-15 08:38:41 +00:00
case HAWK_OPT_RTX_STACK_LIMIT :
2019-12-14 16:05:10 +00:00
hawk - > opt . rtx_stack_limit = * ( const hawk_oow_t * ) value ;
if ( hawk - > opt . rtx_stack_limit < HAWK_MIN_RTX_STACK_LIMIT ) hawk - > opt . rtx_stack_limit = HAWK_MIN_RTX_STACK_LIMIT ;
else if ( hawk - > opt . rtx_stack_limit > HAWK_MAX_RTX_STACK_LIMIT ) hawk - > opt . rtx_stack_limit = HAWK_MAX_RTX_STACK_LIMIT ;
2019-12-13 04:29:58 +00:00
return 0 ;
2019-12-14 16:05:10 +00:00
2020-04-15 08:38:41 +00:00
case HAWK_OPT_LOG_MASK :
2019-12-14 16:05:10 +00:00
hawk - > opt . log_mask = * ( hawk_bitmask_t * ) value ;
return 0 ;
2020-04-15 08:38:41 +00:00
case HAWK_OPT_LOG_MAXCAPA :
2019-12-14 16:05:10 +00:00
hawk - > opt . log_maxcapa = * ( hawk_oow_t * ) value ;
return 0 ;
2019-12-13 04:29:58 +00:00
}
2020-01-01 04:46:49 +00:00
hawk_seterrnum ( hawk , HAWK_NULL , HAWK_EINVAL ) ;
2019-12-13 04:29:58 +00:00
return - 1 ;
}
2019-12-14 16:05:10 +00:00
int hawk_getopt ( hawk_t * hawk , hawk_opt_t id , void * value )
2019-12-13 04:29:58 +00:00
{
switch ( id )
{
2020-04-15 08:38:41 +00:00
case HAWK_OPT_TRAIT :
2019-12-14 16:05:10 +00:00
* ( int * ) value = hawk - > opt . trait ;
2019-12-13 04:29:58 +00:00
return 0 ;
2020-04-15 15:12:28 +00:00
case HAWK_OPT_MODLIBDIRS :
2020-04-15 08:38:41 +00:00
case HAWK_OPT_MODPREFIX :
case HAWK_OPT_MODPOSTFIX :
2020-04-15 15:12:28 +00:00
* ( const hawk_ooch_t * * ) value = hawk - > opt . mod [ id - HAWK_OPT_MODLIBDIRS ] . ptr ;
2019-12-13 04:29:58 +00:00
return 0 ;
2020-04-15 08:38:41 +00:00
case HAWK_OPT_INCLUDEDIRS :
2020-04-15 15:12:28 +00:00
* ( const hawk_ooch_t * * ) value = hawk - > opt . includedirs . ptr ;
2019-12-13 04:29:58 +00:00
return 0 ;
2020-04-15 08:38:41 +00:00
case HAWK_OPT_DEPTH_INCLUDE :
case HAWK_OPT_DEPTH_BLOCK_PARSE :
case HAWK_OPT_DEPTH_BLOCK_RUN :
case HAWK_OPT_DEPTH_EXPR_PARSE :
case HAWK_OPT_DEPTH_EXPR_RUN :
case HAWK_OPT_DEPTH_REX_BUILD :
case HAWK_OPT_DEPTH_REX_MATCH :
* ( hawk_oow_t * ) value = hawk - > opt . depth . a [ id - HAWK_OPT_DEPTH_INCLUDE ] ;
2019-12-13 04:29:58 +00:00
return 0 ;
2020-04-15 08:38:41 +00:00
case HAWK_OPT_RTX_STACK_LIMIT :
2019-12-14 16:05:10 +00:00
* ( hawk_oow_t * ) value = hawk - > opt . rtx_stack_limit ;
2019-12-13 04:29:58 +00:00
return 0 ;
2019-12-14 16:05:10 +00:00
2020-04-15 08:38:41 +00:00
case HAWK_OPT_LOG_MASK :
2019-12-14 16:05:10 +00:00
* ( hawk_bitmask_t * ) value = hawk - > opt . log_mask ;
return 0 ;
2020-04-15 08:38:41 +00:00
case HAWK_OPT_LOG_MAXCAPA :
2019-12-14 16:05:10 +00:00
* ( hawk_oow_t * ) value = hawk - > opt . log_maxcapa ;
return 0 ;
2019-12-13 04:29:58 +00:00
} ;
2020-01-01 04:46:49 +00:00
hawk_seterrnum ( hawk , HAWK_NULL , HAWK_EINVAL ) ;
2019-12-13 04:29:58 +00:00
return - 1 ;
}
2020-04-16 03:42:30 +00:00
void hawk_haltall ( hawk_t * hawk )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
hawk - > haltall = 1 ;
2019-12-13 04:29:58 +00:00
}
2020-04-16 03:42:30 +00:00
hawk_ecb_t * hawk_popecb ( hawk_t * hawk )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
hawk_ecb_t * top = hawk - > ecb ;
if ( top ) hawk - > ecb = top - > next ;
2019-12-13 04:29:58 +00:00
return top ;
}
2020-04-16 03:42:30 +00:00
void hawk_pushecb ( hawk_t * hawk , hawk_ecb_t * ecb )
2019-12-13 04:29:58 +00:00
{
2020-04-16 03:42:30 +00:00
ecb - > next = hawk - > ecb ;
hawk - > ecb = ecb ;
2019-12-13 04:29:58 +00:00
}
2019-12-13 15:24:57 +00:00
/* ------------------------------------------------------------------------ */
2019-12-13 04:29:58 +00:00
2019-12-13 15:24:57 +00:00
hawk_oow_t hawk_fmttoucstr_ ( hawk_t * hawk , hawk_uch_t * buf , hawk_oow_t bufsz , const hawk_uch_t * fmt , . . . )
2019-12-13 04:29:58 +00:00
{
va_list ap ;
hawk_oow_t n ;
2019-12-13 15:24:57 +00:00
va_start ( ap , fmt ) ;
n = hawk_gem_vfmttoucstr ( hawk_getgem ( hawk ) , buf , bufsz , fmt , ap ) ;
va_end ( ap ) ;
return n ;
2019-12-13 04:29:58 +00:00
}
2019-12-13 15:24:57 +00:00
hawk_oow_t hawk_fmttobcstr_ ( hawk_t * hawk , hawk_bch_t * buf , hawk_oow_t bufsz , const hawk_bch_t * fmt , . . . )
2019-12-13 04:29:58 +00:00
{
va_list ap ;
2019-12-13 15:24:57 +00:00
hawk_oow_t n ;
va_start ( ap , fmt ) ;
n = hawk_gem_vfmttobcstr ( hawk_getgem ( hawk ) , buf , bufsz , fmt , ap ) ;
va_end ( ap ) ;
return n ;
2019-12-13 04:29:58 +00:00
}
2020-01-01 14:02:27 +00:00
int hawk_buildrex ( hawk_t * hawk , const hawk_ooch_t * ptn , hawk_oow_t len , hawk_tre_t * * code , hawk_tre_t * * icode )
{
return hawk_gem_buildrex ( hawk_getgem ( hawk ) , ptn , len , ! ( hawk - > opt . trait & HAWK_REXBOUND ) , code , icode ) ;
}
2020-12-17 10:12:57 +00:00
/* ------------------------------------------------------------------------ */
2020-12-22 17:53:51 +00:00
int hawk_findmodsymfnc_noseterr ( hawk_t * hawk , hawk_mod_fnc_tab_t * fnctab , hawk_oow_t count , const hawk_ooch_t * name , hawk_mod_sym_t * sym )
2020-12-17 10:12:57 +00:00
{
int n ;
/* binary search */
hawk_oow_t base , mid , lim ;
for ( base = 0 , lim = count ; lim > 0 ; lim > > = 1 )
{
mid = base + ( lim > > 1 ) ;
2020-12-18 14:43:30 +00:00
n = hawk_comp_oocstr ( name , fnctab [ mid ] . name , 0 ) ;
2020-12-17 10:12:57 +00:00
if ( n = = 0 )
{
sym - > type = HAWK_MOD_FNC ;
2020-12-18 14:43:30 +00:00
sym - > u . fnc_ = fnctab [ mid ] . info ;
return 0 ;
}
if ( n > 0 ) { base = mid + 1 ; lim - - ; }
}
return - 1 ;
}
2020-12-22 17:53:51 +00:00
int hawk_findmodsymint_noseterr ( hawk_t * hawk , hawk_mod_int_tab_t * inttab , hawk_oow_t count , const hawk_ooch_t * name , hawk_mod_sym_t * sym )
2020-12-18 14:43:30 +00:00
{
int n ;
/* binary search */
hawk_oow_t base , mid , lim ;
for ( base = 0 , lim = count ; lim > 0 ; lim > > = 1 )
{
mid = base + ( lim > > 1 ) ;
n = hawk_comp_oocstr ( name , inttab [ mid ] . name , 0 ) ;
if ( n = = 0 )
{
sym - > type = HAWK_MOD_INT ;
sym - > u . int_ = inttab [ mid ] . info ;
return 0 ;
}
if ( n > 0 ) { base = mid + 1 ; lim - - ; }
}
return - 1 ;
}
2020-12-22 17:53:51 +00:00
int hawk_findmodsymflt_noseterr ( hawk_t * hawk , hawk_mod_flt_tab_t * flttab , hawk_oow_t count , const hawk_ooch_t * name , hawk_mod_sym_t * sym )
2020-12-18 14:43:30 +00:00
{
int n ;
/* binary search */
hawk_oow_t base , mid , lim ;
for ( base = 0 , lim = count ; lim > 0 ; lim > > = 1 )
{
mid = base + ( lim > > 1 ) ;
n = hawk_comp_oocstr ( name , flttab [ mid ] . name , 0 ) ;
if ( n = = 0 )
{
sym - > type = HAWK_MOD_FLT ;
sym - > u . flt_ = flttab [ mid ] . info ;
2020-12-17 10:12:57 +00:00
return 0 ;
}
if ( n > 0 ) { base = mid + 1 ; lim - - ; }
}
return - 1 ;
}
2020-12-18 14:43:30 +00:00
int hawk_findmodsymfnc ( hawk_t * hawk , hawk_mod_fnc_tab_t * fnctab , hawk_oow_t count , const hawk_ooch_t * name , hawk_mod_sym_t * sym )
{
2020-12-22 17:53:51 +00:00
int n = hawk_findmodsymfnc_noseterr ( hawk , fnctab , count , name , sym ) ;
2020-12-18 14:43:30 +00:00
if ( n < = - 1 ) hawk_seterrfmt ( hawk , HAWK_NULL , HAWK_ENOENT , HAWK_T ( " '%js' not found " ) , name ) ;
return n ;
}
int hawk_findmodsymint ( hawk_t * hawk , hawk_mod_int_tab_t * inttab , hawk_oow_t count , const hawk_ooch_t * name , hawk_mod_sym_t * sym )
{
2020-12-22 17:53:51 +00:00
int n = hawk_findmodsymint_noseterr ( hawk , inttab , count , name , sym ) ;
2020-12-18 14:43:30 +00:00
if ( n < = - 1 ) hawk_seterrfmt ( hawk , HAWK_NULL , HAWK_ENOENT , HAWK_T ( " '%js' not found " ) , name ) ;
return n ;
}
int hawk_findmodsymflt ( hawk_t * hawk , hawk_mod_flt_tab_t * flttab , hawk_oow_t count , const hawk_ooch_t * name , hawk_mod_sym_t * sym )
{
2020-12-22 17:53:51 +00:00
int n = hawk_findmodsymflt_noseterr ( hawk , flttab , count , name , sym ) ;
2020-12-18 14:43:30 +00:00
if ( n < = - 1 ) hawk_seterrfmt ( hawk , HAWK_NULL , HAWK_ENOENT , HAWK_T ( " '%js' not found " ) , name ) ;
return n ;
}