Something went wrong. Try again.
Actually just three programming languages in a trenchcoat
Something went wrong. Try again.
5.0 kB 134 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135\subsubsection{Assignment}
Variables (specifically, mutable bindings) as well as members of containersmay be assigned to. Syntactically, the left hand side of the assignmentstatement must either:
\begin{itemize} \item Be able to be parsed both as a pattern (\S\ref{sec:patterns}) and an expression (\S\ref{sec:expressions}) \item End with a member access operation (\S\ref{sec:member-access})\end{itemize}
As that is an inconvenience to write out grammatically, and also an inconvenienceto have to implement, note that the syntax for expressions is a superset of thatof patterns, so the left hand side of an assignment is parsed as an expression, andthe validity of this expression as an assignment target is detected separately.
Function assignment is a special syntax for applying a function to a value andthen simultaneously reassigning that value with the result of the function; ageneralization of operator assignment (e.g. \op{+=}) to any function\footnote{Ageneralization which I cannot take credit for as I was inspired to it by\href{https://github.com/betaveros/noulith}{Noulith}, the same projectwhich inspired me to attempt \Trilogy{} at all.}. In this form of assignment,the right side is a list of arguments to apply to the function being used asthe assignment operator. The left hand side is then applied as the last argumentto the function before being re-assigned.
In terms of precedence, these arguments are parsed as if in any other functionapplication, meaning that most complex expressions will require explicitparenthesization. Additionally, as expressions do not typically span multiple lines,these arguments must also fit on one line or do strange things to ensure a specificparsing. It is generally not recommended to use this syntax for any heavy lifting, itis merely intended as syntax sugar for quick one-liners.
Regular operator assignment (\op{+=}, \op{-=}, etc.) is supported as well, in theintuitive way: the left hand identifer is used as the left hand side of the operator.
\begin{bnf*} \bnfprod{Assignment}{ \bnfpn{Assignment} \bnfor \bnfpn{FunctionAssignment} \bnfor } \\ \bnfmore{ \bnfpn{OperatorAssignment} } \\ \bnfprod{Assignment}{ \bnfpn{Expression} \bnfsp \bnfts{\op{=}} \bnfsp \bnfpn{Expression} } \\ \bnfprod{FunctionAssignment}{ \bnfpn{Expression} \bnfsp \bnfpn{IdentifierEq} \bnfsp \bnfpn{ApplicationList} } \\ \bnfprod{OperatorAssignment}{ \bnfpn{Expression} \bnfsp \bnfpn{OperatorEq} \bnfsp \bnfpn{Expression} } \\ \bnfprod{OperatorEq}{ \bnfts{+=}\bnfor \bnfts{-=}\bnfor \bnfts{*=}\bnfor \bnfts{/=}\bnfor \bnfts{\%=}\bnfor \bnfts{**=}\bnfor \bnfts{//=}\bnfor } \\ \bnfmore{ \bnfts{<>=}\bnfor \bnfts{:=}\bnfor \bnfts{|=}\bnfor \bnfts{\&=}\bnfor \bnfts{\textasciicircum=}\bnfor \bnfts{<\textasciitilde=}\bnfor \bnfts{\textasciitilde>=}\bnfor } \\ \bnfmore{ \bnfts{<\textasciitilde\textasciitilde=}\bnfor \bnfts{\textasciitilde\textasciitilde>=}\bnfor \bnfts{<<\textasciitilde=}\bnfor \bnfts{\textasciitilde>>=}\bnfor } \\ \bnfmore{ \bnfts{<<=}\bnfor \bnfts{>>=}\bnfor \bnfts{.=} }\end{bnf*}
Semantically, the assignment statement evaluates its value and then updatesthe binding in the current context. Assignment in this way is the only waya binding in a scope is modified (other operations typically generate a new scopethat extends from the previous, introducing new bindings). In particular, theupdate of the binding updates it in the scope that it was defined, not in thelatest extending scope, therefore making it possible to update the value of avariable defined outside of a block such that the modification persists to beseen outside of that block.
\begin{prooftree} \AxiomC{$\Gamma \vdash y$} \AxiomC{$P = y$} \AxiomC{$a \defby P$} \LeftLabel{Assignment} \TrinaryInfC{$\Gamma\vdash P\ \op{=}\ y \Rightarrow \Gamma[a\mapsto y]$}\end{prooftree}
For a member access assignment, this update simply happens not just withina scope but within the value itself as well.
\begin{prooftree} \AxiomC{$\Gamma\vdash v$} \AxiomC{$\Gamma\vdash E = x$} \AxiomC{$\Gamma\vdash y$} \LeftLabel{Assignment} \TrinaryInfC{$\Gamma\vdash v\ \texttt{.}\ E\ \op{=}\ y \Rightarrow \Gamma[v\mapsto v[x\mapsto y]]$}\end{prooftree}
Rather than defining the exact semantics of function or operator assignment,instead understand those by syntax transformation, and use the same assignmentsemantics as normal:
\begin{align*} \texttt{\$x:expr \$fn:expr= \$(\$a:expr)+} &\Rightarrow \texttt{\$x = \$fn \$(\$a)+ \$x} \\ % \texttt{\$x:expr \$op:op= \$a:expr} & \Rightarrow \texttt{\$x = \$x \$op \$a} \\ %\end{align*}