From 38ff71c2f21b79867109fd40a326dc78d5a4ad5f Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 19 Apr 2006 03:42:08 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/map.c | 25 ++++++++++--------------- ase/awk/parse.c | 20 ++++++++++++++------ ase/awk/run.c | 42 +++++++----------------------------------- ase/test/awk/t2.awk | 4 ++-- 4 files changed, 33 insertions(+), 58 deletions(-) diff --git a/ase/awk/map.c b/ase/awk/map.c index 945dedeb..1b9259eb 100644 --- a/ase/awk/map.c +++ b/ase/awk/map.c @@ -1,5 +1,5 @@ /* - * $Id: map.c,v 1.12 2006-04-18 14:49:42 bacon Exp $ + * $Id: map.c,v 1.13 2006-04-19 03:42:08 bacon Exp $ */ #include @@ -143,7 +143,8 @@ xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* val) return px; } -int xp_awk_map_putx (xp_awk_map_t* map, xp_char_t* key, void* val, xp_awk_pair_t** px) +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* pair; xp_size_t hc; @@ -155,12 +156,6 @@ int xp_awk_map_putx (xp_awk_map_t* map, xp_char_t* key, void* val, xp_awk_pair_t { if (xp_strcmp(pair->key,key) == 0) { - if (pair->key != key) - { - xp_free ((xp_char_t*)pair->key); - pair->key = key; - } - if (px != XP_NULL) *px = xp_awk_map_setpair (map, pair, val); else @@ -174,7 +169,13 @@ int xp_awk_map_putx (xp_awk_map_t* map, xp_char_t* key, void* val, xp_awk_pair_t pair = (xp_awk_pair_t*) xp_malloc (xp_sizeof(xp_awk_pair_t)); if (pair == XP_NULL) return -1; /* error */ - pair->key = key; + /*pair->key = key;*/ + pair->key = xp_strdup (key); /* duplicate the key if it is new */ + if (pair->key == XP_NULL) + { + xp_free (pair); + return -1; /* error */ + } pair->val = val; pair->next = map->buck[hc]; map->buck[hc] = pair; @@ -196,12 +197,6 @@ xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* val) { 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, val); } pair = pair->next; diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 80c48c98..8c97172a 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.84 2006-04-18 16:04:54 bacon Exp $ + * $Id: parse.c,v 1.85 2006-04-19 03:42:08 bacon Exp $ */ #include @@ -412,6 +412,8 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk) xp_awk_nde_t* body; xp_awk_func_t* func; xp_size_t nargs; + xp_awk_pair_t* pair; + int n; /* eat up the keyword 'function' and get the next token */ if (__get_token(awk) == -1) return XP_NULL; @@ -582,8 +584,8 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk) return XP_NULL; } -/* TODO: consider if the parameters should be saved for some reasons.. */ - nargs = xp_awk_tab_getsize(&awk->parse.params); +/* TODO: consider if the parameter names should be saved for some reasons.. */ + nargs = xp_awk_tab_getsize (&awk->parse.params); /* parameter names are not required anymore. clear them */ xp_awk_tab_clear (&awk->parse.params); @@ -595,12 +597,12 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk) return XP_NULL; } - func->name = name_dup; + func->name = XP_NULL; /* function name set below */ func->nargs = nargs; func->body = body; - xp_assert (xp_awk_map_get(&awk->tree.funcs, name_dup) == XP_NULL); - if (xp_awk_map_put(&awk->tree.funcs, name_dup, func) == XP_NULL) + n = xp_awk_map_putx (&awk->tree.funcs, name_dup, func, &pair); + if (n < 0) { xp_free (name_dup); xp_awk_clrpt (body); @@ -608,6 +610,12 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk) PANIC (awk, XP_AWK_ENOMEM); } + /* duplicate functions should have been detected previously */ + xp_assert (n != 0); + + func->name = pair->key; /* do some trick to save a string. */ + xp_free (name_dup); + return body; } diff --git a/ase/awk/run.c b/ase/awk/run.c index fd6883ea..c465c596 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.59 2006-04-18 16:04:58 bacon Exp $ + * $Id: run.c,v 1.60 2006-04-19 03:42:08 bacon Exp $ */ #include @@ -761,32 +761,11 @@ static xp_awk_val_t* __do_assignment ( if (var->type == XP_AWK_NDE_NAMED) { - xp_awk_pair_t* pair; - xp_char_t* name; + int n; - pair = xp_awk_map_get (&awk->run.named, var->id.name); - if (pair == XP_NULL) - { - name = xp_strdup (var->id.name); - if (name == XP_NULL) - { - xp_awk_freeval (awk, val); - awk->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - } - else - { - name = pair->key; - } - - if (xp_awk_map_put(&awk->run.named, name, val) == XP_NULL) - { - xp_free (name); - xp_awk_freeval (awk, val); - awk->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } + n = xp_awk_map_putx ( + &awk->run.named, var->id.name, val, XP_NULL); + if (n < 0) PANIC (awk, XP_AWK_ENOMEM); xp_awk_refupval (val); } @@ -912,21 +891,14 @@ static xp_awk_val_t* __do_assignment_map ( /* xp_printf (XP_TEXT("**** index str=>%s, map->ref=%d, map->type=%d\n"), str, map->ref, map->type); */ - n = xp_awk_map_putx(map->map, str, val, XP_NULL); + n = xp_awk_map_putx (map->map, str, val, XP_NULL); if (n < 0) { xp_free (str); PANIC (awk, XP_AWK_ENOMEM); } - if (n == 0) - { - /* the value for the existing key has changed. - * str is freed only if the key is in the map. - * otherwise, it will be taken by the map */ - xp_free (str); - } - + xp_free (str); xp_awk_refupval (val); return val; } diff --git a/ase/test/awk/t2.awk b/ase/test/awk/t2.awk index 969f0fae..c9accd00 100644 --- a/ase/test/awk/t2.awk +++ b/ase/test/awk/t2.awk @@ -17,8 +17,8 @@ function sum (i) BEGIN { - x = sum (10000000); - //s = sum (100); + //x = sum (10000000); + x = sum (100); s = x; ss = z; }