lama_byterun/spec/03.03.scope_expressions.tex

110 lines
3.8 KiB
TeX
Raw Normal View History

2020-02-04 05:49:12 +03:00
\begin{figure}[t]
\[
2020-02-18 03:39:42 +03:00
\begin{array}{rclc}
\defterm{scopeExpression} & : & \nonterm{definition}^\star\s[\s\nonterm{expression}\s]&\\
\defterm{definition} & : & \nonterm{variableDefinition}&\alt\\
& & \nonterm{functionDefinition}&\alt\\
& & \nonterm{infixDefinition}&\\
2021-02-01 09:52:28 +03:00
\defterm{variableDefinition} & : & (\s\term{var}\alt\term{public}\s)\s\nonterm{variableDefinitionSequence}\s\term{;}&\\
2020-02-18 03:39:42 +03:00
\defterm{variableDefinitionSequence} & : & \nonterm{variableDefinitionItem}\s(\s\term{,}\s\nonterm{variableDefinitionItem}\s)^\star&\\
\defterm{variableDefinitionItem} & : & \token{LIDENT}\s[\s\term{=}\s\nonterm{basicExpression}\s]&\\
\defterm{functionDefinition} & : & [\s\term{public}\s]\s\term{fun}\s\token{LIDENT}\s\term{(}\s\nonterm{functionArguments}\s\term{)}&\\
& & \phantom{XXXXX}\nonterm{functionBody}&\\
\defterm{functionArguments} & : & [\s\token{LIDENT}\s(\s\term{,}\s\token{LIDENT}\s)^\star\s]&\\
\defterm{functionBody} & : & \term{\{}\s\nonterm{scopeExpression}\s\term{\}}&
2020-02-04 05:49:12 +03:00
\end{array}
\]
\caption{Scope expression concrete syntax}
\label{scope_expression}
\end{figure}
\section{Scope Expressions}
2020-02-05 01:18:20 +03:00
\label{sec:scope_expressions}
2020-02-04 05:49:12 +03:00
Scope expressions provide a mean to put expressions is a scoped context. The definitions in scoped expressions comprise of function definitions and
variable definitions (see Fig.~\ref{scope_expression}). For example:
\begin{lstlisting}
2021-02-01 09:52:28 +03:00
var x, y, z; -- variable definitions
2020-02-04 05:49:12 +03:00
fun id (x) {x} -- function definition
\end{lstlisting}
As scope expressions are expressions, they can be nested:
\begin{lstlisting}
2021-02-01 09:52:28 +03:00
var x;
2020-02-04 05:49:12 +03:00
2021-02-01 09:52:28 +03:00
( -- nested scope begins here
var y;
2020-02-04 05:49:12 +03:00
skip
2021-02-01 09:52:28 +03:00
) -- nested scope ends here
2020-02-04 05:49:12 +03:00
\end{lstlisting}
The definitions on the top-level of compilation unit can be tagged as ``\lstinline|public|'', in which case they are exported and become visible by
2021-02-01 09:52:28 +03:00
other units which import a given one. Nested scopes can not contain public definitions.
2020-02-04 05:49:12 +03:00
The nesting relation has the shape of a tree, and in a concrete node of the tree all definitions in all enclosing scopes are visible:
\begin{lstlisting}
2021-02-01 09:52:28 +03:00
var x;
2020-02-04 05:49:12 +03:00
2021-02-01 09:52:28 +03:00
(var y;
(var z;
2020-02-04 05:49:12 +03:00
skip -- x, y, and z are visible here
2021-02-01 09:52:28 +03:00
);
(var t;
2020-02-04 05:49:12 +03:00
skip -- x, y, and t are visible here
2021-02-01 09:52:28 +03:00
);
2020-02-04 05:49:12 +03:00
skip -- x and y are visible here
2021-02-01 09:52:28 +03:00
);
2020-02-04 05:49:12 +03:00
skip -- only x is visible here
\end{lstlisting}
Multiple definitions of the same name in the same scope are prohibited:
\begin{lstlisting}
2021-02-01 09:52:28 +03:00
var x;
2020-02-04 05:49:12 +03:00
fun x () {0} -- error
\end{lstlisting}
However, a definition is a nested scope can override a definition in an enclosing one:
\begin{lstlisting}
2021-02-01 09:52:28 +03:00
var x;
2020-02-04 05:49:12 +03:00
2021-02-01 09:52:28 +03:00
(
2020-02-04 05:49:12 +03:00
fun x () {0} -- ok
skip -- here x is associated with the function
2021-02-01 09:52:28 +03:00
);
2020-02-04 05:49:12 +03:00
2020-02-18 03:39:42 +03:00
skip -- here x is associated with the variable
2020-02-04 05:49:12 +03:00
\end{lstlisting}
A function can freely use all visible definitions; in particular, functions defined in the
same scope can be mutually recursive:
\begin{lstlisting}
2021-02-01 09:52:28 +03:00
var x;
2020-02-04 05:49:12 +03:00
fun f () {0}
2021-02-01 09:52:28 +03:00
(
2020-02-04 05:49:12 +03:00
fun g () {f () + h () + y} -- ok
fun h () {g () + x} -- ok
2021-02-01 09:52:28 +03:00
var y;
2020-02-04 05:49:12 +03:00
skip
2021-02-01 09:52:28 +03:00
);
2020-02-04 05:49:12 +03:00
skip
\end{lstlisting}
2020-02-18 03:39:42 +03:00
A variable, defined in a scope, can be attributed with an expression, calculating its initial value.
2020-02-04 05:49:12 +03:00
These expressions, however, are evaluated in the order of variable declaration. Thus, while
technically it is possible to have forward references in the initialization expression, their
2020-02-18 03:39:42 +03:00
behavior is undefined. For example:
2020-02-04 05:49:12 +03:00
\begin{lstlisting}
2021-02-01 09:52:28 +03:00
var x = y + 2; -- undefined, as y is not yet initialized at this point
var y = x + 2;
2020-02-04 05:49:12 +03:00
skip
\end{lstlisting}