1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
CPPAWK-FUN(1) Indirect Functions CPPAWK-FUN(1)
NAME
fun - indirect function macros for cppawk (requiring GNU Awk)
SYNOPSIS
#include <fun.h>
fun(fname) // create indirect function
bind(fname, env) // create fun, binding left arg
call(fun, ...) // call indirect function
DESCRIPTION
The <fun.h> header provides some abstraction over GNU Awk's indirect functions. "Native"
GNU Awk indirect functions are character strings which hold a function name. These can be
invoked using the @ operator. The macros in this header deal with two function representa-
tions: a function is either the native GNU Awk one (name string) or else a cons cell which
holds the name string in the car field and a bound argument in the cdr field.
The fun macro takes an identifier as its argument and produces a function. This function
may be invoked using the call macro, whose first argument is a function, and the remaining
arguments are passed to that function.
The bind macro creates an indirect function whose first parameter is bound to the value of
bind's env argument. When this function is invoked using call the env value will be passed
as the leftmost argument of the function; the arguments specified in call will be the re-
maining arguments.
Example
#include <fun.h>
function add(x, y)
{
return x + y
}
BEGIN {
fn = bind(3, add)
print call(fn, 4) // prints 7
}
See the documentation for mapcar and mappend in the cppawk-cons manual page for more exam-
ples of bind as well as fun.
SEE ALSO
cppawk(1), cppawk-cons(1)
BUGS
A function name which starts with capital C looks like a cons cell. So that is to say
fun(CreateProcess) will misbehave by yielding the unboxed string "CreateProcess". Then
call tests this unboxed string object with consp which falsely identifies it as a cons
cell, mistaking the C for the type code of a boxed object. This issue could be addressed
by having fun return a boxed string; but then unboxing has to be done to call the function
using the GNU Awk @ operator.
The GNU Awk indirect function implementation has been around since GNU Awk 4.0, but seri-
ous bugs in the implementation were not fixed until after 5.1.1 release. As of Gawk 5.1.1,
indirection did not work correctly on built-in functions as, and nesting of indirect func-
tion calls was broken, as in @foo(@bar(arg,...)).
AUTHOR
Kaz Kylheku <kaz@kylheku.com>
COPYRIGHT
Copyright 2022, BSD2 License.
cppawk Libraries 19 April 2022 CPPAWK-FUN(1)
|