\section{Programs} While the semantics of the language are fun and all, the practical matters of executing a program must also be discussed. One of the goals of \Trilogy{} is to be a practical language, which can be used to build actual programs after all. \subsection{Program Arguments} As with any program run on the command line, \Trilogy{} programs may accept command line arguments which alter the behaviour of the program. When run through the interpreter command, the command line arguments passed to the \Trilogy{} program are specified after the separator \texttt{--}. When the path to the \Trilogy{} interpreter is specified via the shebang, arguments may be passed directly. From within a \Trilogy{} program, these command line arguments are received as an array of strings which may be accessed via the built in procedure \fn{args}. \subsection{Errors} Some errors are considered more non-recoverable than others, typically those that are as a result of code that likely does not do what the author intended. In fact, there are no built-in ``error handling'' mechanisms because indeed a well formed \Trilogy{} program code contains no error states, only exceptional circumstances. A true ``error'' is something that cannot be recovered from, and immediately ends the \Trilogy{} program, which exits with a non-zero exit code indicating an error. The most common of these errors is the \emph{runtime type error}, where an expression is expected by the runtime to evaluate to a value of a particular type, but it does not. As this is most likely a developer error, the safest result as directed by the fail-fast principle is to end the program immediately. Another such error is an \emph{assertion error}, triggered by the \kw{assert} keyword's condition evaluating to \kw{false}. This allows custom abstractions to enforce their invariants and provide in a way that provides a clear indication that something is being done incorrectly. A fizzle is typically not treated as an error; fizzles are often expected to cull unnecessary branches of execution. Fizzles are caused only by bindings which do not have any solutions. The only time a fizzle is treated as an error is when it is the last execution in the program, in which case it is an \emph{execution fizzled error}. Most things considered ``errors'' languages with typical exception handling constructs (e.g. \texttt{try} and \texttt{catch}) are handled through the effect system in \Trilogy{}; situations such as I/O failures and division by zero are in this category. Though these situations are yielded via the effect system, if you do not handle that effect, then it does become an \emph{unhandled effect error}. This is true of any effect that is yielded in a program, even effects of your own design; if they are not handled by any surrounding handler, there can be no known course of action so the program must end. Finally, in the case of an unrecoverable error in the runtime implementation itself, the program ends with an \emph{internal runtime error}. This situation should be rare, maybe caused by the host process running out of memory, or other unexpected hardware failure. Honestly, you should hope never to witness this, as it means there is something seriously going wrong. \subsection{Exit Codes} \label{sec:exitcode} Apart from runtime errors causing the program to exit with a code of \texttt{255}, the developer may choose their own exit codes in a few ways. In the simplest case, a \Trilogy{} program's exit code is determined by the return value of its \fn{main} procedure. The first execution to reach the end of the \fn{main} procedure ends the program with the return value as the exit code. If the return value of \fn{main} is not a valid exit code value, this is a runtime type error and exit code is set to \texttt{255}. Alternatively, the program may be ended at any time by using the \kw{exit} keyword. The \kw{exit} keyword ends the program with the value it is provided. Again, if the value is not a valid exit code value, it is a runtime type error and the exit code is set to \texttt{255}. \subsection{Module Resolution} \label{sec:module-resolution} The ability to define external modules is one of practical concern. Though it would technically be possible to write your entire program in one file, it simply wouldn't be a good time. External modules in \Trilogy{} are referenced by strings, and the ``official'' method by which those strings are resolved into the modules they are meant to represent is described here. Alternative module resolution algorithms may be implemented as extensions by user code in the host language. Strings designating modules should come in one of a few forms: relative paths, absolute paths, or absolute URLs. Given that, there are two phases to the module resolution algorithm: first relative and absolute paths are resolved to absolute URLs, then those absolute URLs are resolved to module. An absolute path begins with a \texttt{/} character, or the sequence \texttt{file:///} indicating an absolute URL with its scheme included, and is resolved to an absolute URL as follows: \begin{enumerate} \item The schema and authority of URL to the current \Trilogy{} file is prepended to the path (replacing any existing scheme if present). \begin{itemize} \item For files on the local system, this would be \texttt{file://}. \item For files located at a remote URL, these values are taken from that URL. \end{itemize} \item The path is \emph{normalized}. \end{enumerate} \noindent A relative path begins with a \texttt{.} character, and is resolved to an absolute URL as follows: \begin{enumerate} \item The scheme, authority, and path up to and including its final \texttt{/} character are prepended to the relative path. \begin{itemize} \item For files on the local system, the scheme and authority are \texttt{file://}. \item For files located at a remote URL, the scheme, authority, and path taken from that URL. \end{itemize} \item The path is \emph{normalized}. \end{enumerate} \noindent Normalization is performed as follows: \begin{enumerate} \item Any empty path segments (\texttt{//}) are replaced with \texttt{/} \item Any useless path segments (\texttt{/./}) are replaced with \texttt{/} \item For any ``parent'' path segments following a named path segment (e.g. \texttt{/folder/../}), replace the two segments together with \texttt{/} \item If the final URL begins with \texttt{/../}, the path is marked invalid and the referenced module cannot be resolved. \end{enumerate} The resulting URL is now the absolute URL that is used to locate the module being resolved. This is also the URL that is used to further resolve external modules referenced by the resolved module. The file's contents are then retrieved depending on its scheme. At this time, the following schemes are supported: \begin{description} \item[\texttt{file}] the file is loaded from the current file system using the operating system's native means. Symbolic links should be resolved, but the module is still considered to be located at the original URL rather than the URL that the link pointed to. \item[\texttt{http} or \texttt{https}] the file is loaded by making an HTTP or HTTPS \texttt{GET} request (respectively) to the URL, as would be sent by following standard cURL command: \lstinputlisting[language=sh]{get-curl.sh} This request must respond with a response code of \texttt{200}, and the body of the response must contain the \Trilogy{} source code. It is the responsibility of the server hosting these paths that the requests are handled in an appropriate manner and that further external modules referenced in those files are resolvable. \end{description} In any case, the expected result is that the contents of the specified \Trilogy{} file are retrieved as a UTF-8 encoded text file. If this is not the case for any reason (invalid file contents, insufficient file system permissions, failed HTTP request, etc.), then the module cannot not be resolved and the program is treated as invalid. Within a single invocation of the \Trilogy{} interpreter, any given module should only be resolved once. If two paths in the program resolve to the same absolute URL, those two modules are identical, as if aliases of each other. A reference to such a module in the source code by any path is exactly equivalent.