*** empty log message ***

This commit is contained in:
hyung-hwan 2006-03-07 15:55:14 +00:00
parent 7c6f19d0bf
commit 7e22a65fd0
10 changed files with 223 additions and 121 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c,v 1.27 2006-03-05 17:07:32 bacon Exp $
* $Id: awk.c,v 1.28 2006-03-07 15:55:14 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -75,12 +75,19 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
awk->src_arg = XP_NULL;
awk->in_arg = XP_NULL;
awk->out_arg = XP_NULL;
awk->parse.nlocals_max = 0;
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;
awk->run.stack = XP_NULL;
awk->run.stack_top = 0;
awk->run.stack_limit = 0;
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h,v 1.32 2006-03-04 10:06:49 bacon Exp $
* $Id: awk.h,v 1.33 2006-03-07 15:55:14 bacon Exp $
*/
#ifndef _XP_AWK_AWK_H_
@ -18,6 +18,7 @@
#include <xp/awk/tab.h>
#include <xp/awk/map.h>
#include <xp/awk/val.h>
#include <xp/awk/run.h>
/*
* TYPE: xp_awk_t
@ -91,6 +92,10 @@ struct xp_awk_t
struct
{
xp_awk_map_t named;
void* stack;
xp_size_t stack_top;
xp_size_t stack_limit;
} run;
/* source buffer management */

View File

