summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lisp-on-tex/README.md
blob: 045f98aaed03bdfc573ea63bbac979c73bb43bb0 (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# LISP on TeX #

A LISP interpreter written only with TeX macros.
It works as a style file of LaTeX.
LISP on TeX adopts static scoping, dynamic typing, and eager evaluation.
We can program easily with LISP on TeX.

## Summary ##

To use LISP on TeX, you should include the `lisp-on-tex` package.
```
\usepackage{lisp-on-tex}
```

If you do it, you can write LISP codes as a argument of `\lispinterp`.
```
\lispintrep{
  (\some \LISP 'codes')
  % example
  (\define (\sum \a \b) (\+ \a \b))
}
```

In LISP on TeX, a symbol is a control sequence;
a string is tokens surrounded by quotation marks;
and an integer is a TeX's integer using colon prefix.

## Installation ##

Put all files into your TEXMF tree.


## Details ##

### Class Options ###

| Option Name | Meaning                         |
| ----------- | ------------------------------- |
| `noGC`      | Never using GC (default)        |
| `markGC`    | Using Mark-Sweep GC             |
| `GCopt=...` | Passing option to the GC engine |

Currently, LISP on TeX supports Mark-Sweep GC.
If you want to use it, you should use `markGC` option.
You can also control heap size by using `GCopt={heapsize=n}`
where `n` is greater than 3000. The default heap size is 32768.
For example, the code
```
\usepackage[markGC, GCopt={heapsize=5000}]{lisp-on-tex}
```
shows that LISP on TeX uses Mark-Sweep GC and the heap size is 5000.

### Syntax ###

| Kinds              | Literals                                       | Examples         |
| ------------------ | ---------------------------------------------- | ---------------- |
| CONS Cell          | `(` *obj* ... `.` *obj* `)`, `(` *obj* ... `)` | `(\+ :1 :2)`     |
| Integer            | `:` *TeX's integer*                            | `:42`, `:"3A`    |
| String             | `'` *TeX's balanced tokens* `'`                | `'\foo{bar}baz'` |
| Symbol             | *TeX's control sequence*                       | `\cs`            |
| Boolean            | `/t` or `/f`                                   |                  |
| Nil                | `()`                                           |                  |
| Skip               | `@` *TeX's skip*                               | `@12pt plus 34cm`|
| Dimen              | `!` *TeX's dimen*                              | `!56pt`          |

### Functions and Special Forms ###

#### Definition ####

`\define` : Define a symbol.
************************************

```
% symbol form
(\define \foo :42) % ()
\foo % :42
% function form
(\define (\foo \n) (\* \n :2))
(\foo :3) % :6
```

`\defineM` : Define a mutable symbol.
*************************************
`\setB` : Rewrite a mutable symbol.
*************************************

```
% symbol form
(\defineM \foo :42) % ()
\foo % :42
(\setB \foo 'bar')
\foo % 'bar'
```

`\defmacro` : Define a macro.
*************************************
`\macroexpand` : Expand a macro
*************************************

```
(\defmacro (\foo \x) (\list (\quote \bar) \x \x \x)) % ()
(\macroexpand (\quote (\foo :1))) % (\bar :1 :1 :1)
```


`\lambda` : Create a function.
**************************************

```
% normal form
((\lambda (\x) (\+ \x :2)) :3) % :5
% list form
((\lambda \x \x) :1 :2) % (:1 :2)
% remain argument form
((\lambda (\x . \y) \y) :1 :2 :3) % (:2 :3)
```

`\let` : Define local symbols.
*****************************************

```
(\define \x 'foo')
(\let ((\x :4) (\y :3)) (\+ \x \y)) % :7
\x % 'foo'
```

`\letM` : Define mutable local symbols.
******************************************

```
(\letM ((\x 'foo'))
  (\begin (\setB \x 'bar') \x)) % 'bar'
```

`\letrec` : Define local symbols recursively.
********************************************

```
(\letrec
  ((\oddQ (\lambda (\n)
             (\lispif (\= \n :0) /f (\evenQ (\- \n :1)))))
   (\evenQ (\lambda (\n)
             (\lispif (\= \n :0) /t (\oddQ (\- \n :1))))))
   (\oddQ :42)) % /f
```

#### Control Flow ####

`\lispif` : Branch.
********************************************

```
(\lispif /t 'true' 'false') % 'true'
(\lispif /f 'true' 'false') % 'false'
```

`\begin` : Execute expressions.
********************************************

```
(\letM ((\x :1)) (\begin (\setB \s 'foo') \x))
% 'foo'
```

`\callOCC` : One-shot continuation.
*******************************************

```
(\defineM \x 'unchanged')
(\callOCC (\lambda (\c)
             (\begin (\c '\foo ')
                     (\setB \x 'changed')))) % '\foo '
\x % 'unchanged'
(\callOCC (\lambda (\c) :42)) % :42
```

#### String Manipulations ####

`\concat` : Concatenate tokens.
************************************

```
(\concat '$' '\foo ' '{bar}' '$') % '$\foo {bar}$'
```

`\intTOstring` : Convert a integer to TeX's tokens.
******************************************************

```
(\intTOstring :42) % '42'
```

`\group` : Grouping.
*******************************

```
(\group '\some {tokens}') % '{\some {tokens}}'
```

`\ungroup` : Ungrouping.
************************

```
(\ungroup '{\some {tokens}}') % '\some {tokens}' 
```

`\expand` : Expand tokens.
******************************

```
\newcommand\foo[1]{I got #1!}
\lispinterp{
  (\expand '\foo{Foo}') % 'I got Foo!'
}
```

#### Arithmetical Functions ####

`\+` : Addition.
***************************

```
(\+) % :0
(\+ :1 :2) % :3
(\+ :3 :4 :5) % :12
```

`\-` : Subtraction.
************************

```
(\- :1) % :-1
(\- :3 :2) % :1
(\- :3 :2 :1) % :0
```

`\*` : Multiplication.
**********************

```
(\*) % :1
(\* :2 :3) % :6
(\* :3 :4 :5) % :60
```

`\/` : Division.
*************************

```
(\/ 2) % :0 (1/2 -> 0)
(\/ 7 2) % :3
```

`\mod` : Modulo.
*************************

```
(\mod :42 :23)  % :19
(\mod :3 :2)    % :1
(\mod :3 :-2)   % :1
(\mod :-3 :2)   % :-1
(\mod :-3 :-2)  % :-1
```

`\>`, `\<`, `\geq`, `\leq` : Comparison.
*******************************************

```
(\> :3 :2)   % /t
(\< :2 :3)   % /t
(\geq :3 :2) % /t
(\geq :3 :3) % /t
(\leq :2 :3) % /t
(\leq :3 :3) % /t
```

Some predicates.
************************

```
(\isZeroQ :0)    % /t
(\positiveQ :42) % /t
(\negativeQ :-2) % /t
```

`\max` : Maximum.
***************************

```
(\max :-10 :-5 :0 :5 :10) % :10
```

`\min` : Minimum.
*****************************

```
(\min :-10 :-5 :0 :5 :10) % :-10
```

#### Logical functions ####

`\and`, `\or`, `\not` : Logical and, or, not
*********************************************

```
(\and /t /t) % /t
(\and /t /f) % /f
(\or /t /t)  % /t
(\or /t /f)  % /t
(\not /t)    % /f
```


#### Traditional LISP Functions and Special Forms ####

`\quote` : Quote.
*******************

```
(\quote :42) % :42
(\quote (\+ :1 :2)) % (\+ :1 :2)
```

`\cons`, `\car`, `\cdr` : CONS, CAR, CDR
*************************************

```
(\cons :42 'foo') % (:42 . 'foo')
(\car (\quote (:1 :2))) % :1
(\cdr (\quote (:1 :2))) % (:2)
```

`\list` : Create a list
***************************

```
(\list :1 :2 (\+ :3 :4)) % (:1 :2 :7)
```

`\length` : Get the length of a list.
*****************************************

```
(\length ()) % :0
(\length (\list :1 :2 'three')) % :3
```

`\map` : Map function.
**************************

```
(\define (\f \x \y \z) (\+ \x \y \z))
(\map \f (\list :1 :2 :3)
         (\list :4 :5 :6)
         (\list :7 :8 :9)) % (:12 :15 :18)
```

`\nth` : Get the n-th value of a list (starting with 0).
***********************************************************

```
(\nth (\list 'foo' 'bar' 'baz') :1) % 'bar'
```


`\=` : Equality.
*******************

```
(\= '42' :42) % /f
(\= :23 :23) % /t
(\= (\cons :1 'foo') (\cons :1 'foo')) % /f
(\= 'foo' 'foo') % /t
```
`\texprint` : Convert a object to TeX's tokens and output it to the document
******************************************************************************

```
(\texprint (\concat '\foo' (\group '42'))) % return () andoutput \foo{42}
(\texprint :42) % output 42
```

`\print` : (For test) output a object as TeX's tokens
*******************************************************

```
(\print ()) % output ()
(\print (\quote \foo)) % output \string\foo
(\print :42) % output :42
(\print 'bar') % output 'bar'
```

Type predicates
*********************

```
(\symbolQ (\quote \cs))
(\stringQ 'foo')
(\intQ :42)
(\booleanQ /f)
(\dimenQ !12pt)
(\skipQ @12pt plus 1in minus 3mm)
(\pairQ (\cons :1 :2))
(\nilQ ())
(\funcQ \+)
(\closureQ (\lambda () ()))
(\defmacro (\x) ())
(\macroQ \x)
(\listQ ())
(\listQ (\list :1 :2))
(\atomQ :23)
(\atomQ 'bar')
(\procedureQ \+)
(\procedureQ (\lambda () ()))
```

#### LaTeX Utils ####

`\readLaTeXCounter` : Read an integer from LaTeX
***************************************************

```
\setcounter{foo}{42}
\lispinterp{
  (\readLaTeXCounter 'foo') % :42
}
```

`\message` : Wrapper of LaTeX's \message
*******************************************

```
(\message 'output') % output "message" to console and return ()
```

#### Others ####

`\read` : Read a LISP expression from stdin
*********************************************

```
(\read) % input :42 and return it
```

`\fgets` : Read a string from stdin.
***************************************

```
(\fgets) % input \some {tokens} and return '\some {tokens}'
```


##Additional Packages ##

### Fixed Point Numbers ###

The package lisp-mod-fpnum adds fixed point numbers
to LISP on TeX. Load it by `\usepackage`:

```
\usepackage{lisp-on-tex}
\usepackage{lisp-mod-fpnum}
```

#### Syntax ####

| Kinds              | Literals                                       | Examples         |
| ------------------ | ---------------------------------------------- | ---------------- |
| Fixed point number | `+{fpnum::` *number* `}`                       | `+{fpnum::1.23}` |

#### Functions ####

`\fpnumTOstring` : Convert a fixed point number to a string.
************************************************************

```
(\fpnumTOstring +{fpnum::1.23}) % '1.23'
```

`\fpplus` : Addition.
*********************

```
(\fpplus +{fpnum::1.2} +{fpnum::1.4}) % 2.59999 (arithmetical error)
```

`\fpminus` : Subtraction.
************************

```
(\fpminus +{fpnum::4.2} +{fpnum::2.3}) % 1.9
```

`\fpmul` : Multiplication.
*****************************

```
(\fpmul +{fpnum::1.2} +{fpnum::1.4}) % 1.67998
```

`\fplt` : Comparison.
**************************

```
(\fplt +{fpnum::1.2} +{fpnum::2.3}) % /t
```


### Regular Expressions ###

The package lisp-mod-l3regex is thin wrapper
of l3regex. Load it by `\usepackage`:

```
\usepackage{lisp-on-tex}
\usepackage{lisp-mod-l3regex}
```

#### Functions ####

`\regMatch`, `\regMatchResult` : Match.
*************************************

```
(\regMatch 'hoge+' 'hogeeeeeee') % /t
(\regMatchResult '(\w+)\s+is\s+(\w+)\.' 'He is crazy.')
% ('He is crazy.' 'He' 'crazy')
```

`\regExtract` : Extraction.
****************************

```
(\regExtract '\w+' 'hello regex world') % ('hello' 'regex' 'world')
```

`\regReplaceAll`, `\regReplaceOnce` : Replace.
**********************************************

```
(\regReplaceAll '(\w+?)to(\w+?)' '$\1\c{to}\2$' 'AtoB BtoC') % '$A\to B$ $B\to C$'
(\regReplaceOnce 'foo+' '[\0]' 'foooofooooooo') % '[foooo]fooooooo'
```

`\regSplit` : Split.
***********************

```
(\regSplit '/' '/path/to/hogehoge') % ('' 'path' 'to' 'hogehoge')
```


## TODOs ##

* Writing user manual
* Add functions and special forms

## CHANGELOG ##

Oct. 25, 2015 : 2.0
*************************

* Add GC
* Refine some special forms like \define
* Add checking #args for some functions.
* Add thin wrapper of l3regex

Jul. 12, 2014 : 1.3
*************************

* Add one shot continuations.
* Add some arithmetical functions.
* Debug environment.

Jan. 03, 2014 : 1.2
**************************

* Added TUG2013's examples.
* Improved the performance.

Aug. 10, 2013 : 1.1
**************************

* Added \letrec and \expand.
* debug

Mar. 04, 2013 : 1.0
**************************

## Licence ##

Modified BSD (see LICENCE)

************************************************
HAKUTA Shizuya <hak7a3@live.jp>

https://bitbucket.org/hak7a3/lisp-on-tex/