diff --git a/ase/awk/awk.h b/ase/awk/awk.h index 68673863..0bbded98 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.77 2006-07-05 16:20:23 bacon Exp $ + * $Id: awk.h,v 1.78 2006-07-12 07:25:15 bacon Exp $ */ #ifndef _XP_AWK_AWK_H_ @@ -183,6 +183,7 @@ int xp_awk_run (xp_awk_t* awk, xp_awk_io_t txtio, void* txtio_arg); xp_size_t xp_awk_getnargs (void* run); xp_awk_val_t* xp_awk_getarg (void* run, xp_size_t idx); void xp_awk_setretval (void* run, xp_awk_val_t* val); +void xp_awk_setglobal (void* run, xp_size_t idx, xp_awk_val_t* val); void xp_awk_seterrnum (void* run, int errnum); /* utility functions exported by awk.h */ diff --git a/ase/awk/run.c b/ase/awk/run.c index bb23e1f5..15b343d3 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.130 2006-07-10 14:28:45 bacon Exp $ + * $Id: run.c,v 1.131 2006-07-12 07:25:15 bacon Exp $ */ #include @@ -199,6 +199,14 @@ void xp_awk_setretval (void* run, xp_awk_val_t* val) xp_awk_refupval (val); } +void xp_awk_setglobal (void* run, xp_size_t idx, xp_awk_val_t* val) +{ + xp_awk_run_t* r = (xp_awk_run_t*)run; + xp_awk_refdownval (run, STACK_GLOBAL(r,idx)); + STACK_GLOBAL(r,idx) = val; + xp_awk_refupval (val); +} + void xp_awk_seterrnum (void* run, int errnum) { xp_awk_run_t* r = (xp_awk_run_t*)run; @@ -3658,6 +3666,7 @@ static int __split_record (xp_awk_run_t* run) xp_char_t* p, * tok; xp_size_t len, tok_len, nflds; + xp_awk_val_t* v; /* inrec should be cleared before __split_record is called */ xp_assert (run->inrec.nflds == 0); @@ -3711,11 +3720,9 @@ static int __split_record (xp_awk_run_t* run) 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) - { - __clear_record (run, xp_false); PANIC_I (run, XP_AWK_ENOMEM); - } xp_awk_refupval (run->inrec.flds[run->inrec.nflds].val); run->inrec.nflds++; @@ -3724,6 +3731,11 @@ static int __split_record (xp_awk_run_t* run) (p - XP_STR_BUF(&run->inrec.line)); } + /* set the number of fields */ + v = xp_awk_makeintval (run, (xp_long_t)nflds); + if (v == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM); + xp_awk_setglobal (run, XP_AWK_GLOBAL_NF, v); + xp_assert (nflds == run->inrec.nflds); return 0; } @@ -3745,6 +3757,7 @@ static void __clear_record (xp_awk_run_t* run, xp_bool_t noline) xp_awk_refdownval (run, run->inrec.flds[i].val); } run->inrec.nflds = 0; + xp_awk_setglobal (run, XP_AWK_GLOBAL_NF, xp_awk_val_zero); } xp_assert (run->inrec.nflds == 0); @@ -3755,6 +3768,7 @@ static int __recomp_record_fields (xp_awk_run_t* run, xp_size_t lv, xp_char_t* str, xp_size_t len, int* errnum) { xp_size_t max, i, nflds; + xp_awk_val_t* v; xp_assert (lv > 0); max = (lv > run->inrec.nflds)? lv: run->inrec.nflds; @@ -3855,6 +3869,15 @@ static int __recomp_record_fields (xp_awk_run_t* run, } } + v = STACK_GLOBAL(run, XP_AWK_GLOBAL_NF); + xp_assert (v->type == XP_AWK_VAL_INT); + if (((xp_awk_val_int_t*)v)->val != max) + { + v = xp_awk_makeintval (run, (xp_long_t)max); + if (v == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM); + xp_awk_setglobal (run, XP_AWK_GLOBAL_NF, v); + } + return 0; } @@ -3971,3 +3994,4 @@ static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde) return str; } + diff --git a/ase/awk/run.h b/ase/awk/run.h index 53a7d96c..ea8bcee8 100644 --- a/ase/awk/run.h +++ b/ase/awk/run.h @@ -1,5 +1,5 @@ /* - * $Id: run.h,v 1.14 2006-06-29 15:40:30 bacon Exp $ + * $Id: run.h,v 1.15 2006-07-12 07:25:15 bacon Exp $ */ #ifndef _XP_AWK_RUN_H_ @@ -73,4 +73,31 @@ enum XP_AWK_INCOP_MINUS }; +enum +{ + /* this table should match __bvtab in parse.c */ + + XP_AWK_GLOBAL_ARGC, + XP_AWK_GLOBAL_ARGIND, + XP_AWK_GLOBAL_ARGV, + XP_AWK_GLOBAL_CONVFMT, + XP_AWK_GLOBAL_FIELDWIDTHS, + XP_AWK_GLOBAL_ENVIRON, + XP_AWK_GLOBAL_ERRNO, + XP_AWK_GLOBAL_FILENAME, + XP_AWK_GLOBAL_FNR, + XP_AWK_GLOBAL_FS, + XP_AWK_GLOBAL_INORECASE, + XP_AWK_GLOBAL_NF, + XP_AWK_GLOBAL_NR, + XP_AWK_GLOBAL_OFMT, + XP_AWK_GLOBAL_OFS, + XP_AWK_GLOBAL_ORS, + XP_AWK_GLOBAL_RS, + XP_AWK_GLOBAL_RT, + XP_AWK_GLOBAL_RSTART, + XP_AWK_GLOBAL_RLENGTH, + XP_AWK_GLOBAL_SUBSEP +}; + #endif diff --git a/ase/awk/val.c b/ase/awk/val.c index 4bf27391..7961620e 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.38 2006-07-10 04:51:38 bacon Exp $ + * $Id: val.c,v 1.39 2006-07-12 07:25:15 bacon Exp $ */ #include @@ -31,6 +31,9 @@ static xp_awk_val_int_t __awk_int[] = { XP_AWK_VAL_INT, 0, 9 }, }; +xp_awk_val_t* xp_awk_val_zero = (xp_awk_val_t*)&__awk_int[1]; +xp_awk_val_t* xp_awk_val_one = (xp_awk_val_t*)&__awk_int[2]; + xp_awk_val_t* xp_awk_makeintval (xp_awk_run_t* run, xp_long_t v) { xp_awk_val_int_t* val; diff --git a/ase/awk/val.h b/ase/awk/val.h index f4035fdd..b801b442 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.28 2006-07-10 04:51:38 bacon Exp $ + * $Id: val.h,v 1.29 2006-07-12 07:25:15 bacon Exp $ */ #ifndef _XP_AWK_VAL_H_ @@ -94,6 +94,8 @@ extern "C" { extern xp_awk_val_t* xp_awk_val_nil; extern xp_awk_val_t* xp_awk_val_zls; +extern xp_awk_val_t* xp_awk_val_zero; +extern xp_awk_val_t* xp_awk_val_one; xp_awk_val_t* xp_awk_makeintval (xp_awk_run_t* run, xp_long_t v); xp_awk_val_t* xp_awk_makerealval (xp_awk_run_t* run, xp_real_t v); diff --git a/ase/test/awk/t14.awk b/ase/test/awk/t14.awk index ac490fa9..008c1d8f 100644 --- a/ase/test/awk/t14.awk +++ b/ase/test/awk/t14.awk @@ -20,18 +20,19 @@ END global x, y; { + print "NF = " NF; for (i = 0; i < 10; i++) { print $i; if (i == 3) a = $i; } - /* - $1 = $2; - $3 = $2; - $2 = $2; + $1 = 100; + //$1 = $2; + //$3 = $2; + //$2 = $2; + $20 = 10; print $0; - */ print "--------------------"; print NF; print "====================";