bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: parameter type is incorrectly reported by typeof()


From: arnold
Subject: Re: parameter type is incorrectly reported by typeof()
Date: Tue, 16 Nov 2021 01:38:22 -0700
User-agent: Heirloom mailx 12.5 7/5/10

Hi.

In the original program (courtesy of gawk --pretty):

BEGIN {
        _a()    # define parameter as unassigned
        mode = 1
        print
        print
        print
        _a()    # define parameter as an array
}


function _a(a)
{
        print "a: " typeof(a)
        _b(a)
        print "a: " typeof(a)
}

function _b(b)
{
        _c(b)
        print "b: " typeof(b)
}

function _c(c, x)
{
        if (mode) {
                "" in c
        } else {
                x = c
        }
        print "c: " typeof(c)
}

In a(), the `a' is untyped the first time since it was never used.

When the `x = c' happens in c(), gawk has to go all the way back up
the call chain to find the original source of the `c' parameter.

That forces the orignal 'a' to become a scalar (since it's value
is fetched), turning it into unassigned the second time typeof is
called.

Now:

> I'm not sure there isn't something weird here, but I could be confused.
> Consider the two attached test scripts using gawk-5.1-stable:
>
> bash-4.2$ cat /tmp/test.awk 
> BEGIN {
>    print typeof(x)
>    y = x
>    print typeof(x)
> }
>
> bash-4.2$ ./gawk -f /tmp/test.awk 
> untyped
> unassigned

Same deal here. `x' is fetched, turning it into a scalar.

> bash-4.2$ cat /tmp/test1.awk 
> function f(x) {
>    print typeof(x)
>    y = x
>    print typeof(x)
> }
>
> BEGIN {
>    print typeof(x)
>    f(x)
> }
>
> bash-4.2$ ./gawk -f /tmp/test1.awk 
> untyped
> untyped
> untyped
>
> So in the 2nd program, inside the function f, why doesn't assigning
> 'y = x' force x to have a scalar value and show as untyped instead
> of unassigned as it does in the first program?

The first 'untyped' is from the BEGIN and is correct. The second
one is also correct.  The third one is what you're asking about.

I think this is because `x' in the function comes from Node_param_list,
and when a value is fetched, it gets the null string.  Maybe the
type here should also be turned into unassigned.  It's very subtle
though and needs looking at in a debugger.

If I have some time I'll look at it.  Or feel free to beat me to it.

Thanks,

Arnold



reply via email to

[Prev in Thread] Current Thread [Next in Thread]