[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] typeof() array subscripts
From: |
Aharon Robbins |
Subject: |
Re: [bug-gawk] typeof() array subscripts |
Date: |
Sun, 21 Jun 2015 22:13:47 +0300 |
User-agent: |
Heirloom mailx 12.5 6/20/10 |
Hi All.
> Date: Sun, 21 Jun 2015 09:55:19 -0400
> From: "Andrew J. Schorr" <address@hidden>
> To: Hermann Peifer <address@hidden>
> Cc: "address@hidden" <address@hidden>
> Subject: Re: [bug-gawk] typeof() array subscripts
>
> On Sun, Jun 21, 2015 at 01:52:46PM +0200, Hermann Peifer wrote:
> > Do I also see it right that typeof()'s "untyped" actually means "untyped
> > scalar" whereas the debugger's "untyped variable" is a different animal, as
> > it can turn into an array?
>
> That appears to be the case:
That isn't supposed to be the case. Untyped should be "no type yet" for both.
> bash-4.2$ gawk 'BEGIN {print typeof(x)}'
> untyped
> bash-4.2$ gawk 'BEGIN {print typeof(x); x[1] = 3; print typeof(x)}'
> untyped
> gawk: cmd. line:1: fatal: attempt to use scalar `x' as an array
>
> It seems that the act of passing 'x' to typeof is instantiating it
> as a scalar. The act of inspecting it changes the result, unfortunately.
This is now fixed and pushed.
This is a REALLY dark corner in the language. It comes from asking the
question, "what does it mean to pass an untyped variable to a built-in
function?"
In this case:
BEGIN { n = split("1 2 3", a, " ")
a = 4 # obvious error here
}
It's obvious that `a' should be turned into an array. What about
BEGIN { n = substr(s, 2, 1)
s[1] = 4 # probably obvious error here
}
The first use of `s' is as a scalar. So it makes sense to force it to
be a scalar. But different awks disagree:
$ for i in nawk mawk mksawk "busybox awk" gawk-4.1.3
> do echo === $i
> $i 'BEGIN { n = substr(s, 2, 1) ; s[1] = 1 }'
> done
=== nawk
nawk: can't read value of s; it's an array name.
source line number 1
=== mawk
mawk: line 1: illegal reference to variable s
=== mksawk
=== busybox awk
=== gawk-4.1.3
gawk-4.1.3: cmd. line:1: fatal: attempt to use scalar `s' as an array
(Really interesting - BWK awk thinks that `s' is an array and is unhappy
with the call to substr!) mawk and gawk agree that `s' becomes scalar.
What was happening was that typeof was being treated like any other
built-in and turning its untyped argument into a scalar. I added some
special case code for typeof to leave things alone and some more tests
to the test suite.
Thanks for the help in nailing this one down.
I'll work on the debugger issue with @/.../ later.
Thanks,
Arnold