summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lisp-on-tex/lisp-arith.sty
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/lisp-arith.sty
Initial commit
Diffstat (limited to 'macros/latex/contrib/lisp-on-tex/lisp-arith.sty')
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-arith.sty165
1 files changed, 165 insertions, 0 deletions
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}