qse/ase/awk/awk.c

241 lines
5.4 KiB
C
Raw Normal View History

2006-03-02 15:36:30 +00:00
/*
2006-04-20 16:17:01 +00:00
* $Id: awk.c,v 1.42 2006-04-20 16:17:01 bacon Exp $
2006-03-02 15:36:30 +00:00
*/
2006-02-23 14:42:33 +00:00
2006-03-31 16:35:37 +00:00
#include <xp/awk/awk_i.h>
2006-02-23 14:42:33 +00:00
2006-04-16 04:31:38 +00:00
#ifndef XP_AWK_STAND_ALONE
2006-02-23 14:42:33 +00:00
#include <xp/bas/memory.h>
#include <xp/bas/assert.h>
#endif
2006-03-27 14:14:00 +00:00
static void __free_func (xp_awk_t* awk, void* func);
2006-03-29 16:39:04 +00:00
static void __free_namedval (xp_awk_t* awk, void* val);
2006-02-23 14:42:33 +00:00
2006-03-31 16:35:37 +00:00
xp_awk_t* xp_awk_open (void)
2006-02-23 14:42:33 +00:00
{
2006-03-31 16:35:37 +00:00
xp_awk_t* awk;
2006-03-31 17:16:23 +00:00
awk = (xp_awk_t*) xp_malloc (xp_sizeof(xp_awk_t));
2006-03-31 16:35:37 +00:00
if (awk == XP_NULL) return XP_NULL;
2006-02-23 14:42:33 +00:00
if (xp_str_open(&awk->token.name, 128) == XP_NULL) {
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
2006-03-27 14:14:00 +00:00
if (xp_awk_map_open (&awk->tree.funcs, awk, 256, __free_func) == XP_NULL) {
2006-02-23 14:42:33 +00:00
xp_str_close (&awk->token.name);
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
if (xp_awk_tab_open(&awk->parse.globals) == XP_NULL) {
xp_str_close (&awk->token.name);
2006-03-02 15:10:59 +00:00
xp_awk_map_close (&awk->tree.funcs);
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
if (xp_awk_tab_open(&awk->parse.locals) == XP_NULL) {
xp_str_close (&awk->token.name);
2006-03-02 15:10:59 +00:00
xp_awk_map_close (&awk->tree.funcs);
2006-02-23 14:42:33 +00:00
xp_awk_tab_close (&awk->parse.globals);
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
if (xp_awk_tab_open(&awk->parse.params) == XP_NULL) {
xp_str_close (&awk->token.name);
2006-03-02 15:10:59 +00:00
xp_awk_map_close (&awk->tree.funcs);
2006-02-23 14:42:33 +00:00
xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals);
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
2006-04-14 10:56:42 +00:00
/* TODO: initial map size... */
2006-03-29 16:39:04 +00:00
if (xp_awk_map_open(&awk->run.named,awk,256,__free_namedval) == XP_NULL) {
2006-02-23 15:37:34 +00:00
xp_str_close (&awk->token.name);
2006-03-02 15:10:59 +00:00
xp_awk_map_close (&awk->tree.funcs);
2006-02-23 15:37:34 +00:00
xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals);
xp_awk_tab_close (&awk->parse.params);
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 15:37:34 +00:00
return XP_NULL;
}
2006-02-23 14:42:33 +00:00
awk->opt.parse = 0;
awk->opt.run = 0;
awk->errnum = XP_AWK_ENOERR;
awk->src_func = XP_NULL;
awk->in_func = XP_NULL;
awk->out_func = XP_NULL;
awk->src_arg = XP_NULL;
awk->in_arg = XP_NULL;
awk->out_arg = XP_NULL;
2006-03-07 15:55:14 +00:00
2006-02-23 14:42:33 +00:00
awk->parse.nlocals_max = 0;
2006-03-07 15:55:14 +00:00
2006-02-23 14:42:33 +00:00
awk->tree.nglobals = 0;
awk->tree.begin = XP_NULL;
awk->tree.end = XP_NULL;
awk->tree.chain = XP_NULL;
awk->tree.chain_tail = XP_NULL;
2006-03-07 15:55:14 +00:00
awk->run.stack = XP_NULL;
awk->run.stack_top = 0;
2006-03-24 06:33:36 +00:00
awk->run.stack_base = 0;
2006-03-07 15:55:14 +00:00
awk->run.stack_limit = 0;
2006-03-26 16:36:30 +00:00
awk->run.exit_level = 0;
2006-03-27 11:43:17 +00:00
awk->run.icache_count = 0;
2006-03-28 16:33:09 +00:00
awk->run.rcache_count = 0;
2006-03-07 15:55:14 +00:00
2006-02-23 14:42:33 +00:00
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
return awk;
}
int xp_awk_close (xp_awk_t* awk)
{
xp_awk_clear (awk);
2006-03-05 17:07:33 +00:00
2006-02-23 14:42:33 +00:00
if (xp_awk_detsrc(awk) == -1) return -1;
2006-03-05 17:07:33 +00:00
xp_awk_map_close (&awk->run.named);
2006-03-02 15:10:59 +00:00
xp_awk_map_close (&awk->tree.funcs);
2006-02-23 14:42:33 +00:00
xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals);
xp_awk_tab_close (&awk->parse.params);
xp_str_close (&awk->token.name);
2006-03-05 17:07:33 +00:00
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return 0;
}
2006-04-14 10:56:42 +00:00
/* TODO: write a function to clear awk->parse data structure.
this would be need either as a separate function or as a part of xp_awk_clear...
do i have to pass an option to xp_awk_clear to do this??? */
2006-02-23 14:42:33 +00:00
void xp_awk_clear (xp_awk_t* awk)
{
2006-04-20 16:17:01 +00:00
/* clear named variables */
2006-03-05 17:07:33 +00:00
xp_awk_map_clear (&awk->run.named);
2006-04-20 16:17:01 +00:00
/* destroy run stack */
if (awk->run.stack != XP_NULL)
{
xp_free (awk->run.stack);
awk->run.stack = XP_NULL;
awk->run.stack_top = 0;
awk->run.stack_base = 0;
awk->run.stack_limit = 0;
}
/* destroy values in free list */
while (awk->run.icache_count > 0)
{
--awk->run.icache_count;
xp_awk_freeval (awk,
awk->run.icache[awk->run.icache_count], xp_false);
}
while (awk->run.rcache_count > 0)
{
--awk->run.rcache_count;
xp_awk_freeval (awk,
awk->run.rcache[awk->run.rcache_count], xp_false);
}
2006-02-23 14:42:33 +00:00
xp_awk_tab_clear (&awk->parse.globals);
xp_awk_tab_clear (&awk->parse.locals);
xp_awk_tab_clear (&awk->parse.params);
awk->parse.nlocals_max = 0;
/* clear parse trees */
awk->tree.nglobals = 0;
2006-03-02 15:10:59 +00:00
xp_awk_map_clear (&awk->tree.funcs);
2006-03-05 17:07:33 +00:00
2006-02-23 14:42:33 +00:00
if (awk->tree.begin != XP_NULL) {
xp_assert (awk->tree.begin->next == XP_NULL);
xp_awk_clrpt (awk->tree.begin);
awk->tree.begin = XP_NULL;
}
if (awk->tree.end != XP_NULL) {
xp_assert (awk->tree.end->next == XP_NULL);
xp_awk_clrpt (awk->tree.end);
awk->tree.end = XP_NULL;
}
while (awk->tree.chain != XP_NULL) {
xp_awk_chain_t* next = awk->tree.chain->next;
if (awk->tree.chain->pattern != XP_NULL)
xp_awk_clrpt (awk->tree.chain->pattern);
if (awk->tree.chain->action != XP_NULL)
xp_awk_clrpt (awk->tree.chain->action);
xp_free (awk->tree.chain);
awk->tree.chain = next;
}
awk->tree.chain_tail = XP_NULL;
/* TODO: destroy function list */
}
2006-03-31 16:35:37 +00:00
void xp_awk_setparseopt (xp_awk_t* awk, int opt)
{
awk->opt.parse = opt;
}
2006-04-09 15:31:13 +00:00
void xp_awk_setrunopt (xp_awk_t* awk, int opt)
{
awk->opt.run = opt;
}
2006-02-23 14:42:33 +00:00
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg)
{
if (xp_awk_detsrc(awk) == -1) return -1;
xp_assert (awk->src_func == XP_NULL);
if (src(XP_AWK_IO_OPEN, arg, XP_NULL, 0) == -1) {
awk->errnum = XP_AWK_ESRCOP;
return -1;
}
awk->src_func = src;
awk->src_arg = arg;
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
return 0;
}
int xp_awk_detsrc (xp_awk_t* awk)
{
if (awk->src_func != XP_NULL) {
if (awk->src_func(XP_AWK_IO_CLOSE, awk->src_arg, XP_NULL, 0) == -1) {
awk->errnum = XP_AWK_ESRCCL;
return -1;
}
awk->src_func = XP_NULL;
awk->src_arg = XP_NULL;
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
}
return 0;
}
2006-03-27 14:14:00 +00:00
static void __free_func (xp_awk_t* awk, void* func)
2006-02-23 14:42:33 +00:00
{
xp_awk_func_t* f = (xp_awk_func_t*) func;
/* f->name doesn't have to be freed */
/*xp_free (f->name);*/
xp_awk_clrpt (f->body);
xp_free (f);
}
2006-03-29 16:39:04 +00:00
static void __free_namedval (xp_awk_t* awk, void* val)
{
xp_awk_refdownval (awk, val);
}