*** empty log message ***

This commit is contained in:
hyung-hwan 2006-01-24 16:14:28 +00:00
parent f25b760df8
commit f77ca39b83
5 changed files with 102 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c,v 1.10 2006-01-22 15:11:17 bacon Exp $ * $Id: awk.c,v 1.11 2006-01-24 16:14:28 bacon Exp $
*/ */
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -36,6 +36,7 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
awk->tree.begin = XP_NULL; awk->tree.begin = XP_NULL;
awk->tree.end = XP_NULL; awk->tree.end = XP_NULL;
awk->tree.unnamed = XP_NULL;
//awk->tree.funcs = XP_NULL; //awk->tree.funcs = XP_NULL;
awk->lex.curc = XP_CHAR_EOF; awk->lex.curc = XP_CHAR_EOF;
@ -46,20 +47,38 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
int xp_awk_close (xp_awk_t* awk) int xp_awk_close (xp_awk_t* awk)
{ {
xp_awk_clear (awk);
if (awk->tree.begin != XP_NULL) xp_awk_clrpt (awk->tree.begin);
if (awk->tree.end != XP_NULL) xp_awk_clrpt (awk->tree.end);
/*
// TODO: destroy function list
if (awk->tree.funcs != XP_NULL)
*/
if (xp_awk_detsrc(awk) == -1) return -1; if (xp_awk_detsrc(awk) == -1) return -1;
xp_str_close (&awk->token.name); xp_str_close (&awk->token.name);
if (awk->__dynamic) xp_free (awk); if (awk->__dynamic) xp_free (awk);
return 0; return 0;
} }
void xp_awk_clear (xp_awk_t* awk)
{
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 */
}
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg) int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg)
{ {
if (xp_awk_detsrc(awk) == -1) return -1; if (xp_awk_detsrc(awk) == -1) return -1;
@ -94,3 +113,4 @@ int xp_awk_detsrc (xp_awk_t* awk)
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h,v 1.17 2006-01-22 15:11:17 bacon Exp $ * $Id: awk.h,v 1.18 2006-01-24 16:14:28 bacon Exp $
*/ */
#ifndef _XP_AWK_AWK_H_ #ifndef _XP_AWK_AWK_H_
@ -88,6 +88,7 @@ struct xp_awk_t
//xp_awk_hash_t* funcs; //xp_awk_hash_t* funcs;
xp_awk_node_t* begin; xp_awk_node_t* begin;
xp_awk_node_t* end; xp_awk_node_t* end;
xp_awk_node_t* unnamed;
} tree; } tree;
/* source buffer management */ /* source buffer management */
@ -124,6 +125,11 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk);
*/ */
int xp_awk_close (xp_awk_t* awk); int xp_awk_close (xp_awk_t* awk);
/*
* FUNCTION: xp_awk_clear
*/
void xp_awk_clear (xp_awk_t* awk);
/* /*
* FUNCTION: xp_awk_attsrc * FUNCTION: xp_awk_attsrc
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c,v 1.29 2006-01-22 15:11:17 bacon Exp $ * $Id: parse.c,v 1.30 2006-01-24 16:14:28 bacon Exp $
*/ */
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -106,6 +106,8 @@ static int __skip_spaces (xp_awk_t* awk);
static int __skip_comment (xp_awk_t* awk); static int __skip_comment (xp_awk_t* awk);
static int __classfy_ident (const xp_char_t* ident); static int __classfy_ident (const xp_char_t* ident);
static int __find_func_arg (xp_awk_t* awk, const xp_char_t* name);
struct __kwent struct __kwent
{ {
const xp_char_t* name; const xp_char_t* name;
@ -165,6 +167,8 @@ static struct __kwent __kwtab[] =
int xp_awk_parse (xp_awk_t* awk) int xp_awk_parse (xp_awk_t* awk)
{ {
/* if you want to parse anew, call xp_awk_clear first.
* otherwise, the result is appened to the accumulated result */
GET_CHAR (awk); GET_CHAR (awk);
GET_TOKEN (awk); GET_TOKEN (awk);
@ -245,7 +249,7 @@ static xp_bool_t __function_defined (xp_awk_t* awk, const xp_char_t* name)
static xp_awk_node_t* __parse_function (xp_awk_t* awk) static xp_awk_node_t* __parse_function (xp_awk_t* awk)
{ {
xp_char_t* name; xp_char_t* name;
xp_awk_func_t* func; //xp_awk_func_t* func;
xp_awk_node_t* body; xp_awk_node_t* body;
if (__get_token(awk) == -1) return XP_NULL; if (__get_token(awk) == -1) return XP_NULL;
@ -335,6 +339,7 @@ static xp_awk_node_t* __parse_function (xp_awk_t* awk)
return XP_NULL; return XP_NULL;
} }
/*
func = (xp_awk_func_t*) xp_malloc (xp_sizeof(xp_awk_func_t)); func = (xp_awk_func_t*) xp_malloc (xp_sizeof(xp_awk_func_t));
if (func == XP_NULL) { if (func == XP_NULL) {
xp_free (name); xp_free (name);
@ -345,6 +350,7 @@ static xp_awk_node_t* __parse_function (xp_awk_t* awk)
func->name = name; func->name = name;
func->nargs = 0; func->nargs = 0;
func->body = body; func->body = body;
*/
/* TODO: weave the function body into awk->tree.funcs */ /* TODO: weave the function body into awk->tree.funcs */
return body; return body;
@ -564,7 +570,7 @@ static xp_awk_node_t* __parse_expression (xp_awk_t* awk)
if (!MATCH(awk,TOKEN_ASSIGN)) return x; if (!MATCH(awk,TOKEN_ASSIGN)) return x;
xp_assert (x->next == XP_NULL); xp_assert (x->next == XP_NULL);
if (x->type != XP_AWK_NODE_VAR && x->type != XP_AWK_NODE_IDX) { if (x->type != XP_AWK_NODE_VAR && x->type != XP_AWK_NODE_VARIDX) {
xp_awk_clrpt (x); xp_awk_clrpt (x);
PANIC (awk, XP_AWK_EASSIGN); PANIC (awk, XP_AWK_EASSIGN);
} }
@ -819,17 +825,26 @@ static xp_awk_node_t* __parse_primary (xp_awk_t* awk, xp_char_t* ident)
} }
else { else {
/* normal variable */ /* normal variable */
xp_awk_node_term_t* node; xp_awk_node_var_t* node;
xp_size_t idxa;
node = (xp_awk_node_term_t*)xp_malloc(xp_sizeof(xp_awk_node_term_t)); node = (xp_awk_node_var_t*)xp_malloc(xp_sizeof(xp_awk_node_var_t));
if (node == XP_NULL) { if (node == XP_NULL) {
xp_free (name); xp_free (name);
PANIC (awk, XP_AWK_ENOMEM); PANIC (awk, XP_AWK_ENOMEM);
} }
node->type = XP_AWK_NODE_VAR; idxa = __find_func_arg (awk, name);
node->next = XP_NULL; if (idxa == (xp_size_t)-1) {
node->value = name; node->type = XP_AWK_NODE_VAR;
node->next = XP_NULL;
node->id.name = name;
}
else {
node->type = XP_AWK_NODE_ARG;
node->next = XP_NULL;
node->id.idxa = idxa;
}
return (xp_awk_node_t*)node; return (xp_awk_node_t*)node;
} }
@ -932,9 +947,9 @@ static xp_awk_node_t* __parse_hashidx (xp_awk_t* awk, xp_char_t* name)
PANIC (awk, XP_AWK_ENOMEM); PANIC (awk, XP_AWK_ENOMEM);
} }
node->type = XP_AWK_NODE_IDX; node->type = XP_AWK_NODE_VARIDX;
node->next = XP_NULL; node->next = XP_NULL;
node->name = name; node->id.name = name;
node->idx = idx; node->idx = idx;
return (xp_awk_node_t*)node; return (xp_awk_node_t*)node;
@ -1607,3 +1622,13 @@ static int __classfy_ident (const xp_char_t* ident)
return TOKEN_IDENT; return TOKEN_IDENT;
} }
static xp_size_t __find_func_arg (xp_awk_t* awk, const xp_char_t* name)
{
if (awk->curfunc != XP_NULL) {
// TODO: finish this....
}
return (xp_size_t)-1;
}

View File

@ -1,5 +1,5 @@
/* /*
* $Id: tree.c,v 1.9 2006-01-20 07:29:54 bacon Exp $ * $Id: tree.c,v 1.10 2006-01-24 16:14:28 bacon Exp $
*/ */
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -66,11 +66,11 @@ static int __print_expr_node (xp_awk_node_t* node)
break; break;
case XP_AWK_NODE_VAR: case XP_AWK_NODE_VAR:
xp_printf (XP_TEXT("%s"), ((xp_awk_node_term_t*)node)->value); xp_printf (XP_TEXT("%s"), ((xp_awk_node_var_t*)node)->id.name);
break; break;
case XP_AWK_NODE_IDX: case XP_AWK_NODE_VARIDX:
xp_printf (XP_TEXT("%s["), ((xp_awk_node_idx_t*)node)->name); xp_printf (XP_TEXT("%s["), ((xp_awk_node_idx_t*)node)->id.name);
__print_expr_node (((xp_awk_node_idx_t*)node)->idx); __print_expr_node (((xp_awk_node_idx_t*)node)->idx);
xp_printf (XP_TEXT("]")); xp_printf (XP_TEXT("]"));
break; break;
@ -339,14 +339,18 @@ void xp_awk_clrpt (xp_awk_node_t* tree)
case XP_AWK_NODE_STR: case XP_AWK_NODE_STR:
case XP_AWK_NODE_NUM: case XP_AWK_NODE_NUM:
case XP_AWK_NODE_VAR:
xp_free (((xp_awk_node_term_t*)p)->value); xp_free (((xp_awk_node_term_t*)p)->value);
xp_free (p); xp_free (p);
break; break;
case XP_AWK_NODE_IDX: case XP_AWK_NODE_VAR:
xp_free (((xp_awk_node_var_t*)p)->id.name);
xp_free (p);
break;
case XP_AWK_NODE_VARIDX:
xp_awk_clrpt (((xp_awk_node_idx_t*)p)->idx); xp_awk_clrpt (((xp_awk_node_idx_t*)p)->idx);
xp_free (((xp_awk_node_idx_t*)p)->name); xp_free (((xp_awk_node_idx_t*)p)->id.name);
xp_free (p); xp_free (p);
break; break;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: tree.h,v 1.15 2006-01-22 15:11:17 bacon Exp $ * $Id: tree.h,v 1.16 2006-01-24 16:14:28 bacon Exp $
*/ */
#ifndef _XP_AWK_TREE_H_ #ifndef _XP_AWK_TREE_H_
@ -21,7 +21,9 @@ enum
XP_AWK_NODE_STR, XP_AWK_NODE_STR,
XP_AWK_NODE_NUM, XP_AWK_NODE_NUM,
XP_AWK_NODE_VAR, XP_AWK_NODE_VAR,
XP_AWK_NODE_IDX, XP_AWK_NODE_VARIDX,
XP_AWK_NODE_ARG,
XP_AWK_NODE_ARGIDX,
XP_AWK_NODE_CALL, XP_AWK_NODE_CALL,
XP_AWK_NODE_IF, XP_AWK_NODE_IF,
XP_AWK_NODE_WHILE, XP_AWK_NODE_WHILE,
@ -34,6 +36,7 @@ typedef struct xp_awk_node_block_t xp_awk_node_block_t;
typedef struct xp_awk_node_assign_t xp_awk_node_assign_t; typedef struct xp_awk_node_assign_t xp_awk_node_assign_t;
typedef struct xp_awk_node_expr_t xp_awk_node_expr_t; typedef struct xp_awk_node_expr_t xp_awk_node_expr_t;
typedef struct xp_awk_node_term_t xp_awk_node_term_t; typedef struct xp_awk_node_term_t xp_awk_node_term_t;
typedef struct xp_awk_node_var_t xp_awk_node_var_t;
typedef struct xp_awk_node_idx_t xp_awk_node_idx_t; typedef struct xp_awk_node_idx_t xp_awk_node_idx_t;
typedef struct xp_awk_node_call_t xp_awk_node_call_t; typedef struct xp_awk_node_call_t xp_awk_node_call_t;
typedef struct xp_awk_node_if_t xp_awk_node_if_t; typedef struct xp_awk_node_if_t xp_awk_node_if_t;
@ -84,10 +87,24 @@ struct xp_awk_node_term_t
xp_char_t* value; xp_char_t* value;
}; };
struct xp_awk_node_var_t
{
XP_AWK_NODE_HDR;
union
{
xp_char_t* name;
xp_size_t idxa;
} id;
};
struct xp_awk_node_idx_t struct xp_awk_node_idx_t
{ {
XP_AWK_NODE_HDR; XP_AWK_NODE_HDR;
xp_char_t* name; union
{
xp_char_t* name;
xp_size_t idxa;
} id;
xp_awk_node_t* idx; xp_awk_node_t* idx;
}; };