\section{Expressions} \label{sect:expressions} \Kaya{} expressions can, approximately, be divided into three groups. These are typed expressions (i.e., those which compute a value), void expressions (i.e., those which do not compute a value and therefore exist for their side effects) and control expressions (i.e., those which affect program flow). Void expressions and control expressions roughly correspond to statements in traditional languages. Typed expressions occur only in typed contexts (e.g. arguments to function calls). A top level expression is either a void expression or a control expression. \fragment{ \BNF{ \Rule{\MC{expr}}{\MC{void\_expr} \CD{;} \maybe{\MC{expr}}} \Or{\MC{control\_expr} \CD{;} \maybe{\MC{expr}}} } } \subsection{Typed Expressions} \subsubsection{Variables} \fragment{ \BNF{ \Rule{\MC{typed\_expr}}{\ldots} \Or{\MC{var\_expr}} \Rule{\MC{var\_expr}}{\MC{identifier}} \Or{\CD{@}\MC{identifier}} \Or{\MC{var\_expr} \CD{.} \MC{identifier}} \Or{\MC{typed\_expr}\CD{[}\MC{typed\_expr}\CD{]}} } } There are four kinds of variable expression: \begin{enumerate} \item An identifier, which refers to a variable or a function definition. If the name refers to a zero argument function then the name becomes a function application. \item A quoted identifier, \CD{@}\MC{identifier}. This refers to a function, but does not apply it even if it is a zero argument function. \item Field access from a structure, \MC{var\_expr}\CD{.}\MC{identifier}. \item Array lookup, \MC{typed\_expr}\CD{[}\MC{typed\_expr}\CD{]}. The array does not have to be a variable, but can arise as the result of a typed computation. \end{enumerate} \subsubsection{Operators} \fragment{ \BNF{ \Rule{\MC{typed\_expr}}{\MC{typed\_expr} \MC{infix} \MC{typed\_expr}} \Or{\MC{prefix} \MC{typed\_expr}} \Or{\CD{(}\MC{typed\_expr}\CD{)}} } } \Kaya{} includes the usual set of operators, with the usual precedence, using brackets to override precedence. The operators are given in Table \ref{optable}, in increasing order of precedence. All associate from left to right. \begin{table}[h] \begin{center} \begin{tabular}{|l|l|l|} \hline Precedence & Operator & Description \\ \hline 1 & \CD{|} & Bitwise OR \\ & \CD{||} & Boolean OR \\ \hline 2 & \CD{\&} & Bitwise AND \\ & \CD{\&\&} & Boolean AND \\ \hline 3 & \CD{==} & Equality \\ &\CD{!=} & Inequality \\ \hline 4 & \CD{<} & Less than \\ & \CD{>} & Greater than \\ & \CD{<=} & Less than or equal\\ & \CD{>=} & Greater than or equal\\ \hline 5 & \CD{<<} & Bitwise shift left \\ & \CD{>>} & Bitwise shift right \\ \hline 6 & \CD{+} & Addition \\ & \CD{-} & Subtraction \\ \hline 7 & \CD{*} & Multiplication \\ & \CD{/} & Division \\ & \CD{\%} & Modulus (remainder) \\ & \CD{**} & Power \\ \hline 8 & \CD{-} & Numerical negation (unary) \\ \hline 9 & \CD{!} & Boolean negation (unary) \\ \hline 10 & \CD{(} \CD{)} & Grouping \\ \hline \end{tabular} \end{center} \caption{Operators} \label{optable} \end{table} \subsubsection{Functions} Including partial application and infix calls. \subsubsection{Lambda Expressions} \subsubsection{Coercions} \subsubsection{Tuples} \subsubsection{Typed Control Structures} See sections \ref{ifexp} and \ref{caseexp} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Control Expressions} Control expressions are used to direct the program flow. They typically manage the execution of blocks of code; blocks may be empty, or complex expressions, or single statements. \fragment{ \BNF{ \Rule{\MC{block}}{\CD{\{}\CD{\}}} \Or{\CD{\{} \MC{expr} \CD{\}}} \Or{\MC{void\_expr}\CD{;}} } } \subsubsection{\CD{if...then...else}} \label{ifexp} % don't forget typed if expressions \subsubsection{\CD{for} loops} \CD{for} loops are used to iterate through an array: \fragment{ \BNF{ \Rule{\MC{control\_expr}}{\ldots} \Or{ \CD{for} \MC{identifier} \maybe{\MC{counter}} \CD{in} \MC{typed\_expr} \MC{block} } \Rule{\MC{counter}}{\CD{@}\MC{identifier}} } } The \MC{variable} can be either a variable name or a reference into an array, and the \MC{expression} must be of type \CD{Void}. Where the iteration is across a range of numbers, the following syntax may be used to generate the array: \begin{itemize} \item \CD{[a,b]} creates an array from \CD{a} to \CD{b}, where a$\le$b. This is equivalent to \CD{range(a,b)}. e.g. \CD{for x in [1..10] \{ ... \} } iterates over the numbers 1 to 10. \item \CD{[a,b..c]} creates an array from \CD{a} to \CD{c}, with step \CD{b-a}. This is equivalent to \CD{range(a,c,b-a)}. e.g. \CD{for x in [10,9..1] \{ ... \} } iterates over the numbers from 10 down to 1. \end{itemize} C-style for loops may also be used, although usually the above style should be preferred: \fragment{ \BNF{ \Rule{\MC{control\_expr}}{\ldots} \Or{ \CD{for (}\MC{void\_expr} \CD{;} \MC{typed\_expr} \CD{;} \MC{void\_expr}\CD{)} \MC{block} } } } %It is also possible to iterate across a \CD{LazyArray} using a for loop. %(TODO: More detail on using LazyArrays with for) \subsubsection{\CD{while} loops} Inluding while and do...while. \subsubsection{\CD{case} statements} \label{caseexp} % don't forget typed case expressions \subsubsection{\CD{try...catch} blocks} \subsubsection{\CD{repeat} loops} \CD{repeat} loops execute the given control statement forever; useful e.g. for repeatedly evaluating a case expression. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Void Expressions} \subsubsection{Assignment} Including tuple assignment, guarded assignment. Also include binding assignment; i.e., assignment with a type declaration. \subsubsection{Maths Shorthand} i.e. +=, ++, etc. \subsubsection{Throwing Exceptions} \subsubsection{Return} \subsubsection{Function calls} As for typed expressions, but for void functions. \subsubsection{Breaking out of loops} \subsubsection{Tracing} trace statement. \subsubsection{No-operations}