Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3fp-parse.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3fp-parse.dtx
4 %
5 % Copyright (C) 2011-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3fp-parse} module\\
35 % Floating point expression parsing^^A
36 % }
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 % \date{Released 2026-05-15}
46 %
47 % \maketitle
48 %
49 % \begin{documentation}
50 %
51 % \end{documentation}
52 %
53 % \begin{implementation}
54 %
55 % \section{\pkg{l3fp-parse} implementation}
56 %
57 % \begin{macrocode}
58 %<*code>
59 % \end{macrocode}
60 %
61 % \begin{macrocode}
62 %<@@=fp>
63 % \end{macrocode}
64 %
65 % \subsection{Work plan}
66 %
67 % The task at hand is non-trivial, and some previous failed attempts
68 % show that the code leads to unreadable logs, so we had better get it
69 % (almost) right the first time. Let us first describe our goal, then
70 % discuss the design precisely before writing any code.
71 %
72 % In this file at least, a \meta{floating point object} is a floating
73 % point number or tuple. This can be extended to anything that starts
74 % with \cs{s_@@} or \cs{s_@@_\meta{type}} and ends with \cs{@@_sep:} with some
75 % internal structure that depends on the \meta{type}.
76 %
77 % \begin{macro}[EXP]{\@@_parse:n}
78 % \begin{syntax}
79 % \cs{@@_parse:n} \Arg{fp expr}
80 % \end{syntax}
81 % Evaluates the \meta{fp expr} and leaves the result
82 % in the input stream as a floating point object. This
83 % function forms the basis of almost all public \pkg{l3fp} functions.
84 % During evaluation, each token is fully \texttt{f}-expanded.
85 %
86 % \cs{@@_parse_o:n} does the same but expands once after its result.
87 % \begin{texnote}
88 % Registers (integers, toks, etc.) are automatically unpacked,
89 % without requiring a function such as \cs{int_use:N}. Invalid
90 % tokens remaining after \texttt{f}-expansion lead to
91 % unrecoverable low-level \TeX{} errors.
92 % \end{texnote}
93 % \end{macro}
94 %
95 % \begin{variable}
96 % {
97 % \c_@@_prec_func_int,
98 % \c_@@_prec_hatii_int,
99 % \c_@@_prec_hat_int,
100 % \c_@@_prec_not_int,
101 % \c_@@_prec_juxt_int,
102 % \c_@@_prec_times_int,
103 % \c_@@_prec_plus_int,
104 % \c_@@_prec_comp_int,
105 % \c_@@_prec_and_int,
106 % \c_@@_prec_or_int,
107 % \c_@@_prec_quest_int,
108 % \c_@@_prec_colon_int,
109 % \c_@@_prec_comma_int,
110 % \c_@@_prec_tuple_int,
111 % \c_@@_prec_end_int,
112 % }
113 % Floating point expressions are composed of numbers, given in various
114 % forms, infix operators, such as |+|, |**|, or~|,| (which joins two
115 % numbers into a list), and prefix operators, such as the unary~|-|,
116 % functions, or opening parentheses. Here is a list of precedences
117 % which control the order of evaluation (some distinctions are
118 % irrelevant for the order of evaluation, but serve as signals), from
119 % the tightest binding to the loosest binding.
120 % \begin{itemize}
121 % \item[16] Function calls.
122 % \item[13/14] Binary |**| and~|^| (right to left).
123 % \item[12] Unary |+|, |-|, |!| (right to left).
124 % \item[11] Juxtaposition (implicit~|*|) with no parenthesis.
125 % \item[10] Binary |*| and~|/|.
126 % \item[9] Binary |+| and~|-|.
127 % \item[7] Comparisons.
128 % \item[6] Logical \texttt{and}, denoted by~|&&|.
129 % \item[5] Logical \texttt{or}, denoted by~\verb*+||+.
130 % \item[4] Ternary operator |?:|, piece~|?|.
131 % \item[3] Ternary operator |?:|, piece~|:|.
132 % \item[2] Commas.
133 % \item[1] Place where a comma is allowed and generates a tuple.
134 % \item[0] Start and end of the expression.
135 % \end{itemize}
136 % \begin{macrocode}
137 \int_const:Nn \c_@@_prec_func_int { 16 }
138 \int_const:Nn \c_@@_prec_hatii_int { 14 }
139 \int_const:Nn \c_@@_prec_hat_int { 13 }
140 \int_const:Nn \c_@@_prec_not_int { 12 }
141 \int_const:Nn \c_@@_prec_juxt_int { 11 }
142 \int_const:Nn \c_@@_prec_times_int { 10 }
143 \int_const:Nn \c_@@_prec_plus_int { 9 }
144 \int_const:Nn \c_@@_prec_comp_int { 7 }
145 \int_const:Nn \c_@@_prec_and_int { 6 }
146 \int_const:Nn \c_@@_prec_or_int { 5 }
147 \int_const:Nn \c_@@_prec_quest_int { 4 }
148 \int_const:Nn \c_@@_prec_colon_int { 3 }
149 \int_const:Nn \c_@@_prec_comma_int { 2 }
150 \int_const:Nn \c_@@_prec_tuple_int { 1 }
151 \int_const:Nn \c_@@_prec_end_int { 0 }
152 % \end{macrocode}
153 % \end{variable}
154 %
155 % \subsubsection{Storing results}
156 %
157 % The main question in parsing expressions expandably is to decide where
158 % to put the intermediate results computed for various subexpressions.
159 %
160 % One option is to store the values at the start of the expression, and
161 % carry them together as the first argument of each macro. However, we
162 % want to \texttt{f}-expand tokens one by one in the expression (as
163 % \cs{int_eval:n} does), and with this approach, expanding the next
164 % unread token forces us to jump with \cs{exp_after:wN} over every value
165 % computed earlier in the expression. With this approach, the run-time
166 % grows at least quadratically in the length of the expression, if
167 % not as its cube (inserting the \cs{exp_after:wN} is tricky and slow).
168 %
169 % A second option is to place those values at the end of the expression.
170 % Then expanding the next unread token is straightforward, but this
171 % still hits a performance issue: for long expressions we would be
172 % reaching all the way to the end of the expression at every step of the
173 % calculation. The run-time is again quadratic.
174 %
175 % A variation of the above attempts to place the intermediate results
176 % which appear when computing a parenthesized expression near the
177 % closing parenthesis. This still lets us expand tokens as we go, and
178 % avoids performance problems as long as there are enough parentheses.
179 % However, it would be better to avoid requiring the closing
180 % parenthesis to be present as soon as the corresponding opening
181 % parenthesis is read: the closing parenthesis may still be hidden in a
182 % macro yet to be expanded.
183 %
184 % Hence, we need to go for some fine expansion control: the result is
185 % stored \emph{before} the start!
186 %
187 % Let us illustrate this idea in a simple model: adding positive
188 % integers which may be resulting from the expansion of macros, or may
189 % be values of registers. Assume that one number, say, $12345$, has
190 % already been found, and that we want to parse the next number. The
191 % current status of the code may look as follows.
192 % \begin{syntax}
193 % \cs{exp_after:wN} |\add:ww| \cs{int_value:w} 12345 \cs{exp_after:wN} ;
194 % \cs{exp:w} |\operand:w| \meta{stuff}
195 % \end{syntax}
196 % One step of expansion expands \cs{exp_after:wN}, which triggers the
197 % primitive \cs{int_value:w}, which reads the five digits we have
198 % already found, |12345|. This integer is unfinished, causing the
199 % second \cs{exp_after:wN} to expand, and to trigger the construction
200 % \cs{exp:w}, which expands |\operand:w|, defined to read
201 % what follows and make a number out of it, then leave \cs{exp_end:}, the
202 % number, and a \cs{@@_sep:} in the input stream. Once |\operand:w| is
203 % done expanding, we obtain essentially
204 % \begin{syntax}
205 % \cs{exp_after:wN} |\add:ww| \cs{int_value:w} 12345 \cs{@@_sep:}
206 % \cs{exp:w} \cs{exp_end:} 333444 \cs{@@_sep:}
207 % \end{syntax}
208 % where in fact \cs{exp_after:wN} has already been expanded,
209 % \cs{int_value:w} has already seen |12345|, and
210 % \cs{exp:w} is still looking for a number. It finds
211 % \cs{exp_end:}, hence expands to nothing. Now, \cs{int_value:w} sees
212 % the \cs{@@_sep:}, which cannot be part of a number. The expansion
213 % stops, and we are left with
214 % \begin{syntax}
215 % |\add:ww| 12345 \cs{@@_sep:} 333444 \cs{@@_sep:}
216 % \end{syntax}
217 % which can safely perform the addition by grabbing two arguments
218 % delimited by~\cs{@@_sep:}.
219 %
220 % If we were to continue parsing the expression, then the following
221 % number should also be cleaned up before the next use of a binary
222 % operation such as |\add:ww|. Just like \cs{int_value:w} |12345|
223 % \cs{exp_after:wN}~\cs{@@_sep:} expanded what follows once, we need |\add:ww|
224 % to do the calculation, and in the process to expand the following
225 % once. This is also true in our real application: all the functions of
226 % the form \cs[no-index]{@@_\ldots_o:ww} expand what follows once. This comes at the
227 % cost of leaving tokens in the input stack, and we need to be
228 % careful not to waste this memory. All of our discussion above is nice
229 % but simplistic, as operations should not simply be performed in the
230 % order they appear.
231 %
232 % \subsubsection{Precedence and infix operators}
233 %
234 % The various operators we will encounter have different precedences,
235 % which influence the order of calculations: $1+2\times 3 = 1+(2\times
236 % 3)$ because $\times$~has a higher precedence than~$+$. The true
237 % analog of our macro |\operand:w| must thus take care of that. When
238 % looking for an operand, it needs to perform calculations until
239 % reaching an operator which has lower precedence than the one which
240 % called |\operand:w|. This means that |\operand:w| must know what the
241 % previous binary operator is, or rather, its precedence: we thus rename
242 % it |\operand:Nw|. Let us describe as an example how we plan to do
243 % the calculation |41-2^3*4+5|. More precisely we describe how to
244 % perform the first operation in this expression. Here, we abuse
245 % notations: the first argument of |\operand:Nw| should be an integer
246 % constant (\cs{c_@@_prec_plus_int}, \ldots{}) equal to the precedence
247 % of the given operator, not directly the operator itself.
248 % \begin{itemize}
249 % \item Clean up~|41| and find~|-|. We call |\operand:Nw|~|-| to find
250 % the second operand.
251 % \item Clean up~|2| and find~|^|.
252 % \item Compare the precedences of |-| and~|^|. Since the latter is
253 % higher, we need to compute the exponentiation. For this, find the
254 % second operand with a nested call to |\operand:Nw|~|^|.
255 % \item Clean up~|3| and find~|*|.
256 % \item Compare the precedences of |^| and~|*|. Since the former is
257 % higher, |\operand:Nw|~|^| has found the second operand of the
258 % exponentiation, which is computed: $2^{3} = 8$.
259 % \item We now have |41-8*4+5|, and |\operand:Nw|~|-| is still
260 % looking for a second operand for the subtraction. Is it~$8$?
261 % \item Compare the precedences of |-| and~|*|. Since the latter is
262 % higher, we are not done with~$8$. Call |\operand:Nw|~|*| to find
263 % the second operand of the multiplication.
264 % \item Clean up~|4|, and find~|+|.
265 % \item Compare the precedences of |*| and~|+|. Since the former is
266 % higher, |\operand:Nw|~|*| has found the second operand of the
267 % multiplication, which is computed: $8*4 = 32$.
268 % \item We now have |41-32+5|, and |\operand:Nw|~|-| is still looking
269 % for a second operand for the subtraction. Is it~$32$?
270 % \item Compare the precedences of |-| and~|+|. Since they are equal,
271 % |\operand:Nw|~|-| has found the second operand for the
272 % subtraction, which is computed: $41-32=9$.
273 % \item We now have |9+5|.
274 % \end{itemize}
275 % The procedure above stops short of performing all computations, but
276 % adding a surrounding call to |\operand:Nw| with a very low precedence
277 % ensures that all computations are performed before |\operand:Nw|
278 % is done. Adding a trailing marker with the same very low precedence
279 % prevents the surrounding |\operand:Nw| from going beyond the marker.
280 %
281 % The pattern above to find an operand for a given operator, is to find
282 % one number and the next operator, then compare precedences to know if
283 % the next computation should be done. If it should, then perform it
284 % after finding its second operand, and look at the next operator, then
285 % compare precedences to know if the next computation should be done.
286 % This continues until we find that the next computation should not be
287 % done. Then, we stop.
288 %
289 % We are now ready to get a bit more technical and describe which of the
290 % \pkg{l3fp-parse} functions correspond to each step above.
291 %
292 % First, \cs{@@_parse_operand:Nw} is the |\operand:Nw| function above,
293 % with small modifications due to expansion issues discussed later. We
294 % denote by \meta{precedence} the argument of \cs{@@_parse_operand:Nw},
295 % that is, the precedence of the binary operator whose operand we are
296 % trying to find. The basic action is to read numbers from the input
297 % stream. This is done by \cs{@@_parse_one:Nw}. A first approximation
298 % of this function is that it reads one \meta{number}, performing no
299 % computation, and finds the following binary \meta{operator}. Then it
300 % expands to
301 % \begin{quote}
302 % \meta{number}\\
303 % | \__fp_parse_infix_|\meta{operator}|:N| \meta{precedence}
304 % \end{quote}
305 % expanding the \texttt{infix} auxiliary before leaving the above in the
306 % input stream.
307 %
308 % We now explain the \texttt{infix} auxiliaries. We need some
309 % flexibility in how we treat the case of equal precedences: most often,
310 % the first operation encountered should be performed, such as |1-2-3|
311 % being computed as |(1-2)-3|, but |2^3^4| should be evaluated as
312 % |2^(3^4)| instead. For this reason, and to support the equivalence
313 % between |**| and~|^| more easily, each binary operator is converted to
314 % a control sequence |\__fp_parse_infix_|\meta{operator}|:N| when it is
315 % encountered for the first time. Instead of passing both precedences
316 % to a test function to do the comparison steps above, we pass the
317 % \meta{precedence} (of the earlier operator) to the \texttt{infix}
318 % auxiliary for the following \meta{operator}, to know whether to
319 % perform the computation of the \meta{operator}. If it should not be
320 % performed, the \texttt{infix} auxiliary expands to
321 % \begin{syntax}
322 % |@| \cs{use_none:n} |\__fp_parse_infix_|\meta{operator}|:N|
323 % \end{syntax}
324 % and otherwise it calls \cs{@@_parse_operand:Nw} with the precedence of
325 % the \meta{operator} to find its second operand \meta{number_2} and the
326 % next \meta{operator_2}, and expands to
327 % \begin{syntax}
328 % |@| \cs{@@_parse_apply_binary:NwNwN}
329 % ~~~~\meta{operator} \meta{number_2}
330 % |@| |\__fp_parse_infix_|\meta{operator_2}|:N|
331 % \end{syntax}
332 % The \texttt{infix} function is responsible for comparing precedences,
333 % but cannot directly call the computation functions, because the first
334 % operand \meta{number} is before the \texttt{infix} function in the
335 % input stream. This is why we stop the expansion here and give control
336 % to another function to close the loop.
337 %
338 % A definition of \cs{@@_parse_operand:Nw} \meta{precedence} with some
339 % of the expansion control removed is
340 % \begin{syntax}
341 % \cs{exp_after:wN} \cs{@@_parse_continue:NwN}
342 % \cs{exp_after:wN} \meta{precedence}
343 % \cs{exp:w} \cs{exp_end_continue_f:w}
344 % ~~\cs{@@_parse_one:Nw} \meta{precedence}
345 % \end{syntax}
346 % This expands \cs{@@_parse_one:Nw} \meta{precedence} completely, which
347 % finds a number, wraps the next \meta{operator} into an \texttt{infix}
348 % function, feeds this function the \meta{precedence}, and expands it,
349 % yielding either
350 % \begin{syntax}
351 % \cs{@@_parse_continue:NwN} \meta{precedence}
352 % \meta{number} |@|
353 % \cs{use_none:n} |\__fp_parse_infix_|\meta{operator}|:N|
354 % \end{syntax}
355 % or
356 % \begin{syntax}
357 % \cs{@@_parse_continue:NwN} \meta{precedence}
358 % \meta{number} |@|
359 % \cs{@@_parse_apply_binary:NwNwN}
360 % ~~\meta{operator} \meta{number_2}
361 % |@| |\__fp_parse_infix_|\meta{operator_2}|:N|
362 % \end{syntax}
363 % The definition of \cs{@@_parse_continue:NwN} is then very simple:
364 % \begin{syntax}
365 % |\cs_new:Npn \__fp_parse_continue:NwN #1#2@#3 { #3 #1 #2 @ }|
366 % \end{syntax}
367 % In the first case, |#3|~is \cs{use_none:n}, yielding
368 % \begin{syntax}
369 % \cs{use_none:n} \meta{precedence} \meta{number} |@|
370 % |\__fp_parse_infix_|\meta{operator}|:N|
371 % \end{syntax}
372 % then \meta{number} |@| |\__fp_parse_infix_|\meta{operator}|:N|. In
373 % the second case, |#3|~is \cs{@@_parse_apply_binary:NwNwN}, whose role
374 % is to compute \meta{number} \meta{operator} \meta{number_2} and to
375 % prepare for the next comparison of precedences: first we get
376 % \begin{syntax}
377 % \cs{@@_parse_apply_binary:NwNwN}
378 % ~~\meta{precedence} \meta{number} |@|
379 % ~~\meta{operator} \meta{number_2}
380 % |@| |\__fp_parse_infix_|\meta{operator_2}|:N|
381 % \end{syntax}
382 % then
383 % \begin{syntax}
384 % \cs{exp_after:wN} \cs{@@_parse_continue:NwN}
385 % \cs{exp_after:wN} \meta{precedence}
386 % \cs{exp:w} \cs{exp_end_continue_f:w}
387 % |\__fp_|\meta{operator}|_o:ww| \meta{number} \meta{number_2}
388 % \cs{exp:w} \cs{exp_end_continue_f:w}
389 % |\__fp_parse_infix_|\meta{operator_2}|:N| \meta{precedence}
390 % \end{syntax}
391 % where |\__fp_|\meta{operator}|_o:ww| computes \meta{number}
392 % \meta{operator} \meta{number_2} and expands after the result, thus
393 % triggers the comparison of the precedence of the \meta{operator_2} and
394 % the \meta{precedence}, continuing the loop.
395 %
396 % We have introduced the most important functions here, and the next few
397 % paragraphs we describe various subtleties.
398 %
399 % \subsubsection{Prefix operators, parentheses, and functions}
400 %
401 % Prefix operators (unary |-|, |+|,~|!|) and parentheses are taken care
402 % of by the same mechanism, and functions (\texttt{sin}, \texttt{exp},
403 % etc.) as well. Finding the argument of the unary~|-|, for instance,
404 % is very similar to grabbing the second operand of a binary infix
405 % operator, with a subtle precedence explained below. Once that operand
406 % is found, the operator can be applied to it (for the unary~|-|, this
407 % simply flips the sign). A left parenthesis is just a prefix operator
408 % with a very low precedence equal to that of the closing parenthesis
409 % (which is treated as an infix operator, since it normally appears just
410 % after numbers), so that all computations are performed until the
411 % closing parenthesis. The prefix operator associated to the left
412 % parenthesis does not alter its argument, but it removes the closing
413 % parenthesis (with some checks).
414 %
415 % Prefix operators are the reason why we only summarily described the
416 % function \cs{@@_parse_one:Nw} earlier. This function is responsible
417 % for reading in the input stream the first possible \meta{number} and
418 % the next infix \meta{operator}. If what follows \cs{@@_parse_one:Nw}
419 % \meta{precedence} is a prefix operator, then we must find the operand
420 % of this prefix operator through a nested call to
421 % \cs{@@_parse_operand:Nw} with the appropriate precedence, then apply
422 % the operator to the operand found to yield the result of
423 % \cs{@@_parse_one:Nw}. So far, all is simple.
424 %
425 % The unary operators |+|, |-|,~|!| complicate things a little bit:
426 % |-3**2| should be $-(3^2)=-9$, and not $(-3)^2=9$. This would easily
427 % be done by giving~|-| a lower precedence, equal to that of the infix
428 % |+| and~|-|. Unfortunately, this fails in cases such as |3**-2*4|,
429 % yielding $3^{-2\times 4}$ instead of the correct $3^{-2}\times 4$. A
430 % second attempt would be to call \cs{@@_parse_operand:Nw} with the
431 % \meta{precedence} of the previous operator, but |0>-2+3| is then
432 % parsed as |0>-(2+3)|: the addition is performed because it binds more
433 % tightly than the comparison which precedes~|-|. The correct approach
434 % is for a unary~|-| to perform operations whose precedence is greater
435 % than both that of the previous operation, and that of the unary~|-|
436 % itself. The unary~|-| is given a precedence higher than
437 % multiplication and division. This does not lead to any surprising
438 % result, since $-(x/y) = (-x)/y$ and similarly for multiplication, and
439 % it reduces the number of nested calls to \cs{@@_parse_operand:Nw}.
440 %
441 % Functions are implemented as prefix operators with very high
442 % precedence, so that their argument is the first number that can
443 % possibly be built.
444 %
445 % Note that contrarily to the \texttt{infix} functions discussed
446 % earlier, the \texttt{prefix} functions do perform tests on the
447 % previous \meta{precedence} to decide whether to find an argument or
448 % not, since we know that we need a number, and must never stop there.
449 %
450 % \subsubsection{Numbers and reading tokens one by one}
451 %
452 % So far, we have glossed over one important point: what is a
453 % \enquote{number}? A number is typically given in the form
454 % \meta{significand}|e|\meta{exponent}, where the \meta{significand} is
455 % any non-empty string composed of decimal digits and at most one
456 % decimal separator (a period), the exponent
457 % \enquote{\texttt{e}\meta{exponent}} is optional and is composed of an
458 % exponent mark~|e| followed by a possibly empty string of signs
459 % |+| or~|-| and a non-empty string of decimal digits. The
460 % \meta{significand} can also be an integer, dimension, skip, or muskip
461 % variable, in which case dimensions are converted from points (or mu
462 % units) to floating points, and the \meta{exponent} can also be an
463 % integer variable. Numbers can also be given as floating point
464 % variables, or as named constants such as |nan|, |inf| or~|pi|. We may
465 % add more types in the future.
466 %
467 % When \cs{@@_parse_one:Nw} is looking for a \enquote{number}, here is
468 % what happens.
469 % \begin{itemize}
470 % \item If the next token is a control sequence with the meaning of
471 % \cs{scan_stop:}, it can be: \cs{s_@@}, in which case our job is
472 % done, as what follows is an internal floating point number, or
473 % \cs{s_@@_expr_mark}, in which case the expression has come to an early
474 % end, as we are still looking for a number here, or something else,
475 % in which case we consider the control sequence to be a bad
476 % variable resulting from \texttt{c}-expansion.
477 % \item If the next token is a control sequence with a different
478 % meaning, we assume that it is a register, unpack it with
479 % \cs{tex_the:D}, and use its value (in \texttt{pt} for dimensions
480 % and skips, \texttt{mu} for muskips) as the \meta{significand} of a
481 % number: we look for an exponent.
482 % \item If the next token is a digit, we remove any leading zeros,
483 % then read a significand larger than~$1$ if the next character is a
484 % digit, read a significand smaller than~$1$ if the next character
485 % is a period, or we have found a significand equal to~$0$
486 % otherwise, and look for an exponent.
487 % \item If the next token is a letter, we collect more letters until
488 % the first non-letter: the resulting word may denote a function
489 % such as |asin|, a constant such as |pi| or be unknown. In the
490 % first case, we call \cs{@@_parse_operand:Nw} to find the argument
491 % of the function, then apply the function, before declaring that we
492 % are done. Otherwise, we are done, either with the value of the
493 % constant, or with the value |nan| for unknown words.
494 % \item If the next token is anything else, we check whether it is a
495 % known prefix operator, in which case \cs{@@_parse_operand:Nw}
496 % finds its operand. If it is not known, then either a number is
497 % missing (if the token is a known infix operator) or the token is
498 % simply invalid in floating point expressions.
499 % \end{itemize}
500 % Once a number is found, \cs{@@_parse_one:Nw} also finds an infix
501 % operator. This goes as follows.
502 % \begin{itemize}
503 % \item If the next token is a control sequence, it could be the
504 % special marker \cs{s_@@_expr_mark}, and
505 % otherwise it is a case of juxtaposing numbers, such as
506 % |2\c_zero_int|, with an implied multiplication.
507 % \item If the next token is a letter, it is also a case of
508 % juxtaposition, as letters cannot be proper infix operators.
509 % \item Otherwise (including in the case of digits), if the token is a
510 % known infix operator, the appropriate
511 % |\__fp_infix_|\meta{operator}|:N| function is built, and if it
512 % does not exist, we complain. In particular, the juxtaposition
513 % |\c_zero_int 2| is disallowed.
514 % \end{itemize}
515 %
516 % In the above, we need to test whether a character token~|#1| is a
517 % digit:
518 % \begin{verbatim}
519 % \if_int_compare:w 9 < 1 \token_to_str:N #1 \exp_stop_f:
520 % is a digit
521 % \else:
522 % not a digit
523 % \fi:
524 % \end{verbatim}
525 % To exclude |0|, replace |9| by |10|. The use of
526 % \cs{token_to_str:N} ensures that a digit with any catcode is detected.
527 % To test if a character token is a letter, we need to work with its
528 % character code, testing if |`#1| lies in $[65,90]$ (uppercase letters)
529 % or $[97,112]$ (lowercase letters)
530 % \begin{verbatim}
531 % \if_int_compare:w \__fp_int_eval:w
532 % ( `#1 \if_int_compare:w `#1 > `Z - 32 \fi: ) / 26 = 3 \exp_stop_f:
533 % is a letter
534 % \else:
535 % not a letter
536 % \fi:
537 % \end{verbatim}
538 % At all steps, we try to accept all category codes: when |#1|~is kept
539 % to be used later, it is almost always converted to category code other
540 % through \cs{token_to_str:N}. More precisely, catcodes $\{3, 6, 7, 8,
541 % 11, 12\}$ should work without trouble, but not $\{1, 2, 4, 10, 13\}$,
542 % and of course $\{0, 5, 9\}$ cannot become tokens.
543 %
544 % Floating point expressions should behave as much as possible like
545 % \eTeX{}-based integer expressions and dimension expressions. In
546 % particular, \texttt{f}-expansion should be performed as the expression
547 % is read, token by token, forcing the expansion of protected macros,
548 % and ignoring spaces. One advantage of expanding at every step is that
549 % restricted expandable functions can then be used in floating point
550 % expressions just as they can be in other kinds of expressions.
551 % Problematically, spaces stop \texttt{f}-expansion: for instance, the
552 % macro~|\X| below would not be expanded if we simply performed
553 % \texttt{f}-expansion.
554 % \begin{verbatim}
555 % \DeclareDocumentCommand {\test} {m} { \fp_eval:n {#1} }
556 % \ExplSyntaxOff
557 % \test { 1 + \X }
558 % \end{verbatim}
559 % Of course, spaces typically do not appear in a code setting, but may very
560 % easily come in document-level input, from which some expressions may
561 % come. To avoid this problem, at every step, we do essentially what
562 % \cs{use:f} would do: take an argument, put it back in the input
563 % stream, then \texttt{f}-expand it. This is not a complete solution,
564 % since a macro's expansion could contain leading spaces which would stop
565 % the \texttt{f}-expansion before further macro calls are performed.
566 % However, in practice it should be enough: in particular, floating
567 % point numbers are correctly expanded to the underlying \cs{s_@@}
568 % \ldots{} structure. The \texttt{f}-expansion is performed by
569 % \cs{@@_parse_expand:w}.
570 %
571 % ^^A begin[todo]
572 %
573 % \subsection{Main auxiliary functions}
574 %
575 % \begin{macro}[rEXP]{\@@_parse_operand:Nw}
576 % \begin{syntax}
577 % \cs{exp:w} \cs{@@_parse_operand:Nw} \meta{precedence} \cs{@@_parse_expand:w}
578 % \end{syntax}
579 % Reads the \enquote{\ttfamily\ldots{}}, performing every computation
580 % with a precedence higher than \meta{precedence}, then expands to
581 % \begin{syntax}
582 % \meta{result} |@| |\__fp_parse_infix_|\meta{operation}|:N| \ldots{}
583 % \end{syntax}
584 % where the \meta{operation} is the first operation with a lower
585 % precedence, possibly \texttt{end}, and the
586 % \enquote{\ttfamily\ldots{}} start just after the \meta{operation}.
587 % \end{macro}
588 %
589 % \begin{macro}[EXP]{\@@_parse_infix_+:N}
590 % \begin{syntax}
591 % \cs{@@_parse_infix_+:N} \meta{precedence} \ldots{}
592 % \end{syntax}
593 % If |+|~has a precedence higher than the \meta{precedence}, cleans up
594 % a second \meta{operand} and finds the \meta{operation_2} which
595 % follows, and expands to
596 % \begin{syntax}
597 % |@| \cs{@@_parse_apply_binary:NwNwN} |+| \meta{operand} |@| \cs{@@_parse_infix_\meta{operation_2}:N} \ldots{}
598 % \end{syntax}
599 % Otherwise expands to
600 % \begin{syntax}
601 % |@| \cs{use_none:n} \cs{@@_parse_infix_+:N} \ldots{}
602 % \end{syntax}
603 % A similar function exists for each infix operator.
604 % \end{macro}
605 %
606 % \begin{macro}[EXP]{\@@_parse_one:Nw}
607 % \begin{syntax}
608 % \cs{@@_parse_one:Nw} \meta{precedence} \ldots{}
609 % \end{syntax}
610 % Cleans up one or two operands depending on how the precedence of the
611 % next operation compares to the \meta{precedence}. If the following
612 % \meta{operation} has a precedence higher than \meta{precedence},
613 % expands to
614 % \begin{syntax}
615 % \meta{operand_1} |@| \cs{@@_parse_apply_binary:NwNwN} \meta{operation} \meta{operand_2} |@| |\__fp_parse_infix_|\meta{operation_2}|:N| \ldots{}
616 % \end{syntax}
617 % and otherwise expands to
618 % \begin{syntax}
619 % \meta{operand} |@| \cs{use_none:n} |\__fp_parse_infix_|\meta{operation}|:N| \ldots{}
620 % \end{syntax}
621 % \end{macro}
622 %
623 % ^^A end[todo]
624 %
625 % \subsection{Helpers}
626 %
627 % \begin{macro}[rEXP]{\@@_parse_expand:w}
628 % \begin{syntax}
629 % \cs{exp:w} \cs{@@_parse_expand:w} \meta{tokens}
630 % \end{syntax}
631 % This function must always come within a \cs{exp:w} expansion.
632 % The \meta{tokens} should be the part of the expression that we have
633 % not yet read. This requires in particular closing all conditionals
634 % properly before expanding.
635 % \begin{macrocode}
636 \cs_new:Npn \@@_parse_expand:w #1 { \exp_end_continue_f:w #1 }
637 % \end{macrocode}
638 % \end{macro}
639 %
640 % \begin{macro}[EXP]{\@@_parse_return_sep:w}
641 % This very odd function swaps its position with the following
642 % \cs{fi:} and removes \cs{@@_parse_expand:w} normally responsible for
643 % expansion. That turns out to be useful.
644 % \begin{macrocode}
645 \cs_new:Npn \@@_parse_return_sep:w
646 #1 \fi: \@@_parse_expand:w { \fi: \@@_sep: #1 }
647 % \end{macrocode}
648 % \end{macro}
649 %
650 % \begin{macro}[rEXP]
651 % {
652 % \@@_parse_digits_vii:N ,
653 % \@@_parse_digits_vi:N ,
654 % \@@_parse_digits_v:N ,
655 % \@@_parse_digits_iv:N ,
656 % \@@_parse_digits_iii:N ,
657 % \@@_parse_digits_ii:N ,
658 % \@@_parse_digits_i:N ,
659 % \@@_parse_digits_:N
660 % }
661 % These functions must be called within an \cs{int_value:w} or
662 % \cs{@@_int_eval:w} construction. The first token which follows must
663 % be \texttt{f}-expanded prior to calling those functions. The
664 % functions read tokens one by one, and output digits into the input
665 % stream, until meeting a non-digit, or up to a number of digits equal
666 % to their index. The full expansion is
667 % \begin{syntax}
668 % \meta{digits} \cs{@@_sep:} \meta{filling 0} \cs{@@_sep:} \meta{length}
669 % \end{syntax}
670 % where \meta{filling 0} is a string of zeros such that \meta{digits}
671 % \meta{filling 0} has the length given by the index of the function,
672 % and \meta{length} is the number of zeros in the \meta{filling 0}
673 % string. Each function puts a digit into the input stream and calls
674 % the next function, until we find a non-digit. We are careful to
675 % pass the tested tokens through \cs{token_to_str:N} to normalize
676 % their category code.
677 % \begin{macrocode}
678 \cs_set_protected:Npn \@@_tmp:w #1 #2 #3
679 {
680 \cs_new:cpn { @@_parse_digits_ #1 :N } ##1
681 {
682 \if_int_compare:w 9 < 1 \token_to_str:N ##1 \exp_stop_f:
683 \token_to_str:N ##1 \exp_after:wN #2 \exp:w
684 \else:
685 \@@_parse_return_sep:w #3 ##1
686 \fi:
687 \@@_parse_expand:w
688 }
689 }
690 \@@_tmp:w {vii} \@@_parse_digits_vi:N { 0000000 \@@_sep: 7 }
691 \@@_tmp:w {vi} \@@_parse_digits_v:N { 000000 \@@_sep: 6 }
692 \@@_tmp:w {v} \@@_parse_digits_iv:N { 00000 \@@_sep: 5 }
693 \@@_tmp:w {iv} \@@_parse_digits_iii:N { 0000 \@@_sep: 4 }
694 \@@_tmp:w {iii} \@@_parse_digits_ii:N { 000 \@@_sep: 3 }
695 \@@_tmp:w {ii} \@@_parse_digits_i:N { 00 \@@_sep: 2 }
696 \@@_tmp:w {i} \@@_parse_digits_:N { 0 \@@_sep: 1 }
697 \cs_new:Npn \@@_parse_digits_:N { \@@_sep: \@@_sep: 0 }
698 % \end{macrocode}
699 % \end{macro}
700 %
701 % \subsection{Parsing one number}
702 %
703 % \begin{macro}[EXP]{\@@_parse_one:Nw}
704 % This function finds one number, and packs the symbol which follows
705 % in an \cs[no-index]{@@_parse_infix_\ldots{}} csname.
706 % |#1|~is the previous \meta{precedence},
707 % and |#2|~the first token of the operand. We distinguish four cases:
708 % |#2|~is equal to \cs{scan_stop:} in meaning, |#2|~is a different
709 % control sequence, |#2|~is a digit, and |#2|~is something else (this
710 % last case is split further later). Despite the earlier
711 % \texttt{f}-expansion, |#2|~may still be expandable if it was
712 % protected by \cs{exp_not:N}, as may happen with the \LaTeXe{} command
713 % \tn{protect}. Using a well placed \cs{reverse_if:N}, this case is
714 % sent to \cs{@@_parse_one_fp:NN} which deals with it robustly.
715 % \begin{macrocode}
716 \cs_new:Npn \@@_parse_one:Nw #1 #2
717 {
718 \if_catcode:w \scan_stop: \exp_not:N #2
719 \exp_after:wN \if_meaning:w \exp_not:N #2 #2 \else:
720 \exp_after:wN \reverse_if:N
721 \fi:
722 \if_meaning:w \scan_stop: #2
723 \exp_after:wN \exp_after:wN
724 \exp_after:wN \@@_parse_one_fp:NN
725 \else:
726 \exp_after:wN \exp_after:wN
727 \exp_after:wN \@@_parse_one_register:NN
728 \fi:
729 \else:
730 \if_int_compare:w 9 < 1 \token_to_str:N #2 \exp_stop_f:
731 \exp_after:wN \exp_after:wN
732 \exp_after:wN \@@_parse_one_digit:NN
733 \else:
734 \exp_after:wN \exp_after:wN
735 \exp_after:wN \@@_parse_one_other:NN
736 \fi:
737 \fi:
738 #1 #2
739 }
740 % \end{macrocode}
741 % \end{macro}
742 %
743 % \begin{macro}[EXP]
744 % {
745 % \@@_parse_one_fp:NN,
746 % \@@_exp_after_expr_mark_f:nw,
747 % \@@_exp_after_?_f:nw
748 % }
749 % This function receives a \meta{precedence} and a control sequence
750 % equal to \cs{scan_stop:} in meaning. There are three cases.
751 % \begin{itemize}
752 % \item \cs{s_@@} starts a floating point number, and we call
753 % \cs{@@_exp_after_f:nw}, which |f|-expands after the floating
754 % point.
755 % \item \cs{s_@@_expr_mark} is a premature end, we call
756 % \cs{@@_exp_after_expr_mark_f:nw}, which triggers an |fp-early-end|
757 % error.
758 % \item For a control sequence not containing \cs[no-index]{s_@@}, we call
759 % \cs{@@_exp_after_?_f:nw}, causing a |bad-variable| error.
760 % \end{itemize}
761 % This scheme is extensible: additional types can be added by starting
762 % the variables with a scan mark of the form \cs[no-index]{s_@@_\meta{type}} and
763 % defining |\__fp_exp_after_|\meta{type}|_f:nw|. In all cases, we
764 % make sure that the second argument of \cs{@@_parse_infix:NN} is
765 % correctly expanded.
766 % A special case only enabled in \LaTeXe{} is that if \tn{protect} is
767 % encountered then the error message mentions the control sequence
768 % which follows it rather than \tn{protect} itself. The test for
769 % \LaTeXe{} uses \tn{@unexpandable@protect} rather than \tn{protect}
770 % because \tn{protect} is often \cs{scan_stop:} hence \enquote{does
771 % not exist}.
772 % \begin{macrocode}
773 \cs_new:Npn \@@_parse_one_fp:NN #1
774 {
775 \@@_exp_after_any_f:nw
776 {
777 \exp_after:wN \@@_parse_infix:NN
778 \exp_after:wN #1 \exp:w \@@_parse_expand:w
779 }
780 }
781 \cs_new:Npn \@@_exp_after_expr_mark_f:nw #1
782 {
783 \int_case:nnF { \exp_after:wN \use_i:nnn \use_none:nnn #1 }
784 {
785 \c_@@_prec_comma_int { }
786 \c_@@_prec_tuple_int { }
787 \c_@@_prec_end_int
788 {
789 \exp_after:wN \c_@@_empty_tuple_fp
790 \exp:w \exp_end_continue_f:w
791 }
792 }
793 {
794 \msg_expandable_error:nn { fp } { early-end }
795 \exp_after:wN \c_nan_fp \exp:w \exp_end_continue_f:w
796 }
797 #1
798 }
799 \cs_new:cpn { @@_exp_after_?_f:nw } #1#2
800 {
801 \msg_expandable_error:nnn { kernel } { bad-variable }
802 {#2}
803 \exp_after:wN \c_nan_fp \exp:w \exp_end_continue_f:w #1
804 }
805 \cs_set_protected:Npn \@@_tmp:w #1
806 {
807 \cs_if_exist:NT #1
808 {
809 \cs_gset:cpn { @@_exp_after_?_f:nw } ##1##2
810 {
811 \exp_after:wN \c_nan_fp \exp:w \exp_end_continue_f:w ##1
812 \str_if_eq:nnTF {##2} { \protect }
813 {
814 \cs_if_eq:NNTF ##2 #1 { \use_i:nn } { \use:n }
815 {
816 \msg_expandable_error:nnn { fp }
817 { robust-cmd }
818 }
819 }
820 {
821 \msg_expandable_error:nnn { kernel }
822 { bad-variable } {##2}
823 }
824 }
825 }
826 }
827 \exp_args:Nc \@@_tmp:w { @unexpandable@protect }
828 % \end{macrocode}
829 % \end{macro}
830 %
831 % \begin{macro}[EXP]
832 % {
833 % \@@_parse_one_register:NN,
834 % \@@_parse_one_register_aux:Nw,
835 % \@@_parse_one_register_auxii:wwwNw,
836 % \@@_parse_one_register_int:www,
837 % \@@_parse_one_register_mu:www,
838 % \@@_parse_one_register_dim:ww,
839 % }
840 % This is called whenever~|#2| is a control sequence other than
841 % \cs{scan_stop:} in meaning. We special-case \tn{wd}, \tn{ht}, \tn{dp}
842 % (see later) and otherwise assume that it is a register, but
843 % carefully unpack it with \cs{tex_the:D} within braces. First, we
844 % find the exponent following~|#2|. Then we unpack~|#2| with
845 % \cs{tex_the:D}, and the \texttt{auxii} auxiliary distinguishes
846 % integer registers from dimensions/skips from muskips, according to
847 % the presence of a period and/or of |pt|. For integers, simply
848 % convert \meta{value}|e|\meta{exponent} to a floating point number
849 % with \cs{@@_parse:n} (this is somewhat wasteful). For other
850 % registers, the decimal rounding provided by \TeX{} does not
851 % accurately represent the binary value that it manipulates, so we
852 % extract this binary value as a number of scaled points with
853 % \cs{int_value:w} \cs{dim_to_decimal_in_sp:n} |{| \meta{decimal value} |pt| |}|, and
854 % use an auxiliary of \cs{dim_to_fp:n}, which performs the
855 % multiplication by $2^{-16}$, correctly rounded.
856 % \begin{macrocode}
857 \cs_new:Npn \@@_parse_one_register:NN #1#2
858 {
859 \exp_after:wN \@@_parse_infix_after_operand:NwN
860 \exp_after:wN #1
861 \exp:w \exp_end_continue_f:w
862 \@@_parse_one_register_special:N #2
863 \exp_after:wN \@@_parse_one_register_aux:Nw
864 \exp_after:wN #2
865 \int_value:w
866 \exp_after:wN \@@_parse_exponent:N
867 \exp:w \@@_parse_expand:w
868 }
869 \cs_new:Npe \@@_parse_one_register_aux:Nw #1
870 {
871 \exp_not:n
872 {
873 \exp_after:wN \use:nn
874 \exp_after:wN \@@_parse_one_register_auxii:wwwNw
875 }
876 \exp_not:N \exp_after:wN { \exp_not:N \tex_the:D #1 }
877 \@@_sep: \exp_not:N \@@_parse_one_register_dim:ww
878 \tl_to_str:n { pt } \@@_sep: \exp_not:N \@@_parse_one_register_mu:www
879 . \tl_to_str:n { pt } \@@_sep: \exp_not:N \@@_parse_one_register_int:www
880 \s_@@_stop
881 }
882 \exp_args:Nno \use:nn
883 { \cs_new:Npn \@@_parse_one_register_auxii:wwwNw #1 . #2 }
884 { \tl_to_str:n { pt } #3 \@@_sep: #4#5 \s_@@_stop }
885 { #4 #1.#2\@@_sep: }
886 \exp_args:Nno \use:nn
887 { \cs_new:Npn \@@_parse_one_register_mu:www #1 }
888 { \tl_to_str:n { mu } \@@_sep: #2 \@@_sep: }
889 { \@@_parse_one_register_dim:ww #1 \@@_sep: }
890 \cs_new:Npn \@@_parse_one_register_int:www #1\@@_sep: #2.\@@_sep: #3\@@_sep:
891 { \@@_parse:n { #1 e #3 } }
892 \cs_new:Npn \@@_parse_one_register_dim:ww #1\@@_sep: #2\@@_sep:
893 {
894 \exp_after:wN \@@_from_dim_test:ww
895 \int_value:w #2 \exp_after:wN ,
896 \int_value:w \dim_to_decimal_in_sp:n { #1 pt } \@@_sep:
897 }
898 % \end{macrocode}
899 % \end{macro}
900 %
901 % \begin{macro}
902 % {
903 % \@@_parse_one_register_special:N,
904 % \@@_parse_one_register_math:NNw,
905 % \@@_parse_one_register_wd:w,
906 % \@@_parse_one_register_wd:Nw
907 % }
908 % The \tn{wd}, \tn{dp}, \tn{ht} primitives expect an integer argument.
909 % We abuse the exponent parser to find the integer argument: simply
910 % include the exponent marker~|e|. Once that \enquote{exponent} is
911 % found, use \cs{tex_the:D} to find the box dimension and then copy
912 % what we did for dimensions.
913 % \begin{macrocode}
914 \cs_new:Npn \@@_parse_one_register_special:N #1
915 {
916 \if_meaning:w \box_wd:N #1 \@@_parse_one_register_wd:w \fi:
917 \if_meaning:w \box_ht:N #1 \@@_parse_one_register_wd:w \fi:
918 \if_meaning:w \box_dp:N #1 \@@_parse_one_register_wd:w \fi:
919 \if_meaning:w \infty #1
920 \@@_parse_one_register_math:NNw \infty #1
921 \fi:
922 \if_meaning:w \pi #1
923 \@@_parse_one_register_math:NNw \pi #1
924 \fi:
925 }
926 \cs_new:Npn \@@_parse_one_register_math:NNw
927 #1#2#3#4 \@@_parse_expand:w
928 {
929 #3
930 \str_if_eq:nnTF {#1} {#2}
931 {
932 \msg_expandable_error:nnn
933 { fp } { infty-pi } {#1}
934 \c_nan_fp
935 }
936 { #4 \@@_parse_expand:w }
937 }
938 \cs_new:Npn \@@_parse_one_register_wd:w
939 #1#2 \exp_after:wN #3#4 \@@_parse_expand:w
940 {
941 #1
942 \exp_after:wN \@@_parse_one_register_wd:Nw
943 #4 \@@_parse_expand:w e
944 }
945 \cs_new:Npn \@@_parse_one_register_wd:Nw #1#2 \@@_sep:
946 {
947 \exp_after:wN \@@_from_dim_test:ww
948 \exp_after:wN 0 \exp_after:wN ,
949 \int_value:w \dim_to_decimal_in_sp:n { #1 #2 } \@@_sep:
950 }
951 % \end{macrocode}
952 % \end{macro}
953 %
954 % \begin{macro}[EXP]{\@@_parse_one_digit:NN}
955 % A digit marks the beginning of an explicit floating point number.
956 % Once the number is found, we catch the case of overflow and
957 % underflow with \cs{@@_sanitize:wN}, then
958 % \cs{@@_parse_infix_after_operand:NwN} expands \cs{@@_parse_infix:NN}
959 % after the number we find, to wrap the following infix operator as
960 % required. Finding the number itself begins by removing leading
961 % zeros: further steps are described later.
962 % \begin{macrocode}
963 \cs_new:Npn \@@_parse_one_digit:NN #1
964 {
965 \exp_after:wN \@@_parse_infix_after_operand:NwN
966 \exp_after:wN #1
967 \exp:w \exp_end_continue_f:w
968 \exp_after:wN \@@_sanitize:wN
969 \int_value:w \@@_int_eval:w 0 \@@_parse_trim_zeros:N
970 }
971 % \end{macrocode}
972 % \end{macro}
973 %
974 % \begin{macro}[EXP]{\@@_parse_one_other:NN}
975 % For this function, |#2|~is a character token which is not a digit.
976 % If it is an \textsc{ascii} letter, \cs{@@_parse_letters:N} beyond this one and give
977 % the result to \cs{@@_parse_word:Nw}. Otherwise, the character is
978 % assumed to be a prefix operator, and we build
979 % |\__fp_parse_prefix_|\meta{operator}|:Nw|.
980 % \begin{macrocode}
981 \cs_new:Npn \@@_parse_one_other:NN #1 #2
982 {
983 \if_int_compare:w
984 \@@_int_eval:w
985 ( `#2 \if_int_compare:w `#2 > `Z - 32 \fi: ) / 26
986 = 3 \exp_stop_f:
987 \exp_after:wN \@@_parse_word:Nw
988 \exp_after:wN #1
989 \exp_after:wN #2
990 \exp:w \exp_after:wN \@@_parse_letters:N
991 \exp:w
992 \else:
993 \exp_after:wN \@@_parse_prefix:NNN
994 \exp_after:wN #1
995 \exp_after:wN #2
996 \cs:w
997 @@_parse_prefix_ \token_to_str:N #2 :Nw
998 \exp_after:wN
999 \cs_end:
1000 \exp:w
1001 \fi:
1002 \@@_parse_expand:w
1003 }
1004 % \end{macrocode}
1005 % \end{macro}
1006 %
1007 % \begin{macro}[EXP]{\@@_parse_word:Nw}
1008 % \begin{macro}[rEXP]{\@@_parse_letters:N}
1009 % Finding letters is a simple recursion. Once \cs{@@_parse_letters:N}
1010 % has done its job, we try to build a control sequence from the
1011 % word~|#2|. If it is a known word, then the corresponding action is
1012 % taken, and otherwise, we complain about an unknown word, yield
1013 % \cs{c_nan_fp}, and look for the following infix operator. Note that
1014 % the unknown word could be a mistyped function as well as a mistyped
1015 % constant, so there is no way to tell whether to look for arguments;
1016 % we do not.
1017 % The standard requires \enquote{inf} and \enquote{infinity} and
1018 % \enquote{nan} to be recognized regardless of case, but we probably
1019 % don't want to allow every \pkg{l3fp} word to have an arbitrary
1020 % mixture of lower and upper case, so we test and use a
1021 % differently-named control sequence.
1022 % \begin{macrocode}
1023 \cs_new:Npn \@@_parse_word:Nw #1#2\@@_sep:
1024 {
1025 \cs_if_exist_use:cF { @@_parse_word_#2:N }
1026 {
1027 \cs_if_exist_use:cF
1028 { @@_parse_caseless_ \str_casefold:n {#2} :N }
1029 {
1030 \msg_expandable_error:nnn
1031 { fp } { unknown-fp-word } {#2}
1032 \exp_after:wN \c_nan_fp \exp:w \exp_end_continue_f:w
1033 \@@_parse_infix:NN
1034 }
1035 }
1036 #1
1037 }
1038 \cs_new:Npn \@@_parse_letters:N #1
1039 {
1040 \exp_end_continue_f:w
1041 \if_int_compare:w
1042 \if_catcode:w \scan_stop: \exp_not:N #1
1043 0
1044 \else:
1045 \@@_int_eval:w
1046 ( `#1 \if_int_compare:w `#1 > `Z - 32 \fi: ) / 26
1047 \fi:
1048 = 3 \exp_stop_f:
1049 \exp_after:wN #1
1050 \exp:w \exp_after:wN \@@_parse_letters:N
1051 \exp:w
1052 \else:
1053 \@@_parse_return_sep:w #1
1054 \fi:
1055 \@@_parse_expand:w
1056 }
1057 % \end{macrocode}
1058 % \end{macro}
1059 % \end{macro}
1060 %
1061 % \begin{macro}[EXP]
1062 % {\@@_parse_prefix:NNN, \@@_parse_prefix_unknown:NNN}
1063 % For this function, |#1|~is the previous \meta{precedence}, |#2|~is
1064 % the operator just seen, and |#3|~is a control sequence which
1065 % implements the operator if it is a known operator. If this control
1066 % sequence is \cs{scan_stop:}, then the operator is in fact unknown.
1067 % Either the expression is missing a number there (if the operator is
1068 % valid as an infix operator), and we put \texttt{nan}, wrapping the
1069 % infix operator in a csname as appropriate, or the character is
1070 % simply invalid in floating point expressions, and we continue
1071 % looking for a number, starting again from \cs{@@_parse_one:Nw}.
1072 % \begin{macrocode}
1073 \cs_new:Npn \@@_parse_prefix:NNN #1#2#3
1074 {
1075 \if_meaning:w \scan_stop: #3
1076 \exp_after:wN \@@_parse_prefix_unknown:NNN
1077 \exp_after:wN #2
1078 \fi:
1079 #3 #1
1080 }
1081 \cs_new:Npn \@@_parse_prefix_unknown:NNN #1#2#3
1082 {
1083 \cs_if_exist:cTF { @@_parse_infix_ \token_to_str:N #1 :N }
1084 {
1085 \msg_expandable_error:nnn
1086 { fp } { missing-number } {#1}
1087 \exp_after:wN \c_nan_fp \exp:w \exp_end_continue_f:w
1088 \@@_parse_infix:NN #3 #1
1089 }
1090 {
1091 \msg_expandable_error:nnn
1092 { fp } { unknown-symbol } {#1}
1093 \@@_parse_one:Nw #3
1094 }
1095 }
1096 % \end{macrocode}
1097 % \end{macro}
1098 %
1099 % \subsubsection{Numbers: trimming leading zeros}
1100 %
1101 % Numbers are parsed as follows: first we trim leading zeros, then
1102 % if the next character is a digit, start reading a significand $\geq 1$
1103 % with the set of functions |\__fp_parse_large|\ldots{}; if it is a
1104 % period, the significand is~$<1$; and otherwise it is zero. In the
1105 % second case, trim additional zeros after the period, counting them for
1106 % an exponent shift $\meta{exp_1}<0$, then read the significand with the
1107 % set of functions |\__fp_parse_small|\ldots{} Once the significand is
1108 % read, read the exponent if |e|~is present.
1109 %
1110 % \begin{macro}[rEXP]{\@@_parse_trim_zeros:N, \@@_parse_trim_end:w}
1111 % This function expects an already expanded token. It removes any
1112 % leading zero, then distinguishes three cases: if the first non-zero
1113 % token is a digit, then call \cs{@@_parse_large:N} (the significand
1114 % is $\geq 1$); if it is |.|, then continue trimming zeros with
1115 % \cs{@@_parse_strim_zeros:N}; otherwise, our number is exactly zero,
1116 % and we call \cs{@@_parse_zero:} to take care of that case.
1117 % \begin{macrocode}
1118 \cs_new:Npn \@@_parse_trim_zeros:N #1
1119 {
1120 \if:w 0 \exp_not:N #1
1121 \exp_after:wN \@@_parse_trim_zeros:N
1122 \exp:w
1123 \else:
1124 \if:w . \exp_not:N #1
1125 \exp_after:wN \@@_parse_strim_zeros:N
1126 \exp:w
1127 \else:
1128 \@@_parse_trim_end:w #1
1129 \fi:
1130 \fi:
1131 \@@_parse_expand:w
1132 }
1133 \cs_new:Npn \@@_parse_trim_end:w #1 \fi: \fi: \@@_parse_expand:w
1134 {
1135 \fi:
1136 \fi:
1137 \if_int_compare:w 9 < 1 \token_to_str:N #1 \exp_stop_f:
1138 \exp_after:wN \@@_parse_large:N
1139 \else:
1140 \exp_after:wN \@@_parse_zero:
1141 \fi:
1142 #1
1143 }
1144 % \end{macrocode}
1145 % \end{macro}
1146 %
1147 % \begin{macro}[rEXP]
1148 % {\@@_parse_strim_zeros:N, \@@_parse_strim_end:w}
1149 % If we have removed all digits until a period (or if the body started
1150 % with a period), then enter the \enquote{\texttt{small_trim}} loop
1151 % which outputs $-1$ for each removed~$0$. Those $-1$ are added to an
1152 % integer expression waiting for the exponent. If the first non-zero
1153 % token is a digit, call \cs{@@_parse_small:N} (our significand is
1154 % smaller than~$1$), and otherwise, the number is an exact zero. The
1155 % name \texttt{strim} stands for \enquote{small trim}.
1156 % \begin{macrocode}
1157 \cs_new:Npn \@@_parse_strim_zeros:N #1
1158 {
1159 \if:w 0 \exp_not:N #1
1160 - 1
1161 \exp_after:wN \@@_parse_strim_zeros:N \exp:w
1162 \else:
1163 \@@_parse_strim_end:w #1
1164 \fi:
1165 \@@_parse_expand:w
1166 }
1167 \cs_new:Npn \@@_parse_strim_end:w #1 \fi: \@@_parse_expand:w
1168 {
1169 \fi:
1170 \if_int_compare:w 9 < 1 \token_to_str:N #1 \exp_stop_f:
1171 \exp_after:wN \@@_parse_small:N
1172 \else:
1173 \exp_after:wN \@@_parse_zero:
1174 \fi:
1175 #1
1176 }
1177 % \end{macrocode}
1178 % \end{macro}
1179 %
1180 % \begin{macro}[EXP]{\@@_parse_zero:}
1181 % After reading a significand of~$0$, find any exponent, then put a
1182 % sign of~|1| for \cs{@@_sanitize:wN}, which removes everything
1183 % and leaves an exact zero.
1184 % \begin{macrocode}
1185 \cs_new:Npn \@@_parse_zero:
1186 {
1187 \exp_after:wN \@@_sep: \exp_after:wN 1
1188 \int_value:w \@@_parse_exponent:N
1189 }
1190 % \end{macrocode}
1191 % \end{macro}
1192 %
1193 % \subsubsection{Number: small significand}
1194 %
1195 % \begin{macro}[rEXP]{\@@_parse_small:N}
1196 % This function is called after we have passed the decimal separator
1197 % and removed all leading zeros from the significand. It is followed
1198 % by a non-zero digit (with any catcode). The goal is to read up to
1199 % $16$ digits. But we can't do that all at once, because
1200 % \cs{int_value:w} (which allows us to collect digits and continue
1201 % expanding) can only go up to $9$ digits. Hence we grab digits in
1202 % two steps of $8$ digits. Since |#1| is a digit, read seven more
1203 % digits using \cs{@@_parse_digits_vii:N}. The \texttt{small_leading}
1204 % auxiliary leaves those digits in the \cs{int_value:w}, and
1205 % grabs some more, or stops if there are no more digits. Then the
1206 % \texttt{pack_leading} auxiliary puts the various parts in the
1207 % appropriate order for the processing further up.
1208 % \begin{macrocode}
1209 \cs_new:Npn \@@_parse_small:N #1
1210 {
1211 \exp_after:wN \@@_parse_pack_leading:NNNNNww
1212 \int_value:w \@@_int_eval:w 1 \token_to_str:N #1
1213 \exp_after:wN \@@_parse_small_leading:wwNN
1214 \int_value:w 1
1215 \exp_after:wN \@@_parse_digits_vii:N
1216 \exp:w \@@_parse_expand:w
1217 }
1218 % \end{macrocode}
1219 % \end{macro}
1220 %
1221 % \begin{macro}[rEXP]{\@@_parse_small_leading:wwNN}
1222 % \begin{syntax}
1223 % \cs{@@_parse_small_leading:wwNN} |1| \meta{digits} \cs{@@_sep:} \meta{zeros} \cs{@@_sep:} \meta{number~of~zeros}
1224 % \end{syntax}
1225 % We leave \meta{digits} \meta{zeros} in the input stream: the
1226 % functions used to grab digits are such that this constitutes digits
1227 % $1$ through~$8$ of the significand. Then prepare to pack $8$~more
1228 % digits, with an exponent shift of zero (this shift is used in
1229 % the case of a large significand). If |#4|~is a digit, leave it
1230 % behind for the packing function, and read $6$~more digits to reach a
1231 % total of $15$~digits: further digits are involved in the rounding.
1232 % Otherwise put $8$~zeros in to complete the significand, then look
1233 % for an exponent.
1234 % \begin{macrocode}
1235 \cs_new:Npn \@@_parse_small_leading:wwNN 1 #1 \@@_sep: #2\@@_sep: #3 #4
1236 {
1237 #1 #2
1238 \exp_after:wN \@@_parse_pack_trailing:NNNNNNww
1239 \exp_after:wN 0
1240 \int_value:w \@@_int_eval:w 1
1241 \if_int_compare:w 9 < 1 \token_to_str:N #4 \exp_stop_f:
1242 \token_to_str:N #4
1243 \exp_after:wN \@@_parse_small_trailing:wwNN
1244 \int_value:w 1
1245 \exp_after:wN \@@_parse_digits_vi:N
1246 \exp:w
1247 \else:
1248 0000 0000 \@@_parse_exponent:Nw #4
1249 \fi:
1250 \@@_parse_expand:w
1251 }
1252 % \end{macrocode}
1253 % \end{macro}
1254 %
1255 % \begin{macro}[rEXP]{\@@_parse_small_trailing:wwNN}
1256 % \begin{syntax}
1257 % \cs{@@_parse_small_trailing:wwNN} |1| \meta{digits} \cs{@@_sep:} \meta{zeros} \cs{@@_sep:} \meta{number~of~zeros} \meta{next~token}
1258 % \end{syntax}
1259 % Leave digits $10$ to~$15$ (arguments |#1| and |#2|) in the input
1260 % stream. If the \meta{next~token} is a digit, it is the $16$th
1261 % digit, we keep it, then the \texttt{small_round} auxiliary considers
1262 % this digit and all further digits to perform the rounding: the
1263 % function expands to nothing, to |+0| or to |+1|.
1264 % Otherwise, there is no $16$-th digit, so we put a~$0$, and look for
1265 % an exponent.
1266 % \begin{macrocode}
1267 \cs_new:Npn \@@_parse_small_trailing:wwNN 1 #1 \@@_sep: #2\@@_sep: #3 #4
1268 {
1269 #1 #2
1270 \if_int_compare:w 9 < 1 \token_to_str:N #4 \exp_stop_f:
1271 \token_to_str:N #4
1272 \exp_after:wN \@@_parse_small_round:NN
1273 \exp_after:wN #4
1274 \exp:w
1275 \else:
1276 0 \@@_parse_exponent:Nw #4
1277 \fi:
1278 \@@_parse_expand:w
1279 }
1280 % \end{macrocode}
1281 % \end{macro}
1282 %
1283 % \begin{macro}[rEXP]
1284 % {
1285 % \@@_parse_pack_trailing:NNNNNNww ,
1286 % \@@_parse_pack_leading:NNNNNww ,
1287 % \@@_parse_pack_carry:w
1288 % }
1289 % Those functions are expanded after all the digits are found, we took
1290 % care of the rounding, as well as the exponent. The last argument is
1291 % the exponent. The previous five arguments are $8$~digits which we
1292 % pack in groups of~$4$, and the argument before that is~$1$, except
1293 % in the rare case where rounding lead to a carry, in which case the
1294 % argument is~$2$. The \texttt{trailing} function has an exponent
1295 % shift as its first argument, which we add to the exponent found in
1296 % the |e...| syntax. If the trailing digits cause a carry, the
1297 % integer expression for the leading digits is incremented (|+1|
1298 % in the code below). If the leading digits propagate this carry all
1299 % the way up, the function \cs{@@_parse_pack_carry:w} increments the
1300 % exponent, and changes the significand from |0000...| to |1000...|:
1301 % this is simple because such a carry can only occur to give rise to a
1302 % power of~$10$.
1303 % \begin{macrocode}
1304 \cs_new:Npn \@@_parse_pack_trailing:NNNNNNww
1305 #1 #2 #3#4#5#6 #7\@@_sep: #8 \@@_sep:
1306 {
1307 \if_meaning:w 2 #2 + 1 \fi:
1308 \@@_sep: #8 + #1 \@@_sep: {#3#4#5#6} {#7}\@@_sep:
1309 }
1310 \cs_new:Npn \@@_parse_pack_leading:NNNNNww #1 #2#3#4#5 #6\@@_sep: #7\@@_sep:
1311 {
1312 + #7
1313 \if_meaning:w 2 #1 \@@_parse_pack_carry:w \fi:
1314 \@@_sep: 0 {#2#3#4#5} {#6}
1315 }
1316 \cs_new:Npn \@@_parse_pack_carry:w \fi: \@@_sep: 0 #1
1317 { \fi: + 1 \@@_sep: 0 {1000} }
1318 % \end{macrocode}
1319 % \end{macro}
1320 %
1321 % \subsubsection{Number: large significand}
1322 %
1323 % Parsing a significand larger than~$1$ is a little bit more difficult
1324 % than parsing small significands. We need to count the number of
1325 % digits before the decimal separator, and add that to the final
1326 % exponent. We also need to test for the presence of a dot each time we
1327 % run out of digits, and branch to the appropriate \texttt{parse_small}
1328 % function in those cases.
1329 %
1330 % \begin{macro}[EXP]{\@@_parse_large:N}
1331 % This function is followed by the first non-zero digit of a
1332 % \enquote{large} significand ($\geq 1$). It is called within an
1333 % integer expression for the exponent. Grab up to $7$~more digits,
1334 % for a total of $8$~digits.
1335 % \begin{macrocode}
1336 \cs_new:Npn \@@_parse_large:N #1
1337 {
1338 \exp_after:wN \@@_parse_large_leading:wwNN
1339 \int_value:w 1 \token_to_str:N #1
1340 \exp_after:wN \@@_parse_digits_vii:N
1341 \exp:w \@@_parse_expand:w
1342 }
1343 % \end{macrocode}
1344 % \end{macro}
1345 %
1346 % \begin{macro}[rEXP]{\@@_parse_large_leading:wwNN}
1347 % \begin{syntax}
1348 % \cs{@@_parse_large_leading:wwNN} |1| \meta{digits} \cs{@@_sep:} \meta{zeros} \cs{@@_sep:} \meta{number~of~zeros} \meta{next~token}
1349 % \end{syntax}
1350 % We shift the exponent by the number of digits in~|#1|, namely the
1351 % target number, $8$, minus the \meta{number of zeros} (number of
1352 % digits missing). Then prepare to pack the $8$~first digits. If the
1353 % \meta{next token} is a digit, read up to $6$~more digits (digits
1354 % $10$ to~$15$). If it is a period, try to grab the end of our
1355 % $8$~first digits, branching to the \texttt{small} functions since
1356 % the number of digit does not affect the exponent anymore. Finally,
1357 % if this is the end of the significand, insert the \meta{zeros} to
1358 % complete the $8$~first digits, insert $8$~more, and look for an
1359 % exponent.
1360 % \begin{macrocode}
1361 \cs_new:Npn \@@_parse_large_leading:wwNN 1 #1 \@@_sep: #2\@@_sep: #3 #4
1362 {
1363 + \c_@@_half_prec_int - #3
1364 \exp_after:wN \@@_parse_pack_leading:NNNNNww
1365 \int_value:w \@@_int_eval:w 1 #1
1366 \if_int_compare:w 9 < 1 \token_to_str:N #4 \exp_stop_f:
1367 \exp_after:wN \@@_parse_large_trailing:wwNN
1368 \int_value:w 1 \token_to_str:N #4
1369 \exp_after:wN \@@_parse_digits_vi:N
1370 \exp:w
1371 \else:
1372 \if:w . \exp_not:N #4
1373 \exp_after:wN \@@_parse_small_leading:wwNN
1374 \int_value:w 1
1375 \cs:w
1376 @@_parse_digits_
1377 \@@_int_to_roman:w #3
1378 :N \exp_after:wN
1379 \cs_end:
1380 \exp:w
1381 \else:
1382 #2
1383 \exp_after:wN \@@_parse_pack_trailing:NNNNNNww
1384 \exp_after:wN 0
1385 \int_value:w 1 0000 0000
1386 \@@_parse_exponent:Nw #4
1387 \fi:
1388 \fi:
1389 \@@_parse_expand:w
1390 }
1391 % \end{macrocode}
1392 % \end{macro}
1393 %
1394 % \begin{macro}[EXP]{\@@_parse_large_trailing:wwNN}
1395 % \begin{syntax}
1396 % \cs{@@_parse_large_trailing:wwNN} |1| \meta{digits} \cs{@@_sep:} \meta{zeros} \cs{@@_sep:} \meta{number~of~zeros} \meta{next~token}
1397 % \end{syntax}
1398 % We have just read $15$~digits. If the \meta{next token} is a digit,
1399 % then the exponent shift caused by this block of $8$~digits is~$8$,
1400 % first argument to the \texttt{pack_trailing} function. We keep the
1401 % \meta{digits} and this $16$-th digit, and find how this should be
1402 % rounded using \cs{@@_parse_large_round:NN}. Otherwise, the exponent
1403 % shift is the number of \meta{digits}, $7$~minus the \meta{number of
1404 % zeros}, and we test for a decimal point. This case happens in
1405 % |123451234512345.67| with exactly $15$ digits before the decimal
1406 % separator. Then branch to the appropriate \texttt{small} auxiliary,
1407 % grabbing a few more digits to complement the digits we already
1408 % grabbed. Finally, if this is truly the end of the significand, look
1409 % for an exponent after using the \meta{zeros} and providing a $16$-th
1410 % digit of~$0$.
1411 % \begin{macrocode}
1412 \cs_new:Npn \@@_parse_large_trailing:wwNN 1 #1 \@@_sep: #2\@@_sep: #3 #4
1413 {
1414 \if_int_compare:w 9 < 1 \token_to_str:N #4 \exp_stop_f:
1415 \exp_after:wN \@@_parse_pack_trailing:NNNNNNww
1416 \exp_after:wN \c_@@_half_prec_int
1417 \int_value:w \@@_int_eval:w 1 #1 \token_to_str:N #4
1418 \exp_after:wN \@@_parse_large_round:NN
1419 \exp_after:wN #4
1420 \exp:w
1421 \else:
1422 \exp_after:wN \@@_parse_pack_trailing:NNNNNNww
1423 \int_value:w \@@_int_eval:w 7 - #3 \exp_stop_f:
1424 \int_value:w \@@_int_eval:w 1 #1
1425 \if:w . \exp_not:N #4
1426 \exp_after:wN \@@_parse_small_trailing:wwNN
1427 \int_value:w 1
1428 \cs:w
1429 @@_parse_digits_
1430 \@@_int_to_roman:w #3
1431 :N \exp_after:wN
1432 \cs_end:
1433 \exp:w
1434 \else:
1435 #2 0 \@@_parse_exponent:Nw #4
1436 \fi:
1437 \fi:
1438 \@@_parse_expand:w
1439 }
1440 % \end{macrocode}
1441 % \end{macro}
1442 %
1443 % \subsubsection{Number: beyond 16 digits, rounding}
1444 %
1445 % \begin{macro}[rEXP]{\@@_parse_round_loop:N, \@@_parse_round_up:N}
1446 % This loop is called when rounding a number (whether the mantissa is
1447 % small or large). It should appear in an integer expression. This
1448 % function reads digits one by one, until reaching a non-digit, and
1449 % adds~$1$ to the integer expression for each digit. If all digits
1450 % found are~$0$, the function ends the expression by \cs{@@_sep:}|0|,
1451 % otherwise by \cs{@@_sep:}|1|. This is done by switching the loop to
1452 % |round_up| at the first non-zero digit, thus we avoid to test
1453 % whether digits are~$0$ or not once we see a first non-zero digit.
1454 % \begin{macrocode}
1455 \cs_new:Npn \@@_parse_round_loop:N #1
1456 {
1457 \if_int_compare:w 9 < 1 \token_to_str:N #1 \exp_stop_f:
1458 + 1
1459 \if:w 0 \token_to_str:N #1
1460 \exp_after:wN \@@_parse_round_loop:N
1461 \exp:w
1462 \else:
1463 \exp_after:wN \@@_parse_round_up:N
1464 \exp:w
1465 \fi:
1466 \else:
1467 \@@_parse_return_sep:w 0 #1
1468 \fi:
1469 \@@_parse_expand:w
1470 }
1471 \cs_new:Npn \@@_parse_round_up:N #1
1472 {
1473 \if_int_compare:w 9 < 1 \token_to_str:N #1 \exp_stop_f:
1474 + 1
1475 \exp_after:wN \@@_parse_round_up:N
1476 \exp:w
1477 \else:
1478 \@@_parse_return_sep:w 1 #1
1479 \fi:
1480 \@@_parse_expand:w
1481 }
1482 % \end{macrocode}
1483 % \end{macro}
1484 %
1485 % \begin{macro}[rEXP]{\@@_parse_round_after:wN}
1486 % After the loop \cs{@@_parse_round_loop:N}, this function fetches an
1487 % exponent with \cs{@@_parse_exponent:N}, and combines it with the
1488 % number of digits counted by \cs{@@_parse_round_loop:N}. At the same
1489 % time, the result |0| or |1| is added to the
1490 % surrounding integer expression.
1491 % \begin{macrocode}
1492 \cs_new:Npn \@@_parse_round_after:wN #1\@@_sep: #2
1493 {
1494 + #2 \exp_after:wN \@@_sep:
1495 \int_value:w \@@_int_eval:w #1 + \@@_parse_exponent:N
1496 }
1497 % \end{macrocode}
1498 % \end{macro}
1499 %
1500 % \begin{macro}[rEXP]
1501 % {\@@_parse_small_round:NN, \@@_parse_round_after:wN}
1502 % Here, |#1|~is the digit that we are currently rounding (we only care
1503 % whether it is even or odd). If |#2|~is not a digit, then fetch an
1504 % exponent and expand to \cs{@@_sep:}\meta{exponent} only. Otherwise, we
1505 % expand to |+0| or |+1|, then \cs{@@_sep:}\meta{exponent}. To
1506 % decide which, call \cs{@@_round_s:NNNw} to know whether to round up,
1507 % giving it as arguments a sign~$0$ (all explicit numbers are
1508 % positive), the digit |#1|~to round, the first following digit~|#2|,
1509 % and either |+0| or |+1| depending on whether the
1510 % following digits are all zero or not. This last argument is
1511 % obtained by \cs{@@_parse_round_loop:N}, whose number of digits we
1512 % discard by multiplying it by~$0$. The exponent which follows the
1513 % number is also fetched by \cs{@@_parse_round_after:wN}.
1514 % \begin{macrocode}
1515 \cs_new:Npn \@@_parse_small_round:NN #1#2
1516 {
1517 \if_int_compare:w 9 < 1 \token_to_str:N #2 \exp_stop_f:
1518 +
1519 \exp_after:wN \@@_round_s:NNNw
1520 \exp_after:wN 0
1521 \exp_after:wN #1
1522 \exp_after:wN #2
1523 \int_value:w \@@_int_eval:w
1524 \exp_after:wN \@@_parse_round_after:wN
1525 \int_value:w \@@_int_eval:w 0 * \@@_int_eval:w 0
1526 \exp_after:wN \@@_parse_round_loop:N
1527 \exp:w
1528 \else:
1529 \@@_parse_exponent:Nw #2
1530 \fi:
1531 \@@_parse_expand:w
1532 }
1533 % \end{macrocode}
1534 % \end{macro}
1535 %
1536 %
1537 % \begin{macro}[rEXP]
1538 % {
1539 % \@@_parse_large_round:NN,
1540 % \@@_parse_large_round_test:NN,
1541 % \@@_parse_large_round_aux:wNN,
1542 % }
1543 % Large numbers are harder to round, as there may be a period in the
1544 % way. Again, |#1|~is the digit that we are currently rounding (we
1545 % only care whether it is even or odd). If there are no more digits
1546 % (|#2|~is not a digit), then we must test for a period: if there is
1547 % one, then switch to the rounding function for small significands,
1548 % otherwise fetch an exponent. If there are more digits (|#2|~is a
1549 % digit), then round, checking with \cs{@@_parse_round_loop:N} if all
1550 % further digits vanish, or some are non-zero. This loop is not
1551 % enough, as it is stopped by a period. After the loop, the
1552 % \texttt{aux} function tests for a period: if it is present, then we
1553 % must continue looking for digits, this time discarding the number of
1554 % digits we find.
1555 % \begin{macrocode}
1556 \cs_new:Npn \@@_parse_large_round:NN #1#2
1557 {
1558 \if_int_compare:w 9 < 1 \token_to_str:N #2 \exp_stop_f:
1559 +
1560 \exp_after:wN \@@_round_s:NNNw
1561 \exp_after:wN 0
1562 \exp_after:wN #1
1563 \exp_after:wN #2
1564 \int_value:w \@@_int_eval:w
1565 \exp_after:wN \@@_parse_large_round_aux:wNN
1566 \int_value:w \@@_int_eval:w 1
1567 \exp_after:wN \@@_parse_round_loop:N
1568 \else: %^^A could be dot, or e, or other
1569 \exp_after:wN \@@_parse_large_round_test:NN
1570 \exp_after:wN #1
1571 \exp_after:wN #2
1572 \fi:
1573 }
1574 \cs_new:Npn \@@_parse_large_round_test:NN #1#2
1575 {
1576 \if:w . \exp_not:N #2
1577 \exp_after:wN \@@_parse_small_round:NN
1578 \exp_after:wN #1
1579 \exp:w
1580 \else:
1581 \@@_parse_exponent:Nw #2
1582 \fi:
1583 \@@_parse_expand:w
1584 }
1585 \cs_new:Npn \@@_parse_large_round_aux:wNN #1 \@@_sep: #2 #3
1586 {
1587 + #2
1588 \exp_after:wN \@@_parse_round_after:wN
1589 \int_value:w \@@_int_eval:w #1
1590 \if:w . \exp_not:N #3
1591 + 0 * \@@_int_eval:w 0
1592 \exp_after:wN \@@_parse_round_loop:N
1593 \exp:w \exp_after:wN \@@_parse_expand:w
1594 \else:
1595 \exp_after:wN \@@_sep:
1596 \exp_after:wN 0
1597 \exp_after:wN #3
1598 \fi:
1599 }
1600 % \end{macrocode}
1601 % \end{macro}
1602 %
1603 % \subsubsection{Number: finding the exponent}
1604 %
1605 % Expansion is a little bit tricky here, in part because we accept input
1606 % where multiplication is implicit.
1607 % \begin{syntax}
1608 % \cs{@@_parse:n} |{ 3.2 erf(0.1) }|
1609 % \cs{@@_parse:n} |{ 3.2 e\l_my_int }|
1610 % \cs{@@_parse:n} |{ 3.2 \c_pi_fp }|
1611 % \end{syntax}
1612 % The first case indicates that just looking one character ahead for an
1613 % \enquote{\texttt{e}} is not enough, since we would mistake the
1614 % function \texttt{erf} for an exponent of \enquote{\texttt{rf}}. An
1615 % alternative would be to look two tokens ahead and check if what
1616 % follows is a sign or a digit, considering in that case that we must be
1617 % finding an exponent. But taking care of the second case requires that
1618 % we unpack registers after \texttt{e}. However, blindly expanding the
1619 % two tokens ahead completely would break the third example (unpacking
1620 % is even worse). Indeed, in the course of reading $3.2$, \cs{c_pi_fp}
1621 % is expanded to \cs{s_@@} \cs{@@_chk:w} |1| |0| |{-1}| |{3141}|
1622 % $\cdots$ \cs{@@_sep:} and \cs{s_@@} stops the expansion. Expanding two tokens
1623 % ahead would then force the expansion of \cs{@@_chk:w} (despite it
1624 % being protected), and that function tries to produce an error.
1625 %
1626 % What can we do? Really, the reason why this last case breaks is that
1627 % just as \TeX{} does, we should read ahead as little as possible.
1628 % Here, the only case where there may be an exponent is if the first
1629 % token ahead is |e|. Then we expand (and possibly unpack) the second
1630 % token.
1631 %
1632 % \begin{macro}[rEXP]{\@@_parse_exponent:Nw}
1633 % This auxiliary is convenient to smuggle some material through
1634 % \cs{fi:} ending conditional processing. We place those \cs{fi:}
1635 % (argument~|#2|) at a very odd place because this allows us to insert
1636 % \cs{@@_int_eval:w} \ldots{} there if needed.
1637 % \begin{macrocode}
1638 \cs_new:Npn \@@_parse_exponent:Nw #1 #2 \@@_parse_expand:w
1639 {
1640 \exp_after:wN \@@_sep:
1641 \int_value:w #2 \@@_parse_exponent:N #1
1642 }
1643 % \end{macrocode}
1644 % \end{macro}
1645 %
1646 % \begin{macro}[rEXP]
1647 % {\@@_parse_exponent:N, \@@_parse_exponent_aux:NN}
1648 % This function should be called within an \cs{int_value:w}
1649 % expansion (or within an integer expression). It leaves digits of the
1650 % exponent behind it in the input stream, and terminates the expansion
1651 % with a \cs{@@_sep:}. If there is no~|e| (or~|E|), leave an exponent of~$0$. If
1652 % there is an~|e| or~|E|, expand the next token to run some tests on it. The
1653 % first rough test is that if the character code of~|#1| is greater
1654 % than that of~|9| (largest code valid for an exponent, less than any
1655 % code valid for an identifier), there was in fact no exponent;
1656 % otherwise, we search for the sign of the exponent.
1657 % \begin{macrocode}
1658 \cs_new:Npn \@@_parse_exponent:N #1
1659 {
1660 \if:w e \if:w E \exp_not:N #1 e \else: \exp_not:N #1 \fi:
1661 \exp_after:wN \@@_parse_exponent_aux:NN
1662 \exp_after:wN #1
1663 \exp:w
1664 \else:
1665 0 \@@_parse_return_sep:w #1
1666 \fi:
1667 \@@_parse_expand:w
1668 }
1669 \cs_new:Npn \@@_parse_exponent_aux:NN #1#2
1670 {
1671 \if_int_compare:w \if_catcode:w \scan_stop: \exp_not:N #2
1672 0 \else: `#2 \fi: > `9 \exp_stop_f:
1673 0 \exp_after:wN \@@_sep: \exp_after:wN #1
1674 \else:
1675 \exp_after:wN \@@_parse_exponent_sign:N
1676 \fi:
1677 #2
1678 }
1679 % \end{macrocode}
1680 % \end{macro}
1681 %
1682 % \begin{macro}[rEXP]{\@@_parse_exponent_sign:N}
1683 % Read signs one by one (if there is any).
1684 % \begin{macrocode}
1685 \cs_new:Npn \@@_parse_exponent_sign:N #1
1686 {
1687 \if:w + \if:w - \exp_not:N #1 + \fi: \token_to_str:N #1
1688 \exp_after:wN \@@_parse_exponent_sign:N
1689 \exp:w \exp_after:wN \@@_parse_expand:w
1690 \else:
1691 \exp_after:wN \@@_parse_exponent_body:N
1692 \exp_after:wN #1
1693 \fi:
1694 }
1695 % \end{macrocode}
1696 % \end{macro}
1697 %
1698 % \begin{macro}[rEXP]{\@@_parse_exponent_body:N}
1699 % An exponent can be an explicit integer (most common case), or
1700 % various other things (most of which are invalid).
1701 % \begin{macrocode}
1702 \cs_new:Npn \@@_parse_exponent_body:N #1
1703 {
1704 \if_int_compare:w 9 < 1 \token_to_str:N #1 \exp_stop_f:
1705 \token_to_str:N #1
1706 \exp_after:wN \@@_parse_exponent_digits:N
1707 \exp:w
1708 \else:
1709 \@@_parse_exponent_keep:NTF #1
1710 { \@@_parse_return_sep:w #1 }
1711 {
1712 \exp_after:wN \@@_sep:
1713 \exp:w
1714 }
1715 \fi:
1716 \@@_parse_expand:w
1717 }
1718 % \end{macrocode}
1719 % \end{macro}
1720 %
1721 % \begin{macro}[rEXP]{\@@_parse_exponent_digits:N}
1722 % Read digits one by one, and leave them behind in the input stream.
1723 % When finding a non-digit, stop, and insert a \cs{@@_sep:}. Note that
1724 % we do not check for overflow of the exponent, hence there can be a
1725 % \TeX{} error. It is mostly harmless, except when parsing
1726 % |0e9876543210|, which should be a valid representation of $0$, but
1727 % is not.
1728 % \begin{macrocode}
1729 \cs_new:Npn \@@_parse_exponent_digits:N #1
1730 {
1731 \if_int_compare:w 9 < 1 \token_to_str:N #1 \exp_stop_f:
1732 \token_to_str:N #1
1733 \exp_after:wN \@@_parse_exponent_digits:N
1734 \exp:w
1735 \else:
1736 \@@_parse_return_sep:w #1
1737 \fi:
1738 \@@_parse_expand:w
1739 }
1740 % \end{macrocode}
1741 % \end{macro}
1742 %
1743 % \begin{macro}[rEXP]{\@@_parse_exponent_keep:NTF}
1744 % This is the last building block for parsing exponents. The
1745 % argument~|#1| is already fully expanded, and neither |+| nor~|-| nor
1746 % a digit. It can be:
1747 % \begin{itemize}
1748 % \item \cs{s_@@}, marking the start of an internal floating point,
1749 % invalid here;
1750 % \item another control sequence equal to \tn{relax}, probably a bad
1751 % variable;
1752 % \item a register: in this case we make sure that it is an integer
1753 % register, not a dimension;
1754 % \item a character other than |+|, |-| or digits, again, an error.
1755 % \end{itemize}
1756 % \begin{macrocode}
1757 \prg_new_conditional:Npnn \@@_parse_exponent_keep:N #1 { TF }
1758 {
1759 \if_catcode:w \scan_stop: \exp_not:N #1
1760 \if_meaning:w \scan_stop: #1
1761 \if:w 0 \@@_str_if_eq:nn { \s_@@ } { \exp_not:N #1 }
1762 0
1763 \msg_expandable_error:nnn
1764 { fp } { after-e } { floating~point~ }
1765 \prg_return_true:
1766 \else:
1767 0
1768 \msg_expandable_error:nnn
1769 { kernel } { bad-variable } {#1}
1770 \prg_return_false:
1771 \fi:
1772 \else:
1773 \if:w 0 \@@_str_if_eq:nn { \int_value:w #1 } { \tex_the:D #1 }
1774 \int_value:w #1
1775 \else:
1776 0
1777 \msg_expandable_error:nnn
1778 { fp } { after-e } { dimension~#1 }
1779 \fi:
1780 \prg_return_false:
1781 \fi:
1782 \else:
1783 0
1784 \msg_expandable_error:nnn
1785 { fp } { missing } { exponent }
1786 \prg_return_true:
1787 \fi:
1788 }
1789 % \end{macrocode}
1790 % \end{macro}
1791 %
1792 % \subsection{Constants, functions and prefix operators}
1793 %
1794 % \subsubsection{Prefix operators}
1795 %
1796 % \begin{macro}[EXP]{\@@_parse_prefix_+:Nw}
1797 % A unary~|+| does nothing: we should continue looking for a number.
1798 % \begin{macrocode}
1799 \cs_new_eq:cN { @@_parse_prefix_+:Nw } \@@_parse_one:Nw
1800 % \end{macrocode}
1801 % \end{macro}
1802 %
1803 % \begin{macro}[EXP]{\@@_parse_apply_function:NNNwN}
1804 % Here, |#1| is a precedence, |#2| is some extra data used by some
1805 % functions, |#3| is \emph{e.g.}, \cs{@@_sin_o:w}, and expands once
1806 % after the calculation, |#4| is the operand, and |#5| is a
1807 % \cs[no-index]{@@_parse_infix_\ldots{}:N} function. We feed the data~|#2|, and the
1808 % argument~|#4|, to the function~|#3|, which expands
1809 % \cs{exp:w} thus the \texttt{infix} function~|#5|.
1810 % \begin{macrocode}
1811 \cs_new:Npn \@@_parse_apply_function:NNNwN #1#2#3#4@#5
1812 {
1813 #3 #2 #4 @
1814 \exp:w \exp_end_continue_f:w #5 #1
1815 }
1816 % \end{macrocode}
1817 % \end{macro}
1818 %
1819 % \begin{macro}[EXP]{\@@_parse_apply_unary:NNNwN}
1820 % \begin{macro}[EXP]{\@@_parse_apply_unary_chk:NwNw, \@@_parse_apply_unary_chk:nNNNw}
1821 % \begin{macro}[EXP]{\@@_parse_apply_unary_type:NNN, \@@_parse_apply_unary_error:NNw}
1822 % In contrast to \cs{@@_parse_apply_function:NNNwN}, this checks that
1823 % the operand |#4| is a single argument (namely there is a single
1824 % \cs{@@_sep:}). We use the fact that any floating point starts with a
1825 % \enquote{safe} token like \cs{s_@@}. If there is no argument
1826 % produce the |fp-no-arg| error; if there are at least two produce
1827 % |fp-multi-arg|. For the error message extract the mathematical
1828 % function name (such as |sin|) from the \pkg{expl3} function that
1829 % computes it, such as \cs{@@_sin_o:w}.
1830 %
1831 % In addition, since there is a single argument we can dispatch on
1832 % type and check that the resulting function exists. This catches
1833 % things like |sin((1,2))| where it does not make sense to take the
1834 % sine of a tuple.
1835 % \begin{macrocode}
1836 \cs_new:Npn \@@_parse_apply_unary:NNNwN #1#2#3#4@#5
1837 {
1838 \@@_parse_apply_unary_chk:NwNw #4 @ \@@_sep: . \s_@@_stop
1839 \@@_parse_apply_unary_type:NNN
1840 #3 #2 #4 @
1841 \exp:w \exp_end_continue_f:w #5 #1
1842 }
1843 \cs_new:Npn \@@_parse_apply_unary_chk:NwNw #1#2 \@@_sep: #3#4 \s_@@_stop
1844 {
1845 \if_meaning:w @ #3 \else:
1846 \token_if_eq_meaning:NNTF . #3
1847 { \@@_parse_apply_unary_chk:nNNNNw { no } }
1848 { \@@_parse_apply_unary_chk:nNNNNw { multi } }
1849 \fi:
1850 }
1851 \cs_new:Npn \@@_parse_apply_unary_chk:nNNNNw #1#2#3#4#5#6 @
1852 {
1853 #2
1854 \@@_error:nffn { #1-arg } { \@@_func_to_name:N #4 } { } { }
1855 \exp_after:wN #4 \exp_after:wN #5 \c_nan_fp @
1856 }
1857 \cs_new:Npn \@@_parse_apply_unary_type:NNN #1#2#3
1858 {
1859 \@@_change_func_type:NNN #3 #1 \@@_parse_apply_unary_error:NNw
1860 #2 #3
1861 }
1862 \cs_new:Npn \@@_parse_apply_unary_error:NNw #1#2#3 @
1863 { \@@_invalid_operation_o:fw { \@@_func_to_name:N #1 } #3 }
1864 % \end{macrocode}
1865 % \end{macro}
1866 % \end{macro}
1867 % \end{macro}
1868 %
1869 % \begin{macro}[EXP]{\@@_parse_prefix_-:Nw, \@@_parse_prefix_!:Nw}
1870 % The unary~|-| and boolean not are harder: we parse the operand using
1871 % a precedence equal to the maximum of the previous precedence~|##1|
1872 % and the precedence \cs{c_@@_prec_not_int} of the unary operator, then call
1873 % the appropriate |\__fp_|\meta{operation}|_o:w| function,
1874 % where the \meta{operation} is |set_sign| or |not|.
1875 % \begin{macrocode}
1876 \cs_set_protected:Npn \@@_tmp:w #1#2#3#4
1877 {
1878 \cs_new:cpn { @@_parse_prefix_ #1 :Nw } ##1
1879 {
1880 \exp_after:wN \@@_parse_apply_unary:NNNwN
1881 \exp_after:wN ##1
1882 \exp_after:wN #4
1883 \exp_after:wN #3
1884 \exp:w
1885 \if_int_compare:w #2 < ##1
1886 \@@_parse_operand:Nw ##1
1887 \else:
1888 \@@_parse_operand:Nw #2
1889 \fi:
1890 \@@_parse_expand:w
1891 }
1892 }
1893 \@@_tmp:w - \c_@@_prec_not_int \@@_set_sign_o:w 2
1894 \@@_tmp:w ! \c_@@_prec_not_int \@@_not_o:w ?
1895 % \end{macrocode}
1896 % \end{macro}
1897 %
1898 % \begin{macro}[EXP]{\@@_parse_prefix_.:Nw}
1899 % Numbers which start with a decimal separator (a~period) end up here.
1900 % Of course, we do not look for an operand, but for the rest of the
1901 % number. This function is very similar to \cs{@@_parse_one_digit:NN}
1902 % but calls \cs{@@_parse_strim_zeros:N} to trim zeros after the
1903 % decimal point, rather than the \texttt{trim_zeros} function for
1904 % zeros before the decimal point.
1905 % \begin{macrocode}
1906 \cs_new:cpn { @@_parse_prefix_.:Nw } #1
1907 {
1908 \exp_after:wN \@@_parse_infix_after_operand:NwN
1909 \exp_after:wN #1
1910 \exp:w \exp_end_continue_f:w
1911 \exp_after:wN \@@_sanitize:wN
1912 \int_value:w \@@_int_eval:w 0 \@@_parse_strim_zeros:N
1913 }
1914 % \end{macrocode}
1915 % \end{macro}
1916 %
1917 % \begin{macro}[EXP]
1918 % {\@@_parse_prefix_(:Nw, \@@_parse_lparen_after:NwN}
1919 % The left parenthesis is treated as a unary prefix operator because
1920 % it appears in exactly the same settings. If the previous precedence
1921 % is \cs{c_@@_prec_func_int} we are parsing arguments of a function
1922 % and commas should not build tuples; otherwise commas should build
1923 % tuples. We distinguish these cases by precedence:
1924 % \cs{c_@@_prec_comma_int} for the case of arguments,
1925 % \cs{c_@@_prec_tuple_int} for the case of tuples.
1926 % Once the operand is found, the \texttt{lparen_after} auxiliary makes
1927 % sure that there was a closing parenthesis (otherwise it complains),
1928 % and leaves in the input stream an operand,
1929 % fetching the following infix operator.
1930 % \begin{macrocode}
1931 \cs_new:cpn { @@_parse_prefix_(:Nw } #1
1932 {
1933 \exp_after:wN \@@_parse_lparen_after:NwN
1934 \exp_after:wN #1
1935 \exp:w
1936 \if_int_compare:w #1 = \c_@@_prec_func_int
1937 \@@_parse_operand:Nw \c_@@_prec_comma_int
1938 \else:
1939 \@@_parse_operand:Nw \c_@@_prec_tuple_int
1940 \fi:
1941 \@@_parse_expand:w
1942 }
1943 \cs_new:Npe \@@_parse_lparen_after:NwN #1#2 @ #3
1944 {
1945 \exp_not:N \token_if_eq_meaning:NNTF #3
1946 \exp_not:c { @@_parse_infix_):N }
1947 {
1948 \exp_not:N \@@_exp_after_array_f:w #2 \s_@@_expr_stop
1949 \exp_not:N \exp_after:wN
1950 \exp_not:N \@@_parse_infix_after_paren:NN
1951 \exp_not:N \exp_after:wN #1
1952 \exp_not:N \exp:w
1953 \exp_not:N \@@_parse_expand:w
1954 }
1955 {
1956 \exp_not:N \msg_expandable_error:nnn
1957 { fp } { missing } { ) }
1958 \exp_not:N \tl_if_empty:nT {#2} \exp_not:N \c_@@_empty_tuple_fp
1959 #2 @
1960 \exp_not:N \use_none:n #3
1961 }
1962 }
1963 % \end{macrocode}
1964 % \end{macro}
1965 %
1966 % \begin{macro}[EXP]{\@@_parse_prefix_):Nw}
1967 % The right parenthesis can appear as a prefix in two similar cases:
1968 % in an empty tuple or tuple ending with a comma, or in an empty
1969 % argument list or argument list ending with a comma, such as in
1970 % |max(1,2,)| or in |rand()|.
1971 % \begin{macrocode}
1972 \cs_new:cpn { @@_parse_prefix_):Nw } #1
1973 {
1974 \if_int_compare:w #1 = \c_@@_prec_comma_int
1975 \else:
1976 \if_int_compare:w #1 = \c_@@_prec_tuple_int
1977 \exp_after:wN \c_@@_empty_tuple_fp \exp:w
1978 \else:
1979 \msg_expandable_error:nnn
1980 { fp } { missing-number } { ) }
1981 \exp_after:wN \c_nan_fp \exp:w
1982 \fi:
1983 \exp_end_continue_f:w
1984 \fi:
1985 \@@_parse_infix_after_paren:NN #1 )
1986 }
1987 % \end{macrocode}
1988 % \end{macro}
1989 %
1990 % \subsubsection{Constants}
1991 %
1992 % \begin{macro}[EXP]
1993 % {
1994 % \@@_parse_word_inf:N , \@@_parse_word_nan:N ,
1995 % \@@_parse_word_pi:N , \@@_parse_word_deg:N ,
1996 % \@@_parse_word_true:N , \@@_parse_word_false:N ,
1997 % }
1998 % Some words correspond to constant floating points. The floating
1999 % point constant is left as a result of \cs{@@_parse_one:Nw} after
2000 % expanding \cs{@@_parse_infix:NN}.
2001 % \begin{macrocode}
2002 \cs_set_protected:Npn \@@_tmp:w #1 #2
2003 {
2004 \cs_new:cpn { @@_parse_word_#1:N }
2005 { \exp_after:wN #2 \exp:w \exp_end_continue_f:w \@@_parse_infix:NN }
2006 }
2007 \@@_tmp:w { inf } \c_inf_fp
2008 \@@_tmp:w { nan } \c_nan_fp
2009 \@@_tmp:w { pi } \c_pi_fp
2010 \@@_tmp:w { deg } \c_one_degree_fp
2011 \@@_tmp:w { true } \c_one_fp
2012 \@@_tmp:w { false } \c_zero_fp
2013 % \end{macrocode}
2014 % \end{macro}
2015 %
2016 % \begin{macro}[EXP]
2017 % {
2018 % \@@_parse_caseless_inf:N,
2019 % \@@_parse_caseless_infinity:N,
2020 % \@@_parse_caseless_nan:N
2021 % }
2022 % Copies of \cs[no-index]{@@_parse_word_\ldots{}:N} commands, to allow
2023 % arbitrary case as mandated by the standard.
2024 % \begin{macrocode}
2025 \cs_new_eq:NN \@@_parse_caseless_inf:N \@@_parse_word_inf:N
2026 \cs_new_eq:NN \@@_parse_caseless_infinity:N \@@_parse_word_inf:N
2027 \cs_new_eq:NN \@@_parse_caseless_nan:N \@@_parse_word_nan:N
2028 % \end{macrocode}
2029 % \end{macro}
2030 %
2031 % \begin{macro}[EXP]
2032 % {
2033 % \@@_parse_word_pt:N , \@@_parse_word_in:N ,
2034 % \@@_parse_word_pc:N , \@@_parse_word_cm:N , \@@_parse_word_mm:N ,
2035 % \@@_parse_word_dd:N , \@@_parse_word_cc:N , \@@_parse_word_nd:N ,
2036 % \@@_parse_word_nc:N , \@@_parse_word_bp:N , \@@_parse_word_sp:N ,
2037 % }
2038 % Dimension units are also floating point constants but their value is
2039 % not stored as a floating point constant. We give the values
2040 % explicitly here.
2041 % \begin{macrocode}
2042 \cs_set_protected:Npn \@@_tmp:w #1 #2
2043 {
2044 \cs_new:cpn { @@_parse_word_#1:N }
2045 {
2046 \@@_exp_after_f:nw { \@@_parse_infix:NN }
2047 \s_@@ \@@_chk:w 10 #2 \@@_sep:
2048 }
2049 }
2050 \@@_tmp:w {pt} { {1} {1000} {0000} {0000} {0000} }
2051 \@@_tmp:w {in} { {2} {7227} {0000} {0000} {0000} }
2052 \@@_tmp:w {pc} { {2} {1200} {0000} {0000} {0000} }
2053 \@@_tmp:w {cm} { {2} {2845} {2755} {9055} {1181} }
2054 \@@_tmp:w {mm} { {1} {2845} {2755} {9055} {1181} }
2055 \@@_tmp:w {dd} { {1} {1070} {0085} {6496} {0630} }
2056 \@@_tmp:w {cc} { {2} {1284} {0102} {7795} {2756} }
2057 \@@_tmp:w {nd} { {1} {1066} {9783} {4645} {6693} }
2058 \@@_tmp:w {nc} { {2} {1280} {3740} {1574} {8031} }
2059 \@@_tmp:w {bp} { {1} {1003} {7500} {0000} {0000} }
2060 \@@_tmp:w {sp} { {-4} {1525} {8789} {0625} {0000} }
2061 % \end{macrocode}
2062 % \end{macro}
2063 %
2064 % \begin{macro}[EXP]{\@@_parse_word_em:N, \@@_parse_word_ex:N}
2065 % The font-dependent units |em| and |ex| must be evaluated on the fly.
2066 % We reuse an auxiliary of \cs{dim_to_fp:n}.
2067 % \begin{macrocode}
2068 \tl_map_inline:nn { {em} {ex} }
2069 {
2070 \cs_new:cpn { @@_parse_word_#1:N }
2071 {
2072 \exp_after:wN \@@_from_dim_test:ww
2073 \exp_after:wN 0 \exp_after:wN ,
2074 \int_value:w \dim_to_decimal_in_sp:n { 1 #1 } \exp_after:wN \@@_sep:
2075 \exp:w \exp_end_continue_f:w \@@_parse_infix:NN
2076 }
2077 }
2078 % \end{macrocode}
2079 % \end{macro}
2080 %
2081 % \subsubsection{Functions}
2082 %
2083 % ^^A begin[todo]
2084 %
2085 % \begin{macro}[EXP]
2086 % {\@@_parse_unary_function:NNN, \@@_parse_function:NNN}
2087 % \begin{macrocode}
2088 \cs_new:Npn \@@_parse_unary_function:NNN #1#2#3
2089 {
2090 \exp_after:wN \@@_parse_apply_unary:NNNwN
2091 \exp_after:wN #3
2092 \exp_after:wN #2
2093 \exp_after:wN #1
2094 \exp:w
2095 \@@_parse_operand:Nw \c_@@_prec_func_int \@@_parse_expand:w
2096 }
2097 \cs_new:Npn \@@_parse_function:NNN #1#2#3
2098 {
2099 \exp_after:wN \@@_parse_apply_function:NNNwN
2100 \exp_after:wN #3
2101 \exp_after:wN #2
2102 \exp_after:wN #1
2103 \exp:w
2104 \@@_parse_operand:Nw \c_@@_prec_func_int \@@_parse_expand:w
2105 }
2106 % \end{macrocode}
2107 % \end{macro}
2108 %
2109 % \subsection{Main functions}
2110 %
2111 % \begin{macro}[EXP]{\@@_parse:n, \@@_parse_o:n}
2112 % \begin{macro}[EXP]{\@@_parse_after:ww}
2113 % Start an \cs{exp:w} expansion so that \cs{@@_parse:n} expands
2114 % in two steps. The \cs{@@_parse_operand:Nw} function performs
2115 % computations until reaching an operation with precedence
2116 % \cs{c_@@_prec_end_int} or less, namely, the end of the expression. The
2117 % marker \cs{s_@@_expr_mark} indicates that the next token is an already
2118 % parsed version of an infix operator, and \cs{@@_parse_infix_end:N}
2119 % has infinitely negative precedence. Finally, clean up a
2120 % (well-defined) set of extra tokens and stop the initial expansion
2121 % with \cs{exp_end:}.
2122 % \begin{macrocode}
2123 \cs_new:Npn \@@_parse:n #1
2124 {
2125 \exp:w
2126 \group_align_safe_begin:
2127 \exp_after:wN \group_align_safe_end:
2128 \exp_after:wN \@@_parse_after:ww
2129 \exp:w
2130 \@@_parse_operand:Nw \c_@@_prec_end_int
2131 \@@_parse_expand:w #1
2132 \s_@@_expr_mark \@@_parse_infix_end:N
2133 \s_@@_expr_stop
2134 \exp_end:
2135 }
2136 \cs_new:Npn \@@_parse_after:ww
2137 #1@ \@@_parse_infix_end:N \s_@@_expr_stop #2 { #2 #1 }
2138 \cs_new:Npn \@@_parse_o:n #1
2139 {
2140 \exp:w
2141 \group_align_safe_begin:
2142 \exp_after:wN \group_align_safe_end:
2143 \exp_after:wN \@@_parse_after:ww
2144 \exp:w
2145 \@@_parse_operand:Nw \c_@@_prec_end_int
2146 \@@_parse_expand:w #1
2147 \s_@@_expr_mark \@@_parse_infix_end:N
2148 \s_@@_expr_stop
2149 {
2150 \exp_end_continue_f:w
2151 \@@_exp_after_any_f:nw { \exp_after:wN \exp_stop_f: }
2152 }
2153 }
2154 % \end{macrocode}
2155 % \end{macro}
2156 % \end{macro}
2157 %
2158 % \begin{macro}[EXP]{\@@_parse_operand:Nw}
2159 % \begin{macro}[EXP]{\@@_parse_continue:NwN}
2160 % This is just a shorthand which sets up both \cs{@@_parse_continue:NwN}
2161 % and \cs{@@_parse_one:Nw} with the same precedence. Note the
2162 % trailing \cs{exp:w}.
2163 % \begin{macrocode}
2164 \cs_new:Npn \@@_parse_operand:Nw #1
2165 {
2166 \exp_end_continue_f:w
2167 \exp_after:wN \@@_parse_continue:NwN
2168 \exp_after:wN #1
2169 \exp:w \exp_end_continue_f:w
2170 \exp_after:wN \@@_parse_one:Nw
2171 \exp_after:wN #1
2172 \exp:w
2173 }
2174 \cs_new:Npn \@@_parse_continue:NwN #1 #2 @ #3 { #3 #1 #2 @ }
2175 % \end{macrocode}
2176 % \end{macro}
2177 % \end{macro}
2178 %
2179 % \begin{macro}[EXP]{\@@_parse_apply_binary:NwNwN}
2180 % \begin{macro}[EXP]
2181 % {\@@_parse_apply_binary_chk:NN, \@@_parse_apply_binary_error:NNN}
2182 % Receives \meta{precedence} \meta{operand_1} |@| \meta{operation}
2183 % \meta{operand_2} |@| \meta{infix command}. Builds the appropriate
2184 % call to the \meta{operation}~|#3|, dispatching on both types.
2185 % If the resulting control sequence does not exist, the operation is
2186 % not allowed.
2187 %
2188 % This is redefined in \pkg{l3fp-extras}.
2189 % \begin{macrocode}
2190 \cs_new:Npn \@@_parse_apply_binary:NwNwN #1 #2#3@ #4 #5#6@ #7
2191 {
2192 \exp_after:wN \@@_parse_continue:NwN
2193 \exp_after:wN #1
2194 \exp:w \exp_end_continue_f:w
2195 \exp_after:wN \@@_parse_apply_binary_chk:NN
2196 \cs:w
2197 @@
2198 \@@_type_from_scan:N #2
2199 _#4
2200 \@@_type_from_scan:N #5
2201 _o:ww
2202 \cs_end:
2203 #4
2204 #2#3 #5#6
2205 \exp:w \exp_end_continue_f:w #7 #1
2206 }
2207 \cs_new:Npn \@@_parse_apply_binary_chk:NN #1#2
2208 {
2209 \if_meaning:w \scan_stop: #1
2210 \@@_parse_apply_binary_error:NNN #2
2211 \fi:
2212 #1
2213 }
2214 \cs_new:Npn \@@_parse_apply_binary_error:NNN #1#2#3
2215 {
2216 #2
2217 \@@_invalid_operation_o:Nww #1
2218 }
2219 % \end{macrocode}
2220 % \end{macro}
2221 % \end{macro}
2222 %
2223 % \begin{macro}[EXP]{\@@_binary_type_o:Nww, \@@_binary_rev_type_o:Nww}
2224 % Applies the operator |#1| to its two arguments, dispatching
2225 % according to their types, and expands once after the result.
2226 % The |rev| version swaps its arguments before doing this.
2227 % \begin{macrocode}
2228 \cs_new:Npn \@@_binary_type_o:Nww #1 #2#3 \@@_sep: #4
2229 {
2230 \exp_after:wN \@@_parse_apply_binary_chk:NN
2231 \cs:w
2232 @@
2233 \@@_type_from_scan:N #2
2234 _ #1
2235 \@@_type_from_scan:N #4
2236 _o:ww
2237 \cs_end:
2238 #1
2239 #2 #3 \@@_sep: #4
2240 }
2241 \cs_new:Npn \@@_binary_rev_type_o:Nww #1 #2#3 \@@_sep: #4#5 \@@_sep:
2242 {
2243 \exp_after:wN \@@_parse_apply_binary_chk:NN
2244 \cs:w
2245 @@
2246 \@@_type_from_scan:N #4
2247 _ #1
2248 \@@_type_from_scan:N #2
2249 _o:ww
2250 \cs_end:
2251 #1
2252 #4 #5 \@@_sep: #2 #3 \@@_sep:
2253 }
2254 % \end{macrocode}
2255 % \end{macro}
2256 %
2257 % \subsection{Infix operators}
2258 %
2259 % \begin{macro}[EXP]{\@@_parse_infix_after_operand:NwN}
2260 % \begin{macrocode}
2261 \cs_new:Npn \@@_parse_infix_after_operand:NwN #1 #2\@@_sep:
2262 {
2263 \@@_exp_after_f:nw { \@@_parse_infix:NN #1 }
2264 #2\@@_sep:
2265 }
2266 \cs_new:Npn \@@_parse_infix:NN #1 #2
2267 {
2268 \if_catcode:w \scan_stop: \exp_not:N #2
2269 \if:w 0 \@@_str_if_eq:nn { \s_@@_expr_mark } { \exp_not:N #2 }
2270 \exp_after:wN \exp_after:wN
2271 \exp_after:wN \@@_parse_infix_mark:NNN
2272 \else:
2273 \exp_after:wN \exp_after:wN
2274 \exp_after:wN \@@_parse_infix_juxt:N
2275 \fi:
2276 \else:
2277 \if_int_compare:w
2278 \@@_int_eval:w
2279 ( `#2 \if_int_compare:w `#2 > `Z - 32 \fi: ) / 26
2280 = 3 \exp_stop_f:
2281 \exp_after:wN \exp_after:wN
2282 \exp_after:wN \@@_parse_infix_juxt:N
2283 \else:
2284 \exp_after:wN \@@_parse_infix_check:NNN
2285 \cs:w
2286 @@_parse_infix_ \token_to_str:N #2 :N
2287 \exp_after:wN \exp_after:wN \exp_after:wN
2288 \cs_end:
2289 \fi:
2290 \fi:
2291 #1
2292 #2
2293 }
2294 \cs_new:Npn \@@_parse_infix_check:NNN #1#2#3
2295 {
2296 \if_meaning:w \scan_stop: #1
2297 \msg_expandable_error:nnn
2298 { fp } { missing } { * }
2299 \exp_after:wN \@@_parse_infix_mul:N
2300 \exp_after:wN #2
2301 \exp_after:wN #3
2302 \else:
2303 \exp_after:wN #1
2304 \exp_after:wN #2
2305 \exp:w \exp_after:wN \@@_parse_expand:w
2306 \fi:
2307 }
2308 % \end{macrocode}
2309 % \end{macro}
2310 %
2311 % \begin{macro}{\@@_parse_infix_after_paren:NN}
2312 % Variant of \cs{@@_parse_infix:NN} for use after a closing
2313 % parenthesis. The only difference is that \cs{@@_parse_infix_juxt:N}
2314 % is replaced by \cs{@@_parse_infix_mul:N}.
2315 % \begin{macrocode}
2316 \cs_new:Npn \@@_parse_infix_after_paren:NN #1 #2
2317 {
2318 \if_catcode:w \scan_stop: \exp_not:N #2
2319 \if:w 0 \@@_str_if_eq:nn { \s_@@_expr_mark } { \exp_not:N #2 }
2320 \exp_after:wN \exp_after:wN
2321 \exp_after:wN \@@_parse_infix_mark:NNN
2322 \else:
2323 \exp_after:wN \exp_after:wN
2324 \exp_after:wN \@@_parse_infix_mul:N
2325 \fi:
2326 \else:
2327 \if_int_compare:w
2328 \@@_int_eval:w
2329 ( `#2 \if_int_compare:w `#2 > `Z - 32 \fi: ) / 26
2330 = 3 \exp_stop_f:
2331 \exp_after:wN \exp_after:wN
2332 \exp_after:wN \@@_parse_infix_mul:N
2333 \else:
2334 \exp_after:wN \@@_parse_infix_check:NNN
2335 \cs:w
2336 @@_parse_infix_ \token_to_str:N #2 :N
2337 \exp_after:wN \exp_after:wN \exp_after:wN
2338 \cs_end:
2339 \fi:
2340 \fi:
2341 #1
2342 #2
2343 }
2344 % \end{macrocode}
2345 % \end{macro}
2346 %
2347 % \subsubsection{Closing parentheses and commas}
2348 %
2349 % \begin{macro}[EXP]{\@@_parse_infix_mark:NNN}
2350 % As an infix operator, \cs{s_@@_expr_mark} means that the next
2351 % token~(|#3|) has already gone through \cs{@@_parse_infix:NN} and
2352 % should be provided the precedence~|#1|. The scan mark~|#2| is
2353 % discarded.
2354 % \begin{macrocode}
2355 \cs_new:Npn \@@_parse_infix_mark:NNN #1#2#3 { #3 #1 }
2356 % \end{macrocode}
2357 % \end{macro}
2358 %
2359 % \begin{macro}[EXP]{\@@_parse_infix_end:N}
2360 % This one is a little bit odd: force every previous operator to end,
2361 % regardless of the precedence.
2362 % \begin{macrocode}
2363 \cs_new:Npn \@@_parse_infix_end:N #1
2364 { @ \use_none:n \@@_parse_infix_end:N }
2365 % \end{macrocode}
2366 % \end{macro}
2367 %
2368 % \begin{macro}[EXP]+\@@_parse_infix_):N+
2369 % This is very similar to \cs{@@_parse_infix_end:N}, complaining about
2370 % an extra closing parenthesis if the previous operator was the
2371 % beginning of the expression, with precedence \cs{c_@@_prec_end_int}.
2372 % \begin{macrocode}
2373 \cs_set_protected:Npn \@@_tmp:w #1
2374 {
2375 \cs_new:Npn #1 ##1
2376 {
2377 \if_int_compare:w ##1 > \c_@@_prec_end_int
2378 \exp_after:wN @
2379 \exp_after:wN \use_none:n
2380 \exp_after:wN #1
2381 \else:
2382 \msg_expandable_error:nnn { fp } { extra } { ) }
2383 \exp_after:wN \@@_parse_infix:NN
2384 \exp_after:wN ##1
2385 \exp:w \exp_after:wN \@@_parse_expand:w
2386 \fi:
2387 }
2388 }
2389 \exp_args:Nc \@@_tmp:w { @@_parse_infix_):N }
2390 % \end{macrocode}
2391 % \end{macro}
2392 %
2393 % \begin{macro}[verb, EXP]{\__fp_parse_infix_,:N}
2394 % \begin{macro}[EXP]{\@@_parse_infix_comma:w, \@@_parse_apply_comma:NwNwN}
2395 % As for other infix operations, if the previous operations has higher
2396 % precedence the comma waits. Otherwise we call
2397 % \cs{@@_parse_operand:Nw} to read more comma-delimited arguments that
2398 % \cs{@@_parse_infix_comma:w} simply concatenates into a |@|-delimited
2399 % array. The first comma in a tuple that is not a function argument
2400 % is distinguished: in that case call \cs{@@_parse_apply_comma:NwNwN}
2401 % whose job is to convert the first item of the tuple and an array of
2402 % the remaining items into a tuple. In contrast to
2403 % \cs{@@_parse_apply_binary:NwNwN} this function's operands are not
2404 % single-object arrays.
2405 % \begin{macrocode}
2406 \cs_set_protected:Npn \@@_tmp:w #1
2407 {
2408 \cs_new:Npn #1 ##1
2409 {
2410 \if_int_compare:w ##1 > \c_@@_prec_comma_int
2411 \exp_after:wN @
2412 \exp_after:wN \use_none:n
2413 \exp_after:wN #1
2414 \else:
2415 \if_int_compare:w ##1 < \c_@@_prec_comma_int
2416 \exp_after:wN @
2417 \exp_after:wN \@@_parse_apply_comma:NwNwN
2418 \exp_after:wN ,
2419 \exp:w
2420 \else:
2421 \exp_after:wN \@@_parse_infix_comma:w
2422 \exp:w
2423 \fi:
2424 \@@_parse_operand:Nw \c_@@_prec_comma_int
2425 \exp_after:wN \@@_parse_expand:w
2426 \fi:
2427 }
2428 }
2429 \exp_args:Nc \@@_tmp:w { @@_parse_infix_,:N }
2430 \cs_new:Npn \@@_parse_infix_comma:w #1 @
2431 { #1 @ \use_none:n }
2432 \cs_new:Npn \@@_parse_apply_comma:NwNwN #1 #2@ #3 #4@ #5
2433 {
2434 \exp_after:wN \@@_parse_continue:NwN
2435 \exp_after:wN #1
2436 \exp:w \exp_end_continue_f:w
2437 \@@_exp_after_tuple_f:nw { }
2438 \s_@@_tuple \@@_tuple_chk:w { #2 #4 } \@@_sep:
2439 #5 #1
2440 }
2441 % \end{macrocode}
2442 % \end{macro}
2443 % \end{macro}
2444 %
2445 % \subsubsection{Usual infix operators}
2446 %
2447 % \begin{macro}[EXP]
2448 % {
2449 % \@@_parse_infix_+:N, \@@_parse_infix_-:N,
2450 % \@@_parse_infix_juxt:N,
2451 % \@@_parse_infix_/:N, \@@_parse_infix_mul:N,
2452 % \@@_parse_infix_and:N, \@@_parse_infix_or:N,
2453 % }
2454 % \begin{macro}[EXP]+\@@_parse_infix_^:N+
2455 % As described in the \enquote{work plan}, each infix operator has an
2456 % associated |\..._infix_...| function, a computing function, and
2457 % precedence, given as arguments to \cs{@@_tmp:w}. Using the general
2458 % mechanism for arithmetic operations. The power operation must be
2459 % associative in the opposite order from all others. For this, we use
2460 % two distinct precedences.
2461 % \begin{macrocode}
2462 \cs_set_protected:Npn \@@_tmp:w #1#2#3#4
2463 {
2464 \cs_new:Npn #1 ##1
2465 {
2466 \if_int_compare:w ##1 < #3
2467 \exp_after:wN @
2468 \exp_after:wN \@@_parse_apply_binary:NwNwN
2469 \exp_after:wN #2
2470 \exp:w
2471 \@@_parse_operand:Nw #4
2472 \exp_after:wN \@@_parse_expand:w
2473 \else:
2474 \exp_after:wN @
2475 \exp_after:wN \use_none:n
2476 \exp_after:wN #1
2477 \fi:
2478 }
2479 }
2480 \exp_args:Nc \@@_tmp:w { @@_parse_infix_^:N } ^
2481 \c_@@_prec_hatii_int \c_@@_prec_hat_int
2482 \exp_args:Nc \@@_tmp:w { @@_parse_infix_juxt:N } *
2483 \c_@@_prec_juxt_int \c_@@_prec_juxt_int
2484 \exp_args:Nc \@@_tmp:w { @@_parse_infix_/:N } /
2485 \c_@@_prec_times_int \c_@@_prec_times_int
2486 \exp_args:Nc \@@_tmp:w { @@_parse_infix_mul:N } *
2487 \c_@@_prec_times_int \c_@@_prec_times_int
2488 \exp_args:Nc \@@_tmp:w { @@_parse_infix_-:N } -
2489 \c_@@_prec_plus_int \c_@@_prec_plus_int
2490 \exp_args:Nc \@@_tmp:w { @@_parse_infix_+:N } +
2491 \c_@@_prec_plus_int \c_@@_prec_plus_int
2492 \exp_args:Nc \@@_tmp:w { @@_parse_infix_and:N } &
2493 \c_@@_prec_and_int \c_@@_prec_and_int
2494 \exp_args:Nc \@@_tmp:w { @@_parse_infix_or:N } |
2495 \c_@@_prec_or_int \c_@@_prec_or_int
2496 % \end{macrocode}
2497 % \end{macro}
2498 % \end{macro}
2499 %
2500 % \subsubsection{Juxtaposition}
2501 %
2502 % \begin{macro}[EXP]+\@@_parse_infix_(:N+
2503 % When an opening parenthesis appears where we expect an infix
2504 % operator, we compute the product of the previous operand and the
2505 % contents of the parentheses using \cs{@@_parse_infix_mul:N}.
2506 % \begin{macrocode}
2507 \cs_new:cpn { @@_parse_infix_(:N } #1
2508 { \@@_parse_infix_mul:N #1 ( }
2509 % \end{macrocode}
2510 % \end{macro}
2511 %
2512 % \subsubsection{Multi-character cases}
2513 %
2514 % \begin{macro}[EXP]{\@@_parse_infix_*:N}
2515 % \begin{macrocode}
2516 \cs_set_protected:Npn \@@_tmp:w #1
2517 {
2518 \cs_new:cpn { @@_parse_infix_*:N } ##1##2
2519 {
2520 \if:w * \exp_not:N ##2
2521 \exp_after:wN #1
2522 \exp_after:wN ##1
2523 \else:
2524 \exp_after:wN \@@_parse_infix_mul:N
2525 \exp_after:wN ##1
2526 \exp_after:wN ##2
2527 \fi:
2528 }
2529 }
2530 \exp_args:Nc \@@_tmp:w { @@_parse_infix_^:N }
2531 % \end{macrocode}
2532 % \end{macro}
2533 %
2534 % \begin{macro}[EXP]+\@@_parse_infix_|:Nw+
2535 % \begin{macro}[EXP]+\@@_parse_infix_&:Nw+
2536 % \begin{macrocode}
2537 \cs_set_protected:Npn \@@_tmp:w #1#2#3
2538 {
2539 \cs_new:Npn #1 ##1##2
2540 {
2541 \if:w #2 \exp_not:N ##2
2542 \exp_after:wN #1
2543 \exp_after:wN ##1
2544 \exp:w \exp_after:wN \@@_parse_expand:w
2545 \else:
2546 \exp_after:wN #3
2547 \exp_after:wN ##1
2548 \exp_after:wN ##2
2549 \fi:
2550 }
2551 }
2552 \exp_args:Nc \@@_tmp:w { @@_parse_infix_|:N } | \@@_parse_infix_or:N
2553 \exp_args:Nc \@@_tmp:w { @@_parse_infix_&:N } & \@@_parse_infix_and:N
2554 % \end{macrocode}
2555 % \end{macro}
2556 % \end{macro}
2557 %
2558 % \subsubsection{Ternary operator}
2559 %
2560 % \begin{macro}[EXP]{\@@_parse_infix_?:N, \@@_parse_infix_::N}
2561 % \begin{macrocode}
2562 \cs_set_protected:Npn \@@_tmp:w #1#2#3#4
2563 {
2564 \cs_new:Npn #1 ##1
2565 {
2566 \if_int_compare:w ##1 < \c_@@_prec_quest_int
2567 #4
2568 \exp_after:wN @
2569 \exp_after:wN #2
2570 \exp:w
2571 \@@_parse_operand:Nw #3
2572 \exp_after:wN \@@_parse_expand:w
2573 \else:
2574 \exp_after:wN @
2575 \exp_after:wN \use_none:n
2576 \exp_after:wN #1
2577 \fi:
2578 }
2579 }
2580 \exp_args:Nc \@@_tmp:w { @@_parse_infix_?:N }
2581 \@@_ternary:NwwN \c_@@_prec_quest_int { }
2582 \exp_args:Nc \@@_tmp:w { @@_parse_infix_::N }
2583 \@@_ternary_auxii:NwwN \c_@@_prec_colon_int
2584 {
2585 \msg_expandable_error:nnnn
2586 { fp } { missing } { ? } { ~for~?: }
2587 }
2588 % \end{macrocode}
2589 % \end{macro}
2590 %
2591 % \subsubsection{Comparisons}
2592 %
2593 % \begin{macro}[EXP]
2594 % {
2595 % \@@_parse_infix_<:N, \@@_parse_infix_=:N,
2596 % \@@_parse_infix_>:N, \@@_parse_infix_!:N
2597 % }
2598 % \begin{macro}[EXP]
2599 % {
2600 % \@@_parse_excl_error:,
2601 % \@@_parse_compare:NNNNNNN,
2602 % \@@_parse_compare_auxi:NNNNNNN,
2603 % \@@_parse_compare_auxii:NNNNN,
2604 % \@@_parse_compare_end:NNNNw,
2605 % \@@_compare:wNNNNw,
2606 % }
2607 % \begin{macrocode}
2608 \cs_new:cpn { @@_parse_infix_<:N } #1
2609 { \@@_parse_compare:NNNNNNN #1 1 0 0 0 0 < }
2610 \cs_new:cpn { @@_parse_infix_=:N } #1
2611 { \@@_parse_compare:NNNNNNN #1 1 0 0 0 0 = }
2612 \cs_new:cpn { @@_parse_infix_>:N } #1
2613 { \@@_parse_compare:NNNNNNN #1 1 0 0 0 0 > }
2614 \cs_new:cpn { @@_parse_infix_!:N } #1
2615 {
2616 \exp_after:wN \@@_parse_compare:NNNNNNN
2617 \exp_after:wN #1
2618 \exp_after:wN 0
2619 \exp_after:wN 1
2620 \exp_after:wN 1
2621 \exp_after:wN 1
2622 \exp_after:wN 1
2623 }
2624 \cs_new:Npn \@@_parse_excl_error:
2625 {
2626 \msg_expandable_error:nnnn
2627 { fp } { missing } { = } { ~after~!. }
2628 }
2629 \cs_new:Npn \@@_parse_compare:NNNNNNN #1
2630 {
2631 \if_int_compare:w #1 < \c_@@_prec_comp_int
2632 \exp_after:wN \@@_parse_compare_auxi:NNNNNNN
2633 \exp_after:wN \@@_parse_excl_error:
2634 \else:
2635 \exp_after:wN @
2636 \exp_after:wN \use_none:n
2637 \exp_after:wN \@@_parse_compare:NNNNNNN
2638 \fi:
2639 }
2640 \cs_new:Npn \@@_parse_compare_auxi:NNNNNNN #1#2#3#4#5#6#7
2641 {
2642 \if_case:w
2643 \@@_int_eval:w \exp_after:wN ` \token_to_str:N #7 - `<
2644 \@@_int_eval_end:
2645 \@@_parse_compare_auxii:NNNNN #2#2#4#5#6
2646 \or: \@@_parse_compare_auxii:NNNNN #2#3#2#5#6
2647 \or: \@@_parse_compare_auxii:NNNNN #2#3#4#2#6
2648 \or: \@@_parse_compare_auxii:NNNNN #2#3#4#5#2
2649 \else: #1 \@@_parse_compare_end:NNNNw #3#4#5#6#7
2650 \fi:
2651 }
2652 \cs_new:Npn \@@_parse_compare_auxii:NNNNN #1#2#3#4#5
2653 {
2654 \exp_after:wN \@@_parse_compare_auxi:NNNNNNN
2655 \exp_after:wN \prg_do_nothing:
2656 \exp_after:wN #1
2657 \exp_after:wN #2
2658 \exp_after:wN #3
2659 \exp_after:wN #4
2660 \exp_after:wN #5
2661 \exp:w \exp_after:wN \@@_parse_expand:w
2662 }
2663 \cs_new:Npn \@@_parse_compare_end:NNNNw #1#2#3#4#5 \fi:
2664 {
2665 \fi:
2666 \exp_after:wN @
2667 \exp_after:wN \@@_parse_apply_compare:NwNNNNNwN
2668 \exp_after:wN \c_one_fp
2669 \exp_after:wN #1
2670 \exp_after:wN #2
2671 \exp_after:wN #3
2672 \exp_after:wN #4
2673 \exp:w
2674 \@@_parse_operand:Nw \c_@@_prec_comp_int \@@_parse_expand:w #5
2675 }
2676 \cs_new:Npn \@@_parse_apply_compare:NwNNNNNwN
2677 #1 #2@ #3 #4#5#6#7 #8@ #9
2678 {
2679 \if_int_odd:w
2680 \if_meaning:w \c_zero_fp #3
2681 0
2682 \else:
2683 \if_case:w \@@_compare_back_any:ww #8 #2 \exp_stop_f:
2684 #5 \or: #6 \or: #7 \else: #4
2685 \fi:
2686 \fi:
2687 \exp_stop_f:
2688 \exp_after:wN \@@_parse_apply_compare_aux:NNwN
2689 \exp_after:wN \c_one_fp
2690 \else:
2691 \exp_after:wN \@@_parse_apply_compare_aux:NNwN
2692 \exp_after:wN \c_zero_fp
2693 \fi:
2694 #1 #8 #9
2695 }
2696 \cs_new:Npn \@@_parse_apply_compare_aux:NNwN #1 #2 #3\@@_sep: #4
2697 {
2698 \if_meaning:w \@@_parse_compare:NNNNNNN #4
2699 \exp_after:wN \@@_parse_continue_compare:NNwNN
2700 \exp_after:wN #1
2701 \exp_after:wN #2
2702 \exp:w \exp_end_continue_f:w
2703 \@@_exp_after_o:w #3\@@_sep:
2704 \exp:w \exp_end_continue_f:w
2705 \else:
2706 \exp_after:wN \@@_parse_continue:NwN
2707 \exp_after:wN #2
2708 \exp:w \exp_end_continue_f:w
2709 \exp_after:wN #1
2710 \exp:w \exp_end_continue_f:w
2711 \fi:
2712 #4 #2
2713 }
2714 \cs_new:Npn \@@_parse_continue_compare:NNwNN #1#2 #3@ #4#5
2715 { #4 #2 #3@ #1 }
2716 % \end{macrocode}
2717 % \end{macro}
2718 % \end{macro}
2719 %
2720 % \subsection{Tools for functions}
2721 %
2722 % \begin{macro}[EXP]{\@@_parse_function_all_fp_o:fnw}
2723 % Followed by \Arg{function name} \Arg{code} \meta{float array} |@|
2724 % this checks all floats are floating point numbers (no tuples).
2725 % \begin{macrocode}
2726 \cs_new:Npn \@@_parse_function_all_fp_o:fnw #1#2#3 @
2727 {
2728 \@@_array_if_all_fp:nTF {#3}
2729 { #2 #3 @ }
2730 {
2731 \@@_error:nffn { bad-args }
2732 {#1}
2733 { \fp_to_tl:n { \s_@@_tuple \@@_tuple_chk:w {#3} \@@_sep: } }
2734 { }
2735 \exp_after:wN \c_nan_fp
2736 }
2737 }
2738 % \end{macrocode}
2739 % \end{macro}
2740 %
2741 % \begin{macro}[EXP]{\@@_parse_function_one_two:nnw}
2742 % \begin{macro}[EXP]
2743 % {
2744 % \@@_parse_function_one_two_error_o:w,
2745 % \@@_parse_function_one_two_aux:nnw,
2746 % \@@_parse_function_one_two_auxii:nnw
2747 % }
2748 % This is followed by \Arg{function name} \Arg{code} \meta{float
2749 % array} |@|. It checks that the \meta{float array} consists of one
2750 % or two floating point numbers (not tuples), then leaves the
2751 % \meta{code} (if there is one float) or its tail (if there are two
2752 % floats) followed by the \meta{float array}. The \meta{code} should
2753 % start with a single token such as \cs{@@_atan_default:w} that deals
2754 % with the single-float case.
2755 %
2756 % The first \cs{@@_if_type_fp:NTwFw} test catches the case of no
2757 % argument and the case of a tuple argument. The next one
2758 % distinguishes the case of a single argument (no error, just add
2759 % \cs{c_one_fp}) from a tuple second argument. Finally check there is
2760 % no further argument.
2761 % \begin{macrocode}
2762 \cs_new:Npn \@@_parse_function_one_two:nnw #1#2#3
2763 {
2764 \@@_if_type_fp:NTwFw
2765 #3 { } \s_@@ \@@_parse_function_one_two_error_o:w \s_@@_stop
2766 \@@_parse_function_one_two_aux:nnw {#1} {#2} #3
2767 }
2768 \cs_new:Npn \@@_parse_function_one_two_error_o:w #1#2#3#4 @
2769 {
2770 \@@_error:nffn { bad-args }
2771 {#2}
2772 { \fp_to_tl:n { \s_@@_tuple \@@_tuple_chk:w {#4} \@@_sep: } }
2773 { }
2774 \exp_after:wN \c_nan_fp
2775 }
2776 \cs_new:Npn \@@_parse_function_one_two_aux:nnw #1#2 #3\@@_sep: #4
2777 {
2778 \@@_if_type_fp:NTwFw
2779 #4 { }
2780 \s_@@
2781 {
2782 \if_meaning:w @ #4
2783 \exp_after:wN \use_iv:nnnn
2784 \fi:
2785 \@@_parse_function_one_two_error_o:w
2786 }
2787 \s_@@_stop
2788 \@@_parse_function_one_two_auxii:nnw {#1} {#2} #3\@@_sep: #4
2789 }
2790 \cs_new:Npn \@@_parse_function_one_two_auxii:nnw #1#2#3\@@_sep: #4\@@_sep: #5
2791 {
2792 \if_meaning:w @ #5 \else:
2793 \exp_after:wN \@@_parse_function_one_two_error_o:w
2794 \fi:
2795 \use_ii:nn {#1} { \use_none:n #2 } #3\@@_sep: #4\@@_sep: #5
2796 }
2797 % \end{macrocode}
2798 % \end{macro}
2799 % \end{macro}
2800 %
2801 % \begin{macro}[EXP]{\@@_tuple_map_o:nw, \@@_tuple_map_loop_o:nw}
2802 % Apply |#1| to all items in the following tuple and expand once
2803 % afterwards. The code |#1| should itself expand once after its
2804 % result.
2805 % \begin{macrocode}
2806 \cs_new:Npn \@@_tuple_map_o:nw #1 \s_@@_tuple \@@_tuple_chk:w #2 \@@_sep:
2807 {
2808 \exp_after:wN \s_@@_tuple
2809 \exp_after:wN \@@_tuple_chk:w
2810 \exp_after:wN {
2811 \exp:w \exp_end_continue_f:w
2812 \@@_tuple_map_loop_o:nw {#1} #2
2813 { \s_@@ \prg_break: } \@@_sep:
2814 \prg_break_point:
2815 \exp_after:wN } \exp_after:wN \@@_sep:
2816 }
2817 \cs_new:Npn \@@_tuple_map_loop_o:nw #1#2#3 \@@_sep:
2818 {
2819 \use_none:n #2
2820 #1 #2 #3 \@@_sep:
2821 \exp:w \exp_end_continue_f:w
2822 \@@_tuple_map_loop_o:nw {#1}
2823 }
2824 % \end{macrocode}
2825 % \end{macro}
2826 %
2827 % \begin{macro}[EXP]{\@@_tuple_mapthread_o:nww, \@@_tuple_mapthread_loop_o:nw}
2828 % Apply |#1| to pairs of items in the two following tuples and expand once
2829 % afterwards.
2830 % \begin{macrocode}
2831 \cs_new:Npn \@@_tuple_mapthread_o:nww #1
2832 \s_@@_tuple \@@_tuple_chk:w #2 \@@_sep:
2833 \s_@@_tuple \@@_tuple_chk:w #3 \@@_sep:
2834 {
2835 \exp_after:wN \s_@@_tuple
2836 \exp_after:wN \@@_tuple_chk:w
2837 \exp_after:wN {
2838 \exp:w \exp_end_continue_f:w
2839 \@@_tuple_mapthread_loop_o:nw {#1}
2840 #2 { \s_@@ \prg_break: } \@@_sep: @
2841 #3 { \s_@@ \prg_break: } \@@_sep:
2842 \prg_break_point:
2843 \exp_after:wN } \exp_after:wN \@@_sep:
2844 }
2845 \cs_new:Npn \@@_tuple_mapthread_loop_o:nw #1#2#3 \@@_sep: #4 @ #5#6 \@@_sep:
2846 {
2847 \use_none:n #2
2848 \use_none:n #5
2849 #1 #2 #3 \@@_sep: #5 #6 \@@_sep:
2850 \exp:w \exp_end_continue_f:w
2851 \@@_tuple_mapthread_loop_o:nw {#1} #4 @
2852 }
2853 % \end{macrocode}
2854 % \end{macro}
2855 %
2856 % ^^A end[todo]
2857 %
2858 % \subsection{Messages}
2859 %
2860 % \begin{macrocode}
2861 \msg_new:nnn { fp } { deprecated }
2862 { '#1'~deprecated;~use~'#2' }
2863 \msg_new:nnn { fp } { unknown-fp-word }
2864 { Unknown~fp~word~#1. }
2865 \msg_new:nnn { fp } { missing }
2866 { Missing~#1~inserted #2. }
2867 \msg_new:nnn { fp } { extra }
2868 { Extra~#1~ignored. }
2869 \msg_new:nnn { fp } { early-end }
2870 { Premature~end~in~fp~expression. }
2871 \msg_new:nnn { fp } { after-e }
2872 { Cannot~use~#1 after~'e'. }
2873 \msg_new:nnn { fp } { missing-number }
2874 { Missing~number~before~'#1'. }
2875 \msg_new:nnn { fp } { unknown-symbol }
2876 { Unknown~symbol~#1~ignored. }
2877 \msg_new:nnn { fp } { extra-comma }
2878 { Unexpected~comma~turned~to~nan~result. }
2879 \msg_new:nnn { fp } { no-arg }
2880 { #1~got~no~argument;~used~nan. }
2881 \msg_new:nnn { fp } { multi-arg }
2882 { #1~got~more~than~one~argument;~used~nan. }
2883 \msg_new:nnn { fp } { bad-args }
2884 { Arguments~in~#1#2~are~invalid. }
2885 \msg_new:nnn { fp } { infty-pi }
2886 { Math~command~#1 is~not~an~fp }
2887 \cs_if_exist:cT { @unexpandable@protect }
2888 {
2889 \msg_new:nnn { fp } { robust-cmd }
2890 { Robust~command~#1 invalid~in~fp~expression! }
2891 }
2892 % \end{macrocode}
2893 %
2894 % \begin{macrocode}
2895 %</code>
2896 % \end{macrocode}
2897 %
2898 % \end{implementation}
2899 %
2900 % \PrintChanges
2901 %
2902 % \PrintIndex