summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lisp-on-tex
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/lisp-on-tex
Initial commit
Diffstat (limited to 'macros/latex/contrib/lisp-on-tex')
-rw-r--r--macros/latex/contrib/lisp-on-tex/LICENSE28
-rw-r--r--macros/latex/contrib/lisp-on-tex/README.md597
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/div2.pdfbin0 -> 11875 bytes
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/div2.tex30
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/fact.pdfbin0 -> 36632 bytes
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/fact.tex30
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.pdfbin0 -> 32295 bytes
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.tex55
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/nqueen.pdfbin0 -> 12971 bytes
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/nqueen.tex47
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/repl.tex12
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/rocket.pdfbin0 -> 30975 bytes
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/rocket.tex26
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/showfont.pdfbin0 -> 13933 bytes
-rw-r--r--macros/latex/contrib/lisp-on-tex/examples/showfont.tex32
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-arith.sty165
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-gc.sty197
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-latexutil.sty17
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-mod-fpnum.sty95
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-mod-l3regex.sty99
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-on-tex.sty918
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-prim.sty176
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-read.sty122
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-simple-alloc.sty32
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-string.sty76
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-util.sty64
-rw-r--r--macros/latex/contrib/lisp-on-tex/tug2013/slide.pdfbin0 -> 247935 bytes
27 files changed, 2818 insertions, 0 deletions
diff --git a/macros/latex/contrib/lisp-on-tex/LICENSE b/macros/latex/contrib/lisp-on-tex/LICENSE
new file mode 100644
index 0000000000..e9b8a4b284
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/LICENSE
@@ -0,0 +1,28 @@
+LISP on TeX
+
+lisp-on-tex.sty, lisp-arith.sty,
+lisp-latexutil.sty, lisp-prim.sty,
+lisp-read.sty, lisp-string.sty,
+lisp-util.sty, lisp-mod-fpnum.sty,
+fact.tex, rocket.tex, fpnummodule-mandelbrot.tex
+
+Copyright (c) 2012 2013 2014 2015 HAKUTA Shizuya
+ All rights reserved.
+
+
+Redistribution and use in source and binary forms,
+with or without modification, are permitted provided
+that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+--
+ Copyright 1993 1994 1995 1996 1997 1998 1999
+ The LaTeX3 Project and any individual authors listed elsewhere
+ in this file.
diff --git a/macros/latex/contrib/lisp-on-tex/README.md b/macros/latex/contrib/lisp-on-tex/README.md
new file mode 100644
index 0000000000..045f98aaed
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/README.md
@@ -0,0 +1,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/
diff --git a/macros/latex/contrib/lisp-on-tex/examples/div2.pdf b/macros/latex/contrib/lisp-on-tex/examples/div2.pdf
new file mode 100644
index 0000000000..6cb01f55aa
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/div2.pdf
Binary files differ
diff --git a/macros/latex/contrib/lisp-on-tex/examples/div2.tex b/macros/latex/contrib/lisp-on-tex/examples/div2.tex
new file mode 100644
index 0000000000..ae2cf824ee
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/div2.tex
@@ -0,0 +1,30 @@
+\documentclass{article}
+%\usepackage{lisp-on-tex}
+\usepackage[markGC]{lisp-on-tex}
+\lispinterp {
+ (\define (\create \n) (\createAcc \n ()))
+ (\define (\createAcc \n \acc)
+ (\lispif (\= \n :0) \acc (\createAcc (\- \n :1) (\cons () \acc))))
+ (\define \ll (\create :200))
+ (\define (\divII \l)
+ (\lispif (\= \l ()) () (\cons (\car \l) (\divII (\cdr (\cdr \l))))))
+ (\define (\test \l) (\testAcc \l :500)) % original is 300
+ (\define (\testAcc \l \i)
+ (\lispif (\= \i :0) ()
+ (\begin
+ (\lispif (\= (\length (\divII \l)) :100)
+ () (\texprint 'Hmm'))
+ (\lispif (\= (\length (\divII \l)) :100)
+ () (\message 'Hmm'))
+ (\lispif (\= (\length (\divII \l)) :100)
+ () (\message 'Hmm'))
+ (\lispif (\= (\length (\divII \l)) :100)
+ () (\message 'Hmm'))
+ (\texprint \i)
+ (\texprint ' ')
+ (\testAcc \l (\- \i :1)))))
+}
+
+\begin{document}
+ \lispinterp{(\test \ll)}
+\end{document} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/examples/fact.pdf b/macros/latex/contrib/lisp-on-tex/examples/fact.pdf
new file mode 100644
index 0000000000..5bb156ab39
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/fact.pdf
Binary files differ
diff --git a/macros/latex/contrib/lisp-on-tex/examples/fact.tex b/macros/latex/contrib/lisp-on-tex/examples/fact.tex
new file mode 100644
index 0000000000..39fb7eeb11
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/fact.tex
@@ -0,0 +1,30 @@
+\documentclass{article}
+\usepackage{lisp-on-tex}
+\lispinterp{%
+ (\define \sq (\lambda (\n) (\* \n \n)))
+ (\define \fact (\lambda (\n) (\lispif (\= \n :0) :1 (\* \n (\fact (\- \n :1))))))
+ }
+\newcommand\sq[1]{\lispinterp{(\texprint (\sq :#1))}}
+\newcommand\fact[1]{\lispinterp{(\texprint (\fact :#1))}}
+
+\begin{document}
+\section{Factorials and Squares}
+ \begin{center}
+ \begin{tabular}{r||rr}\hline\hline
+ $n$ & $n!$ & $n^2$ \\
+ \hline
+ 1 & \fact{1} & \sq{1} \\
+ 2 & \fact{2} & \sq{2} \\
+ 3 & \fact{3} & \sq{3} \\
+ 4 & \fact{4} & \sq{4} \\
+ 5 & \fact{5} & \sq{5} \\
+ 6 & \fact{6} & \sq{6} \\
+ 7 & \fact{7} & \sq{7} \\
+ 8 & \fact{8} & \sq{8} \\
+ 9 & \fact{9} & \sq{9} \\
+ 10 & \fact{10} & \sq{10} \\
+ 11 & \fact{11} & \sq{11} \\
+ \hline
+ \end{tabular}
+ \end{center}
+\end{document} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.pdf b/macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.pdf
new file mode 100644
index 0000000000..b40e5f7c9b
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.pdf
Binary files differ
diff --git a/macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.tex b/macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.tex
new file mode 100644
index 0000000000..f0cf68798b
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/fpnummodule-mandelbrot.tex
@@ -0,0 +1,55 @@
+\documentclass{article}
+\usepackage[pdftex,a3paper,margin=1pt,landscape]{geometry}
+\newcount\mlength
+\newcount\cstate
+\newdimen\mandelunit
+\mandelunit=0.5pt
+
+\def\w{%
+ \ifnum\cstate=1 \global\advance\mlength1
+ \else \vrule width \mlength\mandelunit height \mandelunit depth 0pt \global\mlength1 \fi
+ \global\cstate1}
+\def\b{%
+ \ifnum\cstate=-1 \global\advance\mlength1
+ \else \hspace*{\mlength\mandelunit}\global\mlength1 \fi
+ \global\cstate-1}
+\def\r{%
+ \ifnum\cstate=1\hspace*{\mlength\mandelunit}\else\vrule width \mlength\mandelunit height \mandelunit depth 0pt \fi
+ \global\cstate0 \global\mlength0}
+
+\usepackage{lisp-on-tex}
+\usepackage{lisp-mod-fpnum}
+\lispinterp{%
+ (\define \maxloop :20)
+ (\define \scale +{fpnum::0.002})
+ (\define \isMandell
+ (\lambda (\a \b \k \x \y)
+ (\lispif (\< \maxloop \k) /t
+ (\lispif (\fplt +{fpnum::4.0} (\fpplus (\fpmul \x \x) (\fpmul \y \y)))
+ /f
+ (\isMandell \a \b (\+ \k :1)
+ (\fpplus \a (\fpmul \x \x) (\fpminus (\fpmul \y \y)))
+ (\fpplus \b (\fpmul +{fpnum::2.0} \x \y)))))))
+ (\define \drawMandell (\lambda (\a \b)
+ (\begin
+ (\lispif (\isMandell \a \b :0 +{fpnum::0} +{fpnum::0})
+ (\texprint '\b') (\texprint '\w'))
+ (\immediatewrite))))
+ (\define \loopMandell (\lambda (\a \b)
+ (\lispif (\fplt \b +{fpnum::-1.0}) ()
+ (\begin
+ (\drawMandell \a \b)
+ (\lispif (\fplt +{fpnum::0.5} \a)
+ (\begin
+ (\texprint '\r\\')
+ (\immediatewrite)
+ (\loopMandell +{fpnum::-1.5} (\fpminus \b \scale)))
+ (\loopMandell (\fpplus \a \scale) \b))))))
+}
+
+\begin{document}
+ \thispagestyle{empty}
+ \noindent
+ \leavevmode\baselineskip=\mandelunit
+ \lispinterp{(\loopMandell +{fpnum::-1.5} +{fpnum::1.0})}
+\end{document} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/examples/nqueen.pdf b/macros/latex/contrib/lisp-on-tex/examples/nqueen.pdf
new file mode 100644
index 0000000000..c605d4212c
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/nqueen.pdf
Binary files differ
diff --git a/macros/latex/contrib/lisp-on-tex/examples/nqueen.tex b/macros/latex/contrib/lisp-on-tex/examples/nqueen.tex
new file mode 100644
index 0000000000..9de2d28402
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/nqueen.tex
@@ -0,0 +1,47 @@
+\documentclass{article}
+\usepackage{lisp-on-tex}
+\makeatletter
+\lispinterp{
+ (\define \nqueen (\lambda (\n)
+ (\callOCC (\lambda (\c) (\nqinner \n () \c)))))
+ (\define \nqinner (\lambda (\n \acc \c)
+ (\lispif (\= (\length \acc) \n)
+ (\lispif (\nqcheck \acc) (\c \acc) ())
+ (\nqinnernext \n \acc \c :1))))
+ (\define \nqinnernext (\lambda (\n \acc \c \k)
+ (\lispif (\< \n \k) ()
+ (\begin
+ (\nqinner \n (\cons \k \acc) \c)
+ (\nqinnernext \n \acc \c (\+ \k :1))))))
+ (\define \seq (\lambda (\n)
+ (\lispif (\= \n :1) (\quote (:1)) (\cons \n (\seq (\- \n :1))))))
+ (\define \nqcheck (\lambda (\lst)
+ (\and (\uniqueQ \lst) (\nqcheckdiag \lst))))
+ (\define \nqcheckdiag (\lambda (\lst) (\nqcheckdiaginner \lst :1)))
+ (\define \nqcheckdiaginner (\lambda (\lst \k)
+ (\lispif (\< (\length \lst) \k) /t
+ (\lispif (\nqcheckdiagK \lst \k :1)
+ (\nqcheckdiaginner \lst (\+ \k :1)) /f))))
+ (\define \nqcheckdiagK (\lambda (\lst \k \n)
+ (\lispif (\> \n (\length \lst)) /t
+ (\let ((\t (\nth \lst (\- \k :1)))
+ (\tt (\nth \lst (\- \n :1)))
+ (\d (\- \n \k)))
+ (\lispif (\and
+ (\not (\= \n \k))
+ (\or (\= \t (\+ \tt \d)) (\= \t (\- \tt \d)) ))
+ /f (\nqcheckdiagK \lst \k (\+ \n :1)))))))
+ (\define \uniqueQ (\lambda (\list)
+ (\lispif (\= \list ()) /t
+ (\and (\not (\existQ (\car \list) (\cdr \list))) (\uniqueQ (\cdr \list))))))
+ (\define \existQ (\lambda (\v \list)
+ (\lispif (\= \list ()) /f
+ (\or (\= \v (\car \list)) (\existQ \v (\cdr \list))))))
+ (\define \reverse (\lambda (\l)
+ (\lispif (\= \l ()) () (\append (\reverse (\cdr \l)) (\car \l)))))
+ (\define \append (\lambda (\l \v)
+ (\lispif (\= \l ()) (\cons \v ()) (\cons (\car \l) (\append (\cdr \l) \v)))))
+}
+\begin{document}
+hoge\lispinterp{(\print (\nqueen :5))}fuga
+\end{document} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/examples/repl.tex b/macros/latex/contrib/lisp-on-tex/examples/repl.tex
new file mode 100644
index 0000000000..9a4d1a9bdb
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/repl.tex
@@ -0,0 +1,12 @@
+\documentclass{article}
+\usepackage{lisp-on-tex}
+\lispinterp{%
+ (\define \repl (\lambda ()
+ (\begin
+ (\stdprint (\eval (\read)))
+ (\repl))))
+}
+\begin{document}
+ \typeout{LISP on TeX interpretor mode}
+ \lispinterp{(\repl)}
+\end{document} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/examples/rocket.pdf b/macros/latex/contrib/lisp-on-tex/examples/rocket.pdf
new file mode 100644
index 0000000000..bfff99d340
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/rocket.pdf
Binary files differ
diff --git a/macros/latex/contrib/lisp-on-tex/examples/rocket.tex b/macros/latex/contrib/lisp-on-tex/examples/rocket.tex
new file mode 100644
index 0000000000..c4ed29648f
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/rocket.tex
@@ -0,0 +1,26 @@
+\documentclass{article}
+\usepackage{lisp-on-tex}
+\lispinterp{%
+%rocket body
+(\define \rocket
+ (\letM ((\count :11))
+ (\lambda ()
+ (\begin
+ (\setB \count (\- \count :1))
+ (\lispif (\< \count :0)
+ (\texprint '{\small fired...}\\')
+ (\lispif (\= \count :0)
+ (\texprint '{\Large FIRE!}\\')
+ (\texprint (\concat 'count ' (\intTOstring \count) '...\\'))))))))
+(\define \MAX :15)
+%LOOP function
+(\define \loop
+ (\lambda (\n)
+ (\lispif (\= \n \MAX)
+ ()
+ (\begin (\rocket) (\loop (\+ \n :1))))))
+}
+\begin{document}
+\noindent\lispinterp{(\loop :0)}
+Test done.
+\end{document} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/examples/showfont.pdf b/macros/latex/contrib/lisp-on-tex/examples/showfont.pdf
new file mode 100644
index 0000000000..13ef7fce56
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/showfont.pdf
Binary files differ
diff --git a/macros/latex/contrib/lisp-on-tex/examples/showfont.tex b/macros/latex/contrib/lisp-on-tex/examples/showfont.tex
new file mode 100644
index 0000000000..31200d9796
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/examples/showfont.tex
@@ -0,0 +1,32 @@
+\documentclass{article}
+\usepackage{lisp-on-tex}
+\newcount\hoge \hoge\endlinechar
+\endlinechar=-1
+\read16 to\tmp
+\endlinechar=\hoge
+\global\font\testfont="\tmp"
+\lispinterp{
+ (\define \foldl (\lambda (\func \init \list)
+ (\lispif (\= \list ())
+ \init
+ (\foldl \func (\func \init (\car \list)) (\cdr \list)))))
+ (\define \createtoken (\lambda (\code)
+ (\concat '\iffontchar\testfont' (\concat (\intTOstring \code) 't\else f\fi' ))))
+ (\define \MAX :"30000)}
+\begin{document}
+\noindent
+\lispinterp{
+ (\letrec ((\count :0)
+ (\loop (\lambda ()
+ (\lispif (\< \MAX \count) ()
+ (\begin
+ (\lispif (\= 't' (\expand (\createtoken \count)))
+ (\texprint (\group (\concat '\testfont\char' (\intTOstring \count))))
+ ())
+ (\texprint '\hskip0pt plus1pt')
+ (\immediatewrite)
+ (\setB \count (\+ \count :1))
+ (\loop))))))
+ (\loop))
+ }
+\end{document} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-arith.sty b/macros/latex/contrib/lisp-on-tex/lisp-arith.sty
new file mode 100644
index 0000000000..1e4f622dbe
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-arith.sty
@@ -0,0 +1,165 @@
+\newcount\@temp@inti
+\newcount\@temp@intii
+
+\@lisp@env@add@global\+{\@tlabel@func{\@lisp@func@check@args{*}{\@temp@inti0\relax\@arith@plus}}}
+\def\@arith@plus#1#2#3{%
+ \ifx#2\relax
+ \let\@next@plus\@@arith@pm@fin
+ \else\ifx#2\@tlabel@int
+ \advance\@temp@inti#3\relax
+ \let\@next@plus\@arith@plus
+ \else
+ \let\@next@plus\@arith@type@error
+ \fi\fi\@next@plus#1}
+
+\@lisp@env@add@global\*{\@tlabel@func{\@lisp@func@check@args{*}{\@temp@inti1\relax\@arith@mul}}}
+\def\@arith@mul#1#2#3{%
+ \ifx#2\relax
+ \let\@next@mul\@@arith@pm@fin
+ \else\ifx#2\@tlabel@int
+ \multiply\@temp@inti#3\relax
+ \let\@next@mul\@arith@mul
+ \else
+ \let\@next@mul\@arith@type@error
+ \fi\fi\@next@mul#1}
+
+\def\@@arith@pm@fin#1{\expandafter\gdef
+ \expandafter#1\expandafter{%
+ \expandafter\@tlabel@int\expandafter{\the\@temp@inti}}}
+
+\def\@arith@type@error#1#2\relax{% notice #2 is dust
+ \gdef#1{\@tlabel@exception{{-1}%
+ {\@tlabel@string{arithmetical functions takes integer only.}}}}}
+
+\@lisp@env@add@global\-{\@tlabel@func{\@lisp@func@check@args{+}{\@arith@minus}}}
+%TODO: error handling of 1st argument
+\def\@arith@minus#1#2#3#4#5{%
+ \@temp@inti#3\relax
+ \ifx#4\relax
+ \multiply\@temp@inti-1\relax
+ \let\@next@minus\@@arith@pm@fin
+ \else\ifx#4\@tlabel@int
+ \let\@next@minus\@@arith@minus
+ \else
+ \let\@next@minus\@arith@type@error
+ \fi\fi\@next@minus#1#4{#5}}
+
+\def\@@arith@minus#1#2#3{%
+ \ifx#2\relax
+ \let\@next@minus\@@arith@pm@fin
+ \else\ifx#2\@tlabel@int
+ \@temp@intii#3\relax % #3 < 0
+ \advance\@temp@inti-\@temp@intii\relax
+ \let\@next@minus\@@arith@minus
+ \else
+ \let\@next@minus\@arith@type@error
+ \fi\fi\@next@minus#1}
+
+
+\@lisp@env@add@global\/{\@tlabel@func{\@lisp@func@check@args{+}{\@arith@div}}}
+%TODO: error handling of 1st argument
+\def\@arith@div#1#2#3#4#5{%
+ \@temp@inti#3\relax
+ \ifx#4\relax
+ \@temp@intii\@temp@inti\relax
+ \@temp@inti1\relax
+ \divide\@temp@inti\@temp@intii\relax
+ \let\@next@div\@@arith@pm@fin
+ \else\ifx#4\@tlabel@int
+ \let\@next@div\@@arith@div
+ \else
+ \let\@next@div\@arith@type@error
+ \fi\fi\@next@div#1#4{#5}}
+
+\def\@@arith@div#1#2#3{%
+ \ifx#2\relax
+ \let\@next@div\@@arith@pm@fin
+ \else\ifx#2\@tlabel@int
+ \divide\@temp@inti#3\relax
+ \let\@next@div\@@arith@div
+ \else
+ \let\@next@div\@arith@type@error
+ \fi\fi\@next@div#1}
+
+\@lisp@env@add@global\mod{\@tlabel@func{\@lisp@func@check@args{2}{\@arith@mod}}}
+%TODO: error handling
+\def\@arith@mod#1\@tlabel@int#2\@tlabel@int#3{%
+ \@temp@intii#2\relax
+ \divide\@temp@intii#3\relax
+ \multiply\@temp@intii#3\relax
+ \@temp@inti#2\relax
+ \advance\@temp@inti-\@temp@intii
+ \@@arith@pm@fin#1}
+
+\@lisp@env@add@global\<{\@tlabel@func{\@lisp@func@check@args{2}{\@arith@lt}}}
+%TODO: error handling
+\def\@arith@lt#1\@tlabel@int#2\@tlabel@int#3{%
+ \ifnum#2<#3 \gdef#1{\@tlabel@bool{t}}\else\gdef#1{\@tlabel@bool{f}}\fi}
+
+\@lisp@env@add@global\>{\@tlabel@func{\@lisp@func@check@args{2}{\@arith@gt}}}
+%TODO: error handling
+\def\@arith@gt#1\@tlabel@int#2\@tlabel@int#3{%
+ \ifnum#2>#3 \gdef#1{\@tlabel@bool{t}}\else\gdef#1{\@tlabel@bool{f}}\fi}
+
+\@lisp@env@add@global\leq{\@tlabel@func{\@lisp@func@check@args{2}{\@arith@leq}}}
+%TODO: error handling
+\def\@arith@leq#1\@tlabel@int#2\@tlabel@int#3{%
+ \ifnum#2<#3 \gdef#1{\@tlabel@bool{t}}\else\gdef#1{\@tlabel@bool{f}}\fi
+ \ifnum#2=#3 \gdef#1{\@tlabel@bool{t}}\fi}
+
+\@lisp@env@add@global\geq{\@tlabel@func{\@lisp@func@check@args{2}{\@arith@geq}}}
+%TODO: error handling
+\def\@arith@geq#1\@tlabel@int#2\@tlabel@int#3{%
+ \ifnum#2>#3 \gdef#1{\@tlabel@bool{t}}\else\gdef#1{\@tlabel@bool{f}}\fi
+ \ifnum#2=#3 \gdef#1{\@tlabel@bool{t}}\fi}
+
+\@lisp@env@add@global\isZeroQ{\@tlabel@func{\@lisp@func@check@args{1}{\@arith@is@zero}}}
+%TODO: error handling
+\def\@arith@is@zero#1\@tlabel@int#2{%
+ \ifnum#2=0 \gdef#1{\@tlabel@bool{t}}\else\gdef#1{\@tlabel@bool{f}}\fi}
+
+\@lisp@env@add@global\positiveQ{\@tlabel@func{\@lisp@func@check@args{1}{\@arith@is@positive}}}
+%TODO: error handling
+\def\@arith@is@positive#1\@tlabel@int#2{%
+ \ifnum#2>0 \gdef#1{\@tlabel@bool{t}}\else\gdef#1{\@tlabel@bool{f}}\fi}
+
+\@lisp@env@add@global\negativeQ{\@tlabel@func{\@lisp@func@check@args{1}{\@arith@is@negative}}}
+%TODO: error handling
+\def\@arith@is@negative#1\@tlabel@int#2{%
+ \ifnum#2<0 \gdef#1{\@tlabel@bool{t}}\else\gdef#1{\@tlabel@bool{f}}\fi}
+
+\@lisp@env@add@global\max{\@tlabel@func{\@lisp@func@check@args{+}{\@arith@max}}}
+\def\@arith@max#1#2#3{%
+ \ifx#2\@tlabel@int
+ \@temp@inti#3\relax
+ \let\@next@max\@@arith@max
+ \else
+ \let\@next@max\@arith@type@error
+ \fi\@next@max#1}
+\def\@@arith@max#1#2#3{%
+ \ifx#2\relax
+ \let\@next@max\@@arith@pm@fin
+ \else\ifx#2\@tlabel@int
+ \ifnum#3>\@temp@inti\@temp@inti#3\relax\fi
+ \let\@next@max\@@arith@max
+ \else
+ \let\@next@max\@arith@type@error
+ \fi\fi\@next@max#1}
+
+\@lisp@env@add@global\min{\@tlabel@func{\@lisp@func@check@args{+}{\@arith@min}}}
+\def\@arith@min#1#2#3{%
+ \ifx#2\@tlabel@int
+ \@temp@inti#3\relax
+ \let\@next@min\@@arith@min
+ \else
+ \let\@next@min\@arith@type@error
+ \fi\@next@min#1}
+\def\@@arith@min#1#2#3{%
+ \ifx#2\relax
+ \let\@next@min\@@arith@pm@fin
+ \else\ifx#2\@tlabel@int
+ \ifnum#3<\@temp@inti\@temp@inti#3\relax\fi
+ \let\@next@min\@@arith@min
+ \else
+ \let\@next@min\@arith@type@error
+ \fi\fi\@next@min#1}
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-gc.sty b/macros/latex/contrib/lisp-on-tex/lisp-gc.sty
new file mode 100644
index 0000000000..672593c037
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-gc.sty
@@ -0,0 +1,197 @@
+\RequirePackage{xkeyval}
+
+\newcount\@lisp@gc@count
+\newcount\@lisp@gc@perform@result
+\def\@lisp@gc@alloc@count@max{32768} % memory limit
+\def\@lisp@gc@alloc@local{} % objects on stack
+\def\@lisp@gc@usedenv{} % used local environment
+\def\@lisp@gc@alloc@raw{} % used memory on local
+\def\@lisp@gc@alloc@pinning{} % pinned objects
+\def\@lisp@gc@mark@bitvector{} % mark bitmap
+\def\@lisp@gc@mark@bitvector@{} % mark bitmap (default)
+
+% execute GC option
+\DeclareOptionX{heapsize}{\def\@lisp@gc@alloc@count@max{#1}}
+\ProcessOptionsX
+
+% gc initializer (called by lisp-on-tex.sty)
+\def\@lisp@gc@init{%
+ \@lisp@gc@count=0\relax
+ \@lisp@gc@perform@result=\@lisp@gc@alloc@count@max\relax
+ % create mark bits
+ % clear all mark bits
+ \@tempcnta0
+ \@whilenum\@tempcnta<\@lisp@gc@alloc@count@max\do{%
+ \@lisp@gc@off@markbit{\the\@tempcnta}%
+ \advance\@tempcnta1}%
+ % set default value to each memory
+ \@tempcnta0
+ \@whilenum\@tempcnta<\@lisp@gc@alloc@count@max\do{%
+ \expandafter\gdef
+ \csname @lisp@gc@memory@\the\@tempcnta\endcsname
+ {\relax\relax}% initialize
+ \advance\@tempcnta1}}
+
+% allocator
+% The allocator define #1 to allocated the control sequence
+\def\@lisp@gc@alloc#1{%
+ % if memory is full, do gc
+ \ifnum\@lisp@gc@count<\@lisp@gc@alloc@count@max\else
+ \@lisp@gc@perform
+ \fi
+ % allocation
+ \def#1{}%
+ \@lisp@gc@alloc@@#1}
+\def\@lisp@gc@alloc@@#1{%
+ \csname if@lisp@gc@memory@\the\@lisp@gc@count\endcsname
+ \else % unused memory is found
+ % allocate
+ \expandafter\def\expandafter#1%
+ \expandafter{\csname @lisp@gc@memory@\the\@lisp@gc@count\endcsname}%
+ % clear memory (for marking)
+ \expandafter\gdef
+ \csname @lisp@gc@memory@\the\@lisp@gc@count\endcsname{\relax\relax}%
+ \fi
+ \global\advance\@lisp@gc@count1
+ \let\@@next\@gobble
+ \ifx#1\@empty
+ \ifnum\@lisp@gc@count<\@lisp@gc@alloc@count@max
+ \let\@@next\@lisp@gc@alloc@@
+ \else
+ \def\@@next{%
+ \@lisp@gc@perform
+ \ifnum\@lisp@gc@perform@result=0\relax
+ \expandafter\@gobble % empty
+ \else
+ \expandafter\@lisp@gc@alloc@@
+ \fi}%
+ \fi
+ \fi
+ \@@next#1}
+
+
+% GC main routine. The routine is called by the allocator.
+% If you want to call GC manually, you can do it.
+\def\@lisp@gc@perform{%
+ \message{LISP on TeX: now GCing...^^J}%
+ % reset result
+ \global\@lisp@gc@perform@result\@lisp@gc@alloc@count@max\relax
+ % clear all mark bits
+ \@tempcnta0
+ \@whilenum\@tempcnta<\@lisp@gc@count\do{%
+ \@lisp@gc@off@markbit{\the\@tempcnta}%
+ \advance\@tempcnta1}%
+ % start marking mode
+ \begingroup
+ % define all typelabels to mark mode
+ \@lisp@gc@make@tlabel@mark@mode
+ % init queue and enque root set
+ %% raw memory
+ \expandafter\def\expandafter\@lisp@gc@mark@Q\expandafter{\@lisp@gc@alloc@raw}%
+ %% local env
+ \expandafter\@lisp@gc@mark@env\@lisp@gc@usedenv\@lisp@unused\relax
+ %% global env
+ \expandafter\@lisp@gc@mark@env\@lisp@globalenv\@lisp@unused\relax
+ %% pinned objects
+ \@lisp@gc@alloc@pinning
+ %% stack
+ \@lisp@gc@alloc@local
+ % marking start
+ \@lisp@gc@foreachQ\@lisp@gc@mark@Q\@lisp@gc@mark
+ % end marking mode
+ \endgroup
+ % reset counter
+ \global\@lisp@gc@count0
+ % show GC result
+ \message{LISP on TeX: GC done (free \the\@lisp@gc@perform@result)^^J}}
+\def\@lisp@gc@make@tlabel@mark@mode{%
+ \@for\@lisp@on@tex@type:=\@defined@lisp@on@tex\do{%
+ \expandafter\let
+ \csname @tlabel@\@lisp@on@tex@type\endcsname\@gobble}%
+ \def\@tlabel@cons##1{\@lisp@gc@mark@cons##1}%
+ \def\@tlabel@mutable##1{\@lisp@gc@enQ\@lisp@gc@mark@Q{##1}}%
+ \def\@tlabel@closure##1{\@lisp@gc@mark@closure##1}%
+ \def\@tlabel@macro##1{\@lisp@gc@mark@closure##1}%
+ \def\@tlabel@exception##1{\@lisp@gc@mark@exception##1}%
+ \@for\@lisp@on@tex@type:=\@defined@additional@type\do{%
+ \expandafter\@lisp@gc@create@markmode@additional
+ \csname @tlabel@\@lisp@on@tex@type\endcsname
+ \csname @lisp@gc@trace\@lisp@on@tex@type\endcsname}}
+\def\@lisp@unused{\@lisp@unused@}
+\def\@lisp@gc@mark@cons#1#2{%
+ \@lisp@gc@enQ\@lisp@gc@mark@Q{#1}%
+ \@lisp@gc@enQ\@lisp@gc@mark@Q{#2}}
+\def\@lisp@gc@mark@closure#1#2#3#4{%
+ \@lisp@gc@mark@env#2\@lisp@unused\relax
+ #3{#4}}
+\def\@lisp@gc@mark@exception#1#2{#2}%
+\def\@lisp@gc@create@markmode@additional#1{%
+ \expandafter\@lisp@gc@create@markmode@additional@\expandafter#1}
+\def\@lisp@gc@create@markmode@additional@#1#2{\let#1#2}
+% mark objects in a environment
+\def\@lisp@gc@mark@env#1#2{%
+ \ifx#1\@lisp@unused
+ \let\@@next\relax
+ \else
+ \def\@@next{#2\@lisp@gc@mark@env}%
+ \fi
+ \@@next}
+% global let for save and restore memory
+\def\@lisp@gc@global@let#1{\expandafter\@lisp@gc@global@let@\expandafter#1}
+\def\@lisp@gc@global@let@#1#2{\global\let#1#2}
+% queue
+\def\@lisp@gc@enQ#1#2{% #1 : queue, #2 value
+ \expandafter\def\expandafter#1\expandafter{#1#2}}
+\def\@lisp@gc@deQ#1#2{% #1 : queue, #2 cs
+ \ifx#1\@empty
+ \def#2{}%
+ \else
+ \expandafter\@lisp@gc@deQ@\expandafter#1\expandafter#2#1\relax
+ \fi}
+\def\@lisp@gc@deQ@#1#2#3#4\relax{%
+ \def#1{#4}\def#2{#3}}
+\def\@lisp@gc@foreachQ#1#2{% #1 : queue, #2 : token -> 'a
+ \@lisp@gc@deQ#1\@lisp@gc@temp@local
+ \ifx\@lisp@gc@temp@local\@empty
+ \let\@@next\relax
+ \else
+ \def\@@next{%
+ \expandafter#2\expandafter{\@lisp@gc@temp@local}%
+ \@lisp@gc@foreachQ#1#2}%
+ \fi\@@next}
+% marker
+\def\@lisp@gc@mark#1{\expandafter\@lisp@gc@mark@\string#1\relax}
+\expandafter\def\expandafter\@lisp@gc@mark@\string\@lisp@gc@memory@#1\relax{%
+ \csname if@lisp@gc@memory@#1\endcsname\else
+ % count down remain memory
+ \global\advance\@lisp@gc@perform@result-1
+ % turn mark bit on
+ \@lisp@gc@on@markbit{#1}%
+ % enqueue if we need
+ \csname @lisp@gc@memory@#1\endcsname
+ \fi}
+
+
+% pin a object on global.
+\def\@lisp@gc@pinning#1#2{%
+ \expandafter\gdef\expandafter\@lisp@gc@alloc@pinning
+ \expandafter{\@lisp@gc@alloc@pinning#1{#2}}}
+
+% pinning objects on local.
+\def\@lisp@gc@save@objects@local#1{%
+ \expandafter\def\expandafter\@lisp@gc@alloc@local
+ \expandafter{\@lisp@gc@alloc@local#1}}
+
+% pinning memories on local.
+\def\@lisp@gc@save@memory@local#1{%
+ \expandafter\def\expandafter\@lisp@gc@alloc@raw
+ \expandafter{\@lisp@gc@alloc@raw#1}}
+
+% pinning environmnt on local.
+\def\@lisp@gc@save@env@local#1{%
+ \expandafter\def\expandafter\@lisp@gc@usedenv
+ \expandafter{\@lisp@gc@usedenv#1}}
+
+% for mark bits
+\def\@lisp@gc@on@markbit#1{\expandafter\global\expandafter\let\csname if@lisp@gc@memory@#1\endcsname\iftrue}
+\def\@lisp@gc@off@markbit#1{\expandafter\global\expandafter\let\csname if@lisp@gc@memory@#1\endcsname\iffalse} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-latexutil.sty b/macros/latex/contrib/lisp-on-tex/lisp-latexutil.sty
new file mode 100644
index 0000000000..58c612bb08
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-latexutil.sty
@@ -0,0 +1,17 @@
+%% READ_LaTeX_Counter : string -> int
+%% (\readLaTeXCounter str) -> i
+%% where i = \the\c@str
+\@lisp@env@add@global\readLaTeXCounter{\@tlabel@func{\@lisp@func@check@args{1}{\@tex@read@latex@counter}}}
+%TODO: error handling
+\def\@tex@read@latex@counter#1#2#3{%
+ \expandafter\expandafter\expandafter\gdef
+ \expandafter\expandafter\expandafter#1%
+ \expandafter\expandafter\expandafter{%
+ \expandafter\expandafter\expandafter\@tlabel@int
+ \expandafter\expandafter\expandafter{%
+ \expandafter\the\csname c@#3\endcsname}}}
+
+\@lisp@env@add@global\message{\@tlabel@func{\@lisp@func@check@args{1}{\@tex@lisp@message}}}
+%TODO: error handling
+\def\@tex@lisp@message#1\@tlabel@string#2{%
+ \gdef#1{\@tlabel@nil{}}\message{#2}} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-mod-fpnum.sty b/macros/latex/contrib/lisp-on-tex/lisp-mod-fpnum.sty
new file mode 100644
index 0000000000..291b343baa
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-mod-fpnum.sty
@@ -0,0 +1,95 @@
+%%% module name :: fpnum
+
+% regist type and gc
+\ifx\@defined@additional@type\@empty
+ \def\@defined@additional@type{fpnum}%
+\else
+ \edef\@defined@additional@type{\@defined@additional@type,fpnum}%
+\fi
+\let\@lisp@gc@trace@fpnum\@gobble
+
+\def\@mod@read@fpnum#1#2{\gdef#1{\@tlabel@fpnum{#2}}}
+\def\@tlabel@fpnum{\@eval@fpnum\@apply@fpnum\@@apply@fpnum}
+
+\def\@eval@fpnum#1#2#3{\gdef#3{\@tlabel@fpnum{#1}}} % #1 contents, #2 environment, #3 register
+\def\@apply@fpnum{\errmessage{LISP on TeX [internal apply, module fpnum] cannot apply}}
+\def\@@apply@fpnum{\errmessage{LISP on TeX [function --- apply, module fpnum] cannot apply}}
+
+\newdimen\@tempdimeni
+\newdimen\@tempdimenii
+
+\@lisp@env@add@global\fpnumTOstring{\@tlabel@func{\@lisp@func@check@args{1}{\@mod@fpnum@tostring}}}
+%TODO: error handling
+\def\@mod@fpnum@tostring#1\@tlabel@fpnum#2{\gdef#1{\@tlabel@string{#2}}}
+
+\@lisp@env@add@global\fpplus{\@tlabel@func{\@lisp@func@check@args{*}{\@tempdimeni0pt\relax\@mod@fpnum@plus}}}
+%TODO: error handling
+\def\@mod@fpnum@plus#1#2#3{%
+ \ifx#2\relax
+ \let\@next@fpnum@plus\@@fin@fpnum@plus
+ \else\ifx#2\@tlabel@fpnum
+ \advance\@tempdimeni#3pt\relax
+ \let\@next@fpnum@plus\@mod@fpnum@plus
+ \else
+ \errmessage{LISP on TeX [fpnum --- plus]: Invalid argument}%
+ \fi\fi\@next@fpnum@plus#1}
+\def\@@fin@fpnum@plus#1{\xdef#1{\noexpand\@tlabel@fpnum{\strip@pt\@tempdimeni}}}
+
+\@lisp@env@add@global\fpmul{\@tlabel@func{\@lisp@func@check@args{*}{\@tempdimeni1pt\relax\@mod@fpnum@mul}}}
+%TODO: error handling
+\def\@mod@fpnum@mul#1#2#3{%
+ \ifx#2\relax
+ \let\@next@fpnum@mul\@@fin@fpnum@mul
+ \else\ifx#2\@tlabel@fpnum
+ \@tempdimeni#3\@tempdimeni
+ \let\@next@fpnum@mul\@mod@fpnum@mul
+ \else
+ \errmessage{LISP on TeX [fpnum --- mul]: Invalid argument}%
+ \fi\fi\@next@fpnum@mul#1}
+\def\@@fin@fpnum@mul#1{\xdef#1{\noexpand\@tlabel@fpnum{\strip@pt\@tempdimeni}}}
+
+\@lisp@env@add@global\fpminus{\@tlabel@func{\@lisp@func@check@args{+}{\@mod@fpnum@minus}}}
+\def\@mod@fpnum@minus#1#2#3#4#5{%
+ \ifx#4\relax
+ \ifx#2\@tlabel@fpnum
+ \@tempdimenii#3pt
+ \@tempdimeni0pt
+ \advance\@tempdimeni-\@tempdimenii
+ \let\@next@fpnum@minus\@@fin@fpnum@minus
+ \else
+ \errmessage{LISP on TeX [fpnum --- minus]: Invalid argument}%
+ \fi
+ \else\ifx#4\@tlabel@fpnum
+ \ifx#2\@tlabel@fpnum
+ \@tempdimeni#3pt
+ \let\@next@fpnum@minus\@@next@fpnum@minus
+ \else
+ \errmessage{LISP on TeX [fpnum --- minus]: Invalid argument}%
+ \fi
+ \else
+ \errmessage{LISP on TeX [fpnum --- minus]: Invalid argument}%
+ \fi\fi
+ \@next@fpnum@minus#1#4{#5}}
+\def\@@fin@fpnum@minus#1{\xdef#1{\noexpand\@tlabel@fpnum{\strip@pt\@tempdimeni}}}
+\def\@@next@fpnum@minus#1#2#3{%
+ \ifx#2\relax
+ \let\@next@fpnum@minus\@@fin@fpnum@minus
+ \else\ifx#2\@tlabel@fpnum
+ \@tempdimenii#3pt
+ \advance\@tempdimeni-\@tempdimenii
+ \let\@next@fpnum@minus\@@next@fpnum@minus
+ \else
+ \errmessage{LISP on TeX [fpnum --- minus]: Invalid argument}%
+ \fi\fi
+ \@next@fpnum@minus#1}
+
+\@lisp@env@add@global\fplt{\@tlabel@func{\@lisp@func@check@args{2}{\@mod@fpnum@lt}}}
+\def\@mod@fpnum@lt#1\@tlabel@fpnum#2\@tlabel@fpnum#3{%
+ \ifdim#2pt<#3pt
+ \gdef#1{\@tlabel@bool{t}}%
+ \else
+ \gdef#1{\@tlabel@bool{f}}%
+ \fi
+}
+
+
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-mod-l3regex.sty b/macros/latex/contrib/lisp-on-tex/lisp-mod-l3regex.sty
new file mode 100644
index 0000000000..03b278d386
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-mod-l3regex.sty
@@ -0,0 +1,99 @@
+% wrapper for l3regex
+\RequirePackage{l3regex}
+
+% \regex_match:nnTF
+\@lisp@env@add@global\regMatch{\@tlabel@func{\@lisp@func@check@args{2}{\@lisp@regex@match}}}
+\ExplSyntaxOn
+\cs_new:Npn \lot_regex_match:Nnn #1#2#3 {
+ \regex_match:nnTF{#2}{#3}{\gdef#1{\@tlabel@bool{t}}}{\gdef#1{\@tlabel@bool{f}}}
+}
+\cs_new_eq:NN \@lisp@regex@match@ \lot_regex_match:Nnn
+\ExplSyntaxOff
+%TODO: error handling
+\def\@lisp@regex@match#1\@tlabel@string#2\@tlabel@string#3{\@lisp@regex@match@#1{#2}{#3}}
+
+% \regex_extract_all
+\@lisp@env@add@global\regExtract{\@tlabel@func{\@lisp@func@check@args{2}{\@lisp@regex@extract}}}
+\ExplSyntaxOn
+\seq_new:N \lot_regex_extract_seq
+\cs_new:Npn \lot_regex_extract:nnN #1#2#3{
+ \regex_extract_all:nnN{#1}{#2}\lot_regex_extract_seq
+ \seq_map_variable:NNn \lot_regex_extract_seq \lot_regex_tmp
+ {\@lisp@regex@add@sep@result#3\lot_regex_tmp}}
+\cs_new_eq:NN \@lisp@regex@extract@ \lot_regex_extract:nnN
+\ExplSyntaxOff
+\def\@lisp@regex@add@sep@result#1#2{\expandafter\@lisp@regex@add@sep@result@\expandafter#1\expandafter{#2}}
+\def\@lisp@regex@add@sep@result@#1#2{\expandafter\def\expandafter#1\expandafter{#1{#2}}}
+\def\@lisp@regex@add@typelabel#1#2{%
+ \gdef#2{}%
+ \@tfor\@lisp@regex@tmp:=#1\do{%
+ \begingroup
+ \toks0\expandafter{\@lisp@regex@tmp}%
+ \toks1\expandafter{#2}%
+ \xdef#2{\the\toks1 \noexpand\@tlabel@string{\the\toks0}}%
+ \endgroup}}
+\def\@lisp@regex@extract#1\@tlabel@string#2\@tlabel@string#3{%
+ \def#1{}%
+ \@lisp@regex@extract@{#2}{#3}#1%
+ \expandafter\@lisp@regex@add@typelabel\expandafter{#1}#1%
+ \expandafter\@lisp@prim@list@pre\expandafter#1#1\relax\relax}
+
+
+% \regex_extract_once
+\@lisp@env@add@global\regMatchResult{\@tlabel@func{\@lisp@func@check@args{2}{\@lisp@regex@extract@once}}}
+\ExplSyntaxOn
+\seq_new:N \lot_regex_extract_once_seq
+\cs_new:Npn \lot_regex_extract_once:nnN #1#2#3{
+ \regex_extract_once:nnN{#1}{#2}\lot_regex_extract_once_seq
+ \seq_map_variable:NNn \lot_regex_extract_once_seq \lot_regex_tmp
+ {\@lisp@regex@add@sep@result#3\lot_regex_tmp}}
+\cs_new_eq:NN \@lisp@regex@extract@once@ \lot_regex_extract_once:nnN
+\ExplSyntaxOff
+\def\@lisp@regex@extract@once#1\@tlabel@string#2\@tlabel@string#3{%
+ \def#1{}%
+ \@lisp@regex@extract@once@{#2}{#3}#1%
+ \expandafter\@lisp@regex@add@typelabel\expandafter{#1}#1%
+ \expandafter\@lisp@prim@list@pre\expandafter#1#1\relax\relax}
+
+% \regex_split
+\@lisp@env@add@global\regSplit{\@tlabel@func{\@lisp@func@check@args{2}{\@lisp@regex@split}}}
+\ExplSyntaxOn
+\seq_new:N \lot_regex_split_seq
+\cs_new:Npn \lot_regex_split:nnN #1#2#3{
+ \regex_split:nnN{#1}{#2}\lot_regex_split_seq
+ \seq_map_variable:NNn \lot_regex_split_seq \lot_regex_tmp
+ {\@lisp@regex@add@sep@result#3\lot_regex_tmp}}
+\cs_new_eq:NN \@lisp@regex@split@ \lot_regex_split:nnN
+\ExplSyntaxOff
+\def\@lisp@regex@split#1\@tlabel@string#2\@tlabel@string#3{%
+ \def#1{}%
+ \@lisp@regex@split@{#2}{#3}#1%
+ \expandafter\@lisp@regex@add@typelabel\expandafter{#1}#1%
+ \expandafter\@lisp@prim@list@pre\expandafter#1#1\relax\relax}
+
+
+
+
+% \regex_replace_once
+\@lisp@env@add@global\regReplaceOnce{\@tlabel@func{\@lisp@func@check@args{3}{\@lisp@regex@replace@once}}}
+\ExplSyntaxOn
+\cs_new_eq:NN \@lisp@regex@replace@once@ \regex_replace_once:nnN
+\ExplSyntaxOff
+%TODO: error handling
+\def\@lisp@regex@replace@once#1\@tlabel@string#2\@tlabel@string#3\@tlabel@string#4{%
+ \def#1{#4}%
+ \@lisp@regex@replace@once@{#2}{#3}#1%
+ \expandafter\gdef\expandafter#1\expandafter{%
+ \expandafter\@tlabel@string\expandafter{#1}}}
+
+% \regex_replace_all
+\@lisp@env@add@global\regReplaceAll{\@tlabel@func{\@lisp@func@check@args{3}{\@lisp@regex@replace@all}}}
+\ExplSyntaxOn
+\cs_new_eq:NN \@lisp@regex@replace@all@ \regex_replace_all:nnN
+\ExplSyntaxOff
+%TODO: error handling
+\def\@lisp@regex@replace@all#1\@tlabel@string#2\@tlabel@string#3\@tlabel@string#4{%
+ \def#1{#4}%
+ \@lisp@regex@replace@all@{#2}{#3}#1%
+ \expandafter\gdef\expandafter#1\expandafter{%
+ \expandafter\@tlabel@string\expandafter{#1}}} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-on-tex.sty b/macros/latex/contrib/lisp-on-tex/lisp-on-tex.sty
new file mode 100644
index 0000000000..7b6b79688c
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-on-tex.sty
@@ -0,0 +1,918 @@
+%%
+%% This is file `lisp-on-tex.sty'.
+%%
+%% License: Modified BSD - see LICENSE file
+%%
+\NeedsTeXFormat{LaTeX2e}
+\ProvidesPackage{lisp-on-tex}[2015/06/22 v2.0]
+
+\RequirePackage{xkeyval}
+\newcount\@lisp@gc@style
+\def\@lisp@opt@gc{}
+% I use bad magic numbers...
+\DeclareOptionX{noGC}{\@lisp@gc@style0\relax}
+\DeclareOptionX{markGC}{\@lisp@gc@style1\relax}
+\DeclareOptionX{GCopt}[]{\def\@lisp@opt@gc{#1}}
+\ExecuteOptionsX{noGC}
+\ProcessOptionsX
+
+
+% lisp's global environment and some macros for it
+\def\@lisp@globalenv{}
+
+% Bind a variable (#1) to a value (#2) on global environment.
+% This macro also define [@lisp@env@#1] to #2.
+\def\@lisp@env@add@global#1#2{%
+ \begingroup
+ \toks0{#1{#2}}%
+ \toks2\expandafter{\@lisp@globalenv}%
+ \xdef\@lisp@globalenv{\the\toks0 \the\toks2}%
+ \endgroup
+ \expandafter\gdef\csname @lisp@env@\string#1\endcsname{#2}}
+% for backword compability...
+\def\addassoc#1#2#3{\@lisp@env@add@global#2{#3}}
+
+
+% Set a control sequence (#1) to the value of a variable (#2)
+% on a local environment (#3) and global environment.
+% If #2 is unbound, it set #1 to an exception object.
+\def\@lisp@env@get@value#1#2#3{%
+ \def\@lisp@env@tmpmacro##1#2##2##3\@lisp@env@tail{%
+ \gdef#1{##2}}%
+ % I use a sentinel \@lisp@env@get@value@dummy.
+ \@lisp@env@tmpmacro#1#3#2{\@lisp@env@get@value@dummy}%
+ \@lisp@env@tail
+ % #2 is not bound on #3, so I try to get the value
+ % from global environment.
+ \def\@@next#1#2{}%
+ \ifx#1\@lisp@env@get@value@dummy
+ \let\@@next\@lisp@env@get@value@global\fi
+ \@@next#1#2%
+ % not found
+ \def\@@next#1#2{}%
+ \ifx#1\relax\let\@@next\@lisp@env@get@value@notfound\fi
+ \@@next#1#2}
+\def\@lisp@env@get@value@dummy{\@lisp@env@get@value@dummy}
+\def\@lisp@env@get@value@global#1#2{%
+ \expandafter\global\expandafter\let\expandafter#1%
+ \csname @lisp@env@\string#2\endcsname}
+\def\@lisp@env@get@value@notfound#1#2{%
+ \gdef#1{\@tlabel@exception{{-1}{\@tlabel@string{unbound variable #2}}}}}
+
+% ENTRY point of LISP on TeX. It takes a LISP program (#1)
+% and evaluate the program.
+\def\lispinterp#1{\@lispread\@lisp@read@eval@loop#1\@end@lispread}
+\def\@lisp@read@eval@loop#1#2{%
+ \ifx#1\@tlabel@exception% read error
+ \let\@@next\@lisp@read@eval@loop@reader@error
+ \else
+ \let\@@next\@lisp@read@eval@loop@normal
+ \fi\@@next#1{#2}}
+\def\@lisp@read@eval@loop@normal#1#2{%
+ \def\@lisp@tmp@global@i{#1{#2}}%
+ \lispeval\@lisp@tmp@global@i\@lisp@tmp@global@i
+ \expandafter\@lisp@read@eval@loop@normal@\@lisp@tmp@global@i}
+\def\@lisp@read@eval@loop@normal@#1#2{%
+ \ifx#1\@tlabel@exception
+ \let\@@next\@lisp@read@eval@loop@catch@error
+ \else
+ \let\@@next\@lisp@read@eval@loop@normal@@
+ \fi
+ \@@next#1{#2}}
+\def\@lisp@read@eval@loop@normal@@#1#2{\@lispread\@lisp@read@eval@loop}
+
+\def\@lisp@read@eval@loop@catch@error\@tlabel@exception#1{%
+ \@lisp@read@eval@loop@print@err#1}
+\def\@lisp@read@eval@loop@print@err#1#2{%
+% #1 -> error handler id, #2 -> error message
+ \@lisp@read@eval@loop@print@err@#2}
+\def\@lisp@read@eval@loop@print@err@#1#2{%
+ \toks0{#2}\errmessage{LISP on TeX ERROR: \the\toks0}%
+ \@lisp@read@eval@loop@gobble@remain}
+\def\@lisp@read@eval@loop@gobble@remain#1\@end@lispread{}
+%FIXME: Now, the reader cannot tell consuming all input from imcomplete
+% input.
+\def\@lisp@read@eval@loop@reader@error\@tlabel@exception#1{}
+
+
+
+% CONS CAR CDR
+% get CAR of a CONS cell (#2{#3}) and set it to a control sequence
+% (#1). If the argument is not a CONS cell, it raise an exception.
+\def\@lisp@perform@car#1#2#3{%
+ \ifx#2\@tlabel@cons
+ \let\@@next\@lisp@perform@car@normal
+ \else
+ \let\@@next\@lisp@perform@car@type@error
+ \fi
+ \@@next#1{#3}}
+\def\@lisp@perform@car@normal#1#2{\@lisp@perform@car@normal@#1#2}
+\def\@lisp@perform@car@normal@#1#2#3{\global\let#1#2}
+\def\@lisp@perform@car@type@error#1#2{%
+ \gdef#1{\@tlabel@exception{{-1}%
+ {\@tlabel@string{Argument of car must be a CONS cell.}}}}}
+% get CDR of a CONS cell (#2{#3}) and set it to a control sequence
+% (#1). If the argument is not a CONS cell, it raise an exception.
+\def\@lisp@perform@cdr#1#2#3{%
+ \ifx#2\@tlabel@cons
+ \let\@@next\@lisp@perform@cdr@normal
+ \else
+ \let\@@next\@lisp@perform@cdr@type@error
+ \fi
+ \@@next#1{#3}}
+\def\@lisp@perform@cdr@normal#1#2{\@lisp@perform@cdr@normal@#1#2}
+\def\@lisp@perform@cdr@normal@#1#2#3{\global\let#1#3}
+\def\@lisp@perform@cdr@type@error#1#2{%
+ \gdef#1{\@tlabel@exception{{-1}%
+ {\@tlabel@string{Argument of cdr must be a CONS cell.}}}}}
+% create a CONS cell whose CAR is #2{#3} and CDR is #4{#5}.
+% It set a control sequence (#1) to the created cell.
+\def\@lisp@perform@cons#1#2#3#4#5{%
+ \@lisp@perform@cons@local#1#2{#3}#4{#5}%
+ \global\let#1#1}
+\def\@lisp@perform@cons@local#1#2#3#4#5{%
+ \@lisp@gc@save@objects@local{#2{#3}#4{#5}}% pin arguments
+ \@lisp@gc@alloc\@lisp@tmp@local@i
+ \expandafter\@lisp@gc@save@memory@local\expandafter{\@lisp@tmp@local@i}% pin CAR
+ \@lisp@gc@alloc\@lisp@tmp@local@ii
+ \expandafter\gdef\@lisp@tmp@local@i{#2{#3}}%
+ \expandafter\gdef\@lisp@tmp@local@ii{#4{#5}}%
+ \expandafter\@lisp@perform@cons@\expandafter#1%
+ \@lisp@tmp@local@i\@lisp@tmp@local@ii}
+\def\@lisp@perform@cons@#1#2#3{%
+ \expandafter\@lisp@perform@cons@@\expandafter#1%
+ \expandafter#2#3}
+\def\@lisp@perform@cons@@#1#2#3{\def#1{\@tlabel@cons{#2#3}}}
+
+
+% all primitive types of LISP on TeX.
+\def\@defined@datatype{%
+ symbol,string,int,bool,dimen,skip,cons,nil,%
+ func,closure,macro,envfunc,mutable,%
+ exception,continuation}
+% all special forms of LISP on TeX
+\def\@defined@specialform{%
+ lambda,quote,define,if,defmacro,begin,%
+ mdefine,setb,@let,@mlet,macroexpand,callocc}
+\edef\@defined@lisp@on@tex{\@defined@datatype,\@defined@specialform}
+% all additional types of LISP on TeX
+\def\@defined@additional@type{}
+
+
+%%%%%MEMO
+% \@tlabel@xxx -> \@eval@xxx\@apply@xxx\@@apply@xxx
+% \@dummy => ;
+% \@eval@xxx#1#2#3 : #1 = cont, #2 = env, #3 = reg
+%%%%%
+\def\@@select@eval#1#2#3{#1}
+\def\@@select@apply#1#2#3{#2}
+\def\@@select@@apply#1#2#3{#3}
+
+\def\@type@define@filed#1{\expandafter\@@type@define@filed\expandafter#1}
+\def\@@type@define@filed#1#2{\expandafter\@@@type@define@filed
+ \expandafter#1\expandafter#2}
+\def\@@@type@define@filed#1#2#3{\expandafter\@@@@type@define@field
+ \expandafter#1\expandafter#2\expandafter#3}
+\def\@@@@type@define@field#1#2#3#4{%
+ \def#1##1##2##3{\gdef##3{#4{##1}}}%
+ \def#2{\errmessage{LISP on TeX [internal apply]: cannot apply}}%
+ \def#3{\errmessage{LISP on TeX [function --- apply]: cannot apply}}%
+ \def#4{#1#2#3}}
+
+\@for\@lisp@on@tex@type:=\@defined@lisp@on@tex\do{%
+ \expandafter\@type@define@filed%
+ \csname @eval@\@lisp@on@tex@type\endcsname%
+ \csname @apply@\@lisp@on@tex@type\endcsname%
+ \csname @@apply@\@lisp@on@tex@type\endcsname%
+ \csname @tlabel@\@lisp@on@tex@type\endcsname}
+
+
+
+% write buffer. LISP on TeX's evaluator
+\gdef\@temp@write@buffer{}
+\def\@add@to@write@buffer#1#2{%
+ \expandafter\gdef\expandafter#1\expandafter{#1#2}}
+
+\def\lispeval#1#2{% #1 : \cs -> S-exp, #2 : target register
+ \gdef\@temp@write@buffer{}%
+ \@lisp@eval@save@envfalse
+ \expandafter\@eval#1{}#2%
+ \@temp@write@buffer}
+
+\newif\if@lisp@eval@save@env
+\def\@eval#1#2#3#4{%
+ \begingroup
+ % pin target S-exp object.
+ \if@lisp@eval@save@env
+ \@lisp@eval@save@envfalse
+ \expandafter\@lisp@gc@save@env@local
+ \else
+ \expandafter\@gobble
+ \fi{#3}%
+ \@lisp@gc@save@objects@local{#1{#2}}%
+ \expandafter\@@select@eval#1{#2}{#3}#4%
+ \endgroup}
+
+
+\def\@eval@symbol#1#2#3{% #1 symbol #2 current env #3 dist
+ \@lisp@env@get@value#3#1{#2}%
+ \expandafter\@eval@symbol@expand@mutable#3#3}
+\def\@eval@symbol@expand@mutable#1#2#3{%
+ \ifx#1\@tlabel@mutable\global\let#3#2\fi}
+
+\def\@@eval@get@typelabel#1#2{#1}
+\def\@eval@envcs#1#2#3#4{\expandafter\@@eval@envcs\expandafter{#1}#2{#3}#4}
+\def\@@eval@envcs#1#2#3#4{\@eval#2{#3}{#1}#4}
+
+\def\@eval@cons#1#2#3{\@@eval@cons#1{#2}#3}
+\def\@@eval@cons#1#2#3#4{%
+ \expandafter\@eval#1{#3}\@temp@i
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel\@temp@i\@tlabel@exception
+ \let\@@next\@@eval@cons@exception\else\let\@@next\@@eval@cons@normal\fi
+ \@@next{#2}{#3}{#4}}
+\def\@@eval@cons@normal#1#2#3{%
+ \expandafter\@lisp@gc@save@objects@local\expandafter{\@temp@i}% pin evaluated value of CAR
+ \def\@temp@ii{}% init
+ \expandafter\@flatten@args#1\@temp@ii % args are already pined, maybe...
+ %error-check of argument form
+ \ifx\@temp@ii\@@flatten@error@symbol
+ \let\@@next\@@eval@cons@args@error
+ \else
+ \let\@@next\@@eval@cons@apply
+ \fi
+ \@@next{#2}{#3}}
+\def\@@select@apply@pre{\expandafter\@@select@apply}
+\def\@@eval@cons@exception#1#2#3{\global\let#3\@temp@i}
+\def\@@eval@cons@apply#1#2{%
+ \expandafter\expandafter\expandafter\@@select@apply@pre
+ \expandafter\@temp@i\@temp@ii\@{#1}#2}
+\def\@@eval@cons@args@error#1#2{%
+ \gdef#2{\@tlabel@exception{%
+ {-1}{\@tlabel@string{Eval Improper List}}}}}
+
+\def\@flatten@args#1#2#3{%
+ \ifx#1\@tlabel@cons
+ \let\@flatten@next\@@flatten@next
+ \else\ifx#1\@tlabel@nil
+ \let\@flatten@next\@@flatten@fin
+ \else
+ \let\@flatten@next\@@flatten@error
+ \fi\fi
+ \@flatten@next#2#3}
+\def\@@flatten@next#1#2#3{%
+ \expandafter\expandafter\expandafter\def
+ \expandafter\expandafter\expandafter#3%
+ \expandafter\expandafter\expandafter{\expandafter#3#1}%
+ \expandafter\@flatten@args#2#3}
+\def\@@flatten@fin#1{}
+\def\@@flatten@error#1#2{\def#2{error}}
+\def\@@flatten@error@symbol{error}
+
+\def\@apply@lambda#1#2#3#4#5\@#6#7{%
+ \def\@lambda@args{}% init
+ \@create@bind#2{#3}\@lambda@args % convert arg list into internal form
+ \expandafter\@create@closure\@lambda@args\@{#6}#4{#5}#7}
+\def\@create@closure#1\@#2#3#4#5{\gdef#5{\@tlabel@closure{{#1}{#2}#3{#4}}}}
+
+\def\@create@bind#1#2#3{%
+ \ifx#1\@tlabel@symbol
+ \let\@create@bind@next\@@create@listarg
+ \else\ifx#1\@tlabel@cons
+ \let\@create@bind@next\@@create@normalarg
+ \else\ifx#1\@tlabel@nil
+ \let\@create@bind@next\@@create@finarg
+ \fi\fi\fi
+ \@create@bind@next#2#3}
+\def\@@create@listarg#1#2{\expandafter\def\expandafter#2\expandafter{#2:#1}}
+\def\@@create@normalarg#1#2#3{%
+ \expandafter\@@create@bind@symbol#1#3%
+ \expandafter\@create@bind#2#3}
+\def\@@create@bind@symbol\@tlabel@symbol#1#2{\expandafter\def\expandafter#2\expandafter{#2#1}}
+\def\@@create@finarg#1{\expandafter\def\expandafter#1\expandafter{#1:\@@unused}}
+\def\@@unused{\@@unused}
+
+\def\@apply@quote#1#2#3\@#4#5{\gdef#5{#2{#3}}}
+
+% application of define, defineM, defmacro.
+% #1 empty, #2{#3} 1st argument, #4{#5} 2nd argument,
+% #6 env, #7 cs
+\def\@apply@define#1#2#3#4#5\@#6#7{%
+ \let\@lisp@define@normal\@apply@define@normal
+ \@apply@define@{#1}#2{#3}#4{#5}\@{#6}#7}
+\def\@apply@mdefine#1#2#3#4#5\@#6#7{%
+ \let\@lisp@define@normal\@apply@mdefine@normal
+ \@apply@define@{#1}#2{#3}#4{#5}\@{#6}#7}
+\def\@apply@defmacro#1#2#3#4#5\@#6#7{%
+ \let\@lisp@define@normal\@apply@defmacro@normal
+ \@apply@define@{#1}#2{#3}#4{#5}\@{#6}#7}
+% type check
+\def\@apply@define@#1#2#3#4#5\@#6#7{%
+ \ifx#2\@tlabel@symbol % normal style
+ \let\@@next\@apply@define@@
+ \else\ifx#2\@tlabel@cons % function definition
+ \let\@@next\@apply@define@func
+ \else
+ \let\@@next\@apply@define@error
+ \fi\fi
+ \@@next{#1}#2{#3}#4{#5}\@{#6}#7}
+% \define's normal form
+\def\@apply@define@normal#1#2{%
+ \expandafter\@lisp@env@add@global\expandafter#1\expandafter{#2}%
+ \gdef#2{\@tlabel@nil{}}}
+% \defineM's normal form
+\def\@apply@mdefine@normal#1#2{%
+ \@lisp@gc@save@objects@local{#2}% pin the evaluated value
+ \@lisp@gc@alloc\@lisp@tmp@local@i
+ \expandafter\global\expandafter\let\@lisp@tmp@local@i#2%
+ \expandafter\@apply@mdefine@normal@\@lisp@tmp@local@i#1%
+ \gdef#2{\@tlabel@nil{}}}
+\def\@apply@mdefine@normal@#1#2{%
+ \@lisp@env@add@global#2{\@tlabel@mutable{#1}}}
+% \defmacro's normal form
+\def\@apply@defmacro@normal#1#2{%
+ \expandafter\@apply@defmacro@next\expandafter#1#2%
+ \gdef#2{\@tlabel@nil{}}}
+%TODO: error handling
+\def\@apply@defmacro@next#1\@tlabel@closure#2{%
+ \@lisp@env@add@global#1{\@tlabel@macro{#2}}}
+% normal form
+\def\@apply@define@@#1\@tlabel@symbol#2#3#4\@#5#6{%
+ \@eval#3{#4}{#5}#6% define does NOT use local environment
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#6\@tlabel@exception
+ \let\@@next\@apply@define@exception
+ \else\let\@@next\@lisp@define@normal\fi
+ \@@next{#2}{#6}}
+% function form
+\def\@apply@define@func#1\@tlabel@cons#2#3#4\@#5#6{%
+ \@apply@define@func@#2#3{#4}\@{#5}#6}
+\def\@apply@define@func@#1#2#3#4\@#5#6{%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#1\@tlabel@symbol
+ \let\@@next\@apply@define@func@@
+ \else
+ \let\@@next\@apply@define@error
+ \fi
+ \@@next#1#2#3{#4}\@{#5}#6}
+\def\@apply@define@func@@#1#2#3#4\@#5#6{% notice: no tmp token remain the closure
+ \def\@lisp@tmp@local@i{#3{#4}}% closure body
+ \def\@lisp@tmp@local@ii{\@tlabel@nil{}}%
+ \def\@lisp@tmp@local@iii{\@tlabel@cons{\@lisp@tmp@local@i\@lisp@tmp@local@ii}}%
+ \expandafter\def\expandafter\@lisp@tmp@local@iv\expandafter{#2}% formal arguments
+ \def\@lisp@tmp@local@v{\@tlabel@cons{\@lisp@tmp@local@iv\@lisp@tmp@local@iii}}%
+ \def\@lisp@tmp@local@vi{\@tlabel@lambda{}}% \lambda
+ \expandafter\@apply@define@@\expandafter{\expandafter}#1\@tlabel@cons{\@lisp@tmp@local@vi\@lisp@tmp@local@v}\@{#5}#6}
+% type error
+\def\@apply@define@error#1\@#2#3{%
+ \gdef#3{\@tlabel@exception{{-1}
+ {\@tlabel@string{The 1st argument of \define or \defineM must be a symbol or valid list.}}}}}
+% catch exception at evaluation
+\def\@apply@define@exception#1#2{}
+
+\def\@apply@setb#1#2#3#4#5\@#6#7{%
+ \ifx\@tlabel@symbol#2%
+ \let\@@next\@apply@setb@normal
+ \else
+ \let\@@next\@apply@setb@argtype@symbol@error
+ \fi
+ \@@next#3#4{#5}{#6}#7}
+\def\@apply@setb@normal#1#2#3#4#5{%
+ \@lisp@env@get@value#5#1{#4}%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#5\@tlabel@exception
+ \let\@@next\@apply@setb@raise@exception
+ \else\expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#5\@tlabel@mutable
+ \let\@@next\@apply@setb@normal@
+ \else
+ \let\@@next\@apply@setb@argtype@mutable@error
+ \fi\fi
+ \@@next#1#2{#3}{#4}#5}
+\def\@apply@setb@normal@#1#2#3#4#5{%
+ % the environment and the value is already pinned, maybe...
+ \let\@lisp@tmp@local@i#5%
+ \@eval#2{#3}{#4}#5%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#5\@tlabel@exception
+ \let\@@next\@apply@setb@raise@exception
+ \else
+ \let\@@next\@apply@setb@normal@@
+ \fi
+ \@@next#1#2{#3}{#4}#5}
+\def\@apply@setb@normal@@#1#2#3#4#5{%
+ \expandafter\@apply@setb@final\@lisp@tmp@local@i#5}
+\def\@apply@setb@final\@tlabel@mutable#1#2{%
+ \global\let#1#2%
+ \gdef#2{\@tlabel@nil{}}}
+
+\def\@apply@setb@argtype@symbol@error#1#2#3#4#5{%
+ \gdef#5{\@tlabel@exception{{-1}%
+ {\@tlabel@string{The 1st argument of \setB must be a symbol}}}}}
+\def\@apply@setb@raise@exception#1#2#3#4#5{}
+\def\@apply@setb@argtype@mutable@error#1#2#3#4#5{%
+ \gdef#5{\@tlabel@exception{{-1}%
+ {\@tlabel@string{The symbol #1 is not defined as a 'mutable'. %
+ Use \defineM, \letM, or \letrec.}}}}}
+
+%TODO: error handling
+\def\@apply@@let#1\@tlabel@symbol#2#3#4#5#6\@#7#8{%
+ \@eval#3{#4}{#7}#8%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#8\@tlabel@exception
+ \let\@@next\@apply@@let@exception
+ \else\let\@@next\@apply@@let@normal\fi
+ \@@next{#2}{#5}{#6}{#7}{#8}}
+\def\@apply@@let@normal#1#2#3#4#5{%
+ \expandafter\def\expandafter\@temp@env\expandafter{\expandafter#1\expandafter{#5}#4}%
+ \begingroup
+ \toks0\expandafter{\@temp@env}%
+ \toks1{#2{#3}#5}%
+ \toks2\expandafter{#5}%
+ \xdef\@@tco{%
+ \begingroup
+ \noexpand\@lisp@eval@save@envtrue
+ \noexpand\@lisp@gc@save@objects@local{\the\toks2}% pin appended value
+ \noexpand\@@eval@envcs{\the\toks0}\the\toks1
+ \endgroup}%
+ \endgroup
+ \aftergroup\@@tco}
+\def\@apply@@let@exception#1#2#3#4#5{} % #5 is already set the value of exception
+
+%TODO: error handling
+\def\@apply@@mlet#1\@tlabel@symbol#2#3#4#5#6\@#7#8{%
+ \@eval#3{#4}{#7}#8%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#8\@tlabel@exception
+ \let\@@next\@apply@@let@exception
+ \else\let\@@next\@apply@@mlet@normal\fi
+ \@@next{#2}{#5}{#6}{#7}{#8}}
+
+\def\@apply@@mlet@normal#1#2#3#4#5{%
+ \expandafter\@lisp@gc@save@objects@local\expandafter{#5}% pin target value
+ \@lisp@gc@alloc\@lisp@tmp@local@i
+ \expandafter\global\expandafter\let\@lisp@tmp@local@i#5%
+ \expandafter\@apply@@mlet@normal@\@lisp@tmp@local@i#1{#4}%
+ \begingroup
+ \toks0\expandafter{\@lisp@tmp@local@ii}%
+ \toks1{#2{#3}#5}%
+ \toks2\expandafter{\@lisp@tmp@local@i}%
+ \xdef\@@tco{%
+ \begingroup
+ \noexpand\@lisp@eval@save@envtrue
+ \noexpand\@lisp@gc@save@memory@local{\the\toks2}% pin the appended value
+ \noexpand\@@eval@envcs{\the\toks0}\the\toks1
+ \endgroup}%
+ \endgroup
+ \aftergroup\@@tco}
+\def\@apply@@mlet@normal@#1#2#3{%
+ \def\@lisp@tmp@local@ii{#2{\@tlabel@mutable{#1}}#3}}
+
+
+\def\@apply@if#1#2#3#4#5#6#7\@#8#9{%
+ \@eval#2{#3}{#8}#9%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#9\@tlabel@exception
+ \let\@@next\@apply@if@exception
+ \else\let\@@next\@apply@if@normal\fi
+ \@@next{#4}{#5}{#6}{#7}{#8}{#9}}
+\def\@apply@if@normal#1#2#3#4#5#6{%
+ \expandafter\@apply@if@next#6#1{#2}#3{#4}{#5}#6%
+ \aftergroup\@@tco}
+\def\@apply@if@exception#1#2#3#4#5#6{}
+
+% #1{#2} -> condition, #3{#4} -> then, #5{#6} -> else, #7 -> env, #8 ->cs
+\def\@apply@if@next#1#2#3#4#5#6#7#8{%
+ \ifx#1\@tlabel@bool
+ \ifx#2t%
+ \let\@@next\@apply@if@next@t
+ \else\ifx#2f%
+ \let\@@next\@apply@if@next@f
+ \else
+ \let\@@next\@apply@if@next@invalid
+ \fi\fi
+ \else
+ \let\@@next\@apply@if@next@type@error
+ \fi
+ \@@next#3{#4}#5{#6}{#7}#8}
+\def\@apply@if@next@t#1#2#3#4#5#6{\gdef\@@tco{\@eval#1{#2}{#5}#6}}
+\def\@apply@if@next@f#1#2#3#4#5#6{\gdef\@@tco{\@eval#3{#4}{#5}#6}}
+\def\@apply@if@next@invalid#1#2#3#4#5#6{%
+ \gdef\@@tco{\gdef#6{\@tlabel@exception{%
+ {-1}{\@tlabel@string{Fatal. Invalid Boolean}}}}}}
+\def\@apply@if@next@type@error#1#2#3#4#5#6{%
+ \gdef\@@tco{\gdef#6{\@tlabel@exception{%
+ {-1}{\@tlabel@string{The condition must be a bool.}}}}}}
+
+%% use general
+\def\@apply@eval@args#1#2#3#4{%
+ \let\@@next@pre\@apply@eval@args@pre@default
+ \ifx#3\relax
+ \let\@@next\@@next@fin
+ \else
+ \let\@@next@pre\@apply@eval@args@pre@i
+ \fi\@@next@pre{#1}{#2}{#3}{#4}\@@next#1{#2}}
+\def\@apply@eval@args@pre@default#1#2#3#4{}
+\def\@apply@eval@args@pre@i#1#2#3#4{%
+ \let\@@save#1%
+ \@eval#3{#4}{#2}#1%
+ \expandafter\@lisp@gc@save@objects@local\expandafter{#1}% pin evaluated value
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#1\@tlabel@exception
+ \let\@@next\@apply@eval@args@exception
+ \else\let\@@next\@apply@eval@args@normal\fi
+ \@@next{#1}}
+\def\@apply@eval@args@normal#1{%
+ \expandafter\expandafter\expandafter\def
+ \expandafter\expandafter\expandafter#1\expandafter\expandafter\expandafter{%
+ \expandafter\@@save#1}%
+ \let\@@next\@apply@eval@args}
+\def\@apply@eval@args@exception#1{\let\@@next\@apply@eval@args@ignore}
+
+\def\@@next@fin#1#2{}
+\def\@apply@eval@args@ignore#1#2#3\relax#4{}
+
+\def\@apply@get@args@head@typelabel#1#2\relax{#1}
+
+\def\@apply@func#1#2\@#3#4{%
+ \def\@temp@i{}%
+ \def\@@dummy@temp@i{}%
+ \@lisp@gc@save@objects@local{\@tlabel@func{#1}}% pin function
+ \@apply@eval@args\@temp@i{#3}#2\relax\relax
+ \ifx\@@dummy@temp@i\@temp@i%
+ \def\@@test@temp@i{\@tlabel@dummy{}}%
+ \else
+ \let\@@test@temp@i\@temp@i
+ \fi
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@apply@get@args@head@typelabel\@@test@temp@i\relax\@tlabel@exception
+ \let\@@next\@apply@func@exception
+ \else
+ \let\@@next\@apply@func@normal
+ \fi
+ \@@next{#1}{#3}{#4}}
+\def\@apply@func@normal#1#2#3{%
+ \expandafter\@apply@func@next\expandafter{\@temp@i}{#1}{#2}#3}
+\def\@apply@func@next#1#2#3#4{\@@apply@func{#2}#1\@{#3}#4}
+\def\@apply@func@exception#1#2#3{%
+ \expandafter\gdef\expandafter#3\expandafter{\@temp@i}}
+
+\def\@@apply@func#1#2\@#3#4{%
+ \gdef\@@tco{%
+ % pin arguments because this stack will be fold.
+ \begingroup
+ \@lisp@gc@save@objects@local{#2}%
+ #1#4#2\relax\relax
+ \endgroup}\aftergroup\@@tco}
+
+\def\@apply@envfunc#1#2\@#3#4{%
+ \def\@temp@i{}%
+ \def\@@dummy@temp@i{}%
+ \@lisp@gc@save@objects@local{\@tlabel@envfunc{#1}}% pin function
+ \@apply@eval@args\@temp@i{#3}#2\relax\relax
+ \ifx\@temp@i\@@dummy@temp@i%
+ \def\@@test@temp@i{\@tlabel@dummy{}}%
+ \else
+ \let\@@test@temp@i\@temp@i
+ \fi
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@apply@get@args@head@typelabel\@@test@temp@i\relax\@tlabel@exception
+ \let\@@next\@apply@func@exception
+ \else
+ \let\@@next\@apply@envfunc@normal
+ \fi
+ \@@next{#1}{#3}{#4}}
+\def\@apply@envfunc@normal#1#2#3{%
+ \expandafter\@apply@envfunc@next\expandafter{\@temp@i}{#1}{#2}#3}
+\def\@apply@envfunc@next#1#2#3#4{\@@apply@envfunc{#2}#1\@{#3}#4}
+
+\def\@@apply@envfunc#1#2\@#3#4{%
+ \gdef\@@tco{%
+ \begingroup
+ \@lisp@gc@save@objects@local{#2}%
+ \@lisp@eval@save@envtrue%
+ #1#4{#3}#2\relax\relax
+ \endgroup}\aftergroup\@@tco}
+
+
+\def\@apply@closure#1#2\@#3#4{%
+ \def\@temp@i{}%
+ \def\@@dummy@temp@i{}%
+ \@lisp@gc@save@objects@local{\@tlabel@closure{#1}}% pin closure
+ \@lisp@gc@save@objects@local{#2}% pin arguments
+ \@apply@eval@args\@temp@i{#3}#2\relax\relax
+ \ifx\@temp@i\@@dummy@temp@i%
+ \def\@@test@temp@i{\@tlabel@dummy{}}%
+ \else
+ \let\@@test@temp@i\@temp@i
+ \fi
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@apply@get@args@head@typelabel\@@test@temp@i\relax\@tlabel@exception
+ \let\@@next\@apply@func@exception
+ \else
+ \let\@@next\@apply@closure@normal
+ \fi
+ \@@next{#1}{#3}{#4}}
+\def\@apply@closure@normal#1#2#3{%
+ \expandafter\@apply@closure@next\expandafter{\@temp@i}{#1}{#2}#3}
+\def\@apply@closure@next#1#2#3#4{\@@apply@closure{#2}#1\@{#3}#4}
+
+\def\@@apply@closure#1#2\@#3#4{\@@apply@closure@next#1#2\@#4}
+\def\@@apply@closure@next#1#2#3#4#5\@#6{%
+ \def\@temp@env{}%
+ \@lisp@gc@save@objects@local{#5}% pin arguments
+ \@@apply@create@env\@temp@env#1#5\relax\relax
+ \begingroup
+ \toks0\expandafter{\@temp@env#2}%
+ \toks1{#3{#4}#6}%
+ \xdef\@@tco{%
+ \noexpand\@lisp@eval@save@envtrue
+ \noexpand\@@eval@envcs{\the\toks0}\the\toks1}%
+ \endgroup
+ \aftergroup\@@tco}
+
+% create an environment using in evaluation of closure's body.
+% It set a control sequence (#1) to the created environment locally.
+% #2 is formal arguments and #3 is rest argument. If there is
+% no rest argument, #3 is \@@unused. If an error occurs, it set
+% #1 to \relax by \let.
+\def\@@apply@create@env#1#2:#3{%
+ \def#1{}% initialize
+ \@@apply@create@env@#1{#2}{#3}}
+\def\@@apply@create@env@#1#2#3{%
+ \ifx!#2!% If #2 is empty
+ \ifx#3\@@unused
+ \let\@@next\@@apply@create@env@bind@fin
+ \else
+ \let\@@next\@@apply@create@env@bind@rest
+ \fi
+ \else
+ \let\@@next\@@apply@create@env@bind@one
+ \fi
+ \@@next#1#2|{#3}}
+% end of environment creation. If there are some
+% remaining arguments, it cause error.
+\def\@@apply@create@env@bind@fin#1|#2#3#4{%
+ \ifx#3\relax\else
+ \let#1\relax
+ \def\@@apply@create@env@error@message{Too more arguments}%
+ \fi}
+% bind a formal argument. #5{#6} is an real argument
+% of closure/macro call. If there is no unbound argument,
+% #5 and #6 are \relax (see caller).
+\def\@@apply@create@env@bind@one#1#2#3|#4#5#6{%
+ \ifx#5\relax
+ \let\@@next\@@apply@create@env@bind@one@error
+ \else
+ \let\@@next\@@apply@create@env@bind@one@
+ \fi
+ \@@next#1#2{#3}{#4}{#5{#6}}}
+\def\@@apply@create@env@bind@one@#1#2#3#4#5{%
+ \expandafter\def\expandafter#1\expandafter{#1#2{#5}}%
+ \@@apply@create@env@#1{#3}{#4}}
+\def\@@apply@create@env@bind@one@error#1#2#3#4#5{%
+ \def\@@apply@create@env@error@message{Too few arguments}%
+ \let#1\relax}
+% bind a rest argument (#2).
+\def\@@apply@create@env@bind@rest#1|#2{%
+ \def\@lisp@tmp@local@i{#1#2}%
+ \def\@lisp@tmp@local@ii{}%
+ \@@apply@create@env@bind@rest@}
+% get all remaining arguments and create a list.
+% it set the result to \@lisp@tmp@local@iii
+\def\@@apply@create@env@bind@rest@#1#2{%
+ \ifx#1\relax
+ \let\@@next\@@apply@create@env@bind@rest@create@cell
+ \else
+ \let\@@next\@@apply@create@env@bind@rest@reverse
+ \fi
+ \@@next#1{#2}}
+% reverse the arguments' order and set it to \@lisp@tmp@local@ii
+\def\@@apply@create@env@bind@rest@reverse#1#2{%
+ \toks0{#1{#2}}%
+ \toks2\expandafter{\@lisp@tmp@local@ii}%
+ \edef\@lisp@tmp@local@ii{\the\toks0 \the\toks2}%
+ \@@apply@create@env@bind@rest@}
+% create a list
+\def\@@apply@create@env@bind@rest@create@cell#1#2{% #1 and #2 is dummy
+ \def\@lisp@tmp@local@iii{\@tlabel@nil{}}%
+ \ifx\@lisp@tmp@local@ii\@empty%if there is no argument
+ \let\@@next\@@apply@create@env@bind@rest@create@cell@fin
+ \else
+ \let\@@next\@@apply@create@env@bind@rest@create@cell@alloc
+ \fi
+ \expandafter\@@next\@lisp@tmp@local@ii\relax\relax}
+\def\@@apply@create@env@bind@rest@create@cell@fin{%
+ \expandafter\@@apply@create@env@bind@rest@create@cell@fin@\@lisp@tmp@local@i}
+\def\@@apply@create@env@bind@rest@create@cell@fin@#1#2{%
+ \expandafter\@@apply@create@env@bind@rest@create@cell@fin@@
+ \expandafter{\@lisp@tmp@local@iii}#1#2}
+\def\@@apply@create@env@bind@rest@create@cell@fin@@#1#2#3{%
+ \expandafter\def\expandafter#2\expandafter{#2#3{#1}}}
+\def\@@apply@create@env@bind@rest@create@cell@alloc#1#2#3#4{%
+ \@lisp@gc@alloc\@lisp@tmp@local@iv % CAR
+ \expandafter\gdef\@lisp@tmp@local@iv{#1{#2}}%
+ \@lisp@gc@alloc\@lisp@tmp@local@v % CDR
+ \expandafter\global\expandafter\let\@lisp@tmp@local@v\@lisp@tmp@local@iii
+ \toks0\expandafter{\@lisp@tmp@local@iv}%
+ \toks2\expandafter{\@lisp@tmp@local@v}%
+ \edef\@lisp@tmp@local@iii{\noexpand\@tlabel@cons{\the\toks0 \the\toks2}}%
+ \ifx#3\relax
+ \let\@@next\@@apply@create@env@bind@rest@create@cell@fin
+ \else
+ \let\@@next\@@apply@create@env@bind@rest@create@cell@alloc
+ \fi
+ \@@next#3{#4}}
+
+% koko, @apply to @@apply wo irekaeru beki?
+\def\@@apply@macro#1#2\@#3#4{\@@apply@macro@next#1#2\@{#3}#4}
+\def\@@apply@macro@next#1#2#3#4#5\@#6#7{%
+ \def\@temp@env{}%
+ \@lisp@gc@save@objects@local{#5}% pin arguments
+ \@@apply@create@env\@temp@env#1#5\relax\relax
+ \begingroup
+ \toks0\expandafter{\@temp@env#2}%
+ \toks1{#3{#4}#7}%
+ \toks2{#7{#6}#7}%
+ \xdef\@@tco{%
+ \noexpand\@@eval@envcs{\the\toks0}\the\toks1
+ \noexpand\expandafter\noexpand\@eval\the\toks2}%
+ \endgroup
+ \aftergroup\@@tco}
+\let\@apply@macro\@@apply@macro
+
+\def\@apply@macroexpand#1#2#3\@#4#5{% cont, args, \@ env, reg
+ \let\@@apply@macro@next\@@apply@macro@next@expand@only
+ \@lisp@eval@save@envtrue
+ \@eval#2{#3}{#4}#5%
+ \expandafter\@lisp@gc@save@objects@local\expandafter{#5}% pin tmp result
+ \expandafter\@eval#5{#4}#5}
+\def\@@apply@macro@next@expand@only#1#2#3#4#5\@#6#7{%
+ \def\@temp@env{}%
+ \@lisp@gc@save@objects@local{#5}% pin args
+ \@@apply@create@env\@temp@env#1#5\relax\relax
+ \begingroup
+ \toks0\expandafter{\@temp@env#2}%
+ \toks1{#3{#4}#7}%
+ \toks2\expandafter{#7{#6}#7}%
+ \xdef\@@tco{%
+ \begingroup
+ \noexpand\@lisp@eval@save@envtrue
+ \noexpand\@@eval@envcs{\the\toks0}\the\toks1
+ \noexpand\@lisp@eval@save@envtrue
+ \noexpand\@eval@if@macro\the\toks2
+ \endgroup}%
+ \endgroup
+ \aftergroup\@@tco}
+\def\@eval@if@macro#1#2#3#4{%
+ \ifx#1\@tlabel@cons\@eval@if@macro@#2{#3}#4\fi}
+\def\@eval@if@macro@#1#2#3#4{%
+ \expandafter\@eval#1{#3}\@temp@i
+ \expandafter\@eval@if@macro@@\@temp@i#1#2{#3}#4}
+\def\@eval@if@macro@@#1#2#3#4#5#6{%
+ \ifx#1\@tlabel@macro\@eval\@tlabel@cons{#3#4}{#5}#6\else\gdef#6{\@tlabel@cons{#3#4}}\fi}
+
+
+\def\@apply@begin#1#2\@#3#4{%
+ \@apply@begin@next#4{#3}#2\relax\relax}
+\def\@apply@begin@next#1#2#3#4#5#6{%
+ \@lisp@gc@save@env@local{#2}% pin environment
+ \ifx#5\relax
+ \let\@@next\@apply@begin@final
+ \else
+ \let\@@next\@apply@begin@cont
+ \fi\@@next{#1}{#2}{#3}{#4}{#5}{#6}}
+\def\@apply@begin@final#1#2#3#4#5#6{%
+ \gdef\@@tco{%
+ \@lisp@eval@save@envtrue
+ \@eval#3{#4}{#2}#1}%
+ \aftergroup\@@tco}
+\def\@apply@begin@cont#1#2#3#4#5#6{%
+ \@eval#3{#4}{#2}#1%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#1\@tlabel@exception
+ \let\@@next\@apply@begin@exception
+ \else
+ \let\@@next\@apply@begin@normal
+ \fi
+ \@@next{#1}{#2}{#5}{#6}}
+\def\@apply@begin@normal#1#2#3#4{%
+ \@apply@begin@next#1{#2}#3{#4}}
+\def\@apply@begin@exception#1#2#3#4{}
+
+\newcount\c@lisp@continuation@label
+\def\@apply@callocc#1#2#3\@#4#5{%
+ \advance\c@lisp@continuation@label 1
+ \@lisp@gc@save@env@local{#4}%
+ \@eval#2{#3}{#4}#5%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#5\@tlabel@exception
+ \let\@@next\@apply@callocc@exception
+ \else
+ \let\@@next\@apply@callocc@normal
+ \fi
+ \@@next{#1}{#2}{#3}{#4}{#5}}
+\def\@apply@callocc@exception#1#2#3#4#5{}
+\def\@apply@callocc@normal#1#2#3#4#5{%
+ \toks0\expandafter{#5}%
+ \edef\@@next{\noexpand\begingroup
+ \noexpand\expandafter\noexpand\@@select@apply\the\toks0
+ \noexpand\@tlabel@continuation{\the\c@lisp@continuation@label}}%
+ \@@next\@{#4}#5\endgroup
+ \expandafter\@apply@callocc@check#5#5}
+\def\@apply@callocc@check#1#2#3{%
+ \ifx#1\@tlabel@exception
+ \let\@@next\@apply@callocc@check@contlabel
+ \else
+ \let\@@next\@apply@callocc@gobble
+ \fi
+ \@@next#2\@#3}
+\def\@apply@callocc@gobble#1\@#2{}
+\def\@apply@callocc@check@contlabel#1#2\@#3{%
+ \ifnum#1=\c@lisp@continuation@label
+ \let\@@next\@apply@callocc@hook
+ \else
+ \let\@@next\@apply@callocc@gobble@after@check
+ \fi
+ \@@next#3{#2}}
+\def\@apply@callocc@gobble@after@check#1#2{}
+\def\@apply@callocc@hook#1#2{\gdef#1{#2}}
+
+\def\@apply@continuation#1#2#3\@#4#5{%
+ \@lisp@gc@save@env@local{#4}%
+ \@eval#2{#3}{#4}#5%
+ \expandafter\expandafter\expandafter\ifx
+ \expandafter\@@eval@get@typelabel#5\@tlabel@exception
+ \let\@@next\@apply@continuation@exception
+ \else
+ \let\@@next\@apply@continuation@normal
+ \fi
+ \@@next{#1}{#2}{#3}{#4}{#5}}
+\def\@apply@continuation@normal#1#2#3#4#5{%
+ \toks0\expandafter{#5}%
+ \xdef#5{\noexpand\@tlabel@exception{{#1}{\the\toks0}}}}
+
+% check #function's arguments
+%#1 #args(*, +, or n), #2 function body
+%#3 reg, #4 args
+\newcount\@lisp@func@heck@args@count
+\def\@lisp@func@check@args#1#2#3#4\relax{%
+ \@lisp@func@heck@args@count0
+ \@lisp@func@check@args@#4\relax\relax
+ \def\@@next@true{#2#3#4\relax}%
+ \def\@@next@few{\gdef#3{%
+ \@tlabel@exception{{-1}%
+ {\@tlabel@string{too few arguments}}}}}%
+ \def\@@next@many{\gdef#3{%
+ \@tlabel@exception{{-1}%
+ {\@tlabel@string{too many arguments}}}}}%
+ \ifx#1*%
+ \let\@@next\@@next@true
+ \else\ifx#1+%
+ \ifnum\@lisp@func@heck@args@count>0
+ \let\@@next\@@next@true
+ \else
+ \let\@@next\@@next@few
+ \fi
+ \else
+ \ifnum\@lisp@func@heck@args@count=#1
+ \let\@@next\@@next@true
+ \else\ifnum\@lisp@func@heck@args@count>#1
+ \let\@@next\@@next@many
+ \else
+ \let\@@next\@@next@few
+ \fi\fi
+ \fi\fi
+ \@@next}
+\def\@lisp@func@check@args@#1#2{%
+ \ifx#1\relax
+ \let\@@next\relax
+ \else
+ \advance\@lisp@func@heck@args@count1
+ \let\@@next\@lisp@func@check@args@
+ \fi
+ \@@next}
+
+% select GC engine
+\ifcase\@lisp@gc@style
+ \expandafter\RequirePackage\expandafter[\@lisp@opt@gc]{lisp-simple-alloc} \or
+ \expandafter\RequirePackage\expandafter[\@lisp@opt@gc]{lisp-gc} \fi
+\@lisp@gc@init
+
+% reader
+\RequirePackage{lisp-read}
+
+%arithmetical functions
+\RequirePackage{lisp-arith}
+
+%string manipulation
+\RequirePackage{lisp-string}
+
+% connection to TeX world
+\RequirePackage{lisp-latexutil}
+
+% other primitive functions
+\RequirePackage{lisp-prim}
+
+% functions defined by LISP codes
+\RequirePackage{lisp-util}
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-prim.sty b/macros/latex/contrib/lisp-on-tex/lisp-prim.sty
new file mode 100644
index 0000000000..4f921612d8
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-prim.sty
@@ -0,0 +1,176 @@
+%% Syntax functions
+\@lisp@env@add@global\quote{\@tlabel@quote{}}%
+\@lisp@env@add@global\lambda{\@tlabel@lambda{}}
+\@lisp@env@add@global\define{\@tlabel@define{}}
+\@lisp@env@add@global\lispif{\@tlabel@if{}}
+\@lisp@env@add@global\defmacro{\@tlabel@defmacro{}}
+\@lisp@env@add@global\begin{\@tlabel@begin{}}
+\@lisp@env@add@global\defineM{\@tlabel@mdefine{}}
+\@lisp@env@add@global\setB{\@tlabel@setb{}}
+\@lisp@env@add@global\@let{\@tlabel@@let{}}
+\@lisp@env@add@global\@mlet{\@tlabel@@mlet{}}
+\@lisp@env@add@global\macroexpand{\@tlabel@macroexpand{}}
+\@lisp@env@add@global\callOCC{\@tlabel@callocc{}}
+
+\@lisp@env@add@global\car{\@tlabel@func{\@lisp@func@check@args{1}{\@lisp@perform@car}}}
+\@lisp@env@add@global\cdr{\@tlabel@func{\@lisp@func@check@args{1}{\@lisp@perform@cdr}}}
+\@lisp@env@add@global\cons{\@tlabel@func{\@lisp@func@check@args{2}{\@lisp@perform@cons}}}
+
+\newcount\@temp@count
+\@lisp@env@add@global\length{\@tlabel@func{\@lisp@func@check@args{1}{\@lisp@length}}}
+\def\@lisp@length{\@temp@count0\relax\@lisp@length@}
+\def\@lisp@length@#1#2#3{%
+ \ifx#2\@tlabel@nil
+ \expandafter\gdef\expandafter#1%
+ \expandafter{\expandafter\@tlabel@int\expandafter{\the\@temp@count}}%
+ \let\@length@next\@length@gobble
+ \else\ifx#2\@tlabel@cons
+ \advance\@temp@count1\relax
+ \let\@length@next\@@lisp@length
+ \else
+ \errmessage{LISP on TeX [function--length]: The Argument must be a LIST}%
+ \fi\fi\@length@next#3#1}
+\def\@length@gobble#1{}
+\def\@@lisp@length#1#2#3{\expandafter\@lisp@length@\expandafter#3#2}
+
+%equality
+\@lisp@env@add@global\={\@tlabel@func{\@lisp@func@check@args{2}{\@lisp@equal}}}
+\def\@lisp@equal#1#2#3#4#5{%
+ \gdef#1{\@tlabel@bool{f}}%
+ \ifx#2#4%
+ \def\@@temp@eqchecki{#3}%
+ \def\@@temp@eqcheckii{#5}%
+ \ifx\@@temp@eqchecki\@@temp@eqcheckii\gdef#1{\@tlabel@bool{t}}\fi
+ \fi}
+
+% printing function
+\@lisp@env@add@global\print{\@tlabel@func{\@lisp@func@check@args{1}{\@tex@print}}}
+\def\@tex@print#1{\gdef#1{\@tlabel@nil{}}\@tex@print@}
+\def\@tex@print@#1#2{%
+ \ifx#1\@tlabel@int
+ \@add@to@write@buffer\@temp@write@buffer{:#2}%
+ \else\ifx#1\@tlabel@symbol
+ \@add@to@write@buffer\@temp@write@buffer{\string#2}%
+ \else\ifx#1\@tlabel@string
+ \@add@to@write@buffer\@temp@write@buffer{'#2'}%
+ \else\ifx#1\@tlabel@bool
+ \@add@to@write@buffer\@temp@write@buffer{/#2}%
+ \else\ifx#1\@tlabel@dimen
+ \@add@to@write@buffer\@temp@write@buffer{!#2}%
+ \else\ifx#1\@tlabel@skip
+ \@add@to@write@buffer\@temp@write@buffer{@#2}%
+ \else\ifx#1\@tlabel@nil
+ \@add@to@write@buffer\@temp@write@buffer{()}%
+ \else\ifx#1\@tlabel@cons
+ \@add@to@write@buffer\@temp@write@buffer{(}%
+ \@tex@print@cell#2%
+ \else
+ \@add@to@write@buffer\@temp@write@buffer{[INNER]}%
+ \fi\fi\fi\fi\fi\fi\fi\fi}
+\def\@tex@print@cell#1#2{%
+ \expandafter\@tex@print@#1%
+ \expandafter\@tex@print@cdr#2}
+\def\@tex@print@cdr#1#2{%
+ \ifx#1\@tlabel@cons
+ \@add@to@write@buffer\@temp@write@buffer{ }%
+ \@tex@print@cell#2%
+ \else\ifx#1\@tlabel@nil
+ \@add@to@write@buffer\@temp@write@buffer{)}%
+ \else
+ \@add@to@write@buffer\@temp@write@buffer{.}%
+ \@tex@print@#1{#2}%
+ \@add@to@write@buffer\@temp@write@buffer{)}%
+ \fi\fi}
+
+
+
+\@lisp@env@add@global\texprint{\@tlabel@func{\@lisp@func@check@args{1}{\@tex@texprint}}}
+\def\@tex@texprint#1#2#3{%
+ \gdef#1{\@tlabel@nil{}}\@@tex@texprint#2{#3}}
+\def\@@tex@texprint#1#2{%
+ \ifx#1\@tlabel@string
+ \@add@to@write@buffer\@temp@write@buffer{#2}%
+ \else\ifx#1\@tlabel@int
+ \@add@to@write@buffer\@temp@write@buffer{#2}%
+ \else
+ \@add@to@write@buffer\@temp@write@buffer{INNER TOKEN}%
+ \fi\fi}
+
+\@lisp@env@add@global\stdprint{\@tlabel@func{\@lisp@func@check@args{1}{\@tex@stdprint}}}
+\def\@tex@stdprint#1#2#3{%
+ \gdef#1{\@tlabel@nil{}}%
+ \let\@save@write@buffer\@temp@write@buffer
+ \def\@temp@write@buffer{}%
+ \@tex@print@#2{#3}%
+ \toks0\expandafter{\@temp@write@buffer}%
+ \immediate\write16{\the\toks0}%
+ \let\@temp@write@buffer\@save@write@buffer}
+
+
+% Type predicates
+\def\@type@pred@add#1{\expandafter\@@type@pred@add\expandafter#1}
+\def\@@type@pred@add#1#2{\@lisp@env@add@global#1{\@tlabel@func{\@lisp@func@check@args{1}{#2}}}}
+\def\@type@pred@define#1{\expandafter\@@type@pred@define\expandafter#1}
+\def\@@type@pred@define#1#2{%
+ \def#1##1##2##3{\ifx##2#2\gdef##1{\@tlabel@bool{t}}\else\gdef##1{\@tlabel@bool{f}}\fi}}
+
+\@for\@lisp@on@tex@type:=\@defined@datatype\do{%
+ \expandafter\@type@pred@add\csname\@lisp@on@tex@type Q\endcsname
+ \csname @type@\@lisp@on@tex@type\endcsname
+ \expandafter\@type@pred@define\csname @type@\@lisp@on@tex@type\endcsname\csname @tlabel@\@lisp@on@tex@type\endcsname}
+
+\@lisp@env@add@global\pairQ{\@tlabel@func{\@type@cons}}
+\@lisp@env@add@global\booleanQ{\@tlabel@func{\@type@bool}}
+
+% eval , apply
+\@lisp@env@add@global\eval{\@tlabel@envfunc{\@extern@eval}}
+\def\@extern@eval#1#2#3#4{\@eval#3{#4}{#2}#1}
+\@lisp@env@add@global\apply{\@tlabel@envfunc{\@extern@apply}}
+\def\@extern@apply#1#2#3#4#5#6{% reg, env, labelfun, contfun, labellist, contlist
+ \def\@lisp@apply@temp{}% init
+ \@flatten@args#5{#6}\@lisp@apply@temp
+ \expandafter\@extern@apply@next\expandafter{\@lisp@apply@temp}#3{#4}{#2}#1}
+\def\@extern@apply@next#1#2#3#4#5{% arglist-flatten, labelfun, contfun env, reg
+ {\expandafter\@@select@@apply#2{#3}#1\@{#4}#5}}
+
+% values
+%\@lisp@env@add@global\values{\@tlabel@func{\@lisp@multivalues}}
+%\def\@lisp@multivalues#1#2\relax{\gdef#1{#2}}
+
+% read
+\@lisp@env@add@global\read{\@tlabel@func{\@lisp@func@check@args{0}{\@lisp@read@stdin}}}
+\def\@lisp@read@stdin#1{%
+ \read-1 to#1%
+ \def\@lisp@read@stdin@callback##1##2{%
+ \gdef#1{##1{##2}}}%
+ \expandafter\@lispread\expandafter\@lisp@read@stdin@callback#1}
+
+% create a list
+\@lisp@env@add@global\list{\@tlabel@func{\@lisp@func@check@args{*}{\@lisp@prim@list@pre}}}
+\def\@lisp@prim@list@pre#1{\gdef#1{}\@lisp@prim@list#1}
+\def\@lisp@prim@list#1#2#3{%
+ \ifx#2\relax
+ \let\@@next\@lisp@prim@list@fin
+ \else
+ \let\@@next\@lisp@prim@list@add
+ \fi\@@next#1#2{#3}}
+\def\@lisp@prim@list@add#1#2#3{%
+ \begingroup
+ \toks0\expandafter{#1}%
+ \toks1{#2{#3}}%
+ \xdef#1{\the\toks1 \the\toks0}%reverse arguments
+ \endgroup
+ \@lisp@prim@list#1}
+\def\@lisp@prim@list@fin#1#2#3{\expandafter\@lisp@prim@list@fin@\expandafter#1#1\relax\relax}
+\def\@lisp@prim@list@fin@#1{%
+ \gdef#1{\@tlabel@nil{}}%
+ \@lisp@prim@list@fin@@#1}
+\def\@lisp@prim@list@fin@@#1#2#3{%
+ \ifx#2\relax
+ \def\@@next##1##2##3{}%
+ \else
+ \let\@@next\@lisp@prim@list@fin@@@
+ \fi
+ \@@next#1#2{#3}}
+\def\@lisp@prim@list@fin@@@#1#2#3{\expandafter\@lisp@prim@list@fin@@@@#1#2{#3}#1}
+\def\@lisp@prim@list@fin@@@@#1#2#3#4#5{\@lisp@perform@cons#5#3{#4}#1{#2}\@lisp@prim@list@fin@@#5}
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-read.sty b/macros/latex/contrib/lisp-on-tex/lisp-read.sty
new file mode 100644
index 0000000000..bcfb7fd4fa
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-read.sty
@@ -0,0 +1,122 @@
+%%
+%% This is file `lisp-read.sty'.
+%%
+%% License: Modified BSD - see LICENSE file
+%%
+
+%% End Of Input(EOI)
+\def\@end@lispread{\@@end@lispread}
+
+%% Reader Macro
+%% @param #1 callback function (which gets a S-exp and do something)
+%% @param #2 the first letter of the input or EOI
+%% @return #1\@tlabel@xxx{contents}
+\def\@lispread#1#2{\begingroup\@lispread@main#1#2}
+
+%% The first branch
+%% @param #1 callback function
+%% @param #2 the first letter of the input or EOI
+\def\@lispread@main#1#2{% Define \@@next, the continuation.
+ \if\noexpand#2(% [Branch 1] CONS cell or NIL
+ \def\@@next{\@lispread@cell#1}%
+ \else\ifcat\noexpand#2\relax% [Branch 2] A control sequence or a control symbol
+ \ifx#2\@end@lispread % [Branch 2-1] EOI
+ \def\@@next{\endgroup#1%
+ \@tlabel@exception{{-1}{\@tlabel@string{!Found End of Input!}}}}%
+ \else
+ \def\@@next{\@lispread@symbol#1#2}% [Branch 2-2] Symbol
+ \fi
+ \else\if\noexpand#2'% [Branch 3] String
+ \def\@@next{\@lispread@string#1\@lispread@string@dummy}%
+ \else\if\noexpand#2/% [Branch 4] Boolean
+ \def\@@next{\@lispread@bool#1}%
+ \else\if\noexpand#2:% [Branch 5] Integer
+ \def\@@next{\@lispread@int#1}%
+ \else\if\noexpand#2!% [Branch 6] Dimension
+ \def\@@next{\@lispread@dimen#1}%
+ \else\if\noexpand#2@% [Branch 7] Skip
+ \def\@@next{\@lispread@skip#1}%
+ \else\if\noexpand#2+% [Branch 8] call a Reader Module
+ \def\@@next{\@lispread@module#1}%
+ \else % Otherwise -- parse error
+ \errmessage{LISP on teX [read]: no such type start with \noexpand#2}%
+ \fi\fi\fi\fi\fi\fi\fi\fi
+ \@@next}
+%% Symbol
+\def\@lispread@symbol#1#2{%
+ \endgroup#1\@tlabel@symbol{#2}}
+%% String
+\def\@lispread@string@dummy{} % to avoid consuming group
+\def\@lispread@string#1#2'{%
+ \endgroup\expandafter#1\expandafter\@tlabel@string\expandafter{#2}}
+%% Boolean
+\def\@lispread@bool#1#2{%
+ \endgroup#1\@tlabel@bool{#2}}
+%% Reader Module
+\def\@lispread@module#1#2{%
+ \@lispread@module@main\@register@lispread@module#2\@@end
+ \endgroup
+ \expandafter#1\@register@lispread@module}
+\def\@lispread@module@main#1#2::#3\@@end{\csname @mod@read@#2\endcsname#1{#3}}
+%% Integer
+\newcount\@cnt@lispread@int
+\def\@lispread@int#1{%
+ \gdef\@lispread@int@callback{\@lispread@int@main#1}%
+ \afterassignment\@lispread@int@callback
+ \global\@cnt@lispread@int}
+\def\@lispread@int@main#1{%
+ \endgroup\expandafter#1\expandafter\@tlabel@int\expandafter{\the\@cnt@lispread@int}}
+%% Skip
+\newskip\@cnt@lispread@skip
+\def\@lispread@skip#1{%
+ \gdef\@lispread@skip@callback{\@lispread@skip@main#1}%
+ \afterassignment\@lispread@skip@callback
+ \global\@cnt@lispread@skip}
+\def\@lispread@skip@main#1{%
+ \endgroup\expandafter#1\expandafter\@tlabel@skip\expandafter{\the\@cnt@lispread@skip}}
+%% Dimen
+\newdimen\@cnt@lispread@dimen
+\def\@lispread@dimen#1{%
+ \gdef\@lispread@dimen@callback{\@lispread@dimen@main#1}%
+ \afterassignment\@lispread@dimen@callback
+ \global\@cnt@lispread@dimen}
+\def\@lispread@dimen@main#1{%
+ \endgroup\expandafter#1\expandafter\@tlabel@dimen\expandafter{\the\@cnt@lispread@dimen}}
+%% CONS cell or NIL
+\def\@lispread@cell#1#2{%
+ \if\noexpand#2)% [Branch 1] NIL
+ \def\@@next{\endgroup#1\@tlabel@nil{}}%
+ \else % Otherwise CONS cell
+ \def\@@next{\@lispread@cell@car#1#2}%
+ \fi\@@next}
+%% first part of CONS cell : read CAR
+\def\@lispread@cell@car#1{%
+ \def\@lispread@car@reg##1##2{%
+ \def\@reg@lispread@car{##1{##2}}%
+ \@lispread@cell@dot#1}%
+ \@lispread\@lispread@car@reg}
+\def\@lispread@cell@dot#1#2{%
+ \if\noexpand#2.%
+ \def\@@next{%
+ \def\@lispread@cell@fincheck####1####2{%
+ \def\@reg@lispread@cdr{####1{####2}}%
+ \@lispread@fin#1}%
+ \@lispread\@lispread@cell@fincheck}% kokonaosu
+ \else
+ \def\@@next{\@lispread@cell@cdr#1(#2}%
+ \fi\@@next}
+\def\@lispread@cell@cdr#1{%
+ \def\@lispread@cdr@reg##1##2{%
+ \expandafter\@lisp@perform@cons@local\expandafter\@reg@tmp\@reg@lispread@car##1{##2}%
+ \expandafter\endgroup\expandafter#1\@reg@tmp}%
+ \@lispread\@lispread@cdr@reg}
+\def\@lispread@fin#1#2{%
+ \if\noexpand#2)%
+ \def\@@next{%
+ \expandafter\expandafter\expandafter\@lisp@perform@cons@local
+ \expandafter\expandafter\expandafter\@reg@tmp
+ \expandafter\@reg@lispread@car\@reg@lispread@cdr
+ \expandafter\endgroup\expandafter#1\@reg@tmp}%
+ \else
+ \def\@@next{\errmessage{LISP on teX [read]: missing )}}%
+ \fi\@@next}
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-simple-alloc.sty b/macros/latex/contrib/lisp-on-tex/lisp-simple-alloc.sty
new file mode 100644
index 0000000000..6213d0cb5e
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-simple-alloc.sty
@@ -0,0 +1,32 @@
+\newcount\@lisp@gc@simple@count
+
+% gc initializer (called by lisp-on-tex.sty)
+\def\@lisp@gc@init{\@lisp@gc@simple@count=0\relax}
+
+% allocator
+% The allocator define #1 to allocated the control sequence
+\def\@lisp@gc@alloc#1{%
+ \expandafter\def\expandafter#1\expandafter{%
+ \csname @lisp@gc@memory\the\@lisp@gc@simple@count\endcsname}%
+ \global\advance\@lisp@gc@simple@count1\relax}
+
+% GC main routine. The routine is called by the allocator.
+% If you want to call GC manually, you can do it.
+% In simple mode, this does not affects.
+\def\@lisp@gc@perform{}
+
+% pinning a object on global.
+% In simple mode, this does not affects.
+\def\@lisp@gc@pinning#1#2{}
+
+% pinning objects on local.
+% In simple mode, this does not affects.
+\def\@lisp@gc@save@objects@local#1{}
+
+% pinning memories on local.
+% In simple mode, this does not affects.
+\def\@lisp@gc@save@memory@local#1{}
+
+% pinning environmnt on local.
+% In simple mode, this does not affects.
+\def\@lisp@gc@save@env@local#1{} \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-string.sty b/macros/latex/contrib/lisp-on-tex/lisp-string.sty
new file mode 100644
index 0000000000..9c0c011a2c
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-string.sty
@@ -0,0 +1,76 @@
+%% CONCAT : string -> string -> string
+%% (\concat str1 str2) => str1 ++ str2
+\@lisp@env@add@global\concat{\@tlabel@func{\@lisp@func@check@args{+}{\def\@temp@str{}\@str@concat}}}
+\def\@str@concat#1#2#3{%
+ \let\@@next@str\relax
+ \ifx#2\@tlabel@string
+ \expandafter\def
+ \expandafter\@temp@str
+ \expandafter{\@temp@str#3}%
+ \let\@@next@str\@str@concat
+ \else\ifx#2\relax
+ \let\@@next@str\@str@fin
+ \else
+ \errmessage{LISP on TeX [function --- concat]:%
+ Arguments of concat must be strings}%
+ \fi\fi\@@next@str#1}
+\def\@str@fin#1{%
+ \expandafter\gdef\expandafter#1%
+ \expandafter{\expandafter\@tlabel@string\expandafter{\@temp@str}}}
+
+%% INT2STRING : int -> string
+%% (\intTOstring i) -> 'i'
+\@lisp@env@add@global\intTOstring{\@tlabel@func{\@lisp@func@check@args{1}{\@conv@intTOstr}}}
+\def\@conv@intTOstr#1\@tlabel@int#2{\gdef#1{\@tlabel@string{#2}}}
+
+%% IMMEDIATEWRITE : () -> ()
+%% It outputs the output buffer immediately.
+%% It cause error if the buffer is incomplete.
+\@lisp@env@add@global\immediatewrite{\@tlabel@func{\@lisp@func@check@args{0}{\@immediate@write}}}
+\def\@immediate@write#1{%
+ \gdef#1{\@tlabel@nil{}}%
+ \@temp@write@buffer\gdef\@temp@write@buffer{}}
+
+%% GROUP : string -> string
+%% (\group str) -> '{str}'
+\@lisp@env@add@global\group{\@tlabel@func{\@lisp@func@check@args{1}{\@text@group}}}
+\def\@text@group#1#2#3{%
+ \ifx#2\@tlabel@string
+ \gdef#1{\@tlabel@string{{#3}}}%
+ \else
+ \errmessage{LISP on TeX [function --- group]:%
+ The argument of group must be a string.}%
+ \fi}
+
+%% UNGROUP : string -> string
+%% (\group '{str}') -> 'str'
+\@lisp@env@add@global\ungroup{\@tlabel@func{\@lisp@func@check@args{1}{\@text@ungroup}}}
+\def\@text@ungroup#1#2#3{%
+ \ifx#2\@tlabel@string
+ \@@text@ungroup#1#3\@@end
+ \else
+ \errmessage{LISP on TeX [function --- ungroup]:%
+ The argument of ungroup must be a string.}%
+ \fi}
+\def\@@text@ungroup#1#2#3\@@end{%
+ \ifx;#3;%
+ \gdef#1{\@tlabel@string{#2}}%
+ \else
+ \errmessage{LISP on TeX [function --- ungroup]:%
+ The argument of ungroup must be a grouped string.}%
+ \fi}
+
+%% FGETS : () -> string
+%% (\fgets) -> read tokens from standard-in
+\@lisp@env@add@global\fgets{\@tlabel@func{\@lisp@func@check@args{0}{\@text@fgets}}}
+\def\@text@fgets#1{%
+\endlinechar=-1
+\read-1to#1%
+\expandafter\gdef\expandafter#1\expandafter{\expandafter\@tlabel@string\expandafter{#1}}}
+
+%% EXPAND : string -> string
+%% (\expand '<token>') -> '<token+>' where <token+> is expanded tokens of <token>
+\@lisp@env@add@global\expand{\@tlabel@func{\@lisp@func@check@args{1}{\@lot@text@expand}}}
+\def\@lot@text@expand#1\@tlabel@string#2{%
+ \edef\@temp@i{#2}%
+ \xdef#1{\noexpand\@tlabel@string{\@temp@i}}}
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-util.sty b/macros/latex/contrib/lisp-on-tex/lisp-util.sty
new file mode 100644
index 0000000000..17681f7f54
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-util.sty
@@ -0,0 +1,64 @@
+%%================================
+%% Definition of Utility Functions
+%%================================
+\makeatletter
+\lispinterp{%
+(\define \listQ (\lambda (\x) (\or (\pairQ \x) (\= () \x))))
+(\define \procedureQ (\lambda (\x)
+ (\or (\funcQ \x) (\closureQ \x) (\envfuncQ \x) (\macroQ \x))))
+(\define \atomQ (\lambda (\x)
+ (\and (\not (\pairQ \x)) (\not (\= () \x)) )))
+(\define \map (\lambda (\f.\args)
+ (\lispif (\= () (\car \args)) ()
+ (\let ((\cars (\@inner@map \car \args)) (\cdrs (\@inner@map \cdr \args)))
+ (\cons (\apply \f \cars)
+ (\apply \map (\cons \f \cdrs)))))))
+%% @inner@map : ('a -> 'b) -> 'a list -> 'b list
+(\define \@inner@map
+ (\lambda (\f \l)
+ (\lispif (\= () \l) () (\cons (\f (\car \l)) (\@inner@map \f (\cdr \l))))))
+%% not : bool -> bool
+(\define \not (\lambda (\b) (\lispif \b /f /t)))
+%% and : bool... -> bool
+(\define \and
+ (\lambda \l
+ (\lispif (\= () \l)
+ /t
+ (\lispif (\car \l) (\apply \and (\cdr \l)) /f))))
+%% or bool... -> bool
+(\define \or
+ (\lambda \l
+ (\lispif (\= () \l)
+ /f
+ (\lispif (\car \l) /t (\apply \or (\cdr \l))))))
+%% let bindings
+(\defmacro \let
+ (\lambda (\binds \body)
+ (\lispif (\= \binds ())
+ \body
+ (\list \let (\cdr \binds) (\list \@let (\car (\car \binds)) (\car (\cdr (\car \binds))) \body)))))
+(\defmacro \letM
+ (\lambda (\binds \body)
+ (\lispif (\= \binds ())
+ \body
+ (\list \letM (\cdr \binds) (\list \@mlet (\car (\car \binds)) (\car (\cdr (\car \binds))) \body)))))
+% letrec
+(\defmacro \letrec (\lambda (\binds \body) (\list \@letrec \binds \binds \body)))
+(\defmacro \@letrec
+ (\lambda (\binds \save \body)
+ (\lispif (\= \binds ())
+ (\list \@@letrec \save \body)
+ (\list \@letrec (\cdr \binds) \save
+ (\list \begin
+ (\list \setB (\car (\car \binds)) (\car (\cdr (\car \binds))))
+ \body)))))
+(\defmacro \@@letrec
+ (\lambda (\binds \body)
+ (\lispif (\= \binds ())
+ \body
+ (\list \@@letrec (\cdr \binds) (\list \@mlet (\car (\car \binds)) () \body)))))
+% nth
+(\define \nth (\lambda (\lst \n)
+ (\lispif (\= \n :0) (\car \lst) (\nth (\cdr \lst) (\- \n :1)))))
+}
+\makeatother \ No newline at end of file
diff --git a/macros/latex/contrib/lisp-on-tex/tug2013/slide.pdf b/macros/latex/contrib/lisp-on-tex/tug2013/slide.pdf
new file mode 100644
index 0000000000..5cd14ab14d
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/tug2013/slide.pdf
Binary files differ