diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2021-10-04 22:05:52 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2021-10-04 22:05:52 -0700 |
commit | 0da50195d575904acf55e363117ad98db242923b (patch) | |
tree | 3975bed1e1ed8c4275a0df5b7f97757403b36e07 /txr.1 | |
parent | eb257d57926b2e5d2b965984af136ba0beabbb10 (diff) | |
download | txr-0da50195d575904acf55e363117ad98db242923b.tar.gz txr-0da50195d575904acf55e363117ad98db242923b.tar.bz2 txr-0da50195d575904acf55e363117ad98db242923b.zip |
awk: :fields specifies conversions.
* stdlib/awk.tl (sys:awk-compile-time): Slot field-names renamed to
field-name-conv.
(sys:awk-expander): Parse the new syntax which allows (sym fn)
pairs with optional fn, creating a list of normalized items
in the field-name-conv slot of the compile-time structure.
(sys:awk-symac-let): Adjust the code to the pair representation in
field-name-conv.
(sys:awk-field-name-code): New function for generating the
field conversion code.
(awk): Now that we have two optional pieces of code to wrap around
p-actions form, we factor that out of the awk-lambda, to a series
of conditional assignments. Here we handle the generation of the
field conversionns.
* conv.tl (sys:conv-expand-sym): New macro, used in
sys:awk-field-name-code and sys:conv-let.
(sys:conv-let): Simplify with sys:conv-expand-sym. Drop optional
argument from i; it connects with no documented feature, and is
not usable from fconv.
* tests/015/awk-fields.tl: New tests.
* txr.1: Updated, including cruft in fconv documentation.
Change-Id: Ie42819f58af039fdbcdb1ae365c89dc1add55c93
Diffstat (limited to 'txr.1')
-rw-r--r-- | txr.1 | 115 |
1 files changed, 85 insertions, 30 deletions
@@ -64062,23 +64062,29 @@ clause performs a nonlocal transfer, processing is not triggered, because the processing of the input source is deemed not to have taken place. -.meIP (:fields << sym *) +.meIP (:fields >> { sym | >> ( sym <> [ fun ]) | -}*) The .code :fields -clause may be used to give symbolic names to fields. Every -.meta sym -argument must be either a bindable symbol. The symbol +clause may be specified in order to give symbolic names to fields, +and optionally specify conversions for them. +Every argument must be one of three expressions. It may be +a bindable symbol other than +.code - +(minus). It may be a list whose first element is +a symbol other than +.code - +optionally followed the name of a function. +Or else it may be the .code - -has a special meaning. Symbols other than +symbol, which has a special meaning. +Symbols other than .code - may not be repeated, and the .code :fields clause may appear at most once in a given instance of the .code awk macro. -Each entry in the -.meta sym -list is understood to correspond to a field expression for a successive field, +Each argument is understood to correspond to a field expression for a successive field, starting with the leftmost .meta sym corresponding with the first field, @@ -64094,6 +64100,36 @@ macro. The .code - symbol is a place holder which doesn't bind a symbol macro to the corresponding field. +Additionally, every two-element entry which associates the field symbol +.meta sym +with a function name +.meta fun +specifies a field conversion. After each record is read and divided into +fields, those fields for which +.meta fun +is specified are updated by passing their value to this function +and replacing them by the returned value. +The +.meta fun +symbol may also be one of the short-hand symbols available in the +.code fconv +macro, such as +.codn i , +.code x +and others. +If at least one such conversion is specified in a +.code :fields +clause, then the value of +.code rec +is updated from the converted fields in the usual manner, as if +the fields had been assigned. +Furthermore, it is ensured that every field for which a +.code :fields +clause specifies a conversion exists. Fields with an empty string +value are automatically added so that a field exists for the +rightmost conversion, and the value of +.code nf +is updated to include these fields. .meIP >> ( condition << action *) Clauses which do not have one of the specially recognized keywords @@ -65322,25 +65358,29 @@ effectively providing a shorthand for commonly-needed conversions: .coIP i Provides conversion to integer. It is identical to the .code toint -function. +function, with the default radix. .coIP o Converts a string value holding an octal representation -to the integer which it denotes. The expression -.code "(o str)" -is equivalent to -.codn "(toint str 8)" . +to the integer which it denotes. It is equivalent to +.code toint +with a +.meta radix +argument of 8. .coIP x Converts a string value holding a hexadecimal representation -to the integer which it denotes. The expression -.code "(x str)" +to the integer which it denotes. It is equivalent to +.code toint is equivalent to -.codn "(toint str 16)" . +with a +.meta radix +argument of 16. .coIP b Converts a string value holding a binary (base two) representation -to the integer which it denotes. The expression -.code "(b str)" -is equivalent to -.codn "(toint str 2)" . +to the integer which it denotes. It is equivalent to +.code toint +with a +.meta radix +argument of 2. .coIP c Converts a string value holding a C-language-style representation to the integer which it denotes, meaning that the @@ -65351,16 +65391,18 @@ decimal. These prefixes follow the or .code - sign, if present. -The expression -.code "(c str)" -is equivalent to -.codn "(toint str #\ec)" . +The +.code c +function is equivalent to +.code toint +invoked with a +.meta radix +argument of +.codn #\ec . .coIP r Converts a string holding a floating-point representation to -the floating-point value which it denotes. The expression -.code "(r str)" -is equivalent to -.codn "(tofloat str)" . +the floating-point value which it denotes. It is equivalent to +.codn tofloat . .ccIP @, iz @, oz @, xz @, bz @ cz and @ rz Conversion similar to .codn i , @@ -65370,7 +65412,7 @@ Conversion similar to .code c and .codn r , -but using +but equivalent to using the functions .code tointz and .codn tofloatz . @@ -65407,7 +65449,6 @@ expression can be used as the condition in an clause which triggers the action if one or more fields have been extracted, and performs conversions on them. - Note: although .code fconv is intended for converting textual fields, and the semantic descriptions below @@ -65421,6 +65462,20 @@ means of the .code toint function. +Note: a somewhat less flexible mechanism for converting fields, related to +.codn fconv , +is present in the +.code :fields +clause of the +.code awk +macro, which can specify names for the positional fields, along with +conversion functions. The +.code :fields +clause has different syntax, and doesn't support the +.code : +(colon) separator, instead assuming a fixed number of fields +enumerated from the left. + .TP* Examples: .verb |