summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--stdlib/awk.tl53
-rw-r--r--txr.131
2 files changed, 56 insertions, 28 deletions
diff --git a/stdlib/awk.tl b/stdlib/awk.tl
index 09065bf7..e6ce26f9 100644
--- a/stdlib/awk.tl
+++ b/stdlib/awk.tl
@@ -56,7 +56,7 @@
self.output stream)))))
(defstruct sys:awk-compile-time ()
- inputs output name lets
+ inputs output name lets funs
begin-file-actions end-file-actions
begin-actions end-actions
cond-actions
@@ -318,6 +318,7 @@
(awk-error "bad :name syntax"))
(set awc.name (car actions)))
(:let (push actions awc.lets))
+ (:fun (push actions awc.funs))
(:begin (push actions awc.begin-actions))
(:set (push ^((set ,*actions)) awc.begin-actions))
(:end (push actions awc.end-actions))
@@ -349,6 +350,7 @@
awc.cond-actions))))
(junk (awk-error "bad clause syntax ~s" junk))))
(set awc.lets [apply append (nreverse awc.lets)]
+ awc.funs [apply append (nreverse awc.funs)]
awc.begin-actions [apply append (nreverse awc.begin-actions)]
awc.end-actions [apply append (nreverse awc.end-actions)]
awc.begin-file-actions [apply append (nreverse awc.begin-file-actions)]
@@ -552,27 +554,28 @@
(sys:awk-mac-let ,awc ,aws-sym
(sys:awk-fun-let ,aws-sym
(sys:awk-symac-let ,awc
- (let* (,*(if awc.output
- ^((*stdout* (qref ,aws-sym output))))
- ,*(if (and awc.cond-actions awc.begin-file-actions)
- ^((,awk-begf-fun (lambda (,aws-sym)
- ,*awc.begin-file-actions))))
- ,*(if (and awc.cond-actions awc.end-file-actions)
- ^((,awk-endf-fun (lambda (,aws-sym)
- ,*awc.end-file-actions))))
- ,*(if (or awc.cond-actions awc.begin-file-actions
- awc.end-file-actions awc.end-actions)
- ^((,awk-fun (lambda (,aws-sym)
- ,p-actions-xform)))))
- ,*awc.begin-actions
- (unwind-protect
- ,(if (or awc.cond-actions awc.begin-file-actions
- awc.end-file-actions awc.end-actions)
- ^(qref ,aws-sym (loop ,awk-fun
- ,(if awc.begin-file-actions
- awk-begf-fun)
- ,(if awc.end-file-actions
- awk-endf-fun))))
- (set ,awk-retval (progn ,*awc.end-actions))
- (call-finalizers ,aws-sym))
- ,awk-retval))))))))))))
+ (labels ,awc.funs
+ (let* (,*(if awc.output
+ ^((*stdout* (qref ,aws-sym output))))
+ ,*(if (and awc.cond-actions awc.begin-file-actions)
+ ^((,awk-begf-fun (lambda (,aws-sym)
+ ,*awc.begin-file-actions))))
+ ,*(if (and awc.cond-actions awc.end-file-actions)
+ ^((,awk-endf-fun (lambda (,aws-sym)
+ ,*awc.end-file-actions))))
+ ,*(if (or awc.cond-actions awc.begin-file-actions
+ awc.end-file-actions awc.end-actions)
+ ^((,awk-fun (lambda (,aws-sym)
+ ,p-actions-xform)))))
+ ,*awc.begin-actions
+ (unwind-protect
+ ,(if (or awc.cond-actions awc.begin-file-actions
+ awc.end-file-actions awc.end-actions)
+ ^(qref ,aws-sym (loop ,awk-fun
+ ,(if awc.begin-file-actions
+ awk-begf-fun)
+ ,(if awc.end-file-actions
+ awk-endf-fun))))
+ (set ,awk-retval (progn ,*awc.end-actions))
+ (call-finalizers ,aws-sym))
+ ,awk-retval)))))))))))))
diff --git a/txr.1 b/txr.1
index f1d5d6ea..41177693 100644
--- a/txr.1
+++ b/txr.1
@@ -67897,6 +67897,31 @@ and ordinary clauses, it will be shadowed by the
variable
.codn fs ,
which holds the field-separator regular expression or string.
+
+.meIP (:fun >> {( name < param-list << function-body-form *)}*)
+The
+.code :fun
+clause introduces named functions which are visible inside the
+.code awk
+form, as if bound by a
+.code labels
+operator. Variables defined by
+.code :let
+are visible to these named functions. The reverse is not true: the
+functions are not visible to the
+.metn init-form s
+of the
+.code :let
+clause. This is regardless of the order of appearance of the
+.code :let
+and
+.code :fun
+clauses in the
+.code awk
+macro. Furthermore, functions defined by
+.code :fun
+may refer to awk macros, functions and variables.
+
.meIP (:inputs << source-form *)
The
.code :inputs
@@ -69719,10 +69744,10 @@ previously associated stream is used.
The scope of these macros is the entire containing
.code awk
form; they may be used in the
-.metn init-form s
-of the
.code :let
-clause.
+and
+.code :fun
+clauses.
The
.code ->