Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General subscripting #3395

Merged
merged 9 commits into from Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions chapters/arrays.tex
Expand Up @@ -55,6 +55,7 @@ \section{Array Declarations}\label{array-declarations}
\item
Binding equations of the form \lstinline!x1 = x2! are allowed for arrays independent of whether the index types of dimensions are subtypes of \lstinline!Integer!, \lstinline!Boolean!, or enumeration types.
\end{itemize}
Operations constructing arrays (e.g., matrix multiplication, indexing, array constructors) only generate arrays indexed by \lstinline!Integer!.
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved

% IMPROVETOP
\begin{table}[H]
Expand Down Expand Up @@ -946,6 +947,9 @@ \section{Indexing}\label{array-indexing}\label{indexing}
The number of dimensions of the expression is reduced by the number of scalar index arguments.
If the number of index arguments is smaller than the number of dimensions of the array, the trailing indices will use `\lstinline!:!'.

It is possible to index a general expression by enclosing it in parenthesis.
Note that while the subscripts are applied to a \lstinline[language=grammar]!output-expression-list! in the grammar, it is only semantically valid when the \lstinline[language=grammar]!output-expression-list! represents an expression.

It is also possible to use the array access operator to assign to element/elements of an array in algorithm sections.
This is called an \firstuse[assignment statement!indexed]{indexed assignment statement}.
If the index is an array the assignments take place in the order given by the index array.
Expand All @@ -972,6 +976,10 @@ \section{Indexing}\label{array-indexing}\label{indexing}
v[{j, k}] := {2, 3}; // Same as: v[j] := 2; v[k] := 3;
v[{1, 1}] := {2, 3}; // Same as: v[1] := 3;
\end{lstlisting}
Array indexing of general expression:
\begin{lstlisting}[language=modelica]
(a*a)[:, j] // Vector of the j'th column of a*a
\end{lstlisting}
If \lstinline!x! is a vector, \lstinline!x[1]! is a scalar, but the slice \lstinline!x[1:5]! is a vector
(a vector-valued or colon index expression causes a vector to be returned).
\end{example}
Expand Down
23 changes: 13 additions & 10 deletions chapters/operatorsandexpressions.tex
Expand Up @@ -103,9 +103,10 @@ \section{Operator Precedence and Associativity}\label{operator-precedence-and-as
\end{center}
\end{table}

The postfix array index and postfix access operators are not expression operators in the normal sense that \lstinline!a.b[1]! can be treated as \lstinline!a.(b[1])!.
The postfix array index and postfix access operators are not merely expression operators in the normal sense that \lstinline!a.b[1]! can be treated as \lstinline!a.(b[1])!.
Instead, these operators need to be considered jointly to identify an entire \lstinline[language=grammar]!component-reference! (one of the alternative productions for \lstinline[language=grammar]!primary! in the grammar) which is the smallest unit that can be seen as an expression in itself.
Postfix array index and postifx access can only be applied immediately to a \lstinline[language=grammar]!component-reference!; not even parentheses around the left operand are allowed.
Postfix access can only be applied immediately to a \lstinline[language=grammar]!component-reference!; not even parentheses around the left operand are allowed.
Postfix array index can additionally be applied if there are parentheses around the left operand, see \cref{indexing}.

\begin{example}
Relative precedence of postfix array index and postfix access.
Expand All @@ -118,16 +119,18 @@ \section{Operator Precedence and Associativity}\label{operator-precedence-and-as
\end{lstlisting}
These are some valid as well as invalid ways to using postfix array index and postfix access:
\begin{lstlisting}[language=modelica]
a[3].x[2] // OK: Component reference of type Real
a[3].x // OK: Component reference of type Real[2]
a.x[2] // OK: Component reference of type Real[3]
a.x // OK: Component reference of type Real[3, 2]
(a.x)[2] // Error: Invalid use of parentheses
a[3] // OK: Component reference of type R
(a[3]).x // Error: Invalid use of parentheses
a[3].x[2] // OK: Component reference of type Real
a[3].x // OK: Component reference of type Real[2]
a.x[2] // OK: Component reference of type Real[3]
a.x[2, :] // Error.
a.x // OK: Component reference of type Real[3, 2]
(a.x)[2] // OK: Component reference of type Real[2] - same as a[2].x[:]
(a.x)[2, :] // OK: Component reference of type Real[2] - same as a[2].x[:]
a[3] // OK: Component reference of type R
(a[3]).x // Error: Invalid use of parentheses
\end{lstlisting}
The relation between \lstinline!a.x!, \lstinline!a.x[2]!, and \lstinline!(a.x)[2]! illustrates the effect of giving higher precedence to array index than postfix access.
Had the precedence been equal, this would have changed the meaning of \lstinline!a.x[2]! to the same thing that \lstinline!(a.x)[2]! tries to express, being a component reference of type \lstinline!Real[2]!.
Had the precedence been equal, this would have changed the meaning of \lstinline!a.x[2]! to the same thing that \lstinline!(a.x)[2]! expresses, being a component reference of type \lstinline!Real[2]!.
\end{example}

\begin{example}
Expand Down
2 changes: 1 addition & 1 deletion chapters/syntax.tex
Expand Up @@ -398,7 +398,7 @@ \subsection{Expressions}\label{expressions1}
| true
| ( component-reference | der | initial | pure ) function-call-args
| component-reference
| "(" output-expression-list ")"
| "(" output-expression-list ")" [ array-subscripts ]
| "[" expression-list { ";" expression-list } "]"
| "{" array-arguments "}"
| end
Expand Down