Document expressions

This commit is contained in:
2026-02-13 22:54:22 +01:00
parent d7c27f1c63
commit 7c554f1017
2 changed files with 220 additions and 55 deletions
+220 -23
View File
@@ -15,26 +15,142 @@ where a statement has control over the evaluation of its components.
Statements can also modify the state of the procedure or the program by
mutating variables.
Most simple expressions are identifiers referencing program's symbols and
literals. These atomic expressions can be combined to more complex expressions
by applying various operations to one or more expressions.
\chapter{Expressions}
The expression \verb|@r.field| includes 3 expressions:
Expressions are constructs denoting rules of computation whereby constants and current values of
variables are combined to derive other values by the application of operators and function
procedures. Expressions consist of operands and operators. Parentheses may be used to express
specific associations of operators and operands.
\section{Literal constants}
Literal constants are
\begin{itemize}
\item signed and unsigned integers,
\item real numbers,
\item booleans,
\item characters,
\item strings,
\item and enumerations.
\end{itemize}
\section{Call expressions}
\subsection*{Procedure call}
If the designator object is a procedure followed by a (possibly empty) parameter list,
the designator implies an activation of the procedure and stands for the value resulting
from its execution. The (types of the) actual parameters must correspond to the formal
parameters as specified in the procedure's declaration.
\subsection*{Type cast}
\begin{grammar}
<cast> = `cast' `(' <expression> `:\@' <type> `)'.
\end{grammar}
The type of an object can be reinterpreted with a cast expression: \\
\verb|cast(object: Type)|.
\subsection*{Traits}
\begin{grammar}
<trait-identifier> = `#' <identifier>.
<trait> = <trait-identifier> `(' [<types>] `)'.
\end{grammar}
Traits allow to query some information about the types, like their size or
field offset or alignment. Calling a trait looks like a procedure call but
trait names start with a \verb|#| and their arguments are type expressions and
not value expressions.
Supported compiler traits:
\begin{itemize}
\item \verb|#size(T)| queries type size.
\item \verb|#align(T)| queries type alignment.
\item \verb|#offset(T, F)| queries the offset of the field \verb|F| in the record \verb|T|.
\end{itemize}
\section{Object designators}
With the exception of literal constants and call expressions, operands are denoted by
\textit{designators}. A designator consists of an identifier referring to the constant,
variable, or procedure to be designated. This identifier or any other expression may be
followed by selectors, if the designated object is an element of a structure.
Designators are addressable, meaning that it is possible to get the address of the
designated object or assign a value to it (if the designated object is mutable).
\subsection*{Subscript selector}
If \verb|A| designates an array or a string, then \verb|A[E]| denotes that element of
\verb|A| whose index is the current value of the expression \verb|E|. The type of
\verb|E| must be of type \verb|Word| or \verb|Int|. The first element has index 1,
the second 2 and so on.
\subsection*{Dereference selector}
If \verb|P| designates a pointer, \verb|P^| denotes the object which is
referenced by \verb|P|.
\subsection*{Field selector}
If \verb|R| designates a record, then \verb|R.f| denotes the field \verb|f| of
\verb|R|.
\subsection*{Variable designator}
If the designated object is a variable, then the designator refers to the
variable's current value.
If the designated object is a procedure, a designator without parameter list
refers to the address of the procedure. Procedure designators designate
immutable objects.
\subsection*{Selector evaluation order}
Selectors are evaluated from left to right.
For example the expression \verb|r^.field| includes 3 designators:
\begin{enumerate}
\item Variable expression \verb|r|.
\item Field access \verb|(r).field|.
\item Address taking \verb|@(r.field)|.
\item Variable designator \verb|r|.
\item \verb|r^| dereferences the pointer \verb|r| and refers
to the object at the address in \verb|r|.
\item \verb|r^.field| accesses the field \verb|field| in that object.
\end{enumerate}
The expression is evaluated in the above order. The postfix expressions
like field access have higher precedence than prefix operators.
\section{Unary expressions}
Unary expressions are expressions with a prefix operator followed by one
operand. Unary operators associate from right to left.
\verb|@| (\textit{at sign}) takes the address of its operand. The operand expression should be
addressable.
\verb|-| (\textit{minus}) denotes sign inversion.
\verb|+| (\textit{plus}) denotes the identity operation.
\verb|~| (\textit{tilde}) applied on a boolean acts as logical not. Applied on a number as
bitwise not.
\section{Binary expressions}
\begin{table}
The syntax of expressions distinguishes between several classes of
operators with different precedences (binding strengths). Operators of
the same precedence associate from left to right. For example, $x - y - z$
stands for $(x - y) - z$.
The available operators are listed in the table~\ref{table:binary}.
In some instances, several different operations are designated by
the same operator symbol. In these cases, the actual operation is
identified by the type of the operands.
\begin{table}[ht]
\centering
\begin{tabularx}{0.8\textwidth}{%
l
@@ -51,24 +167,105 @@ like field access have higher precedence than prefix operators.
\midrule
4 & $= \quad <> \quad > \quad < \quad <= \quad >=$ & Relational operators.\\
\midrule
5 & $or \quad xor \quad \&$ & Logical operators.\\
5 & $\&$ & Logical conjuction.\\
\midrule
6 & $or \quad xor \quad$ & Logical disjunction operators.\\
\bottomrule
\end{tabularx}
\caption{Operator precedence}
\caption{Operator precedence}\label{table:binary}
\end{table}
\section{Unary expressions}
\subsection*{Logical operators}
Unary expressions are expressions with a prefix operator followed by one
operand.
These operators apply to boolean operands and yield a boolean result.
See table~\ref{table:logical}.
\verb|@| (\textit{at sign}) takes the address of its operand. The operand expression should be
addressable.
\begin{table}[ht]
\centering
\begin{tabularx}{0.8\textwidth}{%
l
>{\centering\arraybackslash}X
>{\raggedright\arraybackslash}X
}
\textbf{Symbol} & \textbf{Result}\\
\toprule
$+$ & Sum\\
\midrule
$-$ & Difference\\
\midrule
$*$ & Product\\
\midrule
$/$ & Quotient\\
\midrule
$\%$ & Modulus\\
\bottomrule
\end{tabularx}
\caption{Logical operators}\label{table:logical}
\end{table}
\verb|-| (\textit{minus}) negates a numeric value.
\subsection*{Arithmetic operators}
\verb|~| (\textit{tilde}) applied on a boolean acts as logical not. Applied on a numeric as
bitwise not.
These operators apply to operands of numeric types. Both operands must
be of the same type, which is also the type of the result. See
table~\ref{table:arithmetic}.
\chapter{Conditional statements}
\chapter{Loop statements}
Then quotient $q$ and remainder $r$ are defined by the equation
\[
x = q * y + r \mid 0 \leq r < y
\]
\begin{table}[ht]
\centering
\begin{tabularx}{0.8\textwidth}{%
l
>{\centering\arraybackslash}X
>{\raggedright\arraybackslash}X
}
\textbf{Symbol} & \textbf{Result} & \textbf{$p \cdot q$ stands for}\\
\toprule
$or$ & Inclusive logical disjunction & If $p$ then true, else $q$\\
\midrule
$xor$ & Exclusive Logical disjunction & If $p$ then not $q$, else $q$\\
\midrule
$\&$ & Logical conjuction & If $p$ then $q$, else false\\
\bottomrule
\end{tabularx}
\caption{Arithmetic operators}\label{table:arithmetic}
\end{table}
\subsection*{Relations}
Relations are boolean. The ordering relations <, <=, >, >= apply to the
numeric types. The relations = and <> apply to all types. See
table~\ref{table:relation}.
\begin{table}[ht]
\centering
\begin{tabularx}{0.8\textwidth}{%
l
>{\centering\arraybackslash}X
>{\raggedright\arraybackslash}X
}
\textbf{Symbol} & \textbf{Result}\\
\toprule
$=$ & Equal\\
\midrule
$<>$ & Unequal\\
\midrule
$<$ & Less\\
\midrule
$<=$ & Less or equal\\
\midrule
$>$ & Greater\\
\midrule
$>=$ & Greater or equal\\
\bottomrule
\end{tabularx}
\caption{Relational operators}\label{table:relation}
\end{table}
\chapter{Statements}
\section{Conditional statements}
\section{Loop statements}
-32
View File
@@ -124,35 +124,3 @@ begin
e := E.one
end.
\end{lstlisting}
\chapter{Type operations}
\chapter{Cast}
\begin{grammar}
<cast> = `cast' `(' <expression> `:\@' <type> `)'.
\end{grammar}
The type of an object can be reinterpreted with a cast expression: \\
\verb|cast(object: Type)|.
\chapter{Traits}
\begin{grammar}
<trait-identifier> = `#' <identifier>.
<trait> = <trait-identifier> `(' [<types>] `)'.
\end{grammar}
Traits allow to query some information about the types, like their size or
field offset or alignment. Calling a trait looks like a procedure call but
traits names start with a \verb|#| and their arguments are type expressions and
not value expressions.
Supported compiler traits:
\begin{itemize}
\item \verb|#size(T)| queries type size.
\item \verb|#align(T)| queries type alignment.
\item \verb|#offset(T, F)| queries the offset of the field \verb|F| in the record \verb|T|.
\end{itemize}