*** empty log message ***

This commit is contained in:
hyung-hwan 2006-07-06 15:54:41 +00:00
parent d859d98608
commit e0e3516a51
6 changed files with 180 additions and 57 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: awk_i.h,v 1.27 2006-07-06 13:57:31 bacon Exp $
* $Id: awk_i.h,v 1.28 2006-07-06 15:54:41 bacon Exp $
*/
#ifndef _XP_AWK_AWKI_H_
@ -164,13 +164,16 @@ struct xp_awk_run_t
xp_size_t buf_pos;
xp_size_t buf_len;
xp_bool_t eof;
xp_str_t line;
xp_size_t nflds;
xp_str_t line;
xp_awk_val_t* d0; /* $0 */
xp_size_t nflds; /* NF */
struct
{
xp_char_t* ptr;
xp_size_t len;
xp_char_t* ptr;
xp_size_t len;
xp_awk_val_t* val; /* $1 .. $NF */
}* flds;
} inrec;

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c,v 1.124 2006-07-06 13:57:31 bacon Exp $
* $Id: run.c,v 1.125 2006-07-06 15:54:41 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -263,6 +263,7 @@ static int __open_run (
run->inrec.buf_len = 0;
run->inrec.flds = XP_NULL;
run->inrec.nflds = 0;
run->inrec.d0 = xp_awk_val_nil;
if (xp_str_open (&run->inrec.line, DEF_BUF_CAPA) == XP_NULL)
{
run->errnum = XP_AWK_ENOMEM;
@ -303,8 +304,12 @@ static void __close_run (xp_awk_run_t* run)
xp_awk_map_close (&run->named);
/* destroy input record */
xp_awk_refdownval (run, run->inrec.d0);
if (run->inrec.flds != XP_NULL)
{
xp_size_t i;
for (i = 0; i < run->inrec.nflds; i++)
xp_awk_refdownval (run, run->inrec.flds[i].val);
xp_free (run->inrec.flds);
run->inrec.flds = XP_NULL;
run->inrec.nflds = 0;
@ -770,8 +775,10 @@ static int __run_statement (xp_awk_run_t* run, xp_awk_nde_t* nde)
xp_awk_val_t* v;
v = __eval_expression(run,nde);
if (v == XP_NULL) return -1;
xp_awk_refupval (v);
xp_awk_refdownval (run, v);
break;
}
}
@ -1733,8 +1740,10 @@ static xp_awk_val_t* __do_assignment_pos (
xp_awk_val_t* v;
xp_long_t lv;
xp_real_t rv;
xp_char_t* str;
int n, errnum;
/* get the position number */
v = __eval_expression (run, pos->val);
if (v == XP_NULL) return XP_NULL;
@ -1745,22 +1754,85 @@ static xp_awk_val_t* __do_assignment_pos (
if (n == -1) PANIC (run, XP_AWK_EPOSIDX);
if (n == 1) lv = (xp_long_t)rv;
/* convert the value to the string */
str = xp_awk_valtostr (val, &errnum, XP_NULL);
if (str == XP_NULL) PANIC (run, errnum);
if (lv == 0)
{
if (xp_awk_valtostr (
val, &errnum, &run->inrec.line) == XP_NULL)
if (xp_str_cpy (&run->inrec.line, str) == (xp_size_t)-1)
{
xp_free (str);
/* clear $0 ... $NF */
if (run->inrec.flds != XP_NULL)
{
xp_size_t i;
for (i = 0; i < run->inrec.nflds; i++)
xp_awk_refdownval (run, run->inrec.flds[i].val);
xp_free (run->inrec.flds);
run->inrec.flds = XP_NULL;
run->inrec.nflds = 0;
}
xp_assert (run->inrec.nflds == 0);
xp_str_clear (&run->inrec.line);
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = xp_awk_val_nil;
PANIC (run, errnum);
}
xp_free (str);
if (__split_record (run) == -1) return XP_NULL;
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = val;
xp_awk_refupval (val);
}
else
{
xp_printf (XP_T("$X assignemtn NOT IMPLEMENTED\n"));
/* TODO: handle out of bound index... */
/* TODO: reimplement this entire function ........... */
/* what schecm should i use to support $n = value where n is not zero ? */
/* should i use run->inrec.line to recompose $0 and adjust other fileds? */
/* or should $0 be composed of fidls separated by FS? if so, what should i
* do when FS is a regular expression??? */
#if 0
if (xp_str_rpl (&run->inrec.line,
run->inrec.flds[lv].ptr,
run->inrec.flds[lv].len, str) == (xp_size_t)-1)
{
xp_free (str);
/* TODO: clear $0 .. $NF */
PANIC (run, XP_AWK_ENOMEM);
}
xp_free (str);
v = xp_awk_makestrval (
XP_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line));
if (v == XP_NULL)
{
/* TODO: clear $0 .. $NF */
PANIC (run, XP_AWK_ENOMEM);
}
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = v;
xp_awk_refupval (v);
#endif
}
if (__split_record (run) == -1)
{
xp_str_clear (&run->inrec.line);
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = xp_awk_val_nil;
return XP_NULL;
}
//xp_awk_refupval (val);
return val;
}
@ -3405,24 +3477,10 @@ static xp_awk_val_t* __eval_pos (xp_awk_run_t* run, xp_awk_nde_t* nde)
if (n == -1) PANIC (run, XP_AWK_EPOSIDX);
if (n == 1) lv = (xp_long_t)rv;
if (lv == 0)
{
v = xp_awk_makestrval (
XP_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line));
if (v == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
}
else if (lv > 0 && lv <= run->inrec.nflds)
{
v = xp_awk_makestrval (
run->inrec.flds[lv-1].ptr,
run->inrec.flds[lv-1].len);
if (v == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
}
else
{
v = xp_awk_val_nil;
}
if (lv == 0) v = run->inrec.d0;
else if (lv > 0 && lv <= run->inrec.nflds)
v = run->inrec.flds[lv-1].val;
else v = xp_awk_val_nil;
return v;
}
@ -3549,6 +3607,7 @@ static int __read_record (xp_awk_run_t* run)
{
xp_ssize_t n;
xp_char_t c;
xp_awk_val_t* v;
xp_str_clear (&run->inrec.line);
@ -3589,7 +3648,28 @@ static int __read_record (xp_awk_run_t* run)
}
}
if (__split_record (run) == -1) return -1;
v = xp_awk_makestrval (
XP_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line));
if (v == XP_NULL)
{
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = xp_awk_val_nil;
xp_str_clear (&run->inrec.line);
PANIC_I (run, XP_AWK_ENOMEM);
}
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = v;
xp_awk_refupval (v);
if (__split_record (run) == -1)
{
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = xp_awk_val_nil;
xp_str_clear (&run->inrec.line);
return -1;
}
return 1; /* has read a record */
}
@ -3597,13 +3677,16 @@ static int __read_record (xp_awk_run_t* run)
static int __split_record (xp_awk_run_t* run)
{
/* TODO: support FS and regular expression */
/* TODO: set NR after split */
xp_char_t* p, * tok;
xp_size_t len, tok_len, nflds;
xp_size_t len, tok_len, nflds, i;
/* clear input record fields */
if (run->inrec.flds != XP_NULL)
{
for (i = 0; i < run->inrec.nflds; i++)
xp_awk_refdownval (run, run->inrec.flds[i].val);
xp_free (run->inrec.flds);
run->inrec.flds = XP_NULL;
run->inrec.nflds = 0;
@ -3639,8 +3722,22 @@ static int __split_record (xp_awk_run_t* run)
run->inrec.flds[run->inrec.nflds].ptr = tok;
run->inrec.flds[run->inrec.nflds].len = tok_len;
run->inrec.flds[run->inrec.nflds].val =
xp_awk_makestrval (tok, tok_len);
if (run->inrec.flds[run->inrec.nflds].val == XP_NULL)
{
for (i = 0; i < run->inrec.nflds; i++)
xp_awk_refdownval (run, run->inrec.flds[i].val);
xp_free (run->inrec.flds);
run->inrec.flds = XP_NULL;
run->inrec.nflds = 0;
PANIC_I (run, XP_AWK_ENOMEM);
}
xp_awk_refupval (run->inrec.flds[run->inrec.nflds].val);
run->inrec.nflds++;
len = len - tok_len;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: sa.c,v 1.25 2006-07-06 13:57:31 bacon Exp $
* $Id: sa.c,v 1.26 2006-07-06 15:54:41 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -326,20 +326,37 @@ void xp_str_forfeit (xp_str_t* str)
if (str->__dynamic) xp_free (str);
}
xp_size_t xp_str_cpy (xp_str_t* str, const xp_char_t* s)
{
/* TODO: improve it */
return xp_str_ncpy (str, s, xp_strlen(s));
}
xp_size_t xp_str_ncpy (xp_str_t* str, const xp_char_t* s, xp_size_t len)
{
xp_char_t* buf;
if (len > str->capa)
{
buf = (xp_char_t*) xp_malloc (xp_sizeof(xp_char_t) * (len + 1));
if (buf == XP_NULL) return (xp_size_t)-1;
xp_free (str->buf);
str->capa = len;
str->buf = buf;
}
str->size = xp_strncpy (str->buf, s, len);
str->buf[str->size] = XP_CHAR('\0');
return str->size;
}
xp_size_t xp_str_cat (xp_str_t* str, const xp_char_t* s)
{
/* TODO: improve it */
return xp_str_ncat (str, s, xp_strlen(s));
}
static xp_size_t __strncpy (xp_char_t* buf, const xp_char_t* str, xp_size_t len)
{
const xp_char_t* end = str + len;
while (str < end) *buf++ = *str++;
*buf = XP_T('\0');
return len;
}
xp_size_t xp_str_ncat (xp_str_t* str, const xp_char_t* s, xp_size_t len)
{
xp_char_t* buf;
@ -360,7 +377,7 @@ xp_size_t xp_str_ncat (xp_str_t* str, const xp_char_t* s, xp_size_t len)
str->buf = buf;
}
str->size += __strncpy (&str->buf[str->size], s, len);
str->size += xp_strncpy (&str->buf[str->size], s, len);
str->buf[str->size] = XP_T('\0');
return str->size;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: sa.h,v 1.25 2006-07-05 16:20:23 bacon Exp $
* $Id: sa.h,v 1.26 2006-07-06 15:54:41 bacon Exp $
*/
#ifndef _XP_AWK_SA_H_
@ -142,6 +142,12 @@ void xp_str_close (xp_str_t* str);
#define xp_str_forfeit xp_awk_str_forfeit
void xp_str_forfeit (xp_str_t* str);
#define xp_str_cpy xp_awk_str_cpy
xp_size_t xp_str_cpy (xp_str_t* str, const xp_char_t* s);
#define xp_str_ncpy xp_awk_str_ncpy
xp_size_t xp_str_ncpy (xp_str_t* str, const xp_char_t* s, xp_size_t len);
#define xp_str_cat xp_awk_str_cat
xp_size_t xp_str_cat (xp_str_t* str, const xp_char_t* s);

View File

@ -1,5 +1,5 @@
/*
* $Id: val.c,v 1.36 2006-07-06 13:57:31 bacon Exp $
* $Id: val.c,v 1.37 2006-07-06 15:54:41 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -53,6 +53,7 @@ xp_awk_val_t* xp_awk_makeintval (xp_awk_run_t* run, xp_long_t v)
val->ref = 0;
val->val = v;
/*xp_printf (XP_T("makeintval => %p\n"), val);*/
return (xp_awk_val_t*)val;
}
@ -75,6 +76,7 @@ xp_awk_val_t* xp_awk_makerealval (xp_awk_run_t* run, xp_real_t v)
val->ref = 0;
val->val = v;
/*xp_printf (XP_T("makerealval => %p\n"), val);*/
return (xp_awk_val_t*)val;
}
@ -100,7 +102,7 @@ xp_awk_val_t* xp_awk_makestrval (const xp_char_t* str, xp_size_t len)
return XP_NULL;
}
xp_printf (XP_T("makestrval => %p\n"), val);
/*xp_printf (XP_T("makestrval => %p\n"), val);*/
return (xp_awk_val_t*)val;
}
@ -123,6 +125,7 @@ xp_awk_val_t* xp_awk_makestrval2 (
return XP_NULL;
}
/*xp_printf (XP_T("makestrval2 => %p\n"), val);*/
return (xp_awk_val_t*)val;
}
@ -144,6 +147,7 @@ xp_awk_val_t* xp_awk_makerexval (const xp_char_t* str, xp_size_t len)
return XP_NULL;
}
/*xp_printf (XP_T("makerexval => %p\n"), val);*/
return (xp_awk_val_t*)val;
}
@ -191,11 +195,10 @@ void xp_awk_freeval (xp_awk_run_t* run, xp_awk_val_t* val, xp_bool_t cache)
/*xp_printf (XP_T("freeing [cache=%d] ... "), cache);
xp_awk_printval (val);
xp_printf (XP_T("\n"));*/
xp_printf (XP_TEXT("freeing VAL[%p]\n"), val);
switch (val->type)
{
case XP_AWK_VAL_NIL:
xp_free (val);
if (val != xp_awk_val_nil) xp_free (val);
return;
case XP_AWK_VAL_INT:
@ -219,9 +222,6 @@ xp_printf (XP_TEXT("freeing VAL[%p]\n"), val);
return;
case XP_AWK_VAL_STR:
xp_printf (XP_TEXT("freeing STR[%p/%s]\n"),
((xp_awk_val_str_t*)val)->buf,
((xp_awk_val_str_t*)val)->buf);
xp_free (((xp_awk_val_str_t*)val)->buf);
xp_free (val);
return;
@ -267,9 +267,11 @@ xp_printf (XP_T("\n"));
val->ref--;
if (val->ref <= 0)
{
/*
xp_printf (XP_T("**FREEING ["));
xp_awk_printval (val);
xp_printf (XP_T("]\n"));
*/
xp_awk_freeval(run, val, xp_true);
}
}
@ -347,7 +349,7 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf)
}
else
{
//xp_str_clear (buf);
xp_str_clear (buf);
if (xp_str_cat (buf, XP_T("0")) == (xp_size_t)-1)
{
*errnum = XP_AWK_ENOMEM;
@ -375,7 +377,7 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf)
}
else
{
//xp_str_clear (buf);
xp_str_clear (buf);
/* get the current end of the buffer */
tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf);
@ -418,7 +420,7 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf)
}
else
{
//xp_str_clear (buf);
xp_str_clear (buf);
tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf);
if (xp_str_ncat (buf,

View File

@ -20,14 +20,12 @@ END
{
//$3 = "10";
//print $0, $2, $5;
$0 = "wow this is just a test";
print "step 1--------------------";
print $0 = "wow this is just a test";
//print $0;
a = $6;
print "step 2--------------------";
print a = $6;
print a;
/*
print $6;
*/
print "step 3--------------------";
print "--------------------";
}