blob: fb805a63e03331e5a918d5938772fd222fcf81b9 [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
159\subsection{Control Flow}
160
161\definition{
swissChili36f2c692021-08-08 14:31:44 -0700162 (\mac{if} \param{predicate} \param{then} \opt{otherwise}{nil})\index{if} \\
163 (\mac{when} \param{predicate} \param{then\more})\index{when} \\
164 (\mac{unless} \param{predicate} \param{otherwise\more})\index{unless}
swissChili8a4b4ed2021-08-03 19:27:32 -0700165}{
swissChili36f2c692021-08-08 14:31:44 -0700166 First evaluate \param{predicate}. If it is \gls{truthy} evaluate and
167 return \ret{\param{then}}, otherwise \ret{\param{otherwise}}. If
168 either is not provided return \ret{\nil}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700169}
170
171\definition{
swissChili36f2c692021-08-08 14:31:44 -0700172 (\mac{progn} \opt{forms\more}{nil})\index{progn}
swissChili8a4b4ed2021-08-03 19:27:32 -0700173}{
swissChili36f2c692021-08-08 14:31:44 -0700174 Evaluate \param{forms} from first to last, finally returning
175 \ret{the last form}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700176}
177
swissChilia89ee442021-08-04 20:54:51 -0700178\definition{
swissChili36f2c692021-08-08 14:31:44 -0700179 (\mac{and} \param{first} \param{\mighteval{rest}}\more)\index{and} \\
180 (\mac{or} \param{first} \param{\mighteval{rest}}\more)\index{or}
swissChilia89ee442021-08-04 20:54:51 -0700181}{
swissChili36f2c692021-08-08 14:31:44 -0700182 Short circuiting $\land$ and $\lor$, respectively. Return the first
183 value that is \nil{} or truthy, respectively, or the last value if
184 all are truthy/\nil{}.
swissChilia89ee442021-08-04 20:54:51 -0700185}
186
swissChili8a4b4ed2021-08-03 19:27:32 -0700187\section{Numbers}
188
189\subsection{Integers}
190
191\definition{
swissChili36f2c692021-08-08 14:31:44 -0700192 (\func{$+$} \param{a b})\index{+} \\
193 (\func{$-$} \param{a b})\index{-} \\
194 (\func{$*$} \param{a b})\index{*} \\
195 (\func{$/$} \param{a b})\index{/}
swissChili8a4b4ed2021-08-03 19:27:32 -0700196}{
swissChili36f2c692021-08-08 14:31:44 -0700197 The \ret{sum, difference, product, or quotient} of \param{a} and
198 \param{b} as an \type{integer}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700199}
200
201\definition{
swissChili36f2c692021-08-08 14:31:44 -0700202 (\func{=} \param{a b})
swissChili8a4b4ed2021-08-03 19:27:32 -0700203}{
swissChili36f2c692021-08-08 14:31:44 -0700204 \ret{\T} if \param{a} and \param{b} hold the same \type{integer}
205 value, \ret{\nil} otherwise.
swissChili8a4b4ed2021-08-03 19:27:32 -0700206}
207
208\section{Function Manipulation}
209
210\definition{
swissChili36f2c692021-08-08 14:31:44 -0700211 (\func{funcall} \param{args}\more)\index{funcall} \\
212 (\func{apply} \param{function} \param{args})\index{apply}
swissChili8a4b4ed2021-08-03 19:27:32 -0700213}{
swissChili36f2c692021-08-08 14:31:44 -0700214 Call the \type{closure} or \type{function-object} \param{function}
215 with \param{args} and evaluate to \ret{its result}. An error occurs
216 if \param{args} are not acceptable.
swissChili8a4b4ed2021-08-03 19:27:32 -0700217}
218
219\definition{
swissChili36f2c692021-08-08 14:31:44 -0700220 (\func{recurse} \param{args}\more)\index{recurse}
swissChili8a4b4ed2021-08-03 19:27:32 -0700221}{
swissChili36f2c692021-08-08 14:31:44 -0700222 In a lambda definition, call the current lambda with \param{args}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700223}
224
225\definition{
swissChili36f2c692021-08-08 14:31:44 -0700226 (\mac{function} \param{function-name})\index{function} \\
227 \reader{\#\textquotesingle}\param{function-name}\index{\#\textquotesingle}
swissChili8a4b4ed2021-08-03 19:27:32 -0700228}{
swissChili36f2c692021-08-08 14:31:44 -0700229 Create a \ret{\type{function-object} from an existing function or
230 macro}. \param{function} must be a symbol literal at compile time.
swissChili8a4b4ed2021-08-03 19:27:32 -0700231}
232
233\definition{
swissChili36f2c692021-08-08 14:31:44 -0700234 (\func{lambda} (\param{args}\more) \param{body}\more)\index{lambda}
swissChili8a4b4ed2021-08-03 19:27:32 -0700235}{
swissChili36f2c692021-08-08 14:31:44 -0700236 Create a \ret{lexically-scoped \type{\gls{closure}}} taking
237 \param{args} and evaluating to \param{body}.
238}
239
240\definition{
241 (\func{eval} \param{form})\index{eval}
242}{
243 Evaluate and return \ret{\param{form}} in the current global
244 environment. The evaluated form does not use \gls{lexical-scope}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700245}
246
247\section{Lists}
248
249\subsection{Creating Lists}
250
251\definition{
swissChili36f2c692021-08-08 14:31:44 -0700252 (\func{cons} \param{a} \param{b})\index{cons}
swissChili8a4b4ed2021-08-03 19:27:32 -0700253}{
swissChili36f2c692021-08-08 14:31:44 -0700254 Join \param{a} and \param{b} into a \ret{\type{cons} pair}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700255}
256
257\definition{
swissChili36f2c692021-08-08 14:31:44 -0700258 (\func{list} \param{values}\more)\index{list}
swissChili8a4b4ed2021-08-03 19:27:32 -0700259}{
swissChili36f2c692021-08-08 14:31:44 -0700260 Construct a \ret{\type{cons}-list of \param{values}}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700261}
262
263\subsection{Deconstructing Lists}
264
265\definition{
swissChili36f2c692021-08-08 14:31:44 -0700266 (\func{length} \param{list})\index{list}
swissChili8a4b4ed2021-08-03 19:27:32 -0700267}{
swissChili36f2c692021-08-08 14:31:44 -0700268 Return the \ret{length of \param{list}} if it is a \type{cons}-list,
269 \nil{} otherwise.
swissChili8a4b4ed2021-08-03 19:27:32 -0700270}
271
272\definition{
swissChili36f2c692021-08-08 14:31:44 -0700273 (\func{car} \param{pair})\index{car} \\
274 (\func{cdr} \param{pair})\index{cdr}
swissChili8a4b4ed2021-08-03 19:27:32 -0700275}{
swissChili36f2c692021-08-08 14:31:44 -0700276 Return the \ret{first or second item} of \type{cons} \param{pair},
277 respectively.
swissChili8a4b4ed2021-08-03 19:27:32 -0700278}
279
280\definition{
swissChili36f2c692021-08-08 14:31:44 -0700281 (\optlist{
282 \func{caar}\\
283 \func{cadr}\\
284 \func{caddr}\\
285 \func{cadar}\\
286 \func{caddar}
287 } \param{val})
swissChili8a4b4ed2021-08-03 19:27:32 -0700288}{
swissChili36f2c692021-08-08 14:31:44 -0700289 Behave like a combination of \func{car} and \func{cdr} would.
swissChili8a4b4ed2021-08-03 19:27:32 -0700290}
291
292\definition{
swissChili36f2c692021-08-08 14:31:44 -0700293 (\func{elt} \param{list} \param{n})\index{elt}
swissChili8a4b4ed2021-08-03 19:27:32 -0700294}{
swissChili36f2c692021-08-08 14:31:44 -0700295 Return the \ret{\param{n}\super{th} element of \param{list}},
296 starting from 0, or \ret{\nil} if \param{n} $ \ge $ (\func{length}
297 \param{list}) or \param{list} is not a \type{cons}-list or \param{n}
298 is not an \type{integer}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700299}
300
301\subsection{Operating on Lists}
302
303\definition{
swissChili36f2c692021-08-08 14:31:44 -0700304 (\func{mapcar} \param{fun} \param{list})\index{mapcar}
swissChili8a4b4ed2021-08-03 19:27:32 -0700305}{
swissChili36f2c692021-08-08 14:31:44 -0700306 Apply \param{fun} to each element of \param{list}, returning a
307 \ret{new \type{cons}-list} containing the results of the respective
308 applications of \param{fun}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700309}
310
311\definition{
swissChili36f2c692021-08-08 14:31:44 -0700312 (\optlist{\func{remove-if}\\\func{remove-if-not}}
313 \param{predicate} \param{list})\index{remove-if}\index{remove-if-not}
swissChili8a4b4ed2021-08-03 19:27:32 -0700314}{
swissChili36f2c692021-08-08 14:31:44 -0700315 Return a \ret{new \type{cons}-list} of all the items of \param{list}
316 that either do not or do satisfy \param{predicate}, respectively.
swissChili8a4b4ed2021-08-03 19:27:32 -0700317}
318
319\definition{
swissChili36f2c692021-08-08 14:31:44 -0700320 (\func{reduce} \param{fun} \param{list} \opt{initial-value}{\nil})
swissChili8a4b4ed2021-08-03 19:27:32 -0700321}{
swissChili36f2c692021-08-08 14:31:44 -0700322 Apply \param{fun} to two arguments at a time, starting with
323 \param{initial-value} and (\func{car} \param{list}) and continuing
324 with the result of the previous invocation and the successive
325 element of \param{list}. Return \ret{the result of the final
326 invocation}, or \ret{\param{initial-value}} if \param{list} is
327 empty.
swissChili8a4b4ed2021-08-03 19:27:32 -0700328}
329
330\section{Input \& Output}
331
332\definition{
swissChili36f2c692021-08-08 14:31:44 -0700333 (\func{print} \param{value})\index{print}
swissChili8a4b4ed2021-08-03 19:27:32 -0700334}{
swissChili36f2c692021-08-08 14:31:44 -0700335 Print \param{value} to standard output. Return \ret{\nil}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700336}
337
338\definition{
swissChili36f2c692021-08-08 14:31:44 -0700339 (\func{read})\index{read}
swissChili8a4b4ed2021-08-03 19:27:32 -0700340}{
swissChili36f2c692021-08-08 14:31:44 -0700341 Read and return an \ret{S-expression} from standard input
swissChili8a4b4ed2021-08-03 19:27:32 -0700342}
343
344\subsection{Loading Programs}
345
346\definition{
swissChili36f2c692021-08-08 14:31:44 -0700347 \const{\pluses{current-file}}\index{\pluses{current-file}}
swissChili8a4b4ed2021-08-03 19:27:32 -0700348}{
swissChili36f2c692021-08-08 14:31:44 -0700349 The current file being compiled, or \nil{} if not compiling a file.
swissChili8a4b4ed2021-08-03 19:27:32 -0700350}
351
352\definition{
swissChili36f2c692021-08-08 14:31:44 -0700353 (\func{load} \param{lisp-file})\index{load}
swissChili8a4b4ed2021-08-03 19:27:32 -0700354}{
swissChili36f2c692021-08-08 14:31:44 -0700355 Load and evaluate \type{string} \param{lisp-file} as a local path
356 relative to the current file, or the current working directory if
357 not compiling a file. Return \ret{\nil}.
swissChili8a4b4ed2021-08-03 19:27:32 -0700358}
359
360\printindex
361
362\printglossaries
363
364\end{document}