[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: do_lint `delete`
From: |
Aharon Robbins |
Subject: |
Re: do_lint `delete` |
Date: |
Thu, 18 Jul 2002 00:57:57 +0300 |
Greetings. Re all this:
> Date: Tue, 16 Jul 2002 16:15:01 +0300
> From: Aharon Robbins <address@hidden>
> To: address@hidden
> Subject: Re: do_lint `delete`
> Cc: address@hidden
>
> > > > `delete foo[iggy]' should probably be caught by --lint
> > > > (line 528 of awkgram.y)
> > > >
> > > > %>gawk --lint 'BEGIN { delete array; }'
> > > > dgawk: cmd. line:1: warning: `delete array' is a gawk extension
> > > > %>gawk --lint 'BEGIN { delete array[1]; }'
> > > > %>
> > >
> > > What is the error that --lint should catch? It is not an error to delete
> > > an element that isn't there. If you think it ought to be a warning,
> > > there might be room to discuss that.
> > >
> > > Arnold
> >
> > i'm not adamant about this, i just figured it should generate the same
> > warning.
>
> It should generate a "1 is not an element in array" warning, not that
> it's an extension, but that's just a quibble. What's strange is that
> there is a piece of code alreay in place that checks exactly for this
> and is supposed to print a lint message. I'll have to see why it's
> not working. Hmm. I think it's because the variable hasn't been
> used as an array before:
>
> $ gawk --lint 'BEGIN { a[1] = 2 ; delete a[2] }'
> gawk: cmd. line:1: warning: delete: index `2' not in array `a'
>
> Yep, that's it. I'll try to fix the code and post a patch so that
> your case also generates a warning.
>
> > on another note, what is the prominent gawk author's thoughts on 'true'
> > multi-dimensional arrays. I'm interested both in (g)awk philosophy and
> > technically (what would it take to implement)?
>
> "Prominent"? Tee here. You're not famous if you can go grocery shopping
> without hordes of teenage girls throwing themselves at you. :-)
>
> Technically, getting true multidimensional arrays to work in gawk would
> probably be painful. It also introduces very real portability and
> standards-compliance issues. I'm not terribly interested in trying,
> since I've (about) hit the limit on the kinds of things I want to
> continue trying to shoehorn into gawk.
>
> (This is not to say it wouldn't be nice to have, just that it's too late
> both language-wise and implementation-wise to do this.)
>
> Thanks,
>
> Arnold
Here is the patch. It also includes a fix to make sure that subscripts
are always evaluated, in case there are side effects. Enjoy.
Arnold
*** ../gawk-3.1.1/array.c Tue Apr 16 07:40:06 2002
--- array.c Tue Jul 16 09:17:17 2002
***************
*** 352,357 ****
--- 352,362 ----
/* do_delete --- perform `delete array[s]' */
+ /*
+ * `symbol' is array
+ * `tree' is subscript
+ */
+
void
do_delete(NODE *symbol, NODE *tree)
{
***************
*** 359,374 ****
register NODE *bucket, *last;
NODE *subs;
if (symbol->type == Node_param_list) {
symbol = stack_ptr[symbol->param_cnt];
! if (symbol->type == Node_var)
return;
}
if (symbol->type == Node_array_ref)
symbol = symbol->orig_array;
if (symbol->type == Node_var_array) {
! if (symbol->var_array == NULL)
return;
} else
fatal(_("delete: illegal use of variable `%s' as array"),
symbol->vname);
--- 364,402 ----
register NODE *bucket, *last;
NODE *subs;
+ /*
+ * Evaluate subscript first, always, in case there are
+ * side effects.
+ */
+ if (tree != NULL)
+ subs = concat_exp(tree); /* concat_exp returns string
node */
+ else
+ subs = NULL;
+
if (symbol->type == Node_param_list) {
symbol = stack_ptr[symbol->param_cnt];
! if (symbol->type == Node_var) {
! if (subs != NULL) {
! if (do_lint)
! lintwarn(_("delete: index `%s' not in
array `%s'"),
! subs->stptr, symbol->vname);
! free_temp(subs);
! }
return;
+ }
}
if (symbol->type == Node_array_ref)
symbol = symbol->orig_array;
if (symbol->type == Node_var_array) {
! if (symbol->var_array == NULL) {
! if (subs != NULL) {
! if (do_lint)
! lintwarn(_("delete: index `%s' not in
array `%s'"),
! subs->stptr, symbol->vname);
! free_temp(subs);
! }
return;
+ }
} else
fatal(_("delete: illegal use of variable `%s' as array"),
symbol->vname);
***************
*** 378,384 ****
return;
}
- subs = concat_exp(tree); /* concat_exp returns string node */
hash1 = hash(subs->stptr, subs->stlen, (unsigned long)
symbol->array_size);
last = NULL;
--- 406,411 ----