2005-11-06 12:01:29 +00:00
|
|
|
/*
|
2006-02-05 13:45:59 +00:00
|
|
|
* $Id: awk.c,v 1.18 2006-02-05 13:45:59 bacon Exp $
|
2005-11-06 12:01:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/awk/awk.h>
|
2006-01-18 15:16:01 +00:00
|
|
|
|
|
|
|
#ifndef __STAND_ALONE
|
2005-11-06 12:01:29 +00:00
|
|
|
#include <xp/bas/memory.h>
|
2005-11-07 16:02:44 +00:00
|
|
|
#include <xp/bas/assert.h>
|
2006-01-18 15:16:01 +00:00
|
|
|
#endif
|
2005-11-06 12:01:29 +00:00
|
|
|
|
2006-01-30 14:34:47 +00:00
|
|
|
static void __free_func (void* func);
|
|
|
|
|
2005-11-06 12:01:29 +00:00
|
|
|
xp_awk_t* xp_awk_open (xp_awk_t* awk)
|
|
|
|
{
|
|
|
|
if (awk == XP_NULL) {
|
|
|
|
awk = (xp_awk_t*) xp_malloc (xp_sizeof(awk));
|
|
|
|
if (awk == XP_NULL) return XP_NULL;
|
2005-12-05 15:11:29 +00:00
|
|
|
awk->__dynamic = xp_true;
|
2005-11-06 12:01:29 +00:00
|
|
|
}
|
2005-12-05 15:11:29 +00:00
|
|
|
else awk->__dynamic = xp_false;
|
2005-11-06 12:01:29 +00:00
|
|
|
|
2005-11-14 15:23:54 +00:00
|
|
|
if (xp_str_open(&awk->token.name, 128) == XP_NULL) {
|
2005-12-05 15:11:29 +00:00
|
|
|
if (awk->__dynamic) xp_free (awk);
|
2005-11-07 16:02:44 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-30 14:34:47 +00:00
|
|
|
if (xp_awk_hash_open(&awk->tree.funcs, 256, __free_func) == XP_NULL) {
|
|
|
|
xp_str_close (&awk->token.name);
|
|
|
|
if (awk->__dynamic) xp_free (awk);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-04 19:31:51 +00:00
|
|
|
if (xp_awk_tab_open(&awk->parse.globals) == XP_NULL) {
|
|
|
|
xp_str_close (&awk->token.name);
|
|
|
|
xp_awk_hash_close (&awk->tree.funcs);
|
|
|
|
if (awk->__dynamic) xp_free (awk);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xp_awk_tab_open(&awk->parse.locals) == XP_NULL) {
|
|
|
|
xp_str_close (&awk->token.name);
|
|
|
|
xp_awk_hash_close (&awk->tree.funcs);
|
|
|
|
xp_awk_tab_close (&awk->parse.globals);
|
|
|
|
if (awk->__dynamic) xp_free (awk);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-31 16:57:45 +00:00
|
|
|
if (xp_awk_tab_open(&awk->parse.params) == XP_NULL) {
|
2006-01-29 18:28:14 +00:00
|
|
|
xp_str_close (&awk->token.name);
|
2006-01-30 14:34:47 +00:00
|
|
|
xp_awk_hash_close (&awk->tree.funcs);
|
2006-02-04 19:31:51 +00:00
|
|
|
xp_awk_tab_close (&awk->parse.globals);
|
|
|
|
xp_awk_tab_close (&awk->parse.locals);
|
2006-01-29 18:28:14 +00:00
|
|
|
if (awk->__dynamic) xp_free (awk);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-31 16:57:45 +00:00
|
|
|
awk->opt.parse = 0;
|
|
|
|
awk->opt.run = 0;
|
2005-11-06 12:01:29 +00:00
|
|
|
awk->errnum = XP_AWK_ENOERR;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_func = XP_NULL;
|
2006-01-19 16:28:21 +00:00
|
|
|
awk->in_func = XP_NULL;
|
|
|
|
awk->out_func = XP_NULL;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_arg = XP_NULL;
|
2006-01-19 16:28:21 +00:00
|
|
|
awk->in_arg = XP_NULL;
|
|
|
|
awk->out_arg = XP_NULL;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2006-02-04 19:31:51 +00:00
|
|
|
awk->parse.nlocals_max = 0;
|
|
|
|
|
2006-01-22 15:11:17 +00:00
|
|
|
awk->tree.begin = XP_NULL;
|
|
|
|
awk->tree.end = XP_NULL;
|
2006-01-24 16:14:28 +00:00
|
|
|
awk->tree.unnamed = XP_NULL;
|
2006-01-29 18:28:14 +00:00
|
|
|
|
2005-11-07 16:02:44 +00:00
|
|
|
awk->lex.curc = XP_CHAR_EOF;
|
2005-11-14 15:23:54 +00:00
|
|
|
awk->lex.ungotc_count = 0;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-11-06 12:01:29 +00:00
|
|
|
return awk;
|
|
|
|
}
|
|
|
|
|
2006-01-09 16:03:56 +00:00
|
|
|
int xp_awk_close (xp_awk_t* awk)
|
|
|
|
{
|
2006-01-24 16:14:28 +00:00
|
|
|
xp_awk_clear (awk);
|
2005-12-29 12:04:51 +00:00
|
|
|
if (xp_awk_detsrc(awk) == -1) return -1;
|
2006-01-29 18:28:14 +00:00
|
|
|
|
2006-01-30 14:34:47 +00:00
|
|
|
xp_awk_hash_close (&awk->tree.funcs);
|
2006-02-04 19:31:51 +00:00
|
|
|
xp_awk_tab_close (&awk->parse.globals);
|
|
|
|
xp_awk_tab_close (&awk->parse.locals);
|
2006-01-31 16:57:45 +00:00
|
|
|
xp_awk_tab_close (&awk->parse.params);
|
2005-11-14 15:23:54 +00:00
|
|
|
xp_str_close (&awk->token.name);
|
2006-01-29 18:28:14 +00:00
|
|
|
|
2005-12-05 15:11:29 +00:00
|
|
|
if (awk->__dynamic) xp_free (awk);
|
2005-11-06 12:01:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2006-01-31 16:57:45 +00:00
|
|
|
int xp_awk_geterrnum (xp_awk_t* awk)
|
|
|
|
{
|
|
|
|
return awk->errnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
const xp_char_t* xp_awk_geterrstr (xp_awk_t* awk)
|
|
|
|
{
|
|
|
|
static const xp_char_t* __errstr[] =
|
|
|
|
{
|
|
|
|
XP_TEXT("no error"),
|
|
|
|
XP_TEXT("out of memory"),
|
|
|
|
|
|
|
|
XP_TEXT("cannot open source"),
|
|
|
|
XP_TEXT("cannot close source"),
|
|
|
|
XP_TEXT("cannot read source"),
|
|
|
|
|
|
|
|
XP_TEXT("invalid character"),
|
|
|
|
XP_TEXT("cannot unget character"),
|
|
|
|
|
|
|
|
XP_TEXT("unexpected end of source"),
|
|
|
|
XP_TEXT("left brace expected"),
|
|
|
|
XP_TEXT("left parenthesis expected"),
|
|
|
|
XP_TEXT("right parenthesis expected"),
|
|
|
|
XP_TEXT("right bracket expected"),
|
|
|
|
XP_TEXT("comma expected"),
|
|
|
|
XP_TEXT("semicolon expected"),
|
|
|
|
XP_TEXT("expression expected"),
|
|
|
|
|
|
|
|
XP_TEXT("keyword 'while' expected"),
|
|
|
|
XP_TEXT("assignment statement expected"),
|
|
|
|
XP_TEXT("identifier expected"),
|
|
|
|
XP_TEXT("duplicate BEGIN"),
|
|
|
|
XP_TEXT("duplicate END"),
|
|
|
|
XP_TEXT("duplicate function name"),
|
|
|
|
XP_TEXT("duplicate parameter name"),
|
2006-02-04 19:31:51 +00:00
|
|
|
XP_TEXT("duplicate variable name"),
|
2006-01-31 16:57:45 +00:00
|
|
|
XP_TEXT("duplicate name"),
|
2006-02-05 13:45:59 +00:00
|
|
|
XP_TEXT("undefined identifier")
|
2006-01-31 16:57:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (awk->errnum >= 0 && awk->errnum < xp_countof(__errstr)) {
|
|
|
|
return __errstr[awk->errnum];
|
|
|
|
}
|
|
|
|
|
|
|
|
return XP_TEXT("unknown error");
|
|
|
|
}
|
|
|
|
|
2006-01-29 18:28:14 +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-01-24 16:14:28 +00:00
|
|
|
void xp_awk_clear (xp_awk_t* awk)
|
|
|
|
{
|
2006-02-04 19:31:51 +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;
|
|
|
|
|
2006-01-29 18:28:14 +00:00
|
|
|
/* clear parse trees */
|
2006-01-30 14:34:47 +00:00
|
|
|
xp_awk_hash_clear (&awk->tree.funcs);
|
|
|
|
|
2006-01-24 16:14:28 +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.unnamed != XP_NULL) {
|
|
|
|
xp_awk_node_t* next = awk->tree.unnamed->next;
|
|
|
|
xp_awk_clrpt (awk->tree.unnamed);
|
|
|
|
awk->tree.unnamed = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: destroy pattern-actions pairs */
|
|
|
|
/* TODO: destroy function list */
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg)
|
2005-11-07 16:02:44 +00:00
|
|
|
{
|
2005-12-29 12:04:51 +00:00
|
|
|
if (xp_awk_detsrc(awk) == -1) return -1;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
xp_assert (awk->src_func == XP_NULL);
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2006-01-09 16:03:56 +00:00
|
|
|
if (src(XP_AWK_IO_OPEN, arg, XP_NULL, 0) == -1) {
|
2005-11-07 16:02:44 +00:00
|
|
|
awk->errnum = XP_AWK_ESRCOP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_func = src;
|
|
|
|
awk->src_arg = arg;
|
2005-11-14 15:23:54 +00:00
|
|
|
awk->lex.curc = XP_CHAR_EOF;
|
|
|
|
awk->lex.ungotc_count = 0;
|
2005-11-07 16:02:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
int xp_awk_detsrc (xp_awk_t* awk)
|
2005-11-07 16:02:44 +00:00
|
|
|
{
|
2005-12-29 12:04:51 +00:00
|
|
|
if (awk->src_func != XP_NULL) {
|
|
|
|
if (awk->src_func(XP_AWK_IO_CLOSE, awk->src_arg, XP_NULL, 0) == -1) {
|
2005-11-07 16:02:44 +00:00
|
|
|
awk->errnum = XP_AWK_ESRCCL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_func = XP_NULL;
|
|
|
|
awk->src_arg = XP_NULL;
|
2005-11-14 15:23:54 +00:00
|
|
|
awk->lex.curc = XP_CHAR_EOF;
|
|
|
|
awk->lex.ungotc_count = 0;
|
2005-11-07 16:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-24 16:14:28 +00:00
|
|
|
|
2006-01-30 14:34:47 +00:00
|
|
|
static void __free_func (void* func)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|