bug-gawk
[Top][All Lists]
Advanced

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

[PATCH] Feature: parameters declared in body


From: Kaz Kylheku
Subject: [PATCH] Feature: parameters declared in body
Date: Thu, 07 Apr 2022 11:28:54 -0700
User-agent: Roundcube Webmail/1.4.13

Hi,

I'm posting this as a proposal. If this is accepted, in some
shape, then of course there will be test cases and
documentation.

The idea is that there is a new kind of attribute syntax
whereby, inside a function, if we put the @param: prefix
before a variable, that variable is added as a parameter
to the surrounding function, which has the effect of making
it a local variable.

This effectively extends the Awk language with a nice way
of defining locals, that is easy to use from code generators
and macro preprocessors, and is based in the existing model
of Awk local variables being parameters.

A bit of a cautionary word: this relies on knowing the order
in which things are parsed.  If a language construct mentions
a certain variable several times, and one of those mentions
has a @parm: prefix, that mention had better be processed by
the parser before the others, otherwise those others will be
compiled in a scope in which the variable was not defined
as a parameter yet.

For instance if we write:

  for (@param:i = 0; i < 10; i++)

we are relying on the parser processing @param:i
before i < 10 or i+i. We can infer that from knowing
the phrase structure rule, and how it is processed by the
LALR(1) algorithm.


Cheers ...

P.S. Small demo showing how the variables are local:

$ ./gawk -d '
function add(n, m) {
  for (@param:i = n; i <= m; i++)
     @param:sum += i;
  return sum
}

BEGIN { print add(2, 5); }'
14
$ grep '^[a-z]' awkvars.out
$

Now remove @param:

$ ./gawk -d '
function add(n, m) {
  for (i = n; i <= m; i++)
    sum += i;
  return sum
}

BEGIN { print add(2, 5); }'
14
$ grep '^[a-z]' awkvars.out
i: 6
sum: 14


Argument passing works with the @param variables:

$ ./gawk -d '
function add() {
  return @param:a + @param:b
}

BEGIN { print add(2, 5); }
'
7

I almost feel this just wants to be @:a + @:b.



Attachment: 0001-Feature-parameters-declared-in-body.patch
Description: Text Data


reply via email to

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