~swisschili/bluejay

ref: a7568dc427ab04f649fecea3e9425f1c18e586d0 bluejay/doc/lisp_reference/lisp_reference.tex -rw-r--r-- 10.4 KiB
a7568dc4swissChili Add unquote-splice (,@) 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
\documentclass[letterpaper,11pt,twocolumn]{article}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{color}
\usepackage{xcolor}
\usepackage{url}
\usepackage{textcomp}
\usepackage{listings}
\usepackage{glossaries}
\usepackage{parskip}
\usepackage{imakeidx}
\usepackage[normalem]{ulem}

\title{Bluejay Lisp Reference}
\author{swissChili}
\date{Development Version: \today}

\newcommand{\startexplanation}{$\triangleright$\hskip1.4ex}
\newcommand{\definition}[2]{#1
  \begin{quote}%
    \startexplanation#2%
  \end{quote}}
\newcommand{\plus}{\texttt{+}}
\newcommand{\pluses}[1]{\plus{}#1\plus{}}
\newcommand{\earmuff}{\texttt{*}}
\newcommand{\earmuffs}[1]{\earmuff{}#1\earmuff{}}
\newcommand{\func}[1]{\text{$_f$\textbf{#1}}}
\newcommand{\mac}[1]{\text{$_m$\textbf{#1}}}
\newcommand{\reader}[1]{\text{$_r$\textbf{#1}}}
\newcommand{\const}[1]{\text{$_c$\textbf{#1}}}
\newcommand{\var}[1]{\text{$_v$\textbf{#1}}}
\newcommand{\param}[1]{\textit{#1}}
\newcommand{\ret}[1]{\uline{#1}}
\newcommand{\type}[1]{\text{$_t$\textbf{#1}}}
\newcommand{\more}{ \ldots}
\newcommand{\T}{\texttt{t}}
\newcommand{\nil}{\texttt{nil}}
\newcommand{\default}[1]{\text{\textsubscript{
      \setlength{\fboxsep}{1pt}\setlength{\fboxrule}{0.2bp}%
      \fbox{#1}}}}
\newcommand{\opt}[2]{\text{$[$}\param{#1}\default{#2}\text{$]$}}
\newcommand{\mut}[1]{\text{$\widetilde{#1}$}}
\newcommand{\mighteval}[1]{\text{$\widehat{#1}$}}
\newcommand{\super}[1]{\text{$ ^{#1} $}}

\newcommand{\optlist}[1]{\text{\(
    \left\{
      \begin{array}{l}
        #1
      \end{array}
    \right\}
    \)}}

\makeindex
\makeglossaries

\newglossaryentry{closure}{name={closure},description={A
    \type{function-object} that captures certain variables from its
    defining context}}

\newglossaryentry{lexical-scope}{name={lexical scope},description={A
    method of scoping where the values in scope at a given time are
    determined by the static content of the source program, not by the
    state of the callstack or other execution details. This allows for
    example closures to \gls{closure}s to capture some of the state of
    their defining context}}

\newglossaryentry{truthy}{name={truthy},description={A value that is
    not \nil}}

\begin{document}

\maketitle
\tableofcontents

\section{Introduction}

This document provides a brief reference to the Bluejay Lisp language
and standard library. It documents every currently valid function,
macro, reader macro, and special form supported by the compiler.

\subsection{Typography}

The following text styles and symbols are used within this document to
indicate particular values or meanings:

\begin{tabular}[t]{p{0.2\linewidth} p{0.64\linewidth}}
  \func{cons} & A function ``cons.'' \\
  \const{\plus{}foo\plus} & A constant ``\plus{}foo\plus.'' \\
  \var{\earmuff{}bar\earmuff} & A global variable ``\earmuff{}bar\earmuff''. \\
  \reader{baz} & A reader macro ``baz.'' \\
  \mac{quux} & A macro ``quux.'' \\
  \param{parameter} & A function argument ``parameter'' \\
  \opt{var}{123} & An optional function argument ``var'' with the default value ``123.'' \\
  \param{args}\more & ``args'' represents the rest of the items in the list. \\
  \mut{\param{mut}} & A function argument ``mut'' that might be mutated. \\
  \mighteval{\param{maybe}} & ``maybe'' may or may not be evaluated. \\
  \ret{value} & Indicates that a form will evaluate to ``value''. \\
  \type{integer} & The type ``integer''. \\
  \optlist{\text{a}\\\text{b}} & One of ``a'' or ``b.''
\end{tabular}

\section{Primitives}

\subsection{Type Predicates}

\definition{
  (\optlist{
    \func{nilp} \text{ or } \func{not} \\
    \func{integerp}
  } \param{value})\index{nilp}\index{not}
}{
  \ret{\T} if \param{value} is of the specified type, \ret{\nil}
  otherwise.
}

\subsection{Definitions and Variables}

\definition{
  (\mac{defun} \param{name} (\param{args}\more) \param{body}\more)\index{defun} \\
  (\mac{defmacro} \param{name} (\param{args}\more) \param{body}\more)\index{defmacro}
}{
  Define a function or macro respectively, taking \param{args}
  arguments and evaluating \param{body} in turn, finally evaluating to
  \ret{the final entry in \param{body}} or \ret{\nil} if \param{body}
  is empty.
}

\definition{
  (\mac{let1} (\param{variable} \param{form}) \param{body}\more)\index{let1}
}{
  First evaluate \param{form}, binding it to \param{variable}. Then
  evaluate \param{body}, finally evaluating to \ret{the final entry in
    \param{body}} or \ret{\nil} if \param{body} is
  empty. \param{variable} is no longer in scope after this form ends.
}

\definition{
  \reader{\textquotesingle}\param{value} \\
  (\mac{quote} \param{value})\index{quote}
}{
  \ret{Return \param{value}} as-is, without evaluating it.
}

\definition{
  \reader{\`}\param{value} \\
  (\mac{backquote} \param{value})\index{backquote}\index{\`}
}{
  Return \ret{\param{value}} as-is, except for any \mac{unquote}
  and \mac{unquote-splice} forms (and their reader-macro equivalents).
}

\subsection{Control Flow}

\definition{
  (\mac{if} \param{predicate} \param{then} \opt{otherwise}{nil})\index{if} \\
  (\mac{when} \param{predicate} \param{then\more})\index{when} \\
  (\mac{unless} \param{predicate} \param{otherwise\more})\index{unless}
}{
  First evaluate \param{predicate}. If it is \gls{truthy} evaluate and
  return \ret{\param{then}}, otherwise \ret{\param{otherwise}}. If
  either is not provided return \ret{\nil}.
}

\definition{
  (\mac{progn} \opt{forms\more}{nil})\index{progn}
}{
  Evaluate \param{forms} from first to last, finally returning
  \ret{the last form}.
}

\definition{
  (\mac{and} \param{first} \param{\mighteval{rest}}\more)\index{and} \\
  (\mac{or} \param{first} \param{\mighteval{rest}}\more)\index{or}
}{
  Short circuiting $\land$ and $\lor$, respectively. Return the first
  value that is \nil{} or truthy, respectively, or the last value if
  all are truthy/\nil{}.
}

\section{Numbers}

\subsection{Integers}

\definition{
  (\func{$+$} \param{a b})\index{+} \\
  (\func{$-$} \param{a b})\index{-} \\
  (\func{$*$} \param{a b})\index{*} \\
  (\func{$/$} \param{a b})\index{/}
}{
  The \ret{sum, difference, product, or quotient} of \param{a} and
  \param{b} as an \type{integer}.
}

\definition{
  (\func{=} \param{a b})
}{
  \ret{\T} if \param{a} and \param{b} hold the same \type{integer}
  value, \ret{\nil} otherwise.
}

\section{Function Manipulation}

\definition{
  (\func{funcall} \param{args}\more)\index{funcall} \\
  (\func{apply} \param{function} \param{args})\index{apply}
}{
  Call the \type{closure} or \type{function-object} \param{function}
  with \param{args} and evaluate to \ret{its result}. An error occurs
  if \param{args} are not acceptable.
}

\definition{
  (\func{recurse} \param{args}\more)\index{recurse}
}{
  In a lambda definition, call the current lambda with \param{args}.
}

\definition{
  (\mac{function} \param{function-name})\index{function} \\
  \reader{\#\textquotesingle}\param{function-name}\index{\#\textquotesingle}
}{
  Create a \ret{\type{function-object} from an existing function or
    macro}. \param{function} must be a symbol literal at compile time.
}

\definition{
  (\func{lambda} (\param{args}\more) \param{body}\more)\index{lambda}
}{
  Create a \ret{lexically-scoped \type{\gls{closure}}} taking
  \param{args} and evaluating to \param{body}.
}

\definition{
  (\func{eval} \param{form})\index{eval}
}{
  Evaluate and return \ret{\param{form}} in the current global
  environment. The evaluated form does not use \gls{lexical-scope}.
}

\section{Lists}

\subsection{Creating Lists}

\definition{
  (\func{cons} \param{a} \param{b})\index{cons}
}{
  Join \param{a} and \param{b} into a \ret{\type{cons} pair}.
}

\definition{
  (\func{list} \param{values}\more)\index{list}
}{
  Construct a \ret{\type{cons}-list of \param{values}}.
}

\subsection{Deconstructing Lists}

\definition{
  (\func{length} \param{list})\index{list}
}{
  Return the \ret{length of \param{list}} if it is a \type{cons}-list,
  \nil{} otherwise.
}

\definition{
  (\func{car} \param{pair})\index{car} \\
  (\func{cdr} \param{pair})\index{cdr}
}{
  Return the \ret{first or second item} of \type{cons} \param{pair},
  respectively.
}

\definition{
  (\optlist{
    \func{caar}\\
    \func{cadr}\\
    \func{caddr}\\
    \func{cadar}\\
    \func{caddar}
  } \param{val})
}{
  Behave like a combination of \func{car} and \func{cdr} would.
}

\definition{
  (\func{elt} \param{list} \param{n})\index{elt}
}{
  Return the \ret{\param{n}\super{th} element of \param{list}},
  starting from 0, or \ret{\nil} if \param{n} $ \ge $ (\func{length}
  \param{list}) or \param{list} is not a \type{cons}-list or \param{n}
  is not an \type{integer}.
}

\subsection{Operating on Lists}

\definition{
  (\func{mapcar} \param{fun} \param{list})\index{mapcar}
}{
  Apply \param{fun} to each element of \param{list}, returning a
  \ret{new \type{cons}-list} containing the results of the respective
  applications of \param{fun}.
}

\definition{
  (\optlist{\func{remove-if}\\\func{remove-if-not}}
  \param{predicate} \param{list})\index{remove-if}\index{remove-if-not}
}{
  Return a \ret{new \type{cons}-list} of all the items of \param{list}
  that either do not or do satisfy \param{predicate}, respectively.
}

\definition{
  (\func{reduce} \param{fun} \param{list} \opt{initial-value}{\nil})
}{
  Apply \param{fun} to two arguments at a time, starting with
  \param{initial-value} and (\func{car} \param{list}) and continuing
  with the result of the previous invocation and the successive
  element of \param{list}. Return \ret{the result of the final
    invocation}, or \ret{\param{initial-value}} if \param{list} is
  empty.
}

\section{Input \& Output}

\definition{
  (\func{print} \param{value})\index{print}
}{
  Print \param{value} to standard output. Return \ret{\nil}.
}

\definition{
  (\func{read})\index{read}
}{
  Read and return an \ret{S-expression} from standard input
}

\subsection{Loading Programs}

\definition{
  \const{\pluses{current-file}}\index{\pluses{current-file}}
}{
  The current file being compiled, or \nil{} if not compiling a file.
}

\definition{
  (\func{load} \param{lisp-file})\index{load}
}{
  Load and evaluate \type{string} \param{lisp-file} as a local path
  relative to the current file, or the current working directory if
  not compiling a file. Return \ret{\nil}.
}

\printindex

\printglossaries

\end{document}