blob: e53bd149946fa414e4441f75dbcd4c9f39a2c26c [file] [log] [blame]
swissChili8a4b4ed2021-08-03 19:27:32 -07001\documentclass[letterpaper,11pt,twocolumn]{article}
2\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}\usepackage[T1]{fontenc}
3\usepackage[utf8]{inputenc}
4\usepackage{lmodern}
5\usepackage{amsmath}
6\usepackage{amsfonts}
7\usepackage{amssymb}
8\usepackage{amsthm}
9\usepackage{graphicx}
10\usepackage{color}
11\usepackage{xcolor}
12\usepackage{url}
13\usepackage{textcomp}
14\usepackage{listings}
15\usepackage{glossaries}
16\usepackage{parskip}
17\usepackage{imakeidx}
18\usepackage[normalem]{ulem}
19
20\title{Bluejay Lisp Reference}
21\author{swissChili}
22\date{Development Version: \today}
23
24\newcommand{\startexplanation}{$\triangleright$\hskip1.4ex}
25\newcommand{\definition}[2]{#1
swissChili36f2c692021-08-08 14:31:44 -070026 \begin{quote}%
swissChili8a4b4ed2021-08-03 19:27:32 -070027 \startexplanation#2%
swissChili36f2c692021-08-08 14:31:44 -070028 \end{quote}}
swissChili8a4b4ed2021-08-03 19:27:32 -070029\newcommand{\plus}{\texttt{+}}
30\newcommand{\pluses}[1]{\plus{}#1\plus{}}
31\newcommand{\earmuff}{\texttt{*}}
32\newcommand{\earmuffs}[1]{\earmuff{}#1\earmuff{}}
33\newcommand{\func}[1]{\text{$_f$\textbf{#1}}}
34\newcommand{\mac}[1]{\text{$_m$\textbf{#1}}}
35\newcommand{\reader}[1]{\text{$_r$\textbf{#1}}}
36\newcommand{\const}[1]{\text{$_c$\textbf{#1}}}
37\newcommand{\var}[1]{\text{$_v$\textbf{#1}}}
38\newcommand{\param}[1]{\textit{#1}}
39\newcommand{\ret}[1]{\uline{#1}}
40\newcommand{\type}[1]{\text{$_t$\textbf{#1}}}
41\newcommand{\more}{ \ldots}
42\newcommand{\T}{\texttt{t}}
43\newcommand{\nil}{\texttt{nil}}
44\newcommand{\default}[1]{\text{\textsubscript{
swissChili36f2c692021-08-08 14:31:44 -070045 \setlength{\fboxsep}{1pt}\setlength{\fboxrule}{0.2bp}%
46 \fbox{#1}}}}
swissChili8a4b4ed2021-08-03 19:27:32 -070047\newcommand{\opt}[2]{\text{$[$}\param{#1}\default{#2}\text{$]$}}
48\newcommand{\mut}[1]{\text{$\widetilde{#1}$}}
swissChilia89ee442021-08-04 20:54:51 -070049\newcommand{\mighteval}[1]{\text{$\widehat{#1}$}}
swissChili8a4b4ed2021-08-03 19:27:32 -070050\newcommand{\super}[1]{\text{$ ^{#1} $}}
51
52\newcommand{\optlist}[1]{\text{\(
53 \left\{
swissChili36f2c692021-08-08 14:31:44 -070054 \begin{array}{l}
swissChili8a4b4ed2021-08-03 19:27:32 -070055 #1
swissChili36f2c692021-08-08 14:31:44 -070056 \end{array}
swissChili8a4b4ed2021-08-03 19:27:32 -070057 \right\}
swissChili36f2c692021-08-08 14:31:44 -070058 \)}}
swissChili8a4b4ed2021-08-03 19:27:32 -070059
60\makeindex
61\makeglossaries
62
swissChili36f2c692021-08-08 14:31:44 -070063\newglossaryentry{closure}{name={closure},description={A
64 \type{function-object} that captures certain variables from its
65 defining context}}
swissChili8a4b4ed2021-08-03 19:27:32 -070066
swissChili36f2c692021-08-08 14:31:44 -070067\newglossaryentry{lexical-scope}{name={lexical scope},description={A
68 method of scoping where the values in scope at a given time are
69 determined by the static content of the source program, not by the
70 state of the callstack or other execution details. This allows for
71 example closures to \gls{closure}s to capture some of the state of
72 their defining context}}
swissChili8a4b4ed2021-08-03 19:27:32 -070073
swissChili36f2c692021-08-08 14:31:44 -070074\newglossaryentry{truthy}{name={truthy},description={A value that is
75 not \nil}}
swissChili8a4b4ed2021-08-03 19:27:32 -070076
77\begin{document}
78
79\maketitle
80\tableofcontents
81
82\section{Introduction}
83
swissChili36f2c692021-08-08 14:31:44 -070084This document provides a brief reference to the Bluejay Lisp language
85and standard library. It documents every currently valid function,
86macro, reader macro, and special form supported by the compiler.
swissChili8a4b4ed2021-08-03 19:27:32 -070087
88\subsection{Typography}
89
swissChili36f2c692021-08-08 14:31:44 -070090The following text styles and symbols are used within this document to
91indicate particular values or meanings:
swissChili8a4b4ed2021-08-03 19:27:32 -070092
swissChilia89ee442021-08-04 20:54:51 -070093\begin{tabular}[t]{p{0.2\linewidth} p{0.64\linewidth}}
swissChili36f2c692021-08-08 14:31:44 -070094 \func{cons} & A function ``cons.'' \\
95 \const{\plus{}foo\plus} & A constant ``\plus{}foo\plus.'' \\
96 \var{\earmuff{}bar\earmuff} & A global variable ``\earmuff{}bar\earmuff''. \\
97 \reader{baz} & A reader macro ``baz.'' \\
98 \mac{quux} & A macro ``quux.'' \\
99 \param{parameter} & A function argument ``parameter'' \\
100 \opt{var}{123} & An optional function argument ``var'' with the default value ``123.'' \\
101 \param{args}\more & ``args'' represents the rest of the items in the list. \\
102 \mut{\param{mut}} & A function argument ``mut'' that might be mutated. \\
103 \mighteval{\param{maybe}} & ``maybe'' may or may not be evaluated. \\
104 \ret{value} & Indicates that a form will evaluate to ``value''. \\
105 \type{integer} & The type ``integer''. \\
106 \optlist{\text{a}\\\text{b}} & One of ``a'' or ``b.''
swissChili8a4b4ed2021-08-03 19:27:32 -0700107\end{tabular}
108
109\section{Primitives}
110
111\subsection{Type Predicates}
112
113\definition{
swissChili36f2c692021-08-08 14:31:44 -0700114 (\optlist{
115 \func{nilp} \text{ or } \func{not} \\
116 \func{integerp}
117 } \param{value})\index{nilp}\index{not}
swissChili8a4b4ed2021-08-03 19:27:32 -0700118}{
swissChili36f2c692021-08-08 14:31:44 -0700119 \ret{\T} if \param{value} is of the specified type, \ret{\nil}
120 otherwise.
swissChili8a4b4ed2021-08-03 19:27:32 -0700121}
122
123\subsection{Definitions and Variables}
124
125\definition{
swissChili36f2c692021-08-08 14:31:44 -0700126 (\mac{defun} \param{name} (\param{args}\more) \param{body}\more)\index{defun} \\
127 (\mac{defmacro} \param{name} (\param{args}\more) \param{body}\more)\index{defmacro}
swissChili8a4b4ed2021-08-03 19:27:32 -0700128}{
swissChili36f2c692021-08-08 14:31:44 -0700129 Define a function or macro respectively, taking \param{args}
130 arguments and evaluating \param{body} in turn, finally evaluating to
131 \ret{the final entry in \param{body}} or \ret{\nil} if \param{body}
132 is empty.
swissChili8a4b4ed2021-08-03 19:27:32 -0700133}
134
135\definition{
swissChili36f2c692021-08-08 14:31:44 -0700136 (\mac{let1} (\param{variable} \param{form}) \param{body}\more)\index{let1}
swissChili8a4b4ed2021-08-03 19:27:32 -0700137}{
swissChili36f2c692021-08-08 14:31:44 -0700138 First evaluate \param{form}, binding it to \param{variable}. Then
139 evaluate \param{body}, finally evaluating to \ret{the final entry in
140 \param{body}} or \ret{\nil} if \param{body} is
141 empty. \param{variable} is no longer in scope after this form ends.
swissChili8a4b4ed2021-08-03 19:27:32 -0700142}
143
144\definition{
swissChili36f2c692021-08-08 14:31:44 -0700145 \reader{\textquotesingle}\param{value} \\
146 (\mac{quote} \param{value})\index{quote}
swissChili8a4b4ed2021-08-03 19:27:32 -0700147}{
swissChili36f2c692021-08-08 14:31:44 -0700148 \ret{Return \param{value}} as-is, without evaluating it.
149}
150
151\definition{
152 \reader{\`}\param{value} \\
153 (\mac{backquote} \param{value})\index{backquote}\index{\`}
154}{
155 Return \ret{\param{value}} as-is, except for any \mac{unquote}
156 and \mac{unquote-splice} forms (and their reader-macro equivalents).
swissChili8a4b4ed2021-08-03 19:27:32 -0700157}
158
swissChili3f7f5842021-08-08 17:13:45 -0700159\definition{
160 \reader{,}\param{value} \\
161 (\mac{unquote} \param{value}) \\
162 \reader{,\texttt{@}}\param{value} \\
163 (\mac{unquote-splice} \param{value})
164 \index{unquote}\index{unquote-splice}
165 \index{,}\index{,@}
166}{
167 In the context of a \mac{backquote}, evaluate \param{value} instead of
168 using it as-is. \mac{unquote-splice} and \reader{,\texttt{@}} take a
169 list as their argument, and ``splice'' each element in to
170 the \mac{backquote} list so that each element of that argument corresponds
171 to an element of the \mac{backquote} list.
172 The other two macros insert their argument as a single element in the list.
173}
174
swissChili8a4b4ed2021-08-03 19:27:32 -0700175\subsection{Control Flow}
176
177\definition{
swissChili36f2c692021-08-08 14:31:44 -0700178 (\mac{if} \param{predicate} \param{then} \opt{otherwise}{nil})\index{if} \\
179 (\mac{when} \param{predicate} \param{then\more})\index{when} \\
180 (\mac{unless} \param{predicate} \param{otherwise\more})\index{unless}
swissChili8a4b4ed2021-08-03 19:27:32 -0700181}{
swissChili36f2c692021-08-08 14:31:44 -0700182 First evaluate \param{predicate}. If it is \gls{truthy} evaluate and
183 return \ret{\param{then}}, otherwise \ret{\param{otherwise}}. If
184 either is not provided return \ret{\nil}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700185}
186
187\definition{
swissChili36f2c692021-08-08 14:31:44 -0700188 (\mac{progn} \opt{forms\more}{nil})\index{progn}
swissChili8a4b4ed2021-08-03 19:27:32 -0700189}{
swissChili36f2c692021-08-08 14:31:44 -0700190 Evaluate \param{forms} from first to last, finally returning
191 \ret{the last form}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700192}
193
swissChilia89ee442021-08-04 20:54:51 -0700194\definition{
swissChili36f2c692021-08-08 14:31:44 -0700195 (\mac{and} \param{first} \param{\mighteval{rest}}\more)\index{and} \\
196 (\mac{or} \param{first} \param{\mighteval{rest}}\more)\index{or}
swissChilia89ee442021-08-04 20:54:51 -0700197}{
swissChili36f2c692021-08-08 14:31:44 -0700198 Short circuiting $\land$ and $\lor$, respectively. Return the first
199 value that is \nil{} or truthy, respectively, or the last value if
200 all are truthy/\nil{}.
swissChilia89ee442021-08-04 20:54:51 -0700201}
202
swissChili8a4b4ed2021-08-03 19:27:32 -0700203\section{Numbers}
204
205\subsection{Integers}
206
207\definition{
swissChili36f2c692021-08-08 14:31:44 -0700208 (\func{$+$} \param{a b})\index{+} \\
209 (\func{$-$} \param{a b})\index{-} \\
210 (\func{$*$} \param{a b})\index{*} \\
211 (\func{$/$} \param{a b})\index{/}
swissChili8a4b4ed2021-08-03 19:27:32 -0700212}{
swissChili36f2c692021-08-08 14:31:44 -0700213 The \ret{sum, difference, product, or quotient} of \param{a} and
214 \param{b} as an \type{integer}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700215}
216
217\definition{
swissChili36f2c692021-08-08 14:31:44 -0700218 (\func{=} \param{a b})
swissChili8a4b4ed2021-08-03 19:27:32 -0700219}{
swissChili36f2c692021-08-08 14:31:44 -0700220 \ret{\T} if \param{a} and \param{b} hold the same \type{integer}
221 value, \ret{\nil} otherwise.
swissChili8a4b4ed2021-08-03 19:27:32 -0700222}
223
224\section{Function Manipulation}
225
226\definition{
swissChili36f2c692021-08-08 14:31:44 -0700227 (\func{funcall} \param{args}\more)\index{funcall} \\
228 (\func{apply} \param{function} \param{args})\index{apply}
swissChili8a4b4ed2021-08-03 19:27:32 -0700229}{
swissChili36f2c692021-08-08 14:31:44 -0700230 Call the \type{closure} or \type{function-object} \param{function}
231 with \param{args} and evaluate to \ret{its result}. An error occurs
232 if \param{args} are not acceptable.
swissChili8a4b4ed2021-08-03 19:27:32 -0700233}
234
235\definition{
swissChili36f2c692021-08-08 14:31:44 -0700236 (\func{recurse} \param{args}\more)\index{recurse}
swissChili8a4b4ed2021-08-03 19:27:32 -0700237}{
swissChili36f2c692021-08-08 14:31:44 -0700238 In a lambda definition, call the current lambda with \param{args}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700239}
240
241\definition{
swissChili36f2c692021-08-08 14:31:44 -0700242 (\mac{function} \param{function-name})\index{function} \\
243 \reader{\#\textquotesingle}\param{function-name}\index{\#\textquotesingle}
swissChili8a4b4ed2021-08-03 19:27:32 -0700244}{
swissChili36f2c692021-08-08 14:31:44 -0700245 Create a \ret{\type{function-object} from an existing function or
246 macro}. \param{function} must be a symbol literal at compile time.
swissChili8a4b4ed2021-08-03 19:27:32 -0700247}
248
249\definition{
swissChili36f2c692021-08-08 14:31:44 -0700250 (\func{lambda} (\param{args}\more) \param{body}\more)\index{lambda}
swissChili8a4b4ed2021-08-03 19:27:32 -0700251}{
swissChili36f2c692021-08-08 14:31:44 -0700252 Create a \ret{lexically-scoped \type{\gls{closure}}} taking
253 \param{args} and evaluating to \param{body}.
254}
255
256\definition{
257 (\func{eval} \param{form})\index{eval}
258}{
259 Evaluate and return \ret{\param{form}} in the current global
260 environment. The evaluated form does not use \gls{lexical-scope}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700261}
262
263\section{Lists}
264
265\subsection{Creating Lists}
266
267\definition{
swissChili36f2c692021-08-08 14:31:44 -0700268 (\func{cons} \param{a} \param{b})\index{cons}
swissChili8a4b4ed2021-08-03 19:27:32 -0700269}{
swissChili36f2c692021-08-08 14:31:44 -0700270 Join \param{a} and \param{b} into a \ret{\type{cons} pair}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700271}
272
273\definition{
swissChili36f2c692021-08-08 14:31:44 -0700274 (\func{list} \param{values}\more)\index{list}
swissChili8a4b4ed2021-08-03 19:27:32 -0700275}{
swissChili36f2c692021-08-08 14:31:44 -0700276 Construct a \ret{\type{cons}-list of \param{values}}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700277}
278
279\subsection{Deconstructing Lists}
280
281\definition{
swissChili36f2c692021-08-08 14:31:44 -0700282 (\func{length} \param{list})\index{list}
swissChili8a4b4ed2021-08-03 19:27:32 -0700283}{
swissChili36f2c692021-08-08 14:31:44 -0700284 Return the \ret{length of \param{list}} if it is a \type{cons}-list,
285 \nil{} otherwise.
swissChili8a4b4ed2021-08-03 19:27:32 -0700286}
287
288\definition{
swissChili36f2c692021-08-08 14:31:44 -0700289 (\func{car} \param{pair})\index{car} \\
290 (\func{cdr} \param{pair})\index{cdr}
swissChili8a4b4ed2021-08-03 19:27:32 -0700291}{
swissChili36f2c692021-08-08 14:31:44 -0700292 Return the \ret{first or second item} of \type{cons} \param{pair},
293 respectively.
swissChili8a4b4ed2021-08-03 19:27:32 -0700294}
295
296\definition{
swissChili36f2c692021-08-08 14:31:44 -0700297 (\optlist{
298 \func{caar}\\
299 \func{cadr}\\
300 \func{caddr}\\
301 \func{cadar}\\
302 \func{caddar}
303 } \param{val})
swissChili8a4b4ed2021-08-03 19:27:32 -0700304}{
swissChili36f2c692021-08-08 14:31:44 -0700305 Behave like a combination of \func{car} and \func{cdr} would.
swissChili8a4b4ed2021-08-03 19:27:32 -0700306}
307
308\definition{
swissChili36f2c692021-08-08 14:31:44 -0700309 (\func{elt} \param{list} \param{n})\index{elt}
swissChili8a4b4ed2021-08-03 19:27:32 -0700310}{
swissChili36f2c692021-08-08 14:31:44 -0700311 Return the \ret{\param{n}\super{th} element of \param{list}},
312 starting from 0, or \ret{\nil} if \param{n} $ \ge $ (\func{length}
313 \param{list}) or \param{list} is not a \type{cons}-list or \param{n}
314 is not an \type{integer}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700315}
316
317\subsection{Operating on Lists}
318
319\definition{
swissChili36f2c692021-08-08 14:31:44 -0700320 (\func{mapcar} \param{fun} \param{list})\index{mapcar}
swissChili8a4b4ed2021-08-03 19:27:32 -0700321}{
swissChili36f2c692021-08-08 14:31:44 -0700322 Apply \param{fun} to each element of \param{list}, returning a
323 \ret{new \type{cons}-list} containing the results of the respective
324 applications of \param{fun}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700325}
326
327\definition{
swissChili36f2c692021-08-08 14:31:44 -0700328 (\optlist{\func{remove-if}\\\func{remove-if-not}}
329 \param{predicate} \param{list})\index{remove-if}\index{remove-if-not}
swissChili8a4b4ed2021-08-03 19:27:32 -0700330}{
swissChili36f2c692021-08-08 14:31:44 -0700331 Return a \ret{new \type{cons}-list} of all the items of \param{list}
332 that either do not or do satisfy \param{predicate}, respectively.
swissChili8a4b4ed2021-08-03 19:27:32 -0700333}
334
335\definition{
swissChili36f2c692021-08-08 14:31:44 -0700336 (\func{reduce} \param{fun} \param{list} \opt{initial-value}{\nil})
swissChili8a4b4ed2021-08-03 19:27:32 -0700337}{
swissChili36f2c692021-08-08 14:31:44 -0700338 Apply \param{fun} to two arguments at a time, starting with
339 \param{initial-value} and (\func{car} \param{list}) and continuing
340 with the result of the previous invocation and the successive
341 element of \param{list}. Return \ret{the result of the final
342 invocation}, or \ret{\param{initial-value}} if \param{list} is
343 empty.
swissChili8a4b4ed2021-08-03 19:27:32 -0700344}
345
346\section{Input \& Output}
347
348\definition{
swissChili36f2c692021-08-08 14:31:44 -0700349 (\func{print} \param{value})\index{print}
swissChili8a4b4ed2021-08-03 19:27:32 -0700350}{
swissChili36f2c692021-08-08 14:31:44 -0700351 Print \param{value} to standard output. Return \ret{\nil}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700352}
353
354\definition{
swissChili36f2c692021-08-08 14:31:44 -0700355 (\func{read})\index{read}
swissChili8a4b4ed2021-08-03 19:27:32 -0700356}{
swissChili36f2c692021-08-08 14:31:44 -0700357 Read and return an \ret{S-expression} from standard input
swissChili8a4b4ed2021-08-03 19:27:32 -0700358}
359
360\subsection{Loading Programs}
361
362\definition{
swissChili36f2c692021-08-08 14:31:44 -0700363 \const{\pluses{current-file}}\index{\pluses{current-file}}
swissChili8a4b4ed2021-08-03 19:27:32 -0700364}{
swissChili36f2c692021-08-08 14:31:44 -0700365 The current file being compiled, or \nil{} if not compiling a file.
swissChili8a4b4ed2021-08-03 19:27:32 -0700366}
367
368\definition{
swissChili36f2c692021-08-08 14:31:44 -0700369 (\func{load} \param{lisp-file})\index{load}
swissChili8a4b4ed2021-08-03 19:27:32 -0700370}{
swissChili36f2c692021-08-08 14:31:44 -0700371 Load and evaluate \type{string} \param{lisp-file} as a local path
372 relative to the current file, or the current working directory if
373 not compiling a file. Return \ret{\nil}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700374}
375
376\printindex
377
378\printglossaries
379
380\end{document}