On 2020-06-11, Manuel Collado <
m-co...@users.sourceforge.net> wrote:
> El 10/06/2020 a las 20:38, Kaz Kylheku escribió:
>> [...]
>> Assignments to nonexistent fields change NF, so with fields we can
>> create trouble of the following sort, and its ilk:
>>
>> # Suppose NF is initially 1: there is one field.
>>
>> $(++NF + 1) = "foo"
>>
>> What fields now exist, with what values, and what is NF?
>>
>> ++NF yields 2 and so $(1 + 2), namely $3, ends up with "foo".
>>
>> But we have two competing modifications of NF in the same expression:
>> The assignment to $(3) wants to implicitly replace NF with 3.
>> The ++NF increment wants to replace the old value 1 with 2.
>> This cannot be well-defined.
>
> Why not? In imperative programming a basic rule is that function
> arguments are evaluated before calling the function.
I don't see a function there, just special syntax. we don't know when
the ++NF side effect completes in relation to the $(...) assignment
updating NF.
> And operators are
> in fact functions, with a more eye-catching syntax.
We do not have this sort of assurance in Awk and C.
C certainly doesn't say anything such as that the + operator is actually
a function with syntactic sugar; if that were the case, a sequence point
would have to occur before the call to the function.
In C++, a use of + is a function if it resolves to one via overloading,
otherwise it isn't.
Not even in Lisp, which has special operators, but they do not have an
eye-catching syntax at all; you can't always tell them apart from
functions by that alone.
> Rewriting the given
> sentence in a functional style we have:
>
> assign(field(add(incr(NF),1)),"foo")
I hope you don't mean to insinuate that incr(NF) is a function just
because you've made it resemble one using f(x) notation. It has to
operate on the identifier NF, rather than its value!
But, yes, if we think of this as C, *if* assign is an actual function
and not a macro expanding into code, then there is a sequence point
before the function is invoked, whereupon the ++NF side effect is
settled. Thus the function sees the new value of NF reliably and updates
that.
> And the strict evaluation order is
> ++ + $ =
The + operation certainly cannot proceed until it has both its operands,
one of which is the value from the ++ operator. So the ++ operation has
to be carried to sufficient completion to yield that value. However,
the side effect of the ++ can occur anywhere between the previous and
next sequence point.
If add is really a function, then the same reasoning as above applies; a
sequence point occurs after the evaluation of the arguments just before
the function call. + doesn't correspond to an add function though. It
isn't specified that way in C, and the Awk documentation in POSIX defers
to ISO C in these matters.
In Lisp terminology, the C + is a "special operator". (Or, rather,
a special token in the read syntax which denotes an internal special
operator). In a "Lispified" C, we wouldn't see a visual difference.
Whether a function or special operator, the invocation of + would look
like (+ a b). Our best bet would be to check the refernce manual.
There would be other clues, like there not being a function binding
for +, such that attempts to indirect upon + like (apply + 1 2) would
fail. Sneakily, apply could be an operator which makes it work, but
unlikely for (let ((fun +)) (apply fun 1 2)).
--
TXR Programming Lanuage:
http://nongnu.org/txr
Music DIY Mailing List:
http://www.kylheku.com/diy
ADA MP-1 Mailing List:
http://www.kylheku.com/mp1