summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-09-23 15:04:31 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-09-23 15:04:31 -0700
commit77b5a3eda0eda28b30addcfcd62cce1bae8542cf (patch)
treeb7aa4a492896547909f042580171c1e503591ce5
parent158f99b41bc0b75b6f571fc4f87f020ef80268ca (diff)
downloadtxr-77b5a3eda0eda28b30addcfcd62cce1bae8542cf.tar.gz
txr-77b5a3eda0eda28b30addcfcd62cce1bae8542cf.tar.bz2
txr-77b5a3eda0eda28b30addcfcd62cce1bae8542cf.zip
awk macro: support regexes better in ranges.
* share/txr/stdlib/awk.tl (sys:awk-compile-time): New slot, rng-rec-temp. Specifies the name of a temporary variable which is scoped over the evaluation of ranges, and which caches a reference to the current record string. (sys:range-test): New function. (awk:let): Rename the original rng macrolet to sys:rng, and introduce rng as a wrapper around it which inserts the logic for calling the regex or function that might emerge from the evaluation of from-expr or to-expr. (awk): If ranges are present in the syntax, then bind the temporary variable which caches the record around the evaluations of the ranges. * txr.1: Document new special behavior of rng w.r.t functions and regexes. Add admonition against modifying rec and some other awk variables from range expressions.
-rw-r--r--share/txr/stdlib/awk.tl18
-rw-r--r--txr.130
2 files changed, 43 insertions, 5 deletions
diff --git a/share/txr/stdlib/awk.tl b/share/txr/stdlib/awk.tl
index 7c6ae504..07a5f3bc 100644
--- a/share/txr/stdlib/awk.tl
+++ b/share/txr/stdlib/awk.tl
@@ -48,6 +48,7 @@
begin-actions end-actions
cond-actions
(nranges 0)
+ (rng-rec-temp (gensym))
rng-expr-temps
rng-exprs)
@@ -109,6 +110,11 @@
(t (put-string self.rec)
(put-string self.ors))))
+(defun sys:range-test (val rec)
+ (caseq (typeof val)
+ ((regex fun) (call val rec))
+ (t val)))
+
(defun sys:awk-expander (clauses)
(let ((awc (new sys:awk-compile-time)))
(each ((cl clauses))
@@ -176,7 +182,7 @@
(macrolet ((next () '(return-from :awk-rec))
(next-file () '(return-from :awk-file))
(prn (. args) ^(qref ,',aws-sym (prn ,*args)))
- (rng (from-expr to-expr :env e)
+ (sys:rng (from-expr to-expr :env e)
(let ((ix (pinc (qref ,awc nranges)))
(rng-temp (gensym))
(from-expr-ex (sys:expand from-expr e))
@@ -193,6 +199,9 @@
(or (set flag ,flag-new) ,flag-old)))
(qref ,awc rng-exprs))
rng-temp))
+ (rng (from-expr to-expr)
+ ^(sys:rng (sys:range-test ,from-expr ,(qref ,awc rng-rec-temp))
+ (sys:range-test ,to-expr ,(qref ,awc rng-rec-temp))))
(ff (. opip-args)
^(symacrolet ((f (rslot ,',aws-sym 'fields 'f-to-rec)))
(set f [(opip ,*opip-args) f])))
@@ -229,9 +238,10 @@
,*awc.end-file-actions))))
(,awk-fun (lambda (,aws-sym)
,(if awc.rng-exprs
- ^(let* ,(nreverse
- (zip awc.rng-expr-temps
- awc.rng-exprs))
+ ^(let* ((,awc.rng-rec-temp rec)
+ ,*(nreverse
+ (zip awc.rng-expr-temps
+ awc.rng-exprs)))
,p-actions-xform)
p-actions-xform))))
,*awc.begin-actions
diff --git a/txr.1 b/txr.1
index 58506358..a47e776f 100644
--- a/txr.1
+++ b/txr.1
@@ -38771,7 +38771,25 @@ The expressions
.meta from-condition
and
.meta to-condition
-are ordinary expressions which are evaluated; however, the are evaluated
+are ordinary expressions which are evaluated. However, their
+evaluation is unusual in two ways.
+
+Firstly, if either expression
+produces, as its result, a function or regular expression object,
+then that function or regular expression object is applied to
+the current record (value of the
+.code rec
+variable), and the result of that application is then taken
+as the result of the condition. This allows for expressions like
+.code "(rng (f^ #/start/) #/end/)"
+which denotes a range which begins with a record which
+begins with the prefix
+.str start
+and ends with a record which contains
+.str end
+as a substring.
+
+Secondly, the conditions are evaluated
out of order with respect to the surrounding expression
in which they occur. Ranges and their constituent
.meta from-condition
@@ -38813,6 +38831,16 @@ itself is evaluated, the
expression merely fetches a previously computed Boolean value which indicates
whether the range is active for this record.
+Also, the behavior is unspecified if range expressions attempt to modify
+the awk-special variables.
+.codn rec ,
+.codn f ,
+.codn fs ,
+.code ft
+or
+.codn kfs .
+It is not recommended to place any side effects into range expressions.
+
Evaluation of ranges obeys the following logic, which is applied to
each range, prior to the processing of condition-action clauses.
If a range is not currently active, its