Something went wrong. Try again.
Actually just three programming languages in a trenchcoat
Something went wrong. Try again.
8.5 kB · 171 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172\section{Programs}
While the semantics of the language are fun and all, the practical matters ofexecuting a program must also be discussed. One of the goals of \Trilogy{} isto be a practical language, which can be used to build actual programs afterall.
\subsection{Program Arguments}
As with any program run on the command line, \Trilogy{} programs may acceptcommand line arguments which alter the behaviour of the program.
When run through the interpreter command, the command line arguments passedto the \Trilogy{} program are specified after the separator \texttt{--}. Whenthe path to the \Trilogy{} interpreter is specified via the shebang, argumentsmay be passed directly.
From within a \Trilogy{} program, these command line arguments are receivedas 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 thosethat 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 indeeda well formed \Trilogy{} program code contains no error states, only exceptionalcircumstances. A true ``error'' is something that cannot be recovered from, andimmediately ends the \Trilogy{} program, which exits with a non-zero exit codeindicating an error.
The most common of these errors is the \emph{runtime type error}, where anexpression is expected by the runtime to evaluate to a value of a particulartype, but it does not. As this is most likely a developer error, the safestresult 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'scondition evaluating to \kw{false}. This allows custom abstractions to enforcetheir invariants and provide in a way that provides a clear indication thatsomething is being done incorrectly.
A fizzle is typically not treated as an error; fizzles are often expectedto cull unnecessary branches of execution. Fizzles are caused only by bindingswhich do not have any solutions. The only time a fizzle is treated as an erroris when it is the last execution in the program, in which case it is an \emph{executionfizzled error}.
Most things considered ``errors'' languages with typical exceptionhandling constructs (e.g. \texttt{try} and \texttt{catch}) are handled throughthe effect system in \Trilogy{}; situations such as I/O failures and division byzero are in this category. Though these situations are yielded via the effectsystem, 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 ina program, even effects of your own design; if they are not handled by anysurrounding handler, there can be no known course of action so the programmust end.
Finally, in the case of an unrecoverable error in the runtime implementationitself, the program ends with an \emph{internal runtime error}. This situationshould be rare, maybe caused by the host process running out of memory, orother unexpected hardware failure. Honestly, you should hope never to witnessthis, 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 thereturn value of its \fn{main} procedure. The first execution to reach the endof the \fn{main} procedure ends the program with the return value as the exitcode. If the return value of \fn{main} is not a valid exit code value, thisis 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, ifthe value is not a valid exit code value, it is a runtime type error and the exit codeis set to \texttt{255}.
\subsection{Module Resolution}\label{sec:module-resolution}
The ability to define external modules is one of practical concern. Though itwould technically be possible to write your entire program in one file,it simply wouldn't be a good time. External modules in \Trilogy{} arereferenced by strings, and the ``official'' method by which those stringsare resolved into the modules they are meant to represent is described here.Alternative module resolution algorithms may be implemented as extensions byuser 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 moduleresolution algorithm: first relative and absolute paths are resolved to absoluteURLs, 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 absoluteURL 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}
\noindentA relative path begins with a \texttt{.} character, and is resolved to an absoluteURL 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}
\noindentNormalization 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 beingresolved. This is also the URL that is used to further resolve external modulesreferenced by the resolved module. The file's contents are then retrieved dependingon 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 anyreason (invalid file contents, insufficient file system permissions, failed HTTPrequest, etc.), then the module cannot not be resolved and the program is treatedas invalid.
Within a single invocation of the \Trilogy{} interpreter, any given module shouldonly be resolved once. If two paths in the program resolve to the same absoluteURL, those two modules are identical, as if aliases of each other. A reference tosuch a module in the source code by any path is exactly equivalent.