On 2022-04-13, Janis Papanagnou <
[email protected]> wrote:
On 13.04.2022 03:50, Kaz Kylheku wrote:
I have started a repository called Enhanced GNU Awk.
https://www.kylheku.com/cgit/egawk/about/
This adds true local variables to Gawk, in the form of a @let
construct. @let is block scoped, not function scoped, and
may be used outside of functions, e.g.
BEGIN {
@let (here_only = 42) {
print here_only
}
}
I need this for the cppawk project, which has macros. macros sometimes
need hidden temporary variables. It's very bad if these are global.
Understood.
Are GNU Awk Namespaces[*] not sufficient to address the problem?
For the above specific example situation (module variables), yes.
For macros, no.
I'm already using a portable namespacing mechanism which is to prepend two underscores on all private symbols, including macro-generated temporaries.
GNU Awk prohibits namespaced identifiers from being used as function parameters, so all namespaced variables are necessarily global.
(I preserved this restriction in @let: it won't allow names like
foo::bar).
At first I implemented a @local mechanism which had function-wide scope
for variables. That was less work, but it doesn't solve the problem in situations when constructs nest and happen to choose the same variable.
In the C preprocessor, the options are very limited for generating
temporary names. You're looking at something like
#define __temp(pfx) __ ## pfx ## __LINE__
The __LINE__ is derived from the top-level macro invocation. So
if you have a bunch of nested syntax in that macro call which contains __temp(x), all the __temp(x) will generate to the same thing: __x42 if the macro is called on line 42.
With true lexical scope, this becomes a non-issue. E.g.
if we generate, say, some nested loops like this, we have a problem:
for (__x42 = 0; __x42 < ...;__x42++)
for (__x42 = 0; __x42 < ...; __x42++)
but it goes away if we add @let:
@let (__x42) for (__x42 = 0; __x42 < ...; __x42++)
@let (__x42) for (__x42 = 0; __x42 < ...; __x42++)
The reuse of __x42 and _n42 isn't a problem now (with certain
additional care).
--
TXR Programming Language:
http://nongnu.org/txr
Cygnal: Cygwin Native Application Library:
http://kylheku.com/cygnal
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)