diff options
-rw-r--r-- | autoload.c | 1 | ||||
-rw-r--r-- | stdlib/awk.tl | 6 | ||||
-rw-r--r-- | stdlib/doc-syms.tl | 1 | ||||
-rw-r--r-- | tests/015/awk-res.tl | 8 | ||||
-rw-r--r-- | txr.1 | 44 |
5 files changed, 58 insertions, 2 deletions
@@ -352,6 +352,7 @@ static val awk_set_entries(val fun) lit("<-"), lit("!>"), lit("<!"), lit("prn"), lit("i"), lit("o"), lit("x"), lit("b"), lit("c"), lit("r"), lit("iz"), lit("oz"), lit("xz"), lit("bz"), lit("cz"), lit("rz"), + lit("res"), nil }; autoload_sys_set(al_struct, sys_sname, fun); diff --git a/stdlib/awk.tl b/stdlib/awk.tl index 87fe96dd..e966b1d1 100644 --- a/stdlib/awk.tl +++ b/stdlib/awk.tl @@ -520,8 +520,10 @@ (defmacro awk (:env outer-env . clauses) (let ((awc (sys:awk-expander outer-env clauses))) (with-gensyms (aws-sym awk-begf-fun awk-fun awk-endf-fun awk-retval) - (let* ((p-actions-xform-unex (mapcar (aret ^(when (sys:awk-test ,@1 rec) - ,*@rest)) + (let* ((p-actions-xform-unex (mapcar (aret + ^(whenlet ((res (sys:awk-test ,@1 + rec))) + ,*@rest)) awc.cond-actions)) (p-actions-xform (expand ^(sys:awk-mac-let ,awc ,aws-sym diff --git a/stdlib/doc-syms.tl b/stdlib/doc-syms.tl index 1ca8ce22..035b483f 100644 --- a/stdlib/doc-syms.tl +++ b/stdlib/doc-syms.tl @@ -1622,6 +1622,7 @@ ("replace-tree-iter" "N-01225FF3") ("replace-vec" "N-01F59E62") ("require" "D-007F") + ("res" "N-03D33A57") ("reset-struct" "N-002A609F") ("rest" "N-02288559") ("ret" "N-033F39EF") diff --git a/tests/015/awk-res.tl b/tests/015/awk-res.tl new file mode 100644 index 00000000..6c919320 --- /dev/null +++ b/tests/015/awk-res.tl @@ -0,0 +1,8 @@ +(load "../common") + +(test + (build + (awk + (:inputs '#"How about it now brown cow") + (#/.ow/ (add res)))) + ("How" "now" "row" "cow")) @@ -68528,6 +68528,50 @@ and so the .code prn function prints lines. +.coNP Variable @ res +The +.code awk +variable +.code res +is implicitly bound over the scope of the action forms of every +condition-action clause. It holds the result of the condition form. + +Because the action forms execute only if the condition yields true, +it follows that +.code res +is never observed with a value of +.code nil +unless the program explicitly assigns that value. + +Note: this is an original feature in the \*(TL +.code awk +macro, which has no counterpart in POSIX or GNU Awk. + +.TP* Example: + +.verb + (awk + (:inputs '("carpet")) + (#/a.*p/ (prn res))) + + Output: + + arp +.brev + +In this example, the result of the +.code "#/a.*p/" +regular expression being applied to the input +.code carpet +is the string +.str arp +and so over that clause, +.code prn +takes on that string as its value. Thus, +thanks to +.codn prn , +the action has access to the matching part of the record. + .coNP Function @ prn .synb .mets (prn << form *) |