From ba4ea092ce76ce438e8112d8bbeadd01bbf2b202 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 10 Jul 2006 14:28:46 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/map.c | 7 +- ase/awk/map.h | 6 +- ase/awk/parse.c | 134 ++++++++++++++++--------- ase/awk/run.c | 226 +++++++++++++++++++++---------------------- ase/test/awk/t14.awk | 23 +++-- ase/test/awk/t3.awk | 2 +- 6 files changed, 223 insertions(+), 175 deletions(-) diff --git a/ase/awk/map.c b/ase/awk/map.c index 1eac3183..ecf77a4b 100644 --- a/ase/awk/map.c +++ b/ase/awk/map.c @@ -1,5 +1,5 @@ /* - * $Id: map.c,v 1.19 2006-05-06 12:52:36 bacon Exp $ + * $Id: map.c,v 1.20 2006-07-10 14:28:45 bacon Exp $ */ #include @@ -81,7 +81,7 @@ void xp_awk_map_clear (xp_awk_map_t* map) xp_assert (map->size == 0); } -xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, xp_char_t* key) +xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, const xp_char_t* key) { xp_awk_pair_t* pair; xp_size_t hc; @@ -171,13 +171,14 @@ 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** val) + xp_awk_map_t* map, const xp_char_t* key, void** val) { xp_awk_pair_t* pair; pair = xp_awk_map_get (map, key); if (pair == XP_NULL) return XP_NULL; *val = pair->val; + return pair; } diff --git a/ase/awk/map.h b/ase/awk/map.h index d5a9c807..62c64936 100644 --- a/ase/awk/map.h +++ b/ase/awk/map.h @@ -1,5 +1,5 @@ /* - * $Id: map.h,v 1.12 2006-06-16 14:31:42 bacon Exp $ + * $Id: map.h,v 1.13 2006-07-10 14:28:45 bacon Exp $ */ #ifndef _XP_AWK_MAP_H_ @@ -39,13 +39,13 @@ void xp_awk_map_close (xp_awk_map_t* map); void xp_awk_map_clear (xp_awk_map_t* map); -xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, xp_char_t* key); +xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, const xp_char_t* key); xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* val); int xp_awk_map_putx (xp_awk_map_t* map, xp_char_t* key, void* val, xp_awk_pair_t** px); 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** val); + xp_awk_map_t* map, const 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* val); diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 0d49477e..0eaefb12 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.134 2006-07-07 09:48:23 bacon Exp $ + * $Id: parse.c,v 1.135 2006-07-10 14:28:45 bacon Exp $ */ #include @@ -107,6 +107,8 @@ struct __binmap_t static xp_awk_t* __parse_progunit (xp_awk_t* awk); static xp_awk_t* __collect_globals (xp_awk_t* awk); +static xp_awk_t* __add_builtin_globals (xp_awk_t* awk); +static xp_awk_t* __add_global (xp_awk_t* awk, const xp_char_t* name); static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals); static xp_awk_nde_t* __parse_function (xp_awk_t* awk); @@ -220,33 +222,37 @@ static struct __kwent __kwtab[] = { XP_NULL, 0, 0 } }; -/* TODO: -static struct __kwent __bvtab[] = +struct __bvent { - { XP_T("ARGC"), TOKEN_ARGC, 0 }, - { XP_T("ARGIND"), TOKEN_ARGIND, 0 }, - { XP_T("ARGV"), TOKEN_ARGV, 0 }, - { XP_T("CONVFMT"), TOKEN_CONVFMT, 0 }, - { XP_T("FIELDWIDTHS"), TOKEN_FIELDWIDTHS, 0 }, - { XP_T("ENVIRON"), TOKEN_ENVIRON, 0 }, - { XP_T("ERRNO"), TOKEN_ERRNO, 0 }, - { XP_T("FILENAME"), TOKEN_FILENAME, 0 }, - { XP_T("FNR"), TOKEN_FNR, 0 }, - { XP_T("FS"), TOKEN_FS, 0 }, - { XP_T("IGNORECASE"), TOKEN_IGNORECASE, 0 }, - { XP_T("NF"), TOKEN_NF, 0 }, - { XP_T("NR"), TOKEN_NR, 0 }, - { XP_T("OFMT"), TOKEN_OFMT, 0 }, - { XP_T("OFS"), TOKEN_OFS, 0 }, - { XP_T("ORS"), TOKEN_ORS, 0 }, - { XP_T("RS"), TOKEN_RS, 0 }, - { XP_T("RT"), TOKEN_RT, 0 }, - { XP_T("RSTART"), TOKEN_RSTART, 0 }, - { XP_T("RLENGTH"), TOKEN_RLENGTH, 0 }, - { XP_T("SUBSEP"), TOKEN_SUBSEP, 0 }, - { XP_NULL, 0, 0 } + const xp_char_t* name; + int valid; +}; + +static struct __bvent __bvtab[] = +{ + { XP_T("ARGC"), 0 }, + { XP_T("ARGIND"), 0 }, + { XP_T("ARGV"), 0 }, + { XP_T("CONVFMT"), 0 }, + { XP_T("FIELDWIDTHS"), 0 }, + { XP_T("ENVIRON"), 0 }, + { XP_T("ERRNO"), 0 }, + { XP_T("FILENAME"), 0 }, + { XP_T("FNR"), 0 }, + { XP_T("FS"), 0 }, + { XP_T("IGNORECASE"), 0 }, + { XP_T("NF"), 0 }, + { XP_T("NR"), 0 }, + { XP_T("OFMT"), 0 }, + { XP_T("OFS"), 0 }, + { XP_T("ORS"), 0 }, + { XP_T("RS"), 0 }, + { XP_T("RT"), 0 }, + { XP_T("RSTART"), 0 }, + { XP_T("RLENGTH"), 0 }, + { XP_T("SUBSEP"), 0 }, + { XP_NULL, 0 } }; -*/ #define GET_CHAR(awk) \ do { if (__get_char(awk) == -1) return -1; } while(0) @@ -373,6 +379,8 @@ int xp_awk_parse (xp_awk_t* awk) xp_awk_clear (awk); + if (__add_builtin_globals (awk) == XP_NULL) return -1; + GET_CHAR (awk); GET_TOKEN (awk); @@ -894,6 +902,44 @@ and merged to top-level block */ return (xp_awk_nde_t*)block; } +static xp_awk_t* __add_builtin_globals (xp_awk_t* awk) +{ + struct __bvent* p = __bvtab; + + while (p->name != XP_NULL) + { + if (__add_global (awk, p->name) == XP_NULL) return XP_NULL; + p++; + } + + return awk; +} + +static xp_awk_t* __add_global (xp_awk_t* awk, const xp_char_t* name) +{ + if (awk->opt.parse & XP_AWK_UNIQUE) + { + /* check if it conflict with a function name */ + if (xp_awk_map_get(&awk->tree.afns, name) != XP_NULL) + { + PANIC (awk, XP_AWK_EDUPNAME); + } + } + + /* check if it conflicts with other global variable names */ + if (xp_awk_tab_find(&awk->parse.globals, name, 0) != (xp_size_t)-1) + { + PANIC (awk, XP_AWK_EDUPVAR); + } + + if (xp_awk_tab_add(&awk->parse.globals, name) == (xp_size_t)-1) + { + PANIC (awk, XP_AWK_ENOMEM); + } + + return awk; +} + static xp_awk_t* __collect_globals (xp_awk_t* awk) { xp_char_t* global; @@ -907,25 +953,7 @@ static xp_awk_t* __collect_globals (xp_awk_t* awk) global = XP_STR_BUF(&awk->token.name); - if (awk->opt.parse & XP_AWK_UNIQUE) - { - /* check if it conflict with a function name */ - if (xp_awk_map_get(&awk->tree.afns, global) != XP_NULL) - { - PANIC (awk, XP_AWK_EDUPNAME); - } - } - - /* check if it conflicts with other global variable names */ - if (xp_awk_tab_find(&awk->parse.globals, global, 0) != (xp_size_t)-1) - { - PANIC (awk, XP_AWK_EDUPVAR); - } - - if (xp_awk_tab_add(&awk->parse.globals, global) == (xp_size_t)-1) - { - PANIC (awk, XP_AWK_ENOMEM); - } + if (__add_global (awk, global) == XP_NULL) return XP_NULL; if (__get_token(awk) == -1) return XP_NULL; @@ -2132,6 +2160,14 @@ static xp_awk_nde_t* __parse_primary_ident (xp_awk_t* awk) PANIC (awk, XP_AWK_ENOMEM); } + /* TODO: search in the builtin variable name list */ + /* + idxa = .... + if (idxa != (xp_size_t)-1) + { + } + */ + /* search the parameter name list */ idxa = xp_awk_tab_find(&awk->parse.params, name_dup, 0); if (idxa != (xp_size_t)-1) @@ -2250,6 +2286,14 @@ static xp_awk_nde_t* __parse_hashidx (xp_awk_t* awk, xp_char_t* name) PANIC (awk, XP_AWK_ENOMEM); } + /* TODO: search in the builtin variable name list */ + /* + idxa = xp_awk_tab_find (&awk->parse.params, name, 0); + if (idxa != (xp_size_t)-1) + { + } + */ + /* search the parameter name list */ idxa = xp_awk_tab_find (&awk->parse.params, name, 0); if (idxa != (xp_size_t)-1) diff --git a/ase/awk/run.c b/ase/awk/run.c index 056f2a49..bb23e1f5 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.129 2006-07-10 05:20:46 bacon Exp $ + * $Id: run.c,v 1.130 2006-07-10 14:28:45 bacon Exp $ */ #include @@ -80,10 +80,6 @@ static xp_awk_val_t* __do_assignment_map ( static xp_awk_val_t* __do_assignment_pos ( xp_awk_run_t* run, xp_awk_nde_pos_t* pos, xp_awk_val_t* val); -static int __recomp_record_fields ( - xp_awk_run_t* run, xp_size_t lv, - xp_char_t* str, xp_size_t len, int* errnum); - static xp_awk_val_t* __eval_binary ( xp_awk_run_t* run, xp_awk_nde_t* nde); static xp_awk_val_t* __eval_binop_lor ( @@ -165,6 +161,9 @@ static void __raw_pop_times (xp_awk_run_t* run, xp_size_t times); static int __read_record (xp_awk_run_t* run); static int __split_record (xp_awk_run_t* run); static void __clear_record (xp_awk_run_t* run, xp_bool_t noline); +static int __recomp_record_fields (xp_awk_run_t* run, + xp_size_t lv, xp_char_t* str, xp_size_t len, int* errnum); + static int __val_to_num (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r); static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde); @@ -1815,114 +1814,6 @@ static xp_awk_val_t* __do_assignment_pos ( return val; } -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_assert (lv > 0); - max = (lv > run->inrec.nflds)? lv: run->inrec.nflds; - - nflds = run->inrec.nflds; - if (max > run->inrec.maxflds) - { - void* tmp = xp_realloc ( - run->inrec.flds, xp_sizeof(*run->inrec.flds) * max); - if (tmp == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM); - - run->inrec.flds = tmp; - run->inrec.maxflds = max; - } - - lv = lv - 1; /* adjust the value to 0-based index */ - - xp_str_clear (&run->inrec.line); - - for (i = 0; i < max; i++) - { - if (i > 0) - { - /* TODO: use OFS */ - if (xp_str_ccat ( - &run->inrec.line, XP_T(' ')) == (xp_size_t)-1) - { - *errnum = XP_AWK_ENOMEM; - return -1; - } - } - - if (i == lv) - { - xp_awk_val_t* tmp; - - run->inrec.flds[i].ptr = - XP_STR_BUF(&run->inrec.line) + - XP_STR_LEN(&run->inrec.line); - run->inrec.flds[i].len = len; - - if (xp_str_ncat ( - &run->inrec.line, str, len) == (xp_size_t)-1) - { - *errnum = XP_AWK_ENOMEM; - return -1; - } - - tmp = xp_awk_makestrval (str,len); - if (tmp == XP_NULL) - { - *errnum = XP_AWK_ENOMEM; - return -1; - } - - if (i < nflds) - xp_awk_refdownval (run, run->inrec.flds[i].val); - else run->inrec.nflds++; - run->inrec.flds[i].val = tmp; - xp_awk_refupval (tmp); - } - else if (i >= nflds) - { - run->inrec.flds[i].ptr = - XP_STR_BUF(&run->inrec.line) + - XP_STR_LEN(&run->inrec.line); - run->inrec.flds[i].len = 0; - - if (xp_str_cat ( - &run->inrec.line, XP_T("")) == (xp_size_t)-1) - { - *errnum = XP_AWK_ENOMEM; - return -1; - } - - /*xp_awk_refdownval (run, run->inrec.flds[i].val);*/ - run->inrec.flds[i].val = xp_awk_val_zls; - xp_awk_refupval (xp_awk_val_zls); - run->inrec.nflds++; - } - else - { - xp_awk_val_str_t* tmp; - - tmp = (xp_awk_val_str_t*)run->inrec.flds[i].val; - - run->inrec.flds[i].ptr = - XP_STR_BUF(&run->inrec.line) + - XP_STR_LEN(&run->inrec.line); - run->inrec.flds[i].len = tmp->len; - - if (xp_str_ncat (&run->inrec.line, - tmp->buf, tmp->len) == (xp_size_t)-1) - { - *errnum = XP_AWK_ENOMEM; - return -1; - } - } - } - - return 0; -} - static xp_awk_val_t* __eval_binary (xp_awk_run_t* run, xp_awk_nde_t* nde) { static binop_func_t __binop_func[] = @@ -3800,6 +3691,8 @@ static int __split_record (xp_awk_run_t* run) xp_sizeof(*run->inrec.flds) * nflds); if (tmp == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM); + if (run->inrec.flds != NULL) + xp_free (run->inrec.flds); run->inrec.flds = tmp; run->inrec.maxflds = nflds; } @@ -3858,6 +3751,113 @@ static void __clear_record (xp_awk_run_t* run, xp_bool_t noline) if (!noline) xp_str_clear (&run->inrec.line); } +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_assert (lv > 0); + max = (lv > run->inrec.nflds)? lv: run->inrec.nflds; + + nflds = run->inrec.nflds; + if (max > run->inrec.maxflds) + { + void* tmp = xp_realloc ( + run->inrec.flds, xp_sizeof(*run->inrec.flds) * max); + if (tmp == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM); + + run->inrec.flds = tmp; + run->inrec.maxflds = max; + } + + lv = lv - 1; /* adjust the value to 0-based index */ + + xp_str_clear (&run->inrec.line); + + for (i = 0; i < max; i++) + { + if (i > 0) + { + /* TODO: use OFS */ + if (xp_str_ccat ( + &run->inrec.line, XP_T(' ')) == (xp_size_t)-1) + { + *errnum = XP_AWK_ENOMEM; + return -1; + } + } + + if (i == lv) + { + xp_awk_val_t* tmp; + + run->inrec.flds[i].ptr = + XP_STR_BUF(&run->inrec.line) + + XP_STR_LEN(&run->inrec.line); + run->inrec.flds[i].len = len; + + if (xp_str_ncat ( + &run->inrec.line, str, len) == (xp_size_t)-1) + { + *errnum = XP_AWK_ENOMEM; + return -1; + } + + tmp = xp_awk_makestrval (str,len); + if (tmp == XP_NULL) + { + *errnum = XP_AWK_ENOMEM; + return -1; + } + + if (i < nflds) + xp_awk_refdownval (run, run->inrec.flds[i].val); + else run->inrec.nflds++; + run->inrec.flds[i].val = tmp; + xp_awk_refupval (tmp); + } + else if (i >= nflds) + { + run->inrec.flds[i].ptr = + XP_STR_BUF(&run->inrec.line) + + XP_STR_LEN(&run->inrec.line); + run->inrec.flds[i].len = 0; + + if (xp_str_cat ( + &run->inrec.line, XP_T("")) == (xp_size_t)-1) + { + *errnum = XP_AWK_ENOMEM; + return -1; + } + + /*xp_awk_refdownval (run, run->inrec.flds[i].val);*/ + run->inrec.flds[i].val = xp_awk_val_zls; + xp_awk_refupval (xp_awk_val_zls); + run->inrec.nflds++; + } + else + { + xp_awk_val_str_t* tmp; + + tmp = (xp_awk_val_str_t*)run->inrec.flds[i].val; + + run->inrec.flds[i].ptr = + XP_STR_BUF(&run->inrec.line) + + XP_STR_LEN(&run->inrec.line); + run->inrec.flds[i].len = tmp->len; + + if (xp_str_ncat (&run->inrec.line, + tmp->buf, tmp->len) == (xp_size_t)-1) + { + *errnum = XP_AWK_ENOMEM; + return -1; + } + } + } + + return 0; +} + static int __val_to_num (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r) { if (v->type == XP_AWK_VAL_NIL) diff --git a/ase/test/awk/t14.awk b/ase/test/awk/t14.awk index 7c7e2ca5..ac490fa9 100644 --- a/ase/test/awk/t14.awk +++ b/ase/test/awk/t14.awk @@ -17,19 +17,22 @@ END print "\a"; } */ -{ - //$3 = "10"; - //print $0, $2, $5; - //print $0 = "wow this is just a test"; - //print $0 = ""; - //print $0; - $7="abcdefghi"; - print a = $6; - print "[" $0 "],[" a "]"; +global x, y; +{ + for (i = 0; i < 10; i++) + { + print $i; + if (i == 3) a = $i; + } /* - print $6; + $1 = $2; + $3 = $2; + $2 = $2; + print $0; */ print "--------------------"; + print NF; + print "===================="; } diff --git a/ase/test/awk/t3.awk b/ase/test/awk/t3.awk index af249217..39f27056 100644 --- a/ase/test/awk/t3.awk +++ b/ase/test/awk/t3.awk @@ -1,4 +1,4 @@ -/*global x, j;*/ +//global x, j; func func1 (x) {