| 1 |
# LISP on TeX # |
| 2 |
|
| 3 |
A LISP interpreter written only with TeX macros. |
| 4 |
It works as a style file of LaTeX. |
| 5 |
LISP on TeX adopts static scoping, dynamic typing, and eager evaluation. |
| 6 |
We can program easily with LISP on TeX. |
| 7 |
|
| 8 |
## Summary ## |
| 9 |
|
| 10 |
To use LISP on TeX, you should include the `lisp-on-tex` package. |
| 11 |
``` |
| 12 |
\usepackage{lisp-on-tex} |
| 13 |
``` |
| 14 |
|
| 15 |
If you do it, you can write LISP codes as a argument of `\lispinterp`. |
| 16 |
``` |
| 17 |
\lispintrep{ |
| 18 |
(\some \LISP 'codes') |
| 19 |
% example |
| 20 |
(\define (\sum \a \b) (\+ \a \b)) |
| 21 |
} |
| 22 |
``` |
| 23 |
|
| 24 |
In LISP on TeX, a symbol is a control sequence; |
| 25 |
a string is tokens surrounded by quotation marks; |
| 26 |
and an integer is a TeX's integer using colon prefix. |
| 27 |
|
| 28 |
## Installation ## |
| 29 |
|
| 30 |
Put all files into your TEXMF tree. |
| 31 |
|
| 32 |
|
| 33 |
## Details ## |
| 34 |
|
| 35 |
### Class Options ### |
| 36 |
|
| 37 |
| Option Name | Meaning | |
| 38 |
| ----------- | ------------------------------- | |
| 39 |
| `noGC` | Never using GC (default) | |
| 40 |
| `markGC` | Using Mark-Sweep GC | |
| 41 |
| `GCopt=...` | Passing option to the GC engine | |
| 42 |
|
| 43 |
Currently, LISP on TeX supports Mark-Sweep GC. |
| 44 |
If you want to use it, you should use `markGC` option. |
| 45 |
You can also control heap size by using `GCopt={heapsize=n}` |
| 46 |
where `n` is greater than 3000. The default heap size is 32768. |
| 47 |
For example, the code |
| 48 |
``` |
| 49 |
\usepackage[markGC, GCopt={heapsize=5000}]{lisp-on-tex} |
| 50 |
``` |
| 51 |
shows that LISP on TeX uses Mark-Sweep GC and the heap size is 5000. |
| 52 |
|
| 53 |
### Syntax ### |
| 54 |
|
| 55 |
| Kinds | Literals | Examples | |
| 56 |
| ------------------ | ---------------------------------------------- | ---------------- | |
| 57 |
| CONS Cell | `(` *obj* ... `.` *obj* `)`, `(` *obj* ... `)` | `(\+ :1 :2)` | |
| 58 |
| Integer | `:` *TeX's integer* | `:42`, `:"3A` | |
| 59 |
| String | `'` *TeX's balanced tokens* `'` | `'\foo{bar}baz'` | |
| 60 |
| Symbol | *TeX's control sequence* | `\cs` | |
| 61 |
| Boolean | `/t` or `/f` | | |
| 62 |
| Nil | `()` | | |
| 63 |
| Skip | `@` *TeX's skip* | `@12pt plus 34cm`| |
| 64 |
| Dimen | `!` *TeX's dimen* | `!56pt` | |
| 65 |
|
| 66 |
### Functions and Special Forms ### |
| 67 |
|
| 68 |
#### Definition #### |
| 69 |
|
| 70 |
`\define` : Define a symbol. |
| 71 |
************************************ |
| 72 |
|
| 73 |
``` |
| 74 |
% symbol form |
| 75 |
(\define \foo :42) % () |
| 76 |
\foo % :42 |
| 77 |
% function form |
| 78 |
(\define (\foo \n) (\* \n :2)) |
| 79 |
(\foo :3) % :6 |
| 80 |
``` |
| 81 |
|
| 82 |
`\defineM` : Define a mutable symbol. |
| 83 |
************************************* |
| 84 |
`\setB` : Rewrite a mutable symbol. |
| 85 |
************************************* |
| 86 |
|
| 87 |
``` |
| 88 |
% symbol form |
| 89 |
(\defineM \foo :42) % () |
| 90 |
\foo % :42 |
| 91 |
(\setB \foo 'bar') |
| 92 |
\foo % 'bar' |
| 93 |
``` |
| 94 |
|
| 95 |
`\defmacro` : Define a macro. |
| 96 |
************************************* |
| 97 |
`\macroexpand` : Expand a macro |
| 98 |
************************************* |
| 99 |
|
| 100 |
``` |
| 101 |
(\defmacro (\foo \x) (\list (\quote \bar) \x \x \x)) % () |
| 102 |
(\macroexpand (\quote (\foo :1))) % (\bar :1 :1 :1) |
| 103 |
``` |
| 104 |
|
| 105 |
|
| 106 |
`\lambda` : Create a function. |
| 107 |
************************************** |
| 108 |
|
| 109 |
``` |
| 110 |
% normal form |
| 111 |
((\lambda (\x) (\+ \x :2)) :3) % :5 |
| 112 |
% list form |
| 113 |
((\lambda \x \x) :1 :2) % (:1 :2) |
| 114 |
% remain argument form |
| 115 |
((\lambda (\x . \y) \y) :1 :2 :3) % (:2 :3) |
| 116 |
``` |
| 117 |
|
| 118 |
`\let` : Define local symbols. |
| 119 |
***************************************** |
| 120 |
|
| 121 |
``` |
| 122 |
(\define \x 'foo') |
| 123 |
(\let ((\x :4) (\y :3)) (\+ \x \y)) % :7 |
| 124 |
\x % 'foo' |
| 125 |
``` |
| 126 |
|
| 127 |
`\letM` : Define mutable local symbols. |
| 128 |
****************************************** |
| 129 |
|
| 130 |
``` |
| 131 |
(\letM ((\x 'foo')) |
| 132 |
(\begin (\setB \x 'bar') \x)) % 'bar' |
| 133 |
``` |
| 134 |
|
| 135 |
`\letrec` : Define local symbols recursively. |
| 136 |
******************************************** |
| 137 |
|
| 138 |
``` |
| 139 |
(\letrec |
| 140 |
((\oddQ (\lambda (\n) |
| 141 |
(\lispif (\= \n :0) /f (\evenQ (\- \n :1))))) |
| 142 |
(\evenQ (\lambda (\n) |
| 143 |
(\lispif (\= \n :0) /t (\oddQ (\- \n :1)))))) |
| 144 |
(\oddQ :42)) % /f |
| 145 |
``` |
| 146 |
|
| 147 |
#### Control Flow #### |
| 148 |
|
| 149 |
`\lispif` : Branch. |
| 150 |
******************************************** |
| 151 |
|
| 152 |
``` |
| 153 |
(\lispif /t 'true' 'false') % 'true' |
| 154 |
(\lispif /f 'true' 'false') % 'false' |
| 155 |
``` |
| 156 |
|
| 157 |
`\begin` : Execute expressions. |
| 158 |
******************************************** |
| 159 |
|
| 160 |
``` |
| 161 |
(\letM ((\x :1)) (\begin (\setB \s 'foo') \x)) |
| 162 |
% 'foo' |
| 163 |
``` |
| 164 |
|
| 165 |
`\callOCC` : One-shot continuation. |
| 166 |
******************************************* |
| 167 |
|
| 168 |
``` |
| 169 |
(\defineM \x 'unchanged') |
| 170 |
(\callOCC (\lambda (\c) |
| 171 |
(\begin (\c '\foo ') |
| 172 |
(\setB \x 'changed')))) % '\foo ' |
| 173 |
\x % 'unchanged' |
| 174 |
(\callOCC (\lambda (\c) :42)) % :42 |
| 175 |
``` |
| 176 |
|
| 177 |
#### String Manipulations #### |
| 178 |
|
| 179 |
`\concat` : Concatenate tokens. |
| 180 |
************************************ |
| 181 |
|
| 182 |
``` |
| 183 |
(\concat '$' '\foo ' '{bar}' '$') % '$\foo {bar}$' |
| 184 |
``` |
| 185 |
|
| 186 |
`\intTOstring` : Convert a integer to TeX's tokens. |
| 187 |
****************************************************** |
| 188 |
|
| 189 |
``` |
| 190 |
(\intTOstring :42) % '42' |
| 191 |
``` |
| 192 |
|
| 193 |
`\stringTOint` : Convert a string to integer |
| 194 |
****************************************************** |
| 195 |
|
| 196 |
``` |
| 197 |
(\stringTOint '42') % :42 |
| 198 |
``` |
| 199 |
|
| 200 |
`\group` : Grouping. |
| 201 |
******************************* |
| 202 |
|
| 203 |
``` |
| 204 |
(\group '\some {tokens}') % '{\some {tokens}}' |
| 205 |
``` |
| 206 |
|
| 207 |
`\ungroup` : Ungrouping. |
| 208 |
************************ |
| 209 |
|
| 210 |
``` |
| 211 |
(\ungroup '{\some {tokens}}') % '\some {tokens}' |
| 212 |
``` |
| 213 |
|
| 214 |
`\expand` : Expand tokens. |
| 215 |
****************************** |
| 216 |
|
| 217 |
``` |
| 218 |
\newcommand\foo[1]{I got #1!} |
| 219 |
\lispinterp{ |
| 220 |
(\expand '\foo{Foo}') % 'I got Foo!' |
| 221 |
} |
| 222 |
``` |
| 223 |
|
| 224 |
#### Arithmetical Functions #### |
| 225 |
|
| 226 |
`\+` : Addition. |
| 227 |
*************************** |
| 228 |
|
| 229 |
``` |
| 230 |
(\+) % :0 |
| 231 |
(\+ :1 :2) % :3 |
| 232 |
(\+ :3 :4 :5) % :12 |
| 233 |
``` |
| 234 |
|
| 235 |
`\-` : Subtraction. |
| 236 |
************************ |
| 237 |
|
| 238 |
``` |
| 239 |
(\- :1) % :-1 |
| 240 |
(\- :3 :2) % :1 |
| 241 |
(\- :3 :2 :1) % :0 |
| 242 |
``` |
| 243 |
|
| 244 |
`\*` : Multiplication. |
| 245 |
********************** |
| 246 |
|
| 247 |
``` |
| 248 |
(\*) % :1 |
| 249 |
(\* :2 :3) % :6 |
| 250 |
(\* :3 :4 :5) % :60 |
| 251 |
``` |
| 252 |
|
| 253 |
`\/` : Division. |
| 254 |
************************* |
| 255 |
|
| 256 |
``` |
| 257 |
(\/ 2) % :0 (1/2 -> 0) |
| 258 |
(\/ 7 2) % :3 |
| 259 |
``` |
| 260 |
|
| 261 |
`\mod` : Modulo. |
| 262 |
************************* |
| 263 |
|
| 264 |
``` |
| 265 |
(\mod :42 :23) % :19 |
| 266 |
(\mod :3 :2) % :1 |
| 267 |
(\mod :3 :-2) % :1 |
| 268 |
(\mod :-3 :2) % :-1 |
| 269 |
(\mod :-3 :-2) % :-1 |
| 270 |
``` |
| 271 |
|
| 272 |
`\>`, `\<`, `\geq`, `\leq` : Comparison. |
| 273 |
******************************************* |
| 274 |
|
| 275 |
``` |
| 276 |
(\> :3 :2) % /t |
| 277 |
(\< :2 :3) % /t |
| 278 |
(\geq :3 :2) % /t |
| 279 |
(\geq :3 :3) % /t |
| 280 |
(\leq :2 :3) % /t |
| 281 |
(\leq :3 :3) % /t |
| 282 |
``` |
| 283 |
|
| 284 |
Some predicates. |
| 285 |
************************ |
| 286 |
|
| 287 |
``` |
| 288 |
(\isZeroQ :0) % /t |
| 289 |
(\positiveQ :42) % /t |
| 290 |
(\negativeQ :-2) % /t |
| 291 |
``` |
| 292 |
|
| 293 |
`\max` : Maximum. |
| 294 |
*************************** |
| 295 |
|
| 296 |
``` |
| 297 |
(\max :-10 :-5 :0 :5 :10) % :10 |
| 298 |
``` |
| 299 |
|
| 300 |
`\min` : Minimum. |
| 301 |
***************************** |
| 302 |
|
| 303 |
``` |
| 304 |
(\min :-10 :-5 :0 :5 :10) % :-10 |
| 305 |
``` |
| 306 |
|
| 307 |
#### Logical functions #### |
| 308 |
|
| 309 |
`\and`, `\or`, `\not` : Logical and, or, not |
| 310 |
********************************************* |
| 311 |
|
| 312 |
``` |
| 313 |
(\and /t /t) % /t |
| 314 |
(\and /t /f) % /f |
| 315 |
(\or /t /t) % /t |
| 316 |
(\or /t /f) % /t |
| 317 |
(\not /t) % /f |
| 318 |
``` |
| 319 |
|
| 320 |
|
| 321 |
#### Traditional LISP Functions and Special Forms #### |
| 322 |
|
| 323 |
`\quote` : Quote. |
| 324 |
******************* |
| 325 |
|
| 326 |
``` |
| 327 |
(\quote :42) % :42 |
| 328 |
(\quote (\+ :1 :2)) % (\+ :1 :2) |
| 329 |
``` |
| 330 |
|
| 331 |
`\cons`, `\car`, `\cdr` : CONS, CAR, CDR |
| 332 |
************************************* |
| 333 |
|
| 334 |
``` |
| 335 |
(\cons :42 'foo') % (:42 . 'foo') |
| 336 |
(\car (\quote (:1 :2))) % :1 |
| 337 |
(\cdr (\quote (:1 :2))) % (:2) |
| 338 |
``` |
| 339 |
|
| 340 |
`\list` : Create a list |
| 341 |
*************************** |
| 342 |
|
| 343 |
``` |
| 344 |
(\list :1 :2 (\+ :3 :4)) % (:1 :2 :7) |
| 345 |
``` |
| 346 |
|
| 347 |
`\length` : Get the length of a list. |
| 348 |
***************************************** |
| 349 |
|
| 350 |
``` |
| 351 |
(\length ()) % :0 |
| 352 |
(\length (\list :1 :2 'three')) % :3 |
| 353 |
``` |
| 354 |
|
| 355 |
`\map` : Map function. |
| 356 |
************************** |
| 357 |
|
| 358 |
``` |
| 359 |
(\define (\f \x \y \z) (\+ \x \y \z)) |
| 360 |
(\map \f (\list :1 :2 :3) |
| 361 |
(\list :4 :5 :6) |
| 362 |
(\list :7 :8 :9)) % (:12 :15 :18) |
| 363 |
``` |
| 364 |
|
| 365 |
`\nth` : Get the n-th value of a list (starting with 0). |
| 366 |
*********************************************************** |
| 367 |
|
| 368 |
``` |
| 369 |
(\nth (\list 'foo' 'bar' 'baz') :1) % 'bar' |
| 370 |
``` |
| 371 |
|
| 372 |
|
| 373 |
`\=` : Equality. |
| 374 |
******************* |
| 375 |
|
| 376 |
``` |
| 377 |
(\= '42' :42) % /f |
| 378 |
(\= :23 :23) % /t |
| 379 |
(\= (\cons :1 'foo') (\cons :1 'foo')) % /f |
| 380 |
(\= 'foo' 'foo') % /t |
| 381 |
``` |
| 382 |
`\texprint` : Convert a object to TeX's tokens and output it to the document |
| 383 |
****************************************************************************** |
| 384 |
|
| 385 |
``` |
| 386 |
(\texprint (\concat '\foo' (\group '42'))) % return () andoutput \foo{42} |
| 387 |
(\texprint :42) % output 42 |
| 388 |
``` |
| 389 |
|
| 390 |
`\print` : (For test) output a object as TeX's tokens |
| 391 |
******************************************************* |
| 392 |
|
| 393 |
``` |
| 394 |
(\print ()) % output () |
| 395 |
(\print (\quote \foo)) % output \string\foo |
| 396 |
(\print :42) % output :42 |
| 397 |
(\print 'bar') % output 'bar' |
| 398 |
``` |
| 399 |
|
| 400 |
Type predicates |
| 401 |
********************* |
| 402 |
|
| 403 |
``` |
| 404 |
(\symbolQ (\quote \cs)) |
| 405 |
(\stringQ 'foo') |
| 406 |
(\intQ :42) |
| 407 |
(\booleanQ /f) |
| 408 |
(\dimenQ !12pt) |
| 409 |
(\skipQ @12pt plus 1in minus 3mm) |
| 410 |
(\pairQ (\cons :1 :2)) |
| 411 |
(\nilQ ()) |
| 412 |
(\funcQ \+) |
| 413 |
(\closureQ (\lambda () ())) |
| 414 |
(\defmacro (\x) ()) |
| 415 |
(\macroQ \x) |
| 416 |
(\listQ ()) |
| 417 |
(\listQ (\list :1 :2)) |
| 418 |
(\atomQ :23) |
| 419 |
(\atomQ 'bar') |
| 420 |
(\procedureQ \+) |
| 421 |
(\procedureQ (\lambda () ())) |
| 422 |
``` |
| 423 |
|
| 424 |
#### LaTeX Utils #### |
| 425 |
|
| 426 |
`\readLaTeXCounter` : Read an integer from LaTeX |
| 427 |
*************************************************** |
| 428 |
|
| 429 |
``` |
| 430 |
\setcounter{foo}{42} |
| 431 |
\lispinterp{ |
| 432 |
(\readLaTeXCounter 'foo') % :42 |
| 433 |
} |
| 434 |
``` |
| 435 |
|
| 436 |
`\message` : Wrapper of LaTeX's \message |
| 437 |
******************************************* |
| 438 |
|
| 439 |
``` |
| 440 |
(\message 'output') % output "message" to console and return () |
| 441 |
``` |
| 442 |
|
| 443 |
#### Others #### |
| 444 |
|
| 445 |
`\read` : Read a LISP expression from stdin |
| 446 |
********************************************* |
| 447 |
|
| 448 |
``` |
| 449 |
(\read) % input :42 and return it |
| 450 |
``` |
| 451 |
|
| 452 |
`\fgets` : Read a string from stdin. |
| 453 |
*************************************** |
| 454 |
|
| 455 |
``` |
| 456 |
(\fgets) % input \some {tokens} and return '\some {tokens}' |
| 457 |
``` |
| 458 |
|
| 459 |
|
| 460 |
##Additional Packages ## |
| 461 |
|
| 462 |
### Fixed Point Numbers ### |
| 463 |
|
| 464 |
The package lisp-mod-fpnum adds fixed point numbers |
| 465 |
to LISP on TeX. Load it by `\usepackage`: |
| 466 |
|
| 467 |
``` |
| 468 |
\usepackage{lisp-on-tex} |
| 469 |
\usepackage{lisp-mod-fpnum} |
| 470 |
``` |
| 471 |
|
| 472 |
#### Syntax #### |
| 473 |
|
| 474 |
| Kinds | Literals | Examples | |
| 475 |
| ------------------ | ---------------------------------------------- | ---------------- | |
| 476 |
| Fixed point number | `+{fpnum::` *number* `}` | `+{fpnum::1.23}` | |
| 477 |
|
| 478 |
#### Functions #### |
| 479 |
|
| 480 |
`\fpnumTOstring` : Convert a fixed point number to a string. |
| 481 |
************************************************************ |
| 482 |
|
| 483 |
``` |
| 484 |
(\fpnumTOstring +{fpnum::1.23}) % '1.23' |
| 485 |
``` |
| 486 |
|
| 487 |
`\fpplus` : Addition. |
| 488 |
********************* |
| 489 |
|
| 490 |
``` |
| 491 |
(\fpplus +{fpnum::1.2} +{fpnum::1.4}) % 2.59999 (arithmetical error) |
| 492 |
``` |
| 493 |
|
| 494 |
`\fpminus` : Subtraction. |
| 495 |
************************ |
| 496 |
|
| 497 |
``` |
| 498 |
(\fpminus +{fpnum::4.2} +{fpnum::2.3}) % 1.9 |
| 499 |
``` |
| 500 |
|
| 501 |
`\fpmul` : Multiplication. |
| 502 |
***************************** |
| 503 |
|
| 504 |
``` |
| 505 |
(\fpmul +{fpnum::1.2} +{fpnum::1.4}) % 1.67998 |
| 506 |
``` |
| 507 |
|
| 508 |
`\fplt` : Comparison. |
| 509 |
************************** |
| 510 |
|
| 511 |
``` |
| 512 |
(\fplt +{fpnum::1.2} +{fpnum::2.3}) % /t |
| 513 |
``` |
| 514 |
|
| 515 |
|
| 516 |
### Regular Expressions ### |
| 517 |
|
| 518 |
The package lisp-mod-l3regex is thin wrapper |
| 519 |
of l3regex. Load it by `\usepackage`: |
| 520 |
|
| 521 |
``` |
| 522 |
\usepackage{lisp-on-tex} |
| 523 |
\usepackage{lisp-mod-l3regex} |
| 524 |
``` |
| 525 |
|
| 526 |
#### Functions #### |
| 527 |
|
| 528 |
`\regMatch`, `\regMatchResult` : Match. |
| 529 |
************************************* |
| 530 |
|
| 531 |
``` |
| 532 |
(\regMatch 'hoge+' 'hogeeeeeee') % /t |
| 533 |
(\regMatchResult '(\w+)\s+is\s+(\w+)\.' 'He is crazy.') |
| 534 |
% ('He is crazy.' 'He' 'crazy') |
| 535 |
``` |
| 536 |
|
| 537 |
`\regExtract` : Extraction. |
| 538 |
**************************** |
| 539 |
|
| 540 |
``` |
| 541 |
(\regExtract '\w+' 'hello regex world') % ('hello' 'regex' 'world') |
| 542 |
``` |
| 543 |
|
| 544 |
`\regReplaceAll`, `\regReplaceOnce` : Replace. |
| 545 |
********************************************** |
| 546 |
|
| 547 |
``` |
| 548 |
(\regReplaceAll '(\w+?)to(\w+?)' '$\1\c{to}\2$' 'AtoB BtoC') % '$A\to B$ $B\to C$' |
| 549 |
(\regReplaceOnce 'foo+' '[\0]' 'foooofooooooo') % '[foooo]fooooooo' |
| 550 |
``` |
| 551 |
|
| 552 |
`\regSplit` : Split. |
| 553 |
*********************** |
| 554 |
|
| 555 |
``` |
| 556 |
(\regSplit '/' '/path/to/hogehoge') % ('' 'path' 'to' 'hogehoge') |
| 557 |
``` |
| 558 |
|
| 559 |
|
| 560 |
## TODOs ## |
| 561 |
|
| 562 |
* Writing user manual |
| 563 |
* Add functions and special forms |
| 564 |
|
| 565 |
## CHANGELOG ## |
| 566 |
|
| 567 |
Dec. 18, 2024 : 2.1 |
| 568 |
************************* |
| 569 |
|
| 570 |
* Add \stringTOint function, thanks for @homologic |
| 571 |
|
| 572 |
Oct. 25, 2015 : 2.0 |
| 573 |
************************* |
| 574 |
|
| 575 |
* Add GC |
| 576 |
* Refine some special forms like \define |
| 577 |
* Add checking #args for some functions. |
| 578 |
* Add thin wrapper of l3regex |
| 579 |
|
| 580 |
Jul. 12, 2014 : 1.3 |
| 581 |
************************* |
| 582 |
|
| 583 |
* Add one shot continuations. |
| 584 |
* Add some arithmetical functions. |
| 585 |
* Debug environment. |
| 586 |
|
| 587 |
Jan. 03, 2014 : 1.2 |
| 588 |
************************** |
| 589 |
|
| 590 |
* Added TUG2013's examples. |
| 591 |
* Improved the performance. |
| 592 |
|
| 593 |
Aug. 10, 2013 : 1.1 |
| 594 |
************************** |
| 595 |
|
| 596 |
* Added \letrec and \expand. |
| 597 |
* debug |
| 598 |
|
| 599 |
Mar. 04, 2013 : 1.0 |
| 600 |
************************** |
| 601 |
|
| 602 |
## Licence ## |
| 603 |
|
| 604 |
Modified BSD (see LICENCE) |
| 605 |
|
| 606 |
************************************************ |
| 607 |
HAKUTA Shizuya <[email protected]> |
| 608 |
|
| 609 |
https://github.com/hak7a3/lisp-on-tex |