Add type predicate

The main new finction is is(), which tests the type of a value. There
  are also constants for each of the possible token types, and one new
  conversion: number(), which converts a value into the most appropriate number
  type. This change also reuses the functions in scanner.h for exracting ints,
  floats, and identifiers from strings, rather than either rewriting the
  functionality elsewhere, or using the general-purpose scan when only one
  token type was of interest.
This commit is contained in:
Glen Whitney 2019-08-27 23:30:02 -07:00
parent 892fdcdb75
commit 6534aec2fd
10 changed files with 848 additions and 724 deletions

View file

@ -5015,8 +5015,10 @@ y
.
As with
\family typewriter
\series bold
and
\family default
\series default
, this evaluation short-circuits if
\emph on
x
@ -5226,17 +5228,17 @@ y
Note: a string of consecutive relational operators is interpreted as the
conjunction of each consecutive (overlapping) pair.
Thus
\family typewriter
\family sans
2 <= y() <= 10
\family default
will evaluate to true precisely in rows 2 through 10, inclusive (it is
shorthand for
\family typewriter
\family sans
2 <= y() and y() <= 10
\family default
).
Similarly
\family typewriter
\family sans
@(cell1) == @(cell2) == @(cell3)
\family default
will return true exactly when all three cells have the same value.
@ -5499,13 +5501,15 @@ Functions
\begin_layout Standard
This section documents all available functions in alphabetical order.
The functions are given in a C-like notation, so use
\family typewriter
The functions are described in a C-like notation; you don't have to write
the types when you use the function in a formula.
For example, use
\family sans
@(0,0,0)
\family default
instead of
\family typewriter
@(integer 0, integer 0, integer 0)
, not
\family sans
@(int 0, int 0, int 0)
\family default
.
If no type is given for the result of a function, it means the result type
@ -5565,7 +5569,8 @@ y
z
\emph default
is specified, that value overrides the corresponding coordinate of the
given location, which defaults to the location of the current cell.
given location, which defaults to the location of the current cell if it
is omitted.
\end_layout
\begin_layout Description
@ -6436,6 +6441,27 @@ or
of all the supplied values.
\end_layout
\begin_layout Description
bool currently only acts as a keyword to
\family sans
\series bold
is
\series default
();
\family default
there are not currently any conversions to boolean type.
Use e.g.
\family sans
\shape italic
expr
\shape default
!= 0
\family default
to obtain a boolean value from a numerical expression.
\end_layout
\begin_layout Description
\series medium
@ -6641,6 +6667,24 @@ e
Note the parentheses are optional.
\end_layout
\begin_layout Description
empty
\series medium
[(
\shape italic
args
\shape default
)]
\series default
ignores all of its arguments and returns the empty value.
Without parentheses, acts as a keyword, typically for
\family sans
\series bold
is
\series default
().
\end_layout
\begin_layout Description
\series medium
@ -6652,17 +6696,23 @@ error
\series default
error
\series medium
(string
[([
\emph on
\begin_inset space ~
\end_inset
message
\emph default
)
])]
\series default
evaluates to an error with the specified message.
converts its argument to a string just as with the string() function, and
then returns an error with that string as the message.
Without parentheses, acts as a keyword, e.g.
for
\family sans
\series bold
is
\series default
()
\family default
.
\end_layout
@ -6692,6 +6742,19 @@ boolean
false represents the false Boolean value.
\end_layout
\begin_layout Description
fident a keyword indicating the token type of
\begin_inset Quotes eld
\end_inset
function identifier,
\begin_inset Quotes erd
\end_inset
provided for the sake of completeness.
There is probably little practical call for this type.
\end_layout
\begin_layout Description
\series medium
@ -6703,7 +6766,7 @@ float
\series default
float
\series medium
(string|int
[([float|string|int|empty
\emph on
\begin_inset space ~
@ -6711,9 +6774,20 @@ float
s
\emph default
)
])]
\series default
converts the given string or int into a floating point number.
converts into a floating point number the given float, string, int, or
empty value (the latter converts to 0.0).
If the argument is omitted, the value of the current cell is used.
If the parentheses are omitted, acts as a keyword, e.g., for type testing
with
\family sans
\series bold
is
\series default
()
\family default
.
\end_layout
\begin_layout Description
@ -6763,14 +6837,32 @@ frac
x
\emph default
)
)
\series default
evaluates to the fractional part of
\emph on
x
\emph default
.
\end_layout
\begin_layout Description
funcall a keyword indicating the
\begin_inset Quotes eld
\end_inset
funcall
\begin_inset Quotes erd
\end_inset
token type used to encode expressions as tokens; may be used with
\family sans
\series bold
is
\series default
()
\family default
for example.
\end_layout
\begin_layout Description
@ -6789,7 +6881,7 @@ int
\series default
int
\series medium
(float
[([int|float|string|empty
\emph on
\begin_inset space ~
@ -6797,263 +6889,185 @@ int
x
\emph default
[,integer
][,
\emph on
\begin_inset space ~
\end_inset
neg
direction
\emph default
,
\begin_inset space ~
\end_inset
integer
\emph on
\begin_inset space ~
\end_inset
pos
\emph default
])
])]
\series default
converts
converts to an
\emph on
\emph default
integer the given integer, float, string, or empty value
\emph on
x.
\emph default
(The latter converts to 0.) The optional second argument must be the name
of one of the functions that produces a floating point integral value from
a float, i.e.,
\family sans
\series bold
ceil
\family default
\series default
,
\family sans
\series bold
floor
\family default
\series default
,
\family sans
\series bold
round
\family default
\series default
, or
\family sans
\series bold
trunc
\family default
\series default
, and it directs how to convert floating point values to an integer.
(The default is
\family sans
\series bold
trunc
\family default
\series default
, see the documentation of the corresponding functions for a description
of the conversion behavior.) If
\emph on
x
\emph default
to an integer value by cutting off the fractional part.
If given, the value of
\emph on
neg
\emph default
and
\emph on
pos
\emph default
determines how negative and non-negative numbers will be converted:
\end_layout
\begin_layout Quote
\begin_inset Tabular
<lyxtabular version="3" rows="6" columns="2">
<features tabularvalignment="middle">
<column alignment="center" valignment="top">
<column alignment="left" valignment="top">
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\emph on
neg
\emph default
/
\emph on
pos
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
Result
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
< -1
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
next smaller integer value
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
-1
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
round downward
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
0
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
cut fractional part off (default)
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
1
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
round upward
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
> 1
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
next larger integer value
\end_layout
\end_inset
</cell>
</row>
</lyxtabular>
\end_inset
is omitted, the value of the current cell is used.
If in addition the parentheses are omitted, acts as a keyword, e.g., for
value testing with
\family sans
\series bold
is
\series default
()
\family default
.
\end_layout
\begin_layout Description
is
\series medium
int
[([
\emph on
x
\emph default
][,
\emph on
\begin_inset space ~
\end_inset
type1,
\begin_inset space ~
\end_inset
type2
\emph default
,...])]
\series default
int
\series medium
(string|float
Boolean-valued type testing.
Returns true if the given value
\emph on
\begin_inset space ~
\end_inset
s
x
\emph default
[,
\emph on
\begin_inset space ~
\end_inset
rounding
\emph default
])
is any of the listed types.
Each type may be any of the keywords
\family sans
\series bold
bool, empty, error, fident, float, funcall, int
\family default
\series default
converts
,
\family sans
\series bold
lident, location, number, operator,
\family default
\series default
or
\family sans
\series bold
string
\family default
\series default
.
Each of these identifies a single type, except
\family sans
\series bold
number
\family default
\series default
, which is a shorthand for
\family sans
\series bold
empty
\family default
\series default
,
\family sans
\series bold
float
\family default
\series default
,
\family sans
\series bold
int
\family default
\series default
.
If no types are specified, returns true for any non-empty value.
(In other words,
\family sans
\series bold
is
\series default
(
\emph on
s
x
\emph default
to an integer number.
In case of a floating point argument, there is an optional second argument
which determines which nearby integer will be selected.
It must be one of the function names
\begin_inset Quotes eld
\end_inset
ceil,
\begin_inset Quotes erd
\end_inset
\begin_inset Quotes eld
\end_inset
floor,
\begin_inset Quotes erd
\end_inset
\begin_inset Quotes eld
\end_inset
round,
\begin_inset Quotes erd
\end_inset
or
\begin_inset Quotes eld
\end_inset
trunc,
\begin_inset Quotes erd
\end_inset
and the integer corresponding to the value that function would select is
returned.
(See the documentation of each of these functions.) If the second argument
is omitted, the
\begin_inset Quotes eld
\end_inset
trunc
\begin_inset Quotes erd
\end_inset
value is returned.
)
\family default
is a boolean-valued version of
\family sans
\series bold
n
\series default
(
\emph on
x
\emph default
)
\family default
, without the special behavior when
\emph on
x
\emph default
is a location.) If
\emph on
x
\emph default
is omitted, defaults to the value of the current cell; the parentheses
may be omitted as well, so bare
\family sans
\series bold
is
\family default
\series default
tests whether the current cell is non-empty.
\end_layout
\begin_layout Description
@ -7106,7 +7120,20 @@ s
s
\emph default
.
\end_layout
\begin_layout Description
lident a keyword indicating the
\begin_inset Quotes eld
\end_inset
label identifier
\begin_inset Quotes erd
\end_inset
token type.
This type is not generally accessible, as labels always evaluate to their
locations (or error if there is no such label).
\end_layout
\begin_layout Description
@ -7157,7 +7184,25 @@ y
y
\emph default
.
\end_layout
\begin_layout Description
location currently this is only a keyword, e.g., for use with
\family sans
\series bold
is
\series default
()
\family default
.
Use
\family sans
\series bold
&
\series default
()
\family default
to convert three integers to a location.
\end_layout
\begin_layout Description
@ -7449,6 +7494,47 @@ l1
The second form simply returns the number of its arguments which are nonempty.
\end_layout
\begin_layout Description
number
\series medium
[([bool|empty|int|float|string
\emph on
\begin_inset space ~
\end_inset
x
\emph default
])]
\series default
Converts its argument, which defaults to the value of the current cell,
to the most appropriate number type.
Thus, it leaves ints and floats alone, converts boolean values to integers
1 or 0, converts empty to integer 0, and converts numeric strings to ints
if they don't have a decimal point and floats if they do.
Without parentheses, acts as a keyword for
\family sans
\series bold
is
\series default
()
\family default
, abbreviating the combination of float, int, and empty.
\end_layout
\begin_layout Description
operator A keyword indicating the
\begin_inset Quotes eld
\end_inset
operator
\begin_inset Quotes erd
\end_inset
token type, representing symbols like parentheses, plus signs, commas,
etc.
\end_layout
\begin_layout Description
poly
\series medium
@ -7476,7 +7562,7 @@ cn
\begin_inset space ~
\end_inset
...])
...])
\series default
evaluates the polynomial
\emph on