@ -1,5 +1,5 @@
/*
* $Id: map.c,v 1.5 2006-03-06 04:04:47 bacon Exp $
* $Id: map.c,v 1.6 2006-03-07 15:55:14 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -16,7 +16,8 @@
static xp_size_t __hash (const xp_char_t* key);
#define FREE_PAIR(map,pair) \
do { \
do \
{ \
xp_free ((xp_char_t*)(pair)->key); \
if ((map)->freeval != XP_NULL) \
(map)->freeval ((pair)->val); \
@ -26,7 +27,8 @@ static xp_size_t __hash (const xp_char_t* key);
xp_awk_map_t* xp_awk_map_open (
xp_awk_map_t* map, xp_size_t capa, void (*freeval) (void*))
{
if (map == XP_NULL) {
if (map == XP_NULL)
{
map = (xp_awk_map_t*) xp_malloc (xp_sizeof(xp_awk_map_t));
if (map == XP_NULL) return XP_NULL;
map->__dynamic = xp_true;
@ -35,7 +37,8 @@ xp_awk_map_t* xp_awk_map_open (
map->buck = (xp_awk_pair_t**)
xp_malloc (xp_sizeof(xp_awk_pair_t*) * capa);
if (map->buck == XP_NULL) {
if (map->buck == XP_NULL)
{
if (map->__dynamic) xp_free (map);
return XP_NULL;
}
@ -60,10 +63,12 @@ void xp_awk_map_clear (xp_awk_map_t* map)
xp_size_t i;
xp_awk_pair_t* pair, * next;
for (i = 0; i < map->capa; i++) {
for (i = 0; i < map->capa; i++)
{
pair = map->buck[i];
while (pair != XP_NULL) {
while (pair != XP_NULL)
{
next = pair->next;
FREE_PAIR (map, pair);
@ -86,7 +91,8 @@ xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, xp_char_t* key)
hc = __hash(key) % map->capa;
pair = map->buck[hc];
while (pair != XP_NULL) {
while (pair != XP_NULL)
{
if (xp_strcmp(pair->key,key) == 0) return pair;
pair = pair->next;
}
@ -94,7 +100,7 @@ xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, xp_char_t* key)
return XP_NULL;
}
xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* value)
xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* val)
{
xp_awk_pair_t* pair;
xp_size_t hc;
@ -102,15 +108,17 @@ xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* value)
hc = __hash(key) % map->capa;
pair = map->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (pair->key != key) {
while (pair != XP_NULL)
{
if (xp_strcmp(pair->key,key) == 0)
{
if (pair->key != key)
{
xp_free ((xp_char_t*)pair->key);
pair->key = key;
}
return xp_awk_map_setpair (map, pair, value);
return xp_awk_map_setpair (map, pair, val);
}
pair = pair->next;
}
@ -119,7 +127,7 @@ xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* value)
if (pair == XP_NULL) return XP_NULL;
pair->key = key;
pair->val = value;
pair->val = val;
pair->next = map->buck[hc];
map->buck[hc] = pair;
map->size++;
@ -127,7 +135,7 @@ xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* value)
return pair;
}
xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* value)
xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* val)
{
xp_awk_pair_t* pair;
xp_size_t hc;
@ -135,14 +143,17 @@ xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* value)
hc = __hash(key) % map->capa;
pair = map->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (pair->key != key) {
while (pair != XP_NULL)
{
if (xp_strcmp(pair->key,key) == 0)
{
if (pair->key != key)
{
xp_free ((xp_char_t*)pair->key);
pair->key = key;
}
return xp_awk_map_setpair (map, pair, value);
return xp_awk_map_setpair (map, pair, val);
}
pair = pair->next;
}
@ -151,25 +162,27 @@ xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* value)
}
xp_awk_pair_t* xp_awk_map_getpair (
xp_awk_map_t* map, xp_char_t* key, const void** value)
xp_awk_map_t* map, xp_char_t* key, const void** val)
{
xp_awk_pair_t* pair;
pair = xp_awk_map_get (map, key);
if (pair == XP_NULL) return XP_NULL;
*value = pair->val;
*val = pair->val;
return pair;
}
xp_awk_pair_t* xp_awk_map_setpair (
xp_awk_map_t* map, xp_awk_pair_t* pair, void* value)
xp_awk_map_t* map, xp_awk_pair_t* pair, void* val)
{
/* use this function with care */
if (pair->val != value) {
if (map->freeval != XP_NULL) {
if (pair->val != val)
{
if (map->freeval != XP_NULL)
{
map->freeval (pair->val);
}
pair->val = value;
pair->val = val;
}
return pair;
@ -184,8 +197,10 @@ int xp_awk_map_remove (xp_awk_map_t* map, xp_char_t* key)
pair = map->buck[hc];
prev = XP_NULL;
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
while (pair != XP_NULL)
{
if (xp_strcmp(pair->key,key) == 0)
{
if (prev == XP_NULL)
map->buck[hc] = pair->next;
else prev->next = pair->next;
@ -208,10 +223,12 @@ int xp_awk_map_walk (xp_awk_map_t* map, int (*walker) (xp_awk_pair_t*))
xp_size_t i;
xp_awk_pair_t* pair, * next;
for (i = 0; i < map->capa; i++) {
for (i = 0; i < map->capa; i++)
{
pair = map->buck[i];
while (pair != XP_NULL) {
while (pair != XP_NULL)
{
next = pair->next;
if (walker(pair) == -1) return -1;
pair = next;
@ -225,7 +242,8 @@ static xp_size_t __hash (const xp_char_t* key)
{
xp_size_t n = 0, i;
while (*key != XP_CHAR('\0')) {
while (*key != XP_CHAR('\0'))
{
xp_byte_t* bp = (xp_byte_t*)key;
for (i = 0; i < xp_sizeof(*key); i++) n = n * 31 + *bp++;
key++;

View File

@ -1,5 +1,5 @@
/*
* $Id: map.h,v 1.4 2006-03-06 04:04:47 bacon Exp $
* $Id: map.h,v 1.5 2006-03-07 15:55:14 bacon Exp $
*/
#ifndef _XP_AWK_MAP_H_
@ -50,9 +50,9 @@ xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* val);
xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* val);
xp_awk_pair_t* xp_awk_map_getpair (
xp_awk_map_t* map, xp_char_t* key, void** value);
xp_awk_map_t* map, xp_char_t* key, void** val);
xp_awk_pair_t* xp_awk_map_setpair (
xp_awk_map_t* map, xp_awk_pair_t* pair, void* value);
xp_awk_map_t* map, xp_awk_pair_t* pair, void* val);
int xp_awk_map_remove (xp_awk_map_t* map, xp_char_t* key);
int xp_awk_map_walk (xp_awk_map_t* map, int (*walker) (xp_awk_pair_t*));

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c,v 1.58 2006-03-05 17:07:32 bacon Exp $
* $Id: parse.c,v 1.59 2006-03-07 15:55:14 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -73,22 +73,6 @@ enum
__TOKEN_COUNT__
};
enum {
BINOP_PLUS,
BINOP_MINUS,
BINOP_MUL,
BINOP_DIV,
BINOP_MOD,
BINOP_RSHIFT,
BINOP_LSHIFT,
BINOP_EQ,
BINOP_NE,
BINOP_GT,
BINOP_GE,
BINOP_LT,
BINOP_LE
};
#if defined(__BORLANDC__) || defined(_MSC_VER)
#define INLINE
#else
@ -311,7 +295,7 @@ static xp_awk_t* __parse_progunit (xp_awk_t* awk)
nglobals = xp_awk_tab_getsize(&awk->parse.globals);
if (__collect_globals(awk) == XP_NULL)
{
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.globals, nglobals,
xp_awk_tab_getsize(&awk->parse.globals) - nglobals);
return XP_NULL;
@ -462,7 +446,7 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk)
}
/* push the parameter to the parameter list */
if (xp_awk_tab_adddatum(&awk->parse.params, param) == (xp_size_t)-1)
if (xp_awk_tab_add(&awk->parse.params, param) == (xp_size_t)-1)
{
xp_free (name_dup);
xp_awk_tab_clear (&awk->parse.params);
@ -639,7 +623,7 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top)
if (__get_token(awk) == -1)
{
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.locals, nlocals,
xp_awk_tab_getsize(&awk->parse.locals) - nlocals);
return XP_NULL;
@ -647,7 +631,7 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top)
if (__collect_locals(awk, nlocals) == XP_NULL)
{
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.locals, nlocals,
xp_awk_tab_getsize(&awk->parse.locals) - nlocals);
return XP_NULL;
@ -662,7 +646,7 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top)
{
if (MATCH(awk,TOKEN_EOF))
{
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.locals, nlocals,
xp_awk_tab_getsize(&awk->parse.locals) - nlocals);
if (head != XP_NULL) xp_awk_clrpt (head);
@ -673,7 +657,7 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top)
{
if (__get_token(awk) == -1)
{
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.locals, nlocals,
xp_awk_tab_getsize(&awk->parse.locals) - nlocals);
if (head != XP_NULL) xp_awk_clrpt (head);
@ -685,7 +669,7 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top)
nde = __parse_statement (awk);
if (nde == XP_NULL)
{
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.locals, nlocals,
xp_awk_tab_getsize(&awk->parse.locals) - nlocals);
if (head != XP_NULL) xp_awk_clrpt (head);
@ -705,7 +689,7 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top)
block = (xp_awk_nde_blk_t*)xp_malloc(xp_sizeof(xp_awk_nde_blk_t));
if (block == XP_NULL)
{
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.locals, nlocals,
xp_awk_tab_getsize(&awk->parse.locals) - nlocals);
xp_awk_clrpt (head);
@ -715,7 +699,7 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top)
tmp = xp_awk_tab_getsize(&awk->parse.locals);
if (tmp > awk->parse.nlocals_max) awk->parse.nlocals_max = tmp;
xp_awk_tab_remrange (
xp_awk_tab_remove (
&awk->parse.locals, nlocals, tmp - nlocals);
/* adjust number of locals for a block without any statements */
@ -772,7 +756,7 @@ static xp_awk_t* __collect_globals (xp_awk_t* awk)
PANIC (awk, XP_AWK_EDUPVAR);
}
if (xp_awk_tab_adddatum(&awk->parse.globals, global) == (xp_size_t)-1)
if (xp_awk_tab_add(&awk->parse.globals, global) == (xp_size_t)-1)
{
PANIC (awk, XP_AWK_ENOMEM);
}
@ -831,7 +815,7 @@ static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals)
PANIC (awk, XP_AWK_EDUPVAR);
}
if (xp_awk_tab_adddatum(&awk->parse.locals, local) == (xp_size_t)-1)
if (xp_awk_tab_add(&awk->parse.locals, local) == (xp_size_t)-1)
{
PANIC (awk, XP_AWK_ENOMEM);
}
@ -1061,8 +1045,8 @@ static xp_awk_nde_t* __parse_equality (xp_awk_t* awk)
while (1)
{
if (MATCH(awk,TOKEN_EQ)) opcode = BINOP_EQ;
else if (MATCH(awk,TOKEN_NE)) opcode = BINOP_NE;
if (MATCH(awk,TOKEN_EQ)) opcode = XP_AWK_BINOP_EQ;
else if (MATCH(awk,TOKEN_NE)) opcode = XP_AWK_BINOP_NE;
else break;
if (__get_token(awk) == -1)
@ -1111,10 +1095,10 @@ static xp_awk_nde_t* __parse_relational (xp_awk_t* awk)
while (1)
{
if (MATCH(awk,TOKEN_GT)) opcode = BINOP_GT;
else if (MATCH(awk,TOKEN_GE)) opcode = BINOP_GE;
else if (MATCH(awk,TOKEN_LT)) opcode = BINOP_LT;
else if (MATCH(awk,TOKEN_LE)) opcode = BINOP_LE;
if (MATCH(awk,TOKEN_GT)) opcode = XP_AWK_BINOP_GT;
else if (MATCH(awk,TOKEN_GE)) opcode = XP_AWK_BINOP_GE;
else if (MATCH(awk,TOKEN_LT)) opcode = XP_AWK_BINOP_LT;
else if (MATCH(awk,TOKEN_LE)) opcode = XP_AWK_BINOP_LE;
else break;
if (__get_token(awk) == -1)
@ -1163,8 +1147,8 @@ static xp_awk_nde_t* __parse_shift (xp_awk_t* awk)
while (1)
{
if (MATCH(awk,TOKEN_RSHIFT)) opcode = BINOP_RSHIFT;
else if (MATCH(awk,TOKEN_LSHIFT)) opcode = BINOP_LSHIFT;
if (MATCH(awk,TOKEN_RSHIFT)) opcode = XP_AWK_BINOP_RSHIFT;
else if (MATCH(awk,TOKEN_LSHIFT)) opcode = XP_AWK_BINOP_LSHIFT;
else break;
if (__get_token(awk) == -1)
@ -1213,8 +1197,8 @@ static xp_awk_nde_t* __parse_additive (xp_awk_t* awk)
while (1)
{
if (MATCH(awk,TOKEN_PLUS)) opcode = BINOP_PLUS;
else if (MATCH(awk,TOKEN_MINUS)) opcode = BINOP_MINUS;
if (MATCH(awk,TOKEN_PLUS)) opcode = XP_AWK_BINOP_PLUS;
else if (MATCH(awk,TOKEN_MINUS)) opcode = XP_AWK_BINOP_MINUS;
else break;
if (__get_token(awk) == -1)
@ -1263,9 +1247,9 @@ static xp_awk_nde_t* __parse_multiplicative (xp_awk_t* awk)
while (1)
{
if (MATCH(awk,TOKEN_MUL)) opcode = BINOP_MUL;
else if (MATCH(awk,TOKEN_DIV)) opcode = BINOP_DIV;
else if (MATCH(awk,TOKEN_MOD)) opcode = BINOP_MOD;
if (MATCH(awk,TOKEN_MUL)) opcode = XP_AWK_BINOP_MUL;
else if (MATCH(awk,TOKEN_DIV)) opcode = XP_AWK_BINOP_DIV;
else if (MATCH(awk,TOKEN_MOD)) opcode = XP_AWK_BINOP_MOD;
else break;
if (__get_token(awk) == -1)
@ -1293,9 +1277,9 @@ static xp_awk_nde_t* __parse_multiplicative (xp_awk_t* awk)
xp_awk_clrpt (right);
if (opcode == BINOP_MUL) l *= r;
else if (opcode == BINOP_DIV) l /= r;
else if (opcode == BINOP_MOD) l %= r;
if (opcode == XP_AWK_BINOP_MUL) l *= r;
else if (opcode == XP_AWK_BINOP_DIV) l /= r;
else if (opcode == XP_AWK_BINOP_MOD) l %= r;
((xp_awk_nde_int_t*)left)->val = l;
continue;

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c,v 1.8 2006-03-06 04:04:47 bacon Exp $
* $Id: run.c,v 1.9 2006-03-07 15:55:14 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -14,6 +14,7 @@ static int __run_statement (xp_awk_t* awk, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde);
static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde);
int __printval (xp_awk_pair_t* pair)
{
@ -28,13 +29,15 @@ int xp_awk_run (xp_awk_t* awk)
if (awk->tree.begin != XP_NULL)
{
xp_assert (awk->tree.begin->type == XP_AWK_NDE_BLK);
if (__run_block(awk, (xp_awk_nde_blk_t*)awk->tree.begin) == -1) return -1;
if (__run_block (awk,
(xp_awk_nde_blk_t*)awk->tree.begin) == -1) return -1;
}
if (awk->tree.end != XP_NULL)
{
xp_assert (awk->tree.end->type == XP_AWK_NDE_BLK);
if (__run_block(awk, (xp_awk_nde_blk_t*)awk->tree.end) == -1) return -1;
if (__run_block (awk,
(xp_awk_nde_blk_t*)awk->tree.end) == -1) return -1;
}
xp_awk_map_walk (&awk->run.named, __printval);
@ -124,6 +127,8 @@ static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde)
break;
case XP_AWK_NDE_EXP_BIN:
val = __eval_binary(awk,(xp_awk_nde_exp_t*)nde);
break;
case XP_AWK_NDE_EXP_UNR:
@ -148,6 +153,15 @@ static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde)
case XP_AWK_NDE_ARGIDX:
case XP_AWK_NDE_NAMED:
{
xp_awk_nde_var_t* tgt = (xp_awk_nde_var_t*)nde;
xp_awk_pair_t* pair;
pair = xp_awk_map_get(&awk->run.named,tgt->id.name);
if (pair == XP_NULL) val = xp_awk_val_nil;
else val = xp_awk_cloneval (pair->val);
}
break;
case XP_AWK_NDE_NAMEDIDX:
@ -191,21 +205,6 @@ static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde)
xp_assert (tgt != XP_NULL);
if (new == XP_NULL) return XP_NULL;
/*
name = tgt->id.name;
old = (xp_awk_val_t*)xp_awk_map_getval(&awk->run.named, name);
if (old == XP_NULL)
{
name = xp_strdup (tgt->id.name);
if (name == XP_NULL)
{
xp_awk_freeval(new);
awk->errnum = XP_AWK_ENOMEM;
return XP_NULL;
}
}
*/
pair = xp_awk_map_get(&awk->run.named, tgt->id.name);
if (pair == XP_NULL)
{
@ -260,3 +259,34 @@ static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde)
return v;
}
static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde)
{
xp_awk_val_t* left, * right;
xp_assert (nde->type == XP_AWK_NDE_EXP_BIN);
left = __eval_expression (awk, nde->left);
if (left == XP_NULL) return XP_NULL;
right = __eval_expression (awk, nde->right);
if (right == XP_NULL)
{
xp_awk_freeval(left);
return XP_NULL;
}
// TODO: a lot of things to do....
if (nde->opcode == XP_AWK_BINOP_PLUS)
{
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
((xp_awk_val_int_t*)left)->val +=
((xp_awk_val_int_t*)right)->val;
}
}
xp_awk_freeval(right);
return left;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: tab.c,v 1.5 2006-03-03 11:51:48 bacon Exp $
* $Id: tab.c,v 1.6 2006-03-07 15:55:14 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -12,7 +12,8 @@
xp_awk_tab_t* xp_awk_tab_open (xp_awk_tab_t* tab)
{
if (tab == XP_NULL) {
if (tab == XP_NULL)
{
tab = (xp_awk_tab_t*) xp_malloc (xp_sizeof(xp_awk_tab_t));
if (tab == XP_NULL) return XP_NULL;
tab->__dynamic = xp_true;
@ -29,7 +30,8 @@ xp_awk_tab_t* xp_awk_tab_open (xp_awk_tab_t* tab)
void xp_awk_tab_close (xp_awk_tab_t* tab)
{
xp_awk_tab_clear (tab);
if (tab->buf != XP_NULL) {
if (tab->buf != XP_NULL)
{
xp_free (tab->buf);
tab->buf = XP_NULL;
tab->capa = 0;
@ -52,17 +54,20 @@ xp_awk_tab_t* xp_awk_tab_setcapa (xp_awk_tab_t* tab, xp_size_t capa)
{
xp_char_t** tmp;
if (tab->size > capa) {
xp_awk_tab_remrange (tab, capa, tab->size - capa);
if (tab->size > capa)
{
xp_awk_tab_remove (tab, capa, tab->size - capa);
xp_assert (tab->size <= capa);
}
if (capa > 0) {
if (capa > 0)
{
tmp = (xp_char_t**)xp_realloc (
tab->buf, xp_sizeof(xp_char_t*) * capa);
if (tmp == XP_NULL) return XP_NULL;
}
else {
else
{
if (tab->buf != XP_NULL) xp_free (tab->buf);
tmp = XP_NULL;
}
@ -79,7 +84,8 @@ void xp_awk_tab_clear (xp_awk_tab_t* tab)
xp_assert (tab != XP_NULL);
for (i = 0; i < tab->size; i++) {
for (i = 0; i < tab->size; i++)
{
xp_free (tab->buf[i]);
tab->buf[i] = XP_NULL;
}
@ -88,7 +94,7 @@ void xp_awk_tab_clear (xp_awk_tab_t* tab)
}
xp_size_t xp_awk_tab_insdatum (
xp_size_t xp_awk_tab_insert (
xp_awk_tab_t* tab, xp_size_t index, const xp_char_t* value)
{
xp_size_t i;
@ -97,15 +103,18 @@ xp_size_t xp_awk_tab_insdatum (
value_dup = xp_strdup(value);
if (value_dup == XP_NULL) return (xp_size_t)-1;
if (index >= tab->capa) {
if (index >= tab->capa)
{
xp_size_t capa;
if (tab->capa <= 0) capa = (index + 1);
else {
else
{
do { capa = tab->capa * 2; } while (index >= capa);
}
if (xp_awk_tab_setcapa(tab,capa) == XP_NULL) {
if (xp_awk_tab_setcapa(tab,capa) == XP_NULL)
{
xp_free (value_dup);
return (xp_size_t)-1;
}
@ -120,7 +129,7 @@ xp_size_t xp_awk_tab_insdatum (
return index;
}
xp_size_t xp_awk_tab_remrange (
xp_size_t xp_awk_tab_remove (
xp_awk_tab_t* tab, xp_size_t index, xp_size_t count)
{
xp_size_t i, j, k;
@ -132,20 +141,29 @@ xp_size_t xp_awk_tab_remrange (
j = index + count;
k = index + count;
while (i < k) {
while (i < k)
{
xp_free (tab->buf[i]);
if (j >= tab->size)
tab->buf[i] = (j >= tab->size)? XP_NULL: tab->buf[j];
i++; j++;
{
tab->buf[i] = XP_NULL;
i++;
}
else
{
tab->buf[i] = tab->buf[j];
i++; j++;
}
}
tab->size -= count;
return count;
}
xp_size_t xp_awk_tab_adddatum (xp_awk_tab_t* tab, const xp_char_t* value)
xp_size_t xp_awk_tab_add (xp_awk_tab_t* tab, const xp_char_t* value)
{
return xp_awk_tab_insdatum (tab, tab->size, value);
return xp_awk_tab_insert (tab, tab->size, value);
}

View File

@ -1,5 +1,5 @@
/*
* $Id: tab.h,v 1.4 2006-03-03 11:51:48 bacon Exp $
* $Id: tab.h,v 1.5 2006-03-07 15:55:14 bacon Exp $
*/
#ifndef _XP_AWK_TAB_H_
@ -42,12 +42,13 @@ xp_awk_tab_t* xp_awk_tab_setcapa (xp_awk_tab_t* tab, xp_size_t capa);
void xp_awk_tab_clear (xp_awk_tab_t* tab);
xp_size_t xp_awk_tab_insdatum (
xp_size_t xp_awk_tab_insert (
xp_awk_tab_t* tab, xp_size_t index, const xp_char_t* value);
xp_size_t xp_awk_tab_remrange (
xp_size_t xp_awk_tab_remove (
xp_awk_tab_t* tab, xp_size_t index, xp_size_t count);
xp_size_t xp_awk_tab_adddatum (xp_awk_tab_t* tab, const xp_char_t* value);
xp_size_t xp_awk_tab_add (xp_awk_tab_t* tab, const xp_char_t* value);
xp_size_t xp_awk_tab_find (
xp_awk_tab_t* tab, const xp_char_t* value, xp_size_t index);
xp_size_t xp_awk_tab_rfind (

View File

@ -1,9 +1,12 @@
/*
* $Id: val.c,v 1.2 2006-03-05 17:07:33 bacon Exp $
* $Id: val.c,v 1.3 2006-03-07 15:55:14 bacon Exp $
*/
#include <xp/awk/awk.h>
static xp_awk_val_nil_t __awk_nil = { XP_AWK_VAL_NIL };
xp_awk_val_t* xp_awk_val_nil = (xp_awk_val_t*)&__awk_nil;
xp_awk_val_t* xp_awk_makeintval (xp_long_t v)
{
xp_awk_val_int_t* val;
@ -59,11 +62,36 @@ void xp_awk_freeval (xp_awk_val_t* val)
}
}
xp_awk_val_t* xp_awk_cloneval (xp_awk_val_t* val)
{
if (val == XP_NULL) return xp_awk_val_nil;
switch (val->type)
{
case XP_AWK_VAL_NIL:
return xp_awk_val_nil;
case XP_AWK_VAL_INT:
return xp_awk_makeintval (((xp_awk_val_int_t*)val)->val);
case XP_AWK_VAL_REAL:
return xp_awk_makerealval (((xp_awk_val_real_t*)val)->val);
case XP_AWK_VAL_STR:
return xp_awk_makestrval (
((xp_awk_val_str_t*)val)->buf,
((xp_awk_val_str_t*)val)->len);
}
return XP_NULL;
}
void xp_awk_printval (xp_awk_val_t* val)
{
// TODO: better value printing......................
switch (val->type)
{
case XP_AWK_VAL_NIL:
xp_printf (XP_TEXT("nil"));
break;
case XP_AWK_VAL_INT:
xp_printf (XP_TEXT("%lld"),
(long long)((xp_awk_val_int_t*)val)->val);

View File

@ -1,5 +1,5 @@
/*
* $Id: val.h,v 1.3 2006-03-05 17:07:33 bacon Exp $
* $Id: val.h,v 1.4 2006-03-07 15:55:14 bacon Exp $
*/
#ifndef _XP_AWK_VAL_H_
@ -11,12 +11,14 @@
enum
{
XP_AWK_VAL_NIL,
XP_AWK_VAL_INT,
XP_AWK_VAL_REAL,
XP_AWK_VAL_STR
};
typedef struct xp_awk_val_t xp_awk_val_t;
typedef struct xp_awk_val_nil_t xp_awk_val_nil_t;
typedef struct xp_awk_val_int_t xp_awk_val_int_t;
typedef struct xp_awk_val_real_t xp_awk_val_real_t;
typedef struct xp_awk_val_str_t xp_awk_val_str_t;
@ -29,6 +31,12 @@ struct xp_awk_val_t
XP_AWK_VAL_HDR;
};
/* XP_AWK_VAL_NIL */
struct xp_awk_val_nil_t
{
XP_AWK_VAL_HDR;
};
/* XP_AWK_VAL_INT */
struct xp_awk_val_int_t
{
@ -55,10 +63,13 @@ struct xp_awk_val_str_t
extern "C" {
#endif
extern xp_awk_val_t* xp_awk_val_nil;
xp_awk_val_t* xp_awk_makeintval (xp_long_t v);
xp_awk_val_t* xp_awk_makestrval (const xp_char_t* str, xp_size_t len);
void xp_awk_freeval (xp_awk_val_t* val);
xp_awk_val_t* xp_awk_cloneval (xp_awk_val_t* val);
void xp_awk_printval (xp_awk_val_t* val);
#ifdef __cplusplus