[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bug with PROCINFO["identifiers"]
From: |
arnold |
Subject: |
Re: Bug with PROCINFO["identifiers"] |
Date: |
Fri, 04 Sep 2020 04:21:36 -0600 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Hi.
I took a harder look at this. It's a difficult problem since awk
is so dynamic. For example:
$ cat y.awk
BEGIN {
if (a)
foo(b)
else
bar(b)
print typeof(b)
}
function foo(b)
{
b[1] = 1
}
function bar(b)
{
b++
}
$ ./gawk -f y.awk -v a=1
array
$ ./gawk -f y.awk -v a=0
unassigned
I can improve the handling for arrays, but that's about it.
Thanks,
Arnold
---------------------------------------
diff --git a/awkgram.y b/awkgram.y
index cfc12e78..72d79312 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -2147,7 +2147,7 @@ simple_variable
{
char *arr = $1->lextok;
- $1->memory = variable($1->source_line, arr, Node_var_new);
+ $1->memory = variable($1->source_line, arr, Node_var_array);
$1->opcode = Op_push_array;
$$ = list_prepend($2, $1);
}
arnold@skeeve.com wrote:
> Hello.
>
> Thanks for taking the time to send in a bug report.
>
> anoncoward--- via "Bug reports and all discussion about gawk."
> <bug-gawk@gnu.org> wrote:
>
> > If one has a user defined variable (scalar or array),
> > PROCINFO["identifiers"] always returns ``untyped''
> > even if the type is known.
> >
> > (Hushmail will wrap the lines. My apologies.)
> >
> > $ LC_ALL=C ./gawk 'BEGIN {s="a"; i=1; ar[1]=3; print
> > PROCINFO["identifiers"]["s"], PROCINFO["identifiers"]["i"],
> > PROCINFO["identifiers"]["ar"] }'
> > untyped untyped untyped
> >
> > but the program obviously knows that s and i are scalars and ar is
> > an array. For example,
> >
> > $ LC_ALL=C ./gawk 'BEGIN {s="a"; i=1; ar[1]=3; print typeof(s),
> > typeof(i), typeof(ar) }'
> > string number array
> >
> > Per your request, I compiled the latest version of gawk and tested
> > it.
>
> PROCINFO["identifiers"] is built after the program is parsed but
> before it starts to run. typeof is called during execution, when
> the variables types have settled down, so it "knows" more.
>
> The manual is pretty clear about this.
>
> But that said, it may be possible to make gawk a little bit smarter
> about this, so that it says "untyped" in fewer instances. I will
> take a look at it.
>
> Thanks,
>
> Arnold