Просмотр исходного кода

+ Added remark about evaluation order of expressions

michael 25 лет назад
Родитель
Сommit
a240bdcfd1
2 измененных файлов с 25 добавлено и 1 удалено
  1. 19 1
      docs/ref.tex
  2. 6 0
      docs/user.tex

+ 19 - 1
docs/ref.tex

@@ -2197,7 +2197,6 @@ Operator & Precedence & Category \\ \hline
 \end{FPCltable}
 When determining the precedence, the compiler uses the following rules:
 \begin{enumerate}
-\item Operators with equal precedence are executed from left to right.
 \item In operations with unequal precedences the operands belong to the
 operater with the highest precedence. For example, in \var{5*3+7}, the
 multiplication is higher in precedence than the addition, so it is
@@ -2206,6 +2205,25 @@ executed first. The result would be 22.
 first. Thus, \var {5*(3+7)} would result in 50.
 \end{enumerate}
 
+\begin{remark}
+The order in which expressions of the same precedence are evaluated is not
+guaranteed to be left-to-right. In general, no assumptions on which expression
+is evaluated first should be made in such a case.
+The compiler will decide which expression to evaluate first based on
+optimization rules. Thus, in the following expression:
+\begin{verbatim}
+  a := g(3) + f(2);
+\end{verbatim}
+\var{f(2)} may be executed before \var{g(3)}. This behaviour is distinctly
+different from \delphi or \tp.
+
+If one expression {\em must} be executed before the other, it is necessary
+to split up the statement using temporary results:
+\begin{verbatim}
+  e1 := g(3);
+  a  := e1 + f(2);
+\end{verbatim}
+\end{remark}
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Expression syntax

+ 6 - 0
docs/user.tex

@@ -1502,6 +1502,12 @@ which were possible in Turbo Pascal are no longer possible in Free Pascal.
 \item A file is opened for output only (using \var{fmOutput}) when it is
 opened with \var{Rewrite}. In order to be able to read from it, it should
 be reset with \var{Reset}.
+\item The order in which expressions are evaluated is not necessarily the
+same. In the following expression:
+\begin{verbatim}
+a := g(2) + f(3);
+\end{verbatim}
+it is not guaranteed that \var{g(2)} will be evaluated before \var{f(3)}.
 \end{enumerate}
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